[BUG] DoNotOptimize build break with GCC

オープン
#1,675 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
cpp

調査の方向性

sdk/test/trace/sampler_benchmark.cc、特に49行目と58行目のコンストラクターのベンチマークから始め、GCC 7.4.1、7.5.0、または12.3.0と、benchmark 1.8.2または1.8.3を使ってビルドを再現してください。benchmark.hの540行目付近にあるGCC固有のDoNotOptimize実装を調べ、その動作をClangと比較してください。両方のコンパイラーでベンチマークのビルドが正常に成功すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

See downstream bug:

When building the following code:

void BM_ParentBasedSamplerConstruction(benchmark::State &state)
{
  while (state.KeepRunning())
  {
    benchmark::DoNotOptimize(ParentBasedSampler(std::make_shared<AlwaysOnSampler>())); // 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:

The build failure occurs in:

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) {
  asm volatile("" : "+m"(value) : : "memory"); // line 540
}

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

主要言語
C++
スター
10.5k
フォーク
1.8k
平均マージ
2日 2時間
マージ済み PR(30日)
30

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

google/benchmark のほかの issue

google/benchmark の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。