Skip to content

Commit dc5feeb

Browse files
Don't "simplify" during optimizations if optimizations are disabled
1 parent 42e31ff commit dc5feeb

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

compiler/rustc_mir_transform/src/lib.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_span::{Span, Symbol};
3535
#[macro_use]
3636
mod pass_manager;
3737

38-
use pass_manager::{self as pm, Lint, MirLint};
38+
use pass_manager::{self as pm, Lint, MirLint, WithMinOptLevel};
3939

4040
mod abort_unwinding_calls;
4141
mod add_call_guards;
@@ -438,6 +438,10 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
438438
}
439439

440440
fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
441+
fn o1<T>(x: T) -> WithMinOptLevel<T> {
442+
WithMinOptLevel(1, x)
443+
}
444+
441445
// Lowering generator control-flow and variables has to happen before we do anything else
442446
// to them. We run some optimizations before that, because they may be harder to do on the state
443447
// machine than on MIR with async primitives.
@@ -450,7 +454,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
450454
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering
451455
&unreachable_prop::UnreachablePropagation,
452456
&uninhabited_enum_branching::UninhabitedEnumBranching,
453-
&simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
457+
&o1(simplify::SimplifyCfg::new("after-uninhabited-enum-branching")),
454458
&inline::Inline,
455459
&generator::StateTransform,
456460
],
@@ -472,17 +476,21 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
472476
&multiple_return_terminators::MultipleReturnTerminators,
473477
&instcombine::InstCombine,
474478
&separate_const_switch::SeparateConstSwitch,
479+
//
475480
// FIXME(#70073): This pass is responsible for both optimization as well as some lints.
476481
&const_prop::ConstProp,
477-
&simplify_branches::SimplifyBranches::new("after-const-prop"),
482+
//
483+
// FIXME: The old pass manager ran this only at mir-opt-level >= 1, but
484+
// const-prop runs unconditionally. Should this run unconditionally as well?
485+
&o1(simplify_branches::SimplifyConstCondition::new("after-const-prop")),
478486
&early_otherwise_branch::EarlyOtherwiseBranch,
479487
&simplify_comparison_integral::SimplifyComparisonIntegral,
480488
&simplify_try::SimplifyArmIdentity,
481489
&simplify_try::SimplifyBranchSame,
482490
&dest_prop::DestinationPropagation,
483-
&simplify_branches::SimplifyBranches::new("final"),
484-
&remove_noop_landing_pads::RemoveNoopLandingPads,
485-
&simplify::SimplifyCfg::new("final"),
491+
&o1(simplify_branches::SimplifyConstCondition::new("final")),
492+
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
493+
&o1(simplify::SimplifyCfg::new("final")),
486494
&nrvo::RenameReturnPlace,
487495
&const_debuginfo::ConstDebugInfo,
488496
&simplify::SimplifyLocals,

compiler/rustc_mir_transform/src/pass_manager.rs

+23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@ where
4949
}
5050
}
5151

52+
pub struct WithMinOptLevel<T>(pub u32, pub T);
53+
54+
impl<T> MirPass<'tcx> for WithMinOptLevel<T>
55+
where
56+
T: MirPass<'tcx>,
57+
{
58+
fn name(&self) -> Cow<'_, str> {
59+
self.1.name()
60+
}
61+
62+
fn is_enabled(&self, sess: &Session) -> bool {
63+
sess.mir_opt_level() >= self.0 as usize
64+
}
65+
66+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
67+
self.1.run_pass(tcx, body)
68+
}
69+
70+
fn phase_change(&self) -> Option<MirPhase> {
71+
self.1.phase_change()
72+
}
73+
}
74+
5275
pub fn run_passes(tcx: TyCtxt<'tcx>, body: &'mir mut Body<'tcx>, passes: &[&dyn MirPass<'tcx>]) {
5376
let start_phase = body.phase;
5477
let mut cnt = 0;

0 commit comments

Comments
 (0)