Skip to content

Commit 2ad110d

Browse files
committed
mir-opt: Eliminate statements need debuginfo when mir-opt-level >= 2
`-Copt-level=0` (`-Zmir-opt-level=1`) is preferred for debugging.
1 parent 825f2b2 commit 2ad110d

File tree

32 files changed

+121
-65
lines changed

32 files changed

+121
-65
lines changed

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
5353
.visit_body_preserves_cfg(body);
5454

5555
if any_replacement {
56-
crate::simplify::remove_unused_definitions(body);
56+
crate::simplify::remove_unused_definitions(body, false);
5757
}
5858
}
5959

compiler/rustc_mir_transform/src/dead_store_elimination.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ use rustc_mir_dataflow::impls::{
2222
LivenessTransferFunction, MaybeTransitiveLiveLocals, borrowed_locals,
2323
};
2424

25+
use crate::simplify::remove_unused_definitions;
2526
use crate::util::is_within_packed;
2627

2728
/// Performs the optimization on the body
2829
///
2930
/// The `borrowed` set must be a `DenseBitSet` of all the locals that are ever borrowed in this
3031
/// body. It can be generated via the [`borrowed_locals`] function.
31-
fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
32+
fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
3233
let borrowed_locals = borrowed_locals(body);
3334

3435
// If the user requests complete debuginfo, mark the locals that appear in it as live, so
@@ -89,8 +90,9 @@ fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
8990
}
9091

9192
if patch.is_empty() && call_operands_to_move.is_empty() {
92-
return;
93+
return false;
9394
}
95+
let eliminated = !patch.is_empty();
9496

9597
let bbs = body.basic_blocks.as_mut_preserves_cfg();
9698
for (Location { block, statement_index }, drop_debuginfo) in patch {
@@ -104,6 +106,8 @@ fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
104106
let Operand::Copy(place) = *arg else { bug!() };
105107
*arg = Operand::Move(place);
106108
}
109+
110+
eliminated
107111
}
108112

109113
pub(super) enum DeadStoreElimination {
@@ -124,7 +128,10 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
124128
}
125129

126130
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
127-
eliminate(tcx, body);
131+
if eliminate(tcx, body) {
132+
// Remove unnecessary StorageLive and StorageDead annotations.
133+
remove_unused_definitions(body, true);
134+
}
128135
}
129136

130137
fn is_required(&self) -> bool {

compiler/rustc_mir_transform/src/ref_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
9999
replacer.visit_body_preserves_cfg(body);
100100

101101
if replacer.any_replacement {
102-
crate::simplify::remove_unused_definitions(body);
102+
crate::simplify::remove_unused_definitions(body, true);
103103
}
104104

105105
replacer.any_replacement

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
416416
trace!("running SimplifyLocals on {:?}", body.source);
417417

418418
// First, we're going to get a count of *actual* uses for every `Local`.
419-
let mut used_locals = UsedLocals::new(body);
419+
let mut used_locals = UsedLocals::new(body, false);
420420

421421
// Next, we're going to remove any `Local` with zero actual uses. When we remove those
422422
// `Locals`, we're also going to subtract any uses of other `Locals` from the `used_locals`
@@ -444,9 +444,9 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyLocals {
444444
}
445445
}
446446

