GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
builtins

Fix fallback for atomic builtins when operands are memory

Ensures atomic operations correctly handle memory operands by forcing them into registers, preventing data races.

This commit fixes a bug where atomic builtins could incorrectly expand operands into memory locations, leading to potential data races. The expand_builtin_atomic_fetch_op and expand_ifn_atomic_bit_test_and functions now ensure that memory operands are first forced into a pseudo-register before the atomic operation is performed. This improves the reliability of atomic operations when operands might otherwise be subject to modification during expansion.

In Details

The expand_builtin_atomic_fetch_op and related functions in builtins.cc need to handle cases where the value operand val might be expanded into a memory location by the Tree Evaluation and Rewriting (TER) pass. If val is a MEM, subsequent atomic operations on it could be corrupted if the memory is updated. This patch forces val into a pseudo-register if it's a MEM to ensure atomicity and reuses the pseudo for the atomic call itself, addressing PR127492.

For Context
atomic builtins
Compiler intrinsic functions that provide atomic memory operations (e.g., __atomic_load_n, __atomic_store_n). These are crucial for lock-free programming and multi-threaded environments to ensure operations complete indivisibly.
fallback
When a compiler encounters an operation it cannot directly translate into machine code, it may fall back to a more general or slower implementation. In this case, it refers to how atomic operations are implemented when specific hardware instructions are not available or applicable.
TER
Tree Evaluation and Rewriting. A GCC optimization pass that analyzes and transforms the compiler's intermediate representation (tree) to simplify expressions and optimize code.
pseudo-register
A temporary, virtual register used by the compiler during optimization and code generation. Pseudo-registers exist in the compiler's internal representation and are later mapped to actual machine registers.
MEM
GCC's internal representation for a memory access, typically involving an address and possibly an offset and access mode.
Filed Under: builtinsatomicsbugfixthreading