Skip to content

Commit 9e540df

Browse files
committed
Auto merge of #107404 - cjgillot:const-debuginfo, r=oli-obk
Turn on ConstDebugInfo pass. Split from #103657 Moving those constant into debuginfo allows to shrink the number of locals and the actual size of the MIR body.
2 parents f12a7fa + b8c67d8 commit 9e540df

14 files changed

+85
-82
lines changed

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
494494
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { continue };
495495

496496
if let Ok(operand) = self.eval_mir_constant_to_operand(bx, &c) {
497+
self.set_debug_loc(bx, var.source_info);
497498
let base = Self::spill_operand_to_stack(
498499
&operand,
499500
Some(var.name.to_string()),

compiler/rustc_mir_transform/src/const_debuginfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct ConstDebugInfo;
1616

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

2222
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile-flags: -g -O
2+
3+
// Check that simple constant values are preserved in debuginfo across both MIR opts and LLVM opts
4+
5+
#![crate_type = "lib"]
6+
7+
#[no_mangle]
8+
pub fn check_it() {
9+
let a = 1;
10+
let b = 42;
11+
12+
foo(a + b);
13+
}
14+
15+
#[inline(never)]
16+
fn foo(x: i32) {
17+
std::process::exit(x);
18+
}
19+
20+
// CHECK-LABEL: @check_it
21+
// CHECK: call void @llvm.dbg.value(metadata i32 1, metadata ![[a_metadata:[0-9]+]], metadata !DIExpression())
22+
// CHECK: call void @llvm.dbg.value(metadata i32 42, metadata ![[b_metadata:[0-9]+]], metadata !DIExpression())
23+
24+
// CHECK: ![[a_metadata]] = !DILocalVariable(name: "a"
25+
// CHECK-SAME: line: 9
26+
27+
// CHECK: ![[b_metadata]] = !DILocalVariable(name: "b"
28+
// CHECK-SAME: line: 10

tests/debuginfo/auxiliary/macro-stepping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#[macro_export]
66
macro_rules! new_scope {
77
() => {
8-
let x = 1;
8+
let x = 1; opaque(x);
99
}
1010
}

tests/debuginfo/macro-stepping.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,28 @@ extern crate macro_stepping; // exports new_scope!()
7979
// lldb-check:[...]#inc-loc2[...]
8080
// lldb-command:next
8181
// lldb-command:frame select
82+
// lldb-check:[...]#inc-loc1[...]
83+
// lldb-command:next
84+
// lldb-command:frame select
85+
// lldb-check:[...]#inc-loc2[...]
86+
// lldb-command:next
87+
// lldb-command:frame select
8288
// lldb-check:[...]#inc-loc3[...]
8389

8490
macro_rules! foo {
8591
() => {
86-
let a = 1;
87-
let b = 2;
88-
let c = 3;
89-
}
92+
let a = 1; opaque(a);
93+
let b = 2; opaque(b);
94+
let c = 3; opaque(c);
95+
};
9096
}
9197

9298
macro_rules! foo2 {
9399
() => {
94100
foo!();
95-
let x = 1;
101+
let x = 1; opaque(x);
96102
foo!();
97-
}
103+
};
98104
}
99105

