Skip to content

Commit b8965fd

Browse files
Auto merge of #142890 - kornelski:unused-var-debug, r=<try>
MIR inliner maintains unused var_debug_info Only `full` debuginfo level promises variable-level debug information, but the MIR inline pass needlessly preserved the local variable debug info for the `limited` level too.
2 parents 58d5e11 + 8c3a8f8 commit b8965fd

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1515
use rustc_middle::mir::visit::*;
1616
use rustc_middle::mir::*;
1717
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
18-
use rustc_session::config::{DebugInfo, OptLevel};
18+
use rustc_session::config::OptLevel;
1919
use rustc_span::source_map::Spanned;
2020
use tracing::{debug, instrument, trace, trace_span};
2121

@@ -982,16 +982,7 @@ fn inline_call<'tcx, I: Inliner<'tcx>>(
982982
// Insert all of the (mapped) parts of the callee body into the caller.
983983
caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
984984
caller_body.source_scopes.append(&mut callee_body.source_scopes);
985-
if tcx
986-
.sess
987-
.opts
988-
.unstable_opts
989-
.inline_mir_preserve_debug
990-
.unwrap_or(tcx.sess.opts.debuginfo != DebugInfo::None)
991-
{
992-
// Note that we need to preserve these in the standard library so that
993-
// people working on rust can build with or without debuginfo while
994-
// still getting consistent results from the mir-opt tests.
985+
if tcx.sess.keep_var_debug_info() {
995986
caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
996987
}
997988
caller_body.basic_blocks_mut().append(callee_body.basic_blocks_mut());

compiler/rustc_session/src/session.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,18 @@ impl Session {
765765
.unwrap_or(self.target.default_dwarf_version)
766766
}
767767

768+
/// Whether to maintain `Body.var_debug_info` that keeps track where variables were declared
769+
pub fn keep_var_debug_info(&self) -> bool {
770+
// -Zinline-mir-preserve-debug is enabled when building the standard library, so that
771+
// people working on rust can build with or without debuginfo while
772+
// still getting consistent results from the mir-opt tests.
773+
self.opts
774+
.unstable_opts
775+
.inline_mir_preserve_debug
776+
// only "full" promises any variable-level information
777+
.unwrap_or(self.opts.debuginfo == DebugInfo::Full)
778+
}
779+
768780
pub fn stack_protector(&self) -> StackProtector {
769781
if self.target.options.supports_stack_protector {
770782
self.opts.unstable_opts.stack_protector
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//@ test-mir-pass: Inline
2+
//@ revisions: PRESERVE FULL NONE LIMITED
3+
//@ [PRESERVE]compile-flags: -O -C debuginfo=0 -Zinline-mir-preserve-debug
4+
//@ [FULL]compile-flags: -O -C debuginfo=2
5+
//@ [NONE]compile-flags: -O -C debuginfo=0
6+
//@ [LIMITED]compile-flags: -O -C debuginfo=1
7+
8+
#[inline(always)]
9+
fn inline_fn1(arg1: i32) -> i32 {
10+
let local1 = arg1 + 1;
11+
let _local2 = 10;
12+
arg1 + local1
13+
}
14+
15+
#[inline(always)]
16+
fn inline_fn2(binding: i32) -> i32 {
17+
{
18+
let binding = inline_fn1(binding);
19+
binding
20+
}
21+
}
22+
23+
#[inline(never)]
24+
fn test() -> i32 {
25+
// CHECK-LABEL: fn test
26+
inline_fn2(1)
27+
// CHECK-LABEL: (inlined inline_fn2)
28+
29+
// PRESERVE: debug binding =>
30+
// FULL: debug binding =>
31+
// NONE-NOT: debug binding =>
32+
// LIMITED-NOT: debug binding =>
33+
34+
// CHECK-LABEL: (inlined inline_fn1)
35+
36+
// PRESERVE: debug arg1 =>
37+
// FULL: debug arg1 =>
38+
// NONE-NOT: debug arg1 =>
39+
// LIMITED-NOT: debug arg1 =>
40+
41+
// PRESERVE: debug local1 =>
42+
// FULL: debug local1 =>
43+
// NONE-NOT: debug local1 =>
44+
// LIMITED-NOT: debug local1 =>
45+
46+
// PRESERVE: debug _local2 =>
47+
// FULL: debug _local2 =>
48+
// NONE-NOT: debug _local2 =>
49+
// LIMITED-NOT: debug _local2 =>
50+
}

0 commit comments

Comments
 (0)