about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-19 16:53:52 +0000
committerbors <bors@rust-lang.org>2023-10-19 16:53:52 +0000
commite76cb8c49831ff80d7d2e16319c86edbaa9edb8c (patch)
tree0ee10fd0e3fbcb6aea6da062b9ef275fc4ca6a4f /src
parentc104861b7b51d2c28e7023e7e53db16cc6677e29 (diff)
parent328192bff492d32b86004fd80f963296fb654278 (diff)
Auto merge of #116810 - cjgillot:mir-opt-check, r=oli-obk
Add FileCheck annotations to mir-opt tests.

This PR makes compiletest run LLVM `FileCheck` tool on mir-opt tests.

The check is *run by default*, except if disabled using `// skip-filecheck` comment. This ensures that we do not have a silently broken test. For now, the check is only run on the output of `--emit=mir`, ie. on PreCodegen MIR.

I give an example on `reference_prop.rs`.

r? `@oli-obk`
cc `@RalfJung`

Fixes https://github.com/rust-lang/rust/issues/85180
Diffstat (limited to 'src')
-rw-r--r--src/tools/compiletest/src/runtest.rs65
-rw-r--r--src/tools/miropt-test-tools/src/lib.rs27
-rw-r--r--src/tools/tidy/src/mir_opt_tests.rs3
3 files changed, 45 insertions, 50 deletions
diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs
index 875671c64e0..8ce00e00b57 100644
--- a/src/tools/compiletest/src/runtest.rs
+++ b/src/tools/compiletest/src/runtest.rs
@@ -15,6 +15,7 @@ use crate::json;
 use crate::read2::{read2_abbreviated, Truncated};
 use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
 use crate::ColorConfig;
+use miropt_test_tools::{files_for_miropt_test, MiroptTest, MiroptTestFile};
 use regex::{Captures, Regex};
 use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
 
@@ -229,6 +230,7 @@ enum Emit {
     None,
     Metadata,
     LlvmIr,
+    Mir,
     Asm,
     LinkArgsAsm,
 }
