Index: llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h =================================================================== --- llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h +++ llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.h @@ -717,6 +717,8 @@ value_type operator*() { return value_type(Idx, ValueMap[LocIdx(Idx)]); } }; + unsigned PointerSizeBytes = 0; + MLocTracker(MachineFunction &MF, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const TargetLowering &TLI); @@ -936,6 +938,9 @@ std::string IDAsString(const ValueIDNum &Num) const; + bool useDerefSize(const DebugVariable &Var, LocIdx MLoc, + const DIExpression *Expr) const; + #ifndef NDEBUG LLVM_DUMP_METHOD void dump(); Index: llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp =================================================================== --- llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp +++ llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp @@ -996,6 +996,8 @@ LocIDToLocIdx.resize(NumRegs, LocIdx::MakeIllegalLoc()); assert(NumRegs < (1u << NUM_LOC_BITS)); // Detect bit packing failure + PointerSizeBytes = MF.getTarget().getPointerSize(0); + // Always track SP. This avoids the implicit clobbering caused by regmasks // from affectings its values. (LiveDebugValues disbelieves calls and // regmasks that claim to clobber SP). @@ -1154,6 +1156,40 @@ } #endif +bool MLocTracker::useDerefSize(const DebugVariable &Var, LocIdx MLoc, + const DIExpression *Expr) const { + // We need to use deref_size whenever there's a mismatch between the + // size of value and the size of variable portion being read, on big endian + // systems. Consider: + // + // store [r1], r2.64bits + // DBG_VALUE [r1], ... ; 32-bit-variable + // + // If we widen the store of a 32 bit variable into a 64 bit stack spill, on + // little-endian systems the 32 lower bits always end up in the same place. + // However, on big-endian systems this is not true, so we need to describe + // it as "the lower 32 bits that you get after a 64 bit load", by using + // DW_OP_deref_size. + + unsigned ValueSizeInBits = getLocSizeInBits(MLoc); + unsigned DerefSizeInBytes = ValueSizeInBits / 8; + if (DerefSizeInBytes > PointerSizeBytes) { + // This would be illegal DWARF. Perhaps the output will identify a + // flawed location, but it'll at least be legal output. + return false; + } else if (auto Fragment = Var.getFragment()) { + // XXX why the non-complex bit. + unsigned VariableSizeInBits = Fragment->SizeInBits; + if (VariableSizeInBits != ValueSizeInBits || Expr->isComplex()) + return true; + } else if (auto Size = Var.getVariable()->getSizeInBits()) { + if (*Size != ValueSizeInBits) + return true; + } + + return false; +} + MachineInstrBuilder MLocTracker::emitLoc(const SmallVectorImpl &DbgOps, const DebugVariable &Var, @@ -1240,30 +1276,13 @@ // nothing else in their DIExpressions, // * Variables with DW_OP_stack_value in their expr already need an // explicit dereference of the stack location, - // * Values that don't match the variable size need DW_OP_deref_size, + // * Values that don't match the variable size potentially need + // DW_OP_deref_size to describe them, // * Everything else can just become a simple location expression. - - // We need to use deref_size whenever there's a mismatch between the - // size of value and the size of variable portion being read. - // Additionally, we should use it whenever dealing with stack_value - // fragments, to avoid the consumer having to determine the deref size - // from DW_OP_piece. - bool UseDerefSize = false; - unsigned ValueSizeInBits = getLocSizeInBits(MLoc); - unsigned DerefSizeInBytes = ValueSizeInBits / 8; - if (auto Fragment = Var.getFragment()) { - unsigned VariableSizeInBits = Fragment->SizeInBits; - if (VariableSizeInBits != ValueSizeInBits || Expr->isComplex()) - UseDerefSize = true; - } else if (auto Size = Var.getVariable()->getSizeInBits()) { - if (*Size != ValueSizeInBits) { - UseDerefSize = true; - } - } - SmallVector OffsetOps; TRI.getOffsetOpcodes(Spill.SpillOffset, OffsetOps); bool StackValue = false; + bool UseDerefSize = useDerefSize(Var, MLoc, Expr); if (Properties.Indirect) { // This is something like an NRVO variable, where the pointer has been @@ -1277,6 +1296,8 @@ // We're loading a value off the stack that's not the same size as the // variable. Add / subtract stack offset, explicitly deref with a // size, and add DW_OP_stack_value if not already present. + unsigned ValueSizeInBits = getLocSizeInBits(MLoc); + unsigned DerefSizeInBytes = ValueSizeInBits / 8; OffsetOps.push_back(dwarf::DW_OP_deref_size); OffsetOps.push_back(DerefSizeInBytes); StackValue = true; Index: llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_deref_size_too_big.mir =================================================================== --- /dev/null +++ llvm/test/DebugInfo/MIR/InstrRef/livedebugvalues_deref_size_too_big.mir @@ -0,0 +1,97 @@ +# RUN: llc %s -o - --run-pass=livedebugvalues | FileCheck %s +# +# Test that when a small variable ("e", a double) in a large register (xmm0, +# which is 128 bits wide) is spilt, we don't produce a DW_OP_deref_size that +# exceeds the size of an address (64 bits). We should produce a plain +# DW_OP_deref instead, as that's a legal output. +# +# See https://github.com/llvm/llvm-project/issues/64093 +# +# CHECK: DBG_VALUE $rsp, 0, +# +# (That's an indirect location for the variable, at the stack pointer, where +# it's just been spilt). +--- | + target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + target triple = "x86_64-unknown-linux-gnu" + + define dso_local void @e(double noundef %e) local_unnamed_addr #0 !dbg !10 { + entry: + call void @llvm.dbg.value(metadata double %e, metadata !15, metadata !DIExpression()), !dbg !16 + tail call void (...) @c() #3, !dbg !17 + tail call void @b(double noundef %e) #3, !dbg !18 + ret void, !dbg !19 + } + + declare !dbg !20 void @c(...) local_unnamed_addr #1 + + declare !dbg !23 void @b(double noundef) local_unnamed_addr #1 + + ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) + declare void @llvm.dbg.value(metadata, metadata, metadata) #2 + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8} + !llvm.ident = !{!9} + + !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) + !1 = !DIFile(filename: "test3.c", directory: "/tmp") + !2 = !{i32 7, !"Dwarf Version", i32 5} + !3 = !{i32 2, !"Debug Info Version", i32 3} + !4 = !{i32 1, !"wchar_size", i32 4} + !5 = !{i32 8, !"PIC Level", i32 2} + !6 = !{i32 7, !"PIE Level", i32 2} + !7 = !{i32 7, !"uwtable", i32 2} + !8 = !{i32 7, !"debug-info-assignment-tracking", i1 true} + !9 = !{!"clang"} + !10 = distinct !DISubprogram(name: "e", scope: !1, file: !1, line: 3, type: !11, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !14) + !11 = !DISubroutineType(types: !12) + !12 = !{null, !13} + !13 = !DIBasicType(name: "double", size: 64, encoding: DW_ATE_float) + !14 = !{!15} + !15 = !DILocalVariable(name: "e", arg: 1, scope: !10, file: !1, line: 3, type: !13) + !16 = !DILocation(line: 0, scope: !10) + !17 = !DILocation(line: 4, column: 3, scope: !10) + !18 = !DILocation(line: 5, column: 3, scope: !10) + !19 = !DILocation(line: 6, column: 1, scope: !10) + !20 = !DISubprogram(name: "c", scope: !1, file: !1, line: 2, type: !21, spFlags: DISPFlagOptimized) + !21 = !DISubroutineType(types: !22) + !22 = !{null} + !23 = !DISubprogram(name: "b", scope: !1, file: !1, line: 1, type: !11, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized) + +... +--- +name: e +alignment: 16 +tracksRegLiveness: true +debugInstrRef: true +tracksDebugUserValues: true +liveins: + - { reg: '$xmm0' } +frameInfo: + stackSize: 8 + offsetAdjustment: -8 + maxAlignment: 8 + adjustsStack: true + hasCalls: true + maxCallFrameSize: 0 + hasTailCall: true +stack: + - { id: 0, type: spill-slot, offset: -16, size: 8, alignment: 8 } +machineFunctionInfo: {} +body: | + bb.0.entry: + liveins: $xmm0 + + DBG_VALUE $xmm0, $noreg, !15, !DIExpression(), debug-location !16 + frame-setup PUSH64r undef $rax, implicit-def $rsp, implicit $rsp + frame-setup CFI_INSTRUCTION def_cfa_offset 16 + MOVSDmr $rsp, 1, $noreg, 0, $noreg, $xmm0 :: (store (s64) into %stack.0) + dead $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags, implicit-def $al, debug-location !17 + CALL64pcrel32 target-flags(x86-plt) @c, csr_64, implicit $rsp, implicit $ssp, implicit killed $al, implicit-def $rsp, implicit-def $ssp, debug-location !17 + $xmm0 = MOVSDrm_alt $rsp, 1, $noreg, 0, $noreg :: (load (s64) from %stack.0) + $rax = frame-destroy POP64r implicit-def $rsp, implicit $rsp, debug-location !18 + frame-destroy CFI_INSTRUCTION def_cfa_offset 8, debug-location !18 + TAILJMPd64 target-flags(x86-plt) @b, csr_64, implicit $rsp, implicit $ssp, implicit $rsp, implicit $ssp, implicit $xmm0, debug-location !18 + +...