[AArch64] atomicrmw on Armv8.1-a memory ordering can be changed
| Bugzilla Link | 35094 |
| Resolution | FIXED |
| Resolved on | Mar 25, 2019 05:05 |
| Version | trunk |
| OS | All |
| Attachments | example |
| CC | @kbeyls,@ostannard |
Extended Description
The LD
These instructions are used for atomicrmw LLVM operations, and LLVM's "AArch64 Dead register definitions" pass tries to use the zero register. If it does, it changes the semantics (memory order) of the program.
I don't have a real-life example. But I hope the attached example makes the story a bit clearer.
[1] Arm ARM for Armv8-A [ARM DDI 0487B.b] section C3.2.13 http://infocenter.arm.com/help/topic/com.arm.doc.ddi0487b.b/DDI0487B_b_armv8_arm.pdf
"The ST
Commit as r356360. Review happened under https://reviews.llvm.org/D58348
Seems that the bit that is missing, is that LD
The example mentioned from the thread linked by James Greenhalgh (quoting Will Deacon): " P0 (atomic_int* y,atomic_int* x) { atomic_store_explicit(x,1,memory_order_relaxed); atomic_thread_fence(memory_order_release); atomic_store_explicit(y,1,memory_order_relaxed); }
P1 (atomic_int* y,atomic_int* x) { atomic_fetch_add_explicit(y,1,memory_order_relaxed); // STADD atomic_thread_fence(memory_order_acquire); int r0 = atomic_load_explicit(x,memory_order_relaxed); }
P2 (atomic_int* y) { int r1 = atomic_load_explicit(y,memory_order_relaxed); }
My understanding is that it is forbidden for r0 == 0 and r1 == 2 after this test has executed. However, if the relaxed add in P1 compiles to STADD and the subsequent acquire fence is compiled as DMB LD, then we don't have any ordering guarantees in P1 and the forbidden result could be observed. "
Reopening this; the reasoning behind why this is a bug has been spelled out recently by Will Deacon on gcc-patches recently:
https://gcc.gnu.org/ml/gcc-patches/2018-10/msg02042.html https://gcc.gnu.org/ml/gcc-patches/2018-10/msg02072.html
Clang should not weaken the atomic operations beyond what C11 permits.
I've discussed this with Christof, and we don't think this is a bug. It only matters that a load is ordered before a "fence acquire" if the load has some observable effect, which can't happen if the destination register is ZR. The architecture does still specify that there is an ordering edge between the load and the store done by the LD