Issue 58170
Summary Loop Interchange bug due to loop guards not being interchanged
Labels new issue
Assignees
Reporter alexgatea
    Right now loop guards are not interchanged which leads to incorrect control flow after the interchange.
For example, consider the following code before the interchange.
```
if(n > 0)
  for(int i=0; i != n; i++)
    if(m > 0)
      for(int j=0; j != m; j++)
         A[j][i] += B[j][i] + 1;
```
IR: [before.ll](https://github.com/llvm/llvm-project/files/9717558/before.ll.txt)

After interchange:
```
if(n > 0)
  for(int j=0; j != m; j++)
    for(int i=0; i != n; i++) {
      if(m > 0)
         A[j][i] += B[j][i] + 1;
      else break;
    }
```
IR: [after.ll.txt](https://github.com/llvm/llvm-project/files/9717613/after.ll.txt)

If m is -1 at run-time then the original code only executes the outer for-i loop whereas the resulting code is an infinite loop (since `j != m` always evaluates to false).

Possible solutions under consideration:
- See if running simple loop unswitch or LICM prior to loop interchange fixes the issue. I tried this and it doesn't seem to help; loop unswitch does nothing and LICM hoists the computation of the condition m > 0 out of the for-i loop but the branch remains in place (inside the for-i loop). Still, this might be able to be tweaked to work, I'm not sure.
- View non-user generated loop guards (checked via `isGuarded()` for example) as part of anatomy of a loop.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to