Rollup merge of #152474 - sgasho:opt-bisect-limit-mir, r=saethlin
Implement opt-bisect-limit for MIR closes: rust-lang/rust#150910 Enable bisecting MIR optimization passes to enhance debuggability. discussions on zulip: https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/MIR.20dump.20the.20pass.20names/with/573219207 ### Check it works #### Sample code ```rust fn abs(num: isize) -> usize { if num < 0 { -num as usize } else { num as usize } } fn main() { println!("{}", abs(-10)); } ``` #### Output ```shell rustc +mir -Zmir-opt-bisect-limit=30 src/main.rs BISECT: running pass (1) CheckAlignment on main[89d5]::main BISECT: running pass (2) CheckNull on main[89d5]::main BISECT: running pass (3) CheckEnums on main[89d5]::main BISECT: running pass (4) LowerSliceLenCalls on main[89d5]::main BISECT: running pass (5) InstSimplify-before-inline on main[89d5]::main BISECT: running pass (6) ForceInline on main[89d5]::main BISECT: running pass (7) RemoveStorageMarkers on main[89d5]::main BISECT: running pass (8) RemoveZsts on main[89d5]::main BISECT: running pass (9) RemoveUnneededDrops on main[89d5]::main BISECT: running pass (10) UnreachableEnumBranching on main[89d5]::main BISECT: running pass (11) SimplifyCfg-after-unreachable-enum-branching on main[89d5]::main BISECT: running pass (12) InstSimplify-after-simplifycfg on main[89d5]::main BISECT: running pass (13) SimplifyConstCondition-after-inst-simplify on main[89d5]::main BISECT: running pass (14) SimplifyLocals-before-const-prop on main[89d5]::main BISECT: running pass (15) SimplifyLocals-after-value-numbering on main[89d5]::main BISECT: running pass (16) MatchBranchSimplification on main[89d5]::main BISECT: running pass (17) SingleUseConsts on main[89d5]::main BISECT: running pass (18) SimplifyConstCondition-after-const-prop on main[89d5]::main BISECT: running pass (19) SimplifyConstCondition-final on main[89d5]::main BISECT: running pass (20) RemoveNoopLandingPads on main[89d5]::main BISECT: running pass (21) SimplifyCfg-final on main[89d5]::main BISECT: running pass (22) CopyProp on main[89d5]::main BISECT: running pass (23) SimplifyLocals-final on main[89d5]::main BISECT: running pass (24) AddCallGuards on main[89d5]::main BISECT: running pass (25) PreCodegen on main[89d5]::main BISECT: running pass (26) CheckAlignment on main[89d5]::abs BISECT: running pass (27) CheckNull on main[89d5]::abs BISECT: running pass (28) CheckEnums on main[89d5]::abs BISECT: running pass (29) LowerSliceLenCalls on main[89d5]::abs BISECT: running pass (30) InstSimplify-before-inline on main[89d5]::abs BISECT: NOT running pass (31) ForceInline on main[89d5]::abs BISECT: NOT running pass (32) RemoveStorageMarkers on main[89d5]::abs BISECT: NOT running pass (33) RemoveZsts on main[89d5]::abs BISECT: NOT running pass (34) RemoveUnneededDrops on main[89d5]::abs BISECT: NOT running pass (35) UnreachableEnumBranching on main[89d5]::abs BISECT: NOT running pass (36) SimplifyCfg-after-unreachable-enum-branching on main[89d5]::abs BISECT: NOT running pass (37) InstSimplify-after-simplifycfg on main[89d5]::abs BISECT: NOT running pass (38) SimplifyConstCondition-after-inst-simplify on main[89d5]::abs BISECT: NOT running pass (39) SimplifyLocals-before-const-prop on main[89d5]::abs BISECT: NOT running pass (40) SimplifyLocals-after-value-numbering on main[89d5]::abs BISECT: NOT running pass (41) MatchBranchSimplification on main[89d5]::abs BISECT: NOT running pass (42) SingleUseConsts on main[89d5]::abs BISECT: NOT running pass (43) SimplifyConstCondition-after-const-prop on main[89d5]::abs BISECT: NOT running pass (44) SimplifyConstCondition-final on main[89d5]::abs BISECT: NOT running pass (45) RemoveNoopLandingPads on main[89d5]::abs BISECT: NOT running pass (46) SimplifyCfg-final on main[89d5]::abs BISECT: NOT running pass (47) CopyProp on main[89d5]::abs BISECT: NOT running pass (48) SimplifyLocals-final on main[89d5]::abs BISECT: NOT running pass (49) AddCallGuards on main[89d5]::abs BISECT: NOT running pass (50) PreCodegen on main[89d5]::abs ``` r? @saethlin
This commit is contained in:
commit
544462ad8b
5 changed files with 198 additions and 1 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
|
||||
use rustc_middle::mir::{Body, MirDumper, MirPhase, RuntimePhase};
|
||||
|
|
@ -285,6 +286,19 @@ fn run_passes_inner<'tcx>(
|
|||
continue;
|
||||
};
|
||||
|
||||
if is_optimization_stage(body, phase_change, optimizations)
|
||||
&& let Some(limit) = &tcx.sess.opts.unstable_opts.mir_opt_bisect_limit
|
||||
{
|
||||
if limited_by_opt_bisect(
|
||||
tcx,
|
||||
tcx.def_path_debug_str(body.source.def_id()),
|
||||
*limit,
|
||||
*pass,
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let dumper = if pass.is_mir_dump_enabled()
|
||||
&& let Some(dumper) = MirDumper::new(tcx, pass_name, body)
|
||||
{
|
||||
|
|
@ -356,3 +370,46 @@ pub(super) fn dump_mir_for_phase_change<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tc
|
|||
dumper.set_show_pass_num().set_disambiguator(&"after").dump_mir(body)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_optimization_stage(
|
||||
body: &Body<'_>,
|
||||
phase_change: Option<MirPhase>,
|
||||
optimizations: Optimizations,
|
||||
) -> bool {
|
||||
optimizations == Optimizations::Allowed
|
||||
&& body.phase == MirPhase::Runtime(RuntimePhase::PostCleanup)
|
||||
&& phase_change == Some(MirPhase::Runtime(RuntimePhase::Optimized))
|
||||
}
|
||||
|
||||
fn limited_by_opt_bisect<'tcx, P>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_path: String,
|
||||
limit: usize,
|
||||
pass: &P,
|
||||
) -> bool
|
||||
where
|
||||
P: MirPass<'tcx> + ?Sized,
|
||||
{
|
||||
let current_opt_bisect_count =
|
||||
tcx.sess.mir_opt_bisect_eval_count.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
let can_run = current_opt_bisect_count < limit;
|
||||
|
||||
if can_run {
|
||||
eprintln!(
|
||||
"BISECT: running pass ({}) {} on {}",
|
||||
current_opt_bisect_count + 1,
|
||||
pass.name(),
|
||||
def_path
|
||||
);
|
||||
} else {
|
||||
eprintln!(
|
||||
"BISECT: NOT running pass ({}) {} on {}",
|
||||
current_opt_bisect_count + 1,
|
||||
pass.name(),
|
||||
def_path
|
||||
);
|
||||
}
|
||||
|
||||
!can_run
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2481,6 +2481,9 @@ options! {
|
|||
mir_include_spans: MirIncludeSpans = (MirIncludeSpans::default(), parse_mir_include_spans, [UNTRACKED],
|
||||
"include extra comments in mir pretty printing, like line numbers and statement indices, \
|
||||
details about types, etc. (boolean for all passes, 'nll' to enable in NLL MIR only, default: 'nll')"),
|
||||
mir_opt_bisect_limit: Option<usize> = (None, parse_opt_number, [TRACKED],
|
||||
"limit the number of MIR optimization pass executions (global across all bodies). \
|
||||
Pass executions after this limit are skipped and reported. (default: no limit)"),
|
||||
#[rustc_lint_opt_deny_field_access("use `Session::mir_opt_level` instead of this field")]
|
||||
mir_opt_level: Option<usize> = (None, parse_opt_number, [TRACKED],
|
||||
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::any::Any;
|
|||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize};
|
||||
use std::{env, io};
|
||||
|
||||
use rand::{RngCore, rng};
|
||||
|
|
@ -161,6 +161,12 @@ pub struct Session {
|
|||
|
||||
/// Does the codegen backend support ThinLTO?
|
||||
pub thin_lto_supported: bool,
|
||||
|
||||
/// Global per-session counter for MIR optimization pass applications.
|
||||
///
|
||||
/// Used by `-Zmir-opt-bisect-limit` to assign an index to each
|
||||
/// optimization-pass execution candidate during this compilation.
|
||||
pub mir_opt_bisect_eval_count: AtomicUsize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
|
@ -1101,6 +1107,7 @@ pub fn build_session(
|
|||
invocation_temp,
|
||||
replaced_intrinsics: FxHashSet::default(), // filled by `run_compiler`
|
||||
thin_lto_supported: true, // filled by `run_compiler`
|
||||
mir_opt_bisect_eval_count: AtomicUsize::new(0),
|
||||
};
|
||||
|
||||
validate_commandline_args_with_session_available(&sess);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue