[RFC] Add elementwise modifier to atomicrmw

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 elementwise modifier when the value type is a scalar? Or should we just treat it as a noop for scalar types?
4 Likes

Wouldn’t it make more sense to lower it as non-elementwise if the target already supports it?

I initially had misgivings about treating this as a modifier, as opposed to a full suite of elementwise opcodes. However, I think it is safe to introduce the modifier in this direction. If you were to perform a full vector atomic for all elements, that would be conservatively correct. So I think this is fine.

Yes. The defaults should do whatever is conservatively correct, failing is bad.

This should be an IR verifier error

1 Like

yes that would me more sense

+1 to the proposal with the already discussed modification to lower to the non-elementwise version by default.

PR is up: [IR][AtomicExpand][NVPTX] Add elementwise modifier to atomicrmw; implement ISEL for NVPTX by YonahGoldberg · Pull Request #188393 · llvm/llvm-project · GitHub

Looking at [RFC] Add elementwise modifier to atomic loads and stores, why is this current proposal limited to atomicrmw only? Understandably, the motivation may have come from some desire to optimize things on NVIDIA hardware, but I don’t think the change should be limited by that. This should be done all the way through to cover all atomic operations. Even the PTX memory model talks about all memory operations and not just rmw operations.

2 Likes

Well my motivation was to represent PTX vector atomics, which uniquely have these semantics and I didn’t see the use case for the other atomic operations. As it so happens, I think I saw a use case for elementwise atomic loads/stores from some GFX workload where we use an intrinsic internally, so this is beneficial for us as well. I’m going to take a look at the RFC.

1 Like