[llvm] [LoopInterchange] Improve profitability check for vectorization (PR #133672)
Ryotaro Kasuga via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 22 10:39: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;
----------------
kasuga-fj wrote:
> Assuming `>` here means the dependence-vector part of it (since you current encoding puts `*` for backward dependencies):
Correct, I was talking about the original dependence vector returned by `DI->depends`.
> An analysis returning `[* >]` is unlikey, but could be possible because pessimizing `[< >]` to `[* >]` should be conservatively correct. It is not reversed though, because `FullDependence::isDirectionNegative` stops at `*`-like dependencies.
What I meant is that it might be more convenient for LoopInterchange if `[* >]` is normalized to `[* <]`. In other words, it may be useful if `FullDependence::isDirectionNegative` doesn’t stop at `*`-like dependencies.
I don’t think it’s very rare to have a dependence vector with `*` as its head element. For example, consider a case where the outermost loop has scalar dependencies (I don't know if ), like in the following example (I found such cases while investigating TSVC):
```c
for (int n_times = 0; n_times < NTIMES; ++n_times)
for (int i = 0; i < N; ++i)
for (int j = 1; j < M; ++j)
aa[j][i] = aa[j - 1][i] + 1; // This statement itself doesn't depend on `n_times`
```
The direction vector in the above example is `[* = >]`. Interchanging the i-loop and j-loop is legal (I believe), but it is currently rejected because `[= >]` is lexicographically negative. Alternatively, if the outermost one is not counted as a loop, the direction vector would be normalized to `[= <]` and the interchange would be legal.
So, I'm thinking that it may be better if direction vectors like `[* = >]` were normalized to `[* = <]`. This could probably be done by changing `FullDependence::isDirectionNegative` so that it doesn't stop at `*`. And what I'd like to ask is: Are there any concerns that come to mind? One thing that comes to my mind is that it can change the type of the dependence (flow/anti/output), but it is not very important for LoopInterchange, which is currently the only client of `Dependence::normalize`.
https://github.com/llvm/llvm-project/pull/133672
More information about the llvm-commits
mailing list