[llvm] [DA] Rewrite BanerjeeMIV test with safe APInt interval arithmetic (PR #207662)
Ruoyu Qiu via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 7 23:29:44 PDT 2026
================
@@ -1962,464 +2093,251 @@ bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
LLVM_DEBUG(dbgs() << "starting Banerjee\n");
++BanerjeeApplications;
- LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
- const SCEV *A0;
- SmallVector<CoefficientInfo, 4> A;
- collectCoeffInfo(Src, true, A0, A);
- LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
- const SCEV *B0;
- SmallVector<CoefficientInfo, 4> B;
- collectCoeffInfo(Dst, false, B0, B);
- SmallVector<BoundInfo, 4> Bound(MaxLevels + 1);
- const SCEV *Delta = minusSCEVNoSignedOverflow(B0, A0, *SE);
- if (!Delta)
- return false;
- LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
-
- // Compute bounds for all the * directions.
- LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
- for (unsigned K = 1; K <= MaxLevels; ++K) {
- Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
- Bound[K].Direction = Dependence::DVEntry::ALL;
- Bound[K].DirSet = Dependence::DVEntry::NONE;
- findBoundsALL(A, B, Bound, K);
-#ifndef NDEBUG
- LLVM_DEBUG(dbgs() << "\t " << K << '\t');
- if (Bound[K].Lower[Dependence::DVEntry::ALL])
- LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
- else
- LLVM_DEBUG(dbgs() << "-inf\t");
- if (Bound[K].Upper[Dependence::DVEntry::ALL])
- LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
- else
- LLVM_DEBUG(dbgs() << "+inf\n");
-#endif
- }
- // Test the *, *, *, ... case.
- bool Disproved = false;
- if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
- // Explore the direction vector hierarchy.
- unsigned DepthExpanded = 0;
- unsigned NewDeps =
- exploreDirections(1, A, B, Bound, Loops, DepthExpanded, Delta);
- if (NewDeps > 0) {
- bool Improved = false;
- for (unsigned K = 1; K <= CommonLevels; ++K) {
- if (Loops[K]) {
- unsigned Old = Result.DV[K - 1].Direction;
- Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
- Improved |= Old != Result.DV[K - 1].Direction;
- if (!Result.DV[K - 1].Direction) {
- Improved = false;
- Disproved = true;
- break;
- }
- }
- }
- if (Improved)
- ++BanerjeeSuccesses;
- } else {
- ++BanerjeeIndependence;
- Disproved = true;
+ unsigned SrcBits = SE->getTypeSizeInBits(Src->getType());
+ unsigned DstBits = SE->getTypeSizeInBits(Dst->getType());
+ unsigned BaseBits = std::max(SrcBits, DstBits);
+ // Accepted coefficients and constants fit in signed BaseBits, and loop
+ // bounds fit in unsigned BaseBits. WideBits is large enough for products of
+ // those values and for summing one interval term per loop level.
+ unsigned WideBits = std::max(8u, 2 * BaseBits + MaxLevels + 8);
+ APInt Zero(WideBits, 0, true);
----------------
cabbaken wrote:
Thanks, I see your point now.
Widening APInt is only intended to avoid overflow in the analysis arithmetic.
This would indeed be unsound for an addrec that may wrap in its original bit width. In your example, if `{127,+,1}` is an i8 addrec __without no-wrap information__, the actual LLVM semantics can wrap from 127 to -128, so the dependence exists.
In my implementation, `CollectConstantAffine` __only accepts addrecs whose original recurrence is known not to wrap__, currently by requiring `nsw` on the SCEVAddRecExpr. Under that precondition, `{127,+,1}<nsw>` with `BaseBits = 8` and BTC = 1 should not be a valid case. This implementation would bail out conservatively if there is no `nsw`.
https://github.com/llvm/llvm-project/pull/207662
More information about the llvm-commits
mailing list