Skip to content

IR, CodeGen: Add command line flags for dumping instruction addresses and debug locations. #127944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

pcc
Copy link
Contributor

@pcc pcc commented Feb 20, 2025

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

Created using spr 1.3.6-beta.1
@llvmbot
Copy link
Member

llvmbot commented Feb 20, 2025

@llvm/pr-subscribers-llvm-ir

Author: Peter Collingbourne (pcc)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/127944.diff

5 Files Affected:

  • (modified) llvm/lib/CodeGen/MachineInstr.cpp (+7)
  • (modified) llvm/lib/IR/AsmWriter.cpp (+20)
  • (added) llvm/test/Other/print-inst-addrs.ll (+6)
  • (added) llvm/test/Other/print-inst-debug-locs.ll (+20)
  • (added) llvm/test/Other/print-mi-addrs.ll (+11)
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
index 52c977a3651a3..6b7e74c04f6d5 100644
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -63,6 +63,10 @@
 
 using namespace llvm;
 
+static cl::opt<bool>
+    PrintMIAddrs("print-mi-addrs", cl::Hidden,
+                 cl::desc("Print addresses of MachineInstrs when dumping"));
+
 static const MachineFunction *getMFIfAvailable(const MachineInstr &MI) {
   if (const MachineBasicBlock *MBB = MI.getParent())
     if (const MachineFunction *MF = MBB->getParent())
@@ -2059,6 +2063,9 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
   }
   // TODO: DBG_LABEL
 
+  if (PrintMIAddrs)
+    OS << " ; " << this;
+
   if (AddNewLine)
     OS << '\n';
 }
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 57e9cccdc0fb6..73e6d1c9b0b2f 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -88,6 +88,14 @@
 
 using namespace llvm;
 
+static cl::opt<bool>
+    PrintInstAddrs("print-inst-addrs", cl::Hidden,
+                   cl::desc("Print addresses of instructions when dumping"));
+
+static cl::opt<bool> PrintInstDebugLocs(
+    "print-inst-debug-locs", cl::Hidden,
+    cl::desc("Pretty print debug locations of instructions when dumping"));
+
 // Make virtual table appear in this compilation unit.
 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
 
@@ -4236,6 +4244,18 @@ void AssemblyWriter::printInfoComment(const Value &V) {
   if (AnnotationWriter) {
     AnnotationWriter->printInfoComment(V, Out);
   }
+
+  if (PrintInstDebugLocs) {
+    if (auto *I = dyn_cast<Instruction>(&V)) {
+      if (I->getDebugLoc()) {
+        Out << " ; ";
+        I->getDebugLoc().print(Out);
+      }
+    }
+  }
+
+  if (PrintInstAddrs)
+    Out << " ; " << &V;
 }
 
 static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
diff --git a/llvm/test/Other/print-inst-addrs.ll b/llvm/test/Other/print-inst-addrs.ll
new file mode 100644
index 0000000000000..5907b30f0f12c
--- /dev/null
+++ b/llvm/test/Other/print-inst-addrs.ll
@@ -0,0 +1,6 @@
+; RUN: opt -S -print-inst-addrs %s | FileCheck %s
+
+define void @foo() {
+  ; CHECK: ret void ; 0x
+  ret void
+}
diff --git a/llvm/test/Other/print-inst-debug-locs.ll b/llvm/test/Other/print-inst-debug-locs.ll
new file mode 100644
index 0000000000000..93210527e27a7
--- /dev/null
+++ b/llvm/test/Other/print-inst-debug-locs.ll
@@ -0,0 +1,20 @@
+; RUN: opt -S -print-inst-debug-locs < %s | FileCheck %s
+
+define weak i32 @foo(i32 %a, i32 %b) !dbg !3 {
+entry:
+  ; CHECK: call {{.*}} ; foo.c:52
+  %sum = call i32 @fastadd(i32 %a, i32 %b), !dbg !DILocation(line: 52, scope: !3)
+  ; CHECK: ret {{.*}} ; foo.c:53
+  ret i32 %sum, !dbg !DILocation(line: 53, scope: !3)
+}
+
+declare i32 @fastadd(i32, i32)
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 2, !"Debug Info Version", i32 3}
+
+!llvm.dbg.cu = !{!1}
+!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, emissionKind: FullDebug)
+!2 = !DIFile(filename: "foo.c", directory: "/path/to/dir")
+!3 = distinct !DISubprogram(file: !2, scope: !2, line: 51, name: "foo", type: !4, unit: !1)
+!4 = !DISubroutineType(types: !{})
diff --git a/llvm/test/Other/print-mi-addrs.ll b/llvm/test/Other/print-mi-addrs.ll
new file mode 100644
index 0000000000000..5be006d9df282
--- /dev/null
+++ b/llvm/test/Other/print-mi-addrs.ll
@@ -0,0 +1,11 @@
+; RUN: llc -print-after=slotindexes -print-mi-addrs < %s 2>&1 | FileCheck %s
+; REQUIRES: default_triple
+
+; CHECK: IR Dump {{.*}}
+; CHECK: # Machine code for function foo{{.*}}
+
+define void @foo() {
+  ; CHECK: ; 0x
+  ret void
+}
+

@pcc pcc requested a review from rnk February 20, 2025 02:29
@pcc
Copy link
Contributor Author

pcc commented Feb 27, 2025

Ping

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I came to say "sorry I don't have time", but I think this is pretty straightforward.

@pcc pcc merged commit 0ebf7b4 into main Feb 27, 2025
10 checks passed
@pcc pcc deleted the users/pcc/spr/ir-codegen-add-command-line-flags-for-dumping-instruction-addresses-and-debug-locations branch February 27, 2025 23:45
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Feb 28, 2025
…n 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/llvm-project#127944
cheezeburglar pushed a commit to cheezeburglar/llvm-project that referenced this pull request Feb 28, 2025
… 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
mtrofin added a commit that referenced this pull request Mar 7, 2025
Inspired by PR #127944, this patch adds an option to print profile metadata inline with respect to the instruction (or function) it annotates - this saves one time from having to search up and down large textual modules to find this info.
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Inspired by PR llvm#127944, this patch adds an option to print profile metadata inline with respect to the instruction (or function) it annotates - this saves one time from having to search up and down large textual modules to find this info.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants