Skip to content

Commit ae635d6

Browse files
authored
[NVPTX] add support for .debug_loc section (llvm#110905)
Enable .debug_loc section for NVPTX backend. This commit makes NVPTX omit DW_AT_low_pc (and DW_AT_high_pc) for DW_TAG_compile_unit. This is because cuda-gdb uses the compile unit's low_pc as a base address, and adds the addresses in the debug_loc section to it. Removing low_pc is equivalent to setting that base address to zero, so addition doesn't break the location ranges. Additionally, this patch forces debug_loc label emission to emit single labels with no subtraction or base. This would not be necessary if we could emit `label1 - label2` expressions in PTX. The PTX documentation at https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#debugging-directives-section makes it seem like this is supported, but it doesn't actually work. I believe when that documentation says that you can subtract “label addresses between labels in the same dwarf section”, it doesn't merely mean that the labels need to be in the same section as each other, but in fact they need to be in the same section as the use. If support for label subtraction is supported such that in the debug_loc section you can subtract labels from the main code section, then we can remove the workarounds added in this PR. Also, since this now emits valid .debug_loc sections, it replaces the empty .debug_loc to force existence of at least one debug section with an empty .debug_macinfo section, which matches what nvcc does.
1 parent 7b9c6a7 commit ae635d6

File tree

10 files changed

+2483
-2423
lines changed

10 files changed

+2483
-2423
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ DwarfDebug::DwarfDebug(AsmPrinter *A)
352352
else
353353
UseInlineStrings = DwarfInlinedStrings == Enable;
354354

355-
UseLocSection = !TT.isNVPTX();
356-
357355
// Always emit .debug_aranges for SCE tuning.
358356
UseARangesSection = GenerateARangeSection || tuneForSCE();
359357

@@ -1325,15 +1323,22 @@ void DwarfDebug::finalizeModuleInfo() {
13251323
DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
13261324

13271325
if (unsigned NumRanges = TheCU.getRanges().size()) {
1328-
if (NumRanges > 1 && useRangesSection())
1329-
// A DW_AT_low_pc attribute may also be specified in combination with
1330-
// DW_AT_ranges to specify the default base address for use in
1331-
// location lists (see Section 2.6.2) and range lists (see Section
1332-
// 2.17.3).
1333-
U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
1334-
else
1335-
U.setBaseAddress(TheCU.getRanges().front().Begin);
1336-
U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
1326+
// PTX does not support subtracting labels from the code section in the
1327+
// debug_loc section. To work around this, the NVPTX backend needs the
1328+
// compile unit to have no low_pc in order to have a zero base_address
1329+
// when handling debug_loc in cuda-gdb.
1330+
if (!(Asm->TM.getTargetTriple().isNVPTX() && tuneForGDB())) {
1331+
if (NumRanges > 1 && useRangesSection())
1332+
// A DW_AT_low_pc attribute may also be specified in combination with
1333+
// DW_AT_ranges to specify the default base address for use in
1334+
// location lists (see Section 2.6.2) and range lists (see Section
1335+
// 2.17.3).
1336+
U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1337+
0);
1338+
else
1339+
U.setBaseAddress(TheCU.getRanges().front().Begin);
1340+
U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
1341+
}
13371342
}
13381343

13391344
// We don't keep track of which addresses are used in which CU so this
@@ -1920,10 +1925,6 @@ void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
19201925
}
19211926
}
19221927

1923-
// Do not emit location lists if .debug_loc secton is disabled.
1924-
if (!useLocSection())
1925-
continue;
1926-
19271928
// Handle multiple DBG_VALUE instructions describing one variable.
19281929
DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar);
19291930

@@ -2841,7 +2842,17 @@ static void emitRangeList(
28412842
bool BaseIsSet = false;
28422843
for (const auto &P : SectionRanges) {
28432844
auto *Base = CUBase;
2844-
if (!Base && ShouldUseBaseAddress) {
2845+
if ((Asm->TM.getTargetTriple().isNVPTX() && DD.tuneForGDB())) {
2846+
// PTX does not support subtracting labels from the code section in the
2847+
// debug_loc section. To work around this, the NVPTX backend needs the
2848+
// compile unit to have no low_pc in order to have a zero base_address
2849+
// when handling debug_loc in cuda-gdb. Additionally, cuda-gdb doesn't
2850+
// seem to handle setting a per-variable base to zero. To make cuda-gdb
2851+
// happy, just emit labels with no base while having no compile unit
2852+
// low_pc.
2853+
BaseIsSet = false;
2854+
Base = nullptr;
2855+
} else if (!Base && ShouldUseBaseAddress) {
28452856
const MCSymbol *Begin = P.second.front()->Begin;
28462857
const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection());
28472858
if (!UseDwarf5) {

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,6 @@ class DwarfDebug : public DebugHandlerBase {
432432
/// temp symbols inside DWARF sections.
433433
bool UseSectionsAsReferences = false;
434434

435-
///Allow emission of the .debug_loc section.
436-
bool UseLocSection = true;
437-
438435
/// Allow emission of .debug_aranges section
439436
bool UseARangesSection = false;
440437

@@ -791,9 +788,6 @@ class DwarfDebug : public DebugHandlerBase {
791788
return UseSectionsAsReferences;
792789
}
793790

794-
/// Returns whether .debug_loc section should be emitted.
795-
bool useLocSection() const { return UseLocSection; }
796-
797791
/// Returns whether to generate DWARF v4 type units.
798792
bool generateTypeUnits() const { return GenerateTypeUnits; }
799793

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,8 @@ bool NVPTXAsmPrinter::doFinalization(Module &M) {
964964
// Close the last emitted section
965965
if (hasDebugInfo()) {
966966
TS->closeLastSection();
967-
// Emit empty .debug_loc section for better support of the empty files.
968-
OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}");
967+
// Emit empty .debug_macinfo section for better support of the empty files.
968+
OutStreamer->emitRawText("\t.section\t.debug_macinfo\t{\t}");
969969
}
970970

971971
// Output last DWARF .file directives, if any.

llvm/test/DebugInfo/NVPTX/cu-range-hole.ll

Lines changed: 125 additions & 131 deletions
Large diffs are not rendered by default.

llvm/test/DebugInfo/NVPTX/dbg-declare-alloca.ll

Lines changed: 143 additions & 149 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)