Index: llvm/lib/Target/AMDGPU/SIISelLowering.h =================================================================== --- llvm/lib/Target/AMDGPU/SIISelLowering.h +++ llvm/lib/Target/AMDGPU/SIISelLowering.h @@ -497,6 +497,9 @@ shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const override; void emitExpandAtomicRMW(AtomicRMWInst *AI) const override; + LoadInst * + lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const override; + const TargetRegisterClass *getRegClassFor(MVT VT, bool isDivergent) const override; bool requiresUniformRegister(MachineFunction &MF, Index: llvm/lib/Target/AMDGPU/SIISelLowering.cpp =================================================================== --- llvm/lib/Target/AMDGPU/SIISelLowering.cpp +++ llvm/lib/Target/AMDGPU/SIISelLowering.cpp @@ -13432,3 +13432,30 @@ AI->replaceAllUsesWith(Loaded); AI->eraseFromParent(); } + +LoadInst * +SITargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const { + IRBuilder<> Builder(AI); + auto Order = + AtomicCmpXchgInst::getStrongestFailureOrdering(AI->getOrdering()); + auto SSID = AI->getSyncScopeID(); + + // https://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf suggests we need + // a release fence before the load to ensure that the reads become visible + // before an update. Since the update exists only for memory ordering + // purposes, we arrange that the update writes back the just-read value, and + // that the read and write are performed as a single atomic + // "read-modify-write" operation, though there is no actual modification + // involved. + if (isReleaseOrStronger(Order)) + Builder.CreateFence(AtomicOrdering::Release, SSID); + + LoadInst *LI = Builder.CreateAlignedLoad( + AI->getType(), AI->getPointerOperand(), AI->getAlign()); + LI->setAtomic(Order, SSID); + LI->copyMetadata(*AI); + LI->takeName(AI); + AI->replaceAllUsesWith(LI); + AI->eraseFromParent(); + return LI; +} Index: llvm/test/CodeGen/AMDGPU/idemponent-atomics.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/AMDGPU/idemponent-atomics.ll @@ -0,0 +1,129 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx940 -verify-machineinstrs < %s | FileCheck -check-prefixes=GFX940 %s + +define i32 @global_agent_monotonic_idempotent_or(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_monotonic_idempotent_or: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") monotonic, align 4 + ret i32 %val +} + +define i32 @global_agent_acquire_idempotent_or(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_acquire_idempotent_or: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: buffer_inv sc1 +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") acquire, align 4 + ret i32 %val +} + +define i32 @global_agent_release_idempotent_or(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_release_idempotent_or: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") release, align 4 + ret i32 %val +} + +define i32 @global_agent_seq_cst_idempotent_or(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_seq_cst_idempotent_or: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: buffer_wbl2 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: buffer_inv sc1 +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") seq_cst, align 4 + ret i32 %val +} + +define i32 @global_agent_monotonic_idempotent_add(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_monotonic_idempotent_add: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc0 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw add ptr addrspace(1) %in, i32 0 syncscope("workgroup") monotonic, align 4 + ret i32 %val +} + +define i32 @global_agent_monotonic_idempotent_sub(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_monotonic_idempotent_sub: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw sub ptr addrspace(1) %in, i32 0 syncscope("wavefront") monotonic, align 4 + ret i32 %val +} + +define i32 @global_system_seq_cst_idempotent_xor(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_system_seq_cst_idempotent_xor: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: buffer_wbl2 sc0 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) lgkmcnt(0) +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc0 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: buffer_inv sc0 sc1 +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw xor ptr addrspace(1) %in, i32 0 seq_cst, align 4 + ret i32 %val +} + +define i32 @global_agent_monotonic_idempotent_and(ptr addrspace(1) %in, ptr addrspace(1) %out) { +; GFX940-LABEL: global_agent_monotonic_idempotent_and: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v4, 1 +; GFX940-NEXT: global_store_dword v[2:3], v4, off +; GFX940-NEXT: global_load_dword v0, v[0:1], off +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + store i32 1, ptr addrspace(1) %out + %val = atomicrmw and ptr addrspace(1) %in, i32 -1 syncscope("singlethread") monotonic, align 4 + ret i32 %val +}