Don't "simplify" during optimizations if optimizations are disabled

This commit is contained in:
Dylan MacKenzie 2021-12-02 14:38:04 -08:00
parent 42e31fffc4
commit dc5feeb1fa
2 changed files with 37 additions and 6 deletions

View file

@ -35,7 +35,7 @@ use rustc_span::{Span, Symbol};
#[macro_use] #[macro_use]
mod pass_manager; mod pass_manager;
use pass_manager::{self as pm, Lint, MirLint}; use pass_manager::{self as pm, Lint, MirLint, WithMinOptLevel};
mod abort_unwinding_calls; mod abort_unwinding_calls;
mod add_call_guards; mod add_call_guards;
@ -438,6 +438,10 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
} }
fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
fn o1<T>(x: T) -> WithMinOptLevel<T> {
WithMinOptLevel(1, x)
}
// Lowering generator control-flow and variables has to happen before we do anything else // Lowering generator control-flow and variables has to happen before we do anything else
// to them. We run some optimizations before that, because they may be harder to do on the state // to them. We run some optimizations before that, because they may be harder to do on the state
// machine than on MIR with async primitives. // machine than on MIR with async primitives.
@ -450,7 +454,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering &normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
&unreachable_prop::UnreachablePropagation, &unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching, &uninhabited_enum_branching::UninhabitedEnumBranching,
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"), &o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
&inline::Inline, &inline::Inline,
&generator::StateTransform, &generator::StateTransform,
], ],
@ -472,17 +476,21 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&multiple_return_terminators::MultipleReturnTerminators, &multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine, &instcombine::InstCombine,
&separate_const_switch::SeparateConstSwitch, &separate_const_switch::SeparateConstSwitch,
//
// FIXME(#70073): This pass is responsible for both optimization as well as some lints. // FIXME(#70073): This pass is responsible for both optimization as well as some lints.
&const_prop::ConstProp, &const_prop::ConstProp,
&simplify_branches::SimplifyBranches::new("after-const-prop"), //
// FIXME: The old pass manager ran this only at mir-opt-level >= 1, but
// const-prop runs unconditionally. Should this run unconditionally as well?
&o1(simplify_branches::SimplifyConstCondition::new("after-const-prop")),
&early_otherwise_branch::EarlyOtherwiseBranch, &early_otherwise_branch::EarlyOtherwiseBranch,
&simplify_comparison_integral::SimplifyComparisonIntegral, &simplify_comparison_integral::SimplifyComparisonIntegral,
&simplify_try::SimplifyArmIdentity, &simplify_try::SimplifyArmIdentity,
&simplify_try::SimplifyBranchSame, &simplify_try::SimplifyBranchSame,
&dest_prop::DestinationPropagation, &dest_prop::DestinationPropagation,
&simplify_branches::SimplifyBranches::new("final"), &o1(simplify_branches::SimplifyConstCondition::new("final")),
&remove_noop_landing_pads::RemoveNoopLandingPads, &o1(remove_noop_landing_pads::RemoveNoopLandingPads),
&simplify::SimplifyCfg::new("final"), &o1(simplify::SimplifyCfg::new("final")),
&nrvo::RenameReturnPlace, &nrvo::RenameReturnPlace,
&const_debuginfo::ConstDebugInfo, &const_debuginfo::ConstDebugInfo,
&simplify::SimplifyLocals, &simplify::SimplifyLocals,

View file

@ -49,6 +49,29 @@ where
} }
} }
pub struct WithMinOptLevel<T>(pub u32, pub T);
impl<T> MirPass<'tcx> for WithMinOptLevel<T>
where
T: MirPass<'tcx>,
{
fn name(&self) -> Cow<'_, str> {
self.1.name()
}
fn is_enabled(&self, sess: &Session) -> bool {
sess.mir_opt_level() >= self.0 as usize
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
self.1.run_pass(tcx, body)
}
fn phase_change(&self) -> Option<MirPhase> {
self.1.phase_change()
}
}
pub fn run_passes(tcx: TyCtxt<'tcx>, body: &'mir mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) { pub fn run_passes(tcx: TyCtxt<'tcx>, body: &'mir mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) {
let start_phase = body.phase; let start_phase = body.phase;
let mut cnt = 0; let mut cnt = 0;