[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
Sun May 30 18:05:10 PDT 2021


congzhe added inline comments.


================
Comment at: llvm/test/Transforms/LoopInterchange/innermost-latch-uses-values-in-middle-header.ll:16
+; CHECK: Not interchanging loops. Cannot prove legality.
+define void @innermost_latch_uses_values_in_middle_header() {
+entry:
----------------
Whitney wrote:
> congzhe wrote:
> > Whitney wrote:
> > > Is it possible to show the problem with input IR that the innermost and the middle loop is already interchanged?
> > Thanks for the comment! The difficulty is that loop interchange works on loops ordered from the innermost loop toward the outermost loop. Whenever we fail the legality check for a pair of loops, we just bail out the entire pass not trying to interchange the rest of loop nest. The pass does not "skip over" the first pair of candidate loops and go directly to the next one.
> > 
> > With the input IR that the innermost and the middle loop is already interchanged, when the pass starts working on this loop nest, it would do one of the following:
> > 
> > 1) The pass either determines it does not want to interchange the (new) innermost and the (new) middle loop likely due to profitability issue and will just bail out, not interchanging anything.
> > 
> > 2) (Even if we manually change the loop access pattern in the IR to make it pass profitability), it would interchange the the (new) innermost and the (new) middle loop which would result in a new IR that does not expose the problem we would like to expose.
> > 
> > Therefore I thought the current test case might be a in a sense a straightforward one to catch the problem.  
> > - With the trunk code, it run the 1st interchange and crashes on the 2nd interchange. 
> > - With this patch, it run the 1st interchange and bails out the 2nd interchange.
> > 
> > I'm wondering if what I described above makes sense to you? Please correct me if I missed something.
> > 
> When I run this test case with my opt, I got:
> ```
> Loops are legal to interchange
> Loops interchanged.
> Not legal because of current transform limitation
> Not interchanging loops. Cannot prove legality.
> ```
> Can you please check with the latest trunk code, and see if you still see a crash on the 2nd interchange?
> The commit-hash I tried with is `1e344ce4f3fac4beb5e23e04dc3dd59398125956`.
That's right, the latest trunk code does not crash because of my recent patch: https://reviews.llvm.org/D101305. That patch makes the pass bail out when it detects triangular loops, however, it also catches this IR accidently because SCEV fails to detect an lcssa variable is loop invariant. So the trunk code bails out and just hides the problem described in this patch. 

I can modify my other patch by adding some logic that deals with lcssa phis when calling SCEV, which will expose the crash again. Essentially what this means is that at the end of funciton `isLoopStructureUnderstood()`, I can simply change this piece of code 
```
const SCEV *S = SE->getSCEV(Right);
    if (!SE->isLoopInvariant(S, OuterLoop))
      return false;
```
to 
```
const SCEV *S = SE->getSCEV(followLCSSA(Right));
    if (!SE->isLoopInvariant(S, OuterLoop))
      return false;
```

If needed I can add that change to the current patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102300/new/

https://reviews.llvm.org/D102300



More information about the llvm-commits mailing list