100106
fn main() {
@@ -118,4 +124,6 @@ fn main() {
118124

119125
fn zzz() {()}
120126

127+
fn opaque(_: u32) {}
128+
121129
include!("macro-stepping.inc");

tests/incremental/hashes/enum_constructors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ pub fn change_constructor_variant_c_like() {
334334
}
335335

336336
#[cfg(not(any(cfail1,cfail4)))]
337-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes")]
337+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,optimized_mir")]
338338
#[rustc_clean(cfg="cfail3")]
339-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes")]
339+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes,optimized_mir")]
340340
#[rustc_clean(cfg="cfail6")]
341341
pub fn change_constructor_variant_c_like() {
342342
let _x = Clike::C;

tests/incremental/hashes/let_expressions.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn change_mutability_of_slot() {
9191
}
9292

9393
#[cfg(not(any(cfail1,cfail4)))]
94-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck,optimized_mir")]
94+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck")]
9595
#[rustc_clean(cfg="cfail3")]
9696
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes,typeck,optimized_mir")]
9797
#[rustc_clean(cfg="cfail6")]
@@ -176,7 +176,7 @@ pub fn change_mutability_of_binding_in_pattern() {
176176
}
177177

178178
#[cfg(not(any(cfail1,cfail4)))]
179-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck,optimized_mir")]
179+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck")]
180180
#[rustc_clean(cfg="cfail3")]
181181
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes,typeck,optimized_mir")]
182182
#[rustc_clean(cfg="cfail6")]
@@ -193,9 +193,9 @@ pub fn add_initializer() {
193193
}
194194

195195
#[cfg(not(any(cfail1,cfail4)))]
196-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck")]
196+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes,typeck,optimized_mir")]
197197
#[rustc_clean(cfg="cfail3")]
198-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes,typeck")]
198+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes,typeck,optimized_mir")]
199199
#[rustc_clean(cfg="cfail6")]
200200
pub fn add_initializer() {
201201
let _x: i16 = 3i16;
@@ -210,9 +210,9 @@ pub fn change_initializer() {
210210
}
211211

212212
#[cfg(not(any(cfail1,cfail4)))]
213-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes")]
213+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, optimized_mir")]
214214
#[rustc_clean(cfg="cfail3")]
215-
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes")]
215+
#[rustc_clean(cfg="cfail5", except="hir_owner_nodes, optimized_mir")]
216216
#[rustc_clean(cfg="cfail6")]
217217
pub fn change_initializer() {
218218
let _x = 5u16;

tests/mir-opt/const_prop/optimizes_into_variable.main.PreCodegen.after.32bit.mir

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
fn main() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:+0:11: +0:11
5-
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
6-
let mut _3: u32; // in scope 0 at $DIR/optimizes_into_variable.rs:+3:13: +3:36
75
scope 1 {
8-
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
9-
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
6+
debug x => const 4_i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
107
scope 2 {
11-
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
8+
debug y => const 3_i32; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
129
scope 3 {
13-
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
10+
debug z => const 42_u32; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
1411
}
1512
}
1613
}
1714

1815
bb0: {
19-
StorageLive(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
20-
StorageLive(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
21-
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
22-
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
2316
return; // scope 0 at $DIR/optimizes_into_variable.rs:+4:2: +4:2
2417
}
2518
}

tests/mir-opt/const_prop/optimizes_into_variable.main.PreCodegen.after.64bit.mir

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
fn main() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:+0:11: +0:11
5-
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
6-
let mut _3: u32; // in scope 0 at $DIR/optimizes_into_variable.rs:+3:13: +3:36
75
scope 1 {
8-
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
9-
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
6+
debug x => const 4_i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
107
scope 2 {
11-
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
8+
debug y => const 3_i32; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
129
scope 3 {
13-
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
10+
debug z => const 42_u32; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
1411
}
1512
}
1613
}
1714

1815
bb0: {
19-
StorageLive(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
20-
StorageLive(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
21-
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
22-
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
2316
return; // scope 0 at $DIR/optimizes_into_variable.rs:+4:2: +4:2
2417
}
2518
}

tests/mir-opt/const_prop/optimizes_into_variable.main.SimplifyLocals-final.after.32bit.mir

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
fn main() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:+0:11: +0:11
5-
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
6-
let mut _3: u32; // in scope 0 at $DIR/optimizes_into_variable.rs:+3:13: +3:36
75
scope 1 {
8-
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
9-
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
6+
debug x => const 4_i32; // in scope 1 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
107
scope 2 {
11-
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
8+
debug y => const 3_i32; // in scope 2 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
129
scope 3 {
13-
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
10+
debug z => const 42_u32; // in scope 3 at $DIR/optimizes_into_variable.rs:+3:9: +3:10
1411
}
1512
}
1613
}
1714

1815
bb0: {
19-
StorageLive(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+1:9: +1:10
20-
StorageLive(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+2:9: +2:10
21-
StorageDead(_2); // scope 1 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
22-
StorageDead(_1); // scope 0 at $DIR/optimizes_into_variable.rs:+4:1: +4:2
2316
return; // scope 0 at $DIR/optimizes_into_variable.rs:+4:2: +4:2
2417
}
2518
}

0 commit comments

Comments
 (0)