From 67e3c16a52e05cf1943f642ffdc0c1957b4d7101 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Thu, 5 Nov 2026 22:07:23 +0000 Subject: [PATCH] fix(gpu): take AIR 2.9's atomic qualifier between the scope and the volatility --- .../src/compiler/air_to_nir.c | 20 ++++++++++++---- .../src/compiler/tests/air_sync.c | 24 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/stela-driver-gpu/src/compiler/air_to_nir.c b/crates/stela-driver-gpu/src/compiler/air_to_nir.c index aa18a2c3f5..aa2191c08b 100644 --- a/crates/stela-driver-gpu/src/compiler/air_to_nir.c +++ b/crates/stela-driver-gpu/src/compiler/air_to_nir.c @@ -3204,6 +3204,9 @@ pair_member(struct translator *t, LLVMValueRef inst, const struct value *pair) * air.atomic...( * pointer, value, order, scope, volatile) * + * From AIR 2.9 an i32 follows the scope. Apple's compiler emits 0, the only + * value taken; the volatility, when present, stays last. + * * A floating-point type is bare ("f32"); an integer one carries its sign * ("s.i32") for arithmetic, which selects min and max. Orders are relaxed; * scopes need no lowering, as NIR's atomics are device-scope. A @@ -3238,13 +3241,13 @@ air_atomic_call(struct translator *t, LLVMValueRef inst, LLVMValueRef callee, co !memcmp(operation, forms[i].name, strlen(forms[i].name))) form = i; } - /* Then the orders, a compare-exchange's success and failure, the scope - * and, from Metal 2, the volatility. */ + /* Then the orders, a compare-exchange's success and failure, the scope, + * from AIR 2.9 its qualifier and, from Metal 2, the volatility. */ unsigned operands = forms[form].operands, scope = operands + (form == COMPARE ? 2 : 1); unsigned count = LLVMGetNumArgOperands(inst); if (!LLVMIsAFunction(callee) || !LLVMIsDeclaration(callee) || LLVMGetFunctionCallConv(callee) != LLVMCCallConv || - LLVMGetInstructionCallConv(inst) != LLVMCCallConv || (count != scope + 1 && count != scope + 2) || + LLVMGetInstructionCallConv(inst) != LLVMCCallConv || count < scope + 1 || count > scope + 3 || LLVMCountParams(callee) != count || LLVMIsFunctionVarArg(LLVMGlobalGetValueType(callee)) || LLVMGetCalledFunctionType(inst) != LLVMGlobalGetValueType(callee) || LLVMGetNumOperandBundles(inst)) @@ -3260,11 +3263,20 @@ air_atomic_call(struct translator *t, LLVMValueRef inst, LLVMValueRef callee, co return fail(t, STELA_AIR_INSTRUCTION, "AIR atomic types disagree with its name"); for (unsigned i = operands; i < count; i++) { LLVMValueRef value = LLVMGetOperand(inst, i); + /* The volatility is the one i1; a lone operand past the scope is it + * only when it is one bit wide. */ + LLVMTypeRef kind = LLVMTypeOf(value); + bool volatility = i == count - 1 && i > scope && + LLVMGetTypeKind(kind) == LLVMIntegerTypeKind && LLVMGetIntTypeWidth(kind) == 1; + bool qualifier = i == scope + 1 && !volatility; if (!LLVMIsAConstantInt(value) || - LLVMGetIntTypeWidth(LLVMTypeOf(value)) != (i > scope ? 1u : 32u)) + LLVMGetIntTypeWidth(LLVMTypeOf(value)) != (volatility ? 1u : 32u) || + (i > scope && !volatility && !qualifier)) return fail(t, STELA_AIR_INSTRUCTION, "AIR atomic order, scope and volatility must be constants"); if (i < scope && LLVMConstIntGetZExtValue(value)) return fail(t, STELA_AIR_MEMORY, "only relaxed AIR atomics are supported"); + if (qualifier && LLVMConstIntGetZExtValue(value)) + return fail(t, STELA_AIR_MEMORY, "unsupported AIR atomic scope qualifier"); } char overload[16], expected[64]; stela_air_call_overload(type, overload, sizeof(overload)); diff --git a/crates/stela-driver-gpu/src/compiler/tests/air_sync.c b/crates/stela-driver-gpu/src/compiler/tests/air_sync.c index f9211842fa..ec1e4ad7c9 100644 --- a/crates/stela-driver-gpu/src/compiler/tests/air_sync.c +++ b/crates/stela-driver-gpu/src/compiler/tests/air_sync.c @@ -249,6 +249,22 @@ air_atomics_follow_their_names(void) "%a = call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) %out, i32 1, i32 0, i32 2)\n" "store i32 %a, ptr addrspace(1) %out, align 4\nret void\n", added, ARRAY_SIZE(added)); + /* AIR 2.9 puts a qualifier, 0, between the scope and the volatility. */ + const nir_atomic_op qualified[] = {nir_atomic_op_iadd, nir_atomic_op_cmpxchg}; + expect_atomics( + "declare i32 @air.atomic.global.add.u.i32(ptr addrspace(1), i32, i32, i32, i32, i1)\n" + "declare i32 @air.atomic.global.load.i32(ptr addrspace(1), i32, i32, i32, i1)\n" + "declare void @air.atomic.global.store.i32(ptr addrspace(1), i32, i32, i32, i32, i1)\n" + "declare i32 @air.atomic.global.cmpxchg.weak.i32(ptr addrspace(1), ptr, i32, i32, i32, i32, i32, i1)\n", + "%x = alloca i32, align 4\n" + "%a = call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) %out, i32 1, i32 0, i32 2, i32 0, i1 false)\n" + "%l = call i32 @air.atomic.global.load.i32(ptr addrspace(1) %out, i32 0, i32 2, i32 0, i1 false)\n" + "store i32 %l, ptr %x, align 4\n" + "%o = call i32 @air.atomic.global.cmpxchg.weak.i32(ptr addrspace(1) %out, ptr %x, i32 %a," + " i32 0, i32 0, i32 2, i32 0, i1 false)\n" + "call void @air.atomic.global.store.i32(ptr addrspace(1) %out, i32 %o, i32 0, i32 2, i32 0, i1 false)\n" + "ret void\n", + qualified, ARRAY_SIZE(qualified)); /* Loads and stores are coherent accesses; a compare-exchange compares * and receives through its thread pointer; 64-bit min and max return * nothing. */ @@ -301,6 +317,14 @@ refuses_unqualified_atomics(void) refused("declare i32 @air.atomic.global.add.u.i32(ptr addrspace(1), i32, i32, i32, i1)\n", "%a = call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) %out, i32 1, i32 5, i32 3, i1 true)\nret void\n", STELA_AIR_MEMORY); + /* A qualifier other than the 0 Apple's compiler emits is not taken. */ + refused("declare i32 @air.atomic.global.add.u.i32(ptr addrspace(1), i32, i32, i32, i32, i1)\n", + "%a = call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) %out, i32 1, i32 0, i32 2, i32 1, i1 false)\nret void\n", + STELA_AIR_MEMORY); + /* Nothing follows the volatility. */ + refused("declare i32 @air.atomic.global.add.u.i32(ptr addrspace(1), i32, i32, i32, i1, i32)\n", + "%a = call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) %out, i32 1, i32 0, i32 2, i1 false, i32 0)\nret void\n", + STELA_AIR_INSTRUCTION); } static void