Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit d3046a2

Browse files
committed
[DEBUG_INFO, NVPTX] Fix relocation info.
Summary: Initial function labels must follow the debug location for the correct relocation info generation. Reviewers: tra, jlebar, echristo Subscribers: jholewinski, llvm-commits Differential Revision: https://reviews.llvm.org/D45784 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351843 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 880b045 commit d3046a2

File tree

7 files changed

+66
-24
lines changed

7 files changed

+66
-24
lines changed

include/llvm/CodeGen/AsmPrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class AsmPrinter : public MachineFunctionPass {
226226

227227
void EmitToStreamer(MCStreamer &S, const MCInst &Inst);
228228

229+
/// Emits inital debug location directive.
230+
void emitInitialRawDwarfLocDirective(const MachineFunction &MF);
231+
229232
/// Return the current section we are emitting to.
230233
const MCSection *getCurrentSection() const;
231234

lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ void AsmPrinter::EmitToStreamer(MCStreamer &S, const MCInst &Inst) {
231231
S.EmitInstruction(Inst, getSubtargetInfo());
232232
}
233233

234+
void AsmPrinter::emitInitialRawDwarfLocDirective(const MachineFunction &MF) {
235+
assert(DD && "Dwarf debug file is not defined.");
236+
assert(OutStreamer->hasRawTextSupport() && "Expected assembly output mode.");
237+
(void)DD->emitInitialLocDirective(MF, /*CUID=*/0);
238+
}
239+
234240
/// getCurrentSection() - Return the current section we are emitting to.
235241
const MCSection *AsmPrinter::getCurrentSection() const {
236242
return OutStreamer->getCurrentSectionOnly();

lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,46 @@ static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
15201520
return DebugLoc();
15211521
}
15221522

1523+
/// Register a source line with debug info. Returns the unique label that was
1524+
/// emitted and which provides correspondence to the source line list.
1525+
static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col,
1526+
const MDNode *S, unsigned Flags, unsigned CUID,
1527+
uint16_t DwarfVersion,
1528+
ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) {
1529+
StringRef Fn;
1530+
unsigned FileNo = 1;
1531+
unsigned Discriminator = 0;
1532+
if (auto *Scope = cast_or_null<DIScope>(S)) {
1533+
Fn = Scope->getFilename();
1534+
if (Line != 0 && DwarfVersion >= 4)
1535+
if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1536+
Discriminator = LBF->getDiscriminator();
1537+
1538+
FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID])
1539+
.getOrCreateSourceID(Scope->getFile());
1540+
}
1541+
Asm.OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
1542+
Discriminator, Fn);
1543+
}
1544+
1545+
DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF,
1546+
unsigned CUID) {
1547+
// Get beginning of function.
1548+
if (DebugLoc PrologEndLoc = findPrologueEndLoc(&MF)) {
1549+
// Ensure the compile unit is created if the function is called before
1550+
// beginFunction().
1551+
(void)getOrCreateDwarfCompileUnit(
1552+
MF.getFunction().getSubprogram()->getUnit());
1553+
// We'd like to list the prologue as "not statements" but GDB behaves
1554+
// poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1555+
const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
1556+
::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT,
1557+
CUID, getDwarfVersion(), getUnits());
1558+
return PrologEndLoc;
1559+
}
1560+
return DebugLoc();
1561+
}
1562+
15231563
// Gather pre-function debug information. Assumes being called immediately
15241564
// after the function entry point has been emitted.
15251565
void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
@@ -1542,13 +1582,8 @@ void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
15421582
Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
15431583

15441584
// Record beginning of function.
1545-
PrologEndLoc = findPrologueEndLoc(MF);
1546-
if (PrologEndLoc) {
1547-
// We'd like to list the prologue as "not statements" but GDB behaves
1548-
// poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1549-
auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
1550-
recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1551-
}
1585+
PrologEndLoc = emitInitialLocDirective(
1586+
*MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID());
15521587
}
15531588

