[all-commits] [llvm/llvm-project] e12484: [LoopIdiom] Introduce 'left-shift until bittest' i...

Roman Lebedev via All-commits all-commits at lists.llvm.org
Wed Dec 23 11:28:34 PST 2020


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: e124844709228a076483d8b6101bbb362caf625f
      https://github.com/llvm/llvm-project/commit/e124844709228a076483d8b6101bbb362caf625f
  Author: Roman Lebedev <lebedev.ri at gmail.com>
  Date:   2020-12-23 (Wed, 23 Dec 2020)

  Changed paths:
    M llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    M llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll

  Log Message:
  -----------
  [LoopIdiom] Introduce 'left-shift until bittest' idiom

The motivation here is the following inner loop in fp16/fp24 -> fp32 expander,
that runs as part of the floating-point DNG decompression in RawSpeed library:
https://github.com/darktable-org/rawspeed/blob/cd380bb9a209bd2e7a0e7022b0cab04528d151e7/src/librawspeed/decompressors/DeflateDecompressor.cpp#L112-L115
```
      while (!(fp32_fraction & (1 << 23))) {
        fp32_exponent -= 1;
        fp32_fraction <<= 1;
      }
```
(https://godbolt.org/z/r13YMh)
As one might notice, that loop is currently uncountable, and that whole code stays scalar.
Yet, it is rather trivial to make that loop countable:
 https://godbolt.org/z/do8WMz
and we can prove that via alive2:
 https://alive2.llvm.org/ce/z/7vQnji (ha nice, isn't it?)
... and that allow for the whole fp16->fp32 code to vectorize:
 https://godbolt.org/z/7hYr13

Now, while i'd love to get there, i feel like i should take it in steps.

For now, this introduces support for the most basic case,
where the bit position is known as a variable,
and the loop *will* go away (has no live-outs other than the recurrence,
no extra instructions in the loop).

I have added sufficient (i believe) test coverage,
and alive2 is happy with those transforms.

Reviewed By: craig.topper

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


  Commit: cb2e5980bae3bead524895f4f36e4f71cd764a1b
      https://github.com/llvm/llvm-project/commit/cb2e5980bae3bead524895f4f36e4f71cd764a1b
  Author: Roman Lebedev <lebedev.ri at gmail.com>
  Date:   2020-12-23 (Wed, 23 Dec 2020)

  Changed paths:
    M llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    M llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll

  Log Message:
  -----------
  [LoopIdiom] 'left-shift until bittest' idiom: support constant bit mask

The handing of the case where the mask is a constant is trivial,
if said constant is a power of two, the bit in question is log2(mask),
rest just works.

Reviewed By: craig.topper

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


  Commit: a0ddc61c5b95be2585b6b4981cf8634d7a00e4a6
      https://github.com/llvm/llvm-project/commit/a0ddc61c5b95be2585b6b4981cf8634d7a00e4a6
  Author: Roman Lebedev <lebedev.ri at gmail.com>
  Date:   2020-12-23 (Wed, 23 Dec 2020)

  Changed paths:
    M llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    M llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll

  Log Message:
  -----------
  [LoopIdiom] 'left-shift until bittest' idiom: support canonical sign bit mask

If the bitmask is for sign bit, instcombine would have canonicalized
the pattern into a proper sign bit check. Supporting that is still
simple, but requires a bit of a roundtrip - we first have to use
`decomposeBitTestICmp()`, and the rest again just works.

Reviewed By: craig.topper

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


  Commit: 2b61e7c68cda16159b186fea1a1b1252b94b7e16
      https://github.com/llvm/llvm-project/commit/2b61e7c68cda16159b186fea1a1b1252b94b7e16
  Author: Roman Lebedev <lebedev.ri at gmail.com>
  Date:   2020-12-23 (Wed, 23 Dec 2020)

  Changed paths:
    M llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    M llvm/test/Transforms/LoopIdiom/X86/left-shift-until-bittest.ll

  Log Message:
  -----------
  [LoopIdiom] 'left-shift until bittest' idiom: support rewriting loop as countable, allow extra cruft

The current state of the transform is still not enough to support
my motivational pattern, because it has one more "induction variable".

I have delayed posting this patch, because originally even just rewriting
the loop as countable wasn't enough to nicely transform my motivational pattern,
because i expected that extra IV to be rewritten afterwards,
but it wasn't happening until i fixed that in D91800.

So, this patch allows the  'left-shift until bittest' loop idiom
as long as the inserted ops are cheap,
and lifts any and all extra use checks on the instructions.

Reviewed By: craig.topper

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


Compare: https://github.com/llvm/llvm-project/compare/4c37453a04f9...2b61e7c68cda


More information about the All-commits mailing list