Skip to content

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

Merged

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented 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.

Copy link
Member Author

mtrofin commented Mar 7, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@mtrofin mtrofin requested review from pcc, rnk and teresajohnson March 7, 2025 16:22
@mtrofin mtrofin marked this pull request as ready for review March 7, 2025 16:22
@llvmbot llvmbot added the llvm:ir label Mar 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 7, 2025

@llvm/pr-subscribers-llvm-ir

Author: Mircea Trofin (mtrofin)

Changes

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.


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

3 Files Affected:

  • (modified) llvm/lib/IR/AsmWriter.cpp (+17)
  • (added) llvm/test/Other/print-prof-data.ll (+24)
  • (added) llvm/test/Other/print-prof-data.s (+23)
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

@mtrofin mtrofin requested a review from mingmingl-llvm March 7, 2025 16:24
Copy link
Contributor

@teresajohnson teresajohnson left a 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

Copy link
Contributor

@mingmingl-llvm mingmingl-llvm left a comment

Choose a reason for hiding this comment

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

thanks!

@mtrofin mtrofin force-pushed the users/mtrofin/03-07-optionally_print_prof_metadata_inline branch 2 times, most recently from 2d5a881 to 33718e8 Compare March 7, 2025 17:01
@mtrofin mtrofin force-pushed the users/mtrofin/03-07-optionally_print_prof_metadata_inline branch from 33718e8 to fd5e082 Compare March 7, 2025 19:07
Copy link
Member Author

mtrofin commented Mar 7, 2025

Merge activity

  • Mar 7, 3:20 PM EST: A user started a stack merge that includes this pull request via Graphite.
  • Mar 7, 3:22 PM EST: A user merged this pull request with Graphite.

@mtrofin mtrofin merged commit 11b1f15 into main Mar 7, 2025
8 of 11 checks passed
@mtrofin mtrofin deleted the users/mtrofin/03-07-optionally_print_prof_metadata_inline branch March 7, 2025 20:22
@pcc
Copy link
Contributor

pcc commented Mar 7, 2025

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 !prof. This could also be useful for CFI, e.g.

@foo = constant i32 0, type !12345
!12345 = !{i64 0, !"foo"}

becomes

@foo = constant i32 0, !type !{i64 0, !"foo"}

IIRC inline metadata is parsed as expected so this could be useful for developers who are manually editing metadata e.g. for test cases.

@teresajohnson
Copy link
Contributor

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 !prof. This could also be useful for CFI, e.g.

@foo = constant i32 0, type !12345
!12345 = !{i64 0, !"foo"}

becomes

@foo = constant i32 0, !type !{i64 0, !"foo"}

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.

@nikic
Copy link
Contributor

nikic commented Mar 7, 2025

Agree with @pcc, an option to generally print instruction metadata inline would be quite useful. (Excluding distinct and self-referential metadata.)

@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 7, 2025

LLVM Buildbot has detected a new failure on builder lld-x86_64-ubuntu-fast running on as-builder-4 while building llvm at step 6 "test-build-unified-tree-check-all".

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
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'LLVM-Unit :: Support/./SupportTests/48/88' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:/home/buildbot/worker/as-builder-4/ramdisk/lld-x86_64/build/unittests/Support/./SupportTests-LLVM-Unit-2861403-48-88.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=88 GTEST_SHARD_INDEX=48 /home/buildbot/worker/as-builder-4/ramdisk/lld-x86_64/build/unittests/Support/./SupportTests
--

Script:
--
/home/buildbot/worker/as-builder-4/ramdisk/lld-x86_64/build/unittests/Support/./SupportTests --gtest_filter=Caching.NoCommit
--
/home/buildbot/worker/as-builder-4/ramdisk/lld-x86_64/llvm-project/llvm/unittests/Support/Caching.cpp:142: Failure
Value of: AddStream
  Actual: false
Expected: true


/home/buildbot/worker/as-builder-4/ramdisk/lld-x86_64/llvm-project/llvm/unittests/Support/Caching.cpp:142
Value of: AddStream
  Actual: false
Expected: true



********************


Copy link
Member Author

mtrofin commented Mar 8, 2025

Agree with @pcc, an option to generally print instruction metadata inline would be quite useful. (Excluding distinct and self-referential metadata.)

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!

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.

7 participants