[llvm] [LoopVectorize] Improve Vectorization of Low Trip Count Loops (PR #195823)

Sander de Smalen via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 05:01:46 PDT 2026


================
@@ -3068,6 +3068,28 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
       }
     }
 
+    // Allow cases where the ExactTC == (VF * IC) + 1.
+    //
+    // 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.
+    //
+    // If a function is marked as minsize/optsize or OptForSize is set, do not
+    // allow this form of transformation as this will increase CodeSize.
+    ElementCount ExactTC = getSmallConstantTripCount(PSE.getSE(), TheLoop);
+    unsigned TC = ExactTC.getFixedValue();
+    unsigned MaxVFForTC = 1ULL << Log2_32(TC);
+    unsigned EffectiveIC = UserIC > 0 ? UserIC : 1;
+    if (TC - MaxVFForTC <= 1 && !TheFunction->hasOptSize() &&
----------------
sdesmalen-arm wrote:

This is missing an extra check that  `MaxVFForTC <= MaxFactors.FixedVF.getFixedValue()` (and a corresponding test) for the case where the tripcount < the vector length of a target, or the case where a dependence distance limits the maximum VF.

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


More information about the llvm-commits mailing list