[llvm] [LoopVectorize] Improve Vectorization of Small Loops (PR #195823)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 03:05:02 PDT 2026


================
@@ -3016,6 +3031,33 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
         MaxFactors.ScalableVF = ElementCount::getScalable(0);
         return MaxFactors;
       }
+      // Allow cases where the ExactTC == VF + 1. VF can be any power of
+      // 2 between 2 and MaxVF.
+      //
+      // This produces 1 vector iteration, and 1 scalar iteration with
+      // no remainder. Later passes will eliminate the loop and leave
+      // straight-line code as the both iteration counts are statically known.
+      ElementCount ExactTC = getSmallConstantTripCount(PSE.getSE(), TheLoop);
----------------
david-arm wrote:

Is this true? From what I can tell `getSmallConstantTripCount` and `getSmallBestKnownTC` can be completely different. I can see `getSmallBestKnownTC ` is invoked with the default parameters, which means that we could hit this part in the function:

```
  // Check if upper bound estimate is known.
  if (unsigned ExpectedTC = PSE.getSmallConstantMaxTripCount())
    return ElementCount::getFixed(ExpectedTC);
```

Therefore, the value returned may correspond to the upper bound without there being an exact trip count. So I think we still have to use the result from `getSmallConstantTripCount` here, otherwise we're potentially making decisions based on profile data or the max trip count.

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


More information about the llvm-commits mailing list