Skip to content

Commit

Permalink
Rollup merge of rust-lang#128356 - Oneirical:real-estate-reaLTOr, r=j…
Browse files Browse the repository at this point in the history
…ieyouxu

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.
  • Loading branch information
tgross35 authored Jul 31, 2024
2 parents 7e8b44e + b7e90fe commit aadaa32
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 5 deletions.
32 changes: 31 additions & 1 deletion src/tools/run-make-support/src/assertion_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::panic;
use std::path::Path;

use crate::fs;
use crate::{fs, regex};

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

/// Assert that `haystack` contains the regex pattern `needle`.
#[track_caller]
pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
let haystack = haystack.as_ref();
let needle = needle.as_ref();
let re = regex::Regex::new(needle).unwrap();
if !re.is_match(haystack) {
eprintln!("=== HAYSTACK ===");
eprintln!("{}", haystack);
eprintln!("=== NEEDLE ===");
eprintln!("{}", needle);
panic!("needle was not found in haystack");
}
}

/// Assert that `haystack` does not contain the regex pattern `needle`.
#[track_caller]
pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) {
let haystack = haystack.as_ref();
let needle = needle.as_ref();
let re = regex::Regex::new(needle).unwrap();
if re.is_match(haystack) {
eprintln!("=== HAYSTACK ===");
eprintln!("{}", haystack);
eprintln!("=== NEEDLE ===");
eprintln!("{}", needle);
panic!("needle was unexpectedly found in haystack");
}
}

/// Assert that all files in `dir1` exist and have the same content in `dir2`
pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
let dir2 = dir2.as_ref();
Expand Down
33 changes: 32 additions & 1 deletion src/tools/run-make-support/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{ffi, panic};
use build_helper::drop_bomb::DropBomb;

use crate::util::handle_failed_output;
use crate::{assert_contains, assert_equals, assert_not_contains};
use crate::{
assert_contains, assert_contains_regex, assert_equals, assert_not_contains,
assert_not_contains_regex,
};

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

/// Checks that `stdout` does not contain the regex pattern `unexpected`.
#[track_caller]
pub fn assert_stdout_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
self
}

/// Checks that `stdout` contains `expected`.
#[track_caller]
pub fn assert_stdout_contains<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains(&self.stdout_utf8(), expected);
self
}

/// Checks that `stdout` contains the regex pattern `expected`.
#[track_caller]
pub fn assert_stdout_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains_regex(&self.stdout_utf8(), expected);
self
}

/// Checks that trimmed `stderr` matches trimmed `expected`.
#[track_caller]
pub fn assert_stderr_equals<S: AsRef<str>>(&self, expected: S) -> &Self {
Expand All @@ -212,13 +229,27 @@ impl CompletedProcess {
self
}

/// Checks that `stderr` contains the regex pattern `expected`.
#[track_caller]
pub fn assert_stderr_contains_regex<S: AsRef<str>>(&self, expected: S) -> &Self {
assert_contains_regex(&self.stderr_utf8(), expected);
self
}

/// Checks that `stderr` does not contain `unexpected`.
#[track_caller]
pub fn assert_stderr_not_contains<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains(&self.stdout_utf8(), unexpected);
self
}

/// Checks that `stderr` does not contain the regex pattern `unexpected`.
#[track_caller]
pub fn assert_stderr_not_contains_regex<S: AsRef<str>>(&self, unexpected: S) -> &Self {
assert_not_contains_regex(&self.stdout_utf8(), unexpected);
self
}

