google / google/benchmark

[BUG] GCC optimizes away the write with `benchmark::DoNotOptimize`

Open
#2,282 3 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

GCC16.1 optimizes away the write when we use `benchmark::DoNotOptimize`.

## Minimal reproducible example
```cpp
void f() {
int *a = new int[1 << 16];
for (int i = 0; i < 1 << 16; i++) {
benchmark::DoNotOptimize(a[i] = 10); // this is optimized away by the compiler
}
delete[] a;
}
```

Compiler explorer(CE) link: https://godbolt.org/z/j538536ss. The CE link contains a possible fix for the issue.

Contributor guide

Open the contributing guide

Research direction

Start with the minimal reproducer in the issue and inspect the implementation of benchmark::DoNotOptimize, using the linked Compiler Explorer example to compare GCC 16.1 output. Done means the write in the loop is no longer optimized away while preserving the intended DoNotOptimize behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.