Skip to content

Commit 620a27a

Browse files
committed
mir-opt: a sub-BB of a cleanup BB must also be a cleanup BB
1 parent 11e760b commit 620a27a

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,15 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
183183
let eq_targets = SwitchTargets::new(eq_new_targets, parent_targets.otherwise());
184184

185185
// Create `bbEq` in example above
186-
let eq_switch = BasicBlockData::new(Some(Terminator {
186+
let mut eq_switch = BasicBlockData::new(Some(Terminator {
187187
source_info: bbs[parent].terminator().source_info,
188188
kind: TerminatorKind::SwitchInt {
189189
// switch on the first discriminant, so we can mark the second one as dead
190190
discr: parent_op,
191191
targets: eq_targets,
192192
},
193193
}));
194+
eq_switch.is_cleanup = bbs[parent].is_cleanup;
194195

195196
let eq_bb = patch.new_block(eq_switch);
196197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
- // MIR for `cleanup` before EarlyOtherwiseBranch
2+
+ // MIR for `cleanup` after EarlyOtherwiseBranch
3+
4+
fn cleanup(_1: Option<Option<Option<T>>>) -> () {
5+
debug val => _1;
6+
let mut _0: ();
7+
let mut _2: isize;
8+
let mut _3: isize;
9+
let mut _4: isize;
10+
let _5: T;
11+
let mut _6: bool;
12+
let mut _7: bool;
13+
let mut _8: isize;
14+
+ let mut _9: bool;
15+
scope 1 {
16+
debug _v => _5;
17+
}
18+
19+
bb0: {
20+
_6 = const false;
21+
_7 = const false;
22+
_6 = const true;
23+
_7 = const true;
24+
_4 = discriminant(_1);
25+
switchInt(copy _4) -> [0: bb4, 1: bb2, otherwise: bb1];
26+
}
27+
28+
bb1: {
29+
unreachable;
30+
}
31+
32+
bb2: {
33+
_3 = discriminant(((_1 as Some).0: std::option::Option<std::option::Option<T>>));
34+
switchInt(copy _3) -> [0: bb5, 1: bb3, otherwise: bb1];
35+
}
36+
37+
bb3: {
38+
_2 = discriminant(((((_1 as Some).0: std::option::Option<std::option::Option<T>>) as Some).0: std::option::Option<T>));
39+
switchInt(copy _2) -> [0: bb6, 1: bb7, otherwise: bb1];
40+
}
41+
42+
bb4: {
43+
_0 = const ();
44+
- goto -> bb17;
45+
+ goto -> bb15;
46+
}
47+
48+
bb5: {
49+
_0 = const ();
50+
- goto -> bb17;
51+
+ goto -> bb15;
52+
}
53+
54+
bb6: {
55+
_0 = const ();
56+
- goto -> bb17;
57+
+ goto -> bb15;
58+
}
59+
60+
bb7: {
61+
StorageLive(_5);
62+
_5 = move ((((((_1 as Some).0: std::option::Option<std::option::Option<T>>) as Some).0: std::option::Option<T>) as Some).0: T);
63+
_0 = const ();
64+
- drop(_5) -> [return: bb8, unwind: bb20];
65+
+ drop(_5) -> [return: bb8, unwind: bb16];
66+
}
67+
68+
bb8: {
69+
StorageDead(_5);
70+
- goto -> bb17;
71+
+ goto -> bb15;
72+
}
73+
74+
bb9 (cleanup): {
75+
+ StorageDead(_9);
76+
resume;
77+
}
78+
79+
bb10: {
80+
return;
81+
}
82+
83+
bb11: {
84+
- switchInt(copy _6) -> [0: bb12, otherwise: bb16];
85+
+ switchInt(copy _6) -> [0: bb12, otherwise: bb14];
86+
}
87+
88+
bb12: {
89+
_6 = const false;
90+
goto -> bb10;
91+
}
92+
93+
bb13: {
94+
- switchInt(copy _7) -> [0: bb14, otherwise: bb15];
95+
- }
96+
-
97+
- bb14: {
98+
_7 = const false;
99+
goto -> bb12;
100+
}
101+
102+
- bb15: {
103+
- goto -> bb14;
104+
- }
105+
-
106+
- bb16: {
107+
+ bb14: {
108+
_8 = discriminant(((_1 as Some).0: std::option::Option<std::option::Option<T>>));
109+
switchInt(move _8) -> [1: bb13, otherwise: bb12];
110+
}
111+
112+
- bb17: {
113+
+ bb15: {
114+
switchInt(copy _4) -> [1: bb11, otherwise: bb10];
115+
}
116+
117+
- bb18 (cleanup): {
118+
- switchInt(copy _3) -> [1: bb19, otherwise: bb9];
119+
+ bb16 (cleanup): {
120+
+ StorageLive(_9);
121+
+ _9 = Ne(copy _4, copy _3);
122+
+ switchInt(move _9) -> [0: bb17, otherwise: bb9];
123+
}
124+
125+
- bb19 (cleanup): {
126+
+ bb17 (cleanup): {
127+
+ StorageDead(_9);
128+
goto -> bb9;
129+
- }
130+
-
131+
- bb20 (cleanup): {
132+
- switchInt(copy _4) -> [1: bb18, otherwise: bb9];
133+
}
134+
}
135+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ test-mir-pass: EarlyOtherwiseBranch
2+
//@ compile-flags: -Zmir-enable-passes=+GVN,+SimplifyLocals-after-value-numbering
3+
4+
// Ensure that the new sub-BB we create is also a cleanup BB.
5+
6+
// EMIT_MIR early_otherwise_cleanup_branch.cleanup.EarlyOtherwiseBranch.diff
7+
fn cleanup<T>(val: Option<Option<Option<T>>>) {
8+
// CHECK-LABEL: fn cleanup(
9+
// CHECK: drop({{.*}}) -> [return: bb{{.*}}, unwind: [[PARENT_UNWIND_BB:bb.*]]];
10+
// CHECK: [[PARENT_UNWIND_BB]] (cleanup): {
11+
// CHECK-NEXT: StorageLive(_9);
12+
// CHECK-NEXT: [[CMP_LOCAL:_.*]] = Ne
13+
// CHECK-NEXT: switchInt(move [[CMP_LOCAL]]) -> [0: [[NEW_UNWIND_BB:bb.*]], otherwise
14+
// CHECK: [[NEW_UNWIND_BB]] (cleanup): {
15+
match val {
16+
Some(Some(Some(_v))) => {}
17+
Some(Some(None)) => {}
18+
Some(None) => {}
19+
None => {}
20+
}
21+
}

0 commit comments

Comments
 (0)