Skip to content

Commit 09db057

Browse files
committed
Auto merge of #78360 - tmiasko:storage-markers, r=wesleywiser,oli-obk
Remove storage markers if they won't be used during code generation The storage markers constitute a substantial portion of all MIR statements. At the same time, for builds without any optimizations, the storage markers have no further use during and after MIR optimization phase. If storage markers are not necessary for code generation, remove them.
2 parents d2731d8 + 57de468 commit 09db057

File tree

7 files changed

+155
-3
lines changed

7 files changed

+155
-3
lines changed

compiler/rustc_mir/src/transform/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod no_landing_pads;
4242
pub mod nrvo;
4343
pub mod promote_consts;
4444
pub mod remove_noop_landing_pads;
45+
pub mod remove_storage_markers;
4546
pub mod remove_unneeded_drops;
4647
pub mod required_consts;
4748
pub mod rustc_peek;
@@ -493,6 +494,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
493494

494495
// The main optimizations that we do on MIR.
495496
let optimizations: &[&dyn MirPass<'tcx>] = &[
497+
&remove_storage_markers::RemoveStorageMarkers,
496498
&const_goto::ConstGoto,
497499
&remove_unneeded_drops::RemoveUnneededDrops,
498500
&match_branches::MatchBranchSimplification,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//! This pass removes storage markers if they won't be emitted during codegen.
2+
3+
use crate::transform::MirPass;
4+
use rustc_middle::mir::*;
5+
use rustc_middle::ty::TyCtxt;
6+
7+
pub struct RemoveStorageMarkers;
8+
9+
impl<'tcx> MirPass<'tcx> for RemoveStorageMarkers {
10+
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
11+
if tcx.sess.emit_lifetime_markers() {
12+
return;
13+
}
14+
15+
trace!("Running RemoveStorageMarkers on {:?}", body.source);
16+
for data in body.basic_blocks_mut() {
17+
data.statements.retain(|statement| match statement.kind {
18+
StatementKind::StorageLive(..)
19+
| StatementKind::StorageDead(..)
20+
| StatementKind::Nop => false,
21+
_ => true,
22+
})
23+
}
24+
}
25+
}

src/test/codegen/try_identity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C no-prepopulate-passes -Z mir-opt-level=2 -Zunsound-mir-opts
1+
// compile-flags: -C no-prepopulate-passes -O -Z mir-opt-level=2 -Zunsound-mir-opts
22

