[llvm] [VPlan] Thread branch weights into VPlan and use them for cost (NFCI) (PR #209309)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 03:19:00 PDT 2026


================
@@ -1028,20 +1028,10 @@ class LoopVectorizationCostModel {
   /// Superset of instructions that return true for isScalarWithPredication.
   bool isPredicatedInst(Instruction *I) const;
 
-  /// A helper function that returns how much we should divide the cost of a
-  /// predicated block by. Typically this is the reciprocal of the block
-  /// probability, i.e. if we return X we are assuming the predicated block will
-  /// execute once for every X iterations of the loop header so the block should
-  /// only contribute 1/X of its cost to the total cost calculation, but when
-  /// optimizing for code size it will just be 1 as code size costs don't depend
-  /// on execution probabilities.
-  ///
-  /// Note that if a block wasn't originally predicated but was predicated due
-  /// to tail folding, the divisor will still be 1 because it will execute for
-  /// every iteration of the loop header.
-  inline uint64_t
-  getPredBlockCostDivisor(TargetTransformInfo::TargetCostKind CostKind,
-                          const BasicBlock *BB);
+  /// Returns branch_weights estimating how often the predicated block \p BB
+  /// executes relative to the loop header, or nullptr if \p BB is not
----------------
lukel97 wrote:

Yeah loop vectorization flattens control flow generally. So for the original scalar loop, we want to divide the cost of any predicated blocks in it since they will be unconditionally executed in the vectorized version. 

The header block dominates all other blocks in the loop, so for an inner-most nested loop all blocks in the loop will have a probability less than or equal to the header. 

So returning the reciprocal relative probability means you can just divide the cost of each block in the scalar loop by it. 

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


More information about the llvm-commits mailing list