Skip to content

Commit 41bec02

Browse files
committed
Reapply "[DebugLine] Correct debug line emittion" (llvm#158343)
This reverts commit aabf18d.
1 parent aabf18d commit 41bec02

File tree

3 files changed

+64
-49
lines changed

3 files changed

+64
-49
lines changed

llvm/lib/MC/MCDwarf.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,37 +181,47 @@ void MCDwarfLineTable::emitOne(
181181

182182
unsigned FileNum, LastLine, Column, Flags, Isa, Discriminator;
183183
bool IsAtStartSeq;
184-
MCSymbol *LastLabel;
184+
MCSymbol *PrevLabel;
185185
auto init = [&]() {
186186
FileNum = 1;
187187
LastLine = 1;
188188
Column = 0;
189189
Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
190190
Isa = 0;
191191
Discriminator = 0;
192-
LastLabel = nullptr;
192+
PrevLabel = nullptr;
193193
IsAtStartSeq = true;
194194
};
195195
init();
196196

197197
// Loop through each MCDwarfLineEntry and encode the dwarf line number table.
198198
bool EndEntryEmitted = false;
199-
for (const MCDwarfLineEntry &LineEntry : LineEntries) {
200-
MCSymbol *Label = LineEntry.getLabel();
199+
for (auto It = LineEntries.begin(); It != LineEntries.end(); ++It) {
200+
auto LineEntry = *It;
201+
MCSymbol *CurrLabel = LineEntry.getLabel();
201202
const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
202203

203204
if (LineEntry.LineStreamLabel) {
204205
if (!IsAtStartSeq) {
205-
MCOS->emitDwarfLineEndEntry(Section, LastLabel,
206-
/*EndLabel =*/LastLabel);
206+
auto *Label = CurrLabel;
207+
auto NextIt = It + 1;
208+
// LineEntry with a null Label is probably a fake LineEntry we added
209+
// when `-emit-func-debug-line-table-offsets` in order to terminate the
210+
// sequence. Look for the next Label if possible, otherwise we will set
211+
// the PC to the end of the section.
212+
if (!Label && NextIt != LineEntries.end()) {
213+
Label = NextIt->getLabel();
214+
}
215+
MCOS->emitDwarfLineEndEntry(Section, PrevLabel,
216+
/*EndLabel =*/Label);
207217
init();
208218
}
209219
MCOS->emitLabel(LineEntry.LineStreamLabel, LineEntry.StreamLabelDefLoc);
210220
continue;
211221
}
212222

213223
if (LineEntry.IsEndEntry) {
214-
MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, Label,
224+
MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, PrevLabel, CurrLabel,
215225
asmInfo->getCodePointerSize());
216226
init();
217227
EndEntryEmitted = true;
@@ -258,12 +268,12 @@ void MCDwarfLineTable::emitOne(
258268
// At this point we want to emit/create the sequence to encode the delta in
259269
// line numbers and the increment of the address from the previous Label
260270
// and the current Label.
261-
MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
271+
MCOS->emitDwarfAdvanceLineAddr(LineDelta, PrevLabel, CurrLabel,
262272
asmInfo->getCodePointerSize());
263273

264274
Discriminator = 0;
265275
LastLine = LineEntry.getLine();
266-
LastLabel = Label;
276+
PrevLabel = CurrLabel;
267277
IsAtStartSeq = false;
268278
}
269279

@@ -273,7 +283,7 @@ void MCDwarfLineTable::emitOne(
273283
// does not track ranges nor terminate the line table. In that case,
274284
// conservatively use the section end symbol to end the line table.
275285
if (!EndEntryEmitted && !IsAtStartSeq)
276-
MCOS->emitDwarfLineEndEntry(Section, LastLabel);
286+
MCOS->emitDwarfLineEndEntry(Section, PrevLabel);
277287
}
278288

279289
void MCDwarfLineTable::endCurrentSeqAndEmitLineStreamLabel(MCStreamer *MCOS,

llvm/test/DebugInfo/X86/DW_AT_LLVM_stmt_seq_sec_offset.ll

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
; STMT_SEQ: DW_AT_LLVM_stmt_sequence [DW_FORM_sec_offset] (0x00000043)
1515
; STMT_SEQ: DW_AT_name {{.*}}func01
1616
; STMT_SEQ: DW_TAG_subprogram [[[ABBREV_CODE2]]]
17-
; STMT_SEQ: DW_AT_LLVM_stmt_sequence [DW_FORM_sec_offset] (0x00000056)
17+
; STMT_SEQ: DW_AT_LLVM_stmt_sequence [DW_FORM_sec_offset] (0x00000058)
1818
; STMT_SEQ: DW_AT_name {{.*}}main
1919

2020
;; Check the entire line sequence to see that it's correct
@@ -29,22 +29,23 @@
2929
; STMT_SEQ-NEXT: 0x00000050: 05 DW_LNS_set_column (3)
3030
; STMT_SEQ-NEXT: 0x00000052: 67 address += 6, line += 1, op-index += 0
3131
; STMT_SEQ-NEXT: 0x0000000000000006 6 3 0 0 0 0 is_stmt
32-
; STMT_SEQ-NEXT: 0x00000053: 00 DW_LNE_end_sequence
33-
; STMT_SEQ-NEXT: 0x0000000000000006 6 3 0 0 0 0 is_stmt end_sequence
34-
; STMT_SEQ-NEXT: 0x00000056: 04 DW_LNS_set_file (0)
35-
; STMT_SEQ-NEXT: 0x00000058: 00 DW_LNE_set_address (0x00000008)
36-
; STMT_SEQ-NEXT: 0x0000005f: 03 DW_LNS_advance_line (10)
37-
; STMT_SEQ-NEXT: 0x00000061: 01 DW_LNS_copy
32+
; STMT_SEQ-NEXT: 0x00000053: 02 DW_LNS_advance_pc (addr += 2, op-index += 0)
33+
; STMT_SEQ-NEXT: 0x00000055: 00 DW_LNE_end_sequence
34+
; STMT_SEQ-NEXT: 0x0000000000000008 6 3 0 0 0 0 is_stmt end_sequence
35+
; STMT_SEQ-NEXT: 0x00000058: 04 DW_LNS_set_file (0)
36+
; STMT_SEQ-NEXT: 0x0000005a: 00 DW_LNE_set_address (0x00000008)
37+
; STMT_SEQ-NEXT: 0x00000061: 03 DW_LNS_advance_line (10)
38+
; STMT_SEQ-NEXT: 0x00000063: 01 DW_LNS_copy
3839
; STMT_SEQ-NEXT: 0x0000000000000008 10 0 0 0 0 0 is_stmt
39-
; STMT_SEQ-NEXT: 0x00000062: 05 DW_LNS_set_column (10)
40-
; STMT_SEQ-NEXT: 0x00000064: 0a DW_LNS_set_prologue_end
41-
; STMT_SEQ-NEXT: 0x00000065: 83 address += 8, line += 1, op-index += 0
40+
; STMT_SEQ-NEXT: 0x00000064: 05 DW_LNS_set_column (10)
41+
; STMT_SEQ-NEXT: 0x00000066: 0a DW_LNS_set_prologue_end
42+
; STMT_SEQ-NEXT: 0x00000067: 83 address += 8, line += 1, op-index += 0
4243
; STMT_SEQ-NEXT: 0x0000000000000010 11 10 0 0 0 0 is_stmt prologue_end
43-
; STMT_SEQ-NEXT: 0x00000066: 05 DW_LNS_set_column (3)
44-
; STMT_SEQ-NEXT: 0x00000068: 9f address += 10, line += 1, op-index += 0
44+
; STMT_SEQ-NEXT: 0x00000068: 05 DW_LNS_set_column (3)
45+
; STMT_SEQ-NEXT: 0x0000006a: 9f address += 10, line += 1, op-index += 0
4546
; STMT_SEQ-NEXT: 0x000000000000001a 12 3 0 0 0 0 is_stmt
46-
; STMT_SEQ-NEXT: 0x00000069: 02 DW_LNS_advance_pc (addr += 5, op-index += 0)
47-
; STMT_SEQ-NEXT: 0x0000006b: 00 DW_LNE_end_sequence
47+
; STMT_SEQ-NEXT: 0x0000006b: 02 DW_LNS_advance_pc (addr += 5, op-index += 0)
48+
; STMT_SEQ-NEXT: 0x0000006d: 00 DW_LNE_end_sequence
4849
; STMT_SEQ-NEXT: 0x000000000000001f 12 3 0 0 0 0 is_stmt end_sequence
4950

5051
; generated from:

llvm/test/MC/ELF/debug-loc-label.s

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,47 @@
1717
# CHECK-LINE-TABLE-NEXT: 0x0000002a: 00 DW_LNE_set_address (0x0000000000000000)
1818
# CHECK-LINE-TABLE-NEXT: 0x00000035: 01 DW_LNS_copy
1919
# CHECK-LINE-TABLE-NEXT: 0x0000000000000000 1 1 1 0 0 0 is_stmt
20-
# CHECK-LINE-TABLE-NEXT: 0x00000036: 00 DW_LNE_end_sequence
21-
# CHECK-LINE-TABLE-NEXT: 0x0000000000000000 1 1 1 0 0 0 is_stmt end_sequence
22-
# CHECK-LINE-TABLE-NEXT: 0x00000039: 05 DW_LNS_set_column (2)
23-
# CHECK-LINE-TABLE-NEXT: 0x0000003b: 00 DW_LNE_set_address (0x0000000000000008)
24-
# CHECK-LINE-TABLE-NEXT: 0x00000046: 01 DW_LNS_copy
20+
# CHECK-LINE-TABLE-NEXT: 0x00000036: 02 DW_LNS_advance_pc (addr += 8, op-index += 0)
21+
# CHECK-LINE-TABLE-NEXT: 0x00000038: 00 DW_LNE_end_sequence
22+
# CHECK-LINE-TABLE-NEXT: 0x0000000000000008 1 1 1 0 0 0 is_stmt end_sequence
23+
# CHECK-LINE-TABLE-NEXT: 0x0000003b: 05 DW_LNS_set_column (2)
24+
# CHECK-LINE-TABLE-NEXT: 0x0000003d: 00 DW_LNE_set_address (0x0000000000000008)
25+
# CHECK-LINE-TABLE-NEXT: 0x00000048: 01 DW_LNS_copy
2526
# CHECK-LINE-TABLE-NEXT: 0x0000000000000008 1 2 1 0 0 0 is_stmt
26-
# CHECK-LINE-TABLE-NEXT: 0x00000047: 00 DW_LNE_end_sequence
27-
# CHECK-LINE-TABLE-NEXT: 0x0000000000000008 1 2 1 0 0 0 is_stmt end_sequence
28-
# CHECK-LINE-TABLE-NEXT: 0x0000004a: 05 DW_LNS_set_column (3)
29-
# CHECK-LINE-TABLE-NEXT: 0x0000004c: 00 DW_LNE_set_address (0x0000000000000010)
30-
# CHECK-LINE-TABLE-NEXT: 0x00000057: 01 DW_LNS_copy
27+
# CHECK-LINE-TABLE-NEXT: 0x00000049: 02 DW_LNS_advance_pc (addr += 8, op-index += 0)
28+
# CHECK-LINE-TABLE-NEXT: 0x0000004b: 00 DW_LNE_end_sequence
29+
# CHECK-LINE-TABLE-NEXT: 0x0000000000000010 1 2 1 0 0 0 is_stmt end_sequence
30+
# CHECK-LINE-TABLE-NEXT: 0x0000004e: 05 DW_LNS_set_column (3)
31+
# CHECK-LINE-TABLE-NEXT: 0x00000050: 00 DW_LNE_set_address (0x0000000000000010)
32+
# CHECK-LINE-TABLE-NEXT: 0x0000005b: 01 DW_LNS_copy
3133
# CHECK-LINE-TABLE-NEXT: 0x0000000000000010 1 3 1 0 0 0 is_stmt
32-
# CHECK-LINE-TABLE-NEXT: 0x00000058: 00 DW_LNE_end_sequence
33-
# CHECK-LINE-TABLE-NEXT: 0x0000000000000010 1 3 1 0 0 0 is_stmt end_sequence
34-
# CHECK-LINE-TABLE-NEXT: 0x0000005b: 05 DW_LNS_set_column (4)
35-
# CHECK-LINE-TABLE-NEXT: 0x0000005d: 00 DW_LNE_set_address (0x0000000000000018)
36-
# CHECK-LINE-TABLE-NEXT: 0x00000068: 01 DW_LNS_copy
34+
# CHECK-LINE-TABLE-NEXT: 0x0000005c: 02 DW_LNS_advance_pc (addr += 8, op-index += 0)
35+
# CHECK-LINE-TABLE-NEXT: 0x0000005e: 00 DW_LNE_end_sequence
36+
# CHECK-LINE-TABLE-NEXT: 0x0000000000000018 1 3 1 0 0 0 is_stmt end_sequence
37+
# CHECK-LINE-TABLE-NEXT: 0x00000061: 05 DW_LNS_set_column (4)
38+
# CHECK-LINE-TABLE-NEXT: 0x00000063: 00 DW_LNE_set_address (0x0000000000000018)
39+
# CHECK-LINE-TABLE-NEXT: 0x0000006e: 01 DW_LNS_copy
3740
# CHECK-LINE-TABLE-NEXT: 0x0000000000000018 1 4 1 0 0 0 is_stmt
38-
# CHECK-LINE-TABLE-NEXT: 0x00000069: 05 DW_LNS_set_column (5)
39-
# CHECK-LINE-TABLE-NEXT: 0x0000006b: 01 DW_LNS_copy
41+
# CHECK-LINE-TABLE-NEXT: 0x0000006f: 05 DW_LNS_set_column (5)
42+
# CHECK-LINE-TABLE-NEXT: 0x00000071: 01 DW_LNS_copy
4043
# CHECK-LINE-TABLE-NEXT: 0x0000000000000018 1 5 1 0 0 0 is_stmt
41-
# CHECK-LINE-TABLE-NEXT: 0x0000006c: 00 DW_LNE_end_sequence
42-
# CHECK-LINE-TABLE-NEXT: 0x0000000000000018 1 5 1 0 0 0 is_stmt end_sequence
44+
# CHECK-LINE-TABLE-NEXT: 0x00000072: 02 DW_LNS_advance_pc (addr += 8, op-index += 0)
45+
# CHECK-LINE-TABLE-NEXT: 0x00000074: 00 DW_LNE_end_sequence
46+
# CHECK-LINE-TABLE-NEXT: 0x0000000000000020 1 5 1 0 0 0 is_stmt end_sequence
4347

4448
# CHECK-SYM: Symbol table '.symtab' contains 9 entries:
4549
# CHECK-SYM-NEXT: Num: Value Size Type Bind Vis Ndx Name
4650
# CHECK-SYM-NEXT: 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
4751
# CHECK-SYM-NEXT: 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
4852
# CHECK-SYM-NEXT: 2: 0000000000000000 0 SECTION LOCAL DEFAULT 2 .text
49-
# CHECK-SYM-NEXT: 3: 0000000000000039 0 NOTYPE LOCAL DEFAULT 3 my_label_02
50-
# CHECK-SYM-NEXT: 4: 000000000000004a 0 NOTYPE LOCAL DEFAULT 3 my_label_03
51-
# CHECK-SYM-NEXT: 5: 000000000000005b 0 NOTYPE LOCAL DEFAULT 3 my_label_04
52-
# CHECK-SYM-NEXT: 6: 000000000000004a 0 NOTYPE LOCAL DEFAULT 3 my_label_03.1
53-
# CHECK-SYM-NEXT: 7: 000000000000006f 0 NOTYPE LOCAL DEFAULT 3 my_label_05
53+
# CHECK-SYM-NEXT: 3: 000000000000003b 0 NOTYPE LOCAL DEFAULT 3 my_label_02
54+
# CHECK-SYM-NEXT: 4: 000000000000004e 0 NOTYPE LOCAL DEFAULT 3 my_label_03
55+
# CHECK-SYM-NEXT: 5: 0000000000000061 0 NOTYPE LOCAL DEFAULT 3 my_label_04
56+
# CHECK-SYM-NEXT: 6: 000000000000004e 0 NOTYPE LOCAL DEFAULT 3 my_label_03.1
57+
# CHECK-SYM-NEXT: 7: 0000000000000077 0 NOTYPE LOCAL DEFAULT 3 my_label_05
5458
# CHECK-SYM-NEXT: 8: 0000000000000000 0 FUNC GLOBAL DEFAULT 2 foo
5559

56-
# CHECK-OFFSETS: 0000 39000000 4a000000 5b000000
60+
# CHECK-OFFSETS: 0000 3b000000 4e000000 61000000
5761

5862
.text
5963
.file "test.c"

0 commit comments

Comments
 (0)