Skip to content

Commit ed27cb0

Browse files
committed
Reorder passes.
1 parent 31bc7e2 commit ed27cb0

7 files changed

+67
-41
lines changed

compiler/rustc_mir_transform/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,11 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
568568
&[
569569
&check_alignment::CheckAlignment,
570570
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
571-
&unreachable_prop::UnreachablePropagation,
571+
&inline::Inline,
572+
// Substitutions during inlining may introduce switch on enums with uninhabited branches.
572573
&uninhabited_enum_branching::UninhabitedEnumBranching,
574+
&unreachable_prop::UnreachablePropagation,
573575
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
574-
&inline::Inline,
575576
&remove_storage_markers::RemoveStorageMarkers,
576577
&remove_zsts::RemoveZsts,
577578
&normalize_array_len::NormalizeArrayLen, // has to run after `slice::len` lowering

compiler/rustc_mir_transform/src/unreachable_prop.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! when all of their successors are unreachable. This is achieved through a
33
//! post-order traversal of the blocks.
44
5-
use crate::simplify;
65
use crate::MirPass;
76
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
87
use rustc_middle::mir::*;
@@ -52,8 +51,6 @@ impl MirPass<'_> for UnreachablePropagation {
5251
body.basic_blocks_mut()[bb].statements.clear();
5352
}
5453

55-
let replaced = !replacements.is_empty();
56-
5754
for (bb, terminator_kind) in replacements {
5855
if !tcx.consider_optimizing(|| {
5956
format!("UnreachablePropagation {:?} ", body.source.def_id())
@@ -64,9 +61,7 @@ impl MirPass<'_> for UnreachablePropagation {
6461
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
6562
}
6663

67-
if replaced {
68-
simplify::remove_dead_blocks(body);
69-
}
64+
// Do not remove dead blocks, let `SimplifyCfg` do it.
7065
}
7166
}
7267

tests/mir-opt/separate_const_switch.identity.SeparateConstSwitch.diff

+1-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
StorageLive(_10);
5353
StorageLive(_11);
5454
_9 = discriminant(_1);
55-
switchInt(move _9) -> [0: bb7, 1: bb5, otherwise: bb6];
55+
switchInt(move _9) -> [0: bb6, 1: bb5, otherwise: bb3];
5656
}
5757

5858
bb1: {
@@ -92,10 +92,6 @@
9292
}
9393

