Skip to content

Commit e0c2148

Browse files
committed
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: llvm#127944
1 parent 701da0d commit e0c2148

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())
@@ -2063,6 +2067,9 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
20632067
}
20642068
// TODO: DBG_LABEL
20652069

2070+
if (PrintMIAddrs)
2071+
OS << " ; " << this;
2072+
20662073
if (AddNewLine)
20672074
OS << '\n';
20682075
}

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

@@ -4234,6 +4242,18 @@ void AssemblyWriter::printInfoComment(const Value &V) {
42344242
if (AnnotationWriter) {
42354243
AnnotationWriter->printInfoComment(V, Out);
42364244
}
4245+
4246+
if (PrintInstDebugLocs) {
4247+
if (auto *I = dyn_cast<Instruction>(&V)) {
4248+
if (I->getDebugLoc()) {
4249+
Out << " ; ";
4250+
I->getDebugLoc().print(Out);
4251+
}
4252+
}
4253+
}
4254+
4255+
if (PrintInstAddrs)
4256+
Out << " ; " << &V;
42374257
}
42384258

42394259
static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
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)