[llvm] [DependenceAnalysis] Extending SIV to handle fusable loops (PR #128782)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 09:40:53 PDT 2025


kasuga-fj wrote:

My concerns are:

- Are such changes of the results always improvements?
- Is it sound for passes other than LoopFuse to use such improved results?

As for the first one, at least the change to `isConsistent` doesn't seem to be an improvement. Also, I think it’s difficult to manage it correctly while holding an assumption of fusion. That said, it also seems that no one actually uses that information in practice, so it may not matter (or perhaps it would be better to just remove it altogether). Regarding the other results, such as `DV`, it appears that the changes are always improvements. In the end, it seems to me that these result changes are basically improvements.

What I'm more concerned about is the second question. For example, consider a case where a loop guard exists between the two fusable loops, such as:

```c
for (int i = 0; i < N; i++) {
  for (int j = 0; j < M; j++)
    Stmt0;
  if (cond)
    for (int j = 0; j < M; j++)
      Stmt1;
}
```

`Stmt1` can have information inferred from `cond`, such as no-wrap flags. Then, what does "let's assume the inner most loops are fused and then check the dependences" mean? At least, there seem to be two possible interpretations:


```c
// Case 1
for (int i = 0; i < N; i++)
  for (int j = 0; j < M; j++) {
    Stmt0;
    if (cond)
      Stmt1;
  }

// Case 2
for (int i = 0; i < N; i++)
  if (cond)
    for (int j = 0; j < M; j++) {
      Stmt0;
      Stmt1;
    }
```

IIUC, LoopFuse excludes such cases before calling `DependenceInfo::depends`, so it may not be relevant to LoopFuse. But what about other passes? Do they have to have additional checks like `FusableLevels == 0 || isLoopAdjust(...)`? Well, it seems to me that `areLoopsSimilar` is more pessimistic[^ctx].

In conclusion, I'm happy with the improvements themselves when there is evidence supporting their soundness. However, at least at this point, I think it has not been given much consideration.

[^ctx]: The same might be said of the current implementation. Maybe it should consider a case where `Src` and `Dst` are not in the same BB so that they have different contexts.

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


More information about the llvm-commits mailing list