Skip to content

Commit bf5df2d

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 d6c1e45 + ed418b0 commit bf5df2d

File tree

65 files changed

+653
-441
lines changed

Some content is hidden

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

65 files changed

+653
-441
lines changed

compiler/rustc_borrowck/src/dataflow.rs

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-1
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

+26-12
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

+10
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

+51-3
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
}

compiler/rustc_codegen_ssa/src/mir/statement.rs

+66-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use rustc_middle::mir::{self, NonDivergingIntrinsic};
2-
use rustc_middle::span_bug;
2+
use rustc_middle::{bug, span_bug};
33
use tracing::instrument;
44

55
use super::{FunctionCx, LocalRef};
6+
use crate::mir::operand::OperandValue;
7+
use crate::mir::place::PlaceRef;
68
use crate::traits::*;
79

810
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
@@ -92,8 +94,69 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9294
| mir::StatementKind::AscribeUserType(..)
9395
| mir::StatementKind::ConstEvalCounter
9496
| mir::StatementKind::PlaceMention(..)
95-
| mir::StatementKind::BackwardIncompatibleDropHint { .. }
96-
| mir::StatementKind::Nop => {}
97+
| mir::StatementKind::BackwardIncompatibleDropHint { .. } => {}
98+
mir::StatementKind::Nop(ref stmt) => {
99+
if let Some(box stmt_kind) = stmt {
100+
match stmt_kind {
101+
mir::StatementKind::Assign(box (dest, rvalue)) => {
102+
if let Some(index) = dest.as_local()
103+
&& let mir::Rvalue::Ref(_, _, place) = *rvalue
104+
{
105+
let place_ref = match self.locals[place.local] {
106+
LocalRef::Place(place_ref)
107+
| LocalRef::UnsizedPlace(place_ref) => Some(place_ref),
108+
LocalRef::Operand(operand_ref) => match operand_ref.val {
109+
OperandValue::Ref(_place_value) => {
110+
todo!("OperandValue::Ref")
111+
}
112+
OperandValue::Immediate(v) => {
113+
Some(PlaceRef::new_sized(v, operand_ref.layout))
114+
}
115+
OperandValue::Pair(_, _) => None,
116+
OperandValue::ZeroSized => None,
117+
},
118+
LocalRef::PendingOperand => None,
119+
};
120+
let (val, layout, projection) =
121+
match (place_ref, place.is_indirect_first_projection()) {
122+
(Some(place_ref), false) => (
123+
place_ref.val,
124+
place_ref.layout,
125+
place.projection.as_slice(),
126+
),
127+
(Some(place_ref), true) => {
128+
let projected_ty = place_ref
129+
.layout
130+
.ty
131+
.builtin_deref(true)
132+
.unwrap_or_else(|| {
133+
bug!("deref of non-pointer {:?}", place_ref)
134+
});
135+
let layout = bx.cx().layout_of(projected_ty);
136+
(place_ref.val, layout, &place.projection[1..])
137+
}
138+
_ => {
139+
let ty =
140+
self.monomorphize(self.mir.local_decls[index].ty);
141+
let layout = bx.cx().layout_of(ty);
142+
let to_backend_ty =
143+
bx.cx().immediate_backend_type(layout);
144+
let place_ref = PlaceRef::new_sized(
145+
bx.cx().const_poison(to_backend_ty),
146+
layout,
147+
);
148+
(place_ref.val, layout, [].as_slice())
149+
}
150+
};
151+
self.debug_new_value_to_local(bx, index, val, layout, projection);
152+
} else {
153+
todo!("add debugging information")
154+
}
155+
}
156+
_ => {}
157+
}
158+
}
159+
}
97160
}
98161
}
99162
}

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
7171
&mut self,
7272
dbg_var: Self::DIVariable,
7373
dbg_loc: Self::DILocation,
74-
variable_alloca: Self::Value,
74+
is_declared: bool,
75+
val: Self::Value,
7576
direct_offset: Size,
7677
// NB: each offset implies a deref (i.e. they're steps in a pointer chain).
7778
indirect_offsets: &[Size],

compiler/rustc_const_eval/src/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
772772
| StatementKind::Intrinsic(..)
773773
| StatementKind::ConstEvalCounter
774774
| StatementKind::BackwardIncompatibleDropHint { .. }
775-
| StatementKind::Nop => {}
775+
| StatementKind::Nop(_) => {}
776776
}
777777
}
778778

0 commit comments

Comments
 (0)