Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variable debuginfo being optimized away at mir-opt-level=2 #103657

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/const_debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct ConstDebugInfo;

impl<'tcx> MirPass<'tcx> for ConstDebugInfo {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.opts.unstable_opts.unsound_mir_opts && sess.mir_opt_level() > 0
sess.mir_opt_level() > 0
}

fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand Down
28 changes: 22 additions & 6 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ use rustc_mir_dataflow::Analysis;

/// Performs the optimization on the body
///
/// The `borrowed` set must be a `BitSet` of all the locals that are ever borrowed in this body. It
/// The `always_live` set must be a `BitSet` of all the locals that are considered always alive and
/// never eliminated. This should be, at least, the set of locals which are ever borrowed in this
/// body. It may include other locals as well if necessary. The minimum set of always alive locals
/// can be generated via the [`borrowed_locals`] function.
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitSet<Local>) {
let mut live = MaybeTransitiveLiveLocals::new(borrowed)
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, always_live: &BitSet<Local>) {
let mut live = MaybeTransitiveLiveLocals::new(always_live)
.into_engine(tcx, body)
.iterate_to_fixpoint()
.into_results_cursor(body);
Expand All @@ -41,7 +43,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
StatementKind::Assign(box (place, _))
| StatementKind::SetDiscriminant { place: box place, .. }
| StatementKind::Deinit(box place) => {
if !place.is_indirect() && !borrowed.contains(place.local) {
if !place.is_indirect() && !always_live.contains(place.local) {
live.seek_before_primary_effect(loc);
if !live.get().contains(place.local) {
patch.push(loc);
Expand Down Expand Up @@ -80,7 +82,21 @@ impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let borrowed = borrowed_locals(body);
eliminate(tcx, body, &borrowed);
let mut always_live = borrowed_locals(body);

// Include any locals which are used by debuginfo unless we're at a high enough MIR opt
// level that degrading debuginfo is acceptable.
if tcx.sess.mir_opt_level() < 3 {
for x in &body.var_debug_info {
match x.value {
VarDebugInfoContents::Place(p) => {
always_live.insert(p.local);
}
VarDebugInfoContents::Const(..) => {}
}
}
}

eliminate(tcx, body, &always_live);
}
}
28 changes: 28 additions & 0 deletions src/test/codegen/debuginfo-constant-locals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// compile-flags: -g -O

// Check that simple constant values are preserved in debuginfo across both MIR opts and LLVM opts

#![crate_type = "lib"]

#[no_mangle]
pub fn check_it() {
let a = 1;
let b = 42;

foo(a + b);
}

#[inline(never)]
fn foo(x: i32) {
std::process::exit(x);
}

// CHECK-LABEL: @check_it
// CHECK: call void @llvm.dbg.value(metadata i32 1, metadata ![[a_metadata:[0-9]+]], metadata !DIExpression())
// CHECK: call void @llvm.dbg.value(metadata i32 42, metadata ![[b_metadata:[0-9]+]], metadata !DIExpression())

// CHECK: ![[a_metadata]] = !DILocalVariable(name: "a"
// CHECK-SAME: line: 9

// CHECK: ![[b_metadata]] = !DILocalVariable(name: "b"
// CHECK-SAME: line: 10
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@

fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:+0:11: +0:11
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
scope 1 {
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
debug x => const 4_i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
scope 2 {
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
let _3: u32; // in scope 2 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
debug y => const 3_i32; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
scope 3 {
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
debug z => const 42_u32; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
}
}
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
StorageLive(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
StorageLive(_3); // scope 2 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
StorageDead(_3); // scope 2 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
return; // scope 0 at $DIR/optimizes_into_variable.rs:+4:2: +4:2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// compile-flags: -Zmir-opt-level=2
// mir-opt-level is fixed at 2 because that's the max level that can be set on stable
// EMIT_MIR constant_local_debuginfo.main.DeadStoreElimination.diff
fn main() {
let a = 1;
let b = 4;

foo(a + b);
}

#[inline(never)]
fn foo(x: i32) {
std::process::exit(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
- // MIR for `main` before DeadStoreElimination
+ // MIR for `main` after DeadStoreElimination

fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/constant-local-debuginfo.rs:+0:11: +0:11
let _1: i32; // in scope 0 at $DIR/constant-local-debuginfo.rs:+1:9: +1:10
let _3: (); // in scope 0 at $DIR/constant-local-debuginfo.rs:+4:5: +4:15
let mut _4: i32; // in scope 0 at $DIR/constant-local-debuginfo.rs:+4:9: +4:14
let mut _5: i32; // in scope 0 at $DIR/constant-local-debuginfo.rs:+4:9: +4:10
let mut _6: i32; // in scope 0 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
scope 1 {
debug a => const 1_i32; // in scope 1 at $DIR/constant-local-debuginfo.rs:+1:9: +1:10
let _2: i32; // in scope 1 at $DIR/constant-local-debuginfo.rs:+2:9: +2:10
scope 2 {
debug b => const 4_i32; // in scope 2 at $DIR/constant-local-debuginfo.rs:+2:9: +2:10
}
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/constant-local-debuginfo.rs:+1:9: +1:10
- _1 = const 1_i32; // scope 0 at $DIR/constant-local-debuginfo.rs:+1:13: +1:14
+ nop; // scope 0 at $DIR/constant-local-debuginfo.rs:+1:13: +1:14
StorageLive(_2); // scope 1 at $DIR/constant-local-debuginfo.rs:+2:9: +2:10
- _2 = const 4_i32; // scope 1 at $DIR/constant-local-debuginfo.rs:+2:13: +2:14
+ nop; // scope 1 at $DIR/constant-local-debuginfo.rs:+2:13: +2:14
StorageLive(_3); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:5: +4:15
StorageLive(_4); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:9: +4:14
StorageLive(_5); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:9: +4:10
- _5 = const 1_i32; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:9: +4:10
+ nop; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:9: +4:10
StorageLive(_6); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
- _6 = const 4_i32; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
+ nop; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
_4 = const 5_i32; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:9: +4:14
StorageDead(_6); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
StorageDead(_5); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:13: +4:14
_3 = foo(move _4) -> bb1; // scope 2 at $DIR/constant-local-debuginfo.rs:+4:5: +4:15
// mir::Constant
// + span: $DIR/constant-local-debuginfo.rs:8:5: 8:8
// + literal: Const { ty: fn(i32) {foo}, val: Value(<ZST>) }
}

bb1: {
StorageDead(_4); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:14: +4:15
StorageDead(_3); // scope 2 at $DIR/constant-local-debuginfo.rs:+4:15: +4:16
nop; // scope 0 at $DIR/constant-local-debuginfo.rs:+0:11: +5:2
StorageDead(_2); // scope 1 at $DIR/constant-local-debuginfo.rs:+5:1: +5:2
StorageDead(_1); // scope 0 at $DIR/constant-local-debuginfo.rs:+5:1: +5:2
return; // scope 0 at $DIR/constant-local-debuginfo.rs:+5:2: +5:2
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,18 @@

bb3: {
StorageLive(_6); // scope 0 at $DIR/cycle.rs:+4:13: +4:17
- _6 = _3; // scope 0 at $DIR/cycle.rs:+4:20: +4:21
+ nop; // scope 0 at $DIR/cycle.rs:+4:20: +4:21
_6 = _3; // scope 0 at $DIR/cycle.rs:+4:20: +4:21
StorageLive(_7); // scope 1 at $DIR/cycle.rs:+5:13: +5:14
- _7 = _2; // scope 1 at $DIR/cycle.rs:+5:13: +5:14
- _3 = move _7; // scope 1 at $DIR/cycle.rs:+5:9: +5:14
+ nop; // scope 1 at $DIR/cycle.rs:+5:13: +5:14
+ nop; // scope 1 at $DIR/cycle.rs:+5:9: +5:14
_7 = _2; // scope 1 at $DIR/cycle.rs:+5:13: +5:14
_3 = move _7; // scope 1 at $DIR/cycle.rs:+5:9: +5:14
StorageDead(_7); // scope 1 at $DIR/cycle.rs:+5:13: +5:14
StorageLive(_8); // scope 1 at $DIR/cycle.rs:+6:13: +6:14
- _8 = _1; // scope 1 at $DIR/cycle.rs:+6:13: +6:14
- _2 = move _8; // scope 1 at $DIR/cycle.rs:+6:9: +6:14
+ nop; // scope 1 at $DIR/cycle.rs:+6:13: +6:14
+ nop; // scope 1 at $DIR/cycle.rs:+6:9: +6:14
_8 = _1; // scope 1 at $DIR/cycle.rs:+6:13: +6:14
_2 = move _8; // scope 1 at $DIR/cycle.rs:+6:9: +6:14
StorageDead(_8); // scope 1 at $DIR/cycle.rs:+6:13: +6:14
StorageLive(_9); // scope 1 at $DIR/cycle.rs:+7:13: +7:17
- _9 = _6; // scope 1 at $DIR/cycle.rs:+7:13: +7:17
- _1 = move _9; // scope 1 at $DIR/cycle.rs:+7:9: +7:17
+ nop; // scope 1 at $DIR/cycle.rs:+7:13: +7:17
+ nop; // scope 1 at $DIR/cycle.rs:+7:9: +7:17
_9 = _6; // scope 1 at $DIR/cycle.rs:+7:13: +7:17
_1 = move _9; // scope 1 at $DIR/cycle.rs:+7:9: +7:17
StorageDead(_9); // scope 1 at $DIR/cycle.rs:+7:16: +7:17
- _4 = const (); // scope 0 at $DIR/cycle.rs:+3:18: +8:6
+ nop; // scope 0 at $DIR/cycle.rs:+3:18: +8:6
Expand Down
Loading