What does __asm volatile (“pause” ::: “memory”); do?

后端 未结 1 435
灰色年华
灰色年华 2021-02-10 04:07

I am looking at an open source C++ project which has the following code structure:

while(true) {
  // Do something work

  if(some_condition_becomes_true)
     b         


        
1条回答
  •  囚心锁ツ
    2021-02-10 05:02

    It's _mm_pause() and a compile memory barrier wrapped into one GNU C Extended ASM statement. https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html

    asm("" ::: "memory") prevents compile-time reordering of memory operations across it, like C++11 std::atomic_signal_fence(std::memory_order_seq_cst). (not atomic_thread_fence; although on x86 preventing compile-time reordering is sufficient to make it an acquire + release fence because the only run-time reordering that x86 allows is StoreLoad.) See Jeff Preshing's Memory Ordering at Compile Time article.

    Making the asm instruction part non-empty also means those asm instructions will run every time the C logically runs that source line (because it's volatile).

    pause prevents speculative loads from causing memory-ordering mis-speculation pipeling clears (aka machine nukes). It's useful inside spin loops that are waiting to see a value in memory.

    You might find this statement inside a spinloop written without C++11 std::atomic, to tell the compiler it has to re-read the value of a global variable. (Because the "memory" clobber means the compiler has to assume the asm statement might have modified the value of any globally-reachable memory.)

    This looks like the context where you found it: some_condition_becomes_true probably includes reading a non-atomic / non-volatile global.

    The C++11 equivalent of your loop:

    #include 
    #include 
    std::atomic flag;
    
    void wait_for_flag(void) {
        while(flag.load(std::memory_order_seq_cst == 0) {
            _mm_pause();
        }
    }
    

    (Not exactly equivalent, because your version has a full compiler barrier while mine only has a seq-cst load, so it's not a full signal-fence. But probably what wasn't needed, and they just used something stronger than necessary to get the effect of volatile).


    Without the barrier or making flag atomic, the compiler would have optimized it to:

    // Do something work
    
    if(some_condition_becomes_true) {
        // empty
    } else {
    
      while(true) {
         // Do something work
         __asm volatile ("pause" ::: );  // no memory clobber
      }
    }
    

    i.e. it would hoist the check on some_condition_becomes_true out of the loop and not re-read the global every time.

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