diff --git a/crates/metal2vulkan-rt/README.md b/crates/metal2vulkan-rt/README.md index 04b51d25f..6a0c7857f 100644 --- a/crates/metal2vulkan-rt/README.md +++ b/crates/metal2vulkan-rt/README.md @@ -206,6 +206,9 @@ that touch blocks first. dispatch past it is split into dispatches of at most that many, each told its first threadgroup. - `setBytes` takes up to 64 KiB, one ring chunk (`Limit::BytesLength`). +- A dispatch's threadgroup memory, the kernel's own and the lengths set, each rounded up to 16 + bytes, is at most the device's `maxComputeSharedMemorySize` (`Limit::ThreadgroupMemory`), where + Metal's is its `maxThreadgroupMemoryLength`. ## Compute encoders @@ -316,6 +319,9 @@ on disk; the `VkPipelineCache` still serves the device's own pipelines. is dispatched at. - A dispatch of a size other than the descriptor's `requiredThreadsPerThreadgroup`, which Metal's validation stops, runs at the size it gives. +- NVIDIA 615.71.09 on GA10x gives sign-extended bits for a mask over a signed clamp, to exactly N + bits' range, of a sum or difference of two values sign-extended from N bits; not reproduced on + 620.70.06. ## One path for every device diff --git a/crates/metal2vulkan-rt/src/device.rs b/crates/metal2vulkan-rt/src/device.rs index 853e77554..407ab8c67 100644 --- a/crates/metal2vulkan-rt/src/device.rs +++ b/crates/metal2vulkan-rt/src/device.rs @@ -360,6 +360,7 @@ impl Device { let limits = ComputeLimits { max_group_count: limits.max_compute_work_group_count, max_group_size: limits.max_compute_work_group_size, + max_shared_memory: limits.max_compute_shared_memory_size, }; let max_block_len = vulkan11 .max_memory_allocation_size @@ -828,6 +829,8 @@ pub(crate) struct ComputeLimits { pub(crate) max_group_count: [u32; 3], /// `maxComputeWorkGroupSize`. pub(crate) max_group_size: [u32; 3], + /// `maxComputeSharedMemorySize`. + pub(crate) max_shared_memory: u32, } impl Core { diff --git a/crates/metal2vulkan-rt/src/encoder.rs b/crates/metal2vulkan-rt/src/encoder.rs index c513dd9f6..2b9b9193b 100644 --- a/crates/metal2vulkan-rt/src/encoder.rs +++ b/crates/metal2vulkan-rt/src/encoder.rs @@ -328,8 +328,8 @@ impl ComputeCommandEncoder { /// each reading Metal's whole grid from its region. /// /// Err: [`Refusal::NoPipeline`]; [`Refusal::ThreadgroupSize`]; [`Limit::ThreadgroupShape`]; - /// [`Gap::PartialSimdgroups`] for a threadgroup that is not whole SIMD groups of a kernel - /// whose tier needs them; + /// [`Limit::ThreadgroupMemory`]; [`Gap::PartialSimdgroups`] for a threadgroup that is not + /// whole SIMD groups of a kernel whose tier needs them; /// [`Error::Vulkan`] where the pipeline's compile, the ring or recording fails. pub fn dispatch_threadgroups( &mut self, @@ -521,6 +521,18 @@ impl ComputeCommandEncoder { .map(|&index| self.threadgroup_length(index)) .collect(), }; + let length = variant + .threadgroup_lengths + .iter() + .map(|&length| u64::from(length).next_multiple_of(16)) + .sum::() + + u64::from(kernel.interface.static_threadgroup_bytes); + if length > u64::from(limits.max_shared_memory) { + return Err(Error::Limit(Limit::ThreadgroupMemory { + length, + max: limits.max_shared_memory, + })); + } kernel.pipeline(&core, &variant)? } }; diff --git a/crates/metal2vulkan-rt/src/error.rs b/crates/metal2vulkan-rt/src/error.rs index 06f43038e..5bb3306e8 100644 --- a/crates/metal2vulkan-rt/src/error.rs +++ b/crates/metal2vulkan-rt/src/error.rs @@ -222,6 +222,17 @@ pub enum Limit { /// The device's most. max: [u32; 3], }, + /// A dispatch whose threadgroup memory, the kernel's own and the lengths set (each rounded up + /// to 16 bytes), is past the device's `maxComputeSharedMemorySize`. Metal's counterpart is + /// `maxThreadgroupMemoryLength`, past which its validation stops the dispatch. + /// + /// Basis: device (the device's `maxComputeSharedMemorySize`). + ThreadgroupMemory { + /// The threadgroup memory the dispatch takes, in bytes. + length: u64, + /// The device's most. + max: u32, + }, /// `setBytes` data longer than one parameter ring chunk. Metal documents the call for data /// under 4 KB. /// @@ -333,6 +344,10 @@ impl fmt::Display for Limit { Limit::ThreadgroupShape { size, max } => { write!(f, "a threadgroup of {size:?} is past the device's {max:?}") } + Limit::ThreadgroupMemory { length, max } => write!( + f, + "{length} bytes of threadgroup memory are past the device's {max}" + ), Limit::BytesLength { len, max } => { write!(f, "{len} bytes are past the most one call takes, {max}") } diff --git a/crates/metal2vulkan-rt/src/pipeline.rs b/crates/metal2vulkan-rt/src/pipeline.rs index de376fcea..9d7635895 100644 --- a/crates/metal2vulkan-rt/src/pipeline.rs +++ b/crates/metal2vulkan-rt/src/pipeline.rs @@ -123,6 +123,9 @@ pub(crate) struct Interface { pub(crate) threadgroups: Vec, /// `maxTotalThreadsPerThreadgroup`. pub(crate) max_total_threads: u32, + /// The threadgroup memory every threadgroup takes beside the lengths set: Metal's static part + /// and a predicated walk's vote word. + pub(crate) static_threadgroup_bytes: u32, /// What every threadgroup's threads must be a multiple of: the subgroup size of a tier that /// runs each SIMD group as one subgroup, where the local size is flattened. pub(crate) multiples: Vec, @@ -374,9 +377,12 @@ fn interface( } // The bind-time facts the encoder supplies. let mut multiples = Vec::new(); + let mut vote = 0; for dependency in &pipeline.dependencies { match dependency.fact { RuntimeFact::LocalSizeConstraint { multiple } => multiples.push(multiple), + // The walk's vote word: 4 bytes beside the threadgroup memory. + RuntimeFact::PredicationVote => vote = 4, RuntimeFact::ThreadsPerThreadgroup | RuntimeFact::ThreadgroupMemoryLength { .. } | RuntimeFact::LocalSizeLimits @@ -444,10 +450,16 @@ fn interface( if push_len > PUSH_BYTES { return Err(gap()); } - let threadgroups: Vec = translated - .threadgroup + // The threadgroup memory arguments are the lengths the module specializes on, each by its + // `[[threadgroup(n)]]` index. + let threadgroups: Vec = stage + .specialization + .constants .iter() - .map(|argument| argument.index) + .filter_map(|constant| match constant.key { + SpecKey::TgLength(index) => Some(index), + _ => None, + }) .collect(); let mut constants = Vec::new(); for constant in &stage.specialization.constants { @@ -492,6 +504,7 @@ fn interface( constants, threadgroups, max_total_threads, + static_threadgroup_bytes: pipeline.metal_info.static_threadgroup_memory_length + vote, multiples, required_subgroup_size: stage.required_subgroup_size, stage_flags, diff --git a/crates/metal2vulkan-rt/src/tests/compute.rs b/crates/metal2vulkan-rt/src/tests/compute.rs index eff88c87f..9a21fd240 100644 --- a/crates/metal2vulkan-rt/src/tests/compute.rs +++ b/crates/metal2vulkan-rt/src/tests/compute.rs @@ -46,6 +46,7 @@ mod on_device { ], threadgroups: Vec::new(), max_total_threads: 1024, + static_threadgroup_bytes: 0, multiples: Vec::new(), required_subgroup_size: None, stage_flags: ash::vk::PipelineShaderStageCreateFlags::empty(), diff --git a/crates/metal2vulkan-rt/src/tests/translate.rs b/crates/metal2vulkan-rt/src/tests/translate.rs index 8b1c5c545..305f62be4 100644 --- a/crates/metal2vulkan-rt/src/tests/translate.rs +++ b/crates/metal2vulkan-rt/src/tests/translate.rs @@ -466,8 +466,10 @@ mod on_device { /// `a1_sign_and_division_precise`, which does not observe its threadgroup partition and /// carries no fast-math note, answers the same words in a tile as at the app's own shape (an - /// indirect dispatch's size); its fast-math twin's state carries `FastMathRewrites`, the note - /// of the rounding a device may choose per pipeline. + /// indirect dispatch's size), a NaN's bits aside: those are the device's own + /// (`NativeNanEncoding`), and a device may take either operand's payload per pipeline. Its + /// fast-math twin's state carries `FastMathRewrites`, the note of the rounding a device may + /// choose per pipeline. #[test] fn every_device_answers_a_precise_kernel_alike_in_a_tile_and_at_the_apps_shape() { let (Some(precise), Some(fast)) = ( @@ -539,7 +541,27 @@ mod on_device { words(&outs[0], 0, 4 * threads), words(&outs[1], 0, 4 * threads), ); - let first = tiled.iter().zip(&shaped).position(|(a, b)| a != b); + // Words 0 to 2 are `float`s, word 3 two `half`s; two NaNs are alike where the state + // notes NaN bits as the device's own. + let nan_bits = state + .precision_notes() + .contains(&PrecisionNote::NativeNanEncoding); + let alike = |word: usize, a: u32, b: u32| { + let float_nans = || f32::from_bits(a).is_nan() && f32::from_bits(b).is_nan(); + let half_nan = |bits: u32| bits & 0x7c00 == 0x7c00 && bits & 0x3ff != 0; + let halves_alike = || { + [0, 16].iter().all(|&at| { + let (a, b) = ((a >> at) & 0xffff, (b >> at) & 0xffff); + a == b || (half_nan(a) && half_nan(b)) + }) + }; + let nans = match word % 4 { + 3 => halves_alike(), + _ => float_nans(), + }; + a == b || (nan_bits && nans) + }; + let first = (0..tiled.len()).find(|&word| !alike(word, tiled[word], shaped[word])); if let Some(word) = first { let thread = word / 4; let pair = &inputs[1][8 * thread..8 * thread + 8]; diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 9b538e536..cd3b0c1aa 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -12772,6 +12772,7 @@ variant metal2vulkan-rt::Gap::PartialThreadgroups PartialThreadgroups { threads_ variant metal2vulkan-rt::Limit::BlockIds BlockIds variant metal2vulkan-rt::Limit::BlockLen BlockLen { len: u64, max: u64 } variant metal2vulkan-rt::Limit::BytesLength BytesLength { len: u64, max: u64 } +variant metal2vulkan-rt::Limit::ThreadgroupMemory ThreadgroupMemory { length: u64, max: u32 } variant metal2vulkan-rt::Limit::ThreadgroupShape ThreadgroupShape { size: [u32; 3], max: [u32; 3] } variant metal2vulkan-rt::MemoryClass::Private Private variant metal2vulkan-rt::MemoryClass::Shared Shared diff --git a/docs/REFUSALS.md b/docs/REFUSALS.md index 8d4802c7d..5dea162e0 100644 --- a/docs/REFUSALS.md +++ b/docs/REFUSALS.md @@ -38,7 +38,7 @@ Columns: | What a call did wrong, in detail | 174 | 157 | 0 | 0 | 0 | 0 | 0 | | Devices below the new runtime's floor | 6 | 6 | 0 | 0 | 0 | 0 | 0 | | The new runtime's `Refusal` | 11 | 11 | 0 | 0 | 0 | 0 | 0 | -| The new runtime's `Limit` | 4 | 4 | 0 | 0 | 0 | 0 | 0 | +| The new runtime's `Limit` | 5 | 5 | 0 | 0 | 0 | 0 | 0 | | The new runtime's `Gap` | 4 | 4 | 0 | 0 | 0 | 0 | 0 | ## Totals by basis @@ -56,7 +56,7 @@ Columns: | What a call did wrong, in detail | 60 | 26 | 23 | 0 | 39 | 26 | 0 | 0 | | Devices below the new runtime's floor | 0 | 0 | 0 | 6 | 0 | 0 | 0 | 0 | | The new runtime's `Refusal` | 2 | 5 | 0 | 0 | 4 | 0 | 0 | 0 | -| The new runtime's `Limit` | 0 | 0 | 0 | 2 | 2 | 0 | 0 | 0 | +| The new runtime's `Limit` | 0 | 0 | 0 | 3 | 2 | 0 | 0 | 0 | | The new runtime's `Gap` | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | ## 1. Translator and front-end refusals (`Unsupported`: a `Feature` and its `Needs`) @@ -631,6 +631,7 @@ Columns: | `BlockLen` | A block longer than one allocation and one buffer hold on the device (`Device::take_block` past `maxMemoryAllocationSize` or `maxBufferSize`) | at the device's limit | `device`: the device's `maxMemoryAllocationSize` and `maxBufferSize` | 1: `crates/metal2vulkan-rt/src/memory.rs (dedicated_len)` | `-` | | `BlockIds` | A block after every block number has been used (`Device::take_block`) | every device (a public call's own check of its arguments) | `ours`: block numbers are 32 bits | 1: `crates/metal2vulkan-rt/src/memory.rs (number)` | `-` | | `ThreadgroupShape` | A threadgroup longer on an axis than the device's `maxComputeWorkGroupSize` (the floor holds 1,024 by 1,024 by 64) | at the device's limit | `device`: the device's `maxComputeWorkGroupSize` | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | +| `ThreadgroupMemory` | A dispatch whose threadgroup memory, the kernel's own and the lengths set (each rounded up to 16 bytes), is past the device's... | at the device's limit | `device`: the device's `maxComputeSharedMemorySize` | 1: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` | `-` | | `BytesLength` | `setBytes` data longer than one parameter ring chunk | every device (a public call's own check of its arguments) | `ours`: a ring chunk is 64 KiB | 1: `crates/metal2vulkan-rt/src/encoder.rs (set_bytes)` | `-` | ## 13. The new runtime's `Gap` (`Error::Unsupported(Gap)`) @@ -1142,6 +1143,7 @@ Each refusal's sites, with the function that builds it. - `rt:Limit::BlockLen`: `crates/metal2vulkan-rt/src/memory.rs (dedicated_len)` - `rt:Limit::BlockIds`: `crates/metal2vulkan-rt/src/memory.rs (number)` - `rt:Limit::ThreadgroupShape`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` +- `rt:Limit::ThreadgroupMemory`: `crates/metal2vulkan-rt/src/encoder.rs (dispatch)` - `rt:Limit::BytesLength`: `crates/metal2vulkan-rt/src/encoder.rs (set_bytes)` - `rt:Gap::IndirectCommandBuffers`: `crates/metal2vulkan-rt/src/pipeline.rs (new_compute_pipeline_state)` - `rt:Gap::KernelInput`: `crates/metal2vulkan-rt/src/pipeline.rs (gap)`