How to have GCC combine “move r10, r3; store r10” into a “store r3”?

后端 未结 1 2039
半阙折子戏
半阙折子戏 2021-01-29 08:36

I\'m working Power9 and utilizing the hardware random number generator instruction called DARN. I have the following inline assembly:

uint64_t val;
__asm__ __vol         


        
相关标签:
1条回答
  • 2021-01-29 09:18

    GCC will never remove text that's part of the asm template; it doesn't even parse it other than substituting in for %operand. It's literally just a text substitution before the asm is sent to the assembler.

    You have to leave out the mr from your inline asm template, and tell gcc that your output is in r3 (or use a memory-destination output operand, but don't do that). If your inline-asm template ever starts or ends with mov instructions, you're usually doing it wrong.

    Use register uint64_t foo asm("r3"); to force "=r"(foo) to pick r3 on platforms that don't have specific-register constraints.

    (Despite ISO C++17 removing the register keyword, this GNU extension still works with -std=c++17. You can also use register uint64_t foo __asm__("r3"); if you want to avoid the asm keyword. You probably still need to treat register as a reserved word in source that uses this extension; that's fine. ISO C++ removing it from the base language doesn't force implementations to not use it as part of an extension.)


    Or better, don't hard-code a register number. Use an assembler that supports the DARN instruction. (But apparently it's so new that even up-to-date clang lacks it, and you'd only want this inline asm as a fallback for gcc too old to support the __builtin_darn() intrinsic)


    Using these constraints will let you remove the register setup, too, and use foo=0 / bar=-1 before the inline asm statement, and use "+r"(foo).

    But note that darn's output register is write-only. There's no need to zero r3 first. I found a copy of IBM's POWER ISA instruction set manual that is new enough to include darn here: https://wiki.raptorcs.com/w/images/c/cb/PowerISA_public.v3.0B.pdf#page=96

    In fact, you don't need to loop inside the asm at all, you can leave that to the C and only wrap the one asm instruction, like inline-asm is designed for.

    uint64_t random_asm() {
      register uint64_t val asm("r3");
      do {
        //__asm__ __volatile__ ("darn 3, 1");
          __asm__ __volatile__ (".byte 0x7c, 0x61, 0x05, 0xe6  # gcc asm operand = %0\n" : "=r" (val));
      } while(val == -1ULL);
      return val;
    }
    

    compiles cleanly (on the Godbolt compiler explorer) to

    random_asm():
    .L6:                 # compiler-generated label, no risk of name clashes
        .byte 0x7c, 0x61, 0x05, 0xe6  # gcc asm operand = 3
    
        cmpdi 7,3,-1     # compare-immediate
        beq 7,.L6
        blr
    

    Just as tight as your loop, with less setup. (Are you sure you even need to zero r3 before the asm instruction?)

    This function can inline anywhere you want it to, allowing gcc to emit a store instruction that reads r3 directly.


    In practice, you'll want to use a retry counter, as advised in the manual: if the hardware RNG is broken, it might give you failure forever so you should have a fallback to a PRNG. (Same for x86's rdrand)

    Deliver A Random Number (darn) - Programming Note

    When the error value is obtained, software is expected to repeat the operation. If a non-error value has not been obtained after several attempts, a software random number generation method should be used. The recommended number of attempts may be implementation specific. In the absence of other guidance, ten attempts should be adequate.


    xor-zeroing is not efficient on most fixed-instruction-width ISAs, because a mov-immediate is just as short so there's no need to detect and special-case an xor. (And thus CPU designs don't spend transistors on it). Moreover, dependency rules for the PPC asm equivalent of C++11 std::memory_order_consume require it to carry a dependency on the input register, so it couldn't be dependency-breaking even if the designers wanted it to. xor-zeroing is only a thing on x86 and maybe a few other variable-width ISAs.

    Use li r3, 0 like gcc does for int foo(){return 0;} https://godbolt.org/z/-gHI4C.

    0 讨论(0)
提交回复
热议问题