From bce0bfe44d1219a169dd0dd2e78f5674afbc25a3 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Thu, 13 Jun 2024 15:02:08 -0400 Subject: [PATCH] rewrite sepcomp-inlining and -separate to rmake.rs --- src/tools/run-make-support/src/lib.rs | 17 +++++++++++++++++ .../tidy/src/allowed_run_make_makefiles.txt | 2 -- tests/run-make/sepcomp-cci-copies/Makefile | 12 ------------ tests/run-make/sepcomp-cci-copies/rmake.rs | 13 +++++++++++++ tests/run-make/sepcomp-inlining/Makefile | 15 --------------- tests/run-make/sepcomp-inlining/rmake.rs | 19 +++++++++++++++++++ tests/run-make/sepcomp-separate/rmake.rs | 17 ++--------------- 7 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 tests/run-make/sepcomp-cci-copies/Makefile create mode 100644 tests/run-make/sepcomp-cci-copies/rmake.rs delete mode 100644 tests/run-make/sepcomp-inlining/Makefile create mode 100644 tests/run-make/sepcomp-inlining/rmake.rs diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 9e3f785669aeb..ac959c3278001 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -245,6 +245,23 @@ pub fn uname() -> String { output.stdout_utf8() } +/// Inside a glob pattern of files (paths), read their contents and count the +/// number of regex matches with a given expression (re). +#[track_caller] +pub fn count_regex_matches_in_file_glob(re: &str, paths: &str) -> usize { + let re = regex::Regex::new(re).expect(format!("Regex expression {re} is not valid.").as_str()); + let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str()); + use io::BufRead; + paths + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.as_path().is_file()) + .filter_map(|path| fs::File::open(&path).ok()) + .map(|file| io::BufReader::new(file)) + .flat_map(|reader| reader.lines().filter_map(|entry| entry.ok())) + .filter(|line| re.is_match(line)) + .count() +} + fn handle_failed_output(cmd: &Command, output: CompletedProcess, caller_line_number: u32) -> ! { if output.status().success() { eprintln!("command unexpectedly succeeded at line {caller_line_number}"); diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index d67199c6b98c1..497fbfaa541f0 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -208,8 +208,6 @@ run-make/sanitizer-dylib-link/Makefile run-make/sanitizer-staticlib-link/Makefile run-make/separate-link-fail/Makefile run-make/separate-link/Makefile -run-make/sepcomp-cci-copies/Makefile -run-make/sepcomp-inlining/Makefile run-make/share-generics-dylib/Makefile run-make/silly-file-names/Makefile run-make/simd-ffi/Makefile diff --git a/tests/run-make/sepcomp-cci-copies/Makefile b/tests/run-make/sepcomp-cci-copies/Makefile deleted file mode 100644 index df289d0b0b1a2..0000000000000 --- a/tests/run-make/sepcomp-cci-copies/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -include ../tools.mk - -# Check that cross-crate inlined items are inlined in all compilation units -# that refer to them, and not in any other compilation units. -# Note that we have to pass `-C codegen-units=6` because up to two CGUs may be -# created for each source module (see `rustc_const_eval::monomorphize::partitioning`). - -all: - $(RUSTC) cci_lib.rs - $(RUSTC) foo.rs --emit=llvm-ir -C codegen-units=6 \ - -Z inline-in-all-cgus - [ "$$(cat "$(TMPDIR)"/foo.*.ll | grep -c define\ .*cci_fn)" -eq "2" ] diff --git a/tests/run-make/sepcomp-cci-copies/rmake.rs b/tests/run-make/sepcomp-cci-copies/rmake.rs new file mode 100644 index 0000000000000..8d2411c1faec4 --- /dev/null +++ b/tests/run-make/sepcomp-cci-copies/rmake.rs @@ -0,0 +1,13 @@ +// Check that cross-crate inlined items are inlined in all compilation units +// that refer to them, and not in any other compilation units. +// Note that we have to pass `-C codegen-units=6` because up to two CGUs may be +// created for each source module (see `rustc_const_eval::monomorphize::partitioning`). +// See https://github.com/rust-lang/rust/pull/16367 + +use run_make_support::{count_regex_matches_in_file_glob, rustc}; + +fn main() { + rustc().input("cci_lib.rs").run(); + rustc().input("foo.rs").emit("llvm-ir").codegen_units(6).arg("-Zinline-in-all-cgus").run(); + assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*cci_fn"#, "foo.*.ll"), 2); +} diff --git a/tests/run-make/sepcomp-inlining/Makefile b/tests/run-make/sepcomp-inlining/Makefile deleted file mode 100644 index 327aeb75e5eda..0000000000000 --- a/tests/run-make/sepcomp-inlining/Makefile +++ /dev/null @@ -1,15 +0,0 @@ -include ../tools.mk - -# Test that #[inline] functions still get inlined across compilation unit -# boundaries. Compilation should produce three IR files, but only the two -# compilation units that have a usage of the #[inline] function should -# contain a definition. Also, the non-#[inline] function should be defined -# in only one compilation unit. - -all: - $(RUSTC) foo.rs --emit=llvm-ir -C codegen-units=3 \ - -Z inline-in-all-cgus - [ "$$(cat "$(TMPDIR)"/foo.*.ll | grep -c define\ i32\ .*inlined)" -eq "0" ] - [ "$$(cat "$(TMPDIR)"/foo.*.ll | grep -c define\ internal\ i32\ .*inlined)" -eq "2" ] - [ "$$(cat "$(TMPDIR)"/foo.*.ll | grep -c define\ hidden\ i32\ .*normal)" -eq "1" ] - [ "$$(cat "$(TMPDIR)"/foo.*.ll | grep -c declare\ hidden\ i32\ .*normal)" -eq "2" ] diff --git a/tests/run-make/sepcomp-inlining/rmake.rs b/tests/run-make/sepcomp-inlining/rmake.rs new file mode 100644 index 0000000000000..0315fd6997204 --- /dev/null +++ b/tests/run-make/sepcomp-inlining/rmake.rs @@ -0,0 +1,19 @@ +// Test that #[inline] functions still get inlined across compilation unit +// boundaries. Compilation should produce three IR files, but only the two +// compilation units that have a usage of the #[inline] function should +// contain a definition. Also, the non-#[inline] function should be defined +// in only one compilation unit. +// See https://github.com/rust-lang/rust/pull/16367 + +use run_make_support::{count_regex_matches_in_file_glob, glob, regex, rustc}; + +fn main() { + rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).arg("-Zinline-in-all-cgus").run(); + assert_eq!(count_regex_matches_in_file_glob(r#"define\ i32\ .*inlined"#, "foo.*.ll"), 0); + assert_eq!(count_regex_matches_in_file_glob(r#"define\ internal\ .*inlined"#, "foo.*.ll"), 2); + assert_eq!(count_regex_matches_in_file_glob(r#"define\ hidden\ i32\ .*normal"#, "foo.*.ll"), 1); + assert_eq!( + count_regex_matches_in_file_glob(r#"declare\ hidden\ i32\ .*normal"#, "foo.*.ll"), + 2 + ); +} diff --git a/tests/run-make/sepcomp-separate/rmake.rs b/tests/run-make/sepcomp-separate/rmake.rs index d3ef2450a847d..99ee221adc1b6 100644 --- a/tests/run-make/sepcomp-separate/rmake.rs +++ b/tests/run-make/sepcomp-separate/rmake.rs @@ -3,22 +3,9 @@ // wind up in three different compilation units. // See https://github.com/rust-lang/rust/pull/16367 -use run_make_support::{fs_wrapper, glob, regex, rustc}; -use std::io::{BufRead, BufReader}; +use run_make_support::{count_regex_matches_in_file_glob, rustc}; fn main() { - let mut match_count = 0; rustc().input("foo.rs").emit("llvm-ir").codegen_units(3).run(); - let re = regex::Regex::new(r#"define.*magic_fn"#).unwrap(); - let paths = glob::glob("foo.*.ll").unwrap(); - paths.filter_map(|entry| entry.ok()).filter(|path| path.is_file()).for_each(|path| { - let file = fs_wrapper::open_file(path); - let reader = BufReader::new(file); - reader - .lines() - .filter_map(|line| line.ok()) - .filter(|line| re.is_match(line)) - .for_each(|_| match_count += 1); - }); - assert_eq!(match_count, 3); + assert_eq!(count_regex_matches_in_file_glob(r#"define\ .*magic_fn"#, "foo.*.ll"), 3); }