[llvm] [LV] capture weights (PR #182087)
Mircea Trofin via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 18 12:13:46 PST 2026
https://github.com/mtrofin updated https://github.com/llvm/llvm-project/pull/182087
>From f6432080e0cdbc42dcd2bcd026838d0c0a1bf567 Mon Sep 17 00:00:00 2001
From: Mircea Trofin <mtrofin at google.com>
Date: Thu, 13 Nov 2025 20:28:45 -0800
Subject: [PATCH] [LV] capture weights
---
llvm/lib/Analysis/VectorUtils.cpp | 5 ++++-
.../Vectorize/VPlanConstruction.cpp | 8 +++++++-
.../Transforms/Vectorize/VPlanPredicator.cpp | 19 ++++++++++++++++++-
.../lib/Transforms/Vectorize/VPlanRecipes.cpp | 5 ++++-
4 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 79723c9815445..9c603f6ebc2a8 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -1060,7 +1060,8 @@ void llvm::getMetadataToPropagate(
LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
- LLVMContext::MD_access_group, LLVMContext::MD_mmra};
+ LLVMContext::MD_access_group, LLVMContext::MD_mmra,
+ LLVMContext::MD_prof};
// Remove any unsupported metadata kinds from Metadata.
for (unsigned Idx = 0; Idx != Metadata.size();) {
@@ -1112,6 +1113,8 @@ Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
case LLVMContext::MD_access_group:
MD = intersectAccessGroups(Inst, IJ);
break;
+ case LLVMContext::MD_prof:
+ break;
default:
llvm_unreachable("unhandled metadata");
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index d22e8c100529e..a8ef9dd122df3 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -27,6 +27,7 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/LoopVersioning.h"
@@ -467,8 +468,11 @@ static void addCanonicalIVRecipes(VPlan &Plan, VPBasicBlock *HeaderVPBB,
// We are about to replace the branch to exit the region. Remove the original
// BranchOnCond, if there is any.
DebugLoc LatchDL = DL;
+ MDNode *BW = nullptr;
if (!LatchVPBB->empty() && match(&LatchVPBB->back(), m_BranchOnCond())) {
LatchDL = LatchVPBB->getTerminator()->getDebugLoc();
+ if (auto *VPI = dyn_cast<VPInstruction>(LatchVPBB->getTerminator()))
+ BW = VPI->getMetadata(LLVMContext::MD_prof);
LatchVPBB->getTerminator()->eraseFromParent();
}
@@ -480,10 +484,12 @@ static void addCanonicalIVRecipes(VPlan &Plan, VPBasicBlock *HeaderVPBB,
CanonicalIVPHI, &Plan.getVFxUF(), DL, "index.next", {true, false});
CanonicalIVPHI->addOperand(CanonicalIVIncrement);
+ VPIRMetadata Metadata;
+ Metadata.setMetadata(LLVMContext::MD_prof, BW);
// Add the BranchOnCount VPInstruction to the latch.
Builder.createNaryOp(VPInstruction::BranchOnCount,
{CanonicalIVIncrement, &Plan.getVectorTripCount()},
- LatchDL);
+ nullptr, {}, Metadata, LatchDL);
}
/// Creates extracts for values in \p Plan defined in a loop region and used
diff --git a/llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp b/llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp
index f91bf986bda9e..72be95c2bb77c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanPredicator.cpp
@@ -18,6 +18,8 @@
#include "VPlanTransforms.h"
#include "VPlanUtils.h"
#include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/ProfDataUtils.h"
using namespace llvm;
using namespace VPlanPatternMatch;
@@ -111,8 +113,11 @@ VPValue *VPPredicator::createEdgeMask(VPBasicBlock *Src, VPBasicBlock *Dst) {
return setEdgeMask(Src, Dst, SrcMask);
EdgeMask = Term->getOperand(0);
+ SmallVector<uint32_t, 2> BranchWeights;
+ auto *OrigProf = Term->getMetadata(LLVMContext::MD_prof);
+ extractBranchWeights(OrigProf, BranchWeights);
assert(EdgeMask && "No Edge Mask found for condition");
-
+ MDBuilder MDB(EdgeMask->getUnderlyingValue()->getContext());
if (Src->getSuccessors()[0] != Dst)
EdgeMask = Builder.createNot(EdgeMask, Term->getDebugLoc());
@@ -121,7 +126,19 @@ VPValue *VPPredicator::createEdgeMask(VPBasicBlock *Src, VPBasicBlock *Dst) {
// is false and EdgeMask is poison. Avoid that by using 'LogicalAnd'
// instead which generates 'select i1 SrcMask, i1 EdgeMask, i1 false'.
EdgeMask = Builder.createLogicalAnd(SrcMask, EdgeMask, Term->getDebugLoc());
+ SmallVector<uint32_t, 2> SrcWeights;
+ extractBranchWeights(cast<VPInstruction>(SrcMask)->getMetadata(1000),
+ SrcWeights);
+ if (!BranchWeights.empty() && !SrcWeights.empty()) {
+ auto Neg = getDisjunctionWeights(
+ SmallVector<uint32_t, 2>{BranchWeights[1], BranchWeights[0]},
+ SmallVector<uint32_t, 2>{SrcWeights[1], SrcWeights[0]});
+ cast<VPInstruction>(EdgeMask)->setMetadata(
+ 1000, MDB.createBranchWeights(fitWeights({Neg[1], Neg[0]})));
+ }
}
+ if (!cast<VPInstruction>(EdgeMask)->getMetadata(1000))
+ cast<VPInstruction>(EdgeMask)->setMetadata(1000, OrigProf);
return setEdgeMask(Src, Dst, EdgeMask);
}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 4a5145b092d6c..14df5aa561143 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -29,6 +29,7 @@
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
@@ -3614,8 +3615,10 @@ void VPBranchOnMaskRecipe::execute(VPTransformState &State) {
auto *CurrentTerminator = State.CFG.PrevBB->getTerminator();
assert(isa<UnreachableInst>(CurrentTerminator) &&
"Expected to replace unreachable terminator with conditional branch.");
- auto CondBr =
+ auto *CondBr =
State.Builder.CreateCondBr(ConditionBit, State.CFG.PrevBB, nullptr);
+ if (auto *W = dyn_cast<VPWidenRecipe>(BlockInMask))
+ CondBr->setMetadata(LLVMContext::MD_prof, W->getMetadata(1000));
CondBr->setSuccessor(0, nullptr);
CurrentTerminator->eraseFromParent();
}
More information about the llvm-commits
mailing list