rt: a kernel's threadgroup memory arguments come from its length constants

The device legs of the tile (m3-max-41638/41639/41640) refused every
observing kernel with Gap::KernelInput: the runtime took a kernel's
[[threadgroup(n)]] arguments from PipelineInterface::threadgroup, which the
translator leaves empty, so a TgLength specialization found no argument. The
arguments are now the TgLength constants the module specializes on, by
their index.

With threadgroup memory reaching a pipeline, a dispatch checks it: the
kernel's static part (and a predicated walk's 4-byte vote word,
RuntimeFact::PredicationVote, now accepted) plus each length set rounded up
to 16 bytes must be within maxComputeSharedMemorySize, else the new
Limit::ThreadgroupMemory, Metal's maxThreadgroupMemoryLength counterpart.

The precise kernel's test found on the 780M and the XTX one word apart
between a tile and the app's shape: a NaN/NaN division (0xffb86e83 /
0x7f9fc206) answers the first operand's payload in one pipeline and the
second's in the other. The state carries NativeNanEncoding, so two NaNs are
alike there; every other word must be equal.

README: the threadgroup memory limit, and NVIDIA 615.71.09's sign-extended
narrow clamped sum on GA10x (the planner's wording). REFUSALS.md and the API
signatures regenerated.
This commit is contained in:
Anees Iqbal committed 2026-11-17 10:46:51 -08:00
1 parent d65591bff6
commit 4ac52bf082
9 files changed
+85 -10

No files matched your search

+6
View File
@@ -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
+3
View File
@@ -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 {
+14 -2
View File
@@ -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>()
+ 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)?
}
};
+15
View File
@@ -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}")
}
+16 -3
View File
@@ -123,6 +123,9 @@ pub(crate) struct Interface {
pub(crate) threadgroups: Vec<u32>,
/// `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<u32>,
@@ -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<u32> = translated
.threadgroup
// The threadgroup memory arguments are the lengths the module specializes on, each by its
// `[[threadgroup(n)]]` index.
let threadgroups: Vec<u32> = 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,
@@ -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(),
+25 -3
View File
@@ -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];
@@ -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
Can't render this file because it is too large.
+4 -2
View File
@@ -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)`