[PATCH] D102300: [LoopInterchange] Check lcssa phis in the inner latch in scenarios of multi-level nested loops

Congzhe Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 19:34:07 PDT 2021


congzhe created this revision.
congzhe added reviewers: Whitney, bmahjour.
congzhe added a project: LLVM.
Herald added a subscriber: hiraditya.
congzhe requested review of this revision.
Herald added a subscriber: llvm-commits.

This is a fix in legality check.

We already know that we need to check whether lcssa phis are supported in inner loop exit block or in outer loop exit block, and we have logic to check them already. Presumably the inner loop latch does not have lcssa phis and there is no code that deals with lcssa phis in the inner loop latch.

However, that assumption is not true, when we have loops with more than two-level nesting. Take a triply nested loop as an example (which is provided as the test case in this patch). Suppose we do interchange two times for the innermost/middle loop, and the (new) middle/outermost loop. I'll use the following terminologies:

The old innermost/middle/outermost header/latch -> the very initial loop nest.
The intermediate innermost/middle/outermost header/latch -> the loop nest after 1st interchange.
The final innermost/middle/outermost header/latch -> the loop nest after 2nd interchange.

If the old innermost latch uses a value defined in the old middle header, then after 1st interchange, it becomes that the intermediate middle latch uses a value defined in the intermediate inner header. This is exactly when there is an lcssa phi node in  "BasicBlock* InnerLooplatch". Note that since we are in the stage of doing the 2nd interchange,  "BasicBlock* InnerLooplatch" is the intermediate middle latch.

This patch added a function `areInnerLoopLatchPHIsSupported()` to check the legality in `InnerLoopLatch`. Note that the intermediate middle latch will become the final outermost latch, and in certain cases the values in the intermediate innermost loop will not be available in the final outermost latch, e.g., when the old outermost header can jump directly to the old outermost latch.

As an example, for the test case provided in this patch, it will run the 1st interchange without any problem, but in the 2nd interchange it will crash or hit assertion errors with the current trunk. The legality check added in this patch catches the IR after the 1st interchange and bail out the 2nd interchange.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102300

Files:
  llvm/lib/Transforms/Scalar/LoopInterchange.cpp
  llvm/test/Transforms/LoopInterchange/innermost-latch-uses-values-in-middle-header.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102300.344633.patch
Type: text/x-patch
Size: 6121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210512/5ac48a9f/attachment.bin>


More information about the llvm-commits mailing list