Skip to content

Translate the remaining atomic instructions - #17

Merged
maleadt merged 3 commits into
mainfrom
tb/atomic-instructions
Oct 9, 2026
Merged

maleadt merged 3 commits into
mainfrom
tb/atomic-instructions

Conversation

@maleadt

@maleadt maleadt commented Oct 9, 2026

Copy link
Copy Markdown
Member

Fixes #14. These atomic instructions failed translation, and are now lowered the way the Khronos SPIRV-LLVM-Translator lowers them:

  • OpAtomicCompareExchangeWeak takes the OpAtomicCompareExchange path, since SPIR-V gives it the same semantics: atomic_cmpxchg below OpenCL C 2.0, atomic_compare_exchange_strong_explicit from 2.0.
  • OpAtomicFMinEXT / OpAtomicFMaxEXT lower to atomic_fetch_min_explicit / atomic_fetch_max_explicit on atomic_float / atomic_double under cl_ext_float_atomics. They are refused below 2.0, like the float add: there, Khronos emits a float atomic_min / atomic_max, which OpenCL C 1.x doesn't have. Half-precision min/max stays unsupported.
  • OpAtomicFlagTestAndSet / OpAtomicFlagClear lower to atomic_flag_test_and_set_explicit / atomic_flag_clear_explicit from 2.0. Below 2.0, relaxed flag operations exchange 1 or 0 into the flag's storage, as Khronos does there. Ordered ones are refused, like the other 1.x atomics.

The new tests compile their output with clang, which accepts the float min/max and flag builtins since llvm#168016 (clang 22).

Once this is in a spirv2clc_jll release, OpenCL.jl can stop masking the float min/max features on the spirv2clc path (src/compiler/compilation.jl).

SPIR-V gives the weak compare-exchange the semantics of the strong one,
so lower it the same way, as the Khronos translator does: atomic_cmpxchg
below OpenCL C 2.0, atomic_compare_exchange_strong_explicit from 2.0.
OpAtomicFMinEXT and OpAtomicFMaxEXT (SPV_EXT_shader_atomic_float_min_max)
lower like the floating-point atomic add, to atomic_fetch_min_explicit and
atomic_fetch_max_explicit on atomic_float or atomic_double under
cl_ext_float_atomics, and are refused below OpenCL C 2.0. Their NaN and
signed-zero behaviour is the same in both extensions. Half-precision
min/max stays unsupported, because atomic_c11_pointer has no atomic_half.
OpAtomicFlagTestAndSet and OpAtomicFlagClear lower to
atomic_flag_test_and_set_explicit and atomic_flag_clear_explicit on the
flag's 32-bit integer storage reinterpreted as atomic_flag, keeping their
memory order and scope.

OpenCL C 1.x has no flags. As in the Khronos translator, relaxed flag
operations there exchange 1 or 0 into the storage, which works because
only flag instructions may access it. Ordered ones are refused, like the
other 1.x atomics.
@maleadt
maleadt merged commit 78f621a into main Oct 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Untranslated atomic instructions: CompareExchangeWeak, FlagTestAndSet/FlagClear, FMinEXT/FMaxEXT

1 participant