[PATCH] D151988: LoopVectorize: extend SelectCmp pattern for non-invariants

Ramkumar Ramachandra via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 2 04:24:44 PDT 2023


artagnon created this revision.
artagnon added reviewers: fhahn, reames, david-arm, sdesmalen.
Herald added subscribers: shiva0217, StephenFan, arphaman, hiraditya.
Herald added a project: All.
artagnon requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead.
Herald added a project: LLVM.

Extend LoopVectorize's SelectCmp pattern for non-invariant statements,
and get LoopUtils' createCmpSelectTargetReduction to CodeGen for this
case. Consider the following example:

  int src[n] = {4, 5, 2};
  int r = 331;
  for (int i = 0; i < n; i++) {
    if (src[i] > 3)
      r = i;
  }
  return r;

Here, r = i is non-invariant, and CodeGen'ing for this case involves the
following:

1. Create a Splat with the int_min/int_max values, depending on the loop direction.
2. The Src is filled with values: {0, 1, 2, 3, 4, ...}.
3. The Right is filled with values: {0, 1, 331, 331, 331, ...}.
4. The CmpVector is filled with values: {1, 1, 0, 0, 0, ...}.
5. Select using this CmpVector between Src and the Splat with int_min/int_max values.
6. Use a max-reduce/min-reduce on the result of the Select.
7. Use the exising Cmp which determines whether or not any assignment took place in the loop to select between the result of the max-reduce/min-reduce, and the initial value (InitVal).

Hence, the above case is vectorized.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151988

Files:
  llvm/include/llvm/Analysis/IVDescriptors.h
  llvm/include/llvm/Transforms/Utils/LoopUtils.h
  llvm/lib/Analysis/IVDescriptors.cpp
  llvm/lib/Transforms/Utils/LoopUtils.cpp
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/test/Transforms/LoopVectorize/if-reduction.ll
  llvm/test/Transforms/LoopVectorize/select-cmp.ll
  llvm/test/Transforms/LoopVectorize/select-min-index.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151988.527807.patch
Type: text/x-patch
Size: 37046 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230602/7029e47f/attachment.bin>


More information about the llvm-commits mailing list