Skip to content

Commit d08738c

Browse files
committed
mir-opt: Do not handle the cleanup BB in the EarlyOtherwiseBranch
1 parent 93aea1d commit d08738c

4 files changed

+53
-86
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ fn evaluate_candidate<'tcx>(
244244
parent: BasicBlock,
245245
) -> Option<OptimizationData<'tcx>> {
246246
let bbs = &body.basic_blocks;
247+
// NB: If this BB is a cleanup, we may need to figure out what else needs to be handled.
248+
if bbs[parent].is_cleanup {
249+
return None;
250+
}
247251
let TerminatorKind::SwitchInt { targets, discr: parent_discr } = &bbs[parent].terminator().kind
248252
else {
249253
return None;

tests/mir-opt/early_otherwise_branch_unwind.poll.EarlyOtherwiseBranch.diff

+23-39
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
let mut _7: bool;
1313
let mut _8: bool;
1414
let mut _9: isize;
15-
+ let mut _10: bool;
1615
scope 1 {
1716
debug _trailers => _5;
1817
}
@@ -45,41 +44,35 @@
4544

4645
bb4: {
4746
_0 = const ();
48-
- goto -> bb17;
49-
+ goto -> bb15;
47+
goto -> bb17;
5048
}
5149

5250
bb5: {
5351
_0 = const ();
54-
- goto -> bb17;
55-
+ goto -> bb15;
52+
goto -> bb17;
5653
}
5754

5855
bb6: {
5956
StorageLive(_6);
6057
_6 = copy ((((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>) as Err).0: u8);
6158
_0 = const ();
6259
StorageDead(_6);
63-
- goto -> bb17;
64-
+ goto -> bb15;
60+
goto -> bb17;
6561
}
6662

6763
bb7: {
6864
StorageLive(_5);
6965
_5 = move ((((((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>) as Ok).0: std::option::Option<std::vec::Vec<u8>>) as Some).0: std::vec::Vec<u8>);
7066
_0 = const ();
71-
- drop(_5) -> [return: bb8, unwind: bb20];
72-
+ drop(_5) -> [return: bb8, unwind: bb16];
67+
drop(_5) -> [return: bb8, unwind: bb20];
7368
}
7469

7570
bb8: {
7671
StorageDead(_5);
77-
- goto -> bb17;
78-
+ goto -> bb15;
72+
goto -> bb17;
7973
}
8074

8175
bb9 (cleanup): {
82-
+ StorageDead(_10);
8376
resume;
8477
}
8578

@@ -88,8 +81,7 @@
8881
}
8982

9083
bb11: {
91-
- switchInt(copy _7) -> [0: bb12, otherwise: bb16];
92-
+ switchInt(copy _7) -> [0: bb12, otherwise: bb14];
84+
switchInt(copy _7) -> [0: bb12, otherwise: bb16];
9385
}
9486

9587
bb12: {
@@ -98,45 +90,37 @@
9890
}
9991

10092
bb13: {
101-
- switchInt(copy _8) -> [0: bb14, otherwise: bb15];
102-
- }
103-
-
104-
- bb14: {
93+
switchInt(copy _8) -> [0: bb14, otherwise: bb15];
94+
}
95+
96+
bb14: {
10597
_8 = const false;
10698
goto -> bb12;
10799
}
108100

109-
- bb15: {
110-
- goto -> bb14;
111-
- }
112-
-
113-
- bb16: {
114-
+ bb14: {
101+
bb15: {
102+
goto -> bb14;
103+
}
104+
105+
bb16: {
115106
_9 = discriminant(((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>));
116107
switchInt(move _9) -> [0: bb13, otherwise: bb12];
117108
}
118109

119-
- bb17: {
120-
+ bb15: {
110+
bb17: {
121111
switchInt(copy _4) -> [0: bb11, otherwise: bb10];
122112
}
123113

124-
- bb18 (cleanup): {
125-
- switchInt(copy _3) -> [0: bb19, otherwise: bb9];
126-
+ bb16 (cleanup): {
127-
+ StorageLive(_10);
128-
+ _10 = Ne(copy _4, copy _3);
129-
+ switchInt(move _10) -> [0: bb17, otherwise: bb9];
114+
bb18 (cleanup): {
115+
switchInt(copy _3) -> [0: bb19, otherwise: bb9];
130116
}
131117

132-
- bb19 (cleanup): {
133-
+ bb17 (cleanup): {
134-
+ StorageDead(_10);
118+
bb19 (cleanup): {
135119
goto -> bb9;
136-
- }
137-
-
138-
- bb20 (cleanup): {
139-
- switchInt(copy _4) -> [0: bb18, otherwise: bb9];
120+
}
121+
122+
bb20 (cleanup): {
123+
switchInt(copy _4) -> [0: bb18, otherwise: bb9];
140124
}
141125
}
142126

tests/mir-opt/early_otherwise_branch_unwind.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ use std::task::Poll;
66

77
// We find a matching pattern in the unwind path,
88
// and we need to create a cleanup BB for this case to meet the unwind invariants rule.
9+
// NB: This transform is not happening currently.
910

1011
// EMIT_MIR early_otherwise_branch_unwind.unwind.EarlyOtherwiseBranch.diff
1112
fn unwind<T>(val: Option<Option<Option<T>>>) {
1213
// CHECK-LABEL: fn unwind(
1314
// CHECK: drop({{.*}}) -> [return: bb{{.*}}, unwind: [[PARENT_UNWIND_BB:bb.*]]];
1415
// CHECK: [[PARENT_UNWIND_BB]] (cleanup): {
15-
// CHECK-NEXT: StorageLive
16-
// CHECK-NEXT: [[CMP_LOCAL:_.*]] = Ne
17-
// CHECK-NEXT: switchInt(move [[CMP_LOCAL]]) -> [0: [[NEW_UNWIND_BB:bb.*]], otherwise
18-
// CHECK: [[NEW_UNWIND_BB]] (cleanup): {
16+
// CHECK-NEXT: switchInt
1917
match val {
2018
Some(Some(Some(_v))) => {}
2119
Some(Some(None)) => {}
@@ -30,10 +28,7 @@ pub fn poll(val: Poll<Result<Option<Vec<u8>>, u8>>) {
3028
// CHECK-LABEL: fn poll(
3129
// CHECK: drop({{.*}}) -> [return: bb{{.*}}, unwind: [[PARENT_UNWIND_BB:bb.*]]];
3230
// CHECK: [[PARENT_UNWIND_BB]] (cleanup): {
33-
// CHECK-NEXT: StorageLive
34-
// CHECK-NEXT: [[CMP_LOCAL:_.*]] = Ne
35-
// CHECK-NEXT: switchInt(move [[CMP_LOCAL]]) -> [0: [[NEW_UNWIND_BB:bb.*]], otherwise
36-
// CHECK: [[NEW_UNWIND_BB]] (cleanup): {
31+
// CHECK-NEXT: switchInt
3732
match val {
3833
Poll::Ready(Ok(Some(_trailers))) => {}
3934
Poll::Ready(Err(_err)) => {}

tests/mir-opt/early_otherwise_branch_unwind.unwind.EarlyOtherwiseBranch.diff

+23-39
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
let mut _6: bool;
1212
let mut _7: bool;
1313
let mut _8: isize;
14-
+ let mut _9: bool;
1514
scope 1 {
1615
debug _v => _5;
1716
}
@@ -41,38 +40,32 @@
4140

4241
bb4: {
4342
_0 = const ();
44-
- goto -> bb17;
45-
+ goto -> bb15;
43+
goto -> bb17;
4644
}
4745

4846
bb5: {
4947
_0 = const ();
50-
- goto -> bb17;
51-
+ goto -> bb15;
48+
goto -> bb17;
5249
}
5350

5451
bb6: {
5552
_0 = const ();
56-
- goto -> bb17;
57-
+ goto -> bb15;
53+
goto -> bb17;
5854
}
5955

6056
bb7: {
6157
StorageLive(_5);
6258
_5 = move ((((((_1 as Some).0: std::option::Option<std::option::Option<T>>) as Some).0: std::option::Option<T>) as Some).0: T);
6359
_0 = const ();
64-
- drop(_5) -> [return: bb8, unwind: bb20];
65-
+ drop(_5) -> [return: bb8, unwind: bb16];
60+
drop(_5) -> [return: bb8, unwind: bb20];
6661
}
6762

6863
bb8: {
6964
StorageDead(_5);
70-
- goto -> bb17;
71-
+ goto -> bb15;
65+
goto -> bb17;
7266
}
7367

7468
bb9 (cleanup): {
75-
+ StorageDead(_9);
7669
resume;
7770
}
7871

@@ -81,8 +74,7 @@
8174
}
8275

8376
bb11: {
84-
- switchInt(copy _6) -> [0: bb12, otherwise: bb16];
85-
+ switchInt(copy _6) -> [0: bb12, otherwise: bb14];
77+
switchInt(copy _6) -> [0: bb12, otherwise: bb16];
8678
}
8779

8880
bb12: {
@@ -91,45 +83,37 @@
9183
}
9284

9385
bb13: {
94-
- switchInt(copy _7) -> [0: bb14, otherwise: bb15];
95-
- }
96-
-
97-
- bb14: {
86+
switchInt(copy _7) -> [0: bb14, otherwise: bb15];
87+
}
88+
89+
bb14: {
9890
_7 = const false;
9991
goto -> bb12;
10092
}
10193

102-
- bb15: {
103-
- goto -> bb14;
104-
- }
105-
-
106-
- bb16: {
107-
+ bb14: {
94+
bb15: {
95+
goto -> bb14;
96+
}
97+
98+
bb16: {
10899
_8 = discriminant(((_1 as Some).0: std::option::Option<std::option::Option<T>>));
109100
switchInt(move _8) -> [1: bb13, otherwise: bb12];
110101
}
111102

112-
- bb17: {
113-
+ bb15: {
103+
bb17: {
114104
switchInt(copy _4) -> [1: bb11, otherwise: bb10];
115105
}
116106

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];
107+
bb18 (cleanup): {
108+
switchInt(copy _3) -> [1: bb19, otherwise: bb9];
123109
}
124110

125-
- bb19 (cleanup): {
126-
+ bb17 (cleanup): {
127-
+ StorageDead(_9);
111+
bb19 (cleanup): {
128112
goto -> bb9;
129-
- }
130-
-
131-
- bb20 (cleanup): {
132-
- switchInt(copy _4) -> [1: bb18, otherwise: bb9];
113+
}
114+
115+
bb20 (cleanup): {
116+
switchInt(copy _4) -> [1: bb18, otherwise: bb9];
133117
}
134118
}
135119

0 commit comments

Comments
 (0)