Skip to content

Commit

Permalink
[NVPTX] add support for .debug_loc section (llvm#110905)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
willghatch authored Oct 3, 2024
1 parent 7b9c6a7 commit ae635d6
Show file tree
Hide file tree
Showing 10 changed files with 2,483 additions and 2,423 deletions.
43 changes: 27 additions & 16 deletions llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ DwarfDebug::DwarfDebug(AsmPrinter *A)
else
UseInlineStrings = DwarfInlinedStrings == Enable;

UseLocSection = !TT.isNVPTX();

// Always emit .debug_aranges for SCE tuning.
UseARangesSection = GenerateARangeSection || tuneForSCE();

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

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

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

// Do not emit location lists if .debug_loc secton is disabled.
if (!useLocSection())
continue;

// Handle multiple DBG_VALUE instructions describing one variable.
DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar);

Expand Down Expand Up @@ -2841,7 +2842,17 @@ static void emitRangeList(
bool BaseIsSet = false;
for (const auto &P : SectionRanges) {
auto *Base = CUBase;
if (!Base && ShouldUseBaseAddress) {
if ((Asm->TM.getTargetTriple().isNVPTX() && DD.tuneForGDB())) {
// PTX does not support subtracting labels from the code section in the
// debug_loc section. To work around this, the NVPTX backend needs the
// compile unit to have no low_pc in order to have a zero base_address
// when handling debug_loc in cuda-gdb. Additionally, cuda-gdb doesn't
// seem to handle setting a per-variable base to zero. To make cuda-gdb
// happy, just emit labels with no base while having no compile unit
// low_pc.
BaseIsSet = false;
Base = nullptr;
} else if (!Base && ShouldUseBaseAddress) {
const MCSymbol *Begin = P.second.front()->Begin;
const MCSymbol *NewBase = DD.getSectionLabel(&Begin->getSection());
if (!UseDwarf5) {
Expand Down
6 changes: 0 additions & 6 deletions llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,6 @@ class DwarfDebug : public DebugHandlerBase {
/// temp symbols inside DWARF sections.
bool UseSectionsAsReferences = false;

///Allow emission of the .debug_loc section.
bool UseLocSection = true;

/// Allow emission of .debug_aranges section
bool UseARangesSection = false;

Expand Down Expand Up @@ -791,9 +788,6 @@ class DwarfDebug : public DebugHandlerBase {
return UseSectionsAsReferences;
}

/// Returns whether .debug_loc section should be emitted.
bool useLocSection() const { return UseLocSection; }

/// Returns whether to generate DWARF v4 type units.
bool generateTypeUnits() const { return GenerateTypeUnits; }

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ bool NVPTXAsmPrinter::doFinalization(Module &M) {
// Close the last emitted section
if (hasDebugInfo()) {
TS->closeLastSection();
// Emit empty .debug_loc section for better support of the empty files.
OutStreamer->emitRawText("\t.section\t.debug_loc\t{\t}");
// Emit empty .debug_macinfo section for better support of the empty files.
OutStreamer->emitRawText("\t.section\t.debug_macinfo\t{\t}");
}

// Output last DWARF .file directives, if any.
Expand Down
256 changes: 125 additions & 131 deletions llvm/test/DebugInfo/NVPTX/cu-range-hole.ll

Large diffs are not rendered by default.

292 changes: 143 additions & 149 deletions llvm/test/DebugInfo/NVPTX/dbg-declare-alloca.ll

Large diffs are not rendered by default.

Loading

0 comments on commit ae635d6

Please sign in to comment.