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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 18 05:55:03 PDT 2026


================
@@ -3066,6 +3071,34 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
       }
     }
 
+    // Allow cases where the ExactTC == (VF * IC) or ExactTC == (VF * IC) + 1.
+    //
+    // This produces at most 1 vector iteration, and at most 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.
+    //
+    // For loops with small bodies, the cost model is not currently reliable
+    // enough to accurately determine if vectorization is beneficial.
+    unsigned EffectiveIC = UserIC > 0 ? UserIC : 1;
+    unsigned MaxVFForTC = llvm::bit_floor(TC.getFixedValue());
+    unsigned NumOfInstructions = 0;
+    for (BasicBlock *BB : TheLoop->blocks())
+      NumOfInstructions += BB->size();
----------------
fhahn wrote:

Can sink the full walk of the loop after the other checks so it is not done unconditional (can maybe also use `sum_of()`)

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


More information about the llvm-commits mailing list