Specify the ABI of C/++11 atomics

43 views
Skip to first unread message

connor horman

unread,
Jan 26, 2024, 1:17:58 PMJan 26
to X86-64 System V Application Binary Interface
Atomics in C/++11 have multiple possible lowerings (particularily SeqCst, but also some effects on Acquire/Release can affect other operations in weird ways). Some of these lowerings are incompatible (for example, SeqCst load/stores can be implemented by load+mfence/store, load/mfence+store, or by clever use of `lock` rmws), such that code compiled by different compilers that make different choices will not comply with C++11/C++20 or C11 specifications.
The Sys-V ABI for x86-64 should clarify which lowering (or lowerings) are correct so that interoperability between compilers is possible as well as with other languages with the same or similar atomic memory models.

Additionally, the Sys-V ABI should specify which atomic types are considered always lock free, as it is also an ABI issue when compilers disagree on when to use lock-free lowerings or not.

James Y Knight

unread,
Jan 27, 2024, 10:35:33 AMJan 27
to connor horman, X86-64 System V Application Binary Interface
The ABI should also specify the size/alignment of C11 atomic types. E.g.:
  typedef _Atomic struct {char x[32];} Atomic32char;
  _Static_assert(sizeof(Atomic32char) == 32);
  _Static_assert(_Alignof(Atomic32char) == 1);

  typedef _Atomic struct {char x[4];} Atomic3char;
  _Static_assert(sizeof(Atomic4char) == 4);
  _Static_assert(_Alignof(Atomic4char) == 4);

  typedef _Atomic struct {char x[3];} Atomic3char;

  _Static_assert(sizeof(Atomic3char) == ???); // 3 on GCC, 4 on Clang
  _Static_assert(_Alignof(Atomic3char) == ???); // 1 on GCC, 4 on Clang.


Currently, Clang's "_Atomic" type modifier will add padding to a non-power-of-2 object, up to 16-bytes, in order to allow it to become lock-free. GCC never adds padding to _Atomic. Both compilers will increase the alignment of a power-of-2-sized type up to 16 bytes.


--
You received this message because you are subscribed to the Google Groups "X86-64 System V Application Binary Interface" group.
To unsubscribe from this group and stop receiving emails from it, send an email to x86-64-abi+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/x86-64-abi/CADLV35jFq8%3D4CnVs-%2Bzc3SdoftrLbY1ozCrW1uUALm_WqJqXnQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages