diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index c610909b1..74abfb0cd 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 = 2333; +pub const SEMANTICS_VERSION: u32 = 2334; /// 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 3ec51aaf5..aa1c2371f 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -13,8 +13,9 @@ //! compiler may fold the pair (NVIDIA's does, even under those controls) or fuse the operation //! computing the value into the narrowing, so the module records //! `PrecisionNote::NativeHalfConversion` on every device, a static note of the rounding. Elsewhere, where the -//! profile has `shaderFloat16`, it is the device's conversion to 16 bits and its widening -//! ([`QuantizeForm::Converted`]), recording `PrecisionNote::NativeHalfConversion`; without it, the +//! profile has `shaderFloat16`, it is the device's conversion to 16 bits and its widening, each +//! decorated `NoContraction` too ([`QuantizeForm::Converted`]), recording +//! `PrecisionNote::NativeHalfConversion`; without it, the //! device's `OpQuantizeToF16` alone ([`QuantizeForm::Device`]), whose result below the smallest //! normal half may be a zero, recording `PrecisionNote::NativeHalfQuantize` on every such profile //! ([`quantizes`]); a texel's `half` takes [`round`] and keeps the NaN the device decoded @@ -169,14 +170,15 @@ pub fn native_rounding(form: QuantizeForm) -> Option { } } -/// `x` (`f32` lanes of form `wide`) rounded to `half` by the device's conversion to 16 bits, -/// `OpFConvert`, and widened back ([`QuantizeForm::Converted`]), recorded as -/// `PrecisionNote::NativeHalfConversion`. The device's compiler may fold the conversion into the -/// instruction computing `x`: Metal's compiler then rounds toward zero (`a1_half_tanh_regimes`, -/// case 27353187: 1,204 of its 16,384 halves one ulp toward zero on MoltenVK). NVIDIA's compiler -/// (RTX 3090) drops the pair, and so does lavapipe's where measured, so there the value is not -/// rounded at all (the note's stated bound), and the store's conversion to bits rounds it -/// ([`converted_bits`]). +/// `x` (`f32` lanes of form `wide`) rounded to `half` by the device's conversion to 16 bits and +/// its widening, each an `OpFConvert` decorated `NoContraction` as [`controlled`]'s are and for +/// the same reason ([`QuantizeForm::Converted`]), recorded as `PrecisionNote::NativeHalfConversion`: +/// undecorated, Mesa's compiler folds the pair and hands `x` on unrounded (KosmicKrisp on an Apple +/// M3 Max). The device's compiler may fold the conversion into the instruction computing `x`: +/// Metal's compiler then rounds toward zero (`a1_half_tanh_regimes`, case 27353187: 1,204 of its +/// 16,384 halves one ulp toward zero on MoltenVK). A driver whose compiler folds the decorated pair +/// anyway leaves the value not rounded at all (the note's stated bound), and the store's conversion +/// to bits rounds it ([`converted_bits`]). fn converted(fb: &mut FunctionBuilder<'_>, wide: Form, x: Value) -> BResult { let e = fb.module().ty(Type::Float { width: 16 })?; let h = match wide.lanes { @@ -184,8 +186,8 @@ fn converted(fb: &mut FunctionBuilder<'_>, wide: Form, x: Value) -> BResult fb.module().ty(Type::Vector { elem: e, len })?, }; let w = ty(fb, wide)?; - let n = fb.convert(ConvOp::FConvert, x, h)?; - fb.convert(ConvOp::FConvert, n, w) + let n = fb.convert_no_contraction(x, h)?; + fb.convert_no_contraction(n, w) } /// `x` (`f32` lanes of form `wide`) rounded to `half` by the device's conversion to 16 bits and @@ -193,7 +195,13 @@ fn converted(fb: &mut FunctionBuilder<'_>, wide: Form, x: Value) -> BResult, wide: Form, x: Value) -> BResult { let e = fb.module().ty(Type::Float { width: 16 })?; let h = match wide.lanes { @@ -905,9 +913,9 @@ fn encode(fb: &mut FunctionBuilder<'_>, f: Form, x: Value) -> BResult { /// preserved"), where Metal moves a `half` NaN with its bits, and a denormal, which the device may /// flush to the zero of its sign without `DenormPreserve` for 16 bits: kept under the float /// controls ([`QuantizeForm::Controlled`], [`controlled_modes`]), and elsewhere within `PrecisionNote::NativeHalfConversion`'s bound; the module records -/// `PrecisionNote::NativeNanEncoding` ([`encodes_bits`]). A value that is no `half` value (under -/// native half rounding the device's compiler may fold [`converted`]'s pair and hand `x` on -/// unrounded, as NVIDIA's and lavapipe's do) takes the device's rounding, within +/// `PrecisionNote::NativeNanEncoding` ([`encodes_bits`]). A value that is no `half` value (a +/// driver's compiler may fold a decorated pair of [`converted`]'s or [`controlled`]'s and hand `x` +/// on unrounded, as NVIDIA's does [`controlled`]'s) takes the device's rounding, within /// `PrecisionNote::NativeHalfConversion`'s bound, which that module records. fn converted_bits(fb: &mut FunctionBuilder<'_>, f: Form, x: Value) -> BResult { let e = fb.module().ty(Type::Float { width: 16 })?; diff --git a/crates/metal2vulkan/src/spirv/builder/module.rs b/crates/metal2vulkan/src/spirv/builder/module.rs index c5b252ce5..19c0fea9f 100644 --- a/crates/metal2vulkan/src/spirv/builder/module.rs +++ b/crates/metal2vulkan/src/spirv/builder/module.rs @@ -177,8 +177,8 @@ pub enum QuantizeForm { /// below the smallest normal half may be a zero. #[default] Device, - /// No quantize: `OpFConvert` to a 16-bit float and back, on a profile with `shaderFloat16` - /// whose float controls do not make the conversion Metal's. + /// No quantize: `OpFConvert` to a 16-bit float and back, each decorated `NoContraction`, on a + /// profile with `shaderFloat16` whose float controls do not make the conversion Metal's. Converted, /// No quantize: `OpFConvert` to a 16-bit float and back, each decorated `NoContraction`, in /// an entry that declares `RoundingModeRTE`, `DenormPreserve` and `SignedZeroInfNanPreserve` diff --git a/docs/API.md b/docs/API.md index 4326e6102..8de067eb3 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2402,7 +2402,12 @@ category, and says what makes an operation carry the note and what may differ. E Without the float controls that make that conversion Metal's `half(float)` (`RoundingModeRTE`, `DenormPreserve` and `SignedZeroInfNanPreserve` for 16 bits; on a device without 16-bit `DenormPreserve`, the other two, with `metal2vulkan-degrade-device-half-denormals`) the conversion - is the device's own. + is the device's own. The conversion and its widening are each decorated `NoContraction` with or + without those controls: Vulkan assumes `AllowTransform` on an operation without it ("Precision + and Operation of SPIR-V Instructions"), which reaches a conversion though the SPIR-V + specification words `NoContraction` for an arithmetic instruction, and Mesa's compiler folds an + undecorated pair into the value it started from. A driver that folds a decorated pair leaves the + device's own answer, within this note. Under them it is Metal's by the Vulkan spec, but a driver's compiler may fold the conversion's round trip into the value it started from (NVIDIA's does) or fuse the `float` addition, subtraction, multiply or fused multiply-add computing the value into the narrowing and round its