Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 929ef38

Browse files
committed
Auto merge of rust-lang#140117 - dianqk:mir-nop, r=<try>
[experiment] eliminate dead statements while retaining debugging information I don't have a clear idea yet, but I'd like to take a look at perf first. r? ghost
2 parents 6bc57c6 + a456a65 commit 929ef38

File tree

60 files changed

+597
-398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+597
-398
lines changed

compiler/rustc_borrowck/src/dataflow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ impl<'tcx> rustc_mir_dataflow::Analysis<'tcx> for Borrows<'_, 'tcx> {
626626
| mir::StatementKind::Intrinsic(..)
627627
| mir::StatementKind::ConstEvalCounter
628628
| mir::StatementKind::BackwardIncompatibleDropHint { .. }
629-
| mir::StatementKind::Nop => {}
629+
| mir::StatementKind::Nop(_) => {}
630630
}
631631
}
632632

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<
770770
state,
771771
);
772772
}
773-
StatementKind::Nop
773+
StatementKind::Nop(_)
774774
| StatementKind::Retag { .. }
775775
| StatementKind::Deinit(..)
776776
| StatementKind::SetDiscriminant { .. } => {

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
8282
);
8383
}
8484
StatementKind::ConstEvalCounter
85-
| StatementKind::Nop
85+
| StatementKind::Nop(_)
8686
| StatementKind::Retag { .. }
8787
| StatementKind::Deinit(..)
8888
| StatementKind::BackwardIncompatibleDropHint { .. }

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
731731
| StatementKind::ConstEvalCounter
732732
| StatementKind::PlaceMention(..)
733733
| StatementKind::BackwardIncompatibleDropHint { .. }
734-
| StatementKind::Nop => {}
734+
| StatementKind::Nop(_) => {}
735735
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(..))
736736
| StatementKind::Deinit(..)
737737
| StatementKind::SetDiscriminant { .. } => {

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ fn codegen_stmt<'tcx>(
946946
| StatementKind::StorageDead(_)
947947
| StatementKind::Deinit(_)
948948
| StatementKind::ConstEvalCounter
949-
| StatementKind::Nop
949+
| StatementKind::Nop(_)
950950
| StatementKind::FakeRead(..)
951951
| StatementKind::Retag { .. }
952952
| StatementKind::PlaceMention(..)

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
591591
| StatementKind::Coverage(_)
592592
| StatementKind::ConstEvalCounter
593593
| StatementKind::BackwardIncompatibleDropHint { .. }
594-
| StatementKind::Nop => {}
594+
| StatementKind::Nop(_) => {}
595595
}
596596
}
597597
match &bb_data.terminator().kind {

compiler/rustc_codegen_gcc/src/debuginfo.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
2626
&mut self,
2727
_dbg_var: Self::DIVariable,
2828
_dbg_loc: Self::DILocation,
29-
_variable_alloca: Self::Value,
29+
_is_declared: bool,
30+
_val: Self::Value,
3031
_direct_offset: Size,
3132
_indirect_offsets: &[Size],
3233
_fragment: Option<Range<Size>>,

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
154154
&mut self,
155155
dbg_var: &'ll DIVariable,
156156
dbg_loc: &'ll DILocation,
157-
variable_alloca: Self::Value,
157+
is_declared: bool,
158+
val: Self::Value,
158159
direct_offset: Size,
159160
indirect_offsets: &[Size],
160161
fragment: Option<Range<Size>>,
@@ -183,17 +184,30 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
183184
addr_ops.push((fragment.end - fragment.start).bits() as u64);
184185
}
185186

186-
unsafe {
187-
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
188-
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
189-
DIB(self.cx()),
190-
variable_alloca,
191-
dbg_var,
192-
addr_ops.as_ptr(),
193-
addr_ops.len() as c_uint,
194-
dbg_loc,
195-
self.llbb(),
196-
);
187+
if is_declared {
188+
unsafe {
189+
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
190+
DIB(self.cx()),
191+
val,
192+
dbg_var,
193+
addr_ops.as_ptr(),
194+
addr_ops.len() as c_uint,
195+
dbg_loc,
196+
self.llbb(),
197+
);
198+
}
199+
} else {
200+
unsafe {
201+
llvm::LLVMRustDIBuilderInsertDbgValueAtEnd(
202+
DIB(self.cx()),
203+
val,
204+
dbg_var,
205+
addr_ops.as_ptr(),
206+
addr_ops.len() as c_uint,
207+
dbg_loc,
208+
self.llbb(),
209+
);
210+
}
197211
}
198212
}
199213

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,16 @@ unsafe extern "C" {
23012301
InsertAtEnd: &'a BasicBlock,
23022302
);
23032303

