[llvm] [DA] do not handle array accesses of different offsets (PR #123436)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 15 04:28:38 PDT 2025


================
@@ -3656,18 +3669,28 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst) {
 
   if (Src != Dst) {
     // Check that memory access offsets are multiples of element sizes.
-    if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assumptions) ||
-        !SE->isKnownMultipleOf(DstEv, EltSize, Assumptions)) {
+    if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) ||
+        !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) {
       LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n");
-      return std::make_unique<Dependence>(Src, Dst);
+      return std::make_unique<Dependence>(Src, Dst, SE, Assume);
     }
   }
 
+  if (Assumptions.empty())
+    Assumptions.append(Assume.begin(), Assume.end());
+  else
+    // Add non-redundant assumptions.
+    for (auto *P : Assume)
+      for (auto *A : Assumptions)
+        if (!A->implies(P, *SE))
+          Assumptions.push_back(P);
----------------
kasuga-fj wrote:

I don't think this is correct.

- `P` can be appended multiple times.
- It can cause UB to add new elements to `Assumptions` while iterating it.

Perhaps you would like to write a code like this?

```cpp
unsigned N = Assumptions.size();
for (SCEVPredicate *P : Assume) {
  bool Implied = false;
  for (unsigned I = 0; I != N && !Implied; I++)
    if (Assumptions[I]->implies(P))
      Implied = true;
  if (!Implied)
    Assumptions.push_back(P);
}
```

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


More information about the llvm-commits mailing list