447-
pub(super) fn remove_unused_definitions<'tcx>(body: &mut Body<'tcx>) {
447+
pub(super) fn remove_unused_definitions<'tcx>(body: &mut Body<'tcx>, allow_debuginfo: bool) {
448448
// First, we're going to get a count of *actual* uses for every `Local`.
449-
let mut used_locals = UsedLocals::new(body);
449+
let mut used_locals = UsedLocals::new(body, allow_debuginfo);
450450

451451
// Next, we're going to remove any `Local` with zero actual uses. When we remove those
452452
// `Locals`, we're also going to subtract any uses of other `Locals` from the `used_locals`
@@ -486,16 +486,18 @@ struct UsedLocals {
486486
arg_count: u32,
487487
use_count: IndexVec<Local, u32>,
488488
debuginfo_use: IndexVec<Local, bool>,
489+
allow_debuginfo: bool,
489490
}
490491

491492
impl UsedLocals {
492493
/// Determines which locals are used & unused in the given body.
493-
fn new(body: &Body<'_>) -> Self {
494+
fn new(body: &Body<'_>, allow_debuginfo: bool) -> Self {
494495
let mut this = Self {
495496
increment: true,
496497
arg_count: body.arg_count.try_into().unwrap(),
497498
use_count: IndexVec::from_elem(0, &body.local_decls),
498499
debuginfo_use: IndexVec::from_elem(false, &body.local_decls),
500+
allow_debuginfo,
499501
};
500502
this.visit_body(body);
501503
this
@@ -513,7 +515,10 @@ impl UsedLocals {
513515
}
514516

515517
fn is_only_debuginfo_used(&self, local: Local) -> bool {
516-
local.as_u32() > self.arg_count && self.use_count[local] == 0 && self.debuginfo_use[local]
518+
self.allow_debuginfo
519+
&& local.as_u32() > self.arg_count
520+
&& self.use_count[local] == 0
521+
&& self.debuginfo_use[local]
517522
}
518523

519524
fn is_debuginfo_used(&self, local: Local) -> bool {

tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.panic-abort.diff

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
bb0: {
2525
- StorageLive(_2);
2626
_2 = &raw mut _1;
27-
- StorageLive(_3);
28-
- StorageLive(_4);
29-
- _4 = &mut (*_2);
30-
- _3 = &mut (*_4);
31-
- StorageDead(_4);
27+
StorageLive(_3);
28+
StorageLive(_4);
29+
_4 = &mut (*_2);
30+
_3 = &mut (*_4);
31+
StorageDead(_4);
3232
- StorageLive(_5);
3333
- _5 = copy _2;
34-
+ // DBG: AssignRef(_4, (*_2))
35-
+ // DBG: AssignRef(_3, (*_4))
3634
StorageLive(_6);
3735
- StorageLive(_7);
3836
- _7 = copy _5;
@@ -45,7 +43,7 @@
4543
StorageDead(_6);
4644
_0 = const ();
4745
- StorageDead(_5);
48-
- StorageDead(_3);
46+
StorageDead(_3);
4947
- StorageDead(_2);
5048
return;
5149
}

tests/mir-opt/copy-prop/reborrow.demiraw.CopyProp.panic-unwind.diff

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
bb0: {
2525
- StorageLive(_2);
2626
_2 = &raw mut _1;
27-
- StorageLive(_3);
28-
- StorageLive(_4);
29-
- _4 = &mut (*_2);
30-
- _3 = &mut (*_4);
31-
- StorageDead(_4);
27+
StorageLive(_3);
28+
StorageLive(_4);
29+
_4 = &mut (*_2);
30+
_3 = &mut (*_4);
31+
StorageDead(_4);
3232
- StorageLive(_5);
3333
- _5 = copy _2;
34-
+ // DBG: AssignRef(_4, (*_2))
35-
+ // DBG: AssignRef(_3, (*_4))
3634
StorageLive(_6);
3735
- StorageLive(_7);
3836
- _7 = copy _5;
@@ -45,7 +43,7 @@
4543
StorageDead(_6);
4644
_0 = const ();
4745
- StorageDead(_5);
48-
- StorageDead(_3);
46+
StorageDead(_3);
4947
- StorageDead(_2);
5048
return;
5149
}

tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &raw mut _1;
26-
- StorageLive(_3);
26+
StorageLive(_3);
2727
_3 = &raw mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = copy _2;
@@ -39,7 +39,7 @@
3939
StorageDead(_5);
4040
_0 = const ();
4141
- StorageDead(_4);
42-
- StorageDead(_3);
42+
StorageDead(_3);
4343
- StorageDead(_2);
4444
return;
4545
}

tests/mir-opt/copy-prop/reborrow.miraw.CopyProp.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &raw mut _1;
26-
- StorageLive(_3);
26+
StorageLive(_3);
2727
_3 = &raw mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = copy _2;
@@ -39,7 +39,7 @@
3939
StorageDead(_5);
4040
_0 = const ();
4141
- StorageDead(_4);
42-
- StorageDead(_3);
42+
StorageDead(_3);
4343
- StorageDead(_2);
4444
return;
4545
}

tests/mir-opt/copy-prop/reborrow.remut.CopyProp.panic-abort.diff

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &mut _1;
26-
- StorageLive(_3);
27-
- _3 = &mut (*_2);
26+
StorageLive(_3);
27+
_3 = &mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = move _2;
30-
+ // DBG: AssignRef(_3, (*_2))
3130
StorageLive(_5);
3231
- StorageLive(_6);
3332
- _6 = move _4;
@@ -40,7 +39,7 @@
4039
StorageDead(_5);
4140
_0 = const ();
4241
- StorageDead(_4);
43-
- StorageDead(_3);
42+
StorageDead(_3);
4443
- StorageDead(_2);
4544
return;
4645
}

tests/mir-opt/copy-prop/reborrow.remut.CopyProp.panic-unwind.diff

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &mut _1;
26-
- StorageLive(_3);
27-
- _3 = &mut (*_2);
26+
StorageLive(_3);
27+
_3 = &mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = move _2;
30-
+ // DBG: AssignRef(_3, (*_2))
3130
StorageLive(_5);
3231
- StorageLive(_6);
3332
- _6 = move _4;
@@ -40,7 +39,7 @@
4039
StorageDead(_5);
4140
_0 = const ();
4241
- StorageDead(_4);
43-
- StorageDead(_3);
42+
StorageDead(_3);
4443
- StorageDead(_2);
4544
return;
4645
}

tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &mut _1;
26-
- StorageLive(_3);
26+
StorageLive(_3);
2727
_3 = &raw mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = move _2;
@@ -39,7 +39,7 @@
3939
StorageDead(_5);
4040
_0 = const ();
4141
- StorageDead(_4);
42-
- StorageDead(_3);
42+
StorageDead(_3);
4343
- StorageDead(_2);
4444
return;
4545
}