2304+
pub(crate) fn LLVMRustDIBuilderInsertDbgValueAtEnd<'a>(
2305+
Builder: &DIBuilder<'a>,
2306+
Val: &'a Value,
2307+
VarInfo: &'a DIVariable,
2308+
AddrOps: *const u64,
2309+
AddrOpsCount: c_uint,
2310+
DL: &'a DILocation,
2311+
InsertAtEnd: &'a BasicBlock,
2312+
);
2313+
23042314
pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
23052315
Builder: &DIBuilder<'a>,
23062316
Name: *const c_char,

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ fn calculate_debuginfo_offset<
205205
_ => {
206206
// Sanity check for `can_use_in_debuginfo`.
207207
assert!(!elem.can_use_in_debuginfo());
208-
bug!("unsupported var debuginfo projection `{:?}`", projection)
208+
// TODO: Temporarily disabled for testing purposes.
209+
// bug!("unsupported var debuginfo projection `{:?}`", projection)
209210
}
210211
}
211212
}
@@ -377,6 +378,52 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
377378
}
378379
}
379380

381+
pub(crate) fn debug_new_value_to_local(
382+
&self,
383+
bx: &mut Bx,
384+
local: mir::Local,
385+
base: PlaceValue<Bx::Value>,
386+
layout: TyAndLayout<'tcx>,
387+
projection: &[mir::PlaceElem<'tcx>],
388+
) {
389+
let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
390+
if !full_debug_info {
391+
return;
392+
}
393+
394+
let vars = match &self.per_local_var_debug_info {
395+
Some(per_local) => &per_local[local],
396+
None => return,
397+
};
398+
399+
for var in vars.iter().cloned() {
400+
self.debug_new_value_to_local_as_var(bx, base, layout, projection, var);
401+
}
402+
}
403+
404+
fn debug_new_value_to_local_as_var(
405+
&self,
406+
bx: &mut Bx,
407+
base: PlaceValue<Bx::Value>,
408+
layout: TyAndLayout<'tcx>,
409+
projection: &[mir::PlaceElem<'tcx>],
410+
var: PerLocalVarDebugInfo<'tcx, Bx::DIVariable>,
411+
) {
412+
let Some(dbg_var) = var.dbg_var else { return };
413+
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { return };
414+
let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
415+
calculate_debuginfo_offset(bx, projection, layout);
416+
bx.dbg_var_addr(
417+
dbg_var,
418+
dbg_loc,
419+
false,
420+
base.llval,
421+
direct_offset,
422+
&indirect_offsets,
423+
var.fragment,
424+
);
425+
}
426+
380427
fn debug_introduce_local_as_var(
381428
&self,
382429
bx: &mut Bx,
@@ -386,7 +433,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
386433
) {
387434
let Some(dbg_var) = var.dbg_var else { return };
388435
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { return };
389-
390436
let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
391437
calculate_debuginfo_offset(bx, var.projection, base.layout);
392438

@@ -421,6 +467,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
421467
bx.dbg_var_addr(
422468
dbg_var,
423469
dbg_loc,
470+
true,
424471
alloca.val.llval,
425472
Size::ZERO,
426473
&[Size::ZERO],
@@ -430,6 +477,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
430477
bx.dbg_var_addr(
431478
dbg_var,
432479
dbg_loc,
480+
true,
433481
base.val.llval,
434482
direct_offset,
435483
&indirect_offsets,
@@ -455,7 +503,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
455503
let base = FunctionCx::spill_operand_to_stack(operand, Some(name), bx);
456504
bx.clear_dbg_loc();
457505

458-
bx.dbg_var_addr(dbg_var, dbg_loc, base.val.llval, Size::ZERO, &[], fragment);
506+
bx.dbg_var_addr(dbg_var, dbg_loc, true, base.val.llval, Size::ZERO, &[], fragment);
459507
}
460508
}
461509
}

0 commit comments

Comments
 (0)