[llvm] [LV] Vectorize early exit loops with stores using masking (PR #178454)

Graham Hunter via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 11 09:33:39 PST 2026


================
@@ -8139,6 +8146,24 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
                        CM.getMaxSafeElements());
         RUN_VPLAN_PASS(VPlanTransforms::optimizeEVLMasks, *Plan);
       }
+
+      // If we have side effects (e.g. stores) in a loop with uncountable
+      // exits, we must ensure the early exit condition is known before any
+      // state changing operations take place.
+      if (!Plan->hasScalarVFOnly() &&
+          Legal->hasUncountableExitWithSideEffects()) {
+        ArrayRef<BasicBlock *> UncountableExits =
+            Legal->getUncountableExitingBlocks();
+        // TODO: Support more than one.
+        if (UncountableExits.size() > 1)
+          break;
+        auto *UncountableBR =
+            cast<BranchInst>(UncountableExits.front()->getTerminator());
+        if (!RUN_VPLAN_PASS(
+                VPlanTransforms::handleUncountableExitsWithSideEffects, *Plan,
----------------
huntergr-arm wrote:

I've moved the transform to run just after `introduceMasksAndLinearize`, and attached masks to the plain VPInstructions that exist at the point. There's a few things which aren't quite right yet (e.g the widening decisions for loads and stores differ based on whether they're before or after the early exit; I guess it doesn't find a block mask to work with in the header so assumes widening is fine, but does find one in the latch so replicates?), but is this moving in the right direction?

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


More information about the llvm-commits mailing list