Skip to content

Replace offsetof cache-line check with a well-defined runtime check (#1877) - #2264

Open
devtejasx wants to merge 1 commit into
google:mainfrom
devtejasx:fix-invalid-offsetof-state
Open

Replace offsetof cache-line check with a well-defined runtime check (#1877)#2264
devtejasx wants to merge 1 commit into
google:mainfrom
devtejasx:fix-invalid-offsetof-state

Conversation

@devtejasx

Copy link
Copy Markdown
Contributor

Fixes #1877.

The cache-line static_assert used offsetof(State, skipped_). State is not
a standard-layout type (it has non-static data members with differing access
control), so offsetof() on it is undefined behavior and is diagnosed by
-Winvalid-offsetof on clang (and equivalents on ICC/NVCC/NVHPC). Today this is
only kept quiet by a large stack of compiler-specific #pragmas, and it still
breaks -Werror builds on newer clang (clang-18/19 in the report; also seen
with Apple clang 21).

This replaces the offsetof compile-time check with a well-defined runtime
check that computes the offset from the live object (&skipped_ - this), guarded
by BM_CHECK. The invariant is therefore still enforced in debug/CI builds and
compiles to nothing under NDEBUG, and all of the offsetof-suppression pragmas
are removed.

This follows up on @dmah42's request in the issue for a way to "capture the
spirit of the check in another way".

Verification

  • Confirmed clang -Winvalid-offsetof -Werror flags the original pattern and
    compiles cleanly after the change (no pragmas needed).
  • Full release and debug builds pass (GCC and clang).

Per AGENTS.md: AI-assisted — the patch was drafted with AI assistance
(Claude) and then reviewed, tested, and understood by me. I take full
responsibility for it.

…oogle#1877)

The static_assert used offsetof(State, skipped_) to verify that commonly
accessed data stays on the first cache line. State is not a standard-layout
type (it has non-static data members with differing access control), so
offsetof() on it is undefined behavior and is diagnosed by -Winvalid-offsetof
on clang (and equivalents elsewhere). This forced a stack of compiler-specific
pragmas (GCC/Clang, ICC, NVCC, NVHPC) just to silence the warning, and it still
broke -Werror builds on newer clang.

Compute the offset from the live object instead (address of the member minus
address of the object), which is well defined. The check now runs at
construction time via BM_CHECK, so it is active in debug/CI builds and is a
no-op under NDEBUG. This removes all the offsetof-suppression pragmas.

AI-assisted: patch drafted with AI assistance (Claude); reviewed, tested, and
understood by me. I take full responsibility for it.
@LebedevRI

Copy link
Copy Markdown
Collaborator

I just checked, and i don't believe that diagnostic fires any more on clang-22 on linux.
Can you show a screenshot of the terminal with the compilation failure?

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] fails to compile on windows with clang-18 / clang-19

2 participants