Skip to content

Commit 11b1f15

Browse files
authored
Optionally print !prof metadata inline (#130303)
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.
1 parent 0ea02e7 commit 11b1f15

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/IR/AsmWriter.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ static cl::opt<bool> PrintInstDebugLocs(
9696
"print-inst-debug-locs", cl::Hidden,
9797
cl::desc("Pretty print debug locations of instructions when dumping"));
9898

99+
static cl::opt<bool> PrintProfData(
100+
"print-prof-data", cl::Hidden,
101+
cl::desc("Pretty print perf data (branch weights, etc) when dumping"));
102+
99103
// Make virtual table appear in this compilation unit.
100104
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
101105

@@ -4161,6 +4165,13 @@ void AssemblyWriter::printFunction(const Function *F) {
41614165
writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
41624166
}
41634167

4168+
if (PrintProfData) {
4169+
if (auto *MDProf = F->getMetadata(LLVMContext::MD_prof)) {
4170+
Out << " ";
4171+
MDProf->print(Out, TheModule, /*IsForDebug=*/true);
4172+
}
4173+
}
4174+
41644175
if (F->isDeclaration()) {
41654176
Out << '\n';
41664177
} else {
@@ -4287,6 +4298,14 @@ void AssemblyWriter::printInfoComment(const Value &V) {
42874298
}
42884299
}
42894300
}
4301+
if (PrintProfData) {
4302+
if (auto *I = dyn_cast<Instruction>(&V)) {
4303+
if (auto *MD = I->getMetadata(LLVMContext::MD_prof)) {
4304+
Out << " ; ";
4305+
MD->print(Out, TheModule, /*IsForDebug=*/true);
4306+
}
4307+
}
4308+
}
42904309

42914310
if (PrintInstAddrs)
42924311
Out << " ; " << &V;

llvm/test/Other/print-prof-data.ll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; RUN: opt %s -print-prof-data -S | FileCheck %s
2+
3+
define void @foo(ptr %p) !prof !0 {
4+
%isnull = icmp eq ptr %p, null
5+
br i1 %isnull, label %yes, label %no, !prof !1
6+
yes:
7+
%something = select i1 %isnull, i32 1, i32 2, !prof !2
8+
br label %exit
9+
no:
10+
call void %p(), !prof !3
11+
br label %exit
12+
exit:
13+
ret void
14+
}
15+
16+
!0 = !{!"function_entry_count", i64 42}
17+
!1 = !{!"branch_weights", i64 20, i64 101}
18+
!2 = !{!"branch_weights", i64 5, i64 70}
19+
!3 = !{!"VP", i32 0, i64 4, i64 4445083295448962937, i64 2, i64 -2718743882639408571, i64 2}
20+
21+
; CHECK: define void @foo(ptr %p) !0 = !{!"function_entry_count", i64 42} !prof !0 {
22+
; CHECK: br i1 %isnull, label %yes, label %no, !prof !1 ; !1 = !{!"branch_weights", i64 20, i64 101}
23+
; CHECK: %something = select i1 %isnull, i32 1, i32 2, !prof !2 ; !2 = !{!"branch_weights", i64 5, i64 70}
24+
; CHECK: call void %p(), !prof !3 ; !3 = !{!"VP", i32 0, i64 4, i64 4445083295448962937, i64 2, i64 -2718743882639408571, i64 2}

0 commit comments

Comments
 (0)