[llvm] [DA] Add test for RDIV misses dependency due to "minor algebra" (NFC) (PR #179653)

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 4 07:43:06 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Ryotaro Kasuga (kasuga-fj)

<details>
<summary>Changes</summary>

The function `testRDIV` contains the following comments:

```
// With minor algebra, this test can also be used for things like
// [c1 + a1*i + a2*j][c2].
```

```
// we have 3 possible situations here:
//   1) [a*i + b] and [c*j + d]
//   2) [a*i + c*j + b] and [d]
//   3) [b] and [a*i + c*j + d]
// We need to find what we've got and get organized
```

In case `2)`, it moves `c*j` from `Src` to `Dst` and performs dependence testing between `[a*i + b]` and `[-c*j + d]`. Similar algebraic manipulations are applied in case `3)`. This "minor algebra" is unsound in LLVM because whether wraps occur can change. This patch adds a test case to illustrate the issue.

---
Full diff: https://github.com/llvm/llvm-project/pull/179653.diff


1 Files Affected:

- (added) llvm/test/Analysis/DependenceAnalysis/rdiv-minor-algebra.ll (+88) 


``````````diff
diff --git a/llvm/test/Analysis/DependenceAnalysis/rdiv-minor-algebra.ll b/llvm/test/Analysis/DependenceAnalysis/rdiv-minor-algebra.ll
new file mode 100644
index 0000000000000..dab4b9ae48611
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/rdiv-minor-algebra.ll
@@ -0,0 +1,88 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -disable-output "-passes=print<da>" 2>&1 \
+; RUN:     | FileCheck %s --check-prefixes=CHECK,CHECK-ALL
+; RUN: opt < %s -disable-output "-passes=print<da>" -da-enable-dependence-test=symbolic-rdiv 2>&1 \
+; RUN:     | FileCheck %s --check-prefixes=CHECK,CHECK-SYMBOLIC-RDIV
+;
+; for (i = 0; i < 2; i++) {
+;   A[-2] = 0;
+;   for (j = 0; j < 2; j++) {
+;     if (i == j)
+;       A[INT64_MAX*i + INT64_MAX*j] = 1;
+;   }
+; }
+;
+; FIXME: DependenceAnalysis currently detects no dependency between the two
+; stores, but it does exist.
+;
+;  memory access       | (i, j) == (1, 1)
+; ---------------------|------------------
+;  A[3*i - 2]          | A[-2]
+;
+; The root cause is that RDIV performs "minor algebra" and transforms the
+; subscripts as follows:
+;
+;   A[-2] and A[INT64_MAX*i + INT64_MAX*j]
+;
+;    | minor algebra
+;    v
+;
+;   A[-INT64_MAX*j - 2] and A[INT64_MAX*i]
+;
+; This is unsound.
+;
+define void @f(ptr %A) {
+; CHECK-ALL-LABEL: 'f'
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-ALL-NEXT:    da analyze - consistent output [S]!
+; CHECK-ALL-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-ALL-NEXT:    da analyze - none!
+;
+; CHECK-SYMBOLIC-RDIV-LABEL: 'f'
+; CHECK-SYMBOLIC-RDIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 0, ptr %gep.0, align 1
+; CHECK-SYMBOLIC-RDIV-NEXT:    da analyze - consistent output [S]!
+; CHECK-SYMBOLIC-RDIV-NEXT:  Src: store i8 0, ptr %gep.0, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-SYMBOLIC-RDIV-NEXT:    da analyze - none!
+; CHECK-SYMBOLIC-RDIV-NEXT:  Src: store i8 1, ptr %gep.1, align 1 --> Dst: store i8 1, ptr %gep.1, align 1
+; CHECK-SYMBOLIC-RDIV-NEXT:    da analyze - output [* *]!
+;
+entry:
+  br label %loop.i.header
+
+loop.i.header:
+  %i = phi i64 [ 0, %entry ], [ %i.inc, %loop.i.latch ]
+  %i.mul = mul i64 %i, 9223372036854775807
+  %gep.0 = getelementptr i8, ptr %A, i64 -2
+  store i8 0, ptr %gep.0
+  br label %loop.j.header
+
+loop.j.header:
+  %j = phi i64 [ 0, %loop.i.header ], [ %j.inc, %loop.j.latch ]
+  %j.mul = mul i64 %j, 9223372036854775807
+  %offset.tmp = add i64 %i.mul, %j.mul
+  %offset = add i64 %offset.tmp, -2
+  %cond = icmp eq i64 %i, %j
+  br i1 %cond, label %if.then, label %loop.j.latch
+
+if.then:
+  %gep.1 = getelementptr i8, ptr %A, i64 %offset
+  store i8 1, ptr %gep.1
+  br label %loop.j.latch
+
+loop.j.latch:
+  %j.inc = add i64 %j, 1
+  %ec.j = icmp eq i64 %j.inc, 2
+  br i1 %ec.j, label %loop.i.latch, label %loop.j.header
+
+loop.i.latch:
+  %i.inc = add i64 %i, 1
+  %ec.i = icmp eq i64 %i.inc, 2
+  br i1 %ec.i, label %exit, label %loop.i.header
+
+exit:
+  ret void
+}
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; CHECK: {{.*}}

``````````

</details>


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


More information about the llvm-commits mailing list