[llvm] [LAA] Compute MaxBTC * MaxStride in a wide enough type. (PR #230860)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 13:58:46 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/230860
isSafeDependenceDistance proves independence if |Dist| > MaxBTC * MaxStride. The original code did not account for MaxBTC * MaxStride wrapping, e.g. if either has a narrow type.
Fix by computing the product in the wider of the types of the distance and MaxBTC, zero-extending MaxBTC. The distance is sign-extended as before.
The product can still wrap if MaxBTC is as wide as the distance, e.g. an unbounded i64 MaxBTC. Requiring it not to wrap regresses existing proofs for symbolic distances (e.g. PR31098), so that case is left as is for now.
>From d70530d6e4aed0da933d01b180d1a74d5fb1367f Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 28 Sep 2026 10:23:32 +0100
Subject: [PATCH] [LAA] Compute MaxBTC * MaxStride in a wide enough type.
isSafeDependenceDistance proves independence if |Dist| > MaxBTC * MaxStride.
The original code did not account for MaxBTC * MaxStride wrapping, e.g.
if either has a narrow type.
Fix by computing the product in the wider of the types of the distance and
MaxBTC, zero-extending MaxBTC. The distance is sign-extended as before.
The product can still wrap if MaxBTC is as wide as the distance, e.g. an
unbounded i64 MaxBTC. Requiring it not to wrap regresses existing proofs
for symbolic distances (e.g. PR31098), so that case is left as is for now.
---
llvm/lib/Analysis/LoopAccessAnalysis.cpp | 33 ++++++++-----------
.../is-safe-dep-distance-narrow-btc.ll | 20 +++++++++--
2 files changed, 30 insertions(+), 23 deletions(-)
diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 0e717360e963f..09acf265fffe7 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -2873,9 +2873,8 @@ void MemoryDepChecker::mergeInStatus(VectorizationSafetyStatus S) {
/// = out[i];
/// out[i+D] =
/// }
-static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
- const SCEV &MaxBTC, const SCEV &Dist,
- uint64_t MaxStride) {
+static bool isSafeDependenceDistance(ScalarEvolution &SE, const SCEV &MaxBTC,
+ const SCEV &Dist, uint64_t MaxStride) {
// If we can prove that
// (**) |Dist| > MaxBTC * Step
@@ -2894,21 +2893,15 @@ static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
// will be executed only if LoopCount >= VF, proving distance >= LoopCount
// also guarantees that distance >= VF.
//
- const SCEV *Step = SE.getConstant(MaxBTC.getType(), MaxStride);
- const SCEV *Product = SE.getMulExpr(&MaxBTC, Step);
-
- const SCEV *CastedDist = &Dist;
- const SCEV *CastedProduct = Product;
- uint64_t DistTypeSizeBits = DL.getTypeSizeInBits(Dist.getType());
- uint64_t ProductTypeSizeBits = DL.getTypeSizeInBits(Product->getType());
-
- // The dependence distance can be positive/negative, so we sign extend Dist;
- // The multiplication of the absolute stride in bytes and the
- // backedgeTakenCount is non-negative, so we zero extend Product.
- if (DistTypeSizeBits > ProductTypeSizeBits)
- CastedProduct = SE.getZeroExtendExpr(Product, Dist.getType());
- else
- CastedDist = SE.getNoopOrSignExtend(&Dist, Product->getType());
+ // The dependence distance can be positive/negative, so we sign-extend Dist.
+ // MaxBTC is non-negative, so we zero-extended to the wider type before computing MaxBTC * Step.
+ // FIXME: The checks below can still wrap if MaxBTC * MaxStride does not fit
+ // in WideTy with two bits to spare, e.g. for an unbounded i64 MaxBTC.
+ Type *WideTy = SE.getWiderType(Dist.getType(), MaxBTC.getType());
+ const SCEV *Step = SE.getConstant(WideTy, MaxStride);
+ const SCEV *CastedDist = SE.getNoopOrSignExtend(&Dist, WideTy);
+ const SCEV *CastedProduct =
+ SE.getMulExpr(SE.getNoopOrZeroExtend(&MaxBTC, WideTy), Step);
// Is Dist - (MaxBTC * Step) > 0 ?
// (If so, then we have proven (**) because |Dist| >= Dist)
@@ -3151,8 +3144,8 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
// they are far enough appart that accesses won't access the same location
// across all loop ierations.
if (HasSameSize &&
- isSafeDependenceDistance(
- DL, SE, *(PSE.getSymbolicMaxBackedgeTakenCount()), *Dist, MaxStride))
+ isSafeDependenceDistance(SE, *(PSE.getSymbolicMaxBackedgeTakenCount()),
+ *Dist, MaxStride))
return Dependence::NoDep;
const APInt *APDist = nullptr;
diff --git a/llvm/test/Analysis/LoopAccessAnalysis/is-safe-dep-distance-narrow-btc.ll b/llvm/test/Analysis/LoopAccessAnalysis/is-safe-dep-distance-narrow-btc.ll
index 1b2b14df58f03..0197f9d9f6797 100644
--- a/llvm/test/Analysis/LoopAccessAnalysis/is-safe-dep-distance-narrow-btc.ll
+++ b/llvm/test/Analysis/LoopAccessAnalysis/is-safe-dep-distance-narrow-btc.ll
@@ -5,8 +5,13 @@
define void @backward_dep_i8_btc_stride_256(ptr %a) {
; CHECK-LABEL: 'backward_dep_i8_btc_stride_256'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Memory dependences are safe
+; 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: Backward loop carried data dependence.
; CHECK-NEXT: Dependences:
+; CHECK-NEXT: Backward:
+; CHECK-NEXT: %l.a = load i8, ptr %gep.a, align 1 ->
+; CHECK-NEXT: store i8 %l.a, ptr %gep.a.next, align 1
+; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Grouped accesses:
; CHECK-EMPTY:
@@ -35,8 +40,13 @@ exit:
define void @backward_dep_i8_btc_stride_255(ptr %a) {
; CHECK-LABEL: 'backward_dep_i8_btc_stride_255'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Memory dependences are safe
+; 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: Backward loop carried data dependence.
; CHECK-NEXT: Dependences:
+; CHECK-NEXT: Backward:
+; CHECK-NEXT: %l.a = load i8, ptr %gep.a, align 1 ->
+; CHECK-NEXT: store i8 %l.a, ptr %gep.a.next, align 1
+; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Grouped accesses:
; CHECK-EMPTY:
@@ -101,8 +111,12 @@ exit:
define void @backward_dep_i8_btc_product_wraps(ptr %a, i8 %n) {
; CHECK-LABEL: 'backward_dep_i8_btc_product_wraps'
; CHECK-NEXT: loop:
-; CHECK-NEXT: Memory dependences are safe
+; CHECK-NEXT: Memory dependences are safe with a maximum safe vector width of 128 bits
; CHECK-NEXT: Dependences:
+; CHECK-NEXT: BackwardVectorizable:
+; CHECK-NEXT: %l.a = load i32, ptr %gep.a, align 4 ->
+; CHECK-NEXT: store i32 %l.a, ptr %gep.a.256, align 4
+; CHECK-EMPTY:
; CHECK-NEXT: Run-time memory checks:
; CHECK-NEXT: Grouped accesses:
; CHECK-EMPTY:
More information about the llvm-commits
mailing list