Skip to content

Commit 5462da5

Browse files
committed
Auto merge of #99946 - tmiasko:elide-storage-makers, r=oli-obk
Elide superfluous storage markers Follow the existing strategy of omitting the storage markers for temporaries introduced for internal usage when elaborating derefs and deref projections. Those temporaries are simple scalars which are used immediately after being defined and never have their address taken. There is no benefit from storage markers from either liveness analysis or code generation perspective.
2 parents addacb5 + f8ca6aa commit 5462da5

22 files changed

+48
-198
lines changed

compiler/rustc_middle/src/mir/patch.rs

+6-20
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,25 @@ impl<'tcx> MirPatch<'tcx> {
6868
Location { block: bb, statement_index: offset }
6969
}
7070

71-
pub fn new_local_with_info(
71+
pub fn new_internal_with_info(
7272
&mut self,
7373
ty: Ty<'tcx>,
7474
span: Span,
7575
local_info: Option<Box<LocalInfo<'tcx>>>,
7676
) -> Local {
7777
let index = self.next_local;
7878
self.next_local += 1;
79-
let mut new_decl = LocalDecl::new(ty, span);
79+
let mut new_decl = LocalDecl::new(ty, span).internal();
8080
new_decl.local_info = local_info;
8181
self.new_locals.push(new_decl);
8282
Local::new(index as usize)
8383
}
8484

8585
pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
86-
self.new_local_with_info(ty, span, None)
86+
let index = self.next_local;
87+
self.next_local += 1;
88+
self.new_locals.push(LocalDecl::new(ty, span));
89+
Local::new(index as usize)
8790
}
8891

