[llvm] [LoopInterchange] Improve profitability check for vectorization (PR #133672)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 23 04:32:10 PDT 2025
================
@@ -180,10 +197,22 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
// Track Output, Flow, and Anti dependencies.
if (auto D = DI->depends(Src, Dst)) {
assert(D->isOrdered() && "Expected an output, flow or anti dep.");
+ bool IsForward = true;
+
+ // If Src and Dst are in the same BB, Src is always executed before Dst
+ // in the same loop iteration. If not, we must check whether one BB
+ // dominates the other to determine if Src and Dst are executed in this
+ // order. At the moment, we don't perform such check.
+ if (Src->getParent() != Dst->getParent())
+ IsForward = false;
+
// If the direction vector is negative, normalize it to
// make it non-negative.
- if (D->normalize(SE))
+ bool Normalized = D->normalize(SE);
+ if (Normalized) {
LLVM_DEBUG(dbgs() << "Negative dependence vector normalized.\n");
+ IsForward = false;
----------------
Meinersbur wrote:
`*` may just mean "not analyzable", and as mentioned, it might effectively be `<`, but DA was not able to detect it as such. Reversing the dependence vector would be wrong.
`*` might also mean "sometimes <, sometimes >" depending on control flow. In that case there is no single correct normlization of the dependence vector, both (as-is and reversed) would time-negative in some cases.
```c
for (int i = 0; i < 100; ++i) {
A[99 - i] = ..;
use(A[i]); // flow dependency with i >= 50, anti-dependency with i < 50
}
```
Since because of this one cannot assume that the dependency vector is positive even after normalization, it could be considered a heuristic, and it might be reasonable to assume that the `*` is a non-detected `=` direction due to symmetry of `<` and `>`. Could we do that in a different patch, it feels risky.
A better modeling might actually be that a `FullDependence` represents two dependencies: From `Src` to `Dst` and the anti-dependency from `Dst` to `Src`. One of each may actually be empty, because with `[= <]` there is no dependency from `Dst` to `Src`, and with `[= >]` there is not dependency from `Src` to `Dst`. But with `[*]`, neither directions would bne ruled out and one has to pessimistically assume both.
https://github.com/llvm/llvm-project/pull/133672
More information about the llvm-commits
mailing list