diff --git a/crates/metal2vulkan-runtime/src/tests/translate.rs b/crates/metal2vulkan-runtime/src/tests/translate.rs index 6294a2f85..d8ba6cce6 100644 --- a/crates/metal2vulkan-runtime/src/tests/translate.rs +++ b/crates/metal2vulkan-runtime/src/tests/translate.rs @@ -14,7 +14,8 @@ //! An access through a buffer argument at 2 GiB and past lands there, on every device that can //! allocate the buffer. A kernel that reads the stage-in region stores the region each dispatch //! was encoded under, and is refused where the encoder set none; a kernel's `[[stage_in]]` -//! attributes are fetched from the element each step function names in that region. A read of a texture the kernel's function constants removed takes zero, +//! attributes are fetched from the element each step function names in that region, in a ragged +//! grid's partial threadgroup too, whose invocations past the grid write nothing. A read of a texture the kernel's function constants removed takes zero, //! and a read through a buffer argument bound to nothing reads the nil buffer's zeros. //! An observer whose partial-threadgroups variant's vote word takes its threadgroup memory past //! the device's limit runs its whole grids, threadgroup dispatches and indirect ones on its plain @@ -3573,6 +3574,119 @@ define void @nil_read_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i32 %t }); } + /// A `dispatchThreads` grid of 25 threads in threadgroups of 1,024, one partial threadgroup of + /// 25, of each kernel that fetches a `[[stage_in]]` attribute by thread position, under a + /// stage-in region at origin 7 and 1,024 elements long, over a source buffer that ends at the + /// last thread's element, 7 + 24: `engine/kernel_stage_in`'s `stage_in_fetch` under + /// [`stage_in_fetch`]'s descriptor, which does not observe its threadgroup partition and so + /// runs its grid in tiles, and `engine/kernel_stage_in_groups`' `stage_in_neighbour` (one + /// `uint` at index 1, 4 bytes an element), which observes it and holds a threadgroup barrier, + /// so its partial threadgroup walks the body predicated and adds one to + /// `Metrics::partial_variant_ragged_grids`. Each of the 25 threads reports what Metal's does + /// (`stage_in_neighbour`: its threadgroup's next thread's word, 25 threads, its position and + /// index), and every word past the grid keeps its fill, so no invocation past the threadgroup + /// writes. A fetch past the grid would read past the buffer, which the fetch's check reads as + /// zero and no output shows: the translator's interpreter test of the same layout traps on it. + /// Before the partial-threadgroups variant took a stage-input region, each dispatch was + /// `Gap::PartialThreadgroups` at the translator's site. + #[test] + fn every_device_runs_a_stage_in_kernels_partial_threadgroup() { + let (Some(plain), Some(walked)) = ( + descriptor_of("kernel_stage_in", "stage_in_fetch"), + descriptor_of("kernel_stage_in_groups", "stage_in_neighbour"), + ) else { + return; + }; + const THREADS: u32 = 25; + const ORIGIN: u32 = 7; + const FILL: u32 = 0xEEEE_EEEE; + let tiled = stage_in_fetch(&plain, Some(40), StepFunction::ThreadPositionInGridX, 1); + let mut walked = walked; + let mut input = StageInputDescriptor::new(); + let mut layout = StageInputLayoutDescriptor::new(Some(4)); + layout.step_function = StepFunction::ThreadPositionInGridX; + input.layouts.insert(1, layout); + let attribute = StageInputAttributeDescriptor::new(AttributeFormat::UInt, 0, 1); + input.attributes.insert(0, attribute); + walked.stage_input = Some(input); + let word = |e: u32| 0x5150_0000 + 3 * e; + // Elements 0 to 7 + 24, the last thread's. + let elements = 0..ORIGIN + THREADS; + let fetched: Vec = elements.clone().flat_map(stage_in_element).collect(); + let fetched = le_bytes(&fetched); + let neighbours = le_bytes(&elements.map(word).collect::>()); + // Thread `x`'s words: `stage_in_fetch`'s twelve, `stage_in_neighbour`'s four. + let fetch_words = |x: u32| -> Vec { + let [front @ .., i, h] = stage_in_element(ORIGIN + x); + front + .into_iter() + .chain([i, h & 0xffff, h >> 16, x]) + .collect() + }; + let neighbour_words = |x: u32| vec![word(ORIGIN + (x + 1) % THREADS), THREADS, x, x]; + type Words<'a> = &'a dyn Fn(u32) -> Vec; + // Each kernel, its descriptor, its source's bytes, its words a thread, and thread `x`'s. + let kernels: [(&str, &ComputePipelineDescriptor, &[u8], usize, Words<'_>); 2] = [ + ("stage_in_fetch", &tiled, &fetched, 12, &fetch_words), + ( + "stage_in_neighbour", + &walked, + &neighbours, + 4, + &neighbour_words, + ), + ]; + each_device("compute_stage_in_partial_threadgroup", |name, _, device| { + let queue = device.new_command_queue(4).expect("a queue"); + let mut buffers = Buffers::new(&device, 1 << 17); + let mut failed = Vec::new(); + for (kernel, descriptor, bytes, per, expected) in kernels { + let label = format!("{name}: {kernel}"); + let state = state(&label, &device, descriptor); + let source = buffers.buffer(bytes.len() as u64); + write(&source, 0, bytes); + // A thread's words for every invocation of the threadgroup, and one past it. + let count = per * 1025; + let out = buffers.buffer(4 * count as u64); + write(&out, 0, &le_bytes(&vec![FILL; count])); + let before = device.counters().metrics(); + let command_buffer = queue.command_buffer().expect("a command buffer"); + let mut encoder = command_buffer + .compute_command_encoder(DispatchType::Serial) + .expect("an encoder"); + encoder.set_compute_pipeline_state(&state).expect("a state"); + encoder.set_buffer(Some(&out), 0, 0).expect("a buffer"); + encoder.set_buffer(Some(&source), 0, 1).expect("a buffer"); + encoder.set_stage_in_region(Region { + origin: [ORIGIN, 0, 0], + size: [1024, 1, 1], + }); + let dispatched = encoder.dispatch_threads([THREADS, 1, 1], [1024, 1, 1]); + encoder.end_encoding(); + if let Err(error) = dispatched { + println!("{label}: the dispatch: {error:?}"); + failed.push(label); + continue; + } + command_buffer.commit().expect("a commit"); + finished(&label, &command_buffer); + assert_eq!(command_buffer.error(), None, "{label}"); + let mut want: Vec = (0..THREADS).flat_map(expected).collect(); + want.resize(count, FILL); + assert_eq!(words(&out, 0, count), want, "{label}: the words written"); + let ragged = device.counters().metrics().partial_variant_ragged_grids + - before.partial_variant_ragged_grids; + assert_eq!( + ragged, + u64::from(kernel == "stage_in_neighbour"), + "{label}: ragged grids on the partial-threadgroups variant" + ); + println!("{label}: 25 threads of a threadgroup of 1,024 answered Metal's words"); + } + assert!(failed.is_empty(), "refused: {failed:?}"); + }); + } + /// The kernel `entry` of the harness fixture (`fixtures/public/harness/workloads.ll`, our /// own), or None where `llvm-as` is missing. fn harness_kernel(entry: &str) -> Option { diff --git a/crates/metal2vulkan/src/tests/engine_identity.rs b/crates/metal2vulkan/src/tests/engine_identity.rs index 45b6cf71d..706496204 100644 --- a/crates/metal2vulkan/src/tests/engine_identity.rs +++ b/crates/metal2vulkan/src/tests/engine_identity.rs @@ -20,6 +20,12 @@ //! - A kernel's partial-threadgroups variant (`partial`, generated) dispatched at its whole //! threadgroup size over a ragged grid, its pushed region flagged `NONUNIFORM`, answers each //! edge threadgroup's own size, and its invocations past the grid store nothing. +//! - A kernel's `[[stage_in]]` attribute on its partial-threadgroups variant +//! (`engine/kernel_stage_in`'s `stage_in_fetch`, and `engine/kernel_stage_in_groups`' +//! `stage_in_neighbour`, whose barrier the variant walks predicated), over one partial +//! threadgroup of 25 threads of 1,024, a stage-in region that clamps nothing and a source buffer +//! that ends at the last thread's element: on the CPU interpreter no invocation fetches past the +//! buffer or stores past the output, and each kernel stores Metal's answers. //! - The partial-threadgroups variant of a kernel that does not observe its threadgroup partition //! (`edge`, generated) runs its grid in tiles (`RuntimeFact::GridTiles`), its module holds //! no division by a value the driver does not know, and over grids ragged along every axis, @@ -3145,6 +3151,176 @@ fn a_partial_threadgroups_variant_answers_the_edge_threadgroups_of_a_ragged_grid } } +/// A kernel's `[[stage_in]]` attribute, fetched by thread position, on its partial-threadgroups +/// variant: 25 threads in one threadgroup of 1,024 invocations, the region flagged `NONUNIFORM`, +/// the stage-in region at origin 7 and 1,024 elements long, so it clamps nothing, and the source +/// buffer ending at the last thread's element, 7 + 24. A fetch past the grid's threads would read +/// past the buffer, which its check sends to the zero range the test leaves unmapped, so the +/// interpreter traps; and the output ends at the last thread's words, so a store past them traps +/// too. `engine/kernel_stage_in`'s `stage_in_fetch`, which does not observe its partition, returns +/// its invocations past the grid before the fetch; `engine/kernel_stage_in_groups`' +/// `stage_in_neighbour`, whose threadgroup barrier the variant walks predicated, fetches in every +/// invocation, each at a position inside the grid. Each stores Metal's answers. +#[test] +fn a_stage_in_kernels_partial_threadgroup_fetches_inside_its_buffer() { + use crate::metal::{AttributeFormat, StepFunction}; + use crate::pipeline::{ + StageInputAttributeDescriptor, StageInputDescriptor, StageInputLayoutDescriptor, + }; + let (Some(fetch), Some(groups)) = ( + assemble("kernel_stage_in"), + assemble("kernel_stage_in_groups"), + ) else { + return; + }; + const THREADS: u32 = 25; + const ORIGIN: u32 = 7; + const SRC: u64 = 0x2_0000_1000; + // A descriptor of `attributes` (format, offset) in the layout at index 1, `stride` bytes an + // element, stepping by the thread's position along x. + let descriptor = |attributes: &[(AttributeFormat, u32)], stride: u32| { + let mut d = StageInputDescriptor::new(); + let mut layout = StageInputLayoutDescriptor::new(Some(stride)); + layout.step_function = StepFunction::ThreadPositionInGridX; + d.layouts.insert(1, layout); + for (index, &(format, offset)) in (0..).zip(attributes) { + let attribute = StageInputAttributeDescriptor::new(format, offset, 1); + d.attributes.insert(index, attribute); + } + d + }; + // `stage_in_fetch`'s element `e`: a `float4`, a `uint4`, an `int` and a `half2`, all normal. + let element = |e: u32| -> [u32; 10] { + let f = [0, 1, 2, 3].map(|k| 0x4000_0000 + 4 * e + k); + let u = [e, 2 * e, e + 100, 0xdead_0000 | e]; + [ + f[0], + f[1], + f[2], + f[3], + u[0], + u[1], + u[2], + u[3], + !e, + (0x3c00 + e) | (0x4000 + e) << 16, + ] + }; + let word = |e: u32| 0x5150_0000 + 3 * e; + let words = |v: Vec| -> Vec { v.into_iter().flat_map(u32::to_le_bytes).collect() }; + let elements = 0..ORIGIN + THREADS; + let fetched = words(elements.clone().flat_map(element).collect()); + let neighbours = words(elements.map(word).collect()); + let fetch_answer = words( + (0..THREADS) + .flat_map(|x| { + let [front @ .., i, h] = element(ORIGIN + x); + front.into_iter().chain([i, h & 0xffff, h >> 16, x]) + }) + .collect(), + ); + let neighbour_answer = words( + (0..THREADS) + .flat_map(|x| [word(ORIGIN + (x + 1) % THREADS), THREADS, x, x]) + .collect(), + ); + let fetch_attributes = [ + (AttributeFormat::Float4, 0), + (AttributeFormat::UInt4, 16), + (AttributeFormat::Int, 32), + (AttributeFormat::Half2, 36), + ]; + let kernels = [ + ( + "stage_in_fetch", + &fetch, + descriptor(&fetch_attributes, 40), + &fetched, + &fetch_answer, + ), + ( + "stage_in_neighbour", + &groups, + descriptor(&[(AttributeFormat::UInt, 0)], 4), + &neighbours, + &neighbour_answer, + ), + ]; + let mut region_in = Vec::new(); + for word in [ORIGIN, 0, 0, 0, 1024, 1, 1, 0] { + region_in.extend_from_slice(&word.to_le_bytes()); + } + let mut region = region_bytes([0; 3], [THREADS, 1, 1], [1024, 1, 1]).expect("packs"); + region[28..32].copy_from_slice(&NONUNIFORM.to_le_bytes()); + for (entry, bc, d, source, want) in kernels { + let mut inputs = LowerInputs::new(); + inputs.partial_threadgroups = true; + inputs.stage_input = Some(d); + for stem in PROFILES { + let p = profile(stem, identity()).unwrap_or_else(|e| panic!("{e}")); + let a = compile_with(bc, entry, &p, &inputs) + .unwrap_or_else(|e| panic!("{stem} {entry}: {e}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{stem} {entry}: L3: {e}")); + let (plan, module) = typed_module_with(bc, entry, &p, &inputs) + .unwrap_or_else(|e| panic!("{stem} {entry}: {e}")); + assert!( + plan.facts.contains(&RuntimeFact::PartialThreadgroups), + "{stem} {entry}: {:?}", + plan.facts + ); + let mut push = vec![0u8; plan.push.push_range.map_or(0, |(_, end)| end as usize)]; + for f in &plan.push.fields { + let PushLoc::Push { offset } = f.loc else { + panic!("{stem} {entry}: {f:?} spilled"); + }; + let o = offset as usize; + // The output at slot 0, the source at slot 1: each slot's address and length. + let at = |s: SlotRef| match s.index { + 0 => (OUT, want.len()), + _ => (SRC, source.len()), + }; + match f.field { + PushField::SlotGuestBase(s) => { + push[o..o + 8].copy_from_slice(&slot_word(at(s).0).to_le_bytes()) + } + PushField::SlotBounds(s) => { + push[o..o + 8].copy_from_slice(&bounds_word(at(s).1, 0)) + } + PushField::SlotBindOffset(_) => push[o..o + 4].fill(0), + PushField::ZeroAddress => { + push[o..o + 8].copy_from_slice(&ZERO_UNMAPPED.to_le_bytes()) + } + PushField::DispatchRegion => push[o..o + 32].copy_from_slice(®ion), + PushField::StageInRegion => push[o..o + 32].copy_from_slice(®ion_in), + _ => panic!("{stem} {entry}: unexpected push field {f:?}"), + } + } + let res = Resources { + buffers: vec![ + Buffer { + slot: None, + address: Some(OUT), + bytes: vec![0xEE; want.len()], + }, + Buffer { + slot: None, + address: Some(SRC), + bytes: source.clone(), + }, + ], + push, + spec: local_size(1024), + }; + let cfg = Config { + barriers: BarrierRule::Vulkan, + ..Config::default() + }; + let got = run(&module, &cfg, res).unwrap_or_else(|e| panic!("{stem} {entry}: {e}")); + assert_eq!(&got.buffers[0], want, "{stem} {entry}"); + } + } +} + /// A kernel that does not observe its threadgroup partition, storing at its position in the grid /// `(x, y, z)` of a grid `(gx, gy, gz)` the word `1 + x + (y << 10) + (z << 20)`, at /// `(z · gy + y) · gx + x`.