-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Optionally print !prof
metadata inline
#130303
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
Optionally print !prof
metadata inline
#130303
Conversation
@llvm/pr-subscribers-llvm-ir Author: Mircea Trofin (mtrofin) ChangesInspired 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. Full diff: https://github.com/llvm/llvm-project/pull/130303.diff 3 Files Affected:
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 88bb2359cd055..8399fa8a98f80 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -96,6 +96,10 @@ static cl::opt<bool> PrintInstDebugLocs(
"print-inst-debug-locs", cl::Hidden,
cl::desc("Pretty print debug locations of instructions when dumping"));
+static cl::opt<bool> PrintPerfData(
+ "print-perf-data", cl::Hidden,
+ cl::desc("Pretty print perf data (branch weights, etc) when dumping"));
+
// Make virtual table appear in this compilation unit.
AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
@@ -4161,6 +4165,11 @@ void AssemblyWriter::printFunction(const Function *F) {
writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
}
+ if (PrintPerfData && F->getEntryCount()) {
+ Out << " ";
+ F->getMetadata(LLVMContext::MD_prof)->print(Out, TheModule, true);
+ }
+
if (F->isDeclaration()) {
Out << '\n';
} else {
@@ -4287,6 +4296,14 @@ void AssemblyWriter::printInfoComment(const Value &V) {
}
}
}
+ if (PrintPerfData) {
+ if (auto *I = dyn_cast<Instruction>(&V)) {
+ if (auto *MD = I->getMetadata(LLVMContext::MD_prof)) {
+ Out << " ; ";
+ MD->print(Out, TheModule, true);
+ }
+ }
+ }
if (PrintInstAddrs)
Out << " ; " << &V;
diff --git a/llvm/test/Other/print-prof-data.ll b/llvm/test/Other/print-prof-data.ll
new file mode 100644
index 0000000000000..a5a2d444222b5
--- /dev/null
+++ b/llvm/test/Other/print-prof-data.ll
@@ -0,0 +1,24 @@
+; RUN: opt %s -print-prof-data -S | FileCheck %s
+
+define void @foo(ptr %p) !prof !0 {
+ %isnull = icmp eq ptr %p, null
+ br i1 %isnull, label %yes, label %no, !prof !1
+yes:
+ %something = select i1 %isnull, i32 1, i32 2, !prof !2
+ br label %exit
+no:
+ call void %p(), !prof !3
+ br label %exit
+exit:
+ ret void
+}
+
+!0 = !{!"function_entry_count", i64 42}
+!1 = !{!"branch_weights", i64 20, i64 101}
+!2 = !{!"branch_weights", i64 5, i64 70}
+!3 = !{!"VP", i32 0, i64 4, i64 4445083295448962937, i64 2, i64 -2718743882639408571, i64 2}
+
+; CHECK: define void @foo(ptr %p) !0 = !{!"function_entry_count", i64 42} !prof !0 {
+; CHECK: br i1 %isnull, label %yes, label %no, !prof !1 ; !1 = !{!"branch_weights", i64 20, i64 101}
+; CHECK: %something = select i1 %isnull, i32 1, i32 2, !prof !2 ; !2 = !{!"branch_weights", i64 5, i64 70}
+; CHECK: call void %p(), !prof !3 ; !3 = !{!"VP", i32 0, i64 4, i64 4445083295448962937, i64 2, i64 -2718743882639408571, i64 2}
\ No newline at end of file
diff --git a/llvm/test/Other/print-prof-data.s b/llvm/test/Other/print-prof-data.s
new file mode 100644
index 0000000000000..79595393eab2c
--- /dev/null
+++ b/llvm/test/Other/print-prof-data.s
@@ -0,0 +1,23 @@
+ .file "print-prof-data.ll"
+ .text
+ .globl foo # -- Begin function foo
+ .p2align 4
+ .type foo,@function
+foo: # @foo
+ .cfi_startproc
+# %bb.0:
+ testq %rdi, %rdi
+ je .LBB0_2
+# %bb.1: # %no
+ pushq %rax
+ .cfi_def_cfa_offset 16
+ callq *%rdi
+ addq $8, %rsp
+ .cfi_def_cfa_offset 8
+.LBB0_2: # %exit
+ retq
+.Lfunc_end0:
+ .size foo, .Lfunc_end0-foo
+ .cfi_endproc
+ # -- End function
+ .section ".note.GNU-stack","",@progbits
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm with some nits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
2d5a881
to
33718e8
Compare
33718e8
to
fd5e082
Compare
It may be more generally useful to have a flag for printing instruction and GV metadata inline (one level of expansion) instead of having a special case for
becomes
IIRC inline metadata is parsed as expected so this could be useful for developers who are manually editing metadata e.g. for test cases. |
I was thinking about suggesting that too, but there may be complications for metadata that references other metadata. I'm not sure how that gets rendered automatically, and whether and how much to expand it inline, and what would happen when parsed again. But yes, a broader expansion to other metadata types could be very useful. |
Agree with @pcc, an option to generally print instruction metadata inline would be quite useful. (Excluding distinct and self-referential metadata.) |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/33/builds/12631 Here is the relevant piece of the build log for the reference
|
I agree it would be nice - let me look into the complications @teresajohnson was mentioning. I didn't realize that inlined metadata would be parsed, that's good to know! |
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.
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.