From bda23b360fd8214629312813f63bf6d2416ab0d2 Mon Sep 17 00:00:00 2001 From: Alexis Duburcq Date: Sun, 29 Mar 2026 23:33:38 +0200 Subject: [PATCH] Fix Support of float atomics in threadgroup memory. --- spirv_msl.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/spirv_msl.cpp b/spirv_msl.cpp index 1291d5d22..b589023be 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -11098,6 +11098,20 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, else if (opcode == OpAtomicSMax || opcode == OpAtomicSMin) expected_type = to_signed_basetype(type.width); + // Metal does not support float atomics in threadgroup memory. When the + // atomic result type is integer but the pointee type is float (CAS-based + // emulation of float atomics), override the pointer cast type to emit + // atomic_uint / atomic_ulong instead of atomic_float. We leave + // expected_type unchanged so the result bitcast logic is not affected. + auto atomic_cast_type = expected_type; + if (ptr_type.storage == spv::StorageClassWorkgroup && + type_is_floating_point(type)) + { + auto &res_type = get(result_type); + if (type_is_integral(res_type)) + atomic_cast_type = res_type.basetype; + } + bool use_native_image_atomic; if (msl_options.supports_msl_version(3, 1)) use_native_image_atomic = check_atomic_image(obj); @@ -11108,7 +11122,7 @@ void CompilerMSL::emit_atomic_func_op(uint32_t result_type, uint32_t result_id, SPIRV_CROSS_THROW("MSL currently does not support 64-bit atomics."); auto remapped_type = type; - remapped_type.basetype = expected_type; + remapped_type.basetype = atomic_cast_type; auto *var = maybe_get_backing_variable(obj); const auto *res_type = var ? &get(var->basetype) : nullptr;