Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce MIR dump file count for MIR-opt tests #110773

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 66 additions & 21 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ impl<'test> TestCx<'test> {

fn run_cfail_test(&self) {
let pm = self.pass_mode();
let proc_res = self.compile_test(WillExecute::No, self.should_emit_metadata(pm));
let proc_res =
self.compile_test(WillExecute::No, self.should_emit_metadata(pm), Vec::new());
self.check_if_test_should_compile(&proc_res, pm);
self.check_no_compiler_crash(&proc_res, self.props.should_ice);

Expand Down Expand Up @@ -347,7 +348,7 @@ impl<'test> TestCx<'test> {
fn run_rfail_test(&self) {
let pm = self.pass_mode();
let should_run = self.run_if_enabled();
let proc_res = self.compile_test(should_run, self.should_emit_metadata(pm));
let proc_res = self.compile_test(should_run, self.should_emit_metadata(pm), Vec::new());
Comment on lines -350 to +351
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing compile_test function at all, if in all places except one, last parameter will be Vec::new()? You can just add something like compile_test_with_passes and use it exactly in that one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I'll make a follow up with that change


if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -395,7 +396,7 @@ impl<'test> TestCx<'test> {

fn run_cpass_test(&self) {
let emit_metadata = self.should_emit_metadata(self.pass_mode());
let proc_res = self.compile_test(WillExecute::No, emit_metadata);
let proc_res = self.compile_test(WillExecute::No, emit_metadata, Vec::new());

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand All @@ -410,7 +411,7 @@ impl<'test> TestCx<'test> {
fn run_rpass_test(&self) {
let emit_metadata = self.should_emit_metadata(self.pass_mode());
let should_run = self.run_if_enabled();
let proc_res = self.compile_test(should_run, emit_metadata);
let proc_res = self.compile_test(should_run, emit_metadata, Vec::new());

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -440,7 +441,7 @@ impl<'test> TestCx<'test> {
}

let should_run = self.run_if_enabled();
let mut proc_res = self.compile_test(should_run, Emit::None);
let mut proc_res = self.compile_test(should_run, Emit::None, Vec::new());

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -686,7 +687,7 @@ impl<'test> TestCx<'test> {

// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compile_result = self.compile_test(should_run, Emit::None);
let compile_result = self.compile_test(should_run, Emit::None, Vec::new());
if !compile_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compile_result);
}
Expand Down Expand Up @@ -806,7 +807,7 @@ impl<'test> TestCx<'test> {

// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compiler_run_result = self.compile_test(should_run, Emit::None);
let compiler_run_result = self.compile_test(should_run, Emit::None, Vec::new());
if !compiler_run_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compiler_run_result);
}
Expand Down Expand Up @@ -1043,7 +1044,7 @@ impl<'test> TestCx<'test> {
fn run_debuginfo_lldb_test_no_opt(&self) {
// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compile_result = self.compile_test(should_run, Emit::None);
let compile_result = self.compile_test(should_run, Emit::None, Vec::new());
if !compile_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compile_result);
}
Expand Down Expand Up @@ -1482,15 +1483,16 @@ impl<'test> TestCx<'test> {
}
}

fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
self.compile_test_general(will_execute, emit, self.props.local_pass_mode())
fn compile_test(&self, will_execute: WillExecute, emit: Emit, passes: Vec<String>) -> ProcRes {
self.compile_test_general(will_execute, emit, self.props.local_pass_mode(), passes)
}

fn compile_test_general(
&self,
will_execute: WillExecute,
emit: Emit,
local_pm: Option<PassMode>,
passes: Vec<String>,
) -> ProcRes {
// Only use `make_exe_name` when the test ends up being executed.
let output_file = match will_execute {
Expand Down Expand Up @@ -1527,6 +1529,7 @@ impl<'test> TestCx<'test> {
emit,
allow_unused,
LinkToAux::Yes,
passes,
);

self.compose_and_run_compiler(rustc, None)
Expand Down Expand Up @@ -1777,6 +1780,7 @@ impl<'test> TestCx<'test> {
Emit::None,
AllowUnused::No,
LinkToAux::No,
Vec::new(),
);

for key in &aux_props.unset_rustc_env {
Expand Down Expand Up @@ -1908,6 +1912,7 @@ impl<'test> TestCx<'test> {
emit: Emit,
allow_unused: AllowUnused,
link_to_aux: LinkToAux,
passes: Vec<String>, // Vec of passes under mir-opt test to be dumped
) -> Command {
let is_aux = input_file.components().map(|c| c.as_os_str()).any(|c| c == "auxiliary");
let is_rustdoc = self.is_rustdoc() && !is_aux;
Expand Down Expand Up @@ -2008,9 +2013,18 @@ impl<'test> TestCx<'test> {
rustc.arg("-Cstrip=debuginfo");
}
MirOpt => {
// We check passes under test to minimize the mir-opt test dump
// if files_for_miropt_test parses the passes, we dump only those passes
// otherwise we conservatively pass -Zdump-mir=all
let zdump_arg = if !passes.is_empty() {
format!("-Zdump-mir={}", passes.join(" | "))
} else {
"-Zdump-mir=all".to_string()
};

rustc.args(&[
"-Copt-level=1",
"-Zdump-mir=all",
&zdump_arg,
"-Zvalidate-mir",
"-Zdump-mir-exclude-pass-number",
"-Zmir-pretty-relative-line-numbers=yes",
Expand Down Expand Up @@ -2333,6 +2347,7 @@ impl<'test> TestCx<'test> {
Emit::LlvmIr,
AllowUnused::No,
LinkToAux::Yes,
Vec::new(),
);

self.compose_and_run_compiler(rustc, None)
Expand Down Expand Up @@ -2364,8 +2379,14 @@ impl<'test> TestCx<'test> {
None => self.fatal("missing 'assembly-output' header"),
}

let rustc =
self.make_compile_args(input_file, output_file, emit, AllowUnused::No, LinkToAux::Yes);
let rustc = self.make_compile_args(
input_file,
output_file,
emit,
AllowUnused::No,
LinkToAux::Yes,
Vec::new(),
);

(self.compose_and_run_compiler(rustc, None), output_path)
}
Expand Down Expand Up @@ -2496,6 +2517,7 @@ impl<'test> TestCx<'test> {
Emit::None,
AllowUnused::Yes,
LinkToAux::Yes,
Vec::new(),
);
new_rustdoc.build_all_auxiliary(&mut rustc);

Expand Down Expand Up @@ -2769,7 +2791,7 @@ impl<'test> TestCx<'test> {
fn run_codegen_units_test(&self) {
assert!(self.revision.is_none(), "revisions not relevant here");

let proc_res = self.compile_test(WillExecute::No, Emit::None);
let proc_res = self.compile_test(WillExecute::No, Emit::None, Vec::new());

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -3310,14 +3332,15 @@ impl<'test> TestCx<'test> {
if let Some(FailMode::Build) = self.props.fail_mode {
// Make sure a build-fail test cannot fail due to failing analysis (e.g. typeck).
let pm = Some(PassMode::Check);
let proc_res = self.compile_test_general(WillExecute::No, Emit::Metadata, pm);
let proc_res =
self.compile_test_general(WillExecute::No, Emit::Metadata, pm, Vec::new());
self.check_if_test_should_compile(&proc_res, pm);
}

let pm = self.pass_mode();
let should_run = self.should_run(pm);
let emit_metadata = self.should_emit_metadata(pm);
let proc_res = self.compile_test(should_run, emit_metadata);
let proc_res = self.compile_test(should_run, emit_metadata, Vec::new());
self.check_if_test_should_compile(&proc_res, pm);

// if the user specified a format in the ui test
Expand Down Expand Up @@ -3479,6 +3502,7 @@ impl<'test> TestCx<'test> {
emit_metadata,
AllowUnused::No,
LinkToAux::Yes,
Vec::new(),
);
let res = self.compose_and_run_compiler(rustc, None);
if !res.status.success() {
Expand All @@ -3497,14 +3521,14 @@ impl<'test> TestCx<'test> {
let pm = self.pass_mode();
let should_run = self.should_run(pm);
let emit_metadata = self.should_emit_metadata(pm);
let proc_res = self.compile_test(should_run, emit_metadata);
let passes = self.get_passes();

let proc_res = self.compile_test(should_run, emit_metadata, passes);
self.check_mir_dump();
if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
}

self.check_mir_dump();

if let WillExecute::Yes = should_run {
let proc_res = self.exec_compiled_test();

Expand All @@ -3514,6 +3538,26 @@ 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(),
);

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) {
let test_file_contents = fs::read_to_string(&self.testpaths.file).unwrap();

Expand Down Expand Up @@ -3543,8 +3587,9 @@ impl<'test> TestCx<'test> {
&self.testpaths.file,
self.config.get_pointer_width(),
);

for miropt_test_tools::MiroptTestFiles { from_file, to_file, expected_file } in files {
for miropt_test_tools::MiroptTestFiles { from_file, to_file, expected_file, passes: _ } in
files
{
let dumped_string = if let Some(after) = to_file {
self.diff_mir_files(from_file.into(), after.into())
} else {
Expand Down
19 changes: 18 additions & 1 deletion src/tools/miropt-test-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub struct MiroptTestFiles {
pub expected_file: std::path::PathBuf,
pub from_file: String,
pub to_file: Option<String>,
/// Vec of passes under test to be dumped
pub passes: Vec<String>,
}

pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<MiroptTestFiles> {
Expand All @@ -28,17 +30,26 @@ pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<
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");
passes.push(trimmed.split('.').last().unwrap().to_owned());
let test_against = format!("{}.after.mir", trimmed);
from_file = format!("{}.before.mir", trimmed);
expected_file = format!("{}{}.diff", trimmed, bit_width);
assert!(test_names.next().is_none(), "two mir pass names specified for MIR diff");
to_file = Some(test_against);
} else if let Some(first_pass) = test_names.next() {
let second_pass = test_names.next().unwrap();
if let Some((first_pass_name, _)) = first_pass.split_once('.') {
passes.push(first_pass_name.to_owned());
}
if let Some((second_pass_name, _)) = second_pass.split_once('.') {
passes.push(second_pass_name.to_owned());
}
assert!(test_names.next().is_none(), "three mir pass names specified for MIR diff");

expected_file =
format!("{}{}.{}-{}.diff", test_name, bit_width, first_pass, second_pass);
let second_file = format!("{}.{}.mir", test_name, second_pass);
Expand All @@ -51,18 +62,24 @@ pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<
.next()
.expect("test_name has an invalid extension");
let extension = cap.get(1).unwrap().as_str();

expected_file =
format!("{}{}{}", test_name.trim_end_matches(extension), bit_width, extension,);
from_file = test_name.to_string();
assert!(test_names.next().is_none(), "two mir pass names specified for MIR dump");
to_file = None;
// the pass name is the third to last string in the test name
// this gets pushed into passes
passes.push(
test_name.split('.').rev().nth(2).expect("invalid test format").to_string(),
);
};
if !expected_file.starts_with(&test_crate) {
expected_file = format!("{}.{}", test_crate, expected_file);
}
let expected_file = test_dir.join(expected_file);

out.push(MiroptTestFiles { expected_file, from_file, to_file });
out.push(MiroptTestFiles { expected_file, from_file, to_file, passes });
}
}

Expand Down
32 changes: 16 additions & 16 deletions tests/mir-opt/const_allocation.main.ConstProp.after.32bit.mir
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,42 @@ fn main() -> () {
}

alloc1 (static: FOO, size: 8, align: 4) {
╾─alloc18─╼ 03 00 00 00 │ ╾──╼....
╾─alloc19─╼ 03 00 00 00 │ ╾──╼....
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this affects alloc numbers at all?

}

alloc18 (size: 48, align: 4) {
0x00 │ 00 00 00 00 __ __ __ __ ╾─alloc5──╼ 00 00 00 00 │ ....░░░░╾──╼....
0x10 │ 00 00 00 00 __ __ __ __ ╾─alloc8──╼ 02 00 00 00 │ ....░░░░╾──╼....
0x20 │ 01 00 00 00 2a 00 00 00 ╾─alloc13─╼ 03 00 00 00 │ ....*...╾──╼....
alloc19 (size: 48, align: 4) {
0x00 │ 00 00 00 00 __ __ __ __ ╾─alloc6──╼ 00 00 00 00 │ ....░░░░╾──╼....
0x10 │ 00 00 00 00 __ __ __ __ ╾─alloc9──╼ 02 00 00 00 │ ....░░░░╾──╼....
0x20 │ 01 00 00 00 2a 00 00 00 ╾─alloc14─╼ 03 00 00 00 │ ....*...╾──╼....
}

alloc5 (size: 0, align: 4) {}
alloc6 (size: 0, align: 4) {}

alloc8 (size: 16, align: 4) {
╾─alloc9──╼ 03 00 00 00 ╾─alloc10─╼ 03 00 00 00 │ ╾──╼....╾──╼....
alloc9 (size: 16, align: 4) {
╾─alloc10─╼ 03 00 00 00 ╾─alloc11─╼ 03 00 00 00 │ ╾──╼....╾──╼....
}

alloc9 (size: 3, align: 1) {
alloc10 (size: 3, align: 1) {
66 6f 6f │ foo
}

alloc10 (size: 3, align: 1) {
alloc11 (size: 3, align: 1) {
62 61 72 │ bar
}

alloc13 (size: 24, align: 4) {
0x00 │ ╾─alloc14─╼ 03 00 00 00 ╾─alloc15─╼ 03 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾─alloc16─╼ 04 00 00 00 │ ╾──╼....
alloc14 (size: 24, align: 4) {
0x00 │ ╾─alloc15─╼ 03 00 00 00 ╾─alloc16─╼ 03 00 00 00 │ ╾──╼....╾──╼....
0x10 │ ╾─alloc17─╼ 04 00 00 00 │ ╾──╼....
}

alloc14 (size: 3, align: 1) {
alloc15 (size: 3, align: 1) {
6d 65 68 │ meh
}

alloc15 (size: 3, align: 1) {
alloc16 (size: 3, align: 1) {
6d 6f 70 │ mop
}

alloc16 (size: 4, align: 1) {
alloc17 (size: 4, align: 1) {
6d c3 b6 70 │ m..p
}
Loading