[llvm] [ProfCheck] Add utility to get a MDNode for unknown branch weights (PR #180389)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 7 20:52:01 PST 2026


https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/180389

There are some cases where it is non-trivial to get access to a
branch/select instruction and the helper function that creates the
branch/select of interest takes in a MDNode for branch weights. Add a
helper to create a MDNode for unknown branch weights if the function is
profiled to handle this case.


>From 1928c9107755cb889b307bd24a20c41b54c48a1f Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Sun, 8 Feb 2026 04:51:49 +0000
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 llvm/include/llvm/IR/ProfDataUtils.h |  5 +++++
 llvm/lib/IR/ProfDataUtils.cpp        | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/llvm/include/llvm/IR/ProfDataUtils.h b/llvm/include/llvm/IR/ProfDataUtils.h
index 9cca8a4d2297f..5e2e94b0b6ce0 100644
--- a/llvm/include/llvm/IR/ProfDataUtils.h
+++ b/llvm/include/llvm/IR/ProfDataUtils.h
@@ -206,6 +206,11 @@ LLVM_ABI void
 setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName,
                                             const Function *F = nullptr);
 
+/// Returns a metadata node containing unknown branch weights if the function
+/// has an entry count, otherwise returns nullptr.
+LLVM_ABI MDNode *
+getExplicitlyUnknownBranchWeightsIfProfiled(Function &F, StringRef PassName);
+
 /// Analogous to setExplicitlyUnknownBranchWeights, but for functions and their
 /// entry counts.
 LLVM_ABI void setExplicitlyUnknownFunctionEntryCount(Function &F,
diff --git a/llvm/lib/IR/ProfDataUtils.cpp b/llvm/lib/IR/ProfDataUtils.cpp
index 15cb7f9b2927f..00410a74f20a9 100644
--- a/llvm/lib/IR/ProfDataUtils.cpp
+++ b/llvm/lib/IR/ProfDataUtils.cpp
@@ -289,6 +289,18 @@ void llvm::setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I,
     setExplicitlyUnknownBranchWeights(I, PassName);
 }
 
+MDNode *llvm::getExplicitlyUnknownBranchWeightsIfProfiled(Function &F,
+                                                          StringRef PassName) {
+  if (std::optional<Function::ProfileCount> EC = F.getEntryCount();
+      !EC || EC->getCount() == 0)
+    return nullptr;
+  MDBuilder MDB(F.getContext());
+  return MDNode::get(
+      F.getContext(),
+      {MDB.createString(MDProfLabels::UnknownBranchWeightsMarker),
+       MDB.createString(PassName)});
+}
+
 void llvm::setExplicitlyUnknownFunctionEntryCount(Function &F,
                                                   StringRef PassName) {
   MDBuilder MDB(F.getContext());



More information about the llvm-commits mailing list