[llvm] [DA] use NSW arithmetic (PR #116632)

Sebastian Pop via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 5 11:02:31 PST 2025


================
@@ -3116,11 +3116,9 @@ const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
                                              const Loop *TargetLoop,
                                              const SCEV *Value) const {
   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
-  if (!AddRec) // create a new addRec
-    return SE->getAddRecExpr(Expr,
-                             Value,
-                             TargetLoop,
-                             SCEV::FlagAnyWrap); // Worst case, with no info.
+  if (!AddRec)
+    return SE->getAddRecExpr(Expr, Value, TargetLoop, SCEV::FlagNSW);
----------------
sebpop wrote:

Thank you Michael for your review.
I updated the commit message to explain that NSW is used to have the semantics of linear function with no wrap around:

> DA uses SCEVs to represent linear constraints on memory accesses.
addToCoefficient is only called from the "Constraint Propagation engine":
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/DependenceAnalysis.cpp#L3877
>
> The propagation engine works on MIV subscripts.
MIV subscripts are pairs of *linear* memory accesses that vary in multiple loops.
At this point all memory accesses are linear, non-linear have been filtered out.
>
> When addToCoefficient creates an AddRec with flags SCEV::FlagAnyWrap,
classifyPair returns llvm::DependenceInfo::Subscript::NonLinear.
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/DependenceAnalysis.cpp#L3882
>
> Having a non-linear effect this late in the dependence test is surprising, and
the test fails with llvm_unreachable("bad subscript classification").
>
> Instead of building non-linear functions, this patch modifies the flags to
SCEV::FlagNSW that has the meaning of linear functions that do not wrap around.


I also added a comment to the code:

> // When Expr is invariant in TargetLoop, i.e., the stride in TargetLoop is 0,
// addToCoefficient returns an AddRec {Expr, +, Value}_TargetLoop.
// NoWrapFlags is set to FlagNSW for the evolution function to be linear.



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


More information about the llvm-commits mailing list