Overview
I am proposing to add an elementwise modifier to the atomicrmw instruction:
%result = atomicrmw elementwise fadd ptr %ptr, <4 x float> %val
The modifier changes vector atomicrmw semantics from being atomic across the whole vector to being atomic only across each lane.
Motivation
See [NVPTX] atomicrmw on <4 x float> relies on __atomic_compare_exchange_16 · Issue #122760 · llvm/llvm-project · GitHub where this comes up.
Current atomicrmw semantics enforce that the instruction is atomic across reading the entire vector, performing the operation across the entire vector, then storing the entire vector.
NVIDIA hardware atomics only guarentee atomicity across the individual elements of the vector.
From the PTX spec:
Memory operations with a vector data type are modeled as a set of equivalent memory operations with a scalar data type, executed in an unspecified order on the elements in the vector.
So NVPTX can’t lower atomicrmw to PTX atom for vector types. Instead, we lower to a CAS loop when available, and, when not available, we fallback to a call to a non-existant __atomic_compare_exchange_*.
Currently NVVM maintains a set of atomic intrinsics that we’d like to move away from:
defm int_nvvm_atomic_add_gen : PTXAtomicWithScope2_fi;
defm int_nvvm_atomic_inc_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_dec_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_exch_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_xor_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_max_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_min_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_or_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_and_gen_i : PTXAtomicWithScope2<llvm_anyint_ty>;
defm int_nvvm_atomic_cas_gen_i : PTXAtomicWithScope3<llvm_anyint_ty>;
Allowing us to represent hardware vector atomics in the IR is a good step.
Proposed Syntax
The textual syntax becomes:
atomicrmw [volatile] [elementwise] <operation> ptr <pointer>, <ty> <value>
[syncscope("<target-scope>")] <ordering>[, align <alignment>]
Semantics
The semantics will be the same as NVIDIA hardware semantics. i.e. the element-wise vector atomic behaves as a series of scalar atomics, issued in an arbitrary order.
Lowering Strategy
By default atomicrmw elementwise will be expanded in AtomicExpandPass to a series of scalar atomic instructions.
I will provide a target hook to allow custom lowering in the backend.
I will support this in both SelectionDAG and GlobalIsel.
Alternatives
- Continue using intrinsics
- Frontends could just lower to a series of scalar atomics and we could try to combine with DAGCombiner, but the pattern matching for that seems brittle.
Questions
- Would any other backends benefit from this? Or is NVIDIA the only one with vector atomics with these semantics?
- Is the default expansion to a series of scalar atomics the right approach? Or should we fail to lower by default?
- Should we not allow the
elementwisemodifier when the value type is a scalar? Or should we just treat it as a noop for scalar types?