Skip to content

Commit f59d577

Browse files
committed
Auto merge of #112001 - saethlin:enable-matchbranchsimplification, r=cjgillot
Enable MatchBranchSimplification This pass is one of the small number of benefits from `-Zmir-opt-level=3` that has motivated rustc_codegen_cranelift to use it: https://github.com/rust-lang/rust/blob/19ed0aade60e1c1038fe40554bcd9d01b717effa/compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs#L244-L246 Cranelift's motivation for this is _runtime_ performance improvements in debug builds. Lifting this pass all the way to `-Zmir-opt-level=1` seems to come without significant perf overhead, so that's what I'm suggesting here.
2 parents ddad057 + 7e04c93 commit f59d577

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub struct MatchBranchSimplification;
4141
4242
impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
4343
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
44-
sess.mir_opt_level() >= 3
44+
sess.mir_opt_level() >= 1
4545
}
4646

4747
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -62,7 +62,12 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
6262
..
6363
} if targets.iter().len() == 1 => {
6464
let (value, target) = targets.iter().next().unwrap();
65-
if target == targets.otherwise() {
65+
// We require that this block and the two possible target blocks all be
66+
// distinct.
67+
if target == targets.otherwise()
68+
|| bb_idx == target
69+
|| bb_idx == targets.otherwise()
70+
{
6671
continue;
6772
}
6873
(discr, value, target, targets.otherwise())

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl<'a, 'tcx> FulfillmentContext<'tcx> {
116116
}
117117

118118
impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
119+
#[inline]
119120
fn register_predicate_obligation(
120121
&mut self,
121122
infcx: &InferCtxt<'tcx>,

tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.diff

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
let mut _3: std::option::Option<T>; // in scope 0 at $DIR/simplify_locals_fixedpoint.rs:+1:51: +1:68
99
let mut _4: isize; // in scope 0 at $DIR/simplify_locals_fixedpoint.rs:+1:22: +1:26
1010
let mut _5: isize; // in scope 0 at $DIR/simplify_locals_fixedpoint.rs:+1:13: +1:20
11-
- let mut _7: bool; // in scope 0 at $DIR/simplify_locals_fixedpoint.rs:+2:12: +2:20
12-
- let mut _8: u8; // in scope 0 at $DIR/simplify_locals_fixedpoint.rs:+2:12: +2:13
1311
scope 1 {
1412
debug a => _6; // in scope 1 at $DIR/simplify_locals_fixedpoint.rs:+1:18: +1:19
1513
let _6: u8; // in scope 1 at $DIR/simplify_locals_fixedpoint.rs:+1:18: +1:19
@@ -34,10 +32,9 @@
3432
}
3533

3634
bb2: {
35+
StorageLive(_6); // scope 1 at $DIR/simplify_locals_fixedpoint.rs:+1:18: +1:19
3736
_6 = (((_1.0: std::option::Option<u8>) as Some).0: u8); // scope 1 at $DIR/simplify_locals_fixedpoint.rs:+1:18: +1:19
38-
- StorageLive(_7); // scope 1 at $DIR/simplify_locals_fixedpoint.rs:+2:12: +2:20
39-
- _7 = Gt(_6, const 42_u8); // scope 1 at $DIR/simplify_locals_fixedpoint.rs:+2:12: +2:20
40-
- StorageDead(_7); // scope 1 at $DIR/simplify_locals_fixedpoint.rs:+4:9: +4:10
37+
StorageDead(_6); // scope 0 at $DIR/simplify_locals_fixedpoint.rs:+5:5: +5:6
4138
goto -> bb3; // scope 0 at $DIR/simplify_locals_fixedpoint.rs:+1:5: +5:6
4239
}
4340

tests/mir-opt/switch_to_self.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Test that MatchBranchSimplification doesn't ICE on a SwitchInt where
2+
// one of the targets is the block that the SwitchInt terminates.
3+
#![crate_type = "lib"]
4+
#![feature(core_intrinsics, custom_mir)]
5+
use std::intrinsics::mir::*;
6+
7+
// EMIT_MIR switch_to_self.test.MatchBranchSimplification.diff
8+
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
9+
pub fn test(x: bool) {
10+
mir!(
11+
{
12+
Goto(bb0)
13+
}
14+
bb0 = {
15+
match x { false => bb0, _ => bb1 }
16+
}
17+
bb1 = {
18+
match x { false => bb0, _ => bb1 }
19+
}
20+
)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- // MIR for `test` before MatchBranchSimplification
2+
+ // MIR for `test` after MatchBranchSimplification
3+
4+
fn test(_1: bool) -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/switch_to_self.rs:+0:22: +0:22
6+
7+
bb0: {
8+
goto -> bb1; // scope 0 at $DIR/switch_to_self.rs:+3:13: +3:22
9+
}
10+
11+
bb1: {
12+
switchInt(_1) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/switch_to_self.rs:+6:13: +6:47
13+
}
14+
15+
bb2: {
16+
switchInt(_1) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/switch_to_self.rs:+9:13: +9:47
17+
}
18+
}
19+

0 commit comments

Comments
 (0)