This is an archive of the discontinued LLVM Phabricator instance.

[clang][codegen] Don't emit atomic loads for threadsafe init if they aren't inline
ClosedPublic

Authored by efriedma on Oct 10 2022, 3:49 PM.

Details

Summary

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

Diff Detail

Event Timeline

efriedma created this revision.Oct 10 2022, 3:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 10 2022, 3:49 PM
efriedma requested review of this revision.Oct 10 2022, 3:49 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 10 2022, 3:49 PM
efriedma edited the summary of this revision. (Show Details)Oct 10 2022, 3:50 PM
efriedma updated this revision to Diff 466657.Oct 10 2022, 3:52 PM

Upload correct version of patch

rjmccall accepted this revision.Oct 10 2022, 6:03 PM

Makes sense. Nice code-size optimization at any rate.

Hmm, actually, along those lines, should we do the same thing at -Oz?

This revision is now accepted and ready to land.Oct 10 2022, 6:03 PM
This revision was landed with ongoing or failed builds.Oct 11 2022, 2:01 PM
This revision was automatically updated to reflect the committed changes.