From 9192ee00325230acc313268edf0836d9ee7b2c7b Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Wed, 28 Oct 2026 20:51:45 +0000 Subject: [PATCH] Make `TokenInfo` use a single 32-bit store for initialization. (#8054) We need to zero-initialize the `token_payload_` bits in order for the normal creation to just be zeroing the whole structure. We do this only when MSan isn't enabled so we don't lose bit-accurate bug detection. Assisted-by: Antigravity with Gemini --- toolchain/lex/token_info.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/toolchain/lex/token_info.h b/toolchain/lex/token_info.h index 363013c82094..8453a7d8f872 100644 --- a/toolchain/lex/token_info.h +++ b/toolchain/lex/token_info.h @@ -162,14 +162,19 @@ class TokenInfo { // // Only used by the lexer which enforces only the correct kinds are used. // - // When the payload is not being set, we leave it uninitialized. At least in - // some cases, this will allow MSan to correctly detect erroneous attempts - // to access the payload, as it works to track uninitialized memory - // bit-for-bit specifically to handle complex cases like bitfields. + // Under MSan, leave `token_payload_` uninitialized so bit-level tracking can + // detect erroneous attempts to access a missing payload. In non-MSan builds, + // zero-initialize it so constructing a payloadless `TokenInfo` compiles to a + // plain 32-bit store rather than a read-modify-write preserving uninitialized + // upper bits (which SROA otherwise promotes to loop-carried registers). TokenInfo(TokenKind kind, bool has_leading_space, int32_t byte_offset) : kind_(kind), has_leading_space_(has_leading_space), - byte_offset_(byte_offset) {} +#if !__has_feature(memory_sanitizer) + token_payload_(0), +#endif + byte_offset_(byte_offset) { + } // Constructor for a TokenKind that carries a payload. //