Index: llvm/lib/Target/AMDGPU/SIISelLowering.h =================================================================== --- llvm/lib/Target/AMDGPU/SIISelLowering.h +++ llvm/lib/Target/AMDGPU/SIISelLowering.h @@ -494,6 +494,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 @@ -13360,3 +13360,21 @@ AI->replaceAllUsesWith(Loaded); AI->eraseFromParent(); } + +LoadInst * +SITargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const { + IRBuilder<> Builder(AI); + auto Order = + AtomicCmpXchgInst::getStrongestFailureOrdering(AI->getOrdering()); + auto SSID = AI->getSyncScopeID(); + + // We do not need to insert a fence here, memory legalizer will do. + 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,127 @@ +; 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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, off +; 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, off +; 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) undef + %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) { +; 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 v2, 1 +; GFX940-NEXT: global_store_dword v[0:1], v2, 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) undef + %val = atomicrmw and ptr addrspace(1) %in, i32 -1 syncscope("singlethread") monotonic, align 4 + ret i32 %val +}