Skip to content

Commit 48dff89

Browse files
committed
fixup! codegen: Generate dbg_value for the ref statement
Add `debug_new_val_to_local` Inline `debug_new_value_to_local_as_var`
1 parent 1f51a5a commit 48dff89

File tree

5 files changed

+53
-63
lines changed

5 files changed

+53
-63
lines changed

compiler/rustc_codegen_gcc/src/debuginfo.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
2929
_variable_alloca: Self::Value,
3030
_direct_offset: Size,
3131
_indirect_offsets: &[Size],
32-
_fragment: Option<Range<Size>>,
32+
_fragment: &Option<Range<Size>>,
3333
) {
3434
// FIXME(tempdragon): Not sure if this is correct, probably wrong but still keep it here.
3535
#[cfg(feature = "master")]
@@ -38,15 +38,12 @@ impl<'a, 'gcc, 'tcx> DebugInfoBuilderMethods for Builder<'a, 'gcc, 'tcx> {
3838

3939
fn dbg_var_value(
4040
&mut self,
41-
dbg_var: Self::DIVariable,
42-
dbg_loc: Self::DILocation,
43-
value: Self::Value,
44-
direct_offset: Size,
45-
// NB: each offset implies a deref (i.e. they're steps in a pointer chain).
46-
indirect_offsets: &[Size],
47-
// Byte range in the `dbg_var` covered by this fragment,
48-
// if this is a fragment of a composite `DIVariable`.
49-
fragment: Option<Range<Size>>,
41+
_dbg_var: Self::DIVariable,
42+
_dbg_loc: Self::DILocation,
43+
_value: Self::Value,
44+
_direct_offset: Size,
45+
_indirect_offsets: &[Size],
46+
_fragment: &Option<Range<Size>>,
5047
) {
5148
}
5249

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
163163
variable_alloca: Self::Value,
164164
direct_offset: Size,
165165
indirect_offsets: &[Size],
166-
fragment: Option<Range<Size>>,
166+
fragment: &Option<Range<Size>>,
167167
) {
168168
use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst};
169169

@@ -209,7 +209,7 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
209209
value: Self::Value,
210210
direct_offset: Size,
211211
indirect_offsets: &[Size],
212-
fragment: Option<Range<Size>>,
212+
fragment: &Option<Range<Size>>,
213213
) {
214214
use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst, DW_OP_stack_value};
215215

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
253253
spill_slot
254254
}
255255

256-
pub(crate) fn debug_new_value_to_local(
256+
// Indicates that local is set to a new value. The `layout` and `projection` are used to
257+
// calculate the offset.
258+
pub(crate) fn debug_new_val_to_local(
257259
&self,
258260
bx: &mut Bx,
259261
local: mir::Local,
@@ -271,31 +273,32 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
271273
None => return,
272274
};
273275

274-
for var in vars.iter().cloned() {
275-
self.debug_new_value_to_local_as_var(bx, base, layout, projection, var);
276+
let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
277+
calculate_debuginfo_offset(bx, projection, layout);
278+
for var in vars.iter() {
279+
let Some(dbg_var) = var.dbg_var else {
280+
continue;
281+
};
282+
let Some(dbg_loc) = self.dbg_loc(var.source_info) else {
283+
continue;
284+
};
285+
bx.dbg_var_value(
286+
dbg_var,
287+
dbg_loc,
288+
base.llval,
289+
direct_offset,
290+
&indirect_offsets,
291+
&var.fragment,
292+
);
276293
}
277294
}
278295

279-
fn debug_new_value_to_local_as_var(
280-
&self,
281-
bx: &mut Bx,
282-
base: PlaceValue<Bx::Value>,
283-
layout: TyAndLayout<'tcx>,
284-
projection: &[mir::PlaceElem<'tcx>],
285-
var: PerLocalVarDebugInfo<'tcx, Bx::DIVariable>,
286-
) {
287-
let Some(dbg_var) = var.dbg_var else { return };
288-
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { return };
289-
let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
290-
calculate_debuginfo_offset(bx, projection, layout);
291-
bx.dbg_var_value(
292-
dbg_var,
293-
dbg_loc,
294-
base.llval,
295-
direct_offset,
296-
&indirect_offsets,
297-
var.fragment,
298-
);
296+
pub(crate) fn debug_poison_to_local(&self, bx: &mut Bx, local: mir::Local) {
297+
let ty = self.monomorphize(self.mir.local_decls[local].ty);
298+
let layout = bx.cx().layout_of(ty);
299+
let to_backend_ty = bx.cx().immediate_backend_type(layout);
300+
let place_ref = PlaceRef::new_sized(bx.cx().const_poison(to_backend_ty), layout);
301+
self.debug_new_val_to_local(bx, local, place_ref.val, layout, &[]);
299302
}
300303

301304
/// Apply debuginfo and/or name, after creating the `alloca` for a local,
@@ -469,7 +472,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
469472
alloca.val.llval,
470473
Size::ZERO,
471474
&[Size::ZERO],
472-
var.fragment,
475+
&var.fragment,
473476
);
474477
} else {
475478
bx.dbg_var_addr(
@@ -478,7 +481,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
478481
base.val.llval,
479482
direct_offset,
480483
&indirect_offsets,
481-
var.fragment,
484+
&var.fragment,
482485
);
483486
}
484487
}
@@ -500,7 +503,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
500503
let base = FunctionCx::spill_operand_to_stack(operand, Some(name), bx);
501504
bx.clear_dbg_loc();
502505

