[llvm] [LAA] Enable diff checks for non-unit constant stride (PR #188826)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 08:50:07 PDT 2026


================
@@ -2280,15 +2280,50 @@ Value *llvm::addDiffRuntimeChecks(Instruction *Loc,
   // Map to keep track of created compares, The key is the pair of operands for
   // the compare, to allow detecting and re-using redundant compares.
   DenseMap<std::pair<Value *, Value *>, Value *> SeenCompares;
-  for (const auto &[SrcStart, SinkStart, AccessSize, NeedsFreeze] : Checks) {
+
+  assert(Expander.getAllInsertedInstructions().empty() &&
+         "Expected a freshly created Expander so that we could freely erase "
+         "created instructions on bailout!");
+
+  for (const auto &[SrcStart, SinkStart, AccessSize, AbsCommonStrideInBytes,
+                    NeedsFreeze] : Checks) {
     assert(IC * AccessSize > 0 &&
            "Threshold must be non-zero to use diff-check");
     Type *Ty = SinkStart->getType();
-    const SCEV *TotalAccessSize = SE.getElementCount(Ty, VF * IC * AccessSize);
+
+    // Compute the distance between first/last bytes of the accessed memory
+    // during one vector loop iteration. This is equal to
+    // VF*IC*Stride-(Stride-AccessSize). For non-unit-stride accesses we need to
+    // make sure the computation doesn't overflow. Huge strides aren't very
+    // interesting so we can make a conservative check by adding bit widths of
+    // all multiplication terms.
+
+    const SCEV *VFSCEV = SE.getElementCount(Ty, VF);
+    unsigned VFBits = SE.getUnsignedRangeMax(VFSCEV).getActiveBits();
+    unsigned VFxICBits = VFBits + APInt(64, IC).getActiveBits();
+    unsigned TyBits = Ty->getScalarSizeInBits();
+    unsigned AvailableStrideBits = TyBits > VFxICBits ? TyBits - VFxICBits : 0;
+
+    assert(AbsCommonStrideInBytes >= AccessSize &&
+           "Stride is expected to be at least AccessSize!");
+
+    if (AccessSize != AbsCommonStrideInBytes &&
+        !isUIntN(AvailableStrideBits, AbsCommonStrideInBytes)) {
+      if (MemoryRuntimeCheck)
+        RecursivelyDeleteTriviallyDeadInstructions(MemoryRuntimeCheck);
+      Expander.eraseDeadInstructions(nullptr);
+      return nullptr;
+    }
+
+    const SCEV *VectorIterAccessSpan = SE.getMinusSCEV(
+        SE.getMulExpr(SE.getElementCount(Ty, VF), SE.getConstant(Ty, IC),
----------------
fhahn wrote:

```suggestion
        SE.getMulExpr(VFSCEV, SE.getConstant(Ty, IC),
```

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


More information about the llvm-commits mailing list