Skip to content

Commit c6a9554

Browse files
committed
Auto merge of #141485 - dianqk:early_otherwise_branch_loop, r=oli-obk
mir-opt: Do not create storage marks in EarlyOtherwiseBranch Fixes #141212. The first commit add `StorageDead` by creating new indirect BB that makes CFG more complicated, but I think it's better to just not create storage marks. r? mir-opt
2 parents 100199c + 8c7faa6 commit c6a9554

7 files changed

+64
-47
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,27 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
128128

129129
let mut patch = MirPatch::new(body);
130130

131-
let (second_discriminant_temp, second_operand) = if opt_data.need_hoist_discriminant {
131+
let second_operand = if opt_data.need_hoist_discriminant {
132132
// create temp to store second discriminant in, `_s` in example above
133133
let second_discriminant_temp =
134134
patch.new_temp(opt_data.child_ty, opt_data.child_source.span);
135135

136-
patch.add_statement(
137-
parent_end,
138-
StatementKind::StorageLive(second_discriminant_temp),
139-
);
140-
141136
// create assignment of discriminant
142137
patch.add_assign(
143138
parent_end,
144139
Place::from(second_discriminant_temp),
145140
Rvalue::Discriminant(opt_data.child_place),
146141
);
147-
(
148-
Some(second_discriminant_temp),
149-
Operand::Move(Place::from(second_discriminant_temp)),
150-
)
142+
Operand::Move(Place::from(second_discriminant_temp))
151143
} else {
152-
(None, Operand::Copy(opt_data.child_place))
144+
Operand::Copy(opt_data.child_place)
153145
};
154146

155147
// create temp to store inequality comparison between the two discriminants, `_t` in
156148
// example above
157149
let nequal = BinOp::Ne;
158150
let comp_res_type = nequal.ty(tcx, parent_ty, opt_data.child_ty);
159151
let comp_temp = patch.new_temp(comp_res_type, opt_data.child_source.span);
160-
patch.add_statement(parent_end, StatementKind::StorageLive(comp_temp));
161152

162153
// create inequality comparison
163154
let comp_rvalue =
@@ -200,23 +191,6 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
200191
TerminatorKind::if_(Operand::Move(Place::from(comp_temp)), true_case, false_case),
201192
);
202193

203-
if let Some(second_discriminant_temp) = second_discriminant_temp {
204-
// generate StorageDead for the second_discriminant_temp not in use anymore
205-
patch.add_statement(
206-
parent_end,
207-
StatementKind::StorageDead(second_discriminant_temp),
208-
);
209-
}
210-
211-
// Generate a StorageDead for comp_temp in each of the targets, since we moved it into
212-
// the switch
213-
for bb in [false_case, true_case].iter() {
214-
patch.add_statement(
215-
Location { block: *bb, statement_index: 0 },
216-
StatementKind::StorageDead(comp_temp),
217-
);
218-
}
219-
220194
patch.apply(body);
221195
}
222196

tests/mir-opt/early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@
3232
StorageDead(_4);
3333
_9 = discriminant((_3.0: Option2<u32>));
3434
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
35-
+ StorageLive(_12);
3635
+ _12 = discriminant((_3.1: Option2<bool>));
37-
+ StorageLive(_13);
3836
+ _13 = Ne(copy _9, move _12);
39-
+ StorageDead(_12);
4037
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
4138
}
4239

4340
bb1: {
44-
+ StorageDead(_13);
4541
_0 = const 1_u32;
4642
- goto -> bb8;
4743
+ goto -> bb5;
@@ -100,7 +96,6 @@
10096
+ }
10197
+
10298
+ bb7: {
103-
+ StorageDead(_13);
10499
+ switchInt(copy _9) -> [0: bb4, 1: bb3, 2: bb2, otherwise: bb6];
105100
}
106101
}

tests/mir-opt/early_otherwise_branch.opt4.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@
3232
StorageDead(_4);
3333
_9 = discriminant((_3.0: Option2<u32>));
3434
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
35-
+ StorageLive(_12);
3635
+ _12 = discriminant((_3.1: Option2<u32>));
37-
+ StorageLive(_13);
3836
+ _13 = Ne(copy _9, move _12);
39-
+ StorageDead(_12);
4037
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
4138
}
4239

4340
bb1: {
44-
+ StorageDead(_13);
4541
_0 = const 1_u32;
4642
- goto -> bb8;
4743
+ goto -> bb5;
@@ -100,7 +96,6 @@
10096
+ }
10197
+
10298
+ bb7: {
103-
+ StorageDead(_13);
10499
+ switchInt(copy _9) -> [0: bb4, 1: bb3, 2: bb2, otherwise: bb6];
105100
}
106101
}

