[llvm] [DA] Widening SCEV expressions in strong SIV test to prevent overflow (PR #164704)
    Alireza Torabian via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Wed Oct 22 13:49:08 PDT 2025
    
    
  
https://github.com/1997alireza updated https://github.com/llvm/llvm-project/pull/164704
>From 271f32a0ffd2ca9c8531ef47386f3590901acfc8 Mon Sep 17 00:00:00 2001
From: a00917109 <alireza.torabian at huawei.com>
Date: Wed, 22 Oct 2025 15:41:15 -0400
Subject: [PATCH] [DA] Widening SCEV expressions in strong SIV test to prevent
 overflow
By doubling the size of the expressions in the strong SIV test, no
overflow occurs in the multiplication required to check the test.
---
 llvm/lib/Analysis/DependenceAnalysis.cpp | 38 +++++++++++++++++-------
 1 file changed, 28 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 853bd66c8a7f8..af900e2a92998 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1668,17 +1668,35 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
   LLVM_DEBUG(dbgs() << "\t    Delta = " << *Delta);
   LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
 
+  TypeSize CoeffSize =
+      Coeff->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize SrcSize =
+      SrcConst->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize DstSize =
+      DstConst->getType()->getScalarType()->getPrimitiveSizeInBits();
+  TypeSize WideSize = std::max(CoeffSize, std::max(SrcSize, DstSize)) * 2;
+  LLVMContext &Context = CurDstLoop->getHeader()->getParent()->getContext();
+  Type *WideTy = IntegerType::get(Context, WideSize);
+  const SCEV *WideSrcC = SE->getSignExtendExpr(SrcConst, WideTy);
+  const SCEV *WideDstC = SE->getSignExtendExpr(DstConst, WideTy);
+  const SCEV *WideDelta = SE->getMinusSCEV(WideSrcC, WideDstC);
+  const SCEV *WideCoeff = SE->getSignExtendExpr(Coeff, WideTy);
+
   // check that |Delta| < iteration count
-  if (const SCEV *UpperBound =
-          collectUpperBound(CurSrcLoop, Delta->getType())) {
-    LLVM_DEBUG(dbgs() << "\t    UpperBound = " << *UpperBound);
-    LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
-    const SCEV *AbsDelta =
-        SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
-    const SCEV *AbsCoeff =
-        SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
-    const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
-    if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
+  if (const SCEV *WideUpperBound =
+          collectUpperBound(CurSrcLoop, WideDelta->getType())) {
+    LLVM_DEBUG(dbgs() << "\t    WideUpperBound = " << *WideUpperBound);
+    LLVM_DEBUG(dbgs() << ", " << *WideUpperBound->getType() << "\n");
+
+    // FIXME: Use SCEV getAbsExpr function to compute the abstract values
+    const SCEV *WideAbsDelta = SE->isKnownNonNegative(WideDelta)
+                                   ? WideDelta
+                                   : SE->getNegativeSCEV(WideDelta);
+    const SCEV *WideAbsCoeff = SE->isKnownNonNegative(WideCoeff)
+                                   ? WideCoeff
+                                   : SE->getNegativeSCEV(WideCoeff);
+    const SCEV *WideProduct = SE->getMulExpr(WideUpperBound, WideAbsCoeff);
+    if (isKnownPredicate(CmpInst::ICMP_SGT, WideAbsDelta, WideProduct)) {
       // Distance greater than trip count - no dependence
       ++StrongSIVindependence;
       ++StrongSIVsuccesses;
    
    
More information about the llvm-commits
mailing list