diff --git a/crates/metal2vulkan-runtime/src/tests/deep_threadgroups.rs b/crates/metal2vulkan-runtime/src/tests/deep_threadgroups.rs new file mode 100644 index 000000000..c031c5238 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/deep_threadgroups.rs @@ -0,0 +1,88 @@ +//! A threadgroup deeper than the floor's `maxComputeWorkGroupSize` z, under a device cfg on every +//! device of the cfg's kind: `simd_sum_k` (the owned fixture `engine/simd_ops.ll`, each thread's +//! `simd_sum(t) + simd_sum(float(t & 7)) * 100000` at its threadgroup index `t`) dispatched as one +//! threadgroup of `[1, 1, 96]`, which a kernel whose workgroup is its threadgroup runs as one row +//! (`SpecKey::DeepThreadgroup`) and a flattened one as its flat row, so the dispatch is within +//! every device's limits. Each simdgroup is 32 consecutive threads, as Metal's are: thread `t` +//! answers `1024 (t / 32) + 496 + 11200000`. +//! +//! `llvm-as` assembles the fixture; without it the test skips visibly. + +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod on_device { + use crate::tests::command::on_device::{each_device, finished}; + use crate::{ + ComputePipelineDescriptor, DispatchType, FunctionDescriptor, Library, MemoryClass, + StageSource, + }; + use metal2vulkan_devtools::{engine_dir, llvm_assemble, AssembleError}; + + /// The threadgroup's depth: past `DEEP_THREADGROUP`, the floor's z. + const DEPTH: u32 = 96; + + /// What thread `t` writes. + fn want(t: u32) -> u32 { + 1024 * (t / 32) + 496 + 112 * 100_000 + } + + /// One threadgroup of `[1, 1, 96]` answers Metal's simdgroups of 32 consecutive threads on + /// every device. + #[test] + fn every_device_runs_a_threadgroup_deeper_than_the_floor() { + let path = engine_dir().join("simd_ops.ll"); + let bitcode = match llvm_assemble(&path) { + Ok(bitcode) => bitcode, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(error) => panic!("simd_ops: {error:?}"), + }; + let library = Library::from_air(&bitcode).expect("the library"); + let function = library.function("simd_sum_k").expect("the kernel"); + let descriptor = ComputePipelineDescriptor::new(StageSource::Function( + FunctionDescriptor::new(function), + )); + let out_len = u64::from(DEPTH * 4); + each_device("deep_threadgroups", |name, _, device| { + let state = device + .new_compute_pipeline_state(&descriptor, crate::tests::no_constexpr_samplers) + .unwrap_or_else(|error| panic!("{name}: {error}")); + let block = device + .take_block(MemoryClass::Shared, out_len) + .expect("a Shared block"); + let output = device + .new_buffer(block.id, 0, out_len, Some(0)) + .expect("the output"); + let queue = device.new_command_queue(4).expect("a queue"); + 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(&output), 0, 0).expect("a buffer"); + encoder + .dispatch_threads([1, 1, DEPTH], [1, 1, DEPTH]) + .unwrap_or_else(|error| panic!("{name}: the dispatch: {error}")); + encoder.end_encoding(); + command_buffer.commit().expect("a commit"); + finished(name, &command_buffer); + assert_eq!(command_buffer.error(), None, "{name}"); + let contents = output.contents().expect("a Shared buffer"); + let mut wrong = Vec::new(); + for t in 0..DEPTH { + // SAFETY: the word lies in the buffer, which is alive, and its work completed. + let got = unsafe { + (contents.as_ptr().cast::()) + .add(t as usize) + .read_unaligned() + }; + if got != want(t) { + wrong.push((t, got, want(t))); + } + } + assert!(wrong.is_empty(), "{name}: words differ: {wrong:?}"); + println!("{name}: one threadgroup of [1, 1, {DEPTH}], Metal's simdgroup sums"); + }); + } +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 4dc8d388a..1baa0b7c1 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -14,6 +14,7 @@ mod copy_out; mod copy_over; mod cube_views; mod d3d11; +mod deep_threadgroups; mod device; mod dispatch_tiles; mod dma_bufs; diff --git a/crates/metal2vulkan/src/bind/dispatch.rs b/crates/metal2vulkan/src/bind/dispatch.rs index 4655e344f..9897b3972 100644 --- a/crates/metal2vulkan/src/bind/dispatch.rs +++ b/crates/metal2vulkan/src/bind/dispatch.rs @@ -326,12 +326,16 @@ fn region_flags(rules: &LocalRules, r: [u32; 3]) -> vk::PipelineShaderStageCreat } /// The local-size rules of the kernel's emulated-simdgroups variant as the plan of its flattened -/// variant knows them: the same device limits, a local size that is not flattened, and no whole -/// subgroups (the flattened plan's multiples are all its tier's, [`partial`]). The variant's +/// variant knows them: the same device limits, a local size that is not flattened, a threadgroup +/// deeper than [`DEEP_THREADGROUP`] run as one row, and no whole subgroups (the flattened plan's +/// multiples are all its tier's, [`partial`]). The variant is of the `Spec` form, and a kernel of +/// a flattened tier has no compute derivatives (`lower::dispatch::plan_kernel` refuses them +/// there), so it declares `SpecKey::DeepThreadgroup`, as its own plan reads it. The variant's /// scratch only its own plan knows. fn emulated_rules(rules: &LocalRules) -> LocalRules { LocalRules { flat: false, + deep: true, multiples: Vec::new(), required_subgroup_size: None, stage_flags: vk::PipelineShaderStageCreateFlags::empty(), diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index e5b75a60a..8b1dbb00a 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -95,7 +95,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 556; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2541; +pub const RUNTIME_VERSION: u32 = 2542; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles diff --git a/crates/metal2vulkan/src/tests/engine_simd_emulated.rs b/crates/metal2vulkan/src/tests/engine_simd_emulated.rs index fb8511189..997d9f876 100644 --- a/crates/metal2vulkan/src/tests/engine_simd_emulated.rs +++ b/crates/metal2vulkan/src/tests/engine_simd_emulated.rs @@ -2086,11 +2086,12 @@ fn the_emulated_tiers_limits_stay_refused_for_partial_simdgroups() { } /// A `dispatchThreads` grid whose edge is a partial simdgroup runs whole on the variant, so its -/// interior is checked against the variant's limits, not the flattened tier's. On -/// nvidia-rtx50-linux (`Direct`, z at most 64), an interior of `[1, 1, 96]`, which the flattened -/// pipeline runs alone, asks for no variant; a grid of `[1, 1, 100]` in threadgroups of -/// `[1, 1, 96]` is a `LocalSize` refusal up front, and one in threadgroups of `[1, 1, 64]` asks for -/// the variant, whose plan runs both regions. +/// interior is checked against the variant's limits, not the flattened tier's: those of a kernel +/// that runs a threadgroup deeper than `DEEP_THREADGROUP` as one row. On nvidia-rtx50-linux +/// (`Direct`, z at most 64), an interior of `[1, 1, 96]`, which the flattened pipeline runs alone, +/// asks for no variant, and a grid of `[1, 1, 100]` in threadgroups of `[1, 1, 96]` or `[1, 1, 64]` +/// asks for the variant, whose plan runs both regions: the flattened plan's check and the +/// variant's plan agree. #[test] fn a_mixed_dispatch_is_checked_against_the_variants_limits() { let Some(bc) = assemble("simd_ops") else { @@ -2134,20 +2135,11 @@ fn a_mixed_dispatch_is_checked_against_the_variants_limits() { }; assert_eq!(plan.dispatch_variant(&interior), Ok(VariantKey::new())); assert!(plan.instantiate_dispatch(&state, &interior).is_ok()); - match plan.dispatch_variant(&mixed(96)) { - Err(BindError::Unsupported(u)) => assert_eq!( - u.feature, - Feature::LocalSize { - axis: Some(Axis::Z) - } - ), - other => panic!("{other:?}"), + for z in [96, 64] { + let variant = plan.dispatch_variant(&mixed(z)); + let regions = (vplan.instantiate_dispatch(&state, &mixed(z))).map(|c| c.regions.len()); + assert_eq!((variant, regions), (Ok(emulated_key()), Ok(2)), "z {z}"); } - assert_eq!(plan.dispatch_variant(&mixed(64)), Ok(emulated_key())); - let c = vplan - .instantiate_dispatch(&state, &mixed(64)) - .expect("the variant runs both regions"); - assert_eq!(c.regions.len(), 2); } /// The committed moltenvk-m3 profile (32-lane subgroups that require whole subgroups).