Skip to content

Commit

Permalink
Do one round of DestProp at mir-opt-level=2
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Dec 13, 2022
1 parent ed97493 commit e2bf4b9
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 252 deletions.
26 changes: 17 additions & 9 deletions compiler/rustc_mir_transform/src/dest_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,18 @@ pub struct DestinationPropagation;

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

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

apply_merges(body, tcx, &merges, &merged_locals);

// At MIR opt level 2, we only run one iteration.
if tcx.sess.mir_opt_level() < 3 {
break;
}
}

trace!(round_count);
Expand Down
4 changes: 2 additions & 2 deletions src/test/codegen/fewer-names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub fn sum(x: u32, y: u32) -> u32 {

// NO-LABEL: define{{.*}}i32 @sum(i32 %x, i32 %y)
// NO-NEXT: start:
// NO-NEXT: %z = add i32 %y, %x
// NO-NEXT: ret i32 %z
// NO-NEXT: %0 = add i32 %y, %x
// NO-NEXT: ret i32 %0
let z = x + y;
z
}
Loading

0 comments on commit e2bf4b9

Please sign in to comment.