Skip to main content

rustc_codegen_ssa/mir/
debuginfo.rs

1use std::collections::hash_map::Entry;
2use std::marker::PhantomData;
3use std::ops::Range;
4
5use rustc_abi::{BackendRepr, FieldIdx, FieldsShape, Size, VariantIdx};
6use rustc_data_structures::fx::FxHashMap;
7use rustc_index::IndexVec;
8use rustc_index::bit_set::DenseBitSet;
9use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
10use rustc_middle::ty::Ty;
11use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12use rustc_middle::{mir, ty};
13use rustc_session::config::DebugInfo;
14use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, bug, hygiene, sym};
15
16use super::operand::{OperandRef, OperandValue};
17use super::place::{PlaceRef, PlaceValue};
18use super::{FunctionCx, LocalRef, PerLocalVarDebugInfoIndexVec};
19use crate::traits::*;
20
21pub struct FunctionDebugContext<S, L> {
22    /// Maps from source code to the corresponding debug info scope.
23    pub scopes: IndexVec<mir::SourceScope, DebugScope<S, L>>,
24}
25
26#[derive(#[automatically_derived]impl ::core::marker::Copy for VariableKind { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for VariableKind {
}
#[automatically_derived]impl ::core::clone::Clone for VariableKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<usize>;
        *self
    }
}Clone)]
27pub enum VariableKind {
28    ArgumentVariable(usize /*index*/),
29    LocalVariable,
30}
31
32/// Like `mir::VarDebugInfo`, but within a `mir::Local`.
33#[derive(#[automatically_derived]impl<'tcx, D: ::core::clone::Clone>
    ::core::clone::Clone for PerLocalVarDebugInfo<'tcx, D> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        PerLocalVarDebugInfo {
            name: ::core::clone::Clone::clone(&self.name),
            source_info: ::core::clone::Clone::clone(&self.source_info),
            dbg_var: ::core::clone::Clone::clone(&self.dbg_var),
            fragment: ::core::clone::Clone::clone(&self.fragment),
            projection: ::core::clone::Clone::clone(&self.projection),
        }
    }
}Clone)]
34pub struct PerLocalVarDebugInfo<'tcx, D> {
35    pub name: Symbol,
36    pub source_info: mir::SourceInfo,
37
38    /// `DIVariable` returned by `create_dbg_var`.
39    pub dbg_var: Option<D>,
40
41    /// Byte range in the `dbg_var` covered by this fragment,
42    /// if this is a fragment of a composite `VarDebugInfo`.
43    pub fragment: Option<Range<Size>>,
44
45    /// `.place.projection` from `mir::VarDebugInfo`.
46    pub projection: &'tcx ty::List<mir::PlaceElem<'tcx>>,
47}
48
49/// Information needed to emit a constant.
50pub struct ConstDebugInfo<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
51    pub name: String,
52    pub source_info: mir::SourceInfo,
53    pub operand: OperandRef<'tcx, Bx::Value>,
54    pub dbg_var: Bx::DIVariable,
55    pub dbg_loc: Bx::DILocation,
56    pub fragment: Option<Range<Size>>,
57    pub _phantom: PhantomData<&'a ()>,
58}
59
60#[derive(#[automatically_derived]impl<S: ::core::clone::Clone, L: ::core::clone::Clone>
    ::core::clone::Clone for DebugScope<S, L> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        DebugScope {
            dbg_scope: ::core::clone::Clone::clone(&self.dbg_scope),
            inlined_at: ::core::clone::Clone::clone(&self.inlined_at),
            file_start_pos: ::core::clone::Clone::clone(&self.file_start_pos),
            file_end_pos: ::core::clone::Clone::clone(&self.file_end_pos),
        }
    }
}Clone, #[automatically_derived]impl<S: ::core::marker::Copy, L: ::core::marker::Copy>
    ::core::marker::Copy for DebugScope<S, L> {
}Copy, #[automatically_derived]impl<S: ::core::fmt::Debug, L: ::core::fmt::Debug>
    ::core::fmt::Debug for DebugScope<S, L> {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "DebugScope dbg_scope inlined_at file_start_pos file_end_pos",
            &self.dbg_scope, &self.inlined_at, &self.file_start_pos,
            &&self.file_end_pos)
    }
}Debug)]
61pub struct DebugScope<S, L> {
62    pub dbg_scope: S,
63
64    /// Call site location, if this scope was inlined from another function.
65    pub inlined_at: Option<L>,
66
67    // Start and end offsets of the file to which this DIScope belongs.
68    // These are used to quickly determine whether some span refers to the same file.
69    pub file_start_pos: BytePos,
70    pub file_end_pos: BytePos,
71}
72
73impl<'tcx, S: Copy, L: Copy> DebugScope<S, L> {
74    /// DILocations inherit source file name from the parent DIScope. Due to macro expansions
75    /// it may so happen that the current span belongs to a different file than the DIScope
76    /// corresponding to span's containing source scope. If so, we need to create a DIScope
77    /// "extension" into that file.
78    pub fn adjust_dbg_scope_for_span<
79        'a,
80        Bx: BuilderMethods<'a, 'tcx, DIScope = S, DILocation = L>,
81    >(
82        &self,
83        bx: &mut Bx,
84        span: Span,
85    ) -> S {
86        let pos = span.lo();
87        if pos < self.file_start_pos || pos >= self.file_end_pos {
88            let sm = bx.sess().source_map();
89            bx.extend_scope_to_file(self.dbg_scope, &sm.lookup_char_pos(pos).file)
90        } else {
91            self.dbg_scope
92        }
93    }
94}
95
96trait DebugInfoOffsetLocation<'tcx, Bx> {
97    fn deref(&self, bx: &mut Bx) -> Self;
98    fn layout(&self) -> TyAndLayout<'tcx>;
99    fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self;
100    fn project_constant_index(&self, bx: &mut Bx, offset: u64) -> Self;
101    fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self;
102}
103
104impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
105    for PlaceRef<'tcx, Bx::Value>
106{
107    fn deref(&self, bx: &mut Bx) -> Self {
108        bx.load_operand(*self).deref(bx.cx())
109    }
110
111    fn layout(&self) -> TyAndLayout<'tcx> {
112        self.layout
113    }
114
115    fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
116        PlaceRef::project_field(*self, bx, field.index())
117    }
118
119    fn project_constant_index(&self, bx: &mut Bx, offset: u64) -> Self {
120        let lloffset = bx.cx().const_usize(offset);
121        self.project_index(bx, lloffset)
122    }
123
124    fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
125        self.project_downcast(bx, variant)
126    }
127}
128
129impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> DebugInfoOffsetLocation<'tcx, Bx>
130    for TyAndLayout<'tcx>
131{
132    fn deref(&self, bx: &mut Bx) -> Self {
133        bx.cx().layout_of(
134            self.ty.builtin_deref(true).unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
    format_args!("cannot deref `{0}`", self.ty), Location::caller())bug!("cannot deref `{}`", self.ty)),
135        )
136    }
137
138    fn layout(&self) -> TyAndLayout<'tcx> {
139        *self
140    }
141
142    fn project_field(&self, bx: &mut Bx, field: FieldIdx) -> Self {
143        self.field(bx.cx(), field.index())
144    }
145
146    fn project_constant_index(&self, bx: &mut Bx, index: u64) -> Self {
147        self.field(bx.cx(), index as usize)
148    }
149
150    fn downcast(&self, bx: &mut Bx, variant: VariantIdx) -> Self {
151        self.for_variant(bx.cx(), variant)
152    }
153}
154
155struct DebugInfoOffset<T> {
156    /// Offset from the `base` used to calculate the debuginfo offset.
157    direct_offset: Size,
158    /// Each offset in this vector indicates one level of indirection from the base or previous
159    /// indirect offset plus a dereference.
160    indirect_offsets: Vec<Size>,
161    /// The final location debuginfo should point to.
162    result: T,
163}
164
165fn calculate_debuginfo_offset<
166    'a,
167    'tcx,
168    Bx: BuilderMethods<'a, 'tcx>,
169    L: DebugInfoOffsetLocation<'tcx, Bx>,
170>(
171    bx: &mut Bx,
172    projection: &[mir::PlaceElem<'tcx>],
173    base: L,
174) -> DebugInfoOffset<L> {
175    let mut direct_offset = Size::ZERO;
176    // FIXME(eddyb) use smallvec here.
177    let mut indirect_offsets = ::alloc::vec::Vec::new()vec![];
178    let mut place = base;
179
180    for elem in projection {
181        match *elem {
182            mir::ProjectionElem::Deref => {
183                indirect_offsets.push(Size::ZERO);
184                place = place.deref(bx);
185            }
186            mir::ProjectionElem::Field(field, _) => {
187                let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
188                *offset += place.layout().fields.offset(field.index());
189                place = place.project_field(bx, field);
190            }
191            mir::ProjectionElem::Downcast(_, variant) => {
192                place = place.downcast(bx, variant);
193            }
194            mir::ProjectionElem::ConstantIndex {
195                offset: index,
196                min_length: _,
197                from_end: false,
198            } => {
199                let offset = indirect_offsets.last_mut().unwrap_or(&mut direct_offset);
200                let FieldsShape::Array { stride, count: _ } = place.layout().fields else {
201                    ::rustc_span::macros::bug_impl(None,
    format_args!("ConstantIndex on non-array type {0:?}", place.layout()),
    Location::caller())bug!("ConstantIndex on non-array type {:?}", place.layout())
202                };
203                *offset += stride * index;
204                place = place.project_constant_index(bx, index);
205            }
206            _ => {
207                // Sanity check for `can_use_in_debuginfo`.
208                if !!elem.can_use_in_debuginfo() {
    ::core::panicking::panic("assertion failed: !elem.can_use_in_debuginfo()")
};assert!(!elem.can_use_in_debuginfo());
209                ::rustc_span::macros::bug_impl(None,
    format_args!("unsupported var debuginfo projection `{0:?}`", projection),
    Location::caller())bug!("unsupported var debuginfo projection `{:?}`", projection)
210            }
211        }
212    }
213
214    DebugInfoOffset { direct_offset, indirect_offsets, result: place }
215}
216
217impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
218    pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
219        bx.set_span(source_info.span);
220        if let Some(dbg_loc) = self.dbg_loc(bx, source_info) {
221            bx.set_dbg_loc(dbg_loc);
222        }
223    }
224
225    fn dbg_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) -> Option<Bx::DILocation> {
226        let (dbg_scope, inlined_at, span) = self.adjusted_span_and_dbg_scope(bx, source_info)?;
227        Some(bx.dbg_loc(dbg_scope, inlined_at, span))
228    }
229
230    fn adjusted_span_and_dbg_scope(
231        &self,
232        bx: &mut Bx,
233        source_info: mir::SourceInfo,
234    ) -> Option<(Bx::DIScope, Option<Bx::DILocation>, Span)> {
235        let scope = &self.debug_context.as_ref()?.scopes[source_info.scope];
236        let span = hygiene::walk_chain_collapsed(source_info.span, self.mir.span);
237        Some((scope.adjust_dbg_scope_for_span(bx, span), scope.inlined_at, span))
238    }
239
240    fn spill_operand_to_stack(
241        operand: OperandRef<'tcx, Bx::Value>,
242        name: Option<String>,
243        bx: &mut Bx,
244    ) -> PlaceRef<'tcx, Bx::Value> {
245        // "Spill" the value onto the stack, for debuginfo,
246        // without forcing non-debuginfo uses of the local
247        // to also load from the stack every single time.
248        // FIXME(#68817) use `llvm.dbg.value` instead,
249        // at least for the cases which LLVM handles correctly.
250        let spill_slot = PlaceRef::alloca(bx, operand.layout);
251        if let Some(name) = name {
252            bx.set_var_name(spill_slot.val.llval, &(name + ".dbg.spill"));
253        }
254        operand.val.store(bx, spill_slot);
255        spill_slot
256    }
257
258    // Indicates that local is set to a new value. The `layout` and `projection` are used to
259    // calculate the offset.
260    fn debug_new_val_to_local(
261        &self,
262        bx: &mut Bx,
263        local: mir::Local,
264        base: PlaceRef<'tcx, Bx::Value>,
265        projection: &[mir::PlaceElem<'tcx>],
266    ) {
267        let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
268        if !full_debug_info {
269            return;
270        }
271
272        let vars = match &self.per_local_var_debug_info {
273            Some(per_local) => &per_local[local],
274            None => return,
275        };
276
277        let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
278            calculate_debuginfo_offset(bx, projection, base.layout);
279        for var in vars.iter() {
280            let Some(dbg_var) = var.dbg_var else {
281                continue;
282            };
283            let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else {
284                continue;
285            };
286            bx.dbg_var_value(
287                dbg_var,
288                dbg_loc,
289                base.val.llval,
290                direct_offset,
291                &indirect_offsets,
292                &var.fragment,
293            );
294        }
295    }
296
297    fn debug_poison_to_local(&self, bx: &mut Bx, local: mir::Local) {
298        let ty = self.monomorphize(self.mir.local_decls[local].ty);
299        let layout = bx.cx().layout_of(ty);
300        let place_ref = PlaceRef::new_sized(bx.cx().const_poison(bx.cx().type_ptr()), layout);
301        self.debug_new_val_to_local(bx, local, place_ref, &[]);
302    }
303
304    /// Apply debuginfo and/or name, after creating the `alloca` for a local,
305    /// or initializing the local with an operand (whichever applies).
306    pub(crate) fn debug_introduce_local(&self, bx: &mut Bx, local: mir::Local) {
307        let full_debug_info = bx.sess().opts.debuginfo == DebugInfo::Full;
308
309        let vars = match &self.per_local_var_debug_info {
310            Some(per_local) => &per_local[local],
311            None => return,
312        };
313        let whole_local_var = vars.iter().find(|var| var.projection.is_empty()).cloned();
314        let has_proj = || vars.iter().any(|var| !var.projection.is_empty());
315
316        let fallback_var = if self.mir.local_kind(local) == mir::LocalKind::Arg {
317            let arg_index = local.index() - 1;
318
319            // Add debuginfo even to unnamed arguments.
320            // FIXME(eddyb) is this really needed?
321            if arg_index == 0 && has_proj() {
322                // Hide closure environments from debuginfo.
323                // FIXME(eddyb) shouldn't `ArgumentVariable` indices
324                // be offset to account for the hidden environment?
325                None
326            } else if whole_local_var.is_some() {
327                // No need to make up anything, there is a `mir::VarDebugInfo`
328                // covering the whole local.
329                // FIXME(eddyb) take `whole_local_var.source_info.scope` into
330                // account, just in case it doesn't use `ArgumentVariable`
331                // (after #67586 gets fixed).
332                None
333            } else {
334                let name = sym::empty;
335                let decl = &self.mir.local_decls[local];
336                let dbg_var = if full_debug_info {
337                    self.adjusted_span_and_dbg_scope(bx, decl.source_info).map(
338                        |(dbg_scope, _, span)| {
339                            // FIXME(eddyb) is this `+ 1` needed at all?
340                            let kind = VariableKind::ArgumentVariable(arg_index + 1);
341
342                            let arg_ty = self.monomorphize(decl.ty);
343
344                            bx.create_dbg_var(name, arg_ty, dbg_scope, kind, span)
345                        },
346                    )
347                } else {
348                    None
349                };
350
351                Some(PerLocalVarDebugInfo {
352                    name,
353                    source_info: decl.source_info,
354                    dbg_var,
355                    fragment: None,
356                    projection: ty::List::empty(),
357                })
358            }
359        } else {
360            None
361        };
362
363        let local_ref = &self.locals[local];
364
365        let name = if bx.sess().fewer_names() {
366            None
367        } else {
368            Some(match whole_local_var.or_else(|| fallback_var.clone()) {
369                Some(var) if var.name != sym::empty => var.name.to_string(),
370                _ => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", local))
    })format!("{local:?}"),
371            })
372        };
373
374        if let Some(name) = &name {
375            match local_ref {
376                LocalRef::Place(place) | LocalRef::UnsizedPlace(place) => {
377                    bx.set_var_name(place.val.llval, name);
378                }
379                LocalRef::Operand(operand) => match operand.val {
380                    OperandValue::Ref(PlaceValue { llval: x, .. }) | OperandValue::Immediate(x) => {
381                        bx.set_var_name(x, name);
382                    }
383                    OperandValue::Pair(a, b) => {
384                        // FIXME(eddyb) these are scalar components,
385                        // maybe extract the high-level fields?
386                        bx.set_var_name(a, &(name.clone() + ".0"));
387                        bx.set_var_name(b, &(name.clone() + ".1"));
388                    }
389                    OperandValue::ZeroSized => {
390                        // These never have a value to talk about
391                    }
392                },
393                LocalRef::PendingOperand => {}
394            }
395        }
396
397        if !full_debug_info || vars.is_empty() && fallback_var.is_none() {
398            return;
399        }
400
401        let base = match local_ref {
402            LocalRef::PendingOperand => return,
403
404            LocalRef::Operand(operand) => {
405                // Don't spill operands onto the stack in naked functions.
406                // See: https://github.com/rust-lang/rust/issues/42779
407                let attrs = bx.tcx().codegen_instance_attrs(self.instance.def);
408                if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
409                    return;
410                }
411
412                // Don't spill `<vscale x N x i1>` for `N != 16`:
413                //
414                // SVE predicates are only one bit for each byte in an SVE vector (which makes
415                // sense, the predicate only needs to keep track of whether a lane is
416                // enabled/disabled). i.e. a `<vscale x 16 x i8>` vector has a `<vscale x 16 x i1>`
417                // predicate type. `<vscale x 16 x i1>` corresponds to two bytes of storage,
418                // multiplied by the `vscale`, with one bit for each of the sixteen lanes.
419                //
420                // For a vector with fewer elements, such as `svint32_t`/`<vscale x 4 x i32>`,
421                // while only a `<vscale x 4 x i1>` predicate type would be strictly necessary,
422                // relevant intrinsics still take a `svbool_t`/`<vscale x 16 x i1>` - this is
423                // because a `<vscale x 4 x i1>` is only half of a byte (for `vscale=1`), and with
424                // memory being byte-addressable, it's unclear how to store that.
425                //
426                // Due to this, LLVM ultimately decided not to support stores of `<vscale x N x i1>`
427                // for `N != 16`. As for `vscale=1` and `N` fewer than sixteen, partial bytes would
428                // need to be stored (except for `N=8`, but that also isn't supported). `N` can
429                // never be greater than sixteen as that ends up larger than the 128-bit increment
430                // size.
431                //
432                // Internally, with an intrinsic operating on a `svint32_t`/`<vscale x 4 x i32>`
433                // (for example), the intrinsic takes the `svbool_t`/`<vscale x 16 x i1>` predicate
434                // and casts it to a `svbool4_t`/`<vscale x 4 x i1>`. Therefore, it's important that
435                // the `<vscale x 4 x i1>` never spills because that'll cause errors during
436                // instruction selection. Spilling to the stack to create debuginfo for these
437                // intermediate values must be avoided and doing so won't affect the
438                // debugging experience anyway.
439                if operand.layout.ty.is_scalable_vector()
440                    && bx.sess().target.arch == rustc_target::spec::Arch::AArch64
441                {
442                    let (count, element_ty, _) =
443                        operand.layout.ty.scalable_vector_parts(bx.tcx()).unwrap();
444                    // i.e. `<vscale x N x i1>` when `N != 16`
445                    if element_ty.is_bool() && count != 16 {
446                        return;
447                    }
448                }
449
450                Self::spill_operand_to_stack(*operand, name, bx)
451            }
452
453            LocalRef::Place(place) => *place,
454
455            // FIXME(eddyb) add debuginfo for unsized places too.
456            LocalRef::UnsizedPlace(_) => return,
457        };
458
459        let vars = vars.iter().cloned().chain(fallback_var);
460
461        for var in vars {
462            self.debug_introduce_local_as_var(bx, local, base, var);
463        }
464    }
465
466    fn debug_introduce_local_as_var(
467        &self,
468        bx: &mut Bx,
469        local: mir::Local,
470        base: PlaceRef<'tcx, Bx::Value>,
471        var: PerLocalVarDebugInfo<'tcx, Bx::DIVariable>,
472    ) {
473        let Some(dbg_var) = var.dbg_var else { return };
474        let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else { return };
475
476        let DebugInfoOffset { direct_offset, indirect_offsets, result: _ } =
477            calculate_debuginfo_offset(bx, var.projection, base.layout);
478
479        // When targeting MSVC, create extra allocas for arguments instead of pointing multiple
480        // dbg_var_addr() calls into the same alloca with offsets. MSVC uses CodeView records
481        // not DWARF and LLVM doesn't support translating the resulting
482        // [DW_OP_deref, DW_OP_plus_uconst, offset, DW_OP_deref] debug info to CodeView.
483        // Creating extra allocas on the stack makes the resulting debug info simple enough
484        // that LLVM can generate correct CodeView records and thus the values appear in the
485        // debugger. (#83709)
486        let should_create_individual_allocas = bx.cx().sess().target.is_like_msvc
487            && self.mir.local_kind(local) == mir::LocalKind::Arg
488            // LLVM can handle simple things but anything more complex than just a direct
489            // offset or one indirect offset of 0 is too complex for it to generate CV records
490            // correctly.
491            && (direct_offset != Size::ZERO || !#[allow(non_exhaustive_omitted_patterns)] match &indirect_offsets[..] {
    [Size::ZERO] | [] => true,
    _ => false,
}matches!(&indirect_offsets[..], [Size::ZERO] | []));
492
493        if should_create_individual_allocas {
494            let DebugInfoOffset { direct_offset: _, indirect_offsets: _, result: place } =
495                calculate_debuginfo_offset(bx, var.projection, base);
496
497            // Create a variable which will be a pointer to the actual value
498            let ptr_ty = Ty::new_mut_ptr(bx.tcx(), place.layout.ty);
499            let ptr_layout = bx.layout_of(ptr_ty);
500            let alloca = PlaceRef::alloca(bx, ptr_layout);
501            bx.set_var_name(alloca.val.llval, &(var.name.to_string() + ".dbg.spill"));
502
503            // Write the pointer to the variable
504            bx.store_to_place(place.val.llval, alloca.val);
505
506            // Point the debug info to `*alloca` for the current variable
507            bx.dbg_var_addr(
508                dbg_var,
509                dbg_loc,
510                alloca.val.llval,
511                Size::ZERO,
512                &[Size::ZERO],
513                &var.fragment,
514            );
515        } else {
516            bx.dbg_var_addr(
517                dbg_var,
518                dbg_loc,
519                base.val.llval,
520                direct_offset,
521                &indirect_offsets,
522                &var.fragment,
523            );
524        }
525    }
526
527    pub(crate) fn debug_introduce_locals(
528        &self,
529        bx: &mut Bx,
530        consts: Vec<ConstDebugInfo<'a, 'tcx, Bx>>,
531    ) {
532        if bx.sess().opts.debuginfo == DebugInfo::Full || !bx.sess().fewer_names() {
533            for local in self.locals.indices() {
534                self.debug_introduce_local(bx, local);
535            }
536
537            for ConstDebugInfo { name, source_info, operand, dbg_var, dbg_loc, fragment, .. } in
538                consts.into_iter()
539            {
540                self.set_debug_loc(bx, source_info);
541                let base = FunctionCx::spill_operand_to_stack(operand, Some(name), bx);
542                bx.clear_dbg_loc();
543
544                bx.dbg_var_addr(dbg_var, dbg_loc, base.val.llval, Size::ZERO, &[], &fragment);
545            }
546        }
547    }
548
549    /// Partition all `VarDebugInfo` in `self.mir`, by their base `Local`.
550    pub(crate) fn compute_per_local_var_debug_info(
551        &self,
552        bx: &mut Bx,
553    ) -> Option<(
554        PerLocalVarDebugInfoIndexVec<'tcx, Bx::DIVariable>,
555        Vec<ConstDebugInfo<'a, 'tcx, Bx>>,
556    )> {
557        let full_debug_info = self.cx.sess().opts.debuginfo == DebugInfo::Full;
558
559        let target_is_msvc = self.cx.sess().target.is_like_msvc;
560
561        if !full_debug_info && self.cx.sess().fewer_names() {
562            return None;
563        }
564
565        let mut per_local = IndexVec::from_elem(::alloc::vec::Vec::new()vec![], &self.mir.local_decls);
566        let mut constants = ::alloc::vec::Vec::new()vec![];
567        for var in &self.mir.var_debug_info {
568            let dbg_scope_and_span = if full_debug_info {
569                self.adjusted_span_and_dbg_scope(bx, var.source_info)
570            } else {
571                None
572            };
573
574            let var_ty = if let Some(ref fragment) = var.composite {
575                self.monomorphize(fragment.ty)
576            } else {
577                match var.value {
578                    mir::VarDebugInfoContents::Place(place) => {
579                        self.monomorphized_place_ty(place.as_ref())
580                    }
581                    mir::VarDebugInfoContents::Const(c) => self.monomorphize(c.ty()),
582                }
583            };
584
585            let dbg_var = dbg_scope_and_span.map(|(dbg_scope, _, span)| {
586                let var_kind = if let Some(arg_index) = var.argument_index
587                    && var.composite.is_none()
588                    && let mir::VarDebugInfoContents::Place(place) = var.value
589                    && place.projection.is_empty()
590                {
591                    let arg_index = arg_index as usize;
592                    if target_is_msvc {
593                        // ScalarPair parameters are spilled to the stack so they need to
594                        // be marked as a `LocalVariable` for MSVC debuggers to visualize
595                        // their data correctly. (See #81894 & #88625)
596                        let var_ty_layout = self.cx.layout_of(var_ty);
597                        if let BackendRepr::ScalarPair { a: _, b: _, b_offset: _ } =
598                            var_ty_layout.backend_repr
599                        {
600                            VariableKind::LocalVariable
601                        } else {
602                            VariableKind::ArgumentVariable(arg_index)
603                        }
604                    } else {
605                        // FIXME(eddyb) shouldn't `ArgumentVariable` indices be
606                        // offset in closures to account for the hidden environment?
607                        VariableKind::ArgumentVariable(arg_index)
608                    }
609                } else {
610                    VariableKind::LocalVariable
611                };
612
613                bx.create_dbg_var(var.name, var_ty, dbg_scope, var_kind, span)
614            });
615
616            let fragment = if let Some(ref fragment) = var.composite {
617                let var_layout = self.cx.layout_of(var_ty);
618
619                let DebugInfoOffset { direct_offset, indirect_offsets, result: fragment_layout } =
620                    calculate_debuginfo_offset(bx, &fragment.projection, var_layout);
621                if !indirect_offsets.is_empty() {
    ::core::panicking::panic("assertion failed: indirect_offsets.is_empty()")
};assert!(indirect_offsets.is_empty());
622
623                if fragment_layout.size == Size::ZERO {
624                    // Fragment is a ZST, so does not represent anything. Avoid generating anything
625                    // as this may conflict with a fragment that covers the entire variable.
626                    continue;
627                } else if fragment_layout.size == var_layout.size {
628                    // Fragment covers entire variable, so as far as
629                    // DWARF is concerned, it's not really a fragment.
630                    None
631                } else {
632                    Some(direct_offset..direct_offset + fragment_layout.size)
633                }
634            } else {
635                None
636            };
637
638            match var.value {
639                mir::VarDebugInfoContents::Place(place) => {
640                    per_local[place.local].push(PerLocalVarDebugInfo {
641                        name: var.name,
642                        source_info: var.source_info,
643                        dbg_var,
644                        fragment,
645                        projection: place.projection,
646                    });
647                }
648                mir::VarDebugInfoContents::Const(c) => {
649                    if let Some(dbg_var) = dbg_var {
650                        let Some(dbg_loc) = self.dbg_loc(bx, var.source_info) else { continue };
651
652                        let operand = self.eval_mir_constant_to_operand(bx, &c);
653                        constants.push(ConstDebugInfo {
654                            name: var.name.to_string(),
655                            source_info: var.source_info,
656                            operand,
657                            dbg_var,
658                            dbg_loc,
659                            fragment,
660                            _phantom: PhantomData,
661                        });
662                    }
663                }
664            }
665        }
666        Some((per_local, constants))
667    }
668
669    pub(crate) fn codegen_stmt_debuginfo(
670        &mut self,
671        bx: &mut Bx,
672        debuginfo: &mir::StmtDebugInfo<'tcx>,
673    ) {
674        match debuginfo {
675            mir::StmtDebugInfo::AssignRef(dest, place) => {
676                let local_ref = match self.locals[place.local] {
677                    // For an rvalue like `&(_1.1)`, when `BackendRepr` is `BackendRepr::Memory`, we allocate a block of memory to this place.
678                    // The place is an indirect pointer, we can refer to it directly.
679                    LocalRef::Place(place_ref) => Some((place_ref, place.projection.as_slice())),
680                    // For an rvalue like `&((*_1).1)`, we are calculating the address of `_1.1`.
681                    // The deref projection is no-op here.
682                    LocalRef::Operand(operand_ref) if place.is_indirect_first_projection() => {
683                        Some((operand_ref.deref(bx.cx()), &place.projection[1..]))
684                    }
685                    // For an rvalue like `&1`, when `BackendRepr` is `BackendRepr::Scalar`,
686                    // we cannot get the address.
687                    // N.B. `non_ssa_locals` returns that this is an SSA local.
688                    LocalRef::Operand(_) => None,
689                    LocalRef::UnsizedPlace(_) | LocalRef::PendingOperand => None,
690                }
691                .filter(|(_, projection)| {
692                    // Drop unsupported projections.
693                    projection.iter().all(|p| p.can_use_in_debuginfo())
694                });
695                if let Some((base, projection)) = local_ref {
696                    self.debug_new_val_to_local(bx, *dest, base, projection);
697                } else {
698                    // If the address cannot be calculated, use poison to indicate that the value has been optimized out.
699                    self.debug_poison_to_local(bx, *dest);
700                }
701            }
702            mir::StmtDebugInfo::InvalidAssign(local) => {
703                self.debug_poison_to_local(bx, *local);
704            }
705        }
706    }
707
708    pub(crate) fn codegen_stmt_debuginfos(
709        &mut self,
710        bx: &mut Bx,
711        debuginfos: &[mir::StmtDebugInfo<'tcx>],
712    ) {
713        for debuginfo in debuginfos {
714            self.codegen_stmt_debuginfo(bx, debuginfo);
715        }
716    }
717
718    /// Creates the function-specific debug context.
719    ///
720    /// Returns the FunctionDebugContext for the function which holds state needed
721    /// for debug info creation, if it is enabled.
722    pub(super) fn fill_function_debug_context(&mut self, bx: &mut Bx) {
723        if self.cx.sess().opts.debuginfo == DebugInfo::None {
724            return;
725        }
726
727        // Initialize fn debug context (including scopes).
728        self.debug_context = Some(FunctionDebugContext {
729            scopes: IndexVec::with_capacity(self.mir.source_scopes.len()),
730        });
731
732        // Find all scopes with variables defined in them.
733        let variables = if self.cx.sess().opts.debuginfo == DebugInfo::Full {
734            let mut vars = DenseBitSet::new_empty(self.mir.source_scopes.len());
735            // FIXME(eddyb) take into account that arguments always have debuginfo,
736            // irrespective of their name (assuming full debuginfo is enabled).
737            // NOTE(eddyb) actually, on second thought, those are always in the
738            // function scope, which always exists.
739            for var_debug_info in &self.mir.var_debug_info {
740                vars.insert(var_debug_info.source_info.scope);
741            }
742            Some(vars)
743        } else {
744            // Nothing to emit, of course.
745            None
746        };
747
748        // Instantiate all scopes.
749        let mut discriminators = FxHashMap::default();
750        for scope in self.mir.source_scopes.indices() {
751            let scope_data = self.make_mir_scope(bx, &variables, &mut discriminators, scope);
752            let _s = self.debug_context.as_mut().unwrap().scopes.push(scope_data);
753            if true {
    {
        match (&_s, &scope) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(_s, scope);
754        }
755    }
756
757    fn make_mir_scope(
758        &mut self,
759        bx: &mut Bx,
760        variables: &Option<DenseBitSet<mir::SourceScope>>,
761        discriminators: &mut FxHashMap<BytePos, u32>,
762        scope: mir::SourceScope,
763    ) -> DebugScope<Bx::DIScope, Bx::DILocation> {
764        let scope_data = &self.mir.source_scopes[scope];
765        let parent_scope = if let Some(parent) = scope_data.parent_scope {
766            if true {
    if !(parent.as_u32() < scope.as_u32()) {
        ::core::panicking::panic("assertion failed: parent.as_u32() < scope.as_u32()")
    };
};debug_assert!(parent.as_u32() < scope.as_u32());
767            self.debug_context.as_ref().unwrap().scopes[parent]
768        } else {
769            // The root is the function itself.
770            let file = self.cx.sess().source_map().lookup_source_file(self.mir.span.lo());
771            let dbg_scope = bx.dbg_scope_fn(self.instance, self.fn_abi, Some(self.llfn));
772            return DebugScope {
773                dbg_scope,
774                inlined_at: None,
775                file_start_pos: file.start_pos,
776                file_end_pos: file.end_position(),
777            };
778        };
779
780        if let Some(vars) = variables
781            && !vars.contains(scope)
782            && scope_data.inlined.is_none()
783        {
784            // Do not create a DIScope if there are no variables defined in this
785            // MIR `SourceScope`, and it's not `inlined`, to avoid debuginfo bloat.
786            return parent_scope;
787        }
788
789        let dbg_scope = match scope_data.inlined {
790            Some((callee, _)) => {
791                let callee = self.monomorphize(callee);
792                let callee_fn_abi = self.cx.fn_abi_of_instance(callee, ty::List::empty());
793                bx.dbg_scope_fn(callee, callee_fn_abi, None)
794            }
795            None => bx.dbg_create_lexical_block(scope_data.span.lo(), parent_scope.dbg_scope),
796        };
797
798        let inlined_at = scope_data.inlined.map(|(_, callsite_span)| {
799            let callsite_span = hygiene::walk_chain_collapsed(callsite_span, self.mir.span);
800            let callsite_scope = parent_scope.adjust_dbg_scope_for_span(bx, callsite_span);
801            let loc = bx.dbg_loc(callsite_scope, parent_scope.inlined_at, callsite_span);
802
803            // NB: In order to produce proper debug info for variables (particularly
804            // arguments) in multiply-inlined functions, LLVM expects to see a single
805            // DILocalVariable with multiple different DILocations in the IR. While
806            // the source information for each DILocation would be identical, their
807            // inlinedAt attributes will be unique to the particular callsite.
808            //
809            // We generate DILocations here based on the callsite's location in the
810            // source code. A single location in the source code usually can't
811            // produce multiple distinct calls so this mostly works, until
812            // macros get involved. A macro can generate multiple calls
813            // at the same span, which breaks the assumption that we're going to
814            // produce a unique DILocation for every scope we process here. We
815            // have to explicitly add discriminators if we see inlines into the
816            // same source code location.
817            //
818            // Note further that we can't key this hashtable on the span itself,
819            // because these spans could have distinct SyntaxContexts. We have
820            // to key on exactly what we're giving to LLVM.
821            match discriminators.entry(callsite_span.lo()) {
822                Entry::Occupied(mut o) => {
823                    *o.get_mut() += 1;
824                    // NB: We have to emit *something* here or we'll fail LLVM IR verification
825                    // in at least some circumstances (see issue #135322) so if the required
826                    // discriminant cannot be encoded fall back to the dummy location.
827                    bx.dbg_location_clone_with_discriminator(loc, *o.get()).unwrap_or_else(|| {
828                        bx.dbg_loc(callsite_scope, parent_scope.inlined_at, DUMMY_SP)
829                    })
830                }
831                Entry::Vacant(v) => {
832                    v.insert(0);
833                    loc
834                }
835            }
836        });
837
838        let file = self.cx.sess().source_map().lookup_source_file(scope_data.span.lo());
839        DebugScope {
840            dbg_scope,
841            inlined_at: inlined_at.or(parent_scope.inlined_at),
842            file_start_pos: file.start_pos,
843            file_end_pos: file.end_position(),
844        }
845    }
846}