tests/mir-opt/early_otherwise_branch.opt5.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
StorageDead(_5);
2121
StorageDead(_4);
2222
- switchInt(copy (_3.0: u32)) -> [1: bb2, 2: bb3, 3: bb4, otherwise: bb1];
23-
+ StorageLive(_6);
2423
+ _6 = Ne(copy (_3.0: u32), copy (_3.1: u32));
2524
+ switchInt(move _6) -> [0: bb6, otherwise: bb1];
2625
}
2726

2827
bb1: {
29-
+ StorageDead(_6);
3028
_0 = const 0_u32;
3129
- goto -> bb8;
3230
+ goto -> bb5;
@@ -70,7 +68,6 @@
7068
- bb8: {
7169
- StorageDead(_3);
7270
- return;
73-
+ StorageDead(_6);
7471
+ switchInt(copy (_3.0: u32)) -> [1: bb4, 2: bb3, 3: bb2, otherwise: bb1];
7572
}
7673
}

tests/mir-opt/early_otherwise_branch.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//@ test-mir-pass: EarlyOtherwiseBranch
22
//@ compile-flags: -Zmir-enable-passes=+UnreachableEnumBranching
33

4+
#![feature(custom_mir, core_intrinsics)]
5+
6+
use std::intrinsics::mir::*;
7+
48
enum Option2<T> {
59
Some(T),
610
None,
@@ -124,11 +128,40 @@ fn opt5_failed_type(x: u32, y: u64) -> u32 {
124128
}
125129
}
126130

131+
// EMIT_MIR early_otherwise_branch.target_self.EarlyOtherwiseBranch.diff
132+
#[custom_mir(dialect = "runtime")]
133+
fn target_self(val: i32) {
134+
// CHECK-LABEL: fn target_self(
135+
// CHECK: Ne(
136+
// CHECK-NEXT: switchInt
137+
mir! {
138+
{
139+
Goto(bb1)
140+
}
141+
bb1 = {
142+
match val {
143+
0 => bb2,
144+
_ => bb1,
145+
}
146+
}
147+
bb2 = {
148+
match val {
149+
0 => bb3,
150+
_ => bb1,
151+
}
152+
}
153+
bb3 = {
154+
Return()
155+
}
156+
}
157+
}
158+
127159
fn main() {
128160
opt1(None, Some(0));
129161
opt2(None, Some(0));
130162
opt3(Option2::None, Option2::Some(false));
131163
opt4(Option2::None, Option2::Some(0));
132164
opt5(0, 0);
133165
opt5_failed(0, 0);
166+
target_self(1);
134167
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- // MIR for `target_self` before EarlyOtherwiseBranch
2+
+ // MIR for `target_self` after EarlyOtherwiseBranch
3+
4+
fn target_self(_1: i32) -> () {
5+
let mut _0: ();
6+
+ let mut _2: bool;
7+
8+
bb0: {
9+
goto -> bb1;
10+
}
11+
12+
bb1: {
13+
- switchInt(copy _1) -> [0: bb2, otherwise: bb1];
14+
+ _2 = Ne(copy _1, copy _1);
15+
+ switchInt(move _2) -> [0: bb3, otherwise: bb1];
16+
}
17+
18+
bb2: {
19+
- switchInt(copy _1) -> [0: bb3, otherwise: bb1];
20+
+ return;
21+
}
22+
23+
bb3: {
24+
- return;
25+
+ switchInt(copy _1) -> [0: bb2, otherwise: bb1];
26+
}
27+
}
28+

tests/mir-opt/early_otherwise_branch_3_element_tuple.opt2.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@
4242
StorageDead(_5);
4343
_14 = discriminant((_4.0: Option2<u32>));
4444
- switchInt(move _14) -> [0: bb2, 1: bb4, 2: bb6, otherwise: bb12];
45-
+ StorageLive(_18);
4645
+ _18 = discriminant((_4.1: Option2<u32>));
47-
+ StorageLive(_19);
4846
+ _19 = Ne(copy _14, move _18);
49-
+ StorageDead(_18);
5047
+ switchInt(move _19) -> [0: bb10, otherwise: bb1];
5148
}
5249

5350
bb1: {
54-
+ StorageDead(_19);
5551
_0 = const 1_u32;
5652
- goto -> bb11;
5753
+ goto -> bb8;
@@ -134,7 +130,6 @@
134130
+ }
135131
+
136132
+ bb10: {
137-
+ StorageDead(_19);
138133
+ switchInt(copy _14) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
139134
}
140135
}

0 commit comments

Comments
 (0)