15541589
void DwarfDebug::skippedNonDebugFunction() {
@@ -1646,21 +1681,9 @@ void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
16461681
// emitted and which provides correspondence to the source line list.
16471682
void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
16481683
unsigned Flags) {
1649-
StringRef Fn;
1650-
unsigned FileNo = 1;
1651-
unsigned Discriminator = 0;
1652-
if (auto *Scope = cast_or_null<DIScope>(S)) {
1653-
Fn = Scope->getFilename();
1654-
if (Line != 0 && getDwarfVersion() >= 4)
1655-
if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1656-
Discriminator = LBF->getDiscriminator();
1657-
1658-
unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1659-
FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1660-
.getOrCreateSourceID(Scope->getFile());
1661-
}
1662-
Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
1663-
Discriminator, Fn);
1684+
::recordSourceLine(*Asm, Line, Col, S, Flags,
1685+
Asm->OutStreamer->getContext().getDwarfCompileUnitID(),
1686+
getDwarfVersion(), getUnits());
16641687
}
16651688

16661689
//===----------------------------------------------------------------------===//

lib/CodeGen/AsmPrinter/DwarfDebug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ class DwarfDebug : public DebugHandlerBase {
592592
/// Emit all Dwarf sections that should come after the content.
593593
void endModule() override;
594594

595+
/// Emits inital debug location directive.
596+
DebugLoc emitInitialLocDirective(const MachineFunction &MF, unsigned CUID);
597+
595598
/// Process beginning of an instruction.
596599
void beginInstruction(const MachineInstr *MI) override;
597600

lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
472472
// Emit open brace for function body.
473473
OutStreamer->EmitRawText(StringRef("{\n"));
474474
setAndEmitFunctionVirtualRegisters(*MF);
475+
// Emit initial .loc debug directive for correct relocation symbol data.
476+
if (MMI && MMI->hasDebugInfo())
477+
emitInitialRawDwarfLocDirective(*MF);
475478
}
476479

477480
bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
; CHECK: .param .b32 b_param_0
77
; CHECK: )
88
; CHECK: {
9+
; CHECK: .loc 1 1 0
910
; CHECK: Lfunc_begin0:
1011
; CHECK: .loc 1 1 0
1112
; CHECK: .loc 1 1 0
@@ -27,6 +28,7 @@
2728
; CHECK: .param .b32 d_param_0
2829
; CHECK: )
2930
; CHECK: {
31+
; CHECK: .loc 1 3 0
3032
; CHECK: Lfunc_begin2:
3133
; CHECK: .loc 1 3 0
3234
; CHECK: ret;

test/DebugInfo/NVPTX/debug-loc-offset.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
; CHECK: .visible .func (.param .b32 func_retval0) _Z3bari(
1313
; CHECK: {
14-
; CHECK: Lfunc_begin0:
1514
; CHECK: .loc [[CU1:[0-9]+]] 1 0
15+
; CHECK: Lfunc_begin0:
16+
; CHECK: .loc [[CU1]] 1 0
1617

1718
; CHECK: //DEBUG_VALUE: bar:b <- {{[0-9]+}}
1819
; CHECK: //DEBUG_VALUE: bar:b <- {{[0-9]+}}
@@ -39,8 +40,9 @@ declare void @llvm.dbg.value(metadata, metadata, metadata) #1
3940

4041
; CHECK: .visible .func _Z3baz1A(
4142
; CHECK: {
42-
; CHECK: Lfunc_begin1:
4343
; CHECK: .loc [[CU2:[0-9]+]] 6 0
44+
; CHECK: Lfunc_begin1:
45+
; CHECK: .loc [[CU2]] 6 0
4446
; CHECK: //DEBUG_VALUE: baz:z <- {{[0-9]+}}
4547
; CHECK: //DEBUG_VALUE: baz:z <- {{[0-9]+}}
4648
; CHECK: .loc [[CU2]] 10 0

0 commit comments

Comments
 (0)