Optimization barrier for microbenchmarks in MSVC: tell the optimizer you clobber memory?

后端 未结 2 1122
旧时难觅i
旧时难觅i 2021-01-03 21:10

Chandler Carruth introduced two functions in his CppCon2015 talk that can be used to do some fine-grained inhibition of the optimizer. They are useful to write micro-benchma

2条回答
  •  忘掉有多难
    2021-01-03 21:45

    I have used the following in place of escape.

    #ifdef _MSC_VER
    #pragma optimize("", off)
    template 
    inline void escape(T* p) {
        *reinterpret_cast(p) =
            *reinterpret_cast(p); // thanks, @milleniumbug
    }
    #pragma optimize("", on)
    #endif
    

    It's not perfect but it's close enough, I think.

    Sadly, I don't have a way to emulate clobber.

提交回复
热议问题