Issue 235496
Summary [LoopInterchange] Make code transformation more robust
Labels loopoptim
Assignees kasuga-fj
Reporter kasuga-fj
    We have repeatedly run into problems with the IR transformation in LoopInterchange, mostly caused by mishandling of PHI nodes. I think the root cause of these bugs is the complexity of the transformation phase of LoopInterchange. As has been pointed out before (e.g., https://github.com/llvm/llvm-project/pull/212742#pullrequestreview-4958634586), it would be desirable to replace the existing mechanism to make the transformation more robust.

I think the problem with the current transformation phase is that it tries to achieve the interchange by changing the structure of the CFG itself. This makes it difficult to track precisely which instructions depend on which, and makes PHI nodes easy to mishandle. Instead, I'd like to propose achieving the interchange while keeping the structure of the CFG as it is, and only modifying the instructions within the basic blocks. This removes the need for the complicated PHI-node rewiring, the updates of the DT and LoopInfo, and so on. Specifically, given a loop nest like the following:

```llvm
entry:
  br label %for.i.header

for.i.header:
  %i = phi i64 [ %start.i, %entry ], [ %i.next, %for.i.latch ]
  br label %for.j.header

for.j.header:
  %j = phi i64 [ %start.j, %for.i.header ], [ %j.next, %for.j.latch ]
 ...

for.j.latch:
  %j.next = add i64 %j, 1
  %ec.j = icmp eq i64 %j.next, %end.j
  br i1 %ec.j, label %for.i.latch, label %for.j.header

for.i.latch:
  %i.next = add i64 %i, 1
  %ec.i = icmp eq i64 %i.next, %end.i
  br i1 %ec.i, label %exit, label %for.i.header

exit:
  ...
```

we transform it as follows:

- Swap the IVs and the like between the two headers.
- Introduce new counters that count the iterations of the loops, and control the loops by these counters.

```llvm
entry:
  %tc.outer = sub i64 %end.j, %start.j
 %tc.inner = sub i64 %end.i, %start.i
  br label %for.i.header

for.i.header:                ; now iterates over the range of j
  %outer.counter = phi i64 [ 0, %entry ], [ %outer.counter.next, %for.i.latch ]
  %j = phi i64 [ %start.j, %entry ], [ %j.next, %for.i.latch ]
  br label %for.j.header

for.j.header:                ; now iterates over the range of i
  %inner.counter = phi i64 [ 0, %for.i.header ], [ %inner.counter.next, %for.j.latch ]
  %i = phi i64 [ %start.i, %for.i.header ], [ %i.next, %for.j.latch ]
  ...

for.j.latch:
  %i.next = add i64 %i, 1
  %inner.counter.next = add i64 %inner.counter, 1
  %ec.inner = icmp eq i64 %inner.counter.next, %tc.inner
  br i1 %ec.inner, label %for.i.latch, label %for.j.header

for.i.latch:
  %j.next = add i64 %j, 1
 %outer.counter.next = add i64 %outer.counter, 1
  %ec.outer = icmp eq i64 %outer.counter.next, %tc.outer
  br i1 %ec.outer, label %exit, label %for.i.header

exit:
  ...
```

Of course, the actual cases are more complicated, but most of the cases that need to be considered are already restricted by the existing legality checks. For example, it is already required that the trip count of each loop is computable by SCEV, that the instructions in the outer loop but outside the inner loop have no side effects, and that the live-outs of the inner loop flow only into the exit of the loop nest or into reductions through PHI nodes. Therefore, what this approach additionally needs to handle is roughly the following:

- Values that are computed from the outer IVs in the outer loop header (and the inner loop preheader) and used in the inner loop are moved into the inner loop header.
- For the uses of the outer IVs after exiting the inner loop, the values at the time of exiting the inner loop are passed via LCSSA (due to the restrictions above, the values computed there only matter at the exit of the loop nest).
- If a branch before the inner loop depends on the outer IVs, the interchange is rejected by the legality check.

Also, with this approach, the complicated PHI-node rewiring and the updates of the DT and LoopInfo in the current code base mentioned above are no longer necessary. I have already tried this approach locally, and confirmed that it actually allows us to reduce the workaround-like code in the current implementation.

As a by-product, the generated IR also becomes much cleaner (or rather, it has been way too messy so far). The current implementation achieves the interchange by creating additional basic blocks, e.g., by splitting the latch and the header of the inner loop, but this is no longer needed because the new approach doesn't touch the CFG.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to