[PATCH] D142255: [WIP] Loop peeling opportunity for identity operators

Jamie Schmeiser via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 20 13:58:26 PST 2023


jamieschmeiser created this revision.
jamieschmeiser added a reviewer: LoopOptWG.
Herald added subscribers: wenlei, zzheng, hiraditya, nemanjai.
Herald added a project: All.
jamieschmeiser requested review of this revision.
Herald added a subscriber: pcwang-thead.
Herald added a project: LLVM.

This is a work in progress and is to aid discussion in the loop opt working group.

An opportunity for peeling exists for loops like the following:

unsigned Sum = 0;
for (unsigned I = 0; I < 999; ++I)

  Sum += g();

In this situation, the initial value results in an identity operation on the first iteration of the loop.
This loop could be peeled to avoid this operation:

unsigned Sum = g();
for (unsigned I = 0; I < 998; ++I)

  Sum += g();

This idiom frequently occurs in the form of summing up the values in an array.  It can also occur with *=, &&= and ||=.

The number of tests requiring changes here illustrates that this is a common idiom.  There may be more tests that need updating
but since this is currently for discussion purposes, this task has not been completed.  These tests were mostly altered by dis-allowing peeling or changing the initial value to 1 from 0, making it not an identity operation.

There are several questions about the role of peeling in the llvm opt pipeline but the main one raised by this sample follows:  This will likely be a relatively minor small improvement and it may interfere with other optimizations  so is it worth pursuing?

For example, there is an attempt to not interfere with vectorization in the code as it exists.  This heuristic needs improvement as the above sample, for example, will not peel if the upper bound is 10,000 as it may fail the heuristic on some hardware.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142255

Files:
  llvm/lib/Transforms/Utils/LoopPeel.cpp
  llvm/test/Transforms/LoopUnroll/AArch64/runtime-loop.ll
  llvm/test/Transforms/LoopUnroll/ARM/multi-blocks.ll
  llvm/test/Transforms/LoopUnroll/ARM/v6munroll.ll
  llvm/test/Transforms/LoopUnroll/PowerPC/a2-unrolling.ll
  llvm/test/Transforms/LoopUnroll/X86/partial.ll
  llvm/test/Transforms/LoopUnroll/followup.ll
  llvm/test/Transforms/LoopUnroll/partially-unroll-unconditional-latch.ll
  llvm/test/Transforms/LoopUnroll/peel-loop-identity-op.ll
  llvm/test/Transforms/LoopUnroll/peel-loop-not-forced.ll
  llvm/test/Transforms/LoopUnroll/peel-to-turn-invariant-accesses-dereferenceable.ll
  llvm/test/Transforms/LoopUnroll/runtime-epilog-debuginfo.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop-at-most-two-exits.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop2.ll
  llvm/test/Transforms/LoopUnroll/runtime-loop3.ll
  llvm/test/Transforms/LoopUnroll/runtime-multiexit-heuristic.ll
  llvm/test/Transforms/LoopUnroll/unroll-cost-symbolic-execute.ll
  llvm/test/Transforms/LoopUnroll/unroll-heuristics-pgo.ll
  llvm/test/Transforms/PhaseOrdering/ARM/arm_mean_q7.ll
  llvm/test/Transforms/PhaseOrdering/SystemZ/sub-xor.ll
  llvm/test/Transforms/PhaseOrdering/X86/ctlz-loop.ll
  llvm/test/Transforms/PhaseOrdering/pr44461-br-to-switch-rotate.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142255.490967.patch
Type: text/x-patch
Size: 56316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230120/76509240/attachment.bin>


More information about the llvm-commits mailing list