diff --git a/crates/metal2vulkan-validation/src/oracle_fuel/emit.rs b/crates/metal2vulkan-validation/src/oracle_fuel/emit.rs index 17b671152..efaa88a9d 100644 --- a/crates/metal2vulkan-validation/src/oracle_fuel/emit.rs +++ b/crates/metal2vulkan-validation/src/oracle_fuel/emit.rs @@ -40,9 +40,38 @@ pub(super) fn writes_state_kinds(enums: &mut SiteEnums) { enums.func.remove(EnumAttrs::NO_SYNC); } -/// `air.atomic.*` operands the transform passes: relaxed order, device scope, volatile. +/// `air.atomic.*` operands the transform passes: relaxed order, device scope, no `mem_flags` (the +/// AIR 2.9 form's), volatile. const ORDER_RELAXED: u64 = 0; const SCOPE_DEVICE: u64 = 2; +const MEM_FLAGS_NONE: u64 = 0; + +/// The first AIR version whose atomic intrinsics take a `mem_flags` `i32` between the scope and the +/// volatile flag (`T (ptr, i32 order, i32 scope, i32 flags, i1 volatile)` for a load): what Apple's +/// compiler writes for macOS 27, and what `metal-opt` requires of a module that states it. +const FLAGGED_ATOMICS: (u64, u64) = (2, 9); + +/// The `(major, minor)` of the module's `!air.version`, when it states one. +fn air_version(module: &Module) -> Option<(u64, u64)> { + let list = module + .named_md() + .iter() + .find(|l| module.symbols().get(l.name) == Some(b"air.version".as_slice()))?; + let [node] = &*list.ops else { + return None; + }; + let Some(Md::Node { ops, .. }) = module.md(*node) else { + return None; + }; + let int = |at: usize| match module.md((*ops.get(at)?)?)? { + Md::Value(c) => match module.consts().get(*c)?.value { + Const::Int(v) => Some(v), + _ => None, + }, + _ => None, + }; + Some((int(0)?, int(1)?)) +} /// Local iterations per pool chunk, minus one. const CHUNK_MASK: u64 = (1 << CHUNK_SHIFT) - 1; @@ -50,6 +79,18 @@ const CHUNK_MASK: u64 = (1 << CHUNK_SHIFT) - 1; /// The status argument's name in the stage metadata (diagnostics only). const STATUS_ARG_NAME: &str = "m2v_oracle_status"; +impl Ids { + /// An atomic call's operands: `head` (the pointer, and an `rmw`'s value), then the order, the + /// scope, the `mem_flags` where the module's atomics take one, and the volatile flag. + fn atomic(&self, head: &[Value]) -> Vec { + let mut args = head.to_vec(); + args.extend([self.order, self.scope]); + args.extend(self.flags); + args.push(self.volatile); + args + } +} + /// Interning over the module's tables. struct Tables<'m, 'b> { symbols: &'m mut Symbols, @@ -94,6 +135,8 @@ struct Ids { exhausted: Value, order: Value, scope: Value, + /// The `mem_flags` immediate, when the module's atomics take one ([`FLAGGED_ATOMICS`]). + flags: Option, volatile: Value, word: [Value; 4], one_count: Value, @@ -203,6 +246,7 @@ pub(super) fn emit( let entry_name = name(&module, plan.entry); let site = entry_site(&module, plan.entry) .ok_or(FuelUnsupported::EntryNotAStage { entry: entry_name })?; + let flagged = air_version(&module).is_some_and(|v| v >= FLAGGED_ATOMICS); let ModuleParts { mut symbols, mut types, @@ -250,17 +294,30 @@ pub(super) fn emit( let order = int(&mut t, i32, ORDER_RELAXED)?; let scope = int(&mut t, i32, SCOPE_DEVICE)?; let volatile = int(&mut t, i1, 1)?; + let flags = if flagged { + Some(int(&mut t, i32, MEM_FLAGS_NONE)?) + } else { + None + }; let null_status = Value::Const(t.constant(device_ptr, Const::Null)?); - // The runtime intrinsics: reuse a declaration of the same name, else declare one. + // The runtime intrinsics: reuse a declaration of the same name, else declare one, in the form + // of the module's AIR version. + let immediates = |head: &[TypeId]| -> Vec { + let mut params = head.to_vec(); + params.extend([i32, i32]); + params.extend(flagged.then_some(i32)); + params.push(i1); + params + }; let load_ty = t.ty(Type::Function { ret: i32, - params: vec![device_ptr, i32, i32, i1].into(), + params: immediates(&[device_ptr]).into(), vararg: false, })?; let rmw_ty = t.ty(Type::Function { ret: i32, - params: vec![device_ptr, i32, i32, i32, i1].into(), + params: immediates(&[device_ptr, i32]).into(), vararg: false, })?; // The typed-pointer declaration is `i32 (i32 addrspace(1)*, ...)`, as Metal declares it. @@ -289,7 +346,7 @@ pub(super) fn emit( decl } None => { - let params = if which.rmw { 5 } else { 4 }; + let params = if which.rmw { 5 } else { 4 } + usize::from(flagged); funcs.push(Function { name: sym, ty: fn_ty, @@ -309,11 +366,13 @@ pub(super) fn emit( decl } }; - let args: Vec = if which.rmw { - vec![null_status, one, order, scope, volatile] + let mut args: Vec = if which.rmw { + vec![null_status, one, order, scope] } else { - vec![null_status, order, scope, volatile] + vec![null_status, order, scope] }; + args.extend(flags); + args.push(volatile); let class = classify(which.name.as_bytes(), sym, fn_ty, t.types); let bound = match class { Some(class @ DeclClass::Air(_)) => bind(class, &args, t.consts) @@ -349,6 +408,7 @@ pub(super) fn emit( exhausted: int(&mut t, i32, u64::from(EXHAUSTED))?, order, scope, + flags, volatile, word, one_count: int(&mut t, i32, 1)?, @@ -795,16 +855,7 @@ impl Rewrite<'_> { e.push( exit, ids.i32, - self.call( - &ids.or, - &[ - frame.status, - ids.exhausted, - ids.order, - ids.scope, - ids.volatile, - ], - ), + self.call(&ids.or, &ids.atomic(&[frame.status, ids.exhausted])), ); e.push(exit, ids.void, self.store(ids.one, frame.forced)); e.push(exit, ids.void, InstKind::Ret(self.ret_zero)); @@ -836,10 +887,7 @@ impl Rewrite<'_> { let bits = e.push( pool, ids.i32, - self.call( - &ids.load, - &[frame.status, ids.order, ids.scope, ids.volatile], - ), + self.call(&ids.load, &ids.atomic(&[frame.status])), ); let bit = e.push(pool, ids.i32, self.bin(BinOp::And, bits, ids.exhausted)); let done = e.push(pool, ids.i1, self.icmp(IntPredicate::Ne, bit, ids.zero)); @@ -853,21 +901,14 @@ impl Rewrite<'_> { let taken = e.push( draw, ids.i32, - self.call( - &ids.add, - &[consumed, ids.one, ids.order, ids.scope, ids.volatile], - ), + self.call(&ids.add, &ids.atomic(&[consumed, ids.one])), ); let size = e.push( draw, ids.device_ptr, self.gep(ids.i32, frame.status, &[ids.word[2]]), ); - let chunks = e.push( - draw, - ids.i32, - self.call(&ids.load, &[size, ids.order, ids.scope, ids.volatile]), - ); + let chunks = e.push(draw, ids.i32, self.call(&ids.load, &ids.atomic(&[size]))); let over = e.push(draw, ids.i1, self.icmp(IntPredicate::Uge, taken, chunks)); e.push(draw, ids.void, self.cond_br(over, exit, join)); (check, join) diff --git a/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs b/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs index 5d845959d..86cf18866 100644 --- a/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs +++ b/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs @@ -1603,6 +1603,73 @@ fn public_fixtures_with_loops_round_trip_instrumented() { ); } +/// The status atomics take the form of the module's AIR version: a module that states AIR 2.9 +/// (Apple's compiler for macOS 27) gets the `mem_flags` `i32` between the scope and the volatile +/// flag, which `metal-opt` requires of it (it refused the AIR 2.8 load in every `tns_mm_v2` case); +/// one that states AIR 2.8, or no version, keeps the AIR 2.8 form. Every call passes as many +/// operands as its declaration takes. +#[test] +fn the_status_atomics_take_the_form_of_the_modules_air_version() { + if !llvm_as("the_status_atomics_take_the_form_of_the_modules_air_version") { + return; + } + const LOOP: &str = "entry:\n br label %l\nl:\n %i = phi i32 [ 0, %entry ], [ %i1, %l ]\n \ + %i1 = add i32 %i, 1\n %c = icmp ult i32 %i1, %n\n \ + br i1 %c, label %l, label %d\nd:\n ret void"; + let version = + |minor: u32| format!("!air.version = !{{!9}}\n!9 = !{{i32 2, i32 {minor}, i32 0}}"); + for (stated, flagged) in [ + (String::new(), false), + (version(8), false), + (version(9), true), + ] { + let out = run(kernel(LOOP, &stated)).unwrap(); + assert!(out.report.loops > 0, "{stated}: no fuel loop"); + let text = write(&out.module, &out.pointees, &out.enums).unwrap(); + let flags = if flagged { "i32, " } else { "" }; + for declaration in [ + format!("@\"air.atomic.global.load.i32\"(i32 addrspace(1)*, i32, i32, {flags}i1)"), + format!( + "@\"air.atomic.global.add.u.i32\"(i32 addrspace(1)*, i32, i32, i32, {flags}i1)" + ), + format!("@\"air.atomic.global.or.u.i32\"(i32 addrspace(1)*, i32, i32, i32, {flags}i1)"), + ] { + assert!( + text.contains(&declaration), + "{stated}: {declaration}\n{text}" + ); + } + for name in [ + "air.atomic.global.load.i32", + "air.atomic.global.add.u.i32", + "air.atomic.global.or.u.i32", + ] { + let decl = func(&out.module, name); + let params = param_count(&out.module, name); + let calls = out + .module + .parts() + .funcs + .iter() + .filter_map(|(_, f)| f.body.as_ref()) + .flat_map(|b| b.insts.iter().map(|(_, i)| &i.kind)) + .filter_map(|k| match k { + InstKind::Call(c) => match c.callee { + Callee::Air { decl: d, .. } if d == decl => Some(c.args.len()), + _ => None, + }, + _ => None, + }) + .collect::>(); + assert!(!calls.is_empty(), "{stated}: @{name} is never called"); + assert!( + calls.iter().all(|&n| n == params), + "{stated}: @{name}: {calls:?} != {params}" + ); + } + } +} + /// A module whose loops are all barrier loops calls neither the atomic load nor the add, so no /// use constrains their pointer parameter: the transform records it, and the declarations are /// written as Metal declares them (Q2: `metal-opt` refused `air.atomic.global.load.i32` with an