[llvm] LoopVectorize: Use a better heuristic for epilogue branch weights (PR #72589)

David Li via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 16 23:42:41 PST 2023


================
@@ -8079,8 +8075,16 @@ EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
 
   BranchInst &BI =
       *BranchInst::Create(Bypass, LoopVectorPreHeader, CheckMinIters);
-  if (hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator()))
-    setBranchWeights(BI, EpilogueMinItersBypassWeights);
+  if (hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
+    // Assume the tripcount for the epilogue loop is equally distributed between
+    // 0 and `VectorTripCount - 1` leaving us with a `VectorTripCount * 0.5`
+    // branch weight.
+    unsigned CombinedFactor = UF * VF.getKnownMinValue();
+    assert(CombinedFactor > 1 && "UF times VF should be bigger than one");
+    unsigned EstimatedTripCount = (CombinedFactor + 1) / 2;
+    uint32_t Weights[] = {1, EstimatedTripCount};
----------------
david-xl wrote:

Here we are computing the probability of (x < V) where x is the remaining loop trip count, and V is the remainder vector loop's step length. Assuming x is uniformly distributed between [0, V2] where V2 is UF*VF of the main loop, then the probability would be min(V, V2)/V2

https://github.com/llvm/llvm-project/pull/72589


More information about the llvm-commits mailing list