Skip to content

with migrate translates the GCC atomic builtins to std.sync's Atomic operations (#955) - #3567

Merged
ehartford merged 1 commit into
mainfrom
migrate-atomics
Nov 23, 2026
Merged

ehartford merged 1 commit into
mainfrom
migrate-atomics

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Part of #955 (the emit-C lanes). Does not close it.

with migrate refused every GCC atomic builtin ("has no translation"). The C backend emits them for Atomic[T], so the roundtrip lane (the compiler emitted as C and migrated back) stopped on 14 functions.

What is translated. __atomic_load_n, store_n, exchange_n, fetch_add/sub/and/or/xor, add_fetch, sub_fetch, compare_exchange_n and __atomic_thread_fence become the operation of that name on std.sync.Atomic[T] over the same memory, the way std's own locks spell it:

(*((&raw mut g as *mut i32) as *mut Atomic[i32])).swap(v, .Acquire)
  • The ordering must be a constant clang can evaluate (the C backend writes a constant conditional); a run-time ordering is refused by name.
  • X_fetch returns the new value, so its operand is evaluated twice and must be a constant or a name; otherwise refused.
  • A compare-exchange goes through one generated helper, since C's form returns whether it swapped and writes the value found through expected. The weak flag is ignored (a strong exchange is a valid weak one).
  • The module gains use std.sync and the helper from flags set where the translation happens, not from the output's text.

What is not. Only the __atomic_* spellings. <stdatomic.h> (atomic_load(&g)) is still refused, loudly. And one older defect is untouched and separate: _Atomic on a type is dropped silently (_Atomic int g; g++ migrates to a plain add). Both are recorded on #955.

Tests. bs_check_migrate_atomic_builtins (basic migrator lane): a C program whose main returns the number of the first operation whose result or effect is wrong, over int, long and pointer atomics; the migrated program returns 0 as C does. behav_migrate_unexposed_operators used an atomic load as its example of a refused operator; it now uses one with a run-time ordering, which is still refused by name.

Roundtrip after this: the 14 stops are gone; one function remains (a closure the C backend emits as a statement expression used as a value).

:c-migrator-tests green. Battery green; self-audit and :release-checks green (587 s). A first battery was red on the test above before its expectation was updated.

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.

1 participant