llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AArch64] atomicrmw on Armv8.1-a memory ordering can be changed

Open cddouma-arm opened this issue 9 years ago • 4 comments

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[A|L|AL] instructions in Armv8.1-a have different memory orderings if the target register is the zero register (WZR or XZR). In particular they are no longer loads if the target register is the zero register[1]. This means that a load barrier ("fence acquire" in LLVM terms) will no longer order these before the barrier.

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 instructions, and LD instructions where the destination register is WZR or XZR, are not regarded as doing a read for the purpose of a DMB LD barrier."

cddouma-arm avatar Oct 26 '17 10:10 cddouma-arm

Commit as r356360. Review happened under https://reviews.llvm.org/D58348

cddouma-arm avatar Mar 25 '19 12:03 cddouma-arm

Seems that the bit that is missing, is that LD should still not use the zero register because a 'thread fence' could force ordering on that load which would no longer happen if it is turned into ST.

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. "

cddouma-arm avatar Feb 15 '19 16:02 cddouma-arm

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.

llvmbot avatar Oct 31 '18 22:10 llvmbot

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.

ostannard avatar Mar 08 '18 15:03 ostannard