From f280a839a7d327dd2b38360707437cd75c954d97 Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Mon, 11 Apr 2022 15:17:52 -0400 Subject: [PATCH] Add support for MIR opt unit tests --- compiler/rustc_interface/src/tests.rs | 1 + .../rustc_mir_transform/src/pass_manager.rs | 21 ++++++++++++++---- compiler/rustc_session/src/options.rs | 22 +++++++++++++++++++ src/tools/compiletest/src/header.rs | 7 ++++++ src/tools/compiletest/src/runtest.rs | 6 ++++- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index fe75ee8b37b..ee923908e30 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -751,6 +751,7 @@ fn test_debugging_options_tracking_hash() { tracked!(location_detail, LocationDetail { file: true, line: false, column: false }); tracked!(merge_functions, Some(MergeFunctions::Disabled)); tracked!(mir_emit_retag, true); + tracked!(mir_enable_passes, vec![("DestProp".to_string(), false)]); tracked!(mir_opt_level, Some(4)); tracked!(move_size_limit, Some(4096)); tracked!(mutable_noalias, Some(true)); diff --git a/compiler/rustc_mir_transform/src/pass_manager.rs b/compiler/rustc_mir_transform/src/pass_manager.rs index 740a2168b41..cf88e8b2144 100644 --- a/compiler/rustc_mir_transform/src/pass_manager.rs +++ b/compiler/rustc_mir_transform/src/pass_manager.rs @@ -77,17 +77,30 @@ pub fn run_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, passes: &[&dyn let mut cnt = 0; let validate = tcx.sess.opts.debugging_opts.validate_mir; + let overridden_passes = &tcx.sess.opts.debugging_opts.mir_enable_passes; + trace!("Overridden: {:?}", overridden_passes); if validate { validate_body(tcx, body, format!("start of phase transition from {:?}", start_phase)); } for pass in passes { - if !pass.is_enabled(&tcx.sess) { - continue; - } - let name = pass.name(); + + if let Some((_, polarity)) = overridden_passes.iter().rev().find(|(s, _)| s == &*name) { + trace!( + "{} {} as requested by flag", + if *polarity { "Running" } else { "Not running" }, + name + ); + if !polarity { + continue; + } + } else { + if !pass.is_enabled(&tcx.sess) { + continue; + } + } let dump_enabled = pass.is_mir_dump_enabled(); if dump_enabled { diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 4994f8eaeb2..a5c1c713bfc 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -369,6 +369,8 @@ mod desc { pub const parse_opt_langid: &str = "a language identifier"; pub const parse_opt_pathbuf: &str = "a path"; pub const parse_list: &str = "a space-separated list of strings"; + pub const parse_list_with_polarity: &str = + "a comma-separated list of strings, with elements beginning with + or -"; pub const parse_opt_comma_list: &str = "a comma-separated list of strings"; pub const parse_number: &str = "a number"; pub const parse_opt_number: &str = parse_number; @@ -530,6 +532,22 @@ mod parse { } } + crate fn parse_list_with_polarity(slot: &mut Vec<(String, bool)>, v: Option<&str>) -> bool { + match v { + Some(s) => { + for s in s.split(",") { + match s.chars().next() { + Some('+') => slot.push((s[1..].to_string(), true)), + Some('-') => slot.push((s[1..].to_string(), false)), + _ => return false, + } + } + true + } + None => false, + } + } + crate fn parse_location_detail(ld: &mut LocationDetail, v: Option<&str>) -> bool { if let Some(v) = v { ld.line = false; @@ -1319,6 +1337,10 @@ options! { mir_emit_retag: bool = (false, parse_bool, [TRACKED], "emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0 \ (default: no)"), + mir_enable_passes: Vec<(String, bool)> = (Vec::new(), parse_list_with_polarity, [TRACKED], + "use like `-Zmir-enable-passes=+DestProp,-InstCombine`. Forces the specified passes to be \ + enabled, overriding all other checks. Passes that are not specified are enabled or \ + disabled by other flags as usual."), mir_opt_level: Option = (None, parse_opt_number, [TRACKED], "MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"), move_size_limit: Option = (None, parse_opt_number, [TRACKED], diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index 858a576dcb4..e6f058569db 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -157,6 +157,8 @@ pub struct TestProps { pub should_ice: bool, // If true, the stderr is expected to be different across bit-widths. pub stderr_per_bitwidth: bool, + // The MIR opt to unit test, if any + pub mir_unit_test: Option, } mod directives { @@ -189,6 +191,7 @@ mod directives { pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth"; pub const INCREMENTAL: &'static str = "incremental"; pub const KNOWN_BUG: &'static str = "known-bug"; + pub const MIR_UNIT_TEST: &'static str = "unit-test"; // This isn't a real directive, just one that is probably mistyped often pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags"; } @@ -232,6 +235,7 @@ impl TestProps { assembly_output: None, should_ice: false, stderr_per_bitwidth: false, + mir_unit_test: None, } } @@ -392,6 +396,9 @@ impl TestProps { config.set_name_directive(ln, STDERR_PER_BITWIDTH, &mut self.stderr_per_bitwidth); config.set_name_directive(ln, INCREMENTAL, &mut self.incremental); config.set_name_directive(ln, KNOWN_BUG, &mut self.known_bug); + config.set_name_value_directive(ln, MIR_UNIT_TEST, &mut self.mir_unit_test, |s| { + s.trim().to_string() + }); }); } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 6b27d1ecbf5..6618fff64d3 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1856,10 +1856,14 @@ impl<'test> TestCx<'test> { rustc.args(&[ "-Copt-level=1", "-Zdump-mir=all", - "-Zmir-opt-level=4", "-Zvalidate-mir", "-Zdump-mir-exclude-pass-number", ]); + if let Some(pass) = &self.props.mir_unit_test { + rustc.args(&["-Zmir-opt-level=0", &format!("-Zmir-enable-passes=+{}", pass)]); + } else { + rustc.arg("-Zmir-opt-level=4"); + } let mir_dump_dir = self.get_mir_dump_dir(); let _ = fs::remove_dir_all(&mir_dump_dir);