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 @@ -13435,3 +13435,25 @@ AI->replaceAllUsesWith(Loaded); AI->eraseFromParent(); } + +LoadInst * +SITargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const { + IRBuilder<> Builder(AI); + auto Order = AI->getOrdering(); + + // The optimization removes store aspect of the atomicrmw. Therefore, cache + // must be flushed if the atomic ordering had a release semantics. This is + // not necessary a fence, a release fence just coincides to do that flush. + // Avoid replacing of an atomicrmw with a release semantics. + if (isReleaseOrStronger(AI->getOrdering())) + return nullptr; + + LoadInst *LI = Builder.CreateAlignedLoad( + AI->getType(), AI->getPointerOperand(), AI->getAlign()); + LI->setAtomic(Order, AI->getSyncScopeID()); + 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,122 @@ +; 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: global_load_dword v0, v[0:1], off sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %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: 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: + %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, 0 +; GFX940-NEXT: buffer_wbl2 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: global_atomic_or v0, v[0:1], v2, off sc0 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") release, align 4 + ret i32 %val +} + +define i32 @global_agent_acquire_release_idempotent_or(ptr addrspace(1) %in) { +; GFX940-LABEL: global_agent_acquire_release_idempotent_or: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: v_mov_b32_e32 v2, 0 +; GFX940-NEXT: buffer_wbl2 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: global_atomic_or v0, v[0:1], v2, off sc0 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: buffer_inv sc1 +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %val = atomicrmw or ptr addrspace(1) %in, i32 0 syncscope("agent-one-as") acq_rel, 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, 0 +; GFX940-NEXT: buffer_wbl2 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: global_atomic_or v0, v[0:1], v2, off sc0 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: buffer_inv sc1 +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %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: global_load_dword v0, v[0:1], off sc0 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %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: global_load_dword v0, v[0:1], off +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %val = atomicrmw sub ptr addrspace(1) %in, i32 0 syncscope("wavefront") monotonic, align 4 + ret i32 %val +} + +define i32 @global_system_monotonic_idempotent_xor(ptr addrspace(1) %in) { +; GFX940-LABEL: global_system_monotonic_idempotent_xor: +; GFX940: ; %bb.0: ; %entry +; GFX940-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0) +; GFX940-NEXT: global_load_dword v0, v[0:1], off sc0 sc1 +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %val = atomicrmw xor ptr addrspace(1) %in, i32 0 monotonic, 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: global_load_dword v0, v[0:1], off +; GFX940-NEXT: s_waitcnt vmcnt(0) +; GFX940-NEXT: s_setpc_b64 s[30:31] +entry: + %val = atomicrmw and ptr addrspace(1) %in, i32 -1 syncscope("singlethread") monotonic, align 4 + ret i32 %val +}