Skip to content

Fixed LTO issue on no discard variable#1761

Merged
LebedevRI merged 5 commits into
google:mainfrom
tmiguelf:feature/LTO_fix
Mar 6, 2024
Merged

Fixed LTO issue on no discard variable#1761
LebedevRI merged 5 commits into
google:mainfrom
tmiguelf:feature/LTO_fix

Conversation

@tmiguelf

@tmiguelf tmiguelf commented Mar 5, 2024

Copy link
Copy Markdown
Contributor

Fixed LTO issue on no discard variable.
Tested on MSVC

Comment thread src/benchmark.cc Outdated
Comment thread src/benchmark.cc Outdated
@tmiguelf tmiguelf requested a review from LebedevRI March 5, 2024 20:30
Comment thread src/benchmark.cc Outdated

// FIXME: Verify if LTO still messes this up?
void UseCharPointer(char const volatile* const v) {
//forces compiler to issue instruction by assigning dummy value to a volatile preventing LTO

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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.

@tmiguelf tmiguelf requested a review from LebedevRI March 5, 2024 22:49

@LebedevRI LebedevRI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works, at least better than the original: https://godbolt.org/z/v1493hd81

@LebedevRI

Copy link
Copy Markdown
Collaborator

I'll merge after #1761 unbreaks the CI...

@tmiguelf

tmiguelf commented Mar 5, 2024

Copy link
Copy Markdown
Contributor Author

Yes, I was having an issue where my code was optimized away and the benchmark::DoNotOptimize failed to prevent optimization. With this it works for my case, now my statistics are more accurate.

@LebedevRI LebedevRI merged commit 654d8d6 into google:main Mar 6, 2024
@LebedevRI

Copy link
Copy Markdown
Collaborator

@tmiguelf thank you!

@tmiguelf tmiguelf deleted the feature/LTO_fix branch March 6, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants