[llvm] [LAA] Allow vectorizing `A[NonZeroNonConstantStride*I] += 1` (PR #186262)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 01:08:35 PDT 2026


================
@@ -2788,14 +2806,27 @@ bool LoopAccessInfo::analyzeLoop(AAResults *AA, const LoopInfo *LI,
     // allows us to vectorize expressions such as A[i] += x; Because the address
     // of A[i] is a read-write pointer. This only works if the index of A[i] is
     // strictly monotonic, which we approximate (conservatively) via
-    // getPtrStride. If the address is unknown (e.g. A[B[i]]) then we may read,
-    // modify, and write overlapping words. Note that "zero stride" is unsafe
-    // and is being handled below.
+    // getPtrStrideScev. If the address is unknown (e.g. A[B[i]]) then we may
+    // read, modify, and write overlapping words. Note that "zero stride" is
+    // unsafe and is being handled below.
     bool IsReadOnlyPtr = false;
     Type *AccessTy = getLoadStoreType(LD);
-    if (Seen.insert({Ptr, AccessTy}).second ||
-        !getPtrStride(*PSE, AccessTy, Ptr, TheLoop, *DT, SymbolicStrides, false,
-                      true)) {
+    auto IsSafeReadWrite = [&] {
+      const SCEV *Stride = getPtrStrideScev(*PSE, AccessTy, Ptr, TheLoop, *DT,
+                                            SymbolicStrides, true, nullptr);
+      if (!Stride)
+        return false;
+
+      // Statically known invariant address, preserve old behavior for the
+      // LoopDistributePass. For LoopVectorizer we will detect a load from the
+      // uniform store pointer and bail out further below.
+      if (Stride->isZero())
+        return true;
+
+      auto *SE = PSE->getSE();
+      return SE->isKnownPositive(SE->getAbsExpr(Stride, false));
+    };
+    if (Seen.insert({Ptr, AccessTy}).second || !IsSafeReadWrite()) {
----------------
artagnon wrote:

Is this the core change? Not sure what this is: it is either zero or its absolute value is known-positive? Not sure how a value can be anything else?

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


More information about the llvm-commits mailing list