33
// Ensure that `x?` has no overhead on `Result<T, E>` due to identity `match`es in lowering.
44
// This requires inlining to trigger the MIR optimizations in `SimplifyArmIdentity`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
- // MIR for `main` before RemoveStorageMarkers
2+
+ // MIR for `main` after RemoveStorageMarkers
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/remove_storage_markers.rs:6:11: 6:11
6+
let mut _1: i32; // in scope 0 at $DIR/remove_storage_markers.rs:7:9: 7:16
7+
let mut _2: std::ops::Range<i32>; // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
8+
let mut _3: std::ops::Range<i32>; // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
9+
let mut _5: (); // in scope 0 at $DIR/remove_storage_markers.rs:6:1: 11:2
10+
let _7: (); // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
11+
let mut _8: std::option::Option<i32>; // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
12+
let mut _9: &mut std::ops::Range<i32>; // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
13+
let mut _10: &mut std::ops::Range<i32>; // in scope 0 at $DIR/remove_storage_markers.rs:8:14: 8:19
14+
let mut _11: isize; // in scope 0 at $DIR/remove_storage_markers.rs:8:9: 8:10
15+
let mut _13: i32; // in scope 0 at $DIR/remove_storage_markers.rs:8:9: 8:10
16+
let mut _14: !; // in scope 0 at $DIR/remove_storage_markers.rs:8:5: 10:6
17+
let _16: (); // in scope 0 at $DIR/remove_storage_markers.rs:8:20: 10:6
18+
let mut _17: i32; // in scope 0 at $DIR/remove_storage_markers.rs:9:16: 9:17
19+
scope 1 {
20+
debug sum => _1; // in scope 1 at $DIR/remove_storage_markers.rs:7:9: 7:16
21+
let mut _4: std::ops::Range<i32>; // in scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
22+
scope 2 {
23+
debug iter => _4; // in scope 2 at $DIR/remove_storage_markers.rs:8:14: 8:19
24+
let mut _6: i32; // in scope 2 at $DIR/remove_storage_markers.rs:8:14: 8:19
25+
scope 3 {
26+
debug __next => _6; // in scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
27+
let _12: i32; // in scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
28+
let _15: i32; // in scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
29+
scope 4 {
30+
debug val => _12; // in scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
31+
}
32+
scope 5 {
33+
debug i => _15; // in scope 5 at $DIR/remove_storage_markers.rs:8:9: 8:10
34+
}
35+
}
36+
}
37+
scope 6 (inlined <std::ops::Range<i32> as IntoIterator>::into_iter) { // at $DIR/remove_storage_markers.rs:8:14: 8:19
38+
debug self => _3; // in scope 6 at $DIR/remove_storage_markers.rs:8:14: 8:19
39+
}
40+
}
41+
42+
bb0: {
43+
- StorageLive(_1); // scope 0 at $DIR/remove_storage_markers.rs:7:9: 7:16
44+
_1 = const 0_i32; // scope 0 at $DIR/remove_storage_markers.rs:7:19: 7:20
45+
- StorageLive(_2); // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
46+
- StorageLive(_3); // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
47+
(_3.0: i32) = const 0_i32; // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
48+
(_3.1: i32) = const 10_i32; // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
49+
_2 = move _3; // scope 6 at $DIR/remove_storage_markers.rs:8:14: 8:19
50+
- StorageDead(_3); // scope 1 at $DIR/remove_storage_markers.rs:8:18: 8:19
51+
- StorageLive(_4); // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
52+
_4 = move _2; // scope 1 at $DIR/remove_storage_markers.rs:8:14: 8:19
53+
goto -> bb1; // scope 2 at $DIR/remove_storage_markers.rs:8:5: 10:6
54+
}
55+
56+
bb1: {
57+
- StorageLive(_6); // scope 2 at $DIR/remove_storage_markers.rs:8:14: 8:19
58+
- StorageLive(_7); // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
59+
- StorageLive(_8); // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
60+
- StorageLive(_9); // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
61+
- StorageLive(_10); // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
62+
_10 = &mut _4; // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
63+
_9 = &mut (*_10); // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
64+
_8 = <std::ops::Range<i32> as Iterator>::next(move _9) -> bb2; // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
65+
// mir::Constant
66+
// + span: $DIR/remove_storage_markers.rs:8:14: 8:19
67+
// + literal: Const { ty: for<'r> fn(&'r mut std::ops::Range<i32>) -> std::option::Option<<std::ops::Range<i32> as std::iter::Iterator>::Item> {<std::ops::Range<i32> as std::iter::Iterator>::next}, val: Value(Scalar(<ZST>)) }
68+
}
69+
70+
bb2: {
71+
- StorageDead(_9); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
72+
_11 = discriminant(_8); // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
73+
switchInt(move _11) -> [0_isize: bb3, otherwise: bb4]; // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
74+
}
75+
76+
bb3: {
77+
_0 = const (); // scope 3 at $DIR/remove_storage_markers.rs:8:5: 10:6
78+
- StorageDead(_10); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
79+
- StorageDead(_8); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
80+
- StorageDead(_7); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
81+
- StorageDead(_6); // scope 2 at $DIR/remove_storage_markers.rs:10:5: 10:6
82+
- StorageDead(_4); // scope 1 at $DIR/remove_storage_markers.rs:10:5: 10:6
83+
- StorageDead(_2); // scope 1 at $DIR/remove_storage_markers.rs:8:18: 8:19
84+
- StorageDead(_1); // scope 0 at $DIR/remove_storage_markers.rs:11:1: 11:2
85+
return; // scope 0 at $DIR/remove_storage_markers.rs:11:2: 11:2
86+
}
87+
88+
bb4: {
89+
- StorageLive(_12); // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
90+
_12 = ((_8 as Some).0: i32); // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
91+
- StorageLive(_13); // scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
92+
_13 = _12; // scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
93+
_6 = move _13; // scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
94+
_7 = const (); // scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
95+
- StorageDead(_13); // scope 4 at $DIR/remove_storage_markers.rs:8:9: 8:10
96+
- StorageDead(_12); // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
97+
- StorageDead(_10); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
98+
- StorageDead(_8); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
99+
- StorageDead(_7); // scope 3 at $DIR/remove_storage_markers.rs:8:18: 8:19
100+
- StorageLive(_15); // scope 3 at $DIR/remove_storage_markers.rs:8:9: 8:10
101+
_15 = _6; // scope 3 at $DIR/remove_storage_markers.rs:8:14: 8:19
102+
- StorageLive(_16); // scope 5 at $DIR/remove_storage_markers.rs:8:20: 10:6
103+
- StorageLive(_17); // scope 5 at $DIR/remove_storage_markers.rs:9:16: 9:17
104+
_17 = _15; // scope 5 at $DIR/remove_storage_markers.rs:9:16: 9:17
105+
_1 = Add(_1, move _17); // scope 5 at $DIR/remove_storage_markers.rs:9:9: 9:17
106+
- StorageDead(_17); // scope 5 at $DIR/remove_storage_markers.rs:9:16: 9:17
107+
_16 = const (); // scope 5 at $DIR/remove_storage_markers.rs:8:20: 10:6
108+
- StorageDead(_16); // scope 5 at $DIR/remove_storage_markers.rs:10:5: 10:6
109+
_5 = const (); // scope 2 at $DIR/remove_storage_markers.rs:8:5: 10:6
110+
- StorageDead(_15); // scope 3 at $DIR/remove_storage_markers.rs:10:5: 10:6
111+
- StorageDead(_6); // scope 2 at $DIR/remove_storage_markers.rs:10:5: 10:6
112+
goto -> bb1; // scope 2 at $DIR/remove_storage_markers.rs:8:5: 10:6
113+
}
114+
}
115+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Checks that storage markers are removed at opt-level=0.
2+
//
3+
// compile-flags: -C opt-level=0 -Coverflow-checks=off
4+
5+
// EMIT_MIR remove_storage_markers.main.RemoveStorageMarkers.diff
6+
fn main() {
7+
let mut sum = 0;
8+
for i in 0..10 {
9+
sum += i;
10+
}
11+
}

src/test/run-make/const_fn_mir/dump.mir

-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ fn main() -> () {
55
let _1: i32; // in scope 0 at main.rs:9:5: 9:10
66

77
bb0: {
8-
StorageLive(_1); // scope 0 at main.rs:9:5: 9:10
98
_1 = foo() -> bb1; // scope 0 at main.rs:9:5: 9:10
109
// mir::Constant
1110
// + span: main.rs:9:5: 9:8
1211
// + literal: Const { ty: fn() -> i32 {foo}, val: Value(Scalar(<ZST>)) }
1312
}
1413

1514
bb1: {
16-
StorageDead(_1); // scope 0 at main.rs:9:10: 9:11
1715
_0 = const (); // scope 0 at main.rs:8:11: 10:2
1816
return; // scope 0 at main.rs:10:2: 10:2
1917
}

src/tools/compiletest/src/runtest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ impl<'test> TestCx<'test> {
19571957
}
19581958
MirOpt => {
19591959
rustc.args(&[
1960+
"-Copt-level=1",
19601961
"-Zdump-mir=all",
19611962
"-Zmir-opt-level=3",
19621963
"-Zvalidate-mir",

0 commit comments

Comments
 (0)