Fix pass rvalue to DoNotOptimize (#1608)

* Fix pass rvalue to DoNotOptimize #1584

* Add test
This commit is contained in:
Bulat Gaifullin 2023-06-19 10:35:52 +03:00 committed by GitHub
parent 604f6fd3f4
commit df9a99d998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -465,7 +465,13 @@ inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
}
template <class Tp>
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 <class Tp>
inline BENCHMARK_ALWAYS_INLINE
typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
(sizeof(Tp) <= sizeof(Tp*))>::type
DoNotOptimize(Tp& value) {
DoNotOptimize(Tp&& value) {
asm volatile("" : "+m,r"(value) : : "memory");
}
@ -509,7 +515,7 @@ template <class Tp>
inline BENCHMARK_ALWAYS_INLINE
typename std::enable_if<!std::is_trivially_copyable<Tp>::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 <class Tp>
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

View File

@ -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
}