From e265644b324424bbc00185293594d3207150ee6e Mon Sep 17 00:00:00 2001 From: Jeremy Morse Date: Tue, 5 Oct 2021 13:44:40 +0100 Subject: [PATCH] [DebugInfo][InstrRef] Track all of DBG_PHIs operands An important part of the instruction referencing solution is that we identify all the registers that values move between before we then compute an SSA-like function from the machine code, and from the variable intrinsics. DBG_PHIs weren't causing all the subregisters of their operands to be tracked; this patch forces that to happen. The practical implications were that not enough space is allocated for storing values when analysing the function -- asan will crash on the attached test case with an unpatched compiler. Non-asan llc's will produce a DBG_VALUE $noreg, where it should be $dil. Differential Revision: https://reviews.llvm.org/D109064 --- .../CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp | 6 ++ .../MIR/InstrRef/dbg-phi-subregister-location.mir | 71 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 llvm/test/DebugInfo/MIR/InstrRef/dbg-phi-subregister-location.mir diff --git a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp index 233824f..cc30bfc 100644 --- a/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp +++ b/llvm/lib/CodeGen/LiveDebugValues/InstrRefBasedImpl.cpp @@ -2027,6 +2027,12 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) { auto PHIRec = DebugPHIRecord( {InstrNum, MI.getParent(), Num, MTracker->lookupOrTrackRegister(Reg)}); DebugPHINumToValue.push_back(PHIRec); + + // Subsequent register operations, or variable locations, might occur for + // any of the subregisters of this DBG_PHIs operand. Ensure that all + // registers aliasing this register are tracked. + for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); RAI.isValid(); ++RAI) + MTracker->lookupOrTrackRegister(*RAI); } else { // The value is whatever's in this stack slot. assert(MO.isFI()); diff --git a/llvm/test/DebugInfo/MIR/InstrRef/dbg-phi-subregister-location.mir b/llvm/test/DebugInfo/MIR/InstrRef/dbg-phi-subregister-location.mir new file mode 100644 index 0000000..1730bca --- /dev/null +++ b/llvm/test/DebugInfo/MIR/InstrRef/dbg-phi-subregister-location.mir @@ -0,0 +1,71 @@ +# RUN: llc %s -run-pass=livedebugvalues -experimental-debug-variable-locations\ +# RUN: -o - | FileCheck %s +# +# In the MIR below, there's an argument in the lowest byte of $edi. The debug +# intrinsics correctly identify the value and where it becomes the variables +# value, however a bug in InstrRefBasedLDV meant that not all subregisters of +# DBG_PHI operands are tracked. That leads to the wrong DBG_VALUE location +# being produced, and a crash under asan. +# +# CHECK-LABEL: name: foo +# CHECK: DBG_PHI $edi +# CHECK-NEXT: DBG_INSTR_REF 2, 0 +# CHECK-NEXT: DBG_VALUE $dil +--- | + ; ModuleID = 'out.ll' + source_filename = "out.ll" + 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-scei-ps4" + + @someglobal = external local_unnamed_addr global i8, align 1 + + define hidden void @foo(i1 zeroext %bar) !dbg !7 { + entry: + ret void, !dbg !13 + } + + declare void @llvm.dbg.value(metadata, metadata, metadata) + + !llvm.dbg.cu = !{!0} + !llvm.module.flags = !{!3, !4, !5} + !llvm.ident = !{!6} + + !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None) + !1 = !DIFile(filename: "test.c", directory: "/tmp/out.c") + !2 = !{} + !3 = !{i32 7, !"Dwarf Version", i32 4} + !4 = !{i32 2, !"Debug Info Version", i32 3} + !5 = !{i32 1, !"wchar_size", i32 4} + !6 = !{!""} + !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) + !8 = !DISubroutineType(types: !9) + !9 = !{!10, !11, !11} + !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) + !11 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) + !12 = !DILocalVariable(name: "baz", arg: 2, scope: !7, file: !1, line: 3, type: !11) + !13 = !DILocation(line: 0, scope: !7) + +... +--- +name: foo +alignment: 16 +tracksRegLiveness: true +liveins: + - { reg: '$edi' } +frameInfo: + maxAlignment: 1 + maxCallFrameSize: 0 +debugValueSubstitutions: + - { srcinst: 2, srcop: 0, dstinst: 1, dstop: 0, subreg: 1 } +machineFunctionInfo: {} +body: | + bb.0.entry: + liveins: $edi + + DBG_PHI $edi, 1 + DBG_INSTR_REF 2, 0, !12, !DIExpression(), debug-location !13 + renamable $rax = MOV64rm $rip, 1, $noreg, target-flags(x86-gotpcrel) @someglobal, $noreg, debug-location !13 :: (load (s64) from got) + MOV8mr killed renamable $rax, 1, $noreg, 0, $noreg, renamable $dil, debug-location !13 :: (store (s8) into @someglobal) + RETQ debug-location !13 + +... -- 2.7.4