This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Allow atomic_test to pass on x86_64
AbandonedPublic

Authored by arichardson on Dec 8 2020, 4:18 AM.

Details

Summary

c11_atomic_load is implemented using cmpxcghg16b on x86_64 (assuming
compiler-rt is compiled with a new-enough -march= flag) and this
instruction requires a writable memory mapping.
To allow for this case, use a writable mapping and make the
atomic_load
function signature match the one atomic.c (non-const src pointer).

I do not know if __atomic_load needs to support read-only mappings, but if
it does we need to disable the lock-free path for 16-byte atomic load.

I believe this is currently only an issue on macOS systems since the clang
driver defaults to enabling cmpxchg16b support, whereas other targets need an
explicit -march= flag.
This change allows atomic_test.c to pass on my macOS system, before it would
fail with a EXC_BAD_ACCESS error.

Diff Detail

Event Timeline

arichardson created this revision.Dec 8 2020, 4:18 AM
arichardson requested review of this revision.Dec 8 2020, 4:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2020, 4:18 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
jrtc27 added a comment.Dec 8 2020, 4:24 AM

Note that this shows there is currently another bug in x86_64 clang (ignoring OS, since with manual -march=athlon64 on macOS you can also get the non-macOS behaviour). Whether or not libatomic is used is part of the ABI, as libatomic may (and likely will for 16B atomics on your standard Linux distro) use hash tables of locks to emulate atomic operations, which will not correctly synchronise with the inlined hardware atomics. See https://godbolt.org/z/fqfvev for evidence of the ABI breakage.

It appears that GCC changed its behaviour from cmpxchg to calling the helper function for GCC 7: https://godbolt.org/z/s9fxfY

See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70490 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80878

It seems that everything is pointing towards keeping it non-writeable?

  • DR 459 was resolved by adding const.
  • GCC seems to have moved away from cmpxchg.
  • Jessica's point about clang's behavior leading to ABI incompatibilities.

Even if we accept that's the case, should this test still have conditional workarounds for problematic implementations?

ldionne resigned from this revision.Sep 7 2023, 8:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2023, 8:41 AM
Herald added a subscriber: Enna1. · View Herald Transcript
arichardson abandoned this revision.Oct 4 2023, 11:40 AM

Not planning to continue work on this.