Skip to content

Commit dc6cf37

Browse files
committed
Adding support for inlined_at debug directive in NVPTX backend
1 parent 4c59219 commit dc6cf37

File tree

19 files changed

+1800
-44
lines changed

19 files changed

+1800
-44
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
277277
AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer,
278278
char &ID = AsmPrinter::ID);
279279

280+
/// Create the DwarfDebug handler. Targets can override this to provide
281+
/// custom debug information handling.
282+
virtual DwarfDebug *createDwarfDebug();
283+
280284
public:
281285
~AsmPrinter() override;
282286

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,14 @@ class LLVM_ABI MCStreamer {
902902
StringRef FileName,
903903
StringRef Comment = {});
904904

905+
/// This is same as emitDwarfLocDirective, except has capability to
906+
/// add inlined_at information.
907+
virtual void emitDwarfLocDirectiveWithInlinedAt(
908+
unsigned FileNo, unsigned Line, unsigned Column, unsigned FileIA,
909+
unsigned LineIA, unsigned ColumnIA, const MCSymbol *Sym, unsigned Flags,
910+
unsigned Isa, unsigned Discriminator, StringRef FileName,
911+
StringRef Comment = {}) {}
912+
905913
/// This implements the '.loc_label Name' directive.
906914
virtual void emitDwarfLocLabelDirective(SMLoc Loc, StringRef Name);
907915

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ const MCSection *AsmPrinter::getCurrentSection() const {
470470
return OutStreamer->getCurrentSectionOnly();
471471
}
472472

473+
DwarfDebug *AsmPrinter::createDwarfDebug() { return new DwarfDebug(this); }
474+
473475
void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
474476
AU.setPreservesAll();
475477
MachineFunctionPass::getAnalysisUsage(AU);
@@ -592,7 +594,7 @@ bool AsmPrinter::doInitialization(Module &M) {
592594
Handlers.push_back(std::make_unique<CodeViewDebug>(this));
593595
if (!EmitCodeView || M.getDwarfVersion()) {
594596
if (hasDebugInfo()) {
595-
DD = new DwarfDebug(this);
597+
DD = createDwarfDebug();
596598
Handlers.push_back(std::unique_ptr<DwarfDebug>(DD));
597599
}
598600
}

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,13 +2204,27 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
22042204
Flags |= DWARF2_FLAG_IS_STMT;
22052205
}
22062206

2207-
RecordSourceLine(DL, Flags);
2207+
// Call the hook that allows targets to customize source line recording
2208+
recordSourceLineHook(*MI, DL, Flags);
22082209

22092210
// If we're not at line 0, remember this location.
22102211
if (DL.getLine())
22112212
PrevInstLoc = DL;
22122213
}
22132214

2215+
// Default implementation of target-specific hook for custom source line
2216+
// recording
2217+
void DwarfDebug::recordSourceLineHook(const MachineInstr &MI,
2218+
const DebugLoc &DL, unsigned Flags) {
2219+
SmallString<128> LocationString;
2220+
if (Asm->OutStreamer->isVerboseAsm()) {
2221+
raw_svector_ostream OS(LocationString);
2222+
DL.print(OS);
2223+
}
2224+
recordSourceLine(DL.getLine(), DL.getCol(), DL.getScope(), Flags,
2225+
LocationString);
2226+
}
2227+
22142228
// Returns the position where we should place prologue_end, potentially nullptr,
22152229
// which means "no good place to put prologue_end". Returns true in the second
22162230
// return value if there are no setup instructions in this function at all,
@@ -2705,6 +2719,9 @@ void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
27052719
Asm->OutStreamer->getContext().setDwarfCompileUnitID(
27062720
getDwarfCompileUnitIDForLineTable(CU));
27072721

2722+
// Call target-specific hook for custom initialization
2723+
beginFunctionHook(*MF);
2724+
27082725
// Record beginning of function.
27092726
PrologEndLoc = emitInitialLocDirective(
27102727
*MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID());

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,6 @@ class DwarfDebug : public DebugHandlerBase {
394394
/// table for the same directory as DW_AT_comp_dir.
395395
StringRef CompilationDir;
396396

397-
/// Holder for the file specific debug information.
398-
DwarfFile InfoHolder;
399-
400397
/// Holders for the various debug information flags that we might need to
401398
/// have exposed. See accessor functions below for description.
402399

@@ -532,10 +529,6 @@ class DwarfDebug : public DebugHandlerBase {
532529

533530
MCDwarfDwoLineTable *getDwoLineTable(const DwarfCompileUnit &);
534531

535-
const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
536-
return InfoHolder.getUnits();
537-
}
538-
539532
using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
540533

541534
void ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
@@ -711,6 +704,8 @@ class DwarfDebug : public DebugHandlerBase {
711704
void computeKeyInstructions(const MachineFunction *MF);
712705

713706
protected:
707+
/// Holder for the file specific debug information.
708+
DwarfFile InfoHolder;
714709
/// Gather pre-function debug information.
715710
void beginFunctionImpl(const MachineFunction *MF) override;
716711

@@ -722,6 +717,18 @@ class DwarfDebug : public DebugHandlerBase {
722717

723718
void skippedNonDebugFunction() override;
724719

720+
/// Target-specific hook for custom initialization,
721+
/// default implementation is empty, only being used for NVPTX target
722+
virtual void beginFunctionHook(const MachineFunction &MF) {}
723+
724+
/// Target-specific hook for custom source line recording
725+
virtual void recordSourceLineHook(const MachineInstr &MI, const DebugLoc &DL,
726+
unsigned Flags);
727+
728+
const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
729+
return InfoHolder.getUnits();
730+
}
731+
725732
public:
726733
//===--------------------------------------------------------------------===//
727734
// Main entry points.

llvm/lib/CodeGen/AsmPrinter/DwarfStringPool.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ void DwarfStringPool::emit(AsmPrinter &Asm, MCSection *StrSection,
9696
if (ShouldCreateSymbols)
9797
Asm.OutStreamer->emitLabel(Entry->getValue().Symbol);
9898

99+
// Emit a comment with the string offset and the string itself.
100+
Asm.OutStreamer->AddComment(
101+
"string offset=" + Twine(Entry->getValue().Offset) + " ; " +
102+
StringRef(Entry->getKeyData(), Entry->getKeyLength()));
103+
99104
// Emit the string itself with a terminating null byte.
100-
Asm.OutStreamer->AddComment("string offset=" +
101-
Twine(Entry->getValue().Offset));
102105
Asm.OutStreamer->emitBytes(
103106
StringRef(Entry->getKeyData(), Entry->getKeyLength() + 1));
104107
}

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 90 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ class MCAsmStreamer final : public MCStreamer {
7070
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
7171
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
7272

73+
/// Helper to emit common .loc directive flags, isa, and discriminator
74+
void emitDwarfLocDirectiveFlags(unsigned Flags, unsigned Isa,
75+
unsigned Discriminator);
76+
77+
/// Helper to emit the common suffix of .loc directives (flags, comment, EOL,
78+
/// parent call)
79+
void emitDwarfLocDirectiveSuffix(unsigned FileNo, unsigned Line,
80+
unsigned Column, unsigned Flags,
81+
unsigned Isa, unsigned Discriminator,
82+
StringRef FileName, StringRef Comment);
83+
7384
public:
7485
MCAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> os,
7586
std::unique_ptr<MCInstPrinter> printer,
@@ -297,6 +308,14 @@ class MCAsmStreamer final : public MCStreamer {
297308
StringRef Location = {}) override;
298309
void emitDwarfLocLabelDirective(SMLoc Loc, StringRef Name) override;
299310

311+
void emitDwarfLocDirectiveWithInlinedAt(unsigned FileNo, unsigned Line,
312+
unsigned Column, unsigned FileIA,
313+
unsigned LineIA, unsigned ColIA,
314+
const MCSymbol *Sym, unsigned Flags,
315+
unsigned Isa, unsigned Discriminator,
316+
StringRef FileName,
317+
StringRef Comment = {}) override;
318+
300319
MCSymbol *getDwarfLineTableSymbol(unsigned CUID) override;
301320

302321
bool emitCVFileDirective(unsigned FileNo, StringRef Filename,
@@ -1675,6 +1694,57 @@ void MCAsmStreamer::emitDwarfFile0Directive(
16751694
emitRawText(OS1.str());
16761695
}
16771696

1697+
/// Helper to emit common .loc directive flags, isa, and discriminator
1698+
void MCAsmStreamer::emitDwarfLocDirectiveFlags(unsigned Flags, unsigned Isa,
1699+
unsigned Discriminator) {
1700+
if (!MAI->supportsExtendedDwarfLocDirective())
1701+
return;
1702+
1703+
if (Flags & DWARF2_FLAG_BASIC_BLOCK)
1704+
OS << " basic_block";
1705+
if (Flags & DWARF2_FLAG_PROLOGUE_END)
1706+
OS << " prologue_end";
1707+
if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1708+
OS << " epilogue_begin";
1709+
1710+
const unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
1711+
if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
1712+
OS << " is_stmt ";
1713+
OS << ((Flags & DWARF2_FLAG_IS_STMT) ? "1" : "0");
1714+
}
1715+
1716+
if (Isa)
1717+
OS << " isa " << Isa;
1718+
if (Discriminator)
1719+
OS << " discriminator " << Discriminator;
1720+
}
1721+
1722+
/// Helper to emit the common suffix of .loc directives
1723+
void MCAsmStreamer::emitDwarfLocDirectiveSuffix(unsigned FileNo, unsigned Line,
1724+
unsigned Column, unsigned Flags,
1725+
unsigned Isa,
1726+
unsigned Discriminator,
1727+
StringRef FileName,
1728+
StringRef Comment) {
1729+
// Emit flags, isa, and discriminator
1730+
emitDwarfLocDirectiveFlags(Flags, Isa, Discriminator);
1731+
1732+
// Emit verbose comment if enabled
1733+
if (IsVerboseAsm) {
1734+
OS.PadToColumn(MAI->getCommentColumn());
1735+
OS << MAI->getCommentString() << ' ';
1736+
if (Comment.empty())
1737+
OS << FileName << ':' << Line << ':' << Column;
1738+
else
1739+
OS << Comment;
1740+
}
1741+
1742+
// Emit end of line and update parent state
1743+
EmitEOL();
1744+
MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
1745+
Discriminator, FileName, Comment);
1746+
}
1747+
16781748
void MCAsmStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
16791749
unsigned Column, unsigned Flags,
16801750
unsigned Isa, unsigned Discriminator,
@@ -1691,42 +1761,29 @@ void MCAsmStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
16911761
return;
16921762
}
16931763

1764+
// Emit the basic .loc directive
16941765
OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
1695-
if (MAI->supportsExtendedDwarfLocDirective()) {
1696-
if (Flags & DWARF2_FLAG_BASIC_BLOCK)
1697-
OS << " basic_block";
1698-
if (Flags & DWARF2_FLAG_PROLOGUE_END)
1699-
OS << " prologue_end";
1700-
if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1701-
OS << " epilogue_begin";
1702-
1703-
unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
1704-
if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
1705-
OS << " is_stmt ";
1706-
1707-
if (Flags & DWARF2_FLAG_IS_STMT)
1708-
OS << "1";
1709-
else
1710-
OS << "0";
1711-
}
17121766

1713-
if (Isa)
1714-
OS << " isa " << Isa;
1715-
if (Discriminator)
1716-
OS << " discriminator " << Discriminator;
1717-
}
1767+
// Emit common suffix (flags, comment, EOL, parent call)
1768+
emitDwarfLocDirectiveSuffix(FileNo, Line, Column, Flags, Isa, Discriminator,
1769+
FileName, Comment);
1770+
}
17181771