503-
bx.dbg_var_addr(dbg_var, dbg_loc, base.val.llval, Size::ZERO, &[], fragment);
506+
bx.dbg_var_addr(dbg_var, dbg_loc, base.val.llval, Size::ZERO, &[], &fragment);
504507
}
505508
}
506509
}

compiler/rustc_codegen_ssa/src/mir/statement.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,28 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
130130
// Only pointers can calculate addresses.
131131
bx.type_kind(bx.val_ty(place_ref.val.llval)) == TypeKind::Pointer
132132
});
133-
let assign_ref = match (local_ref, place.is_indirect_first_projection()) {
134-
(Some(local_ref), false) => {
135-
Some((local_ref.val, local_ref.layout, place.projection.as_slice()))
136-
}
137-
(Some(local_ref), true) => {
133+
if let Some(local_ref) = local_ref {
134+
let (base_layout, projection) = if place.is_indirect_first_projection() {
135+
// For `_n = &((*_1).0: i32);`, we are calculating the address of `_1.0`, so
136+
// we should drop the deref projection.
138137
let projected_ty = local_ref
139138
.layout
140139
.ty
141140
.builtin_deref(true)
142141
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", local_ref));
143142
let layout = bx.cx().layout_of(projected_ty);
144-
Some((local_ref.val, layout, &place.projection[1..]))
145-
}
146-
_ => None,
147-
};
148-
let (val, layout, projection) = assign_ref.unwrap_or_else(|| {
143+
(layout, &place.projection[1..])
144+
} else {
145+
(local_ref.layout, place.projection.as_slice())
146+
};
147+
self.debug_new_val_to_local(bx, *dest, local_ref.val, base_layout, projection);
148+
} else {
149149
// If the address cannot be computed, use poison to indicate that the value has been optimized out.
150-
let ty = self.monomorphize(self.mir.local_decls[*dest].ty);
151-
let layout = bx.cx().layout_of(ty);
152-
let to_backend_ty = bx.cx().immediate_backend_type(layout);
153-
let place_ref =
154-
PlaceRef::new_sized(bx.cx().const_poison(to_backend_ty), layout);
155-
(place_ref.val, layout, [].as_slice())
156-
});
157-
self.debug_new_value_to_local(bx, *dest, val, layout, projection);
150+
self.debug_poison_to_local(bx, *dest);
151+
}
158152
}
159153
StmtDebugInfo::InvalidAssign(local) => {
160-
let ty = self.monomorphize(self.mir.local_decls[*local].ty);
161-
let layout = bx.cx().layout_of(ty);
162-
let to_backend_ty = bx.cx().immediate_backend_type(layout);
163-
let place_ref = PlaceRef::new_sized(bx.cx().const_poison(to_backend_ty), layout);
164-
self.debug_new_value_to_local(bx, *local, place_ref.val, layout, &[]);
154+
self.debug_poison_to_local(bx, *local);
165155
}
166156
}
167157
}

compiler/rustc_codegen_ssa/src/traits/debuginfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
7777
indirect_offsets: &[Size],
7878
// Byte range in the `dbg_var` covered by this fragment,
7979
// if this is a fragment of a composite `DIVariable`.
80-
fragment: Option<Range<Size>>,
80+
fragment: &Option<Range<Size>>,
8181
);
8282
fn dbg_var_value(
8383
&mut self,
@@ -89,7 +89,7 @@ pub trait DebugInfoBuilderMethods: BackendTypes {
8989
indirect_offsets: &[Size],
9090
// Byte range in the `dbg_var` covered by this fragment,
9191
// if this is a fragment of a composite `DIVariable`.
92-
fragment: Option<Range<Size>>,
92+
fragment: &Option<Range<Size>>,
9393
);
9494
fn set_dbg_loc(&mut self, dbg_loc: Self::DILocation);
9595
fn clear_dbg_loc(&mut self);

0 commit comments

Comments
 (0)