|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use run_make_support::{CompletedProcess, rfs, rustc}; |
| 4 | + |
| 5 | +struct Case { |
| 6 | + name: &'static str, |
| 7 | + flags: &'static [&'static str], |
| 8 | + expect_inline_dump: bool, |
| 9 | + expect_running: ExpectedCount, |
| 10 | + expect_not_running: ExpectedCount, |
| 11 | +} |
| 12 | + |
| 13 | +enum ExpectedCount { |
| 14 | + Exactly(usize), |
| 15 | + AtLeastOne, |
| 16 | + Zero, |
| 17 | +} |
| 18 | + |
| 19 | +fn main() { |
| 20 | + let cases = [ |
| 21 | + Case { |
| 22 | + name: "limit0", |
| 23 | + flags: &["-Zmir-opt-bisect-limit=0"], |
| 24 | + expect_inline_dump: false, |
| 25 | + expect_running: ExpectedCount::Exactly(0), |
| 26 | + expect_not_running: ExpectedCount::AtLeastOne, |
| 27 | + }, |
| 28 | + Case { |
| 29 | + name: "limit1", |
| 30 | + flags: &["-Zmir-opt-bisect-limit=1"], |
| 31 | + expect_inline_dump: false, |
| 32 | + expect_running: ExpectedCount::Exactly(1), |
| 33 | + expect_not_running: ExpectedCount::AtLeastOne, |
| 34 | + }, |
| 35 | + Case { |
| 36 | + name: "huge_limit", |
| 37 | + flags: &["-Zmir-opt-bisect-limit=1000000000"], |
| 38 | + expect_inline_dump: true, |
| 39 | + expect_running: ExpectedCount::AtLeastOne, |
| 40 | + expect_not_running: ExpectedCount::Zero, |
| 41 | + }, |
| 42 | + Case { |
| 43 | + name: "limit0_with_force_enable_inline", |
| 44 | + flags: &["-Zmir-opt-bisect-limit=0", "-Zmir-enable-passes=+Inline"], |
| 45 | + expect_inline_dump: false, |
| 46 | + expect_running: ExpectedCount::Exactly(0), |
| 47 | + expect_not_running: ExpectedCount::AtLeastOne, |
| 48 | + }, |
| 49 | + ]; |
| 50 | + |
| 51 | + for case in cases { |
| 52 | + let (inline_dumped, running_count, not_running_count, output) = |
| 53 | + compile_case(case.name, case.flags); |
| 54 | + |
| 55 | + assert_eq!( |
| 56 | + inline_dumped, case.expect_inline_dump, |
| 57 | + "{}: unexpected Inline dump presence", |
| 58 | + case.name |
| 59 | + ); |
| 60 | + |
| 61 | + assert_expected_count( |
| 62 | + running_count, |
| 63 | + case.expect_running, |
| 64 | + &format!("{}: running count", case.name), |
| 65 | + ); |
| 66 | + assert_expected_count( |
| 67 | + not_running_count, |
| 68 | + case.expect_not_running, |
| 69 | + &format!("{}: NOT running count", case.name), |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn compile_case(dump_dir: &str, extra_flags: &[&str]) -> (bool, usize, usize, CompletedProcess) { |
| 75 | + if Path::new(dump_dir).exists() { |
| 76 | + rfs::remove_dir_all(dump_dir); |
| 77 | + } |
| 78 | + rfs::create_dir_all(dump_dir); |
| 79 | + |
| 80 | + let mut cmd = rustc(); |
| 81 | + cmd.input("main.rs") |
| 82 | + .arg("--emit=mir") |
| 83 | + .arg("-Zmir-opt-level=2") |
| 84 | + .arg("-Copt-level=2") |
| 85 | + .arg("-Zthreads=1") |
| 86 | + .arg("-Zdump-mir=Inline") |
| 87 | + .arg(format!("-Zdump-mir-dir={dump_dir}")); |
| 88 | + |
| 89 | + for &flag in extra_flags { |
| 90 | + cmd.arg(flag); |
| 91 | + } |
| 92 | + |
| 93 | + let output = cmd.run(); |
| 94 | + let (running_count, not_running_count) = bisect_line_counts(&output); |
| 95 | + (has_inline_dump_file(dump_dir), running_count, not_running_count, output) |
| 96 | +} |
| 97 | + |
| 98 | +fn assert_expected_count(actual: usize, expected: ExpectedCount, context: &str) { |
| 99 | + match expected { |
| 100 | + ExpectedCount::Exactly(n) => assert_eq!(actual, n, "{context}"), |
| 101 | + ExpectedCount::AtLeastOne => assert!(actual > 0, "{context}"), |
| 102 | + ExpectedCount::Zero => assert_eq!(actual, 0, "{context}"), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fn has_inline_dump_file(dir: &str) -> bool { |
| 107 | + rfs::read_dir(dir) |
| 108 | + .flatten() |
| 109 | + .any(|entry| entry.file_name().to_string_lossy().contains(".Inline.")) |
| 110 | +} |
| 111 | + |
| 112 | +fn bisect_line_counts(output: &CompletedProcess) -> (usize, usize) { |
| 113 | + let stderr = output.stderr_utf8(); |
| 114 | + let running_count = |
| 115 | + stderr.lines().filter(|line| line.starts_with("BISECT: running pass (")).count(); |
| 116 | + let not_running_count = |
| 117 | + stderr.lines().filter(|line| line.starts_with("BISECT: NOT running pass (")).count(); |
| 118 | + (running_count, not_running_count) |
| 119 | +} |
0 commit comments