[clang][codegen] Don't emit atomic loads for threadsafe init if they aren't inline
authorEli Friedman <efriedma@quicinc.com>
Tue, 11 Oct 2022 21:00:33 +0000 (14:00 -0700)
committerEli Friedman <efriedma@quicinc.com>
Tue, 11 Oct 2022 21:00:33 +0000 (14:00 -0700)
commit1079662d2fff7ae799503d910b299c5108d105fd
treeb7ab0a158e7e6615117d8f8004cccfcd52239aa2
parent0456acbfb942f127359a8defd1b4f1f44420df3e
[clang][codegen] Don't emit atomic loads for threadsafe init if they aren't inline

Performing a load before calling __cxa_guard_acquire is supposed to be
an optimization, but it isn't much of one if we're just going to emit a
call to __atomic_load_1 instead.  Instead, just skip the load, and
let __cxa_guard_acquire do whatever it wants.

(In practice, on such targets, the C++ library is just built with
threading turned off, so the result isn't actually threadsafe, but
there's not really anything clang can do about that.)

The alternative here is that we try to define some ABI for threadsafe
init that allows the speculative load without full atomics.  Almost any
target without full atomics has a load that's s "atomic enough" for this
purpose. But it's not clear how we emit an "atomic enough" load in LLVM
IR, and there isn't any ABI document we can refer to.

Or I guess we could turn off -fthreadsafe-statics by default on
Cortex-M0, but that seems like it would be surprising.

Fixes https://github.com/llvm/llvm-project/issues/58184

Differential Revision: https://reviews.llvm.org/D135628
clang/lib/CodeGen/ItaniumCXXABI.cpp
clang/test/CodeGenCXX/threadsafe-statics-no-atomic.cpp [new file with mode: 0644]