Skip to content

Conversation

@andreabedini
Copy link
Member

Replace legacy _sync_fetch_and* builtins with their modern _atomic_fetch* equivalents. This simplifies the code significantly, particularly for the nand operation which previously required extensive workarounds for compiler compatibility issues.

Changes:

  • Replace _sync_fetch_and{add,sub,and,or,xor} with _atomic_fetch*

  • Replace __sync_fetch_and_nand with __atomic_fetch_nand

  • Remove CAS-based fallback for nand operations

  • Remove compiler-specific warning suppressions for -Wsync-nand

  • Remove volatile qualifiers (not needed with __atomic builtins)

  • Update comments to reflect modern atomics usage

All operations maintain __ATOMIC_SEQ_CST memory ordering for sequential consistency, matching the original behavior.

Replace legacy __sync_fetch_and_* builtins with their modern __atomic_fetch_* equivalents. This simplifies the code significantly, particularly for the nand operation which previously required extensive workarounds for compiler compatibility issues.

Changes:

- Replace __sync_fetch_and_{add,sub,and,or,xor} with __atomic_fetch_*

- Replace __sync_fetch_and_nand with __atomic_fetch_nand

- Remove CAS-based fallback for nand operations

- Remove compiler-specific warning suppressions for -Wsync-nand

- Remove volatile qualifiers (not needed with __atomic builtins)

- Update comments to reflect modern atomics usage

All operations maintain __ATOMIC_SEQ_CST memory ordering for sequential consistency, matching the original behavior.
Copy link

@hsyl20 hsyl20 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. We should upstream this if it works on all supported platforms.

I was put off by the Note [__sync_fetch_and_nand usage] but I think the note is wrong (see inline comment).

#pragma GCC diagnostic ignored "-Wsync-nand"
#endif
// The modern __atomic_fetch_nand has well-defined semantics:
// it atomically replaces *ptr with ~(*ptr & val) and returns the old value.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the original note bogus? I think it has the semantics change reversed, no?

@hasufell
Copy link
Member

It was moved to rts/prim/atomic.c recently: https://gitlab.haskell.org/ghc/ghc/-/blob/master/rts/prim/atomic.c

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.

4 participants