Skip to content

Commit 654d8d6

Browse files
authored
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.
1 parent ef88520 commit 654d8d6

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/benchmark.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,16 @@ BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext() {
152152
return global_context;
153153
}
154154

155-
// FIXME: wouldn't LTO mess this up?
156-
void UseCharPointer(char const volatile*) {}
155+
static void const volatile* volatile global_force_escape_pointer;
156+
157+
// FIXME: Verify if LTO still messes this up?
158+
void UseCharPointer(char const volatile* const v) {
159+
// We want to escape the pointer `v` so that the compiler can not eliminate
160+
// computations that produced it. To do that, we escape the pointer by storing
161+
// it into a volatile variable, since generally, volatile store, is not
162+
// something the compiler is allowed to elide.
163+
global_force_escape_pointer = reinterpret_cast<void const volatile*>(v);
164+
}
157165

158166
} // namespace internal
159167

0 commit comments

Comments
 (0)