[llvm] [DA] Rewrite the formula in the Strong SIV test (PR #179665)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 12 05:46:25 PST 2026
================
@@ -1353,38 +1331,55 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
assert(0 < Level && Level <= CommonLevels && "level out of range");
Level--;
- const SCEV *Delta = minusSCEVNoSignedOverflow(SrcConst, DstConst, *SE);
- if (!Delta) {
- Result.Consistent = false;
- return false;
- }
- LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
- LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
-
- // check that |Delta| < iteration count
- bool IsDeltaLarge = [&] {
- const SCEV *UpperBound = collectUpperBound(CurSrcLoop, Delta->getType());
+ // Src: [a*0 + c1], [a*1 + c1], [a*2 + c1], ..., [a*BTC + c1]
+ // Dst: [a*0 + c2], [a*1 + c2], [a*2 + c2], ..., [a*BTC + c2]
+ //
+ // When 0 <=s a, we can prove independence if:
+ //
+ // (a*BTC + c1 <s a*0 + c2) or (a*BTC + c2 <s a*0 + c1)
+ //
+ // When a <s 0, we can prove independence if:
+ //
+ // (a*0 + c1 <s a*BTC + c2) or (a*0 + c2 <s a*BTC + c1)
+ //
+ // These can be combined by computing the smin and smax of the Src and Dst.
+ //
+ // FIXME: Currently the nowrap properties of Src and Dst are not checked.
+ // Therefore, evaluating them at the last iteration may produce incorrect
+ // results.
+ bool IsNoOverlap = [&] {
+ const SCEV *UpperBound = collectUpperBound(Src->getLoop(), Src->getType());
if (!UpperBound)
return false;
-
LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
- const SCEV *AbsDelta = absSCEVNoSignedOverflow(Delta, *SE);
- const SCEV *AbsCoeff = absSCEVNoSignedOverflow(Coeff, *SE);
- if (!AbsDelta || !AbsCoeff)
- return false;
- const SCEV *Product = mulSCEVNoSignedOverflow(UpperBound, AbsCoeff, *SE);
- if (!Product)
- return false;
- return SE->isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product);
+
+ const SCEV *SrcBegin = SrcConst;
+ const SCEV *DstBegin = DstConst;
+ const SCEV *SrcEnd = Src->evaluateAtIteration(UpperBound, *SE);
+ const SCEV *DstEnd = Dst->evaluateAtIteration(UpperBound, *SE);
+ const SCEV *SrcMin = SE->getSMinExpr(SrcBegin, SrcEnd);
+ const SCEV *SrcMax = SE->getSMaxExpr(SrcBegin, SrcEnd);
+ const SCEV *DstMin = SE->getSMinExpr(DstBegin, DstEnd);
+ const SCEV *DstMax = SE->getSMaxExpr(DstBegin, DstEnd);
+ return SE->isKnownPredicate(ICmpInst::ICMP_SLT, SrcMax, DstMin) ||
+ SE->isKnownPredicate(ICmpInst::ICMP_SLT, DstMax, SrcMin);
}();
----------------
kasuga-fj wrote:
I still don't understand your argument, but it seems that you don't agree with this direction. So, assuming that, I'll write my thoughts here.
> More complex code. Correctness is proven.
If you refer to your proof, please keep in mind that probably no one other than you isn't convinced that. I think relying on it is really risky because:
- The correctness of the proof itself is still suspicious. To discuss it, you at the very least need to clarify the assumptions the proof makes. E.g., a statement like "RHS is a DA computation so it cannot overflow" is clearly not rigorous enough. It doesn't even define the semantics of `nsw`. These points have already been [raised](https://github.com/llvm/llvm-project/pull/176367#issuecomment-3789902471). It's unclear to me why you are bringing up the proof again without addressing these issues.
- As mentioned in the same comment, I also don't think there is anything more to gain from discussing the proof any further. With this comment, I'd like to make it the last time I refer to your proof.
- Even if the proof were to be correct, relying on it would still be risky. One of the most significant issues is how to maintain the consistency between the code and the proof. If the code is changed in future, the proof may no longer be valid. In that case, would we need to re-verify the proof? If so, that becomes a maintenance burden. It is also worth noting that several inconsistencies between the code and the comments in the DA already exist. I am quite certain that the consistency between the code and the proof would eventually break as well.
> at this level of complexity, confidence about correctness is definitely more important
I agree with this, and that's why I choose this way.
> The other issue is that, in future, if we actually have the correctness problem, then we need to rediscover the existing method. Those of us who are here right now, may not be around 5 years from now or may not remember everything. The knowledge hidden in hundreds of thousands of issues and PRs may not be easy to rediscover. Even the fact that this two approaches are different in terms of their assumptions may not be easy to realize.
>From this point of view, simpler logic or code is clearly preferable. Adding several checks (e.g., overflow checks) one by one for each edge case makes the code more complex, and it wouldn't be surprising if the context were lost and those checks ended up being removed. Also, let me repeat my concern about the proof‑based approach. The proof is itself a part of the "knowledge". Your concern applies to your proof as well.
All of the thoughts above also reflect my experience reverse‑engineering DA and fixing several of its bugs. I think it's fair to say that I've spent a fair amount of time on DA. From my experience, complex logic or code is more likely to cause issues, even if it is initially correct. If we can achieve the same functionality with simpler logic or code, it should definitely be better to do so.
---
> What I am trying to do right now is to look into the history and see if a similar decision has been made in the past?
If you are investigating the history, I'd also recommend looking at the changes that have been merged so far. You can see that incorrect changes have been landed multiple times, probably because the code is too complex to fully understand. I think your concern about "the knowledge hidden in ..." has already been realized in practice.
---
> Risk of correctness issue if SCEV changes in future.
As for addrecs, I think the semantics of "nowrap properties under some context" is very unclear, or even ill-defined. Consider the following code (assuming 64-bit integers):
```c
a0 = (1 << 62) + 1;
a1 = (1 << 62) + 2;
for (i = 0; i < 100; i++) {
if (i % 4 == 0) {
A[a0 * i] = ...;
A[a1 * i] = ...;
}
}
```
The addrecs for the two accesses would be something like `{0,+,a0}` and `{0,+,a1}`, respectively, where `a0 = 2^62 + 1` and `a1 = 2^62 + 2`. So, are they `nsw` under the condition `i % 4 == 0`? Well, `{0,+,a0}` will evolve as follows:
| `0` | `1` | `2` | `3` | `4` | `5` | ... |
|-|-|-|-|-|-|-|
| `0` | `2^62 + 1` | `-2^63 + 2` (overflow) | `-2^62 + 3` | `4` | `2^62 + 5` | ... |
Yeah, the addrec doesn't overflow "when it is executed". The same applies to `{0,+,a1}`. So, is it useful? I don't think so. Moreover, it looks like a deterioration to me. You might expect a simpler condition such as `i < 3000`, but things are not that simple. In any case, if such a change were made in the future, other parts of DA would also break severely. It doesn't make much sense to me to worry about such a future change to SCEV only in a specific part of DA.
https://github.com/llvm/llvm-project/pull/179665
More information about the llvm-commits
mailing list