Skip to content

Commit aadaa32

Browse files
authored
Rollup merge of rust-lang#128356 - Oneirical:real-estate-reaLTOr, r=jieyouxu
Migrate `cross-lang-lto-clang` and `cross-lang-lto-pgo-smoketest` `run-make` tests to rmake Part of rust-lang#121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html). This has the same problem outlined by rust-lang#126180, where the tests do not actually run as no test-running CI enviroment has `RUSTBUILD_FORCE_CLANG_BASED_TESTS` set. However, I still find it interesting to turn the Makefiles into the rmake format until the Clang issue is fixed. This should technically be tested on MSVC... if MSVC actually ran Clang tests.
2 parents 7e8b44e + b7e90fe commit aadaa32

File tree

8 files changed

+245
-5
lines changed

8 files changed

+245
-5
lines changed

src/tools/run-make-support/src/assertion_helpers.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::panic;
44
use std::path::Path;
55

6-
use crate::fs;
6+
use crate::{fs, regex};
77

88
/// Assert that `actual` is equal to `expected`.
99
#[track_caller]
@@ -47,6 +47,36 @@ pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N)
4747
}
4848
}
4949

50+
/// Assert that `haystack` contains the regex pattern `needle`.
51+
#[track_caller]
52+
pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
53+
let haystack = haystack.as_ref();
54+
let needle = needle.as_ref();
55+
let re = regex::Regex::new(needle).unwrap();
56+
if !re.is_match(haystack) {
57+
eprintln!("=== HAYSTACK ===");
58+
eprintln!("{}", haystack);
59+
eprintln!("=== NEEDLE ===");
60+
eprintln!("{}", needle);
61+
panic!("needle was not found in haystack");
62+
}
63+
}
64+
65+
/// Assert that `haystack` does not contain the regex pattern `needle`.
66+
#[track_caller]
67+
pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
68+
let haystack = haystack.as_ref();
69+
let needle = needle.as_ref();
70+
let re = regex::Regex::new(needle).unwrap();
71+
if re.is_match(haystack) {
72+
eprintln!("=== HAYSTACK ===");
73+
eprintln!("{}", haystack);
74+
eprintln!("=== NEEDLE ===");
75+
eprintln!("{}", needle);
76+
panic!("needle was unexpectedly found in haystack");
77+
}
78+
}
79+
5080
/// Assert that all files in `dir1` exist and have the same content in `dir2`
5181
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
5282
let dir2 = dir2.as_ref();

src/tools/run-make-support/src/command.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::{ffi, panic};
77
use build_helper::drop_bomb::DropBomb;
88

99
use crate::util::handle_failed_output;
10-
use crate::{assert_contains, assert_equals, assert_not_contains};
10+
use crate::{
11+
assert_contains, assert_contains_regex, assert_equals, assert_not_contains,
12+
assert_not_contains_regex,
13+
};
1114

1215
/// This is a custom command wrapper that simplifies working with commands and makes it easier to
1316
/// ensure that we check the exit status of executed processes.
@@ -191,13 +194,27 @@ impl CompletedProcess {
191194
self
192195
}
193196

197+
/// Checks that `stdout` does not contain the regex pattern `unexpected`.
198+
#[track_caller]
199+
pub fn assert_stdout_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
200+
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
201+
self
202+
}
203+
194204
/// Checks that `stdout` contains `expected`.
195205
#[track_caller]
196206
pub fn assert_stdout_contains<S: AsRef<str>>(&self, expected: S) -> &Self {
197207
assert_contains(&self.stdout_utf8(), expected);
198208
self
199209
}
200210

211+
/// Checks that `stdout` contains the regex pattern `expected`.
212+
#[track_caller]
213+
pub fn assert_stdout_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
214+
assert_contains_regex(&self.stdout_utf8(), expected);
215+
self
216+
}
217+
201218
/// Checks that trimmed `stderr` matches trimmed `expected`.
202219
#[track_caller]
203220
pub fn assert_stderr_equals<S: AsRef<str>>(&self, expected: S) -> &Self {
@@ -212,13 +229,27 @@ impl CompletedProcess {
212229
self
213230
}
214231

232+
/// Checks that `stderr` contains the regex pattern `expected`.
233+
#[track_caller]
234+
pub fn assert_stderr_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
235+
assert_contains_regex(&self.stderr_utf8(), expected);
236+
self
237+
}
238+
215239
/// Checks that `stderr` does not contain `unexpected`.
216240
#[track_caller]
217241
pub fn assert_stderr_not_contains<S: AsRef<str>>(&self, unexpected: S) -> &Self {
218242
assert_not_contains(&self.stdout_utf8(), unexpected);
219243
self
220244
}
221245

