“Escape” and “Clobber” equivalent in MSVC

前端 未结 2 1894
悲&欢浪女
悲&欢浪女 2020-12-29 06:24

In Chandler Carruth\'s CppCon 2015 talk he introduces two magical functions for defeating the optimizer without any extra performance penalties.

For reference, here

相关标签:
2条回答
  • 2020-12-29 06:51

    I was looking for a way to achieve the exact same thing in my own little benchmark lib. The frustrating thing about MSVC is that targeting x64 disallows the __asm trick while x86 allows it!

    After some tries I reused google's solution without incurring additional call! The nice thing is that the solution works with both MSVC(/Ox) and GCC(-O3).

    template <class T>
    inline auto doNotOptimizeAway(T const& datum) {
        return reinterpret_cast<char const volatile&>(datum);
    }
    

    At the call site I simply do no use the volatile returned!

    int main()
    {
        int a{10};
        doNotOptimizeAway(a);
        return 0;
    }
    

    Generated ASM (Compiler Explorer)

    a$ = 8
    main    PROC
            mov     DWORD PTR a$[rsp], 10
            movzx   eax, BYTE PTR a$[rsp]
            xor     eax, eax
            ret     0
    main    ENDP
    
    0 讨论(0)
  • 2020-12-29 07:01

    While I don't know of an equivalent assembly trick for MSVC, Facebook uses the following in their Folly benchmark library:

    /**
     * Call doNotOptimizeAway(var) against variables that you use for
     * benchmarking but otherwise are useless. The compiler tends to do a
     * good job at eliminating unused variables, and this function fools
     * it into thinking var is in fact needed.
     */
    #ifdef _MSC_VER
    
    #pragma optimize("", off)
    
    template <class T>
    void doNotOptimizeAway(T&& datum) {
      datum = datum;
    }
    
    #pragma optimize("", on)
    
    #elif defined(__clang__)
    
    template <class T>
    __attribute__((__optnone__)) void doNotOptimizeAway(T&& /* datum */) {}
    
    #else
    
    template <class T>
    void doNotOptimizeAway(T&& datum) {
      asm volatile("" : "+r" (datum));
    }
    
    #endif
    

    Here is a link to code on GitHub.

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