[llvm-branch-commits] [llvm] [DA] Fix overflow of calculation in weakCrossingSIVtest (PR #188450)

Ruoyu Qiu via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Mar 25 02:53:32 PDT 2026


https://github.com/cabbaken created https://github.com/llvm/llvm-project/pull/188450

This patch fixes a correctness issue where integer overflow in the
upper bound calculation of weakCrossingSIVtest caused the pass to
incorrectly prove independence.

The previous logic used `SCEV::getMulExpr` to calculate
`2 * ConstCoeff * UpperBound` and compared it to `Delta` using
`isKnownPredicate`. In the presence of overflow, this could yield
unsafe results.

This change replaces the SCEV arithmetic with `ConstantRange` and
its operation (`smul_fast`). If the calculation overflows,
`intersectWith(MLRange).isEmptySet()` would be false, ensures we
conservatively assume a dependence if the bounds cannot be proven
safe.



>From c879ca735ce2d169b1f82e2a342a50dbcdb66cf7 Mon Sep 17 00:00:00 2001
From: Ruoyu Qiu <cabbaken at outlook.com>
Date: Wed, 25 Mar 2026 09:44:15 +0000
Subject: [PATCH] [DA] Fix overflow of calculation in weakCrossingSIVtest

This patch fixes a correctness issue where integer overflow in the
upper bound calculation of weakCrossingSIVtest caused the pass to
incorrectly prove independence.

The previous logic used `SCEV::getMulExpr` to calculate
`2 * ConstCoeff * UpperBound` and compared it to `Delta` using
`isKnownPredicate`. In the presence of overflow, this could yield
unsafe results.

This change replaces the SCEV arithmetic with `ConstantRange` and
its operation (`smul_fast`). If the calculation overflows,
`intersectWith(MLRange).isEmptySet()` would be false, ensures we
conservatively assume a dependence if the bounds cannot be proven
safe.

Signed-off-by: Ruoyu Qiu <cabbaken at outlook.com>
---
 llvm/lib/Analysis/DependenceAnalysis.cpp       | 18 ++++++++++++------
 .../weak-crossing-siv-overflow.ll              |  4 ++--
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 5885306302de1..aa7581e15d761 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1454,17 +1454,23 @@ bool DependenceInfo::weakCrossingSIVtest(const SCEV *Coeff,
   if (const SCEV *UpperBound =
           collectUpperBound(CurSrcLoop, Delta->getType())) {
     LLVM_DEBUG(dbgs() << "\t    UpperBound = " << *UpperBound << "\n");
-    const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
-    const SCEV *ML =
-        SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), ConstantTwo);
-    LLVM_DEBUG(dbgs() << "\t    ML = " << *ML << "\n");
-    if (SE->isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
+    ConstantRange UBRange = SE->getSignedRange(UpperBound);
+    ConstantRange MLRange =
+        UBRange.smul_fast(ConstCoeff->getAPInt())
+            .smul_fast(APInt(Distance.getBitWidth(), 2, true));
+    ConstantRange DeltaRange(ConstDelta->getAPInt());
+    LLVM_DEBUG(dbgs() << "\t    UBRange = " << UBRange << "\n");
+    LLVM_DEBUG(dbgs() << "\t    MLRange = " << MLRange << "\n");
+    LLVM_DEBUG(dbgs() << "\t    DeltaRange = " << DeltaRange << "\n");
+
+    if (DeltaRange.intersectWith(MLRange).isEmptySet() &&
+        DeltaRange.getSignedMin().sgt(MLRange.getSignedMax())) {
       // Delta too big, no dependence
       ++WeakCrossingSIVindependence;
       ++WeakCrossingSIVsuccesses;
       return true;
     }
-    if (SE->isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
+    if (DeltaRange.getSignedMin().eq(MLRange.getSignedMax())) {
       // i = i' = UB
       Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
       Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
diff --git a/llvm/test/Analysis/DependenceAnalysis/weak-crossing-siv-overflow.ll b/llvm/test/Analysis/DependenceAnalysis/weak-crossing-siv-overflow.ll
index acc1c0a69eb0f..a44b8d4a2c533 100644
--- a/llvm/test/Analysis/DependenceAnalysis/weak-crossing-siv-overflow.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/weak-crossing-siv-overflow.ll
@@ -89,7 +89,7 @@ define void @weakcorssing_prod_ovfl(ptr %A) {
 ; CHECK-ALL-NEXT:  Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 0, ptr %idx.0, align 1
 ; CHECK-ALL-NEXT:    da analyze - none!
 ; CHECK-ALL-NEXT:  Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
-; CHECK-ALL-NEXT:    da analyze - none!
+; CHECK-ALL-NEXT:    da analyze - output [*|<]!
 ; CHECK-ALL-NEXT:  Src: store i8 1, ptr %idx.1, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
 ; CHECK-ALL-NEXT:    da analyze - none!
 ;
@@ -97,7 +97,7 @@ define void @weakcorssing_prod_ovfl(ptr %A) {
 ; CHECK-WEAK-CROSSING-SIV-NEXT:  Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 0, ptr %idx.0, align 1
 ; CHECK-WEAK-CROSSING-SIV-NEXT:    da analyze - output [*]!
 ; CHECK-WEAK-CROSSING-SIV-NEXT:  Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
-; CHECK-WEAK-CROSSING-SIV-NEXT:    da analyze - none!
+; CHECK-WEAK-CROSSING-SIV-NEXT:    da analyze - output [*|<]!
 ; CHECK-WEAK-CROSSING-SIV-NEXT:  Src: store i8 1, ptr %idx.1, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
 ; CHECK-WEAK-CROSSING-SIV-NEXT:    da analyze - output [*]!
 ;



More information about the llvm-branch-commits mailing list