246+
/// Checks that `stderr` does not contain the regex pattern `unexpected`.
247+
#[track_caller]
248+
pub fn assert_stderr_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
249+
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
250+
self
251+
}
252+
222253
#[track_caller]
223254
pub fn assert_exit_code(&self, code: i32) -> &Self {
224255
assert!(self.output.status.code() == Some(code));

src/tools/run-make-support/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ pub use path_helpers::{
8585
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
8686

8787
pub use assertion_helpers::{
88-
assert_contains, assert_dirs_are_equal, assert_equals, assert_not_contains,
88+
assert_contains, assert_contains_regex, assert_dirs_are_equal, assert_equals,
89+
assert_not_contains, assert_not_contains_regex,
8990
};
9091

9192
pub use string::{

src/tools/tidy/src/allowed_run_make_makefiles.txt

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
run-make/branch-protection-check-IBT/Makefile
22
run-make/cat-and-grep-sanity-check/Makefile
33
run-make/cdylib-dylib-linkage/Makefile
4-
run-make/cross-lang-lto-clang/Makefile
5-
run-make/cross-lang-lto-pgo-smoketest/Makefile
64
run-make/cross-lang-lto-upstream-rlibs/Makefile
75
run-make/cross-lang-lto/Makefile
86
run-make/dep-info-doesnt-run-much/Makefile

tests/run-make/cross-lang-lto-clang/Makefile renamed to tests/run-make/cross-lang-lto-clang/_Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# FIXME(Oneirical): This is already implemented as an rmake.rs file, but due to #126180,
2+
# the rmake.rs is not ran on CI. Once the rmake test has been proven to work, remove this
3+
# Makefile.
4+
15
# needs-force-clang-based-tests
26

37
# This test makes sure that cross-language inlining actually works by checking
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This test checks that cross-language inlining actually works by checking
2+
// the generated machine code.
3+
// See https://github.com/rust-lang/rust/pull/57514
4+
5+
//@ needs-force-clang-based-tests
6+
// FIXME(#126180): This test doesn't actually run anywhere, because the only
7+
// CI job that sets RUSTBUILD_FORCE_CLANG_BASED_TESTS runs very few tests.
8+
9+
use run_make_support::{clang, env_var, llvm_ar, llvm_objdump, rustc, static_lib_name};
10+
11+
fn main() {
12+
rustc()
13+
.linker_plugin_lto("on")
14+
.output(static_lib_name("rustlib-xlto"))
15+
.opt_level("2")
16+
.codegen_units(1)
17+
.input("rustlib.rs")
18+
.run();
19+
clang()
20+
.lto("thin")
21+
.use_ld("lld")
22+
.arg("-lrustlib-xlto")
23+
.out_exe("cmain")
24+
.input("cmain.c")
25+
.arg("-O3")
26+
.run();
27+
// Make sure we don't find a call instruction to the function we expect to
28+
// always be inlined.
29+
llvm_objdump()
30+
.arg("-d")
31+
.input("cmain")
32+
.run()
33+
.assert_stdout_not_contains_regex("call.*rust_always_inlined");
34+
// As a sanity check, make sure we do find a call instruction to a
35+
// non-inlined function
36+
llvm_objdump()
37+
.arg("-d")
38+
.input("cmain")
39+
.run()
40+
.assert_stdout_contains_regex("call.*rust_never_inlined");
41+
clang().input("clib.c").lto("thin").arg("-c").out_exe("clib.o").arg("-O2").run();
42+
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
43+
rustc()
44+
.linker_plugin_lto("on")
45+
.opt_level("2")
46+
.linker(&env_var("CLANG"))
47+
.link_arg("-fuse-ld=lld")
48+
.input("main.rs")
49+
.output("rsmain")
50+
.run();
51+
llvm_objdump()
52+
.arg("-d")
53+
.input("rsmain")
54+
.run()
55+
.assert_stdout_not_contains_regex("call.*c_always_inlined");
56+
llvm_objdump()
57+
.arg("-d")
58+
.input("rsmain")
59+
.run()
60+
.assert_stdout_contains_regex("call.*c_never_inlined");
61+
}

tests/run-make/cross-lang-lto-pgo-smoketest/Makefile renamed to tests/run-make/cross-lang-lto-pgo-smoketest/_Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# FIXME(Oneirical): This is already implemented as an rmake.rs file, but due to #126180,
2+
# the rmake.rs is not ran on CI. Once the rmake test has been proven to work, remove this
3+
# Makefile.
4+
15
# needs-force-clang-based-tests
26

37
# FIXME(#126180): This test doesn't actually run anywhere, because the only
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// This test makes sure that cross-language inlining can be used in conjunction
2+
// with profile-guided optimization. The test only tests that the whole workflow
3+
// can be executed without anything crashing. It does not test whether PGO or
4+
// xLTO have any specific effect on the generated code.
5+
// See https://github.com/rust-lang/rust/pull/61036
6+
7+
//@ needs-force-clang-based-tests
8+
// FIXME(#126180): This test doesn't actually run anywhere, because the only
9+
// CI job that sets RUSTBUILD_FORCE_CLANG_BASED_TESTS runs very few tests.
10+
11+
//FIXME(Oneirical): There was a strange workaround for MSVC on this test
12+
// which added -C panic=abort to every RUSTC call. It was justified as follows:
13+
// LLVM doesn't support instrumenting binaries that use SEH:
14+
// https://bugs.llvm.org/show_bug.cgi?id=41279
15+
// Things work fine with -Cpanic=abort though.
16+
17+
use run_make_support::{
18+
clang, env_var, has_extension, has_prefix, llvm_ar, llvm_profdata, rfs, run, rustc,
19+
shallow_find_files, static_lib_name,
20+
};
21+
22+
fn main() {
23+
rustc()
24+
.linker_plugin_lto("on")
25+
.output(static_lib_name("rustlib-xlto"))
26+
.opt_level("3")
27+
.codegen_units(1)
28+
.input("rustlib.rs")
29+
.arg("-Cprofile-generate=cpp-profdata")
30+
.run();
31+
clang()
32+
.lto("thin")
33+
.arg("-fprofile-generate=cpp-profdata")
34+
.use_ld("lld")
35+
.arg("-lrustlib-xlto")
36+
.out_exe("cmain")
37+
.input("cmain.c")
38+
.arg("-O3")
39+
.run();
40+
run("cmain");
41+
// Postprocess the profiling data so it can be used by the compiler
42+
let profraw_files = shallow_find_files("cpp-profdata", |path| {
43+
has_prefix(path, "default") && has_extension(path, "profraw")
44+
});
45+
let profraw_file = profraw_files.get(0).unwrap();
46+
llvm_profdata().merge().output("cpp-profdata/merged.profdata").input(profraw_file).run();
47+
rustc()
48+
.linker_plugin_lto("on")
49+
.profile_use("cpp-profdata/merged.profdata")
50+
.output(static_lib_name("rustlib-xlto"))
51+
.opt_level("3")
52+
.codegen_units(1)
53+
.input("rustlib.rs")
54+
.run();
55+
clang()
56+
.lto("thin")
57+
.arg("-fprofile-use=cpp-profdata/merged.profdata")
58+
.use_ld("lld")
59+
.arg("-lrustlib-xlto")
60+
.out_exe("cmain")
61+
.input("cmain.c")
62+
.arg("-O3")
63+
.run();
64+
65+
clang()
66+
.input("clib.c")
67+
.arg("-fprofile-generate=rs-profdata")
68+
.lto("thin")
69+
.arg("-c")
70+
.out_exe("clib.o")
71+
.arg("-O3")
72+
.run();
73+
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
74+
rustc()
75+
.linker_plugin_lto("on")
76+
.opt_level("3")
77+
.codegen_units(1)
78+
.arg("-Cprofile-generate=rs-profdata")
79+
.linker(&env_var("CLANG"))
80+
.link_arg("-fuse-ld=lld")
81+
.input("main.rs")
82+
.output("rsmain")
83+
.run();
84+
run("rsmain");
85+
// Postprocess the profiling data so it can be used by the compiler
86+
let profraw_files = shallow_find_files("rs-profdata", |path| {
87+
has_prefix(path, "default") && has_extension(path, "profraw")
88+
});
89+
let profraw_file = profraw_files.get(0).unwrap();
90+
llvm_profdata().merge().output("rs-profdata/merged.profdata").input(profraw_file).run();
91+
clang()
92+
.input("clib.c")
93+
.arg("-fprofile-use=rs-profdata/merged.profdata")
94+
.arg("-c")
95+
.lto("thin")
96+
.out_exe("clib.o")
97+
.arg("-O3")
98+
.run();
99+
rfs::remove_file(static_lib_name("xyz"));
100+
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
101+
rustc()
102+
.linker_plugin_lto("on")
103+
.opt_level("3")
104+
.codegen_units(1)
105+
.arg("-Cprofile-use=rs-profdata/merged.profdata")
106+
.linker(&env_var("CLANG"))
107+
.link_arg("-fuse-ld=lld")
108+
.input("main.rs")
109+
.output("rsmain")
110+
.run();
111+
}

0 commit comments

Comments
 (0)