Skip to content

Fix DoNotOptimize compilation error with partially const structs (Fixes #1997)#2246

Open
MaciekChoszczyk wants to merge 1 commit into
google:mainfrom
MaciekChoszczyk:fix-issue-1997
Open

Fix DoNotOptimize compilation error with partially const structs (Fixes #1997)#2246
MaciekChoszczyk wants to merge 1 commit into
google:mainfrom
MaciekChoszczyk:fix-issue-1997

Conversation

@MaciekChoszczyk

Copy link
Copy Markdown

Fixes #1997.

What's wrong?

Passing structs with const members to DoNotOptimize causes compilation errors because the asm constraints (+m, +r) request write access to memory that the compiler considers const.

Fix

Cast the input value to a raw byte array (char(&)[sizeof(Tp)]) to strip const qualifiers for the optimizer. This safely allows write access in the assembly block.

Notes

  • const_cast is not removing 'const' from internal struct members.
  • std::bit_cast was considered but avoided to maintain compatibility with older C++ standards.
  • This approach aligns with the existing workaround used for MSVC, which was missing from the GCC/Clang implementations.

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.

[BUG] DoNotOptimize on values with const members again

1 participant