Skip to content

Commit

Permalink
Don't "simplify" during optimizations if optimizations are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstatic-morse committed Dec 3, 2021
1 parent 42e31ff commit dc5feeb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
20 changes: 14 additions & 6 deletions compiler/rustc_mir_transform/src/lib.rs
Expand Up @@ -35,7 +35,7 @@ use rustc_span::{Span, Symbol};
#[macro_use]
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 add_call_guards;
Expand Down Expand Up @@ -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 o1<T>(x: T) -> WithMinOptLevel<T> {
WithMinOptLevel(1, x)
}

// 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
// machine than on MIR with async primitives.
Expand All @@ -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
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
&inline::Inline,
&generator::StateTransform,
],
Expand All @@ -472,17 +476,21 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&multiple_return_terminators::MultipleReturnTerminators,
&instcombine::InstCombine,
&separate_const_switch::SeparateConstSwitch,
//
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
&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,
&simplify_comparison_integral::SimplifyComparisonIntegral,
&simplify_try::SimplifyArmIdentity,
&simplify_try::SimplifyBranchSame,
&dest_prop::DestinationPropagation,
&simplify_branches::SimplifyBranches::new("final"),
&remove_noop_landing_pads::RemoveNoopLandingPads,
&simplify::SimplifyCfg::new("final"),
&o1(simplify_branches::SimplifyConstCondition::new("final")),
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
&o1(simplify::SimplifyCfg::new("final")),
&nrvo::RenameReturnPlace,
&const_debuginfo::ConstDebugInfo,
&simplify::SimplifyLocals,
Expand Down
23 changes: 23 additions & 0 deletions compiler/rustc_mir_transform/src/pass_manager.rs
Expand Up @@ -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>]) {
let start_phase = body.phase;
let mut cnt = 0;
Expand Down

0 comments on commit dc5feeb

Please sign in to comment.