Skip to content

Commit 0ebf7b4

Browse files
authored
IR, CodeGen: Add command line flags for dumping instruction addresses and debug locations.
As previously discussed [1], it is sometimes useful to be able to see instruction addresses and debug locations as part of IR dumps. The same applies to MachineInstrs which already dump debug locations but not addresses. Therefore add some flags that can be used to enable dumping of this information. [1] https://discourse.llvm.org/t/small-improvement-to-llvm-debugging-experience/79914 Reviewers: rnk Reviewed By: rnk Pull Request: #127944
1 parent 11e65b9 commit 0ebf7b4

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363

6464
using namespace llvm;
6565

66+
static cl::opt<bool>
67+
PrintMIAddrs("print-mi-addrs", cl::Hidden,
68+
cl::desc("Print addresses of MachineInstrs when dumping"));
69+
6670
static const MachineFunction *getMFIfAvailable(const MachineInstr &MI) {
6771
if (const MachineBasicBlock *MBB = MI.getParent())
6872
if (const MachineFunction *MF = MBB->getParent())
@@ -2076,6 +2080,9 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
20762080
}
20772081
// TODO: DBG_LABEL
20782082

2083+
if (PrintMIAddrs)
2084+
OS << " ; " << this;
2085+
20792086
if (AddNewLine)
20802087
OS << '\n';
20812088
}

llvm/lib/IR/AsmWriter.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@
8888

8989
using namespace llvm;
9090

91+
static cl::opt<bool>
92+
PrintInstAddrs("print-inst-addrs", cl::Hidden,
93+
cl::desc("Print addresses of instructions when dumping"));
94+
95+
static cl::opt<bool> PrintInstDebugLocs(
96+
"print-inst-debug-locs", cl::Hidden,
97+
cl::desc("Pretty print debug locations of instructions when dumping"));
98+
9199
// Make virtual table appear in this compilation unit.
92100
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
93101

@@ -4256,6 +4264,18 @@ void AssemblyWriter::printInfoComment(const Value &V) {
42564264
if (AnnotationWriter) {
42574265
AnnotationWriter->printInfoComment(V, Out);
42584266
}
4267+
4268+
if (PrintInstDebugLocs) {
4269+
if (auto *I = dyn_cast<Instruction>(&V)) {
4270+
if (I->getDebugLoc()) {
4271+
Out << " ; ";
4272+
I->getDebugLoc().print(Out);
4273+
}
4274+
}
4275+
}
4276+
4277+
if (PrintInstAddrs)
4278+
Out << " ; " << &V;
42594279
}
42604280

42614281
static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,

llvm/test/Other/print-inst-addrs.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: opt -S -print-inst-addrs %s | FileCheck %s
2+
3+
define void @foo() {
4+
; CHECK: ret void ; 0x
5+
ret void
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: opt -S -print-inst-debug-locs < %s | FileCheck %s
2+
3+
define weak i32 @foo(i32 %a, i32 %b) !dbg !3 {
4+
entry:
5+
; CHECK: call {{.*}} ; foo.c:52
6+
%sum = call i32 @fastadd(i32 %a, i32 %b), !dbg !DILocation(line: 52, scope: !3)
7+
; CHECK: ret {{.*}} ; foo.c:53
8+
ret i32 %sum, !dbg !DILocation(line: 53, scope: !3)
9+
}
10+
11+
declare i32 @fastadd(i32, i32)
12+
13+
!llvm.module.flags = !{!0}
14+
!0 = !{i32 2, !"Debug Info Version", i32 3}
15+
16+
!llvm.dbg.cu = !{!1}
17+
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
18+
!2 = !DIFile(filename: "foo.c", directory: "/path/to/dir")
19+
!3 = distinct !DISubprogram(file: !2, scope: !2, line: 51, name: "foo", type: !4, unit: !1)
20+
!4 = !DISubroutineType(types: !{})

llvm/test/Other/print-mi-addrs.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: llc -print-after=slotindexes -print-mi-addrs < %s 2>&1 | FileCheck %s
2+
; REQUIRES: default_triple
3+
4+
; CHECK: IR Dump {{.*}}
5+
; CHECK: # Machine code for function foo{{.*}}
6+
7+
define void @foo() {
8+
; CHECK: ; 0x
9+
ret void
10+
}
11+

0 commit comments

Comments
 (0)