Fixed LTO issue on no discard variable (#1761)

Improve `UseCharPointer()` (thus, `DoNotOptimize()`) under MSVC LTO,
make it actually escape the pointer and prevent it from being optimized away.
This commit is contained in:
Tiago Freire 2024-03-06 13:50:45 +01:00 committed by GitHub
parent ef88520d6f
commit 654d8d6cf3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 2 deletions

View File

@ -152,8 +152,16 @@ BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext() {
return global_context;
}
// FIXME: wouldn't LTO mess this up?
void UseCharPointer(char const volatile*) {}
static void const volatile* volatile global_force_escape_pointer;
// FIXME: Verify if LTO still messes this up?
void UseCharPointer(char const volatile* const v) {
// We want to escape the pointer `v` so that the compiler can not eliminate
// computations that produced it. To do that, we escape the pointer by storing
// it into a volatile variable, since generally, volatile store, is not
// something the compiler is allowed to elide.
global_force_escape_pointer = reinterpret_cast<void const volatile*>(v);
}
} // namespace internal