[PATCH] D45321: [atomics] Fix runtime calls for misaligned atomics

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 16 15:50:21 PDT 2018


efriedma added inline comments.
Herald added a reviewer: jfb.


================
Comment at: compiler-rt/lib/builtins/atomic.c:193
+      type tmp = 0;\
+      *((type*)dest) = __c11_atomic_compare_exchange_weak((_Atomic(type)*)src, &tmp, 0, model, model);\
+    }\
----------------
Isn't the return value of `__c11_atomic_compare_exchange_weak` the success boolean?

Even with that fixed, though, this is UB because you're violating the alignment rules for `_Atomic`.  It might appear to emit the right code for now, but it's a ticking time bomb because the compiler could optimize the "cmpxchg" to a "mov" (since you're not actually modifying the memory on success).

The "right" way to do this is to use `__atomic_load` on a pointer to an unligned type.  Granted, clang currently doesn't lower that to the sequence you want (instead it generates a libcall to `__atomic_load_4`).


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D45321





More information about the llvm-commits mailing list