aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCDwarf.cpp
diff options
context:
space:
mode:
authorPeter Rong <peterrong96@gmail.com>2025-09-16 12:22:42 -0700
committerGitHub <noreply@github.com>2025-09-16 12:22:42 -0700
commitb7bf9dd9ae20aacf2f82b14c08c7316a588ba90c (patch)
treedfa0f3403e4f90b5a7bbea28ac97e81c64a2536e /llvm/lib/MC/MCDwarf.cpp
parent26549262b3b7ad265d25586be4dca38d37d26069 (diff)
downloadllvm-b7bf9dd9ae20aacf2f82b14c08c7316a588ba90c.zip
llvm-b7bf9dd9ae20aacf2f82b14c08c7316a588ba90c.tar.gz
llvm-b7bf9dd9ae20aacf2f82b14c08c7316a588ba90c.tar.bz2
Reapply "[DebugLine] Correct debug line emittion" (#158343) (#158379)
This reverts commit aabf18d7184298566993e3141606cd79ff617d2d. #157529 included a test that used clang, which doesn't exists in some CI. #158343 reverts it. This PR reapplies the original patch with the incorrect test removed. ### Context #99710 introduced `.loc_label` so we can terminate a line sequence. However, it did not advance PC properly. This is problematic for 1-instruction functions as it will have zero-length sequence. The test checked in that PR shows the problem: ``` # CHECK-LINE-TABLE: Address Line Column File ISA Discriminator OpIndex Flags # CHECK-LINE-TABLE-NEXT: ------------------ ------ ------ ------ --- ------------- ------- ------------- # CHECK-LINE-TABLE-NEXT: 0x00000028: 05 DW_LNS_set_column (1) # CHECK-LINE-TABLE-NEXT: 0x0000002a: 00 DW_LNE_set_address (0x0000000000000000) # CHECK-LINE-TABLE-NEXT: 0x00000035: 01 DW_LNS_copy # CHECK-LINE-TABLE-NEXT: 0x0000000000000000 1 1 1 0 0 0 is_stmt # CHECK-LINE-TABLE-NEXT: 0x00000036: 00 DW_LNE_end_sequence # CHECK-LINE-TABLE-NEXT: 0x0000000000000000 1 1 1 0 0 0 is_stmt end_sequence ``` Both rows having PC 0x0 is incorrect, and parsers won't be able to parse them. See more explanation why this is wrong in #154851. ### Design This PR attempts to fix this by advancing the PC to the next available Label, and advance to the end of the section if no Label is available. ### Implementation - `emitDwarfLineEndEntry` will advance PC to the `CurrLabel` - If `CurrLabel` is null, its probably a fake LineEntry we introduced in #110192. In that case look for the next Label - If still not label can be found, use `null` and `emitDwarfLineEndEntry` is smart enough to advance PC to the end of the section - Rename `LastLabel` to `PrevLabel`, "last" can mean "previous" or "final", this is ambigous. - Updated the tests to emit a correct label. ### Note This fix should render #154986 and #154851 obsolete, they were temporary fixes and don't resolve the root cause.
Diffstat (limited to 'llvm/lib/MC/MCDwarf.cpp')
-rw-r--r--llvm/lib/MC/MCDwarf.cpp30
1 files changed, 20 insertions, 10 deletions
diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp
index e7c0d37..e8f000a 100644
--- a/llvm/lib/MC/MCDwarf.cpp
+++ b/llvm/lib/MC/MCDwarf.cpp
@@ -181,7 +181,7 @@ void MCDwarfLineTable::emitOne(
unsigned FileNum, LastLine, Column, Flags, Isa, Discriminator;
bool IsAtStartSeq;
- MCSymbol *LastLabel;
+ MCSymbol *PrevLabel;
auto init = [&]() {
FileNum = 1;
LastLine = 1;
@@ -189,21 +189,31 @@ void MCDwarfLineTable::emitOne(
Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
Isa = 0;
Discriminator = 0;
- LastLabel = nullptr;
+ PrevLabel = nullptr;
IsAtStartSeq = true;
};
init();
// Loop through each MCDwarfLineEntry and encode the dwarf line number table.
bool EndEntryEmitted = false;
- for (const MCDwarfLineEntry &LineEntry : LineEntries) {
- MCSymbol *Label = LineEntry.getLabel();
+ for (auto It = LineEntries.begin(); It != LineEntries.end(); ++It) {
+ auto LineEntry = *It;
+ MCSymbol *CurrLabel = LineEntry.getLabel();
const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
if (LineEntry.LineStreamLabel) {
if (!IsAtStartSeq) {
- MCOS->emitDwarfLineEndEntry(Section, LastLabel,
- /*EndLabel =*/LastLabel);
+ auto *Label = CurrLabel;
+ auto NextIt = It + 1;
+ // LineEntry with a null Label is probably a fake LineEntry we added
+ // when `-emit-func-debug-line-table-offsets` in order to terminate the
+ // sequence. Look for the next Label if possible, otherwise we will set
+ // the PC to the end of the section.
+ if (!Label && NextIt != LineEntries.end()) {
+ Label = NextIt->getLabel();
+ }
+ MCOS->emitDwarfLineEndEntry(Section, PrevLabel,
+ /*EndLabel =*/Label);
init();
}
MCOS->emitLabel(LineEntry.LineStreamLabel, LineEntry.StreamLabelDefLoc);
@@ -211,7 +221,7 @@ void MCDwarfLineTable::emitOne(
}
if (LineEntry.IsEndEntry) {
- MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, Label,
+ MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, PrevLabel, CurrLabel,
asmInfo->getCodePointerSize());
init();
EndEntryEmitted = true;
@@ -258,12 +268,12 @@ void MCDwarfLineTable::emitOne(
// At this point we want to emit/create the sequence to encode the delta in
// line numbers and the increment of the address from the previous Label
// and the current Label.
- MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
+ MCOS->emitDwarfAdvanceLineAddr(LineDelta, PrevLabel, CurrLabel,
asmInfo->getCodePointerSize());
Discriminator = 0;
LastLine = LineEntry.getLine();
- LastLabel = Label;
+ PrevLabel = CurrLabel;
IsAtStartSeq = false;
}
@@ -273,7 +283,7 @@ void MCDwarfLineTable::emitOne(
// does not track ranges nor terminate the line table. In that case,
// conservatively use the section end symbol to end the line table.
if (!EndEntryEmitted && !IsAtStartSeq)
- MCOS->emitDwarfLineEndEntry(Section, LastLabel);
+ MCOS->emitDwarfLineEndEntry(Section, PrevLabel);
}
void MCDwarfLineTable::endCurrentSeqAndEmitLineStreamLabel(MCStreamer *MCOS,