[all-commits] [llvm/llvm-project] a4e88c: [Reassociation] Only form CSE expressions for loca...

qcolombet via All-commits all-commits at lists.llvm.org
Mon Jun 26 03:00:52 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: a4e88cba183719531cf3ce92da392ea72fdacaca
      https://github.com/llvm/llvm-project/commit/a4e88cba183719531cf3ce92da392ea72fdacaca
  Author: Quentin Colombet <quentin.colombet at gmail.com>
  Date:   2023-06-26 (Mon, 26 Jun 2023)

  Changed paths:
    M llvm/lib/Transforms/Scalar/Reassociate.cpp
    A llvm/test/Transforms/Reassociate/defeat-licm.ll
    A llvm/test/Transforms/Reassociate/local-cse.ll

  Log Message:
  -----------
  [Reassociation] Only form CSE expressions for local operands

 # TL;DR #
This patch constrains how much freedom the heuristic that tries to from CSE
expressions has. The added constrain is that the CSE-able expressions must be
within the same basic block as the expressions they get moved before.

 # Details #
The reassociation pass currently tweaks the rewrite of the final expression
towards surfacing pairs of operands that would be CSE-able.

This heuristic applies after the regular ordering of the expression.
The regular ordering uses the program structure to choose in which order each
subexpression is materialized. That order follows the topological order.

Now, to expose more CSE opportunities, this heurisitc effectively bypasses the
previous ordering normally defined by the program and pushes up sub-expressions
that are arbitrary deep in the CFG.
E.g., let's say the program order (top to bottom) gives `((a*b)*c)*d)*e` and
`b*e` appears the most in the program. The expression will be reordered in
`(((b*e)*a)*c)*d`

This reordering implies that all the sub expressions (in this example `xx*a`,
then `yy*c`, etc.) will need to appear after the CSE-able expression.

This may over-constrain where the (sub) expressions may live and in particular
it may create loop-dependent expressions.

This patch only allows to move expressions up the expression chain when the
related values are definied in the same basic block as the ones they
"push-down".

This constrain is far for being perfect but at least it avoids accidentally
creating loop dependent variables.

If we really want to expose CSE-able expressions in a proper way, we would need
a profitability metric and also make the decision globally as opposed to one
chain at a time.

I've put the new constrain behind an option to make comparing the old and new
versions easy. However, I believe that even if we find cases where the old
version performs better it is probably by accident. What I am aiming for with
this change is more predictability, then we can improve if need be.

This fixes www.llvm.org/PR61458

Differential Revision: https://reviews.llvm.org/D147457




More information about the All-commits mailing list