[PATCH] D150535: [IR][TRE] Support associative intrinsics

Joshua Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 14 23:13:41 PDT 2023


caojoshua created this revision.
Herald added subscribers: laytonio, hiraditya.
Herald added a project: All.
caojoshua updated this revision to Diff 522046.
caojoshua edited the summary of this revision.
caojoshua added a comment.
caojoshua updated this revision to Diff 522050.
caojoshua edited the summary of this revision.
caojoshua updated this revision to Diff 522052.
caojoshua edited the summary of this revision.
caojoshua added reviewers: avl, efriedma.
caojoshua published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Update commit msg


caojoshua added a comment.

include basic benchmark note in commit msg


caojoshua added a comment.

Note on updates in other passes


There is support for intrinsics in Instruction::isCommunative, but there
is no equivalent implementation for isAssociative. This patch builds
support for associative intrinsics with TRE is an application. TRE can
now have associative intrinsics as an accumulator. For example:

  struct Node {
    Node *next;
    unsigned val;
  }
  
  unsigned maxval(struct Node *n) {
    if (!n) return 0;
    return std::max(n->val, maxval(n->next));
  }

Can be transformed into:

  unsigned maxval(struct Node *n) {
    struct Node *head = n;
    unsigned max = 0; // Identity of unsigned std::max
    while (true) {
      if (!head) return max;
      max = std::max(max, head->val);
      head = head->next;
    }
    return max;
  }

This example results in about 5x speedup in local runs.

We conservatively only consider min/max and saturating add intrinsics as
associative for this patch to limit testing scope. There are probably
other intrinsics that could be considered associative. There are a few
consumers of isAssociative() that could be impacted. Testing has only
required to Reassociate pass be updated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150535

Files:
  llvm/include/llvm/IR/Constants.h
  llvm/include/llvm/IR/IntrinsicInst.h
  llvm/lib/IR/Constants.cpp
  llvm/lib/IR/Instruction.cpp
  llvm/lib/Transforms/Scalar/Reassociate.cpp
  llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
  llvm/test/Transforms/TailCallElim/tre-basic-intrinsic.ll
  llvm/test/Transforms/TailCallElim/tre-minmax-intrinsic.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150535.522052.patch
Type: text/x-patch
Size: 18142 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230515/465e52fd/attachment.bin>


More information about the llvm-commits mailing list