[llvm] [LLVM][LV] Improve UF calculation for vscale based scalar loops. (PR #146102)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 11 06:56:08 PDT 2025


================
@@ -4813,16 +4832,22 @@ LoopVectorizationCostModel::selectInterleaveCount(VPlan &Plan, ElementCount VF,
       MaxInterleaveCount = ForceTargetMaxVectorInterleaveFactor;
   }
 
-  unsigned EstimatedVF = getEstimatedRuntimeVF(VF, VScaleForTuning);
-
   // Try to get the exact trip count, or an estimate based on profiling data or
   // ConstantMax from PSE, failing that.
-  if (auto BestKnownTC = getSmallBestKnownTC(PSE, TheLoop)) {
+  auto BestKnownTC = getSmallBestKnownTC(PSE, TheLoop);
+
+  // For fixed length VFs treat a scalable trip count as unknown.
+  if (BestKnownTC && (BestKnownTC->isFixed() || VF.isScalable())) {
+    // Re-evaluate VF to be in the same numerical space as the trip count.
+    unsigned EstimatedVF = VF.getKnownMinValue();
+    if (VF.isScalable() && BestKnownTC->isFixed())
+      EstimatedVF = getEstimatedRuntimeVF(VF, VScaleForTuning);
+
     // At least one iteration must be scalar when this constraint holds. So the
     // maximum available iterations for interleaving is one less.
     unsigned AvailableTC = requiresScalarEpilogue(VF.isVector())
-                               ? BestKnownTC->getFixedValue() - 1
-                               : BestKnownTC->getFixedValue();
+                               ? BestKnownTC->getKnownMinValue() - 1
----------------
paulwalker-arm wrote:

I don't think it matters because when `BestKnownTC` is scalable `EstimatedVF` must also be scalable with all uses being to create ratios between the two.  By using `getKnownMinValue()` we're just removing the common vscale factor.

That said, I don't much like `getKnownMinValue()` so have changed the implementation to force everything to unsigned, be they know values or estimates, which as expected does not seem to affect the result but I feel looks more readable?

Please let me know if you'd rather I stick with the original approach.

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


More information about the llvm-commits mailing list