Skip to content

Commit 03e17da

Browse files
omern1OCHyams
andauthored
[DWARF] Emit line 0 source locations for BB padding nops (#99496)
This patch makes LLVM emit line 0 source locations for nops emitted as basic block padding. --------- Co-authored-by: Orlando Cazalet-Hyams <[email protected]>
1 parent 0362a29 commit 03e17da

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

llvm/include/llvm/CodeGen/DebugHandlerBase.h

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class DebugHandlerBase {
137137
void beginBasicBlockSection(const MachineBasicBlock &MBB);
138138
void endBasicBlockSection(const MachineBasicBlock &MBB);
139139

140+
virtual void beginCodeAlignment(const MachineBasicBlock &MBB) {}
141+
140142
/// Return Label preceding the instruction.
141143
MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
142144

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3967,6 +3967,9 @@ void AsmPrinter::emitBasicBlockStart(const MachineBasicBlock &MBB) {
39673967
CurrentSectionBeginSym = MBB.getSymbol();
39683968
}
39693969

3970+
for (auto &Handler : DebugHandlers)
3971+
Handler->beginCodeAlignment(MBB);
3972+
39703973
// Emit an alignment directive for this block, if needed.
39713974
const Align Alignment = MBB.getAlignment();
39723975
if (Alignment != Align(1))

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -3674,3 +3674,21 @@ bool DwarfDebug::alwaysUseRanges(const DwarfCompileUnit &CU) const {
36743674
return true;
36753675
return false;
36763676
}
3677+
3678+
void DwarfDebug::beginCodeAlignment(const MachineBasicBlock &MBB) {
3679+
if (MBB.getAlignment() == Align(1))
3680+
return;
3681+
3682+
auto *SP = MBB.getParent()->getFunction().getSubprogram();
3683+
bool NoDebug =
3684+
!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug;
3685+
3686+
if (NoDebug)
3687+
return;
3688+
3689+
auto PrevLoc = Asm->OutStreamer->getContext().getCurrentDwarfLoc();
3690+
Asm->OutStreamer->emitDwarfLocDirective(
3691+
PrevLoc.getFileNum(), 0, PrevLoc.getColumn(), 0, 0, 0, StringRef());
3692+
MCDwarfLineEntry::make(Asm->OutStreamer.get(),
3693+
Asm->OutStreamer->getCurrentSectionOnly());
3694+
}

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h

+3
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,9 @@ class DwarfDebug : public DebugHandlerBase {
730730
/// Process beginning of an instruction.
731731
void beginInstruction(const MachineInstr *MI) override;
732732

733+
/// Process beginning of code alignment.
734+
void beginCodeAlignment(const MachineBasicBlock &MBB) override;
735+
733736
/// Perform an MD5 checksum of \p Identifier and return the lower 64 bits.
734737
static uint64_t makeTypeSignature(StringRef Identifier);
735738

llvm/test/CodeGen/X86/fsafdo_test1.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
; Check that fs-afdo discriminators are generated.
55
; V01: .loc 1 7 3 is_stmt 0 discriminator 2 # foo.c:7:3
66
; V01: .loc 1 9 5 is_stmt 1 discriminator 2 # foo.c:9:5
7-
; V0: .loc 1 9 5 is_stmt 0 discriminator 11266 # foo.c:9:5
7+
; V0: .loc 1 9 5 discriminator 11266 # foo.c:9:5
88
; V0: .loc 1 7 3 is_stmt 1 discriminator 11266 # foo.c:7:3
9-
; V1: .loc 1 9 5 is_stmt 0 discriminator 514 # foo.c:9:5
9+
; V1: .loc 1 9 5 discriminator 514 # foo.c:9:5
1010
; V1: .loc 1 7 3 is_stmt 1 discriminator 258 # foo.c:7:3
1111
; Check that variable __llvm_fs_discriminator__ is generated.
1212
; V01: .type __llvm_fs_discriminator__,@object # @__llvm_fs_discriminator__

llvm/test/CodeGen/X86/fsafdo_test4.ll

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=false < %s | FileCheck %s
2-
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true < %s | FileCheck %s
1+
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=false < %s | FileCheck --implicit-check-not=.loc %s
2+
; RUN: llc -enable-fs-discriminator -improved-fs-discriminator=true < %s | FileCheck --implicit-check-not=.loc %s
33
;
44
; Check that fs-afdo discriminators are NOT generated, as debugInfoForProfiling is false (not set).
5+
; CHECK: .loc 1 7 15 prologue_end discriminator 2 # foo.c:7:15
56
; CHECK: .loc 1 7 3 is_stmt 0 discriminator 2 # foo.c:7:3
7+
; CHECK: .loc 1 0 3 # foo.c:0:3
68
; CHECK: .loc 1 9 5 is_stmt 1 discriminator 2 # foo.c:9:5
7-
; CHECK-NOT: .loc 1 9 5 is_stmt 0 discriminator
8-
; CHECK-NOT: .loc 1 7 3 is_stmt 1 discriminator
9+
; CHECK: .loc 1 0 5 is_stmt 0 # :0:5
10+
; CHECK: .loc 1 9 5 discriminator 2 # foo.c:9:5
11+
; CHECK: .loc 1 0 5 # :0:5
12+
; CHECK: .loc 1 7 3 is_stmt 1 discriminator 2 # foo.c:7:3
13+
; CHECK: .loc 1 14 3 # foo.c:14:3
914
; Check that variable __llvm_fs_discriminator__ is NOT generated.
1015
; CHECK-NOT: __llvm_fs_discriminator__:
1116

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; RUN: llc %s --filetype=obj -o %t
2+
; RUN: llvm-objdump -d %t | FileCheck %s --check-prefixes=OBJ
3+
; RUN: llvm-dwarfdump --debug-line %t | FileCheck %s --check-prefixes=DBG
4+
; RUN: llc %s -o - | FileCheck %s --check-prefixes=ASM
5+
6+
; OBJ: 1:{{.*}}nop
7+
8+
;; Address Line Column File ISA Discriminator OpIndex Flags
9+
; DBG: 0x0000000000000000 3 0 0 0 0 0 is_stmt
10+
; DBG: 0x0000000000000001 0 0 0 0 0 0
11+
; DBG: 0x0000000000000010 5 0 0 0 0 0 is_stmt prologue_end
12+
; DBG: 0x0000000000000017 5 0 0 0 0 0 is_stmt end_sequence
13+
14+
; ASM: .loc 0 0 0 is_stmt 0
15+
; ASM-NEXT: .L{{.*}}:
16+
; ASM-NEXT: .p2align 4, 0x90
17+
18+
;; $ cat test.cpp
19+
;; void g();
20+
;; void f() {
21+
;; [[clang::code_align(16)]]
22+
;; while (1) { g(); }
23+
;; }
24+
25+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
26+
target triple = "x86_64-unknown-linux-gnu"
27+
28+
define dso_local void @f() local_unnamed_addr !dbg !9 {
29+
entry:
30+
br label %while.body, !dbg !12
31+
32+
while.body: ; preds = %entry, %while.body
33+
tail call void @g(), !dbg !12
34+
br label %while.body, !dbg !12, !llvm.loop !13
35+
}
36+
37+
declare !dbg !16 void @g() local_unnamed_addr
38+
39+
!llvm.dbg.cu = !{!0}
40+
!llvm.module.flags = !{!2, !3}
41+
!llvm.ident = !{!8}
42+
43+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 19.0.0git", isOptimized: true, runtimeVersion: 0, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
44+
!1 = !DIFile(filename: "test.cpp", directory: "/")
45+
!2 = !{i32 7, !"Dwarf Version", i32 5}
46+
!3 = !{i32 2, !"Debug Info Version", i32 3}
47+
!8 = !{!"clang version 19.0.0git"}
48+
!9 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 3, type: !10, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
49+
!10 = !DISubroutineType(types: !11)
50+
!11 = !{}
51+
!12 = !DILocation(line: 5, scope: !9)
52+
!13 = distinct !{!13, !12, !12, !14, !15}
53+
!14 = !{!"llvm.loop.mustprogress"}
54+
!15 = !{!"llvm.loop.align", i32 16}
55+
!16 = !DISubprogram(name: "g", scope: !1, file: !1, line: 2, type: !10, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)

0 commit comments

Comments
 (0)