[llvm-branch-commits] [llvm] [LAA] Find no dependence between invariant accesses a constant distance apart (PR #245056)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 22 09:07:19 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-loopaccessanalysis
Author: Matt Turner (mattst88)
<details>
<summary>Changes</summary>
Stacked on https://github.com/llvm/llvm-project/pull/245055 (the precommit test).
Two loop-invariant accesses whose addresses differ by a constant overlap
in every iteration or in none, even if the addresses wrap. LAA showed
they were disjoint only when one ended exactly where the other began,
or when it could prove the addresses do not wrap. Otherwise it reported
an unknown dependence and gave up on the loop, so a loop that updates
three or more fields of a struct through a pointer was not vectorized:
struct trio { int a, b, c; };
void acc3(struct trio *p, const int *x, int n) {
for (int i = 0; i < n; i++) {
p->a += x[i];
p->b ^= x[i];
p->c |= x[i];
}
}
This now vectorizes on x86-64 (VF 4, interleaved by 2). 200000 calls on
4096 elements take 0.054 s instead of 0.287 s (-O3, median of 11 runs
pinned to one core of an AMD Ryzen AI Max+ 395). No llvm-test-suite
program changes on x86-64.
Assisted-by: Claude Code
---
Full diff: https://github.com/llvm/llvm-project/pull/245056.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/LoopAccessAnalysis.cpp (+15-4)
- (modified) llvm/test/Analysis/LoopAccessAnalysis/invariant-stores-constant-distance.ll (+4-24)
``````````diff
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 190a7fecd96616..8ff19f3800c048 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -3076,10 +3076,21 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
// At least Src or Sink are loop invariant and the other is strided or
// invariant.
if (!StrideAPtrInt || !StrideBPtrInt) {
- // If both are loop-invariant and access the same location, we cannot
- // vectorize.
- if (!StrideAPtrInt && !StrideBPtrInt && Dist->isZero())
- return MemoryDepChecker::Dependence::InvariantUnsafe;
+ if (!StrideAPtrInt && !StrideBPtrInt) {
+ // If both are loop-invariant and access the same location, we cannot
+ // vectorize.
+ if (Dist->isZero())
+ return MemoryDepChecker::Dependence::InvariantUnsafe;
+ // Two loop-invariant accesses a constant distance apart overlap in every
+ // iteration or in none, even if the addresses wrap.
+ const APInt *APDist;
+ TypeSize ASz = DL.getTypeStoreSize(ATy);
+ TypeSize BSz = DL.getTypeStoreSize(BTy);
+ if (match(Dist, m_scev_APInt(APDist)) && !ASz.isScalable() &&
+ !BSz.isScalable() && APDist->uge(ASz.getFixedValue()) &&
+ (-*APDist).uge(BSz.getFixedValue()))
+ return MemoryDepChecker::Dependence::NoDep;
+ }
// Otherwise, we can generate a runtime check to disambiguate the accesses.
return MemoryDepChecker::Dependence::Unknown;
}
diff --git a/llvm/test/Analysis/LoopAccessAnalysis/invariant-stores-constant-distance.ll b/llvm/test/Analysis/LoopAccessAnalysis/invariant-stores-constant-distance.ll
index 984c9a5bd99036..65e056615e0215 100644
--- a/llvm/test/Analysis/LoopAccessAnalysis/invariant-stores-constant-distance.ll
+++ b/llvm/test/Analysis/LoopAccessAnalysis/invariant-stores-constant-distance.ll
@@ -8,13 +8,8 @@
define void @disjoint(ptr %s, ptr %p, i64 %n) {
; CHECK-LABEL: 'disjoint'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-; CHECK-NEXT: Unknown data dependence.
+; CHECK-NEXT: Memory dependences are safe with run-time checks
; CHECK-NEXT: Dependences:
-; CHECK-NEXT: Unknown:
-; CHECK-NEXT: store i64 %x, ptr %a, align 4 ->
-; CHECK-NEXT: store i64 %x, ptr %b, align 4
-; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Check 0:
; CHECK-NEXT: Comparing group GRP0:
@@ -59,13 +54,8 @@ exit:
define void @disjoint_below(ptr %s, ptr %p, i64 %n) {
; CHECK-LABEL: 'disjoint_below'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-; CHECK-NEXT: Unknown data dependence.
+; CHECK-NEXT: Memory dependences are safe with run-time checks
; CHECK-NEXT: Dependences:
-; CHECK-NEXT: Unknown:
-; CHECK-NEXT: store i64 %x, ptr %a, align 4 ->
-; CHECK-NEXT: store i64 %x, ptr %b, align 4
-; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Check 0:
; CHECK-NEXT: Comparing group GRP0:
@@ -215,13 +205,8 @@ exit:
define void @disjoint_lower_second(ptr %s, ptr %p, i64 %n) {
; CHECK-LABEL: 'disjoint_lower_second'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-; CHECK-NEXT: Unknown data dependence.
+; CHECK-NEXT: Memory dependences are safe with run-time checks
; CHECK-NEXT: Dependences:
-; CHECK-NEXT: Unknown:
-; CHECK-NEXT: store i32 %y, ptr %a, align 4 ->
-; CHECK-NEXT: store i64 %x, ptr %b, align 4
-; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Check 0:
; CHECK-NEXT: Comparing group GRP0:
@@ -268,13 +253,8 @@ exit:
define void @disjoint_load(ptr %s, ptr %p, i64 %n) {
; CHECK-LABEL: 'disjoint_load'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop
-; CHECK-NEXT: Unknown data dependence.
+; CHECK-NEXT: Memory dependences are safe with run-time checks
; CHECK-NEXT: Dependences:
-; CHECK-NEXT: Unknown:
-; CHECK-NEXT: %y = load i64, ptr %b, align 4 ->
-; CHECK-NEXT: store i64 %z, ptr %a, align 4
-; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Check 0:
; CHECK-NEXT: Comparing group GRP0:
``````````
</details>
https://github.com/llvm/llvm-project/pull/245056
More information about the llvm-branch-commits
mailing list