[llvm] [LoopUtils] Mark createMinMaxOp's new select with unknown branch weights (PR #201750)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 4 22:30:51 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Madhur Amilkanthwar (madhur13490)
<details>
<summary>Changes</summary>
`createMinMaxOp()` lowers an FP min/max reduction to a freshly built fcmp+select. That select is not derived from any existing branch, so it carries no real profile data, and prof-verify reports a "select annotation missing" failure. Mark the synthesized select's branch weights as explicitly unknown when the enclosing function is profiled, guarded by the existing profcheck-disable-metadata-fixes option.
Add a unit test covering the profiled and non-profiled cases.
---
Full diff: https://github.com/llvm/llvm-project/pull/201750.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Utils/LoopUtils.cpp (+4)
- (modified) llvm/unittests/Transforms/Utils/LoopUtilsTest.cpp (+32)
``````````diff
diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index f0586e4f0f464..d23166eed1e4b 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -1312,6 +1312,10 @@ Value *llvm::createMinMaxOp(IRBuilderBase &Builder, RecurKind RK, Value *Left,
CmpInst::Predicate Pred = getMinMaxReductionPredicate(RK);
Value *Cmp = Builder.CreateCmp(Pred, Left, Right, "rdx.minmax.cmp");
Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
+ // This select is synthesized fresh, not lowered from an existing branch, so
+ // it carries no real profile. Mark its weights as explicitly unknown.
+ if (auto *SI = dyn_cast<SelectInst>(Select))
+ setExplicitlyUnknownBranchWeightsIfProfiled(*SI, DEBUG_TYPE);
return Select;
}
diff --git a/llvm/unittests/Transforms/Utils/LoopUtilsTest.cpp b/llvm/unittests/Transforms/Utils/LoopUtilsTest.cpp
index 4e51061f1cdec..e1bc582783c09 100644
--- a/llvm/unittests/Transforms/Utils/LoopUtilsTest.cpp
+++ b/llvm/unittests/Transforms/Utils/LoopUtilsTest.cpp
@@ -8,11 +8,14 @@
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/IVDescriptors.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Dominators.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Support/SourceMgr.h"
@@ -275,3 +278,32 @@ TEST(LoopUtils, zeroEstimatedTripCount) {
}
});
}
+
+// createMinMaxOp lowers an FP min/max to a freshly built fcmp+select that has
+// no real profile. In a profiled function its branch weights must be marked
+// explicitly unknown; in a non-profiled function no profile metadata is added.
+TEST(LoopUtils, CreateMinMaxOpExplicitlyUnknownWeights) {
+ LLVMContext C;
+ std::unique_ptr<Module> M =
+ parseIR(C, "define float @profiled(float %a, float %b) !prof !0 {\n"
+ " ret float %a\n"
+ "}\n"
+ "define float @not_profiled(float %a, float %b) {\n"
+ " ret float %a\n"
+ "}\n"
+ "!0 = !{!\"function_entry_count\", i64 100}\n");
+
+ auto BuildMinMaxSelect = [](Function *F) -> SelectInst * {
+ IRBuilder<> Builder(F->getEntryBlock().getTerminator());
+ Value *V =
+ createMinMaxOp(Builder, RecurKind::FMin, F->getArg(0), F->getArg(1));
+ return cast<SelectInst>(V);
+ };
+
+ SelectInst *ProfiledSel = BuildMinMaxSelect(M->getFunction("profiled"));
+ EXPECT_TRUE(hasExplicitlyUnknownBranchWeights(*ProfiledSel));
+
+ SelectInst *UnprofiledSel = BuildMinMaxSelect(M->getFunction("not_profiled"));
+ EXPECT_FALSE(hasExplicitlyUnknownBranchWeights(*UnprofiledSel));
+ EXPECT_EQ(UnprofiledSel->getMetadata(LLVMContext::MD_prof), nullptr);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/201750
More information about the llvm-commits
mailing list