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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 9 03:17:56 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);
----------------
david-arm wrote:

With this model it sounds like you're saying honouring the user IC takes priority over the VF. What if that prevents vectorisation entirely, such as for fixed-width VFs? For a known trip count of 4 and UserIC=4, you have a choice between ending up with VF=1,IC=4 or VF=4,IC=1. In this case it sounds like honouring the UserIC is the worst outcome.

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


More information about the llvm-commits mailing list