[PATCH] D152281: [Transforms][LICM] Add the ability to undo unprofitable reassociation

Paul Osmialowski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 6 09:09:05 PDT 2023


pawosm01 created this revision.
pawosm01 added reviewers: qcolombet, fhahn.
Herald added subscribers: StephenFan, asbirlea, hiraditya.
Herald added a project: All.
pawosm01 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Consider the following piece of code:

  void innermost_loop(int i, double d1, double d2, double delta, int n, double cells[n])
  {
    int j;
    const double d1d = d1 * delta;
    const double d2d = d2 * delta;
  
    for (j = 0; j <= i; j++)
      cells[j] = d1d * cells[j + 1] + d2d * cells[j];
  }

When compiling at -Ofast level, after the "Reassociate expressions"
pass, this code is transformed into an equivalent of:

  int j;
  
  for (j = 0; j <= i; j++)
    cells[j] = (d1 * cells[j + 1] + d2 * cells[j]) * delta;

Effectively, the computation of those loop invariants isn't done
before the loop anymore, we have one extra multiplication on each
loop iteration instead. Sadly, this results in a significant
performance hit.

Similarly, specifically crafted user code will also experience
inability to hoist those invariants.

This patch is solving this issue by adding the ability to undo such
reassociation into the LICM pass. Note that for doing such
transformation this pass requires the same conditions as the
"Reassociate expressions" pass, namely, the involved binary operators
must have the reassociations allowed (e.g. by specifying the `fast`
attribute) and they must have single use only.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152281

Files:
  llvm/lib/Transforms/Scalar/LICM.cpp
  llvm/test/Transforms/LICM/expr-reassociate.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152281.528898.patch
Type: text/x-patch
Size: 13638 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230606/cd22079e/attachment.bin>


More information about the llvm-commits mailing list