diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 358250496..441dd27fa 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2073,15 +2073,17 @@ degradations! { "the result is Metal's count multiplied by the colors the draw writes; a `Boolean` \ result is Metal's"; /// A device whose float functions are not Metal's own (not MoltenVK, - /// `DriverDeviation::MetalFloatFunctions`): Metal flushes a `float` product or fused - /// multiply-add whose exact value lies below `2^-126` to the zero of its sign, deciding - /// tininess before rounding, and the device rounds before it decides (measured on RADV, - /// ANV, NVIDIA and lavapipe, whether they flush denormals or keep them), so the device's - /// multiply and fused multiply-add answer `2^-126` there (`device/float_tininess.rs`). + /// `DriverDeviation::MetalFloatFunctions`): Metal flushes a `float` product, fused + /// multiply-add or `ldexp` whose exact value lies below `2^-126` to the zero of its sign, + /// deciding tininess before rounding, and the device rounds before it decides (measured on + /// RADV, ANV, NVIDIA and lavapipe, whether they flush denormals or keep them), so the + /// device's multiply and fused multiply-add answer `2^-126` there, and so does `ldexp`, + /// whose result below `2^-126` is the device's product (`device/float_tininess.rs`). /// Announced once, as the device is made; the payload names the device. FloatTininessAfterRounding = "float-tininess-after-rounding", Precision, - "a `float` product or fused multiply-add whose exact value lies within half a unit \ - below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its sign"; + "a `float` product, fused multiply-add or `ldexp` whose exact value lies within half a \ + unit below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its \ + sign"; /// An entry of the device's shader cache directory was not written: the directory, a /// temporary file in it or the rename over the entry failed, on the device's cache thread /// or on the thread that handed the entry over past the queue's bound diff --git a/crates/metal2vulkan-runtime/src/device/float_tininess.rs b/crates/metal2vulkan-runtime/src/device/float_tininess.rs index 2a090da24..6ab64cd53 100644 --- a/crates/metal2vulkan-runtime/src/device/float_tininess.rs +++ b/crates/metal2vulkan-runtime/src/device/float_tininess.rs @@ -1,10 +1,11 @@ //! `float-tininess-after-rounding`: where the device's own `float` multiply and fused multiply-add -//! answer for Metal's. Metal flushes a `float` product or fused multiply-add whose exact value lies -//! below `2^-126` to the zero of its sign, deciding tininess before rounding; every Vulkan device -//! measured off MoltenVK (RADV, ANV, NVIDIA and lavapipe, whether they flush denormals or keep -//! them) rounds before it decides, so an exact value within half a unit below `2^-126` answers -//! `2^-126`. The translation takes the device's, which every pipeline of a `float` multiply may -//! meet, so the device announces it once, as it is made, rather than each pipeline. +//! answer for Metal's. Metal flushes a `float` product, fused multiply-add or `ldexp` whose exact +//! value lies below `2^-126` to the zero of its sign, deciding tininess before rounding; every +//! Vulkan device measured off MoltenVK (RADV, ANV, NVIDIA and lavapipe, whether they flush +//! denormals or keep them) rounds before it decides, so an exact value within half a unit below +//! `2^-126` answers `2^-126`. The translation takes the device's, which every pipeline of a `float` +//! multiply may meet, and a `float` `ldexp`'s result below `2^-126` is the device's product, so the +//! device announces it once, as it is made, rather than each pipeline. use crate::degradation::Degradation; use metal2vulkan::error::DriverDeviation; diff --git a/crates/metal2vulkan-validation/runtime-sources.sha256 b/crates/metal2vulkan-validation/runtime-sources.sha256 index b48a93f6b..9af582a46 100644 --- a/crates/metal2vulkan-validation/runtime-sources.sha256 +++ b/crates/metal2vulkan-validation/runtime-sources.sha256 @@ -1 +1 @@ -4625597d37a12d9a1a0a743387208dcc72f1bad5c696b899c4b837a9f64fa7f4 +994c21cf2d9e02b567ab545e9773ea6043f95d68731d354fe3d0bc2695424419 diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 3c82303fd..8aea29a35 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 = 1920; +pub const SEMANTICS_VERSION: u32 = 1922; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 478; /// 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 = 1692; +pub const RUNTIME_VERSION: u32 = 1693; /// 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/lower/arith/denormal.rs b/crates/metal2vulkan/src/lower/arith/denormal.rs index 9c417bce7..e6496e010 100644 --- a/crates/metal2vulkan/src/lower/arith/denormal.rs +++ b/crates/metal2vulkan/src/lower/arith/denormal.rs @@ -43,12 +43,17 @@ pub enum Covered { /// | `dot` | the deviation | /// | `fma`, `llvm.fmuladd` (`OpFmaKHR`) | the deviation | /// | `fast::atan2` | the deviation | +/// | `ldexp` on `float` | the deviation | /// /// `fast::atan2(y, x)` reads a denormal `y` or `x` as the zero of its sign and answers no denormal /// (an Apple M3 Max under macOS 27.0.1: every pair of 28 patterns, in entries with and without fast /// math); its quotient is the device's division, whose operands are those two, and a denormal /// quotient reaches the fast `atan`, which reads it as a zero. /// +/// `ldexp` on `float` (`math::ldexp_bits`) reads a denormal operand and rounds a result below +/// `2^-126` by the device's `float` multiply, which the deviation covers: a denormal operand times +/// `2^64`, and a normal times `2^-126`. +/// /// `trunc` and `rint` (`Trunc`, `RoundEven`) of a denormal answer the zero of its sign on every /// venue measured, as Metal does (the probes' words, both modes, all seven venues), but Vulkan /// states no sign for a flushed denormal, so only the deviation covers them. @@ -64,7 +69,8 @@ pub fn coverage(op: FloatFn) -> Option { | F::Dot | F::Fma | F::MulAdd - | F::FastAtan2 { .. } => Covered::Deviation, + | F::FastAtan2 { .. } + | F::Ldexp => Covered::Deviation, _ => return None, }) } diff --git a/crates/metal2vulkan/src/lower/arith/math.rs b/crates/metal2vulkan/src/lower/arith/math.rs index 4a8b81a57..745558917 100644 --- a/crates/metal2vulkan/src/lower/arith/math.rs +++ b/crates/metal2vulkan/src/lower/arith/math.rs @@ -139,12 +139,12 @@ fn u32_form(f: Form) -> Form { }) } -/// `ldexp(x, k)` (`x` of form `f`, `k` a held `i32` of its lanes) as Metal answers it for every -/// exponent. GLSL's `Ldexp` leaves an exponent past `+128` and a product past the format's range -/// undefined, and may flush any result of an exponent below `-126`, so a `float` -/// `ldexp(2^100, -150)` (`2^-50`) could read zero. A `float` (and a `bfloat`, held in its `f32`) -/// is computed on the container's bits ([`ldexp_bits`]; a `bfloat`'s denormals, which Metal's -/// `bfloat` environment leaves unmeasured, read and flush as a `float`'s); a `half`, held exactly +/// `ldexp(x, k)` (`x` of form `f`, `k` a held `i32` of its lanes) for every exponent, as Metal +/// answers it but for denormals, which are the device's. GLSL's `Ldexp` leaves an exponent past +/// `+128` and a product past the format's range undefined, and may flush any result of an +/// exponent below `-126`, so a `float` `ldexp(2^100, -150)` (`2^-50`) could read zero. A `float` +/// (and a `bfloat`, held in its `f32`) is computed on the container's bits and the device's +/// products ([`ldexp_bits`]; a `bfloat`'s denormals are the device's as a `float`'s); a `half`, held exactly /// in an `f32`, is /// scaled by `2^k` with `k` clamped to `[-64, 64]`, which keeps every product an exact normal /// `f32` and saturates it past `half`'s range, and the caller rounds it to `half` once. A @@ -168,41 +168,69 @@ fn ldexp(fb: &mut FunctionBuilder<'_>, f: Form, x: Value, k: Value) -> BResult, f: Form, x: Value, k: Value) -> BResult { let i = u32_form(f); let it = ty(fb, i)?; - let bits = fb.convert(ConvOp::Bitcast, x, it)?; - let sign = splat(fb, i, 0x8000_0000)?; - let sign = bin(fb, BinOp::BitwiseAnd, bits, sign)?; + let ft = ty(fb, f)?; let at = splat(fb, i, 23)?; let field = splat(fb, i, 0xFF)?; - let e = bin(fb, BinOp::ShiftRightLogical, bits, at)?; - let e = bin(fb, BinOp::BitwiseAnd, e, field)?; + let zero = splat(fb, i, 0)?; + let exponent = |fb: &mut FunctionBuilder<'_>, bits: Value| -> BResult { + let e = bin(fb, BinOp::ShiftRightLogical, bits, at)?; + bin(fb, BinOp::BitwiseAnd, e, field) + }; + let given = fb.convert(ConvOp::Bitcast, x, it)?; + let e0 = exponent(fb, given)?; let (lo, hi) = (splat(fb, i, (-512i32) as u32 as u64)?, splat(fb, i, 512)?); let k = glsl(fb, GlslOp::SClamp, &[k, lo, hi])?; + // A zero or denormal operand, normalized by the device's product (or flushed by it). + let tiny = bin(fb, BinOp::IEqual, e0, zero)?; + let up = fsplat(fb, f, 2f64.powi(64))?; + let raised = bin(fb, BinOp::FMul, x, up)?; + let source = fb.select(tiny, raised, x)?; + let sixty_four = splat(fb, i, 64)?; + let lowered = bin(fb, BinOp::ISub, k, sixty_four)?; + let k = fb.select(tiny, lowered, k)?; + let bits = fb.convert(ConvOp::Bitcast, source, it)?; + let sign = splat(fb, i, 0x8000_0000)?; + let sign = bin(fb, BinOp::BitwiseAnd, bits, sign)?; + let e = exponent(fb, bits)?; let ne = bin(fb, BinOp::IAdd, e, k)?; let keep = splat(fb, i, 0x807F_FFFF)?; let kept = bin(fb, BinOp::BitwiseAnd, bits, keep)?; let moved = bin(fb, BinOp::ShiftLeftLogical, ne, at)?; let scaled = bin(fb, BinOp::BitwiseOr, kept, moved)?; + // A result below `2^-126`: the device's product rounds it once (or flushes it). + let bias = splat(fb, i, 126)?; + let low = bin(fb, BinOp::IAdd, ne, bias)?; + let one = splat(fb, i, 1)?; + let low = glsl(fb, GlslOp::SMax, &[low, one])?; + let low = bin(fb, BinOp::ShiftLeftLogical, low, at)?; + let low = bin(fb, BinOp::BitwiseOr, kept, low)?; + let low = fb.convert(ConvOp::Bitcast, low, ft)?; + let down = fsplat(fb, f, 2f64.powi(-126))?; + let low = bin(fb, BinOp::FMul, low, down)?; + let low = fb.convert(ConvOp::Bitcast, low, it)?; + let under = bin(fb, BinOp::SLessThanEqual, ne, zero)?; + let r = fb.select(under, low, scaled)?; let inf = splat(fb, i, 0x7F80_0000)?; let inf = bin(fb, BinOp::BitwiseOr, sign, inf)?; - let zero = splat(fb, i, 0)?; - let under = bin(fb, BinOp::SLessThanEqual, ne, zero)?; - let r = fb.select(under, sign, scaled)?; let over = bin(fb, BinOp::SGreaterThanEqual, ne, field)?; let r = fb.select(over, inf, r)?; - let special = bin(fb, BinOp::IEqual, e, field)?; - let r = fb.select(special, bits, r)?; - let small = bin(fb, BinOp::IEqual, e, zero)?; - let r = fb.select(small, sign, r)?; - let ft = ty(fb, f)?; + let nothing = bin(fb, BinOp::IEqual, e, zero)?; + let r = fb.select(nothing, bits, r)?; + let special = bin(fb, BinOp::IEqual, e0, field)?; + let r = fb.select(special, given, r)?; fb.convert(ConvOp::Bitcast, r, ft) } @@ -995,8 +1023,7 @@ impl FloatFn { /// Whether the lowering answers Metal at `f32` denormal operands and results itself, whatever /// the device's denormal mode, or records the device's answer under another note, so it records - /// no `PrecisionNote::DeviceDenormals` of its own: `ldexp` (on `float`, [`ldexp_bits`]), which reads a denormal operand as the zero of - /// its sign and flushes a denormal result on the bits, `copysign`, which moves bits, and precise + /// no `PrecisionNote::DeviceDenormals` of its own: `copysign`, which moves bits, and precise /// `fmod` on `float`, the remainder's routine, which reads a denormal divisor as a zero on the /// bits and computes the remainder in integers (`libm::fmod`), or on MoltenVK Metal's own; /// `fmin`, `fmax`, `min3`, `max3`, `clamp`, `saturate` and `median3`, the device's `NMin`, @@ -1017,7 +1044,6 @@ impl FloatFn { matches!( self, F::Abs - | F::Ldexp | F::Round | F::Fmod | F::Sign @@ -1215,8 +1241,9 @@ impl FloatFn { // `log10`'s composition of the device's `log2` (`log10`): the device's `log2` and // the product, the polynomial's fused steps, and the range test. FloatFn::Log10 => 200, - // The exponent's clamp and the bits' fields, tests and selects (`ldexp_bits`). - FloatFn::Ldexp => 160, + // The exponent's clamp, the bits' fields, the device's two products, tests and + // selects (`ldexp_bits`). + FloatFn::Ldexp => 320, _ => 48, }; // Each function the call takes: the device's instruction, or precise `fmod`'s routine's diff --git a/crates/metal2vulkan/src/lower/arith/ops.rs b/crates/metal2vulkan/src/lower/arith/ops.rs index 4c2447666..98c9ff27f 100644 --- a/crates/metal2vulkan/src/lower/arith/ops.rs +++ b/crates/metal2vulkan/src/lower/arith/ops.rs @@ -758,7 +758,8 @@ fn lane(fb: &mut FunctionBuilder<'_>, f: Form, x: Value, i: u8) -> BResult, lane: Form, v: Value) -> BResult { if lane.is_narrow() { narrow::to_bits(fb, lane, v) diff --git a/crates/metal2vulkan/src/lower/arith/tests.rs b/crates/metal2vulkan/src/lower/arith/tests.rs index e5f4b509d..c3661701a 100644 --- a/crates/metal2vulkan/src/lower/arith/tests.rs +++ b/crates/metal2vulkan/src/lower/arith/tests.rs @@ -3243,12 +3243,15 @@ fn device_precision_float_functions(part: usize, of: usize) { false, ); })); - // `ldexp` answers Metal's for every exponent: past GLSL's `[-126, 128]` and past the format's - // range, a denormal operand read as zero, a result below `2^-126` flushed, a NaN kept bit - // for bit (`math::ldexp_bits`); `half` rounds the exact product once. + // `ldexp` answers for every exponent, past GLSL's `[-126, 128]` and past the format's range, + // a NaN kept bit for bit (`math::ldexp_bits`), its denormals the device's: on the interpreter, + // which keeps them, a denormal operand scales as its value and a result below `2^-126` is the + // product rounded once (Metal flushes both: `PrecisionNote::DeviceDenormals`), and one within + // half a unit below `2^-126` rounds up to `2^-126` (Metal flushes it before rounding: + // `float-tininess-after-rounding`); `half` rounds the exact product once. checks.push(Box::new(|| { let f = float(32); - let bits: [u32; 12] = [ + let bits: [u32; 13] = [ 0x3FC0_0000, // 1.5 0xBFC0_0000, // -1.5 0x7180_0000, // 2^100 @@ -3261,6 +3264,7 @@ fn device_precision_float_functions(part: usize, of: usize) { 0x8000_0000, // -0 0x7F7F_FFFF, // the greatest finite 0x4040_0000, // 3.0 + 0x3FFF_FFFF, // 2 - 2^-23: times 2^-127, half a unit below 2^-126 ]; let exps: [i32; 16] = [ 0, @@ -3287,22 +3291,22 @@ fn device_precision_float_functions(part: usize, of: usize) { .map(move |&e| vec![vec![u64::from(x)], vec![u64::from(e as u32)]]) }) .collect(); - // Metal's answer, from the exact product in `f64`. + // The answer with the device's denormals kept: the exact product in `f64` (a power of + // two's scaling, exact but where it is far below every `float`), rounded once to `f32`. let metal = |x: u32, e: i32| -> u32 { - let (sign, field) = (x & 0x8000_0000, (x >> 23) & 0xFF); - match field { - 0 => sign, + match (x >> 23) & 0xFF { 0xFF => x, _ => { let v = f64::from(f32::from_bits(x)) * 2f64.powi(e.clamp(-1000, 1000)); - match v.abs() { - a if a >= 2f64.powi(128) => sign | 0x7F80_0000, - a if a < 2f64.powi(-126) => sign, - _ => (v as f32).to_bits(), - } + (v as f32).to_bits() } } }; + // Metal's flush is not this answer: the least denormal doubled is `2^-148`, not zero. + assert_eq!(metal(0x0000_0001, 1), 0x0000_0002); + assert_eq!(metal(0x0080_0000, -1), 0x0040_0000); + // Rounded before any flush: `2^-126 - 2^-150` ties to even, `2^-126`, where Metal answers 0. + assert_eq!(metal(0x3FFF_FFFF, -127), 0x0080_0000); check_accept( "ldexp f32, every exponent", &[f, int(32)], diff --git a/crates/metal2vulkan/src/lower/fragment/snapshot.rs b/crates/metal2vulkan/src/lower/fragment/snapshot.rs index 8f2c450d0..67cc38102 100644 --- a/crates/metal2vulkan/src/lower/fragment/snapshot.rs +++ b/crates/metal2vulkan/src/lower/fragment/snapshot.rs @@ -87,8 +87,10 @@ //! back as that code. A read decodes `c` by the device's arithmetic, `c` times the `f32` nearest //! `1 / D` (a signed code's at least `-1`), within an `f32` rounding or two of `c / D` //! (`PrecisionNote::DevicePackConversion`; Metal reads `c / D` correctly rounded). -//! - `Half`: each lane's half-float code (`narrow::to_bits`, exact for every half value, a NaN's -//! payload included), in an `R16G16B16A16_UINT` image; a read widens it back as a `half` read +//! - `Half`: each lane's half-float code (`narrow::to_bits`: the device's narrowing, every half +//! value's bits but a NaN's and, where the device may flush a 16-bit denormal, a denormal's, +//! each recorded, `narrow::encodes_bits`; a code loaded unchanged keeps its bits), in an +//! `R16G16B16A16_UINT` image; a read widens it back as a `half` read //! from memory widens (`narrow::from_bits`): the device's widening on every device, every code's //! value but a NaN's bits and, where the device may flush a 16-bit denormal, a denormal, each //! recorded (`narrow::widens_bits`). diff --git a/crates/metal2vulkan/src/memory/leaf.rs b/crates/metal2vulkan/src/memory/leaf.rs index 3f1cb8bd1..6f6d28e0b 100644 --- a/crates/metal2vulkan/src/memory/leaf.rs +++ b/crates/metal2vulkan/src/memory/leaf.rs @@ -896,7 +896,8 @@ pub fn words_of(fb: &mut FunctionBuilder<'_>, leaf: &Leaf, v: Value) -> BResult< } } Held::Float(FloatFmt::Half | FloatFmt::BFloat) => { - // A narrow float is held widened: encode its 16 format bits exactly. + // A narrow float is held widened: its 16 format bits, a `half`'s by the device's + // narrowing (`narrow::to_bits`). let w = narrow::to_bits(fb, scalar, x)?; put(fb, &mut out, 2 * i, w)?; } @@ -973,7 +974,8 @@ fn vector_words( Ok(Some(match form.held { Held::Int { container: 32, .. } => placed(fb, v)?, Held::Float(FloatFmt::Half | FloatFmt::BFloat) => { - // A narrow float is held widened: one exact encode of every lane. + // A narrow float is held widened: one encode of every lane, a `half`'s by the + // device's narrowing (`narrow::to_bits`). let bits = narrow::to_bits(fb, form, v)?; placed(fb, bits)? } diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 7b31ebded..ab8a06e7e 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -613,15 +613,16 @@ pub enum PrecisionNote { /// to one, where a Vulkan device keeps or flushes them by its own float controls (this /// translation declares no `DenormFlushToZero`). Recorded where the module leaves an /// operation's denormals to a device not measured to read them as Metal does: `+ - *`, the - /// comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, `trunc` and `rint` but on a device that takes + /// comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, `trunc`, `rint` and `ldexp` on `float` (by the + /// device's products) but on a device that takes /// the measured `DriverDeviation::MetalFloatDenormals`; and on every device the divisions and /// the remainder, `floor`, `ceil` and `fneg`, the device's maths functions (`fast::fmod`'s /// fused multiply-add, `fabs`, `sign` and `fract` among them), `mix` by `FMix`, `fast::sqrt` /// by `Sqrt`, `dot` of vectors of other than two to four lanes, whose chunks' products the /// device sums where Metal flushes each one below `2^-126`, beside their own notes, and a /// conversion to or from `double`, whose reading Metal's is not measured. The operations that - /// read a denormal on its bits answer Metal on every device: `ldexp` and `copysign`; and so do - /// the functions the device answers as Metal's own. Where the minimum and maximum functions + /// read a denormal on its bits answer Metal on every device: `copysign`; and so do the + /// functions the device answers as Metal's own. Where the minimum and maximum functions /// (`llvm.minnum` and `llvm.maxnum` among them, with any flags) and `saturate` are the device's /// `NMin`, `NMax` and `NClamp`, their pick of a denormal is /// [`PrecisionNote::NativeMinMax`]'s. Validation derives no tolerance and no explanation from diff --git a/crates/metal2vulkan/src/tests/engine_arith.rs b/crates/metal2vulkan/src/tests/engine_arith.rs index 58b00b761..5f5019b1f 100644 --- a/crates/metal2vulkan/src/tests/engine_arith.rs +++ b/crates/metal2vulkan/src/tests/engine_arith.rs @@ -3226,13 +3226,13 @@ fn a_float_nan_is_the_device_s_on_every_profile() { /// Metal reads an `f32` denormal operand of `+ - *`, a comparison, `sqrt`, `floor`, `ceil` and /// `ldexp` as the zero of its sign and flushes a denormal result. The module leaves the reading to /// the device: where the profile takes its driver's measured deviation `MetalFloatDenormals` it -/// requires the fact for `+ - *`, the comparison and `sqrt`, which then read them as Metal does; +/// requires the fact for `+ - *`, the comparison, `sqrt` and `ldexp` (whose denormal operand and +/// result are the device's products, `math::ldexp_bits`), which then read them as Metal does; /// elsewhere they are the device's, noted `DeviceDenormals` (`sqrt`, the device's maths function, /// off Metal's own functions notes them beside its precision on either). `floor` and `ceil` are /// the device's -/// whatever the fact, noted: no device measured reads their operand as Metal does. `ldexp` is -/// Metal's whatever the fact, computed on the operand's bits for every exponent. Flipping the fact -/// changes the requirement and the note, never the module. The interpreter keeps denormals as +/// whatever the fact, noted: no device measured reads their operand as Metal does. Flipping the +/// fact changes the requirement and the note, never the module. The interpreter keeps denormals as /// lavapipe does. The operands are bits (`llvm.umin` of constants), so each kernel's float /// operation is its only one. #[test] @@ -3370,7 +3370,6 @@ fn float_denormals_are_the_devices_and_noted_off_the_device_fact() { let a = compile(&bitcode, kk.name, &p).unwrap_or_else(|e| panic!("{what}: {e:?}")); let (_, words) = link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{what}: L3: {e}")); - let bits = matches!(kk.name, "den_ldexp" | "den_ldexp_tiny"); let never = matches!(kk.name, "den_floor" | "den_ceil"); // The device's `Sqrt`, a maths function off Metal's own functions, records its // denormals with its precision. @@ -3380,22 +3379,17 @@ fn float_denormals_are_the_devices_and_noted_off_the_device_fact() { a.requirements .properties .contains(&crate::device::requirements::VkProperty::MetalFloatDenormals), - fact && !bits && !never, + fact && !never, "{what}" ); assert_eq!( a.precision.contains(&PrecisionNote::DeviceDenormals), - never || function || (!fact && !bits), + never || function || !fact, "{what}: {:?}", a.precision ); let got = interpret(&bitcode, kk.name, &p); - let want = if bits { - kk.want.expect("Metal's word") - } else { - *device - }; - assert_eq!(got, want, "{what}: {got:#x} != {want:#x}"); + assert_eq!(got, *device, "{what}: {got:#x} != {device:#x}"); per.push(words); } emitted.push(per); diff --git a/docs/API.md b/docs/API.md index 5e4c7a51d..af340085c 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2669,11 +2669,12 @@ bound. attachment, on ANV (`RuntimeDriverDefect::OcclusionCountPerColorOutput`). Cost: the result is Metal's count multiplied by the colors the draw writes; a `Boolean` result is Metal's. - `metal2vulkan-degrade-float-tininess-after-rounding` (precision). `FloatTininessAfterRounding`: A - device whose float functions are not Metal's own (not MoltenVK): Metal flushes a `float` product or fused - multiply-add whose exact value lies below `2^-126` to the zero of its sign, deciding tininess - before rounding, and the device rounds before it decides (measured on RADV, ANV, NVIDIA and - lavapipe, whether they flush denormals or keep them). Announced once, as the device is made. Cost: - a `float` product or fused multiply-add whose exact value lies within half a unit below `2^-126` + device whose float functions are not Metal's own (not MoltenVK): Metal flushes a `float` product, + fused multiply-add or `ldexp` whose exact value lies below `2^-126` to the zero of its sign, + deciding tininess before rounding, and the device rounds before it decides (measured on RADV, + ANV, NVIDIA and lavapipe, whether they flush denormals or keep them); a `float` `ldexp`'s result + below `2^-126` is the device's product. Announced once, as the device is made. Cost: a `float` + product, fused multiply-add or `ldexp` whose exact value lies within half a unit below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its sign. - `metal2vulkan-degrade-shader-cache-entry-not-written` (performance). `ShaderCacheEntryNotWritten`: an entry of the device's shader cache directory was not written -- @@ -2708,18 +2709,20 @@ bound. Vulkan device keeps or flushes them by its own float controls (this translation declares no `DenormFlushToZero`). Recorded where the module leaves an operation's denormals to a device not measured to read them as Metal does: `+ - *`, the comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, - `trunc` and `rint` but on a device that takes the measured + `trunc`, `rint` and `ldexp` on `float` (by the device's products) but on a device that takes the + measured `DriverDeviation::MetalFloatDenormals`; and on every device the divisions and the remainder, `floor`, `ceil` and `fneg`, the device's maths functions (`fast::fmod`'s fused multiply-add, `fabs`, `sign` and `fract` among them), `mix` by `FMix`, `fast::sqrt` by `Sqrt`, `dot` of vectors of other than two to four lanes, whose chunks' products the device sums where Metal flushes each one below `2^-126`, and a conversion to or from `double`, where Metal's reading is not measured. The operations that read a denormal on its bits answer Metal on every device: - `ldexp`, `copysign` and `atan2`; and so do the functions the device answers as Metal's own. Where the minimum and maximum + `copysign` and `atan2`; and so do the functions the device answers as Metal's own. Where the + minimum and maximum functions and `saturate` are the device's `NMin`, `NMax` and `NClamp`, their pick of a denormal - is `NativeMinMax`'s. Off MoltenVK the device's multiply and fused multiply-add round a result whose - exact value is below `2^-126` up to `2^-126`, which Metal flushes - (`float-tininess-after-rounding`). + is `NativeMinMax`'s. Off MoltenVK the device's multiply and fused multiply-add, and `ldexp` on + `float` by that multiply, round a result whose exact value is below `2^-126` up to `2^-126`, which + Metal flushes (`float-tininess-after-rounding`). - `metal2vulkan-degrade-pixel-sampler-scaling` (precision). `PrecisionNote::PixelSamplerScaling`: A pixel-coordinate sampler's coordinate reaches the device's sampler divided by the texture's level-0 size in the shader, on every device, and the device reads its texels from that coordinate diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 3938b09ec..2b43dcf0d 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -59,7 +59,7 @@ known. The rest of the differences are docs/API.md section 9's precision notes. | `dot`, `mix` and `fast::sqrt` are the device's `OpDot`, `FMix` and `Sqrt` on every device, not Metal's fused chain, its two fused multiply-adds and `x · rsqrt(x)`: a `float` one on MoltenVK reaches Metal's own `dot`, `mix` and `fast::sqrt`, a `half` one is the instruction on its `float` container, rounded once; a `dot` of other than two to four lanes sums the `OpDot` of each chunk of up to four lanes. | The device's order is the contract. | `float` `dot` of n lanes within (3n − 1)·2^-24·Σ\|aᵢbᵢ\|, `mix` within 2^-21 of its two terms, `fast::sqrt` 4.5 ulp. `NativeDot` (`native-dot`), `NativeMix` (`native-mix`), `NativeTranscendentals` (`native-transcendentals`); a `dot` of other than two to four lanes within Vulkan's precision of its chunks' `OpDot` and sum, `DeviceArithmetic` (`device-arithmetic`) | | Fast-math sums, differences and products group, and a multiply contracts into an addition, as the device's compiler groups and fuses them, on every device, MoltenVK included: each operation is emitted in the AIR's order, one carrying both `contract` and `reassoc` undecorated and any other `NoContraction`. Metal's compiler takes its own grouping (its rank order of a sum's leaves, its pairing of a product's lane-uniform factors) and its own fusion pairs. | The device's grouping is the contract: what the AIR's flags allow, the device's compiler chooses; nothing reproduces Metal's choice. | Unbounded relative to the result where a sum of mixed signs is grouped otherwise, or a multiply-add is fused on one side alone; each operation kept apart is exact as written. `FastMathRewrites` (`fast-math-rewrites`) | | `air.pack` and `air.unpack` of a normalized format are the device's conversion: `GLSL.std.450`'s `Pack`/`Unpack` `Unorm4x8`, `Snorm4x8`, `Unorm2x16` and `Snorm2x16`, and for `rgb10a2` and `rgb565` their definition by the device's `NClamp`, multiply, `RoundEven` (a pack) and multiply by the reciprocal (an unpack); `unorm4x8_srgb`'s colour is the MSL transfer functions by the device's `Pow` around the 8-bit instruction, as is an 8-bit sRGB storage texture's write through its UNORM alias. A `float` one of the four named formats on MoltenVK reaches Metal's own. The same conversions serve an implicit imageblock access of a normalized attachment (a `half` load rounding the unpacked `float` once more), the interlocked storage form's 8-bit sRGB attachment and sRGB word, and a snapshot's normalized codes (`c` times the `f32` nearest `1/D`). | The device's conversion is the contract. | A packed code within one of Metal's, apart only where `clamp(c)·scale` lies within its rounding of a half-integer (`Round`'s tie is the device's); an unpacked value within 2.5 ulp of the correctly rounded quotient; a decoded sRGB colour within half a twelve-bit step (`1/8190`) of Metal's table step `k/4095`, beyond its `Pow`'s bound; a NaN channel's code the device's (Metal's: 0). `DevicePackConversion` (`device-pack-conversion`) | -| Off MoltenVK a `float` multiply, and a fused multiply-add by `OpFmaKHR`, round before Metal's flush: Metal flushes a result whose exact value lies below `2^-126` before rounding it, so a result whose exact value lies within half a unit below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its sign. | Every device measured off MoltenVK (RADV, ANV, NVIDIA, lavapipe) rounds first; its multiply is the contract. | Announced once per device as it is made: `float-tininess-after-rounding` | +| Off MoltenVK a `float` multiply, a fused multiply-add by `OpFmaKHR`, and a `float` `ldexp` (whose result below `2^-126` is that multiply's product) round before Metal's flush: Metal flushes a result whose exact value lies below `2^-126` before rounding it, so a result whose exact value lies within half a unit below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its sign. | Every device measured off MoltenVK (RADV, ANV, NVIDIA, lavapipe) rounds first; its multiply is the contract. | Announced once per device as it is made: `float-tininess-after-rounding` | | `f32` denormals in a `float` operation on a device not measured to read them as Metal does (`DriverDeviation::MetalFloatDenormals`: lavapipe, and every unmeasured device), in `floor`, `ceil` and `fneg` on every device, in the maths functions (`fabs`, `sign` and `fract` among them, and the zero and sign tests of `pow`, `powr` and `atan2`) and the device's order of the rows above, and in a conversion to or from `double`. Metal reads them as zero and flushes a denormal result. | The device's reading is the contract: Vulkan lets each device keep or flush them, and Metal runs no `double`, so its reading of a conversion is not measured. | `DeviceDenormals` (`device-denormals`) | | Samples, gathers, depth comparisons and level queries are the device's own sampler's on every device whose sampler is not Metal's (every device but MoltenVK): its filter, footprint, level, anisotropy, sRGB decode and comparison, its level clamp (the descriptor's as given, which Metal holds on a `1/64` grid) and its reading of a subnormal coordinate (which Metal reads as the zero of its sign before addressing). A sample's or gather's residency on a device with `shaderResourceResidency` is the device's `OpImageSparse*` word and non-resident value (`SparseResidency`); Metal's footprint rule answers it only where the device has no residency and the slot's tile map does. A two-plane YCbCr texture's linear sample is the device's filter of its texels (the device's own Y′CbCr conversion's where it converts the format, next row; Metal's reads exactly elsewhere); where the device's memory is the host's, a sample that reads the planes in place is Metal's filter exactly. An 8-bit sRGB format the device cannot sample (`R8_SRGB` on lavapipe, `R8G8_SRGB` on lavapipe, ANV and NVIDIA) is held as its UNORM twin, the device lacking its decode: a read, a sample, a fetch, a gather and a bicubic tap decode what the twin's view reads (one texel's codes, or the device's filter of them) by the MSL transfer through the device's `Pow`. | The device's sampler is the contract. | Linear filter within one code (1/255) on the measured devices, sRGB within 1.83, a two-plane YCbCr texture's within 1.183; levels, anisotropy, cubes, 3D, gathers and comparisons unmeasured. A twin's texel: the transfer within the device's `Pow` (MoltenVK's within 1.83e-7 through a storage alias, `runtime_lane::srgb_storage`) where Metal's table rounds it to a twelve-bit step (up to 1/8190 apart); a filtered sample decodes the filtered codes, up to 0.286 off for a blend of codes 0 and 255 (`SrgbTexelDecode`). `DeviceSampling` (`device-sampling`) | | A pixel-coordinate sampler's sample, gather, comparison and level query is the device's sampler's at the coordinate divided by the texture's level-0 size in the shader, on every device, MoltenVK included: the device reads its texels from that coordinate in its own arithmetic, where Metal reads the coordinate as given (its nearest texel `floor(x)`, its filter's footprint from `x`). A constexpr pixel sampler that filters nearest and clamps to the edge fetches its texel (`sampler.pixel_fetch`), as does an argument buffer's sample without a comparison through a state that does. | The device's sampler is the contract. Vulkan's unnormalized samplers take no gather, offset, comparison, array, 3D image or level of detail, so the shader scales the coordinate; nothing reproduces Metal's position at a texel edge. | The device answers as its normalized sampler at a coordinate within one `f32` ulp of the quotient on MoltenVK (10 of 800 cells of each mirror-clamp configuration move by it; a nearest sample at a texel edge then reads the neighbouring texel) and at the quotient itself on lavapipe (`runtime_lane::pixel_samplers`: 800 coordinates at and around each texel edge of a 5x3 texture, every filter and address mode); a linear downscale by 8 of textures 1000 to 1920 texels wide within 1/255 of Metal's on MoltenVK and 2/255 on lavapipe (`runtime_lane::pixel_downscales`); other devices unmeasured. `PixelSamplerScaling` (`pixel-sampler-scaling`), `ArgumentPixelSamplerScaling` (`argument-pixel-sampler-scaling`) |