From df9a99d998d9f038a53a970a77ce31c8c2a882ce Mon Sep 17 00:00:00 2001 From: Bulat Gaifullin Date: Mon, 19 Jun 2023 10:35:52 +0300 Subject: [PATCH] Fix pass rvalue to DoNotOptimize (#1608) * Fix pass rvalue to DoNotOptimize #1584 * Add test --- include/benchmark/benchmark.h | 20 ++++++++++++++++---- test/donotoptimize_test.cc | 5 +++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index e44d5341..558aca8d 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -465,7 +465,13 @@ inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) { } template -inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) { +inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize( +#ifdef BENCHMARK_HAS_CXX11 + Tp&& value +#else + Tp& value +#endif +) { #if defined(__clang__) asm volatile("" : "+r,m"(value) : : "memory"); #else @@ -501,7 +507,7 @@ template inline BENCHMARK_ALWAYS_INLINE typename std::enable_if::value && (sizeof(Tp) <= sizeof(Tp*))>::type - DoNotOptimize(Tp& value) { + DoNotOptimize(Tp&& value) { asm volatile("" : "+m,r"(value) : : "memory"); } @@ -509,7 +515,7 @@ template inline BENCHMARK_ALWAYS_INLINE typename std::enable_if::value || (sizeof(Tp) > sizeof(Tp*))>::type - DoNotOptimize(Tp& value) { + DoNotOptimize(Tp&& value) { asm volatile("" : "+m"(value) : : "memory"); } @@ -526,7 +532,13 @@ inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) { } template -inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp& value) { +inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize( +#ifdef BENCHMARK_HAS_CXX11 + Tp&& value +#else + Tp& value +#endif +) { asm volatile("" : "+m"(value) : : "memory"); } #endif diff --git a/test/donotoptimize_test.cc b/test/donotoptimize_test.cc index 90d5af35..04ec9386 100644 --- a/test/donotoptimize_test.cc +++ b/test/donotoptimize_test.cc @@ -61,4 +61,9 @@ int main(int, char*[]) { // These tests are to e BitRef lval = BitRef::Make(); benchmark::DoNotOptimize(lval); + +#ifdef BENCHMARK_HAS_CXX11 + // Check that accept rvalue. + benchmark::DoNotOptimize(BitRef::Make()); +#endif }