Skip to content

Commit 4a03f14

Browse files
committed
Auto merge of rust-lang#110569 - saethlin:mir-pass-cooperation, r=cjgillot
Deduplicate unreachable blocks, for real this time In rust-lang#106428 (in particular rust-lang@41eda69) we noticed that inlining `unreachable_unchecked` can produce duplicate unreachable blocks. So we improved two MIR optimizations: `SimplifyCfg` was given a simplify to deduplicate unreachable blocks, then `InstCombine` was given a combiner to deduplicate switch targets that point at the same block. The problem is that change doesn't actually work. Our current pass order is ``` SimplifyCfg (does nothing relevant to this situation) Inline (produces multiple unreachable blocks) InstCombine (doesn't do anything here, oops) SimplifyCfg (produces the duplicate SwitchTargets that InstCombine is looking for) ``` So in here, I have factored out the specific function from `InstCombine` and placed it inside the simplify that produces the case it is looking for. This should ensure that it runs in the scenario it was designed for. Fixes rust-lang#110551 r? `@cjgillot`
2 parents 4096619 + 8ec49ad commit 4a03f14

5 files changed

+61
-19
lines changed

compiler/rustc_mir_transform/src/instcombine.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! Performs various peephole optimizations.
22
3+
use crate::simplify::combine_duplicate_switch_targets;
34
use crate::MirPass;
45
use rustc_hir::Mutability;
5-
use rustc_middle::mir::{
6-
BinOp, Body, CastKind, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem,
7-
Rvalue, SourceInfo, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, UnOp,
8-
};
6+
use rustc_middle::mir::*;
97
use rustc_middle::ty::layout::ValidityRequirement;
108
use rustc_middle::ty::util::IntTypeExt;
119
use rustc_middle::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt};
@@ -46,7 +44,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
4644
&mut block.terminator.as_mut().unwrap(),
4745
&mut block.statements,
4846
);
49-
ctx.combine_duplicate_switch_targets(&mut block.terminator.as_mut().unwrap());
47+
combine_duplicate_switch_targets(block.terminator.as_mut().unwrap());
5048
}
5149
}
5250
}
@@ -264,19 +262,6 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
264262
terminator.kind = TerminatorKind::Goto { target: destination_block };
265263
}
266264

267-
fn combine_duplicate_switch_targets(&self, terminator: &mut Terminator<'tcx>) {
268-
let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind
269-
else { return };
270-
271-
let otherwise = targets.otherwise();
272-
if targets.iter().any(|t| t.1 == otherwise) {
273-
*targets = SwitchTargets::new(
274-
targets.iter().filter(|t| t.1 != otherwise),
275-
targets.otherwise(),
276-
);
277-
}
278-
}
279-
280265
fn combine_intrinsic_assert(
281266
&self,
282267
terminator: &mut Terminator<'tcx>,

compiler/rustc_mir_transform/src/simplify.rs

+14
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
278278
}
279279
}
280280

281+
pub fn combine_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
282+
if let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind {
283+
let otherwise = targets.otherwise();
284+
if targets.iter().any(|t| t.1 == otherwise) {
285+
*targets = SwitchTargets::new(
286+
targets.iter().filter(|t| t.1 != otherwise),
287+
targets.otherwise(),
288+
);
289+
}
290+
}
291+
}
292+
281293
pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
282294
struct OptApplier<'tcx> {
283295
tcx: TyCtxt<'tcx>,
@@ -298,6 +310,8 @@ pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut B
298310
}
299311
}
300312

313+
combine_duplicate_switch_targets(terminator);
314+
301315
self.super_terminator(terminator, location);
302316
}
303317
}

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- // + literal: Const { ty: unsafe fn(Option<T>) -> T {Option::<T>::unwrap_unchecked}, val: Value(<ZST>) }
3535
+ StorageLive(_3); // scope 0 at $DIR/unwrap_unchecked.rs:+1:9: +1:27
3636
+ _4 = discriminant(_2); // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
37-
+ switchInt(move _4) -> [0: bb1, 1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
37+
+ switchInt(move _4) -> [1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
3838
}
3939

4040
bb1: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -Zmir-opt-level=2 -Zinline-mir
2+
// ignore-debug: standard library debug assertions add a panic that breaks this optimization
3+
#![crate_type = "lib"]
4+
5+
pub enum Thing {
6+
A,
7+
B,
8+
}
9+
10+
// EMIT_MIR instcombine_duplicate_switch_targets_e2e.ub_if_b.PreCodegen.after.mir
11+
pub unsafe fn ub_if_b(t: Thing) -> Thing {
12+
match t {
13+
Thing::A => t,
14+
Thing::B => std::hint::unreachable_unchecked(),
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// MIR for `ub_if_b` after PreCodegen
2+
3+
fn ub_if_b(_1: Thing) -> Thing {
4+
debug t => _1; // in scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+0:23: +0:24
5+
let mut _0: Thing; // return place in scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+0:36: +0:41
6+
let mut _2: isize; // in scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+2:9: +2:17
7+
scope 1 (inlined unreachable_unchecked) { // at $DIR/instcombine_duplicate_switch_targets_e2e.rs:14:21: 14:55
8+
scope 2 {
9+
scope 3 (inlined unreachable_unchecked::runtime) { // at $SRC_DIR/core/src/intrinsics.rs:LL:COL
10+
}
11+
}
12+
}
13+
14+
bb0: {
15+
_2 = discriminant(_1); // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:11: +1:12
16+
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:5: +1:12
17+
}
18+
19+
bb1: {
20+
unreachable; // scope 2 at $SRC_DIR/core/src/intrinsics.rs:LL:COL
21+
}
22+
23+
bb2: {
24+
_0 = move _1; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+2:21: +2:22
25+
return; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+5:2: +5:2
26+
}
27+
}

0 commit comments

Comments
 (0)