[llvm] [DependenceAnalysis] Fix incorrect analysis of wrapping AddRec expressions (PR #154982)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 22 12:41:51 PDT 2025


================
@@ -959,6 +962,25 @@ bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
   if (!AddRec)
     return isLoopInvariant(Expr, LoopNest);
 
+  const SCEV *Step = AddRec->getStepRecurrence(*SE);
+  if (!isLoopInvariant(Step, LoopNest))
+    return false;
+
+  // Check if this AddRec expression may wrap, making it non-affine.
+  std::optional<bool> MayWrap = SE->mayAddRecWrap(AddRec);
+  // Conservative: reject if unknown or definitely wraps.
+  if (MayWrap.value_or(true)) {
+    Type *Ty = AddRec->getType();
+    unsigned BitWidth = Ty->getScalarSizeInBits();
+    // Domain-specific knowledge for array access functions: wider types are
+    // extremely unlikely to wrap because having an array allocated with more
+    // than 2^32 bytes (such that we can observe the wrap-around without causing
+    // undefined behavior from out-of-bounds access) is not realistic.
----------------
kasuga-fj wrote:

Please avoid using a phrase like "not realistic" in the context of dependence analysis. In the world of LLVM IR, it is allowed to allocate an object whose size exceeds 2^32 bytes, if the size of index type is large enough.

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


More information about the llvm-commits mailing list