@@ -2506,6 +2508,9 @@ impl<'test> TestCx<'test> {
             Emit::LlvmIr => {
                 rustc.args(&["--emit", "llvm-ir"]);
             }
+            Emit::Mir => {
+                rustc.args(&["--emit", "mir"]);
+            }
             Emit::Asm => {
                 rustc.args(&["--emit", "asm"]);
             }
@@ -3984,11 +3989,17 @@ impl<'test> TestCx<'test> {
     fn run_mir_opt_test(&self) {
         let pm = self.pass_mode();
         let should_run = self.should_run(pm);
-        let emit_metadata = self.should_emit_metadata(pm);
-        let passes = self.get_passes();
 
-        let proc_res = self.compile_test_with_passes(should_run, emit_metadata, passes);
-        self.check_mir_dump();
+        let mut test_info = files_for_miropt_test(
+            &self.testpaths.file,
+            self.config.get_pointer_width(),
+            self.config.target_cfg().panic.for_miropt_test_tools(),
+        );
+
+        let passes = std::mem::take(&mut test_info.passes);
+
+        let proc_res = self.compile_test_with_passes(should_run, Emit::Mir, passes);
+        self.check_mir_dump(test_info);
         if !proc_res.status.success() {
             self.fatal_proc_rec("compilation failed!", &proc_res);
         }
@@ -4002,37 +4013,12 @@ impl<'test> TestCx<'test> {
         }
     }
 
-    fn get_passes(&self) -> Vec<String> {
-        let files = miropt_test_tools::files_for_miropt_test(
-            &self.testpaths.file,
-            self.config.get_pointer_width(),
-            self.config.target_cfg().panic.for_miropt_test_tools(),
-        );
-
-        let mut out = Vec::new();
-
-        for miropt_test_tools::MiroptTestFiles {
-            from_file: _,
-            to_file: _,
-            expected_file: _,
-            passes,
-        } in files
-        {
-            out.extend(passes);
-        }
-        out
-    }
-
-    fn check_mir_dump(&self) {
+    fn check_mir_dump(&self, test_info: MiroptTest) {
         let test_dir = self.testpaths.file.parent().unwrap();
         let test_crate =
             self.testpaths.file.file_stem().unwrap().to_str().unwrap().replace("-", "_");
 
-        let suffix = miropt_test_tools::output_file_suffix(
-            &self.testpaths.file,
-            self.config.get_pointer_width(),
-            self.config.target_cfg().panic.for_miropt_test_tools(),
-        );
+        let MiroptTest { run_filecheck, suffix, files, passes: _ } = test_info;
 
         if self.config.bless {
             for e in
@@ -4047,14 +4033,7 @@ impl<'test> TestCx<'test> {
             }
         }
 
-        let files = miropt_test_tools::files_for_miropt_test(
-            &self.testpaths.file,
-            self.config.get_pointer_width(),
-            self.config.target_cfg().panic.for_miropt_test_tools(),
-        );
-        for miropt_test_tools::MiroptTestFiles { from_file, to_file, expected_file, passes: _ } in
-            files
-        {
+        for MiroptTestFile { from_file, to_file, expected_file } in files {
             let dumped_string = if let Some(after) = to_file {
                 self.diff_mir_files(from_file.into(), after.into())
             } else {
@@ -4095,6 +4074,14 @@ impl<'test> TestCx<'test> {
                 }
             }
         }
+
+        if run_filecheck {
+            let output_path = self.output_base_name().with_extension("mir");
+            let proc_res = self.verify_with_filecheck(&output_path);
+            if !proc_res.status.success() {
+                self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
+            }
+        }
     }
 
     fn diff_mir_files(&self, before: PathBuf, after: PathBuf) -> String {
diff --git a/src/tools/miropt-test-tools/src/lib.rs b/src/tools/miropt-test-tools/src/lib.rs
index e33ecfe8eab..cae96f59319 100644
--- a/src/tools/miropt-test-tools/src/lib.rs
+++ b/src/tools/miropt-test-tools/src/lib.rs
@@ -1,10 +1,16 @@
 use std::fs;
 use std::path::Path;
 
-pub struct MiroptTestFiles {
+pub struct MiroptTestFile {
     pub expected_file: std::path::PathBuf,
     pub from_file: String,
     pub to_file: Option<String>,
+}
+
+pub struct MiroptTest {
+    pub run_filecheck: bool,
+    pub suffix: String,
+    pub files: Vec<MiroptTestFile>,
     /// Vec of passes under test to be dumped
     pub passes: Vec<String>,
 }
@@ -14,11 +20,7 @@ pub enum PanicStrategy {
     Abort,
 }
 
-pub fn output_file_suffix(
-    testfile: &Path,
-    bit_width: u32,
-    panic_strategy: PanicStrategy,
-) -> String {
+fn output_file_suffix(testfile: &Path, bit_width: u32, panic_strategy: PanicStrategy) -> String {
     let mut each_bit_width = false;
     let mut each_panic_strategy = false;
     for line in fs::read_to_string(testfile).unwrap().lines() {
@@ -47,7 +49,7 @@ pub fn files_for_miropt_test(
     testfile: &std::path::Path,
     bit_width: u32,
     panic_strategy: PanicStrategy,
-) -> Vec<MiroptTestFiles> {
+) -> MiroptTest {
     let mut out = Vec::new();
     let test_file_contents = fs::read_to_string(&testfile).unwrap();
 
@@ -55,8 +57,14 @@ pub fn files_for_miropt_test(
     let test_crate = testfile.file_stem().unwrap().to_str().unwrap().replace('-', "_");
 
     let suffix = output_file_suffix(testfile, bit_width, panic_strategy);
+    let mut run_filecheck = true;
+    let mut passes = Vec::new();
 
     for l in test_file_contents.lines() {
+        if l.starts_with("// skip-filecheck") {
+            run_filecheck = false;
+            continue;
+        }
         if l.starts_with("// EMIT_MIR ") {
             let test_name = l.trim_start_matches("// EMIT_MIR ").trim();
             let mut test_names = test_name.split(' ');
@@ -65,7 +73,6 @@ pub fn files_for_miropt_test(
             let mut expected_file;
             let from_file;
             let to_file;
-            let mut passes = Vec::new();
 
             if test_name.ends_with(".diff") {
                 let trimmed = test_name.trim_end_matches(".diff");
@@ -114,9 +121,9 @@ pub fn files_for_miropt_test(
             }
             let expected_file = test_dir.join(expected_file);
 
-            out.push(MiroptTestFiles { expected_file, from_file, to_file, passes });
+            out.push(MiroptTestFile { expected_file, from_file, to_file });
         }
     }
 
-    out
+    MiroptTest { run_filecheck, suffix, files: out, passes }
 }
diff --git a/src/tools/tidy/src/mir_opt_tests.rs b/src/tools/tidy/src/mir_opt_tests.rs
index c307bcb9390..76feeb34331 100644
--- a/src/tools/tidy/src/mir_opt_tests.rs
+++ b/src/tools/tidy/src/mir_opt_tests.rs
@@ -26,7 +26,8 @@ fn check_unused_files(path: &Path, bless: bool, bad: &mut bool) {
     for file in rs_files {
         for bw in [32, 64] {
             for ps in [PanicStrategy::Unwind, PanicStrategy::Abort] {
-                for output_file in miropt_test_tools::files_for_miropt_test(&file, bw, ps) {
+                let mir_opt_test = miropt_test_tools::files_for_miropt_test(&file, bw, ps);
+                for output_file in mir_opt_test.files {
                     output_files.remove(&output_file.expected_file);
                 }
             }