#[track_caller]
pub fn assert_exit_code(&self, code: i32) -> &Self {
assert!(self.output.status.code() == Some(code));
Expand Down
3 changes: 2 additions & 1 deletion src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ pub use path_helpers::{
pub use scoped_run::{run_in_tmpdir, test_while_readonly};

pub use assertion_helpers::{
assert_contains, assert_dirs_are_equal, assert_equals, assert_not_contains,
assert_contains, assert_contains_regex, assert_dirs_are_equal, assert_equals,
assert_not_contains, assert_not_contains_regex,
};

pub use string::{
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
run-make/branch-protection-check-IBT/Makefile
run-make/cat-and-grep-sanity-check/Makefile
run-make/cdylib-dylib-linkage/Makefile
run-make/cross-lang-lto-clang/Makefile
run-make/cross-lang-lto-pgo-smoketest/Makefile
run-make/cross-lang-lto-upstream-rlibs/Makefile
run-make/cross-lang-lto/Makefile
run-make/dep-info-doesnt-run-much/Makefile
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# FIXME(Oneirical): This is already implemented as an rmake.rs file, but due to #126180,
# the rmake.rs is not ran on CI. Once the rmake test has been proven to work, remove this
# Makefile.

# needs-force-clang-based-tests

# This test makes sure that cross-language inlining actually works by checking
Expand Down
61 changes: 61 additions & 0 deletions tests/run-make/cross-lang-lto-clang/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// This test checks that cross-language inlining actually works by checking
// the generated machine code.
// See https://github.com/rust-lang/rust/pull/57514

//@ needs-force-clang-based-tests
// FIXME(#126180): This test doesn't actually run anywhere, because the only
// CI job that sets RUSTBUILD_FORCE_CLANG_BASED_TESTS runs very few tests.

use run_make_support::{clang, env_var, llvm_ar, llvm_objdump, rustc, static_lib_name};

fn main() {
rustc()
.linker_plugin_lto("on")
.output(static_lib_name("rustlib-xlto"))
.opt_level("2")
.codegen_units(1)
.input("rustlib.rs")
.run();
clang()
.lto("thin")
.use_ld("lld")
.arg("-lrustlib-xlto")
.out_exe("cmain")
.input("cmain.c")
.arg("-O3")
.run();
// Make sure we don't find a call instruction to the function we expect to
// always be inlined.
llvm_objdump()
.arg("-d")
.input("cmain")
.run()
.assert_stdout_not_contains_regex("call.*rust_always_inlined");
// As a sanity check, make sure we do find a call instruction to a
// non-inlined function
llvm_objdump()
.arg("-d")
.input("cmain")
.run()
.assert_stdout_contains_regex("call.*rust_never_inlined");
clang().input("clib.c").lto("thin").arg("-c").out_exe("clib.o").arg("-O2").run();
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
rustc()
.linker_plugin_lto("on")
.opt_level("2")
.linker(&env_var("CLANG"))
.link_arg("-fuse-ld=lld")
.input("main.rs")
.output("rsmain")
.run();
llvm_objdump()
.arg("-d")
.input("rsmain")
.run()
.assert_stdout_not_contains_regex("call.*c_always_inlined");
llvm_objdump()
.arg("-d")
.input("rsmain")
.run()
.assert_stdout_contains_regex("call.*c_never_inlined");
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# FIXME(Oneirical): This is already implemented as an rmake.rs file, but due to #126180,
# the rmake.rs is not ran on CI. Once the rmake test has been proven to work, remove this
# Makefile.

# needs-force-clang-based-tests

# FIXME(#126180): This test doesn't actually run anywhere, because the only
Expand Down
111 changes: 111 additions & 0 deletions tests/run-make/cross-lang-lto-pgo-smoketest/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This test makes sure that cross-language inlining can be used in conjunction
// with profile-guided optimization. The test only tests that the whole workflow
// can be executed without anything crashing. It does not test whether PGO or
// xLTO have any specific effect on the generated code.
// See https://github.com/rust-lang/rust/pull/61036

//@ needs-force-clang-based-tests
// FIXME(#126180): This test doesn't actually run anywhere, because the only
// CI job that sets RUSTBUILD_FORCE_CLANG_BASED_TESTS runs very few tests.

//FIXME(Oneirical): There was a strange workaround for MSVC on this test
// which added -C panic=abort to every RUSTC call. It was justified as follows:
// LLVM doesn't support instrumenting binaries that use SEH:
// https://bugs.llvm.org/show_bug.cgi?id=41279
// Things work fine with -Cpanic=abort though.

use run_make_support::{
clang, env_var, has_extension, has_prefix, llvm_ar, llvm_profdata, rfs, run, rustc,
shallow_find_files, static_lib_name,
};

fn main() {
rustc()
.linker_plugin_lto("on")
.output(static_lib_name("rustlib-xlto"))
.opt_level("3")
.codegen_units(1)
.input("rustlib.rs")
.arg("-Cprofile-generate=cpp-profdata")
.run();
clang()
.lto("thin")
.arg("-fprofile-generate=cpp-profdata")
.use_ld("lld")
.arg("-lrustlib-xlto")
.out_exe("cmain")
.input("cmain.c")
.arg("-O3")
.run();
run("cmain");
// Postprocess the profiling data so it can be used by the compiler
let profraw_files = shallow_find_files("cpp-profdata", |path| {
has_prefix(path, "default") && has_extension(path, "profraw")
});
let profraw_file = profraw_files.get(0).unwrap();
llvm_profdata().merge().output("cpp-profdata/merged.profdata").input(profraw_file).run();
rustc()
.linker_plugin_lto("on")
.profile_use("cpp-profdata/merged.profdata")
.output(static_lib_name("rustlib-xlto"))
.opt_level("3")
.codegen_units(1)
.input("rustlib.rs")
.run();
clang()
.lto("thin")
.arg("-fprofile-use=cpp-profdata/merged.profdata")
.use_ld("lld")
.arg("-lrustlib-xlto")
.out_exe("cmain")
.input("cmain.c")
.arg("-O3")
.run();

clang()
.input("clib.c")
.arg("-fprofile-generate=rs-profdata")
.lto("thin")
.arg("-c")
.out_exe("clib.o")
.arg("-O3")
.run();
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
rustc()
.linker_plugin_lto("on")
.opt_level("3")
.codegen_units(1)
.arg("-Cprofile-generate=rs-profdata")
.linker(&env_var("CLANG"))
.link_arg("-fuse-ld=lld")
.input("main.rs")
.output("rsmain")
.run();
run("rsmain");
// Postprocess the profiling data so it can be used by the compiler
let profraw_files = shallow_find_files("rs-profdata", |path| {
has_prefix(path, "default") && has_extension(path, "profraw")
});
let profraw_file = profraw_files.get(0).unwrap();
llvm_profdata().merge().output("rs-profdata/merged.profdata").input(profraw_file).run();
clang()
.input("clib.c")
.arg("-fprofile-use=rs-profdata/merged.profdata")
.arg("-c")
.lto("thin")
.out_exe("clib.o")
.arg("-O3")
.run();
rfs::remove_file(static_lib_name("xyz"));
llvm_ar().obj_to_ar().output_input(static_lib_name("xyz"), "clib.o").run();
rustc()
.linker_plugin_lto("on")
.opt_level("3")
.codegen_units(1)
.arg("-Cprofile-use=rs-profdata/merged.profdata")
.linker(&env_var("CLANG"))
.link_arg("-fuse-ld=lld")
.input("main.rs")
.output("rsmain")
.run();
}

0 comments on commit aadaa32

Please sign in to comment.