9494
bb6: {
95-
unreachable;
96-
}
97-
98-
bb7: {
9995
_10 = ((_1 as Ok).0: i32);
10096
_3 = ControlFlow::<Result<Infallible, i32>, i32>::Continue(move _10);
10197
goto -> bb1;

tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff

+14-13
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
bb1: {
2626
_2 = discriminant(_1);
27-
- switchInt(move _2) -> [1: bb2, otherwise: bb6];
28-
+ switchInt(move _2) -> [1: bb2, otherwise: bb3];
27+
switchInt(move _2) -> [1: bb2, otherwise: bb6];
2928
}
3029

3130
bb2: {
@@ -36,29 +35,31 @@
3635
- StorageLive(_6);
3736
- _6 = const true;
3837
- switchInt(move _6) -> [0: bb4, otherwise: bb3];
39-
- }
40-
-
41-
- bb3: {
38+
+ unreachable;
39+
}
40+
41+
bb3: {
4242
- _4 = const 21_i32;
4343
- _5 = const ();
4444
- goto -> bb5;
45-
- }
46-
-
47-
- bb4: {
45+
+ unreachable;
46+
}
47+
48+
bb4: {
4849
- _4 = const 42_i32;
4950
- _5 = const ();
5051
- goto -> bb5;
51-
- }
52-
-
53-
- bb5: {
52+
+ unreachable;
53+
}
54+
55+
bb5: {
5456
- StorageDead(_6);
5557
- StorageDead(_5);
5658
- StorageLive(_7);
5759
unreachable;
5860
}
5961

60-
- bb6: {
61-
+ bb3: {
62+
bb6: {
6263
_0 = const ();
6364
StorageDead(_1);
6465
return;

tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff

+14-13
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
bb1: {
2626
_2 = discriminant(_1);
27-
- switchInt(move _2) -> [1: bb2, otherwise: bb6];
28-
+ switchInt(move _2) -> [1: bb2, otherwise: bb3];
27+
switchInt(move _2) -> [1: bb2, otherwise: bb6];
2928
}
3029

3130
bb2: {
@@ -36,29 +35,31 @@
3635
- StorageLive(_6);
3736
- _6 = const true;
3837
- switchInt(move _6) -> [0: bb4, otherwise: bb3];
39-
- }
40-
-
41-
- bb3: {
38+
+ unreachable;
39+
}
40+
41+
bb3: {
4242
- _4 = const 21_i32;
4343
- _5 = const ();
4444
- goto -> bb5;
45-
- }
46-
-
47-
- bb4: {
45+
+ unreachable;
46+
}
47+
48+
bb4: {
4849
- _4 = const 42_i32;
4950
- _5 = const ();
5051
- goto -> bb5;
51-
- }
52-
-
53-
- bb5: {
52+
+ unreachable;
53+
}
54+
55+
bb5: {
5456
- StorageDead(_6);
5557
- StorageDead(_5);
5658
- StorageLive(_7);
5759
unreachable;
5860
}
5961

60-
- bb6: {
61-
+ bb3: {
62+
bb6: {
6263
_0 = const ();
6364
StorageDead(_1);
6465
return;

tests/mir-opt/unreachable.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// skip-filecheck
1+
// unit-test: UnreachablePropagation
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3+
34
enum Empty {}
45

56
fn empty() -> Option<Empty> {
@@ -8,6 +9,21 @@ fn empty() -> Option<Empty> {
89

910
// EMIT_MIR unreachable.main.UnreachablePropagation.diff
1011
fn main() {
12+
// CHECK-LABEL: fn main(
13+
// CHECK: bb0: {
14+
// CHECK: {{_.*}} = empty()
15+
// CHECK: bb1: {
16+
// CHECK: switchInt({{.*}}) -> [1: bb2, otherwise: bb6];
17+
// CHECK: bb2: {
18+
// CHECK: unreachable;
19+
// CHECK: bb3: {
20+
// CHECK: unreachable;
21+
// CHECK: bb4: {
22+
// CHECK: unreachable;
23+
// CHECK: bb5: {
24+
// CHECK: unreachable;
25+
// CHECK: bb6: {
26+
// CHECK: return;
1127
if let Some(_x) = empty() {
1228
let mut _y;
1329

tests/mir-opt/unreachable_diverging.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// skip-filecheck
1+
// unit-test: UnreachablePropagation
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
3+
34
pub enum Empty {}
45

56
fn empty() -> Option<Empty> {
@@ -12,6 +13,21 @@ fn loop_forever() {
1213

1314
// EMIT_MIR unreachable_diverging.main.UnreachablePropagation.diff
1415
fn main() {
16+
// CHECK-LABEL: fn main(
17+
// CHECK: bb0: {
18+
// CHECK: {{_.*}} = empty()
19+
// CHECK: bb1: {
20+
// CHECK: switchInt({{.*}}) -> [1: bb2, otherwise: bb6];
21+
// CHECK: bb2: {
22+
// CHECK: switchInt({{.*}}) -> [0: bb4, otherwise: bb3];
23+
// CHECK: bb3: {
24+
// CHECK: {{_.*}} = loop_forever()
25+
// CHECK: bb4: {
26+
// CHECK: unreachable;
27+
// CHECK: bb5: {
28+
// CHECK: unreachable;
29+
// CHECK: bb6: {
30+
// CHECK: return;
1531
let x = true;
1632
if let Some(bomb) = empty() {
1733
if x {

0 commit comments

Comments
 (0)