[PATCH] D120386: [LoopInterchange] Try to achieve the most optimal access pattern after interchange

Congzhe Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 22 22:04:17 PST 2022


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

Motivated by pr43326 (https://bugs.llvm.org/show_bug.cgi?id=43326), where a slightly modified test case is as follows.

  void f(int e[10][10][10], int f[10][10][10]) {
    for (int a = 0; a < 10; a++) {
      for (int b = 0; b < 10; b++) {
        for (int c = 0; c < 10; c++) {
          f[c][b][a] = e[c][b][a];
        }
      }
    }
  }

The ideal optimal access pattern after running interchange is supposed to be the following

  void f(int e[10][10][10], int f[10][10][10]) {
    for (int c = 0;  c < 10; c++) {
      for (int b = 0; b < 10; b++) {
        for (int a = 0; a < 10; a++) {
          f[c][b][a] = e[c][b][a];
        }
      }
    }
  }

Currently we interchange two times, changing the loop order from `a->b->c` to `a->c->b` and `c->a->b`. In other words, we are limited to picking up the innermost loop and finding an order that is locally optimal for it. However, the pass failed to produce the optimal loop access order which is `c->b->a`. For more complex examples what we get would be somehow quite far from the globally optimal ordering.

What is proposed in this pattern is to do a "bubble-sort" fashion when doing interchange. By comparing neighbors in `LoopList` in each iteration, we would be able to move each loop onto a most appropriate place, hence this is an approach that tries to achieve the globally optimal ordering.

To be specific, for the motivating example, the original order is

  a->b->c

In the first iteration, we first interchange `b` and `c`, which gives us

  a->c->b,

then we interchange `a` and `c`, which gives us

  c->a->b.

Then in the second iteration, we interchange `a` and `b`, which gives us

  c->b->a,

which is the ideal ordering.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120386

Files:
  llvm/lib/Transforms/Scalar/LoopInterchange.cpp
  llvm/test/Transforms/LoopInterchange/phi-ordering.ll
  llvm/test/Transforms/LoopInterchange/pr43326-ideal-access-pattern.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120386.410708.patch
Type: text/x-patch
Size: 10207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220223/c45e056b/attachment.bin>


More information about the llvm-commits mailing list