1719-
if (IsVerboseAsm) {
1720-
OS.PadToColumn(MAI->getCommentColumn());
1721-
OS << MAI->getCommentString() << ' ';
1722-
if (Comment.empty())
1723-
OS << FileName << ':' << Line << ':' << Column;
1724-
else
1725-
OS << Comment;
1726-
}
1727-
EmitEOL();
1728-
this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
1729-
Discriminator, FileName, Comment);
1772+
/// This is same as emitDwarfLocDirective, except also emits inlined function
1773+
/// and inlined callsite information.
1774+
void MCAsmStreamer::emitDwarfLocDirectiveWithInlinedAt(
1775+
unsigned FileNo, unsigned Line, unsigned Column, unsigned FileIA,
1776+
unsigned LineIA, unsigned ColIA, const MCSymbol *Sym, unsigned Flags,
1777+
unsigned Isa, unsigned Discriminator, StringRef FileName,
1778+
StringRef Comment) {
1779+
// Emit the basic .loc directive with NVPTX-specific extensions
1780+
OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
1781+
OS << ", function_name " << *Sym;
1782+
OS << ", inlined_at " << FileIA << " " << LineIA << " " << ColIA;
1783+
1784+
// Emit common suffix (flags, comment, EOL, parent call)
1785+
emitDwarfLocDirectiveSuffix(FileNo, Line, Column, Flags, Isa, Discriminator,
1786+
FileName, Comment);
17301787
}
17311788

17321789
void MCAsmStreamer::emitDwarfLocLabelDirective(SMLoc Loc, StringRef Name) {

llvm/lib/Target/NVPTX/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(NVPTXCodeGen_sources
1818
NVPTXAssignValidGlobalNames.cpp
1919
NVPTXAtomicLower.cpp
2020
NVPTXCtorDtorLowering.cpp
21+
NVPTXDwarfDebug.cpp
2122
NVPTXForwardParams.cpp
2223
NVPTXFrameLowering.cpp
2324
NVPTXGenericToNVVM.cpp

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "MCTargetDesc/NVPTXMCAsmInfo.h"
1818
#include "MCTargetDesc/NVPTXTargetStreamer.h"
1919
#include "NVPTX.h"
20+
#include "NVPTXDwarfDebug.h"
2021
#include "NVPTXMCExpr.h"
2122
#include "NVPTXMachineFunctionInfo.h"
2223
#include "NVPTXRegisterInfo.h"
@@ -676,6 +677,10 @@ void NVPTXAsmPrinter::emitStartOfAsmFile(Module &M) {
676677
OutStreamer->emitRawText(OS1.str());
677678
}
678679

680+
DwarfDebug *NVPTXAsmPrinter::createDwarfDebug() {
681+
return new NVPTXDwarfDebug(this);
682+
}
683+
679684
bool NVPTXAsmPrinter::doInitialization(Module &M) {
680685
const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
681686
const NVPTXSubtarget &STI = *NTM.getSubtargetImpl();

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
194194
bool doInitialization(Module &M) override;
195195
bool doFinalization(Module &M) override;
196196

197+
// Create NVPTX-specific DwarfDebug handler
198+
DwarfDebug *createDwarfDebug() override;
199+
197200
private:
198201
bool GlobalsEmitted;
199202

0 commit comments

Comments
 (0)