Skip to content

Commit 4cba69e

Browse files
authored
Rollup merge of #70595 - wesleywiser:remove_unused_discriminant_reads, r=oli-obk
Remove unused discriminant reads from MIR bodies Allow the `SimplifyLocals` pass to remove reads of discriminants if the read is never used. Fixes #70531 r? @oli-obk
2 parents 1eabbd0 + 75e2e8c commit 4cba69e

File tree

4 files changed

+71
-20
lines changed

4 files changed

+71
-20
lines changed

Diff for: src/librustc_mir/transform/simplify.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,22 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
368368
if location.statement_index != block.statements.len() {
369369
let stmt = &block.statements[location.statement_index];
370370

371-
if let StatementKind::Assign(box (p, Rvalue::Use(Operand::Constant(c)))) =
372-
&stmt.kind
373-
{
374-
match c.literal.val {
375-
// Keep assignments from unevaluated constants around, since the evaluation
376-
// may report errors, even if the use of the constant is dead code.
377-
ty::ConstKind::Unevaluated(..) => {}
378-
_ => {
379-
if !p.is_indirect() {
380-
trace!("skipping store of const value {:?} to {:?}", c, p);
381-
return;
371+
if let StatementKind::Assign(box (dest, rvalue)) = &stmt.kind {
372+
if !dest.is_indirect() && dest.local == *local {
373+
if let Rvalue::Use(Operand::Constant(c)) = rvalue {
374+
match c.literal.val {
375+
// Keep assignments from unevaluated constants around, since the
376+
// evaluation may report errors, even if the use of the constant
377+
// is dead code.
378+
ty::ConstKind::Unevaluated(..) => {}
379+
_ => {
380+
trace!("skipping store of const value {:?} to {:?}", c, dest);
381+
return;
382+
}
382383
}
384+
} else if let Rvalue::Discriminant(d) = rvalue {
385+
trace!("skipping store of discriminant value {:?} to {:?}", d, dest);
386+
return;
383387
}
384388
}
385389
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn map(x: Option<Box<()>>) -> Option<Box<()>> {
2+
match x {
3+
None => None,
4+
Some(x) => Some(x),
5+
}
6+
}
7+
8+
fn main() {
9+
map(None);
10+
}
11+
12+
// EMIT_MIR rustc.map.SimplifyLocals.diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- // MIR for `map` before SimplifyLocals
2+
+ // MIR for `map` after SimplifyLocals
3+
4+
fn map(_1: std::option::Option<std::boxed::Box<()>>) -> std::option::Option<std::boxed::Box<()>> {
5+
debug x => _1; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:8: 1:9
6+
let mut _0: std::option::Option<std::boxed::Box<()>>; // return place in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:31: 1:46
7+
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
8+
let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
9+
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
10+
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
11+
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
12+
scope 1 {
13+
debug x => _3; // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
14+
}
15+
16+
bb0: {
17+
_2 = discriminant(_1); // bb0[0]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
18+
switchInt(move _2) -> [0isize: bb2, otherwise: bb1]; // bb0[1]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
19+
}
20+
21+
bb1: {
22+
_0 = move _1; // bb1[0]: scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
23+
goto -> bb3; // bb1[1]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
24+
}
25+
26+
bb2: {
27+
discriminant(_0) = 0; // bb2[0]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
28+
goto -> bb3; // bb2[1]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
29+
}
30+
31+
bb3: {
32+
- _5 = discriminant(_1); // bb3[0]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
33+
- return; // bb3[1]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:2: 6:2
34+
+ return; // bb3[0]: scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:2: 6:2
35+
}
36+
}
37+

Diff for: src/test/mir-opt/simplify_try.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -183,33 +183,31 @@ fn main() {
183183
// fn try_identity(_1: std::result::Result<u32, i32>) -> std::result::Result<u32, i32> {
184184
// debug x => _1;
185185
// let mut _0: std::result::Result<u32, i32>;
186-
// let mut _2: isize;
187-
// let _3: i32;
188-
// let _4: u32;
186+
// let _2: i32;
187+
// let _3: u32;
189188
// scope 1 {
190-
// debug y => _4;
189+
// debug y => _3;
191190
// }
192191
// scope 2 {
193-
// debug err => _3;
192+
// debug err => _2;
194193
// scope 3 {
195194
// scope 7 {
196-
// debug t => _3;
195+
// debug t => _2;
197196
// }
198197
// scope 8 {
199-
// debug v => _3;
198+
// debug v => _2;
200199
// }
201200
// }
202201
// }
203202
// scope 4 {
204-
// debug val => _4;
203+
// debug val => _3;
205204
// scope 5 {
206205
// }
207206
// }
208207
// scope 6 {
209208
// debug self => _1;
210209
// }
211210
// bb0: {
212-
// _2 = discriminant(_1);
213211
// _0 = move _1;
214212
// return;
215213
// }

0 commit comments

Comments
 (0)