diff --git a/crates/metal2vulkan-conformance/known-gaps-rt.tsv b/crates/metal2vulkan-conformance/known-gaps-rt.tsv index 30dada4de..0e11faa68 100644 --- a/crates/metal2vulkan-conformance/known-gaps-rt.tsv +++ b/crates/metal2vulkan-conformance/known-gaps-rt.tsv @@ -8023,3 +8023,23 @@ attribute-format-fetches NVIDIA GeForce RTX 3090 divergent case/MTLAttributeForm attribute-format-fetches NVIDIA GeForce RTX 3090 divergent case/MTLAttributeFormatHalf2/attribute0/MTLAttributeFormatHalf2 4 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. attribute-format-fetches NVIDIA GeForce RTX 3090 divergent case/MTLAttributeFormatHalf3/attribute0/MTLAttributeFormatHalf3 6 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. attribute-format-fetches NVIDIA GeForce RTX 3090 divergent case/MTLAttributeFormatHalf4/attribute0/MTLAttributeFormatHalf4 8 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 5080 Laptop GPU divergent case/MTLAttributeFormatFloatRG11B10/attribute0/MTLAttributeFormatFloatRG11B10 10 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 5080 Laptop GPU divergent case/MTLAttributeFormatHalf/attribute0/MTLAttributeFormatHalf 2 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 5080 Laptop GPU divergent case/MTLAttributeFormatHalf2/attribute0/MTLAttributeFormatHalf2 4 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 5080 Laptop GPU divergent case/MTLAttributeFormatHalf3/attribute0/MTLAttributeFormatHalf3 6 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 5080 Laptop GPU divergent case/MTLAttributeFormatHalf4/attribute0/MTLAttributeFormatHalf4 8 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 2080 divergent case/MTLAttributeFormatFloatRG11B10/attribute0/MTLAttributeFormatFloatRG11B10 10 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 2080 divergent case/MTLAttributeFormatHalf/attribute0/MTLAttributeFormatHalf 2 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 2080 divergent case/MTLAttributeFormatHalf2/attribute0/MTLAttributeFormatHalf2 4 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 2080 divergent case/MTLAttributeFormatHalf3/attribute0/MTLAttributeFormatHalf3 6 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches NVIDIA GeForce RTX 2080 divergent case/MTLAttributeFormatHalf4/attribute0/MTLAttributeFormatHalf4 8 elements differ (U32), the largest difference 4194303 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set where Metal reads the canonical NaN, at each NaN element Metal answers. Metal reads 0x7fc00000, the device 0x7fffffff at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon 780M Graphics (RADV PHOENIX) divergent case/MTLAttributeFormatFloatRG11B10/attribute0/MTLAttributeFormatFloatRG11B10 9 elements differ (U32), the largest difference 4063232 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7ffe0000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon 780M Graphics (RADV PHOENIX) divergent case/MTLAttributeFormatHalf/attribute0/MTLAttributeFormatHalf 2 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon 780M Graphics (RADV PHOENIX) divergent case/MTLAttributeFormatHalf2/attribute0/MTLAttributeFormatHalf2 4 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon 780M Graphics (RADV PHOENIX) divergent case/MTLAttributeFormatHalf3/attribute0/MTLAttributeFormatHalf3 6 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon 780M Graphics (RADV PHOENIX) divergent case/MTLAttributeFormatHalf4/attribute0/MTLAttributeFormatHalf4 8 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon RX 550 / 550 Series (RADV POLARIS12) divergent case/MTLAttributeFormatFloatRG11B10/attribute0/MTLAttributeFormatFloatRG11B10 9 elements differ (U32), the largest difference 4063232 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7ffe0000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon RX 550 / 550 Series (RADV POLARIS12) divergent case/MTLAttributeFormatHalf/attribute0/MTLAttributeFormatHalf 2 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon RX 550 / 550 Series (RADV POLARIS12) divergent case/MTLAttributeFormatHalf2/attribute0/MTLAttributeFormatHalf2 4 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon RX 550 / 550 Series (RADV POLARIS12) divergent case/MTLAttributeFormatHalf3/attribute0/MTLAttributeFormatHalf3 6 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. +attribute-format-fetches AMD Radeon RX 550 / 550 Series (RADV POLARIS12) divergent case/MTLAttributeFormatHalf4/attribute0/MTLAttributeFormatHalf4 8 elements differ (U32), the largest difference 2147491840 The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal reads the canonical NaN. Metal reads 0x7fc00000, the device 0x7fc02000 at the first differing element. Note `device-nan-encoding`. diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index c0791f5c6..21ab5c70f 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2275; +pub const SEMANTICS_VERSION: u32 = 2276; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/arith/narrow.rs b/crates/metal2vulkan/src/lower/arith/narrow.rs index baa4409ef..8cf03eab9 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -538,10 +538,11 @@ pub fn to_bits(fb: &mut FunctionBuilder<'_>, f: Form, x: Value) -> BResult, fmt: NarrowFmt, @@ -559,11 +560,79 @@ fn known_bits( return Ok(Some(k)); } if f.lanes > 1 { + if let Some(b) = constructed_bits(fb, fmt, f, x)? { + return Ok(Some(b)); + } return inserted_bits(fb, fmt, f, x); } Ok(None) } +/// Where one part of a vector construct gets its bits from, for [`constructed_bits`]. +enum PartBits { + /// Noted bits, and their dirty lanes. + Noted(Value, u32), + /// A constant's lanes' bits. + Const(Vec), + /// Lane `.2` of a vector's noted bits (dirty in the lanes `.1` has a bit for). + Of(Value, u32, u32), +} + +/// The bits of vector `x` (form `f`) built by one `OpCompositeConstruct` of parts +/// (`FunctionBuilder::vector_parts`) whose bits are each known (noted, a constant's, or a lane of +/// a vector's noted bits): the construct of the parts' bits, so a vector the lowering joins from +/// decoded lanes (a pulled attribute's fetch, a packed float's unpack, a chunked bitcast's lanes) +/// keeps every lane's bits. Each lane then holds the bits of the lane `x` holds, so they are +/// `x`'s bits. Checked before anything is emitted; `None` (nothing emitted) when a part's bits are +/// unknown. +fn constructed_bits( + fb: &mut FunctionBuilder<'_>, + fmt: NarrowFmt, + f: Form, + x: Value, +) -> BResult> { + let Some(parts) = fb.vector_parts(x) else { + return Ok(None); + }; + let mut known = Vec::with_capacity(parts.len()); + for mut p in parts { + while let Some(member) = fb.member_source(p) { + p = member; + } + let lanes = vector_lanes(fb, p)?.unwrap_or(1); + let part = Form { lanes, ..f }; + let bits = if let Some((b, d)) = fb.bits_of(fmt, p) { + PartBits::Noted(b, d) + } else if let Some(k) = constant_lanes(fb, part, p)? { + PartBits::Const(k) + } else if let (1, Some((w, j))) = (lanes, fb.lane_of(p)) { + match fb.bits_of(fmt, w) { + Some((wb, wd)) => PartBits::Of(wb, wd, j), + None => return Ok(None), + } + } else { + return Ok(None); + }; + known.push((bits, part)); + } + let (mut part_bits, mut dirty, mut at) = (Vec::with_capacity(known.len()), 0u32, 0u32); + for (bits, part) in known { + let (b, d) = match bits { + PartBits::Noted(b, d) => (b, d), + PartBits::Const(k) => (materialize(fb, part, &k)?, 0), + PartBits::Of(wb, wd, j) => (fb.composite_extract(wb, &[j])?, wd >> j & 1), + }; + let own = u32::MAX >> (32 - u32::from(part.lanes).clamp(1, 32)); + dirty |= (d & own).checked_shl(at).unwrap_or(0); + at += u32::from(part.lanes); + part_bits.push(b); + } + let t = ty(fb, u32_form(f))?; + let rb = fb.composite_construct(t, &part_bits)?; + fb.note_bits(fmt, x, rb, dirty, false); + clean(fb, fmt, f, x, rb, dirty).map(Some) +} + /// `bits` (the noted bits of `x`, dirty in the lanes `dirty` has a bit for) cleared above each /// lane's 16 bits, noted for `x` as clean in the open block. fn clean( diff --git a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs index 51581b0f1..bdf97fceb 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs @@ -215,6 +215,64 @@ fn narrow_members_move_as_bits_through_structs() { } } +/// A narrow vector the builder constructs of decoded lanes (`FunctionBuilder::vector_parts`), as a +/// pulled attribute's fetch, a packed float's unpack and a chunked bitcast join them, is those +/// lanes with their bits (`narrow::to_bits`): stored unchanged, every lane keeps its bits, a NaN's +/// sign and payload and a signalling NaN included, through a construct of four decoded lanes, of +/// two beside the constants `0` and `1.0` (a fetch's missing lanes), and through the parent's +/// own load of a vector, which constructs it from its lanes. An encode of the device's widening +/// would answer the interpreter's canonical NaN (`0x7E00`) instead. +#[test] +fn a_constructed_narrow_vector_keeps_its_lanes_bits() { + let codes = [ + 0x7E01u64, 0xFE01, 0x7C01, 0xFDFF, 0x7C00, 0x8000, 0x0001, 0x3C00, + ]; + let quads: Vec = (0..codes.len()) + .map(|i| (0..4).map(|k| vec![codes[(i + k) % codes.len()]]).collect()) + .collect(); + let four = |fb: &mut FunctionBuilder<'_>, x: &[Value]| -> BResult { + let t = ops::ty(fb, vec(half(), 4))?; + fb.composite_construct(t, x) + }; + check_accept( + "four decoded lanes", + &[half(); 4], + vec(half(), 4), + &four, + &quads, + &|c| Some(c.iter().map(|lane| lane[0]).collect()), + &|g, w| g == w, + ); + let padded = |fb: &mut FunctionBuilder<'_>, x: &[Value]| -> BResult { + let zero = ops::fsplat(fb, float(32), 0.0)?; + let one = ops::fsplat(fb, float(32), 1.0)?; + let t = ops::ty(fb, vec(half(), 4))?; + fb.composite_construct(t, &[x[0], x[1], zero, one]) + }; + check_accept( + "two decoded lanes and the constants 0 and 1.0", + &[half(); 2], + vec(half(), 4), + &padded, + &quads.iter().map(|c| c[..2].to_vec()).collect::>(), + &|c| Some(vec![c[0][0], c[1][0], 0, 0x3C00]), + &|g, w| g == w, + ); + let loaded: Vec = quads + .iter() + .map(|c| vec![c.iter().map(|lane| lane[0]).collect()]) + .collect(); + check_accept( + "a loaded vector", + &[vec(half(), 4)], + vec(half(), 4), + &|_, x| Ok(x[0]), + &loaded, + &|c| Some(c[0].clone()), + &|g, w| g == w, + ); +} + /// One half round trip through its format bits, `to_bits` then `from_bits` (their types and /// constants shared with the kernel's own load and store), is the device's narrowing and widening /// alone: at most 40 words. A load also decodes diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index febd255e5..7a9812de2 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1594,7 +1594,8 @@ pub enum PrecisionNote { /// every `half` NaN to `0x7fc00000` and moves one it does not widen with its bits, so a `half` /// NaN the module moves as a value (through the program's select, phi or sign operation) may /// leave with the device's bits; a `half` stored back unchanged keeps its bits, through a - /// containment check or the choice among a pointer's candidate buffers too. And by every module + /// vector the module joins from read lanes, a containment check or the choice among a + /// pointer's candidate buffers too. And by every module /// that encodes a `half` to its bits where they are not the bits it was read from (a store, a /// reinterpretation, a texel's bits), on every device: the encode is the device's narrowing /// (an `OpFConvert` to 16 bits with `shaderFloat16`, `PackHalf2x16` without it), and a NaN diff --git a/crates/metal2vulkan/src/spirv/builder/function.rs b/crates/metal2vulkan/src/spirv/builder/function.rs index 03f55a9a0..1707e1094 100644 --- a/crates/metal2vulkan/src/spirv/builder/function.rs +++ b/crates/metal2vulkan/src/spirv/builder/function.rs @@ -228,6 +228,22 @@ impl<'m> FunctionBuilder<'m> { self.m.inserts.get(&y).copied() } + /// Record that vector `y`, of `f32` lanes, is the construct of `parts` + /// ([`FunctionBuilder::vector_parts`]). + pub(super) fn note_vector_parts(&mut self, y: Value, parts: &[Value]) { + self.m.vector_part_bytes = self + .m + .vector_part_bytes + .saturating_add(std::mem::size_of_val(parts)); + self.m.vector_parts.insert(y, parts.into()); + } + + /// The parts, scalars or vectors, of `f32` vector `y` where the builder made it by + /// `OpCompositeConstruct`, in lane order: each dominates `y`. + pub fn vector_parts(&self, y: Value) -> Option> { + self.m.vector_parts.get(&y).map(|parts| parts.to_vec()) + } + /// Record how struct `y` holds its members, or which member of a struct it reads /// ([`FunctionBuilder::member_source`]). pub(super) fn note_struct(&mut self, y: Value, op: StructOp) { diff --git a/crates/metal2vulkan/src/spirv/builder/module.rs b/crates/metal2vulkan/src/spirv/builder/module.rs index ae07e0f8d..1be26c91e 100644 --- a/crates/metal2vulkan/src/spirv/builder/module.rs +++ b/crates/metal2vulkan/src/spirv/builder/module.rs @@ -111,6 +111,13 @@ pub struct ModuleBuilder { /// The vector, lane value, and lane index of each narrow lane insert the lowering noted /// (`FunctionBuilder::note_insert`). pub(super) inserts: HashMap, + /// The parts of each `OpCompositeConstruct` of a vector of `f32` lanes, the type a narrow + /// float is held in, by result (`FunctionBuilder::vector_parts`), so a narrow vector's bits + /// can be read from its parts' (`lower::arith::narrow::to_bits`). Nothing here changes the IR. + pub(super) vector_parts: HashMap>, + /// The heap bytes of [`ModuleBuilder::vector_parts`]' part lists, for + /// [`ModuleBuilder::footprint`]. + pub(super) vector_part_bytes: usize, /// The value and constant each 32-bit unsigned sum of a value and a constant the lowering /// noted adds (`FunctionBuilder::note_sum`). Nothing here changes the IR. pub(super) sums: HashMap, @@ -325,6 +332,8 @@ impl ModuleBuilder { + map_bytes(&self.bits) + map_bytes(&self.lanes) + map_bytes(&self.inserts) + + map_bytes(&self.vector_parts) + + self.vector_part_bytes + map_bytes(&self.sums) + map_bytes(&self.structs) + self.struct_bytes diff --git a/crates/metal2vulkan/src/spirv/builder/ops/composite.rs b/crates/metal2vulkan/src/spirv/builder/ops/composite.rs index e9dc9e620..1c50d11ea 100644 --- a/crates/metal2vulkan/src/spirv/builder/ops/composite.rs +++ b/crates/metal2vulkan/src/spirv/builder/ops/composite.rs @@ -23,6 +23,12 @@ impl FunctionBuilder<'_> { } let ty_def = self.mb().types().get(ty)?.clone(); let is_struct = matches!(ty_def, Type::Struct { .. }); + let f32_lanes = match ty_def { + Type::Vector { elem, .. } => { + matches!(self.mb().types().get(elem)?, Type::Float { width: 32 }) + } + _ => false, + }; let expected: Vec = match ty_def { Type::Vector { elem, len } => { if parts.len() < 2 { @@ -72,6 +78,8 @@ impl FunctionBuilder<'_> { )?; if is_struct { self.note_struct(y, StructOp::Construct(parts.into())); + } else if f32_lanes { + self.note_vector_parts(y, parts); } Ok(y) } diff --git a/crates/metal2vulkan/src/tests/engine_kernel_stage_in.rs b/crates/metal2vulkan/src/tests/engine_kernel_stage_in.rs index 6d9411212..86cf4ddee 100644 --- a/crates/metal2vulkan/src/tests/engine_kernel_stage_in.rs +++ b/crates/metal2vulkan/src/tests/engine_kernel_stage_in.rs @@ -588,3 +588,95 @@ fn a_step_by_thread_position_records_the_region() { ); } } + +/// A `half` attribute read into the kernel's `half2` and reinterpreted unchanged keeps the field's +/// own bits, as Metal's fetch does: `stage_in_fetch`'s `as_type(in.h.x)` and `.y` reuse +/// the bits the fetch widened, through the vector the fetch constructs of its lanes +/// (`narrow::to_bits`), so its module encodes no `half` (no `OpFConvert` to a 16-bit float, no +/// `PackHalf2x16`) on any committed profile, with the attribute's format `Half2` (both lanes read) +/// or `Half` (the second lane missing, `0`). An encode there narrows the device's widening of a +/// NaN field to the device's NaN: the RTX 5080 Laptop read `0x7E01` and `0xFE01` as `0x7FFF` +/// (job m3-max-68835). +#[test] +fn a_half_attribute_reinterpreted_unchanged_encodes_no_half() { + use crate::lower::arith::narrow::quantize_form; + let Some(bc) = fixture() else { return }; + let (mut compiled, mut encoding) = (Vec::new(), Vec::new()); + for format in [AttributeFormat::Half2, AttributeFormat::Half] { + let mut d = fetch_descriptor( + (StepFunction::ThreadPositionInGridX, 1), + (StepFunction::ThreadPositionInGridY, 1), + IndexType::UInt16, + ); + d.attributes + .insert(3, StageInputAttributeDescriptor::new(format, 16, 1)); + for (stem, p) in committed_profiles().expect("profiles") { + let consts = FunctionConstantValues::defaults(); + let a = match compile_bitcode( + &bc, + EntryRef::Name("stage_in_fetch"), + &consts, + &p, + &inputs(&d), + &mut meter(), + SPIRV_WORDS, + ) { + Ok(a) => a, + Err(Error::Unsupported(u)) if u.needs != Needs::NotImplemented => continue, + Err(e) => panic!("{stem} {format:?}: {e}"), + }; + let encodes = half_encodes(&a.module.words); + if encodes > 0 { + encoding.push((stem.clone(), format, encodes)); + } + compiled.push((stem, format, quantize_form(&p))); + } + } + // Each `half` form of the profiles is among the modules: with `shaderFloat16` under the float + // controls and without them, and without it. + let forms: std::collections::BTreeSet<_> = + compiled.iter().map(|(_, _, q)| format!("{q:?}")).collect(); + assert_eq!(forms.len(), 3, "a profile of each form: {compiled:?}"); + assert!( + encoding.is_empty(), + "{} of {} modules encode a half: {encoding:?}", + encoding.len(), + compiled.len() + ); +} + +/// The module's encodes of a `half` to its bits by the device: its `OpFConvert`s to a 16-bit +/// float (or a vector of them) and its `GLSL.std.450 PackHalf2x16`s. +fn half_encodes(words: &[u32]) -> usize { + use crate::spirv::ir::GlslOp; + use crate::spirv_binary::{parse_bytes, Operand}; + use crate::spv::Op; + let bytes: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let parsed = parse_bytes(&bytes).expect("parse"); + let id = |o: Option<&Operand>| match o { + Some(Operand::Word(w) | Operand::IdRef(w)) => Some(*w), + _ => None, + }; + let pack = GlslOp::PackHalf2x16.to_spirv() as u32; + let mut halves = std::collections::HashSet::new(); + let mut encodes = 0; + for i in &parsed.instructions { + match i.opcode { + Op::TypeFloat if id(i.operands.first()) == Some(16) => halves.extend(i.result_id), + Op::TypeVector if id(i.operands.first()).is_some_and(|e| halves.contains(&e)) => { + halves.extend(i.result_id) + } + Op::FConvert if i.result_type.is_some_and(|t| halves.contains(&t)) => encodes += 1, + Op::ExtInst + if matches!( + i.operands.get(1), + Some(Operand::LiteralExtInstInteger(n)) if *n == pack + ) => + { + encodes += 1 + } + _ => {} + } + } + encodes +} diff --git a/docs/API.md b/docs/API.md index a0c34c2b0..d1d9a3bd4 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2202,8 +2202,10 @@ bound. the device's, where Metal widens every `half` NaN to `0x7fc00000` and moves one it does not widen with its bits. And wherever a `half` is encoded to its bits (a store, a reinterpretation, a texel's bits) where they are not the bits it was read from (a `half` moved unchanged from its - read keeps them, through a struct's member, a containment check and the choice among a - pointer's candidate buffers too), on every device, since the encode is + read keeps them, through a struct's member, a vector the translation joins from read lanes (a + stage-in or pulled vertex attribute's fetch, a packed float's unpack, a bitcast's lanes), a + containment check and the choice among a pointer's candidate buffers too), on every device, + since the encode is the device's narrowing (its conversion with `shaderFloat16`, `PackHalf2x16` without): a NaN's bits the device's, where Metal keeps a `half` NaN's sign and payload. And wherever the entry reaches a `float` constant whose bits are a signalling NaN (every exponent bit set, the quiet bit diff --git a/docs/CONFORMANCE-COVERAGE.md b/docs/CONFORMANCE-COVERAGE.md index ad0b9ae18..aed2e6cd2 100644 --- a/docs/CONFORMANCE-COVERAGE.md +++ b/docs/CONFORMANCE-COVERAGE.md @@ -42,7 +42,7 @@ Its limits, measured rather than asserted: | committed recordings | 1325 | | distinct SDK items the Metal leg reaches, of any status | 808 | | of those, reached only where this cannot name the family | 5 | -| ledger rows (a case whose gap is recorded) | 7968 | +| ledger rows (a case whose gap is recorded) | 7988 | | declared crossings of the option space (pairs put to Metal at once) | 26 | | declared crossings that are no pair of it (a boolean or numeric field, or one type in two slots) | 30 | @@ -10032,4 +10032,24 @@ A case whose difference from Metal is recorded rather than compared away. The le | `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | | `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | | `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device reads with every payload bit set… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… | +| `attribute-format-fetches` | The device's own answer (rule 1): a NaN of a `half` or `RG11B10` stage-in attribute widened by the shader's fetch on the device keeps its payload where Metal re… |