Skip to content

Commit ee62461

Browse files
committed
Extend SimplifyLocals to remove ZST writes
1 parent 7ce1b3b commit ee62461

File tree

34 files changed

+40
-40
lines changed

34 files changed

+40
-40
lines changed

compiler/rustc_mir/src/transform/simplify.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
329329
// count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from
330330
// `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a
331331
// fixedpoint where there are no more unused locals.
332-
remove_unused_definitions(&mut used_locals, body);
332+
remove_unused_definitions(&mut used_locals, body, tcx);
333333

334334
// Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s.
335335
let map = make_local_map(&mut body.local_decls, &used_locals);
@@ -459,12 +459,22 @@ impl Visitor<'_> for UsedLocals {
459459
}
460460

461461
/// Removes unused definitions. Updates the used locals to reflect the changes made.
462-
fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &mut Body<'tcx>) {
462+
fn remove_unused_definitions<'a, 'tcx>(
463+
used_locals: &'a mut UsedLocals,
464+
body: &mut Body<'tcx>,
465+
tcx: TyCtxt<'tcx>,
466+
) {
463467
// The use counts are updated as we remove the statements. A local might become unused
464468
// during the retain operation, leading to a temporary inconsistency (storage statements or
465469
// definitions referencing the local might remain). For correctness it is crucial that this
466470
// computation reaches a fixed point.
467471

472+
let def_id = body.source.def_id();
473+
let is_static = tcx.is_static(def_id);
474+
let param_env = tcx.param_env(def_id);
475+
476+
let local_decls = body.local_decls.clone();
477+
468478
let mut modified = true;
469479
while modified {
470480
modified = false;
@@ -476,7 +486,21 @@ fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &m
476486
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
477487
used_locals.is_used(*local)
478488
}
479-
StatementKind::Assign(box (place, _)) => used_locals.is_used(place.local),
489+
StatementKind::Assign(box (place, _)) => {
490+
let used = used_locals.is_used(place.local);
491+
let mut is_zst = false;
492+
493+
// ZST locals can be removed
494+
if used && !is_static {
495+
let ty = local_decls[place.local].ty;
496+
let param_env_and = param_env.and(ty);
497+
if let Ok(layout) = tcx.layout_of(param_env_and) {
498+
is_zst = layout.is_zst();
499+
}
500+
}
501+
502+
used && !is_zst
503+
}
480504

481505
StatementKind::SetDiscriminant { ref place, .. } => {
482506
used_locals.is_used(place.local)

src/test/codegen/naked-functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#[naked]
1010
pub fn naked_empty() {
1111
// CHECK-NEXT: {{.+}}:
12+
// CHECK-NEXT: %0 = alloca {}, align 1
1213
// CHECK-NEXT: ret void
1314
}
1415

@@ -18,6 +19,7 @@ pub fn naked_empty() {
1819
// CHECK-NEXT: define void @naked_with_args(i{{[0-9]+( %a)?}})
1920
pub fn naked_with_args(a: isize) {
2021
// CHECK-NEXT: {{.+}}:
22+
// CHECK-NEXT: %0 = alloca {}, align 1
2123
// CHECK: ret void
2224
}
2325

src/test/mir-opt/const_prop/control_flow_simplification.hello.PreCodegen.before.mir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ fn hello() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/control-flow-simplification.rs:11:14: 11:14
55

66
bb0: {
7-
_0 = const (); // scope 0 at $DIR/control-flow-simplification.rs:14:6: 14:6
87
return; // scope 0 at $DIR/control-flow-simplification.rs:15:2: 15:2
98
}
109
}

src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.32bit.mir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ fn main() -> () {
2222
_2 = const 3_i32; // scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
2323
StorageLive(_3); // scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
2424
_3 = const 42_u32; // scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
25-
_0 = const (); // scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
2625
StorageDead(_3); // scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
2726
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
2827
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2

src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.64bit.mir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ fn main() -> () {
2222
_2 = const 3_i32; // scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
2323
StorageLive(_3); // scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
2424
_3 = const 42_u32; // scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
25-
_0 = const (); // scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
2625
StorageDead(_3); // scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
2726
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
2827
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2

src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
StorageLive(_6); // scope 3 at $DIR/cycle.rs:14:10: 14:11
5757
- _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11
5858
+ _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11
59-
_5 = const (); // scope 4 at $DIR/cycle.rs:14:5: 14:12
6059
StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12
6160
StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13
6261
_0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2

src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27
3232
StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26
3333
_4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24
34-
_3 = const (); // scope 3 at $DIR/union.rs:15:5: 15:27
3534
StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27
3635
StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28
3736
_0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2

src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
- }
1717
-
1818
- bb1: {
19-
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:24:5: 24:18
2019
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:24:18: 24:19
2120
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:23:37: 25:2
2221
return; // scope 0 at $DIR/inline-compatibility.rs:25:2: 25:2

src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
- }
1717
-
1818
- bb1: {
19-
+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:13:5: 13:21
2019
StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:13:21: 13:22
2120
_0 = const (); // scope 0 at $DIR/inline-compatibility.rs:12:40: 14:2
2221
return; // scope 0 at $DIR/inline-compatibility.rs:14:2: 14:2

src/test/mir-opt/inline/inline_cycle.two.Inline.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
+ StorageDead(_5); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12
3838
+ StorageDead(_4); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12
3939
+ StorageDead(_3); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12
40-
+ _1 = const (); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12
4140
+ StorageDead(_2); // scope 0 at $DIR/inline-cycle.rs:49:5: 49:12
4241
StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:49:12: 49:13
4342
_0 = const (); // scope 0 at $DIR/inline-cycle.rs:48:10: 50:2

0 commit comments

Comments
 (0)