<div dir="ltr"><div><div>Thanks for writing this up. I'd like to have this intrinsic too.<br><br></div><div>Another argument for having the intrinsic is shown in PR37426:<br></div><a href="https://bugs.llvm.org/show_bug.cgi?id=37426" target="_blank">https://bugs.llvm.org/show_bug<wbr>.cgi?id=37426</a><br><br></div>Vectorization goes overboard because the throughput cost model used by the vectorizers doesn't match the 6 IR instructions that correspond to 1 x86 rotate instruction. Instead, we have:<br><div><br>$ opt 37426prevectorize.ll -S -cost-model -analyze<br>...<br>Cost Model: Found an estimated cost of 1 for instruction: %and = and i32 %cond, 31<br>Cost Model: Found an estimated cost of 1 for instruction: %shl = shl i32 %1, %and<br>Cost Model: Found an estimated cost of 1 for instruction: %sub = sub nsw i32 0, %cond<br>Cost Model: Found an estimated cost of 1 for instruction: %and5 = and i32 %sub, 31<br>Cost Model: Found an estimated cost of 1 for instruction: %shr = lshr i32 %1, %and5<br>Cost Model: Found an estimated cost of 1 for instruction: %or = or i32 %shl, %shr<br><br></div><div>The broken cost model also affects unrolling and inlining. Size costs are overestimated for a target that has a rotate instruction.<br>This cost problem isn't limited to rotate patterns (it's come up in the context of min/max/abs/fma too). But it would be simpler if we had a rotate intrinsic, and the 6-to-1 margin is the biggest I've seen.<br></div><div><br></div><div>Another potential benefit of a generic intrinsic is that it can replace target-specific intrinsics. PowerPC and x86 have those. ARM translates source-level target-specific intrinsics into regular IR, so that could use the intrinsic too.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 14, 2018 at 2:53 AM, Fabian Giesen via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi everyone!<br>
<br>
I recently ran into some interesting issues with generation of rotate instructions - the details are in the bug tracker (<a href="https://bugs.llvm.org/show_bug.cgi?id=37387" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bu<wbr>g.cgi?id=37387</a> and related bugs) for those interested - and it brought up the issue of rotates in the IR again. Now this is a proposal that has been made (and been rejected) several times, but I've been told that this time round we ran into issues not previously considered, and that I should bring the topic up on llvm-dev.<br>
<br>
I'll try to summarize the status quo first and then bring up the tricky cases that might tip the balance in favor of rotate intrinsics.<br>
<br>
Rotates<br>
=======<br>
<br>
Rotates, or circular shifts, are bit shifts where bits "dropped out" on one side of the operand are shifted back in on the other.<br>
<br>
They occur less frequently in real-world code than regular (non-circular) bit shifts, but are nevertheless directly supported in most popular ISAs. They are useful primitives for cryptography, non-cryptographic hashing, pseudo-random number generation, bit I/O, and probably other cases as well.<br>
<br>
They do not have a direct representation in C/C++, but several compilers support them via intrinsics (including MSVC, which Clang-CL tries to be compatible to), and some languages such as Rust include them explicitly.<br>
<br>
The status quo<br>
==============<br>
<br>
Rotates by compile-time constant amounts are fairly easy to express in C-like languages by ORing together two opposite-direction shifts. For example, rotating a uint32_t x left by 3 positions in C boils down to<br>
<br>
(x << 3) | (x >> 29)<br>
<br>
which is simple enough. The backends with rotate support detect this type of pattern at the DAG stage and map it to a rotate instruction.<br>
<br>
This, in a nutshell, is the argument against a direct rotate in the IR: the existing primitives are sufficient to model it, and doing so allows LLVM to leverage transforms and analyses such as SimplifyDemandedBits that know how to deal with the more common regular shifts to work on bit rotate expressions as well. Adding a new primitive would mean that a whole bunch of optimization passes would need to know how to deal with it (or else they might lose on optimization opportunities).<br>
<br>
Variable-distance rotates<br>
=========================<br>
<br>
Things become murkier when we consider not just rotates by a compile-time constant, but also rotates where the amount is determined at run time.<br>
<br>
For one thing, it's not immediately obvious how to write a well-defined corresponding C expression: the most "natural" version (for a 32-bit left rotate) is<br>
<br>
(x << k) | (x >> (32 - k))<br>
<br>
but this is undefined for k==0 due to the right-shift of a 32-bit value by 32. That said, both GCC and Clang will generally compile this particular expression into a rotate-left as of this writing, but having UB is unfortunate. A trickier variant that is always well-defined is<br>
<br>
(x << (k & 31)) | (x >> (-k & 31))<br>
<br>
but this is a substantially more complicated sub-dag than the OR of two shifts you would see for a compile-time constant rotate distance, and there are more opportunities for things to go wrong along the way. Bug 37387 contains a few examples, the simplest of which was given by Sanjay:<br>
<br>
void rotateInLoop(unsigned *x, unsigned N, unsigned *a, int b) {<br>
for (unsigned i = 0; i < N; ++i)<br>
x[ (a[i] >> b) | (a[i] << (32 - b)) ] = i; // shift amt is loop invariant<br>
}<br>
<br>
"32 - b" is loop-invariant and gets hoisted, and what's left of the expression doesn't match a known "rotate" pattern (since the DAG can't see across BB boundaries at the moment), so instead we get two shifts and an OR inside the loop.<br>
<br>
The counterargument here was that GlobalISel should eventually fix this particular problem, but nevertheless, as of today, LICM breaks recognition of the rotate pattern in this (very simple) loop, and there is reason to believe that other transforms (existing or forthcoming) might well break the pattern as well; the issues referenced in the linked bug illustrate some of the existing failure modes.<br>
<br>
This situation is frustrating for users of variable-distance rotates; right now, there simply is no way to write code in a source language that will reliably turn into rotates; you either have to watch the generated code closely, or just throw up your hands and use platform-dependent inline assembly. (It is assumed that the code in question is in a hot spot.)<br>
<br>
The proposal<br>
============<br>
<br>
The idea would be to add rotate intrinsics, primarily intended to be used for the variable-distance case, which is otherwise fragile. <br>
<br>
Frontends can emit them directly (for source language-level intrinsics, or when matched from "known good" rotate patterns) and the backends can match them and select appropriate instructions.<br>
<br>
For the constant-distance case, the preferred form would still be the OR-of-shifts variant, which plays nice with SimplifyDemandedBits and is known to existing optimization passes.<br>
<br>
SDB does not currently do anything with shifts by non-constant amounts anyway; and, for the use cases in question, a rotate that is not touched by other optimization passes is still preferable to a more complex expression that can be partially optimized, but then ends up in a form that the backend doesn't know how to turn back into a rotate, which is a net loss.<br>
<br>
Thoughts?<br>
<br>
-Fabian<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div></div>