google / google/benchmark

[BUG] DoNotOptimize build break with GCC

Open
#1,675 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
10.4k
Forks
1.8k
Avg merge
2d 4h
Merged PRs (30d)
8

Description

See downstream bug:

* https://github.com/open-telemetry/opentelemetry-cpp/issues/2343

When building the following code:

```
void BM_ParentBasedSamplerConstruction(benchmark::State &state)
{
while (state.KeepRunning())
{
benchmark::DoNotOptimize(ParentBasedSampler(std::make_shared())); // line 49
}
}
```

and

```
void BM_TraceIdRatioBasedSamplerConstruction(benchmark::State &state)
{
while (state.KeepRunning())
{
benchmark::DoNotOptimize(TraceIdRatioBasedSampler(0.01)); // line 58
}
}
```

the build fails with GCC 7.4.1, GCC 7.5.0, GCC 12.3.0.

The error reported is:

```
[ 56%] Building CXX object sdk/test/trace/CMakeFiles/sampler_benchmark.dir/sampler_benchmark.cc.o
In file included from /home/malff/CODE/MARC_GITHUB/opentelemetry-cpp/sdk/test/trace/sampler_benchmark.cc:17:
/opt/benchmark-1.8.2/include/benchmark/benchmark.h: In instantiation of ‘typename std::enable_if<((! std::is_trivially_copyable<_Tp>::value) || (sizeof (Tp) > sizeof (Tp*)))>::type benchmark::DoNotOptimize(Tp&&) [with Tp = opentelemetry::v1::sdk::trace::ParentBasedSampler; typename std::enable_if<((! std::is_trivially_copyable<_Tp>::value) || (sizeof (Tp) > sizeof (Tp*)))>::type = void]’:
/home/malff/CODE/MARC_GITHUB/opentelemetry-cpp/sdk/test/trace/sampler_benchmark.cc:49:29: required from here
/opt/benchmark-1.8.2/include/benchmark/benchmark.h:540:3: error: read-only reference ‘value’ used as ‘asm’ output
540 | asm volatile("" : "+m"(value) : : "memory");
| ^~~
/opt/benchmark-1.8.2/include/benchmark/benchmark.h: In instantiation of ‘typename std::enable_if<((! std::is_trivially_copyable<_Tp>::value) || (sizeof (Tp) > sizeof (Tp*)))>::type benchmark::DoNotOptimize(Tp&&) [with Tp = opentelemetry::v1::sdk::trace::TraceIdRatioBasedSampler; typename std::enable_if<((! std::is_trivially_copyable<_Tp>::value) || (sizeof (Tp) > sizeof (Tp*)))>::type = void]’:
/home/malff/CODE/MARC_GITHUB/opentelemetry-cpp/sdk/test/trace/sampler_benchmark.cc:58:29: required from here
/opt/benchmark-1.8.2/include/benchmark/benchmark.h:540:3: error: read-only reference ‘value’ used as ‘asm’ output
make[2]: *** [sdk/test/trace/CMakeFiles/sampler_benchmark.dir/build.make:76: sdk/test/trace/CMakeFiles/sampler_benchmark.dir/sampler_benchmark.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:4217: sdk/test/trace/CMakeFiles/sampler_benchmark.dir/all] Error 2
make: *** [Makefile:146: all] Error 2
```

Verified with benchmark 1.8.2 and 1.8.3.

Note that the same code builds fine with CLang.

If this matters, `ParentBasedSampler` and `TraceIdRatioBasedSampler` are classes: what is benchmarked here is a call to the constructor.

Note how the benchmark header file is different for GCC, with:

* https://github.com/google/benchmark/blob/main/include/benchmark/benchmark.h#L486-L488

The build failure occurs in:

```
template
inline BENCHMARK_ALWAYS_INLINE
typename std::enable_if::value ||
(sizeof(Tp) > sizeof(Tp*))>::type
DoNotOptimize(Tp&& value) {
asm volatile("" : "+m"(value) : : "memory"); // line 540
}
```

Expected result is a successful build with both GCC and Clang.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.