[Bug inline-asm/93648] Inline assembly and C++ references cause uninitialized read

rzhikharevich@yandex-team.ru gcc-bugzilla@gcc.gnu.org
Mon Feb 10 09:33:00 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93648

--- Comment #7 from Roman Zhikharevich <rzhikharevich@yandex-team.ru> ---
I am writing benchmarking code for my project and trying to make the compiler
ignore its knowledge of certain values during optimization. I settled for

  template <typename T>
  void black_box(T* value) {
    asm volatile ("" :: "g"(value) : "memory");
  }

but I am trying to figure out the reasoning behind Google Benchmark's
implementation of similar functionality:
https://github.com/google/benchmark/blob/7d97a057e16597e8020d0aca110480fe82c9ca67/include/benchmark/benchmark.h#L318

They use

  template <class Tp>
  inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) {
  #if defined(__clang__)
    asm volatile("" : "+r,m"(value) : : "memory");
  #else
    asm volatile("" : "+m,r"(value) : : "memory");
  #endif
  }


More information about the Gcc-bugs mailing list