[llvm] [VPlan] Set branch weight metadata on middle term in VPlan (NFC) (PR #143035)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 12 04:16:19 PDT 2025
================
@@ -7273,6 +7273,33 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
BypassBlock, MainResumePhi->getIncomingValueForBlock(BypassBlock));
}
+/// Add branch weight metadata, if the \p Plan's middle block is terminated by a
+/// BranchOnCond recipe.
+static void addBranchWeightToMiddleTerminator(VPlan &Plan, ElementCount VF,
+ Loop *OrigLoop) {
+ // 4. Adjust branch weight of the branch in the middle block.
+ Instruction *LatchTerm = OrigLoop->getLoopLatch()->getTerminator();
+ if (!hasBranchWeightMD(*LatchTerm))
+ return;
+
+ VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
+ auto *MiddleTerm =
+ dyn_cast_or_null<VPInstruction>(MiddleVPBB->getTerminator());
+ // Only add branch metadata if there is a (conditional) terminator.
+ if (!MiddleTerm)
+ return;
+
+ assert(MiddleTerm->getOpcode() == VPInstruction::BranchOnCond &&
+ "must have a BranchOnCond");
+ // Assume that `Count % VectorTripCount` is equally distributed.
----------------
ayalz wrote:
`Count` is aka original `TripCount`, and `VectorTripCount` should be `VectorStep`: the branch terminating middle block has 1:VectorStep chance of skipping scalar epilog, i.e., when Count or original TripCount is divisible by VectorStep, assuming modulo remainder `Count % VectorStep` is uniformly distributed.
https://github.com/llvm/llvm-project/pull/143035
More information about the llvm-commits
mailing list