[all-commits] [llvm/llvm-project] c71484: [AArch64] Add an AArch64 pass for loop idiom trans...

David Sherwood via All-commits all-commits at lists.llvm.org
Tue Jan 9 03:29:41 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c7148467fc08eefaaae876c7d11d629c849f42cf
      https://github.com/llvm/llvm-project/commit/c7148467fc08eefaaae876c7d11d629c849f42cf
  Author: David Sherwood <57997763+david-arm at users.noreply.github.com>
  Date:   2024-01-09 (Tue, 09 Jan 2024)

  Changed paths:
    M llvm/include/llvm/Analysis/TargetTransformInfo.h
    M llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
    M llvm/lib/Analysis/TargetTransformInfo.cpp
    M llvm/lib/Target/AArch64/AArch64.h
    A llvm/lib/Target/AArch64/AArch64LoopIdiomTransform.cpp
    A llvm/lib/Target/AArch64/AArch64LoopIdiomTransform.h
    M llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
    M llvm/lib/Target/AArch64/AArch64TargetMachine.h
    M llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h
    M llvm/lib/Target/AArch64/CMakeLists.txt
    A llvm/test/Transforms/LoopIdiom/AArch64/byte-compare-index.ll
    M llvm/utils/gn/secondary/llvm/lib/Target/AArch64/BUILD.gn

  Log Message:
  -----------
  [AArch64] Add an AArch64 pass for loop idiom transformations (#72273)

We have added a new pass that looks for loops such as the following:

```
  while (i != max_len)
      if (a[i] != b[i])
          break;

  ... use index i ...
```

Although similar to a memcmp, this is slightly different because instead
of returning the difference between the values of the first non-matching
pair of bytes, it returns the index of the first mismatch. As such, we
are not able to lower this to a memcmp call.

The new pass can now spot such idioms and transform them into a
specialised predicated loop that gives a significant performance
improvement for AArch64. It is intended as a stop-gap solution until
this can be handled by the vectoriser, which doesn't currently deal with
early exits.

This specialised loop makes use of a generic intrinsic that counts the
trailing zero elements in a predicate vector. This was added in
https://reviews.llvm.org/D159283 and for SVE we end up with brkb & incp
instructions.

Although we have added this pass only for AArch64, it was written in a
generic way so that in theory it could be used by other targets.
Currently the pass requires scalable vector support and needs to know
the minimum page size for the target, however it's possible to make it
work for fixed-width vectors too. Also, the llvm.experimental.cttz.elts
intrinsic used by the pass has generic lowering, but can be made
efficient for targets with instructions similar to SVE's brkb, cntp and
incp.

Original version of patch was posted on Phabricator:

 https://reviews.llvm.org/D158291

Patch co-authored by Kerry McLaughlin (@kmclaughlin-arm) and David
Sherwood (@david-arm)

See the original discussion on Discourse:

https://discourse.llvm.org/t/aarch64-target-specific-loop-idiom-recognition/72383




More information about the All-commits mailing list