Skip to content

Commit e2bf4b9

Browse files
committed
Do one round of DestProp at mir-opt-level=2
1 parent ed97493 commit e2bf4b9

17 files changed

+216
-252
lines changed

compiler/rustc_mir_transform/src/dest_prop.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,18 @@ pub struct DestinationPropagation;
149149

150150
impl<'tcx> MirPass<'tcx> for DestinationPropagation {
151151
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
152-
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
153-
// turned on by default:
154-
// 1. Because of the overeager removal of storage statements, this can cause stack space
155-
// regressions. This opt is not the place to fix this though, it's a more general
156-
// problem in MIR.
157-
// 2. Despite being an overall perf improvement, this still causes a 30% regression in
158-
// keccak. We can temporarily fix this by bounding function size, but in the long term
159-
// we should fix this by being smarter about invalidating analysis results.
160-
sess.mir_opt_level() >= 3
152+
// This pass is technically enabled at MIR opt level 2, but in a reduced form.
153+
// At MIR opt level 2, we run a single round, and at MIR opt level 3 or greater we keep
154+
// running rounds until we reach a fixed point. Based on experimentation with the rustc
155+
// benchmark suite, the majority of the benefit from this pass comes from the first round,
156+
// though on some code it continues to find optimizations for >10 rounds.
157+
// It may be possible to enable multiple rounds at MIR opt level 2 by being more careful
158+
// about invalidating analysis results.
159+
//
160+
// Note that due to overeager removal of storage statements, this pass (particularly at MIR
161+
// opt level 3), can cause stack space regressions. This opt is not the place to fix this
162+
// though, it's a more general problem in MIR.
163+
sess.mir_opt_level() >= 2
161164
}
162165

163166
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -236,6 +239,11 @@ impl<'tcx> MirPass<'tcx> for DestinationPropagation {
236239
round_count += 1;
237240

238241
apply_merges(body, tcx, &merges, &merged_locals);
242+
243+
// At MIR opt level 2, we only run one iteration.
244+
if tcx.sess.mir_opt_level() < 3 {
245+
break;
246+
}
239247
}
240248

241249
trace!(round_count);

src/test/codegen/fewer-names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub fn sum(x: u32, y: u32) -> u32 {
1313

1414
// NO-LABEL: define{{.*}}i32 @sum(i32 %x, i32 %y)
1515
// NO-NEXT: start:
16-
// NO-NEXT: %z = add i32 %y, %x
17-
// NO-NEXT: ret i32 %z
16+
// NO-NEXT: %0 = add i32 %y, %x
17+
// NO-NEXT: ret i32 %0
1818
let z = x + y;
1919
z
2020
}

0 commit comments

Comments
 (0)