From ee6246158e42874b2bb758bc0f76e9a71cfc8527 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Tue, 29 Dec 2020 23:32:31 +0100 Subject: [PATCH 1/3] Extend SimplifyLocals to remove ZST writes --- compiler/rustc_mir/src/transform/simplify.rs | 30 +++++++++++++++++-- src/test/codegen/naked-functions.rs | 2 ++ ...simplification.hello.PreCodegen.before.mir | 1 - ...riable.main.SimplifyLocals.after.32bit.mir | 1 - ...riable.main.SimplifyLocals.after.64bit.mir | 1 - .../cycle.main.DestinationPropagation.diff | 1 - .../union.main.DestinationPropagation.diff | 1 - ...patibility.inlined_no_sanitize.Inline.diff | 1 - ...ibility.inlined_target_feature.Inline.diff | 1 - .../inline/inline_cycle.two.Inline.diff | 1 - .../inline_options.main.Inline.after.mir | 1 - ...ine_scopes_parenting.main.Inline.after.mir | 2 -- .../issue_73223.main.PreCodegen.32bit.diff | 1 - .../issue_73223.main.PreCodegen.64bit.diff | 1 - ...wer_intrinsics.f_u64.PreCodegen.before.mir | 1 - ...er_intrinsics.f_unit.PreCodegen.before.mir | 1 - ...nators.test.MultipleReturnTerminators.diff | 2 -- ...annot_opt_generic.RemoveUnneededDrops.diff | 1 - ...ed_drops.dont_opt.RemoveUnneededDrops.diff | 1 - ...nneeded_drops.opt.RemoveUnneededDrops.diff | 1 - ....opt_generic_copy.RemoveUnneededDrops.diff | 1 - .../simplify_locals.c.SimplifyLocals.diff | 2 +- .../simplify_locals.d1.SimplifyLocals.diff | 2 +- .../simplify_locals.d2.SimplifyLocals.diff | 2 +- .../simplify_locals.r.SimplifyLocals.diff | 2 +- .../simplify_locals.t1.SimplifyLocals.diff | 2 +- .../simplify_locals.t2.SimplifyLocals.diff | 2 +- .../simplify_locals.t3.SimplifyLocals.diff | 2 +- ..._locals_fixedpoint.foo.SimplifyLocals.diff | 6 ++-- ...ves_unused_consts.main.SimplifyLocals.diff | 2 +- ...enum.process_void.SimplifyLocals.after.mir | 1 - ...hange_loop_body.PreCodegen.after.32bit.mir | 1 - ...hange_loop_body.PreCodegen.after.64bit.mir | 1 - ...le_storage.while_loop.PreCodegen.after.mir | 2 -- 34 files changed, 40 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_mir/src/transform/simplify.rs b/compiler/rustc_mir/src/transform/simplify.rs index 11539d3ef30f4..06091a07590bd 100644 --- a/compiler/rustc_mir/src/transform/simplify.rs +++ b/compiler/rustc_mir/src/transform/simplify.rs @@ -329,7 +329,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals { // count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from // `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a // fixedpoint where there are no more unused locals. - remove_unused_definitions(&mut used_locals, body); + remove_unused_definitions(&mut used_locals, body, tcx); // Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s. let map = make_local_map(&mut body.local_decls, &used_locals); @@ -459,12 +459,22 @@ impl Visitor<'_> for UsedLocals { } /// Removes unused definitions. Updates the used locals to reflect the changes made. -fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &mut Body<'tcx>) { +fn remove_unused_definitions<'a, 'tcx>( + used_locals: &'a mut UsedLocals, + body: &mut Body<'tcx>, + tcx: TyCtxt<'tcx>, +) { // The use counts are updated as we remove the statements. A local might become unused // during the retain operation, leading to a temporary inconsistency (storage statements or // definitions referencing the local might remain). For correctness it is crucial that this // computation reaches a fixed point. + let def_id = body.source.def_id(); + let is_static = tcx.is_static(def_id); + let param_env = tcx.param_env(def_id); + + let local_decls = body.local_decls.clone(); + let mut modified = true; while modified { modified = false; @@ -476,7 +486,21 @@ fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &m StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => { used_locals.is_used(*local) } - StatementKind::Assign(box (place, _)) => used_locals.is_used(place.local), + StatementKind::Assign(box (place, _)) => { + let used = used_locals.is_used(place.local); + let mut is_zst = false; + + // ZST locals can be removed + if used && !is_static { + let ty = local_decls[place.local].ty; + let param_env_and = param_env.and(ty); + if let Ok(layout) = tcx.layout_of(param_env_and) { + is_zst = layout.is_zst(); + } + } + + used && !is_zst + } StatementKind::SetDiscriminant { ref place, .. } => { used_locals.is_used(place.local) diff --git a/src/test/codegen/naked-functions.rs b/src/test/codegen/naked-functions.rs index 43a6be465bcf8..974f174de8272 100644 --- a/src/test/codegen/naked-functions.rs +++ b/src/test/codegen/naked-functions.rs @@ -9,6 +9,7 @@ #[naked] pub fn naked_empty() { // CHECK-NEXT: {{.+}}: + // CHECK-NEXT: %0 = alloca {}, align 1 // CHECK-NEXT: ret void } @@ -18,6 +19,7 @@ pub fn naked_empty() { // CHECK-NEXT: define void @naked_with_args(i{{[0-9]+( %a)?}}) pub fn naked_with_args(a: isize) { // CHECK-NEXT: {{.+}}: + // CHECK-NEXT: %0 = alloca {}, align 1 // CHECK: ret void } diff --git a/src/test/mir-opt/const_prop/control_flow_simplification.hello.PreCodegen.before.mir b/src/test/mir-opt/const_prop/control_flow_simplification.hello.PreCodegen.before.mir index c2f75e5daeeb2..30512d0bbe874 100644 --- a/src/test/mir-opt/const_prop/control_flow_simplification.hello.PreCodegen.before.mir +++ b/src/test/mir-opt/const_prop/control_flow_simplification.hello.PreCodegen.before.mir @@ -4,7 +4,6 @@ fn hello() -> () { let mut _0: (); // return place in scope 0 at $DIR/control-flow-simplification.rs:11:14: 11:14 bb0: { - _0 = const (); // scope 0 at $DIR/control-flow-simplification.rs:14:6: 14:6 return; // scope 0 at $DIR/control-flow-simplification.rs:15:2: 15:2 } } diff --git a/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.32bit.mir b/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.32bit.mir index a78a6341c299d..c6f1d86ae3a07 100644 --- a/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.32bit.mir +++ b/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.32bit.mir @@ -22,7 +22,6 @@ fn main() -> () { _2 = const 3_i32; // scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34 StorageLive(_3); // scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10 _3 = const 42_u32; // scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38 - _0 = const (); // scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2 StorageDead(_3); // scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2 StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2 StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2 diff --git a/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.64bit.mir b/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.64bit.mir index a78a6341c299d..c6f1d86ae3a07 100644 --- a/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.64bit.mir +++ b/src/test/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals.after.64bit.mir @@ -22,7 +22,6 @@ fn main() -> () { _2 = const 3_i32; // scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34 StorageLive(_3); // scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10 _3 = const 42_u32; // scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38 - _0 = const (); // scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2 StorageDead(_3); // scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2 StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2 StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2 diff --git a/src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff b/src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff index 1fbda50f783e1..621a747e131dd 100644 --- a/src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff +++ b/src/test/mir-opt/dest-prop/cycle.main.DestinationPropagation.diff @@ -56,7 +56,6 @@ StorageLive(_6); // scope 3 at $DIR/cycle.rs:14:10: 14:11 - _6 = _1; // scope 3 at $DIR/cycle.rs:14:10: 14:11 + _6 = _4; // scope 3 at $DIR/cycle.rs:14:10: 14:11 - _5 = const (); // scope 4 at $DIR/cycle.rs:14:5: 14:12 StorageDead(_6); // scope 3 at $DIR/cycle.rs:14:11: 14:12 StorageDead(_5); // scope 3 at $DIR/cycle.rs:14:12: 14:13 _0 = const (); // scope 0 at $DIR/cycle.rs:8:11: 15:2 diff --git a/src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff b/src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff index 0ff3e4b2dcf7d..a3368dcfcc6c7 100644 --- a/src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff +++ b/src/test/mir-opt/dest-prop/union.main.DestinationPropagation.diff @@ -31,7 +31,6 @@ StorageLive(_3); // scope 1 at $DIR/union.rs:15:5: 15:27 StorageLive(_4); // scope 1 at $DIR/union.rs:15:10: 15:26 _4 = (_1.0: u32); // scope 2 at $DIR/union.rs:15:19: 15:24 - _3 = const (); // scope 3 at $DIR/union.rs:15:5: 15:27 StorageDead(_4); // scope 1 at $DIR/union.rs:15:26: 15:27 StorageDead(_3); // scope 1 at $DIR/union.rs:15:27: 15:28 _0 = const (); // scope 0 at $DIR/union.rs:8:11: 16:2 diff --git a/src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff b/src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff index c95cf47695785..77eb326cd8263 100644 --- a/src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff +++ b/src/test/mir-opt/inline/inline_compatibility.inlined_no_sanitize.Inline.diff @@ -16,7 +16,6 @@ - } - - bb1: { -+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:24:5: 24:18 StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:24:18: 24:19 _0 = const (); // scope 0 at $DIR/inline-compatibility.rs:23:37: 25:2 return; // scope 0 at $DIR/inline-compatibility.rs:25:2: 25:2 diff --git a/src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff b/src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff index 2bb928343229f..a32db48715c23 100644 --- a/src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff +++ b/src/test/mir-opt/inline/inline_compatibility.inlined_target_feature.Inline.diff @@ -16,7 +16,6 @@ - } - - bb1: { -+ _1 = const (); // scope 1 at $DIR/inline-compatibility.rs:13:5: 13:21 StorageDead(_1); // scope 0 at $DIR/inline-compatibility.rs:13:21: 13:22 _0 = const (); // scope 0 at $DIR/inline-compatibility.rs:12:40: 14:2 return; // scope 0 at $DIR/inline-compatibility.rs:14:2: 14:2 diff --git a/src/test/mir-opt/inline/inline_cycle.two.Inline.diff b/src/test/mir-opt/inline/inline_cycle.two.Inline.diff index b44baca9bf497..aaf65c5d0a5f3 100644 --- a/src/test/mir-opt/inline/inline_cycle.two.Inline.diff +++ b/src/test/mir-opt/inline/inline_cycle.two.Inline.diff @@ -37,7 +37,6 @@ + StorageDead(_5); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12 + StorageDead(_4); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12 + StorageDead(_3); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12 -+ _1 = const (); // scope 1 at $DIR/inline-cycle.rs:49:5: 49:12 + StorageDead(_2); // scope 0 at $DIR/inline-cycle.rs:49:5: 49:12 StorageDead(_1); // scope 0 at $DIR/inline-cycle.rs:49:12: 49:13 _0 = const (); // scope 0 at $DIR/inline-cycle.rs:48:10: 50:2 diff --git a/src/test/mir-opt/inline/inline_options.main.Inline.after.mir b/src/test/mir-opt/inline/inline_options.main.Inline.after.mir index 4cbdde2ba07d7..865cf25863fdc 100644 --- a/src/test/mir-opt/inline/inline_options.main.Inline.after.mir +++ b/src/test/mir-opt/inline/inline_options.main.Inline.after.mir @@ -48,7 +48,6 @@ fn main() -> () { bb4: { StorageDead(_5); // scope 1 at $DIR/inline-options.rs:10:5: 10:21 - _2 = const (); // scope 1 at $DIR/inline-options.rs:10:5: 10:21 StorageDead(_2); // scope 0 at $DIR/inline-options.rs:10:21: 10:22 _0 = const (); // scope 0 at $DIR/inline-options.rs:8:11: 11:2 return; // scope 0 at $DIR/inline-options.rs:11:2: 11:2 diff --git a/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir index 3d386e3b1756b..d62f78eaa322b 100644 --- a/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir +++ b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir @@ -28,8 +28,6 @@ fn main() -> () { StorageLive(_5); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 _5 = move (_3.0: ()); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageLive(_6); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 - _6 = const (); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 - _0 = const (); // scope 3 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_6); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_5); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_4); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:9: 6:10 diff --git a/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff b/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff index 435b2a1360a6b..d2034bd1e18b1 100644 --- a/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff +++ b/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff @@ -138,7 +138,6 @@ StorageDead(_9); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_7); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2 StorageDead(_1); // scope 0 at $DIR/issue-73223.rs:9:1: 9:2 return; // scope 0 at $DIR/issue-73223.rs:9:2: 9:2 } diff --git a/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff b/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff index 435b2a1360a6b..d2034bd1e18b1 100644 --- a/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff +++ b/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff @@ -138,7 +138,6 @@ StorageDead(_9); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_7); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2 StorageDead(_1); // scope 0 at $DIR/issue-73223.rs:9:1: 9:2 return; // scope 0 at $DIR/issue-73223.rs:9:2: 9:2 } diff --git a/src/test/mir-opt/lower_intrinsics.f_u64.PreCodegen.before.mir b/src/test/mir-opt/lower_intrinsics.f_u64.PreCodegen.before.mir index 740a6e0edb0c4..d0184020f75b5 100644 --- a/src/test/mir-opt/lower_intrinsics.f_u64.PreCodegen.before.mir +++ b/src/test/mir-opt/lower_intrinsics.f_u64.PreCodegen.before.mir @@ -25,7 +25,6 @@ fn f_u64() -> () { bb1: { StorageDead(_3); // scope 1 at $DIR/lower_intrinsics.rs:35:5: 35:21 StorageDead(_2); // scope 1 at $DIR/lower_intrinsics.rs:35:5: 35:21 - _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:34:16: 36:2 return; // scope 0 at $DIR/lower_intrinsics.rs:36:2: 36:2 } } diff --git a/src/test/mir-opt/lower_intrinsics.f_unit.PreCodegen.before.mir b/src/test/mir-opt/lower_intrinsics.f_unit.PreCodegen.before.mir index f8857eda73721..2b9ffaaf97108 100644 --- a/src/test/mir-opt/lower_intrinsics.f_unit.PreCodegen.before.mir +++ b/src/test/mir-opt/lower_intrinsics.f_unit.PreCodegen.before.mir @@ -22,7 +22,6 @@ fn f_unit() -> () { bb1: { StorageDead(_2); // scope 1 at $DIR/lower_intrinsics.rs:29:5: 29:19 StorageDead(_1); // scope 0 at $DIR/lower_intrinsics.rs:29:18: 29:19 - _0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:28:17: 30:2 return; // scope 0 at $DIR/lower_intrinsics.rs:30:2: 30:2 } } diff --git a/src/test/mir-opt/multiple_return_terminators.test.MultipleReturnTerminators.diff b/src/test/mir-opt/multiple_return_terminators.test.MultipleReturnTerminators.diff index fb25cb90021f3..3dd526cfbc5e2 100644 --- a/src/test/mir-opt/multiple_return_terminators.test.MultipleReturnTerminators.diff +++ b/src/test/mir-opt/multiple_return_terminators.test.MultipleReturnTerminators.diff @@ -13,12 +13,10 @@ } bb1: { - _0 = const (); // scope 0 at $DIR/multiple_return_terminators.rs:5:10: 7:6 goto -> bb3; // scope 0 at $DIR/multiple_return_terminators.rs:5:5: 9:6 } bb2: { - _0 = const (); // scope 0 at $DIR/multiple_return_terminators.rs:7:12: 9:6 goto -> bb3; // scope 0 at $DIR/multiple_return_terminators.rs:5:5: 9:6 } diff --git a/src/test/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff b/src/test/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff index e55ee287ba34f..2fc64fa8f524d 100644 --- a/src/test/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff +++ b/src/test/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff @@ -14,7 +14,6 @@ StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:21:5: 21:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11 _3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:21:10: 21:11 - _2 = const (); // scope 1 at $DIR/remove_unneeded_drops.rs:21:5: 21:12 drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $DIR/remove_unneeded_drops.rs:21:5: 21:12 } diff --git a/src/test/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff b/src/test/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff index e5ad8f1ac75a9..18fdac1bc0044 100644 --- a/src/test/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff +++ b/src/test/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff @@ -14,7 +14,6 @@ StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:9:5: 9:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11 _3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:9:10: 9:11 - _2 = const (); // scope 1 at $DIR/remove_unneeded_drops.rs:9:5: 9:12 drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $DIR/remove_unneeded_drops.rs:9:5: 9:12 } diff --git a/src/test/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff b/src/test/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff index bddf0e2103920..1eb3d736765ed 100644 --- a/src/test/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff +++ b/src/test/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff @@ -14,7 +14,6 @@ StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:4:5: 4:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11 _3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:4:10: 4:11 - _2 = const (); // scope 1 at $DIR/remove_unneeded_drops.rs:4:5: 4:12 - drop(_3) -> bb1; // scope 1 at $DIR/remove_unneeded_drops.rs:4:5: 4:12 - } - diff --git a/src/test/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff b/src/test/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff index 87fb8a295c6b0..9b8a50e85416b 100644 --- a/src/test/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff +++ b/src/test/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff @@ -14,7 +14,6 @@ StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:14:5: 14:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11 _3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:14:10: 14:11 - _2 = const (); // scope 1 at $DIR/remove_unneeded_drops.rs:14:5: 14:12 - drop(_3) -> bb1; // scope 1 at $DIR/remove_unneeded_drops.rs:14:5: 14:12 - } - diff --git a/src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff index dbac7dff9e2bb..f7da10c6f0332 100644 --- a/src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff @@ -20,7 +20,7 @@ - _3 = &_1; // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26 - _2 = move _3 as &[u8] (Pointer(Unsize)); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26 - StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:16:26: 16:27 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:13:8: 17:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:13:8: 17:2 StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:17:1: 17:2 return; // scope 0 at $DIR/simplify-locals.rs:17:2: 17:2 } diff --git a/src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff index 76bf175d073d7..caa2a17700bcc 100644 --- a/src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff @@ -11,7 +11,7 @@ - StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17 - discriminant(_1) = 0; // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17 - StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:22:17: 22:18 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:20:9: 23:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:20:9: 23:2 return; // scope 0 at $DIR/simplify-locals.rs:23:2: 23:2 } } diff --git a/src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff index c06fa09053ce8..80496d9a42ab3 100644 --- a/src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff @@ -31,7 +31,7 @@ - // + literal: Const { ty: E, val: Value(Scalar(0x01)) } - StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26 - StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:26: 28:27 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:26:9: 29:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:26:9: 29:2 return; // scope 0 at $DIR/simplify-locals.rs:29:2: 29:2 } } diff --git a/src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff index 85cf398d31693..7d36e1a992855 100644 --- a/src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff @@ -23,7 +23,7 @@ - StorageLive(_3); // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19 - _3 = &mut _1; // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19 - StorageDead(_3); // scope 2 at $DIR/simplify-locals.rs:36:19: 36:20 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:32:8: 37:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:32:8: 37:2 StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:37:1: 37:2 return; // scope 0 at $DIR/simplify-locals.rs:37:2: 37:2 } diff --git a/src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff index 991a0721cca30..89275418d68f0 100644 --- a/src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff @@ -15,7 +15,7 @@ - _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15 - StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18 - StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:42:9: 45:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:42:9: 45:2 return; // scope 0 at $DIR/simplify-locals.rs:45:2: 45:2 } } diff --git a/src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff index 6c9ed96e78ffe..b2344c871e423 100644 --- a/src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff @@ -15,7 +15,7 @@ - _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20 - StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23 - StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:48:9: 51:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:48:9: 51:2 return; // scope 0 at $DIR/simplify-locals.rs:51:2: 51:2 } } diff --git a/src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff index 2d5fb352f8be3..95982f0a9cffe 100644 --- a/src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff @@ -19,7 +19,7 @@ - StorageDead(_3); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24 - StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24 - StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24 - _0 = const (); // scope 0 at $DIR/simplify-locals.rs:54:9: 57:2 +- _0 = const (); // scope 0 at $DIR/simplify-locals.rs:54:9: 57:2 return; // scope 0 at $DIR/simplify-locals.rs:57:2: 57:2 } } diff --git a/src/test/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals.diff index 70725e5f14f7d..69da1c7be8842 100644 --- a/src/test/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals.diff @@ -30,7 +30,7 @@ } bb1: { - _0 = const (); // scope 0 at $DIR/simplify-locals-fixedpoint.rs:8:6: 8:6 +- _0 = const (); // scope 0 at $DIR/simplify-locals-fixedpoint.rs:8:6: 8:6 goto -> bb7; // scope 0 at $DIR/simplify-locals-fixedpoint.rs:4:5: 8:6 } @@ -51,12 +51,12 @@ } bb4: { - _0 = const (); // scope 1 at $DIR/simplify-locals-fixedpoint.rs:5:21: 7:10 +- _0 = const (); // scope 1 at $DIR/simplify-locals-fixedpoint.rs:5:21: 7:10 goto -> bb6; // scope 1 at $DIR/simplify-locals-fixedpoint.rs:5:9: 7:10 } bb5: { - _0 = const (); // scope 1 at $DIR/simplify-locals-fixedpoint.rs:7:10: 7:10 +- _0 = const (); // scope 1 at $DIR/simplify-locals-fixedpoint.rs:7:10: 7:10 goto -> bb6; // scope 1 at $DIR/simplify-locals-fixedpoint.rs:5:9: 7:10 } diff --git a/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff index 34f8ca870cd23..8d6d531bd34e0 100644 --- a/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff @@ -71,8 +71,8 @@ - StorageDead(_9); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:16:34: 16:35 - StorageDead(_11); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:16:35: 16:36 - StorageDead(_8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:16:35: 16:36 +- _0 = const (); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:12:11: 17:2 + StorageDead(_2); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:16:35: 16:36 - _0 = const (); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:12:11: 17:2 return; // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:17:2: 17:2 } } diff --git a/src/test/mir-opt/uninhabited_enum.process_void.SimplifyLocals.after.mir b/src/test/mir-opt/uninhabited_enum.process_void.SimplifyLocals.after.mir index 539ed710719dc..eeaabb7b9884e 100644 --- a/src/test/mir-opt/uninhabited_enum.process_void.SimplifyLocals.after.mir +++ b/src/test/mir-opt/uninhabited_enum.process_void.SimplifyLocals.after.mir @@ -13,7 +13,6 @@ fn process_void(_1: *const Void) -> () { bb0: { StorageLive(_2); // scope 0 at $DIR/uninhabited-enum.rs:14:8: 14:14 _2 = &(*_1); // scope 2 at $DIR/uninhabited-enum.rs:14:26: 14:33 - _0 = const (); // scope 0 at $DIR/uninhabited-enum.rs:13:41: 17:2 StorageDead(_2); // scope 0 at $DIR/uninhabited-enum.rs:17:1: 17:2 return; // scope 0 at $DIR/uninhabited-enum.rs:17:2: 17:2 } diff --git a/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.32bit.mir b/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.32bit.mir index dae0cbb65a4b3..b6767077d429a 100644 --- a/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.32bit.mir +++ b/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.32bit.mir @@ -10,7 +10,6 @@ fn change_loop_body() -> () { bb0: { StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15 _1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19 - _0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6 StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2 return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2 } diff --git a/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.64bit.mir b/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.64bit.mir index dae0cbb65a4b3..b6767077d429a 100644 --- a/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.64bit.mir +++ b/src/test/mir-opt/while_let_loops.change_loop_body.PreCodegen.after.64bit.mir @@ -10,7 +10,6 @@ fn change_loop_body() -> () { bb0: { StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15 _1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19 - _0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6 StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2 return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2 } diff --git a/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir b/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir index 0c034891a80db..eb3ab1c31e36c 100644 --- a/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir +++ b/src/test/mir-opt/while_storage.while_loop.PreCodegen.after.mir @@ -24,7 +24,6 @@ fn while_loop(_1: bool) -> () { } bb2: { - _0 = const (); // scope 0 at $DIR/while-storage.rs:10:5: 14:6 goto -> bb7; // scope 0 at $DIR/while-storage.rs:10:5: 14:6 } @@ -44,7 +43,6 @@ fn while_loop(_1: bool) -> () { } bb5: { - _0 = const (); // scope 0 at $DIR/while-storage.rs:12:13: 12:18 StorageDead(_4); // scope 0 at $DIR/while-storage.rs:13:9: 13:10 goto -> bb7; // scope 0 at $DIR/while-storage.rs:1:1: 1:1 } From ac1cebb771b5478c325bfb02345872c64737e9aa Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 3 Jan 2021 14:51:24 +0100 Subject: [PATCH 2/3] Move ZST check inside UsedLocals --- compiler/rustc_mir/src/transform/simplify.rs | 103 +++++++++--------- ...ine_scopes_parenting.main.Inline.after.mir | 1 + 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_mir/src/transform/simplify.rs b/compiler/rustc_mir/src/transform/simplify.rs index 06091a07590bd..d06dbdc3b792c 100644 --- a/compiler/rustc_mir/src/transform/simplify.rs +++ b/compiler/rustc_mir/src/transform/simplify.rs @@ -31,10 +31,10 @@ use crate::transform::MirPass; use rustc_index::vec::{Idx, IndexVec}; use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::*; +use rustc_middle::ty::ParamEnv; use rustc_middle::ty::TyCtxt; use smallvec::SmallVec; -use std::borrow::Cow; -use std::convert::TryInto; +use std::{borrow::Cow, convert::TryInto}; pub struct SimplifyCfg { label: String, @@ -322,17 +322,18 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals { trace!("running SimplifyLocals on {:?}", body.source); // First, we're going to get a count of *actual* uses for every `Local`. - let mut used_locals = UsedLocals::new(body); + let mut used_locals = UsedLocals::new(body, tcx); // Next, we're going to remove any `Local` with zero actual uses. When we remove those // `Locals`, we're also going to subtract any uses of other `Locals` from the `used_locals` // count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from // `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a // fixedpoint where there are no more unused locals. - remove_unused_definitions(&mut used_locals, body, tcx); + remove_unused_definitions(&mut used_locals, body); // Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s. - let map = make_local_map(&mut body.local_decls, &used_locals); + let arg_count = body.arg_count.try_into().unwrap(); + let map = make_local_map(&mut body.local_decls, &used_locals, arg_count); // Only bother running the `LocalUpdater` if we actually found locals to remove. if map.iter().any(Option::is_none) { @@ -346,54 +347,61 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals { } /// Construct the mapping while swapping out unused stuff out from the `vec`. -fn make_local_map( +fn make_local_map<'tcx, V>( local_decls: &mut IndexVec, - used_locals: &UsedLocals, + used_locals: &UsedLocals<'tcx>, + arg_count: u32, ) -> IndexVec> { - let mut map: IndexVec> = IndexVec::from_elem(None, &*local_decls); + let mut map: IndexVec> = IndexVec::from_elem(None, local_decls); let mut used = Local::new(0); for alive_index in local_decls.indices() { - // `is_used` treats the `RETURN_PLACE` and arguments as used. - if !used_locals.is_used(alive_index) { - continue; - } - - map[alive_index] = Some(used); - if alive_index != used { - local_decls.swap(alive_index, used); + // When creating the local map treat the `RETURN_PLACE` and arguments as used. + if alive_index.as_u32() <= arg_count || used_locals.is_used(alive_index) { + map[alive_index] = Some(used); + if alive_index != used { + local_decls.swap(alive_index, used); + } + used.increment_by(1); } - used.increment_by(1); } local_decls.truncate(used.index()); map } /// Keeps track of used & unused locals. -struct UsedLocals { +struct UsedLocals<'tcx> { increment: bool, - arg_count: u32, use_count: IndexVec, + is_static: bool, + local_decls: IndexVec>, + param_env: ParamEnv<'tcx>, + tcx: TyCtxt<'tcx>, } -impl UsedLocals { +impl UsedLocals<'tcx> { /// Determines which locals are used & unused in the given body. - fn new(body: &Body<'_>) -> Self { + fn new(body: &Body<'tcx>, tcx: TyCtxt<'tcx>) -> Self { + let def_id = body.source.def_id(); + let is_static = tcx.is_static(def_id); + let param_env = tcx.param_env(def_id); + let local_decls = body.local_decls.clone(); let mut this = Self { increment: true, - arg_count: body.arg_count.try_into().unwrap(), use_count: IndexVec::from_elem(0, &body.local_decls), + is_static, + local_decls, + param_env, + tcx, }; this.visit_body(body); this } /// Checks if local is used. - /// - /// Return place and arguments are always considered used. fn is_used(&self, local: Local) -> bool { trace!("is_used({:?}): use_count: {:?}", local, self.use_count[local]); - local.as_u32() <= self.arg_count || self.use_count[local] != 0 + self.use_count[local] != 0 } /// Updates the use counts to reflect the removal of given statement. @@ -422,7 +430,7 @@ impl UsedLocals { } } -impl Visitor<'_> for UsedLocals { +impl Visitor<'tcx> for UsedLocals<'tcx> { fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { match statement.kind { StatementKind::LlvmInlineAsm(..) @@ -448,7 +456,23 @@ impl Visitor<'_> for UsedLocals { } } - fn visit_local(&mut self, local: &Local, _ctx: PlaceContext, _location: Location) { + fn visit_local(&mut self, local: &Local, ctx: PlaceContext, _location: Location) { + debug!("local: {:?} is_static: {:?}, ctx: {:?}", local, self.is_static, ctx); + // Do not count a local as used in `_local = ` if RHS is a ZST. + let store = matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Store)); + // Do not count _0 as a used in `return;` if it is a ZST. + let return_place = *local == RETURN_PLACE + && matches!(ctx, PlaceContext::NonMutatingUse(visit::NonMutatingUseContext::Move)); + if !self.is_static && (store || return_place) { + let ty = self.local_decls[*local].ty; + let param_env_and = self.param_env.and(ty); + if let Ok(layout) = self.tcx.layout_of(param_env_and) { + debug!("layout.is_zst: {:?}", layout.is_zst()); + if layout.is_zst() { + return; + } + } + } if self.increment { self.use_count[*local] += 1; } else { @@ -460,21 +484,14 @@ impl Visitor<'_> for UsedLocals { /// Removes unused definitions. Updates the used locals to reflect the changes made. fn remove_unused_definitions<'a, 'tcx>( - used_locals: &'a mut UsedLocals, + used_locals: &'a mut UsedLocals<'tcx>, body: &mut Body<'tcx>, - tcx: TyCtxt<'tcx>, ) { // The use counts are updated as we remove the statements. A local might become unused // during the retain operation, leading to a temporary inconsistency (storage statements or // definitions referencing the local might remain). For correctness it is crucial that this // computation reaches a fixed point. - let def_id = body.source.def_id(); - let is_static = tcx.is_static(def_id); - let param_env = tcx.param_env(def_id); - - let local_decls = body.local_decls.clone(); - let mut modified = true; while modified { modified = false; @@ -486,21 +503,7 @@ fn remove_unused_definitions<'a, 'tcx>( StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => { used_locals.is_used(*local) } - StatementKind::Assign(box (place, _)) => { - let used = used_locals.is_used(place.local); - let mut is_zst = false; - - // ZST locals can be removed - if used && !is_static { - let ty = local_decls[place.local].ty; - let param_env_and = param_env.and(ty); - if let Ok(layout) = tcx.layout_of(param_env_and) { - is_zst = layout.is_zst(); - } - } - - used && !is_zst - } + StatementKind::Assign(box (place, _)) => used_locals.is_used(place.local), StatementKind::SetDiscriminant { ref place, .. } => { used_locals.is_used(place.local) diff --git a/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir index d62f78eaa322b..e5ce03a453ba1 100644 --- a/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir +++ b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir @@ -28,6 +28,7 @@ fn main() -> () { StorageLive(_5); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 _5 = move (_3.0: ()); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageLive(_6); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + _6 = const (); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_6); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_5); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 StorageDead(_4); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:9: 6:10 From bab2e5696017fed106f2782b069fe8bcadbd12c5 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 3 Jan 2021 15:45:15 +0100 Subject: [PATCH 3/3] Do not emit alloca for ZST local even if it is uninitialized --- compiler/rustc_codegen_ssa/src/mir/analyze.rs | 13 ++++++++++++- src/test/codegen/naked-functions.rs | 2 -- src/test/run-make/const_fn_mir/dump.mir | 1 - 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/analyze.rs b/compiler/rustc_codegen_ssa/src/mir/analyze.rs index fd0ff5b66e607..714aed341e250 100644 --- a/compiler/rustc_codegen_ssa/src/mir/analyze.rs +++ b/compiler/rustc_codegen_ssa/src/mir/analyze.rs @@ -281,7 +281,18 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx> Some(assignment_location) => { assignment_location.dominates(location, &self.dominators) } - None => false, + None => { + debug!("No first assignment found for {:?}", local); + // We have not seen any assignment to the local yet, + // but before marking not_ssa, check if it is a ZST, + // in which case we don't need to initialize the local. + let ty = self.fx.mir.local_decls[local].ty; + let ty = self.fx.monomorphize(ty); + + let is_zst = self.fx.cx.layout_of(ty).is_zst(); + debug!("is_zst: {}", is_zst); + is_zst + } }; if !ssa_read { self.not_ssa(local); diff --git a/src/test/codegen/naked-functions.rs b/src/test/codegen/naked-functions.rs index 974f174de8272..43a6be465bcf8 100644 --- a/src/test/codegen/naked-functions.rs +++ b/src/test/codegen/naked-functions.rs @@ -9,7 +9,6 @@ #[naked] pub fn naked_empty() { // CHECK-NEXT: {{.+}}: - // CHECK-NEXT: %0 = alloca {}, align 1 // CHECK-NEXT: ret void } @@ -19,7 +18,6 @@ pub fn naked_empty() { // CHECK-NEXT: define void @naked_with_args(i{{[0-9]+( %a)?}}) pub fn naked_with_args(a: isize) { // CHECK-NEXT: {{.+}}: - // CHECK-NEXT: %0 = alloca {}, align 1 // CHECK: ret void } diff --git a/src/test/run-make/const_fn_mir/dump.mir b/src/test/run-make/const_fn_mir/dump.mir index 3dac42c6782ea..40a21d73634f0 100644 --- a/src/test/run-make/const_fn_mir/dump.mir +++ b/src/test/run-make/const_fn_mir/dump.mir @@ -14,7 +14,6 @@ fn main() -> () { bb1: { StorageDead(_1); // scope 0 at main.rs:9:10: 9:11 - _0 = const (); // scope 0 at main.rs:8:11: 10:2 return; // scope 0 at main.rs:10:2: 10:2 } }