8992
pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
@@ -147,7 +150,6 @@ impl<'tcx> MirPatch<'tcx> {
147150

148151
let mut delta = 0;
149152
let mut last_bb = START_BLOCK;
150-
let mut stmts_and_targets: Vec<(Statement<'_>, BasicBlock)> = Vec::new();
151153
for (mut loc, stmt) in new_statements {
152154
if loc.block != last_bb {
153155
delta = 0;
@@ -156,27 +158,11 @@ impl<'tcx> MirPatch<'tcx> {
156158
debug!("MirPatch: adding statement {:?} at loc {:?}+{}", stmt, loc, delta);
157159
loc.statement_index += delta;
158160
let source_info = Self::source_info_for_index(&body[loc.block], loc);
159-
160-
// For mir-opt `Derefer` to work in all cases we need to
161-
// get terminator's targets and apply the statement to all of them.
162-
if loc.statement_index > body[loc.block].statements.len() {
163-
let term = body[loc.block].terminator();
164-
for i in term.successors() {
165-
stmts_and_targets.push((Statement { source_info, kind: stmt.clone() }, i));
166-
}
167-
delta += 1;
168-
continue;
169-
}
170-
171161
body[loc.block]
172162
.statements
173163
.insert(loc.statement_index, Statement { source_info, kind: stmt });
174164
delta += 1;
175165
}
176-
177-
for (stmt, target) in stmts_and_targets.into_iter().rev() {
178-
body[target].statements.insert(0, stmt);
179-
}
180166
}
181167

182168
pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {

compiler/rustc_mir_transform/src/deref_separator.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
2828
let mut last_len = 0;
2929
let mut last_deref_idx = 0;
3030

31-
let mut prev_temp: Option<Local> = None;
32-
3331
for (idx, elem) in place.projection[0..].iter().enumerate() {
3432
if *elem == ProjectionElem::Deref {
3533
last_deref_idx = idx;
@@ -39,14 +37,12 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
3937
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
4038
if !p_ref.projection.is_empty() && p_elem == ProjectionElem::Deref {
4139
let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
42-
let temp = self.patcher.new_local_with_info(
40+
let temp = self.patcher.new_internal_with_info(
4341
ty,
4442
self.local_decls[p_ref.local].source_info.span,
4543
Some(Box::new(LocalInfo::DerefTemp)),
4644
);
4745

48-
self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
49-
5046
// We are adding current p_ref's projections to our
5147
// temp value, excluding projections we already covered.
5248
let deref_place = Place::from(place_local)
@@ -66,22 +62,8 @@ impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
6662
Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
6763
*place = temp_place;
6864
}
69-
70-
// We are destroying the previous temp since it's no longer used.
71-
if let Some(prev_temp) = prev_temp {
72-
self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
73-
}
74-
75-
prev_temp = Some(temp);
7665
}
7766
}
78-
79-
// Since we won't be able to reach final temp, we destroy it outside the loop.
80-
if let Some(prev_temp) = prev_temp {
81-
let last_loc =
82-
Location { block: loc.block, statement_index: loc.statement_index + 1 };
83-
self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
84-
}
8567
}
8668
}
8769
}

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> {
6969
let (unique_ty, nonnull_ty, ptr_ty) =
7070
build_ptr_tys(tcx, base_ty.boxed_ty(), self.unique_did, self.nonnull_did);
7171

72-
let ptr_local = self.patch.new_temp(ptr_ty, source_info.span);
73-
74-
self.patch.add_statement(location, StatementKind::StorageLive(ptr_local));
72+
let ptr_local = self.patch.new_internal(ptr_ty, source_info.span);
7573

7674
self.patch.add_assign(
7775
location,
@@ -83,11 +81,6 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> {
8381
);
8482

8583
place.local = ptr_local;
86-
87-
self.patch.add_statement(
88-
Location { block: location.block, statement_index: location.statement_index + 1 },
89-
StatementKind::StorageDead(ptr_local),
90-
);
9184
}
9285

9386
self.super_place(place, context, location);

compiler/rustc_mir_transform/src/inline.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ impl<'tcx> Inliner<'tcx> {
616616
// If there are any locals without storage markers, give them storage only for the
617617
// duration of the call.
618618
for local in callee_body.vars_and_temps_iter() {
619-
if integrator.always_live_locals.contains(local) {
619+
if !callee_body.local_decls[local].internal
620+
&& integrator.always_live_locals.contains(local)
621+
{
620622
let new_local = integrator.map_local(local);
621623
caller_body[callsite.block].statements.push(Statement {
622624
source_info: callsite.source_info,
@@ -629,7 +631,9 @@ impl<'tcx> Inliner<'tcx> {
629631
// the slice once.
630632
let mut n = 0;
631633
for local in callee_body.vars_and_temps_iter().rev() {
632-
if integrator.always_live_locals.contains(local) {
634+
if !callee_body.local_decls[local].internal
635+
&& integrator.always_live_locals.contains(local)
636+
{
633637
let new_local = integrator.map_local(local);
634638
caller_body[block].statements.push(Statement {
635639
source_info: callsite.source_info,

src/test/mir-opt/const_prop/boxes.main.ConstProp.diff

-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@
3535
bb1: {
3636
StorageLive(_7); // scope 0 at $DIR/boxes.rs:+1:14: +1:22
3737
_7 = ShallowInitBox(move _6, i32); // scope 0 at $DIR/boxes.rs:+1:14: +1:22
38-
StorageLive(_8); // scope 0 at $DIR/boxes.rs:+1:19: +1:21
3938
_8 = (((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); // scope 0 at $DIR/boxes.rs:+1:19: +1:21
4039
(*_8) = const 42_i32; // scope 0 at $DIR/boxes.rs:+1:19: +1:21
41-
StorageDead(_8); // scope 0 at $DIR/boxes.rs:+1:14: +1:22
4240
_3 = move _7; // scope 0 at $DIR/boxes.rs:+1:14: +1:22
4341
StorageDead(_7); // scope 0 at $DIR/boxes.rs:+1:21: +1:22
44-
StorageLive(_9); // scope 0 at $DIR/boxes.rs:+1:13: +1:22
4542
_9 = (((_3.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>).0: *const i32); // scope 0 at $DIR/boxes.rs:+1:13: +1:22
4643
_2 = (*_9); // scope 0 at $DIR/boxes.rs:+1:13: +1:22
47-
StorageDead(_9); // scope 0 at $DIR/boxes.rs:+1:13: +1:26
4844
_1 = Add(move _2, const 0_i32); // scope 0 at $DIR/boxes.rs:+1:13: +1:26
4945
StorageDead(_2); // scope 0 at $DIR/boxes.rs:+1:25: +1:26
5046
drop(_3) -> [return: bb2, unwind: bb3]; // scope 0 at $DIR/boxes.rs:+1:26: +1:27

src/test/mir-opt/derefer_complex_case.main.Derefer.diff

-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@
6868
bb4: {
6969
StorageLive(_12); // scope 1 at $DIR/derefer_complex_case.rs:+1:10: +1:13
7070
- _12 = (*((_7 as Some).0: &i32)); // scope 1 at $DIR/derefer_complex_case.rs:+1:10: +1:13
71-
+ StorageLive(_15); // scope 1 at $DIR/derefer_complex_case.rs:+1:10: +1:13
7271
+ _15 = deref_copy ((_7 as Some).0: &i32); // scope 1 at $DIR/derefer_complex_case.rs:+1:10: +1:13
7372
+ _12 = (*_15); // scope 1 at $DIR/derefer_complex_case.rs:+1:10: +1:13
74-
+ StorageDead(_15); // scope 2 at $DIR/derefer_complex_case.rs:+1:34: +1:37
7573
StorageLive(_13); // scope 2 at $DIR/derefer_complex_case.rs:+1:34: +1:37
7674
_13 = _12; // scope 2 at $DIR/derefer_complex_case.rs:+1:34: +1:37
7775
_6 = std::mem::drop::<i32>(move _13) -> bb7; // scope 2 at $DIR/derefer_complex_case.rs:+1:29: +1:38

src/test/mir-opt/derefer_terminator_test.main.Derefer.diff

-7
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,18 @@
5555
_5 = &_6; // scope 2 at $DIR/derefer_terminator_test.rs:+3:17: +3:21
5656
_4 = &_5; // scope 2 at $DIR/derefer_terminator_test.rs:+3:15: +3:22
5757
- switchInt((*(*(*(*_4))))) -> [false: bb3, otherwise: bb4]; // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
58-
+ StorageLive(_10); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
5958
+ _10 = deref_copy (*_4); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
60-
+ StorageLive(_11); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
6159
+ _11 = deref_copy (*_10); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
62-
+ StorageDead(_10); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
63-
+ StorageLive(_12); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
6460
+ _12 = deref_copy (*_11); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
65-
+ StorageDead(_11); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
6661
+ switchInt((*_12)) -> [false: bb3, otherwise: bb4]; // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
6762
}
6863

6964
bb3: {
70-
+ StorageDead(_12); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
7165
_3 = const (); // scope 2 at $DIR/derefer_terminator_test.rs:+5:18: +5:20
7266
goto -> bb5; // scope 2 at $DIR/derefer_terminator_test.rs:+5:18: +5:20
7367
}
7468

7569
bb4: {
76-
+ StorageDead(_12); // scope 2 at $DIR/derefer_terminator_test.rs:+3:5: +3:22
7770
StorageLive(_8); // scope 2 at $DIR/derefer_terminator_test.rs:+4:22: +4:23
7871
_8 = const 5_i32; // scope 2 at $DIR/derefer_terminator_test.rs:+4:26: +4:27
7972
_3 = const (); // scope 2 at $DIR/derefer_terminator_test.rs:+4:17: +4:29

src/test/mir-opt/derefer_test.main.Derefer.diff

-4
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@
3333
StorageDead(_3); // scope 1 at $DIR/derefer_test.rs:+2:28: +2:29
3434
StorageLive(_4); // scope 2 at $DIR/derefer_test.rs:+3:9: +3:10
3535
- _4 = &mut ((*(_2.1: &mut (i32, i32))).0: i32); // scope 2 at $DIR/derefer_test.rs:+3:13: +3:26
36-
+ StorageLive(_6); // scope 2 at $DIR/derefer_test.rs:+3:13: +3:26
3736
+ _6 = deref_copy (_2.1: &mut (i32, i32)); // scope 2 at $DIR/derefer_test.rs:+3:13: +3:26
3837
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:+3:13: +3:26
39-
+ StorageDead(_6); // scope 3 at $DIR/derefer_test.rs:+4:9: +4:10
4038
StorageLive(_5); // scope 3 at $DIR/derefer_test.rs:+4:9: +4:10
4139
- _5 = &mut ((*(_2.1: &mut (i32, i32))).1: i32); // scope 3 at $DIR/derefer_test.rs:+4:13: +4:26
42-
+ StorageLive(_7); // scope 3 at $DIR/derefer_test.rs:+4:13: +4:26
4340
+ _7 = deref_copy (_2.1: &mut (i32, i32)); // scope 3 at $DIR/derefer_test.rs:+4:13: +4:26
4441
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:+4:13: +4:26
45-
+ StorageDead(_7); // scope 0 at $DIR/derefer_test.rs:+0:11: +5:2
4642
_0 = const (); // scope 0 at $DIR/derefer_test.rs:+0:11: +5:2
4743
StorageDead(_5); // scope 3 at $DIR/derefer_test.rs:+5:1: +5:2
4844
StorageDead(_4); // scope 2 at $DIR/derefer_test.rs:+5:1: +5:2

src/test/mir-opt/derefer_test_multiple.main.Derefer.diff

-12
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,16 @@
5757
StorageDead(_7); // scope 3 at $DIR/derefer_test_multiple.rs:+4:28: +4:29
5858
StorageLive(_8); // scope 4 at $DIR/derefer_test_multiple.rs:+5:9: +5:10
5959
- _8 = &mut ((*((*((*(_6.1: &mut (i32, &mut (i32, &mut (i32, i32))))).1: &mut (i32, &mut (i32, i32)))).1: &mut (i32, i32))).1: i32); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
60-
+ StorageLive(_10); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
6160
+ _10 = deref_copy (_6.1: &mut (i32, &mut (i32, &mut (i32, i32)))); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
62-
+ StorageLive(_11); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
6361
+ _11 = deref_copy ((*_10).1: &mut (i32, &mut (i32, i32))); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
64-
+ StorageDead(_10); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
65-
+ StorageLive(_12); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
6662
+ _12 = deref_copy ((*_11).1: &mut (i32, i32)); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
67-
+ StorageDead(_11); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
6863
+ _8 = &mut ((*_12).1: i32); // scope 4 at $DIR/derefer_test_multiple.rs:+5:13: +5:30
69-
+ StorageDead(_12); // scope 5 at $DIR/derefer_test_multiple.rs:+6:9: +6:10
7064
StorageLive(_9); // scope 5 at $DIR/derefer_test_multiple.rs:+6:9: +6:10
7165
- _9 = &mut ((*((*((*(_6.1: &mut (i32, &mut (i32, &mut (i32, i32))))).1: &mut (i32, &mut (i32, i32)))).1: &mut (i32, i32))).1: i32); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
72-
+ StorageLive(_13); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
7366
+ _13 = deref_copy (_6.1: &mut (i32, &mut (i32, &mut (i32, i32)))); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
74-
+ StorageLive(_14); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
7567
+ _14 = deref_copy ((*_13).1: &mut (i32, &mut (i32, i32))); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
76-
+ StorageDead(_13); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
77-
+ StorageLive(_15); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
7868
+ _15 = deref_copy ((*_14).1: &mut (i32, i32)); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
79-
+ StorageDead(_14); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
8069
+ _9 = &mut ((*_15).1: i32); // scope 5 at $DIR/derefer_test_multiple.rs:+6:13: +6:30
81-
+ StorageDead(_15); // scope 0 at $DIR/derefer_test_multiple.rs:+0:12: +7:2
8270
_0 = const (); // scope 0 at $DIR/derefer_test_multiple.rs:+0:12: +7:2
8371
StorageDead(_9); // scope 5 at $DIR/derefer_test_multiple.rs:+7:1: +7:2
8472
StorageDead(_8); // scope 4 at $DIR/derefer_test_multiple.rs:+7:1: +7:2

0 commit comments

Comments
 (0)