tests/mir-opt/copy-prop/reborrow.reraw.CopyProp.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
bb0: {
2424
- StorageLive(_2);
2525
_2 = &mut _1;
26-
- StorageLive(_3);
26+
StorageLive(_3);
2727
_3 = &raw mut (*_2);
2828
- StorageLive(_4);
2929
- _4 = move _2;
@@ -39,7 +39,7 @@
3939
StorageDead(_5);
4040
_0 = const ();
4141
- StorageDead(_4);
42-
- StorageDead(_3);
42+
StorageDead(_3);
4343
- StorageDead(_2);
4444
return;
4545
}

tests/mir-opt/dead-store-elimination/cycle.cycle.DeadStoreElimination-initial.diff

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
- _3 = copy _2;
2020
- _2 = copy _1;
2121
- _1 = copy _5;
22-
+ nop;
23-
+ nop;
24-
+ nop;
25-
+ nop;
2622
_4 = cond() -> [return: bb1, unwind continue];
2723
}
2824

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-abort.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
bb0: {
1818
StorageLive(_1);
1919
_1 = Un { us: const 1_u32 };
20+
StorageLive(_2);
2021
_2 = copy (_1.0: u32);
22+
StorageDead(_2);
2123
StorageDead(_1);
2224
return;
2325
}

tests/mir-opt/dest-prop/union.main.DestinationPropagation.panic-unwind.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
bb0: {
1818
StorageLive(_1);
1919
_1 = Un { us: const 1_u32 };
20+
StorageLive(_2);
2021
_2 = copy (_1.0: u32);
22+
StorageDead(_2);
2123
StorageDead(_1);
2224
return;
2325
}

tests/mir-opt/early_otherwise_branch_unwind.poll.EarlyOtherwiseBranch.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@
5353
}
5454

5555
bb6: {
56+
StorageLive(_6);
5657
_6 = copy ((((_1 as Ready).0: std::result::Result<std::option::Option<std::vec::Vec<u8>>, u8>) as Err).0: u8);
5758
_0 = const ();
59+
StorageDead(_6);
5860
goto -> bb17;
5961
}
6062

tests/mir-opt/issue_101973.inner.GVN.panic-abort.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
StorageLive(_4);
3131
StorageLive(_5);
3232
_5 = copy _1;
33-
nop;
3433
- StorageLive(_14);
3534
- _14 = BitAnd(copy _5, const 255_u32);
3635
- _4 = BitOr(const 0_u32, move _14);

tests/mir-opt/issue_101973.inner.GVN.panic-unwind.diff

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
StorageLive(_4);
3131
StorageLive(_5);
3232
_5 = copy _1;
33-
nop;
3433
- StorageLive(_14);
3534
- _14 = BitAnd(copy _5, const 255_u32);
3635
- _4 = BitOr(const 0_u32, move _14);

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-abort.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn num_to_digit(_1: char) -> u32 {
1919
}
2020

2121
bb0: {
22+
StorageLive(_7);
2223
StorageLive(_2);
2324
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind unreachable];
2425
}
@@ -28,6 +29,7 @@ fn num_to_digit(_1: char) -> u32 {
2829
StorageLive(_3);
2930
_3 = discriminant(_2);
3031
StorageDead(_2);
32+
StorageDead(_7);
3133
switchInt(move _3) -> [1: bb2, otherwise: bb7];
3234
}
3335

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.32bit.panic-unwind.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn num_to_digit(_1: char) -> u32 {
1919
}
2020

2121
bb0: {
22+
StorageLive(_7);
2223
StorageLive(_2);
2324
_2 = char::methods::<impl char>::to_digit(copy _1, const 8_u32) -> [return: bb1, unwind continue];
2425
}
@@ -28,6 +29,7 @@ fn num_to_digit(_1: char) -> u32 {
2829
StorageLive(_3);
2930
_3 = discriminant(_2);
3031
StorageDead(_2);
32+
StorageDead(_7);
3133
switchInt(move _3) -> [1: bb2, otherwise: bb7];
3234
}
3335

0 commit comments

Comments
 (0)