[llvm] [LV] capture branch weights for constant trip counts (PR #175096)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 21 12:47:15 PST 2026


================
@@ -1692,17 +1696,35 @@ void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
   // For scalable vectorization we can't know at compile time how many
   // iterations of the loop are handled in one vector iteration, so instead
   // use the value of vscale used for tuning.
-  if (!OrigAverageTripCount)
-    return;
-  // Calculate number of iterations in unrolled loop.
-  unsigned AverageVectorTripCount = *OrigAverageTripCount / EstimatedVFxUF;
-  // Calculate number of iterations for remainder loop.
-  unsigned RemainderAverageTripCount = *OrigAverageTripCount % EstimatedVFxUF;
-
+  unsigned AverageVectorTripCount = 0;
+  unsigned RemainderAverageTripCount = 0;
+  auto EC = VectorLoop->getLoopPreheader()->getParent()->getEntryCount();
+  auto IsProfiled = EC && EC->getCount();
+  if (!OrigAverageTripCount) {
+    if (!IsProfiled)
+      return;
+    auto &SE = *PSE.getSE();
+    AverageVectorTripCount = SE.getSmallConstantTripCount(VectorLoop);
----------------
jdenny-ornl wrote:

I understand OrigLoop does not appear in the output IR in `exact.ll`.  Are you saying that changes to the IR prior to this `updateLoopMetadataAndProfileInfo` call would cause `SE.getSmallConstantTripCount(OrigLoop)` to sometimes return the wrong result?  But it is guaranteed to return the right result when `Plan.getScalarPreheader()->hasPredecessors()` is true so you can always compute `RemainderAverageTripCount` correctly?

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


More information about the llvm-commits mailing list