[llvm] [LV] Consider UserIC when limiting VF. (PR #174573)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 12 03:57:27 PST 2026


================
@@ -3807,16 +3811,24 @@ ElementCount LoopVectorizationCostModel::clampVFByMaxTripCount(
   if (MaxTripCount > 0 && requiresScalarEpilogue(true))
     MaxTripCount -= 1;
 
-  if (MaxTripCount && MaxTripCount <= EstimatedVF &&
+  // When the user specifies an interleave count, we need to ensure that
+  // VF * UserIC <= MaxTripCount to avoid a dead vector loop.
+  unsigned IC = UserIC > 0 ? UserIC : 1;
+  unsigned EstimatedVFTimesIC = EstimatedVF * IC;
+
+  if (MaxTripCount && MaxTripCount <= EstimatedVFTimesIC &&
       (!FoldTailByMasking || isPowerOf2_32(MaxTripCount))) {
     // If upper bound loop trip count (TC) is known at compile time there is no
-    // point in choosing VF greater than TC (as done in the loop below). Select
-    // maximum power of two which doesn't exceed TC. If VF is
+    // point in choosing VF greater than TC / IC (as done in the loop below).
+    // Select maximum power of two which doesn't exceed TC / IC. If VF is
     // scalable, we only fall back on a fixed VF when the TC is less than or
     // equal to the known number of lanes.
-    auto ClampedUpperTripCount = llvm::bit_floor(MaxTripCount);
+    auto ClampedUpperTripCount = llvm::bit_floor(MaxTripCount / IC);
----------------
fhahn wrote:

Yep, this is what the patch currently does, as this is the easiest to fit in terms of current interfaces. We could adjust the interface to let  `clampVFByMaxTripCount` also limit the IC as first step, but it would probably be better to keep the interface simpler as-is.

Alternatively we could limit the IC once we chosen the VF, although there's a bit of duplication, because we will have to account for tail-folding.

I can update the latter, although I am not sure it is worth doing so for user input that is invalid. We should probably at least emit a remark to inform the user?

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


More information about the llvm-commits mailing list