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

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 14 13:03:36 PDT 2025


kasuga-fj wrote:

> > I don't think so. For example, regarding LoopInterchange, the following case isn't currently supported, but I believe it could be extended relatively easily
> > ```c
> > for (int i = 0; i < N; i++)
> >   for (int j = 0; j < M; j++) {
> >     for (int k = 0; k < O; k++)
> >       Stmt1;
> >     for (int l = 0; l < O; l++)
> >       Stmt2;
> >   }
> > ```
> 
> Your example is not a perfect loop nest (the j-loop has two inner loops) and the legality check for loop interchange assumes a perfect loop nest (see the code snippet below taken from loop interchange source code).

Yes, they are not perfectly nested loops if we take the k-loop and l-loop into account. However, I believe it is sound to ignore them and focus only on the i-loop and j-loop. LoopInterchange currently doesn't have the logic to ignore the k-loop and l-loop, but such functionality could be added in the future. In that case, I think the fusion assumption could lead to something unexpected.

In addition, I suspect a similar problem could arise in LoopFuse. What happens when we attempt to fuse the k0-loop and k1-loop in the example below? Is it sound to assume that the l0-loop and l1-loop are fused?

```c
for (int i = 0; i < M; i++)
  for (int j = 0; j < N; j++) {
    for (int k0 = 0; k0 < O; k0++)
      for (int l0 = 0; l0 < P; l0++)
        Stmt1;
    for (int k1 = 0; k1 < O; k1++)
      for (int l1 = 0; l1 < P; l1++)
        Stmt2;
  }
```

> But anyways, let's use that flag to address this concern.

Just in case: I don't request to add such a flag. I think it's also okay if you can prove the soundness of the new assumption. But if adding such a flag is more convenient, then I don't have strong objections at the moment.

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


More information about the llvm-commits mailing list