From 2bc702f4b4d87c45d9d3ce12a8e318609c149e76 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Wed, 11 Nov 2026 00:41:52 -0800 Subject: [PATCH] air.convert roundings: drop the double paths AIR cannot name; fix the tests' first run air.convert names no double (the AIR element types stop at f32), so the directed double narrowings, the double results and the 64-bit RTE width were unreachable: removed. The nearest-even plan is now a flag for a float result, and narrow::rte_modes decides 32 bits and the widths the device's independence ties to it. The engine test reads the interpreter's buffers directly, pins the RTE widths of the venue profiles (lavapipe and RADV 32, ANV 16/32/64, MoltenVK and the floor none) and the parse test names a float narrowing instead of a double one. Verified: the first test job (m3-max-14044, homelab-ms01-1) failed on the double case in the parse test and a compile error in the engine test; both fixed here, rerun next. --- .../metal2vulkan-air/src/intrinsics/tests.rs | 2 +- .../metal2vulkan-runtime/src/degradation.rs | 11 +- crates/metal2vulkan/src/lower/arith/math.rs | 3 +- crates/metal2vulkan/src/lower/arith/mod.rs | 58 ++++----- crates/metal2vulkan/src/lower/arith/narrow.rs | 27 ++--- .../metal2vulkan/src/lower/arith/rounding.rs | 114 ++++-------------- crates/metal2vulkan/src/lower/arith/wide.rs | 11 +- crates/metal2vulkan/src/pipeline/artifact.rs | 7 +- .../src/tests/engine_convert_rounding.rs | 22 +++- docs/API.md | 8 +- 10 files changed, 97 insertions(+), 166 deletions(-) diff --git a/crates/metal2vulkan-air/src/intrinsics/tests.rs b/crates/metal2vulkan-air/src/intrinsics/tests.rs index 41bdf049d..1b78009e4 100644 --- a/crates/metal2vulkan-air/src/intrinsics/tests.rs +++ b/crates/metal2vulkan-air/src/intrinsics/tests.rs @@ -322,7 +322,7 @@ fn convert_parses_its_rounding_and_saturation() { false, ), ("air.convert.f.f16.s.i32.rtz", ConvertRounding::Rtz, false), - ("air.convert.f.f32.f.f64.rtp", ConvertRounding::Rtp, false), + ("air.convert.f.f16.f.f32.rtp", ConvertRounding::Rtp, false), ("air.convert.s.i32.f.f32.rtn", ConvertRounding::Rtn, false), ( "air.convert.s.i32.f.f32.rtz.sat", diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 34a9c7579..74bad280d 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2585,12 +2585,11 @@ degradations! { NativeHalfConversion = "native-half-conversion"; /// Class: native device maths (a conversion). The device's correctly rounded conversion /// answers an `air.convert` whose name rounds to nearest even (`.rte`, OpenCL's - /// `convert_float` of an `int`) and that can round (a 32- or 64-bit integer to `float`, a - /// 64-bit integer to `double`, a `double` to `float`), on a device whose float controls - /// cannot declare `RoundingModeRTE` for the result's width with the widths its - /// independence ties to it (MoltenVK). Recorded bound: the exact value's neighbour on - /// either side, one unit in the last place, the device's choice, where Metal answers the - /// nearer one, ties to even. + /// `convert_float` of an `int`) and that can round (a 32- or 64-bit integer to `float`), on + /// a device whose float controls cannot declare `RoundingModeRTE` for 32 bits with the + /// widths its independence ties to it (MoltenVK). Recorded bound: the exact value's + /// neighbour on either side, one unit in the last place, the device's choice, where Metal + /// answers the nearer one, ties to even. NativeConversionRounding = "native-conversion-rounding"; /// Class: native half rounding. The device's `OpQuantizeToF16` alone rounds the /// pipeline's `half` results computed in `float` on every device without `shaderFloat16`, diff --git a/crates/metal2vulkan/src/lower/arith/math.rs b/crates/metal2vulkan/src/lower/arith/math.rs index 5c9faf735..c524e0087 100644 --- a/crates/metal2vulkan/src/lower/arith/math.rs +++ b/crates/metal2vulkan/src/lower/arith/math.rs @@ -2376,7 +2376,8 @@ pub enum ConvertKind { /// Integer (`signed` source) to integer. Int { signed: bool }, /// Integer (`src_signed` source) to integer (`dst_signed`), saturating (`air.convert`'s - /// `.sat`): clamped to the range both share ([`rounding::saturate`]), then [`ConvertKind::Int`]. + /// `.sat`): clamped to the range both share ([`rounding::saturate`]), then + /// [`ConvertKind::Int`]. SatInt { src_signed: bool, dst_signed: bool }, /// Integer (`signed` source) to a float that cannot hold every value of it, rounded `dir` /// (`air.convert`'s `.rtz`, `.rtp` and `.rtn`): exact by integer arithmetic diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs index dcdf58343..9af921124 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs @@ -555,7 +555,7 @@ impl FamilyStrategy for ArithStrategy { kind: ConvertKind::ToFloatDirected { .. } | ConvertKind::FloatDirected(_), from, to, - } => 160 + conversion_words(*from, *to) + rounding::directed_words(*from, *to), + } => 160 + conversion_words(*from, *to) + rounding::directed_words(*to), S::Convert { from, to, .. } => 160 + conversion_words(*from, *to), S::Pack { dir, @@ -752,13 +752,12 @@ pub struct ArithPlan { /// [`pack::measured`]), recorded at selection: the module then requires /// `VkProperty::MetalPackConversions`. pub metal_packs: bool, - /// The float widths (32 or 64) of the conversions that round to nearest even by their name - /// (`air.convert`'s `.rte`) and can round (an integer of more bits than the format's - /// significand, a `double` to `float`), recorded at selection; [`finish`] decides - /// [`ArithPlan::rounding_modes`] from them. - pub rte: BTreeSet, + /// Whether a conversion rounds an integer to `float` to nearest even by its name + /// (`air.convert`'s `.rte`) and can round (an integer of more than 24 bits of magnitude), + /// recorded at selection; [`finish`] decides [`ArithPlan::rounding_modes`] from it. + pub rounds_even: bool, /// The widths whose `RoundingModeRTE` execution mode the entry declares, decided by [`finish`]: - /// those [`ArithPlan::rte`] needs and the device's float controls tie to them + /// those [`ArithPlan::rounds_even`] needs and the device's float controls tie to them /// (`narrow::rte_modes`), where the profile supports them all; elsewhere the conversions are /// the device's (`PrecisionNote::NativeConversionRounding`). pub rounding_modes: BTreeSet, @@ -848,12 +847,11 @@ pub fn finish(cx: &mut SelectCx<'_>) -> Result<(), Error> { cx.precision.insert(PrecisionNote::DeviceSignedZeroInfNan); } } - // A conversion that rounds to nearest even by its name: under `RoundingModeRTE` for its width + // A conversion that rounds to nearest even by its name: under `RoundingModeRTE` for 32 bits // (and the widths the device ties to it) where the device has them all, the device's // correctly rounded conversion elsewhere. - let rte: Vec = cx.families.arith.rte.iter().copied().collect(); - for w in rte { - match narrow::rte_modes(cx.profile, w) { + if cx.families.arith.rounds_even { + match narrow::rte_modes(cx.profile) { Some(widths) => cx.families.arith.rounding_modes.extend(widths), None => { cx.precision.insert(PrecisionNote::NativeConversionRounding); @@ -2837,10 +2835,8 @@ fn select_convert( /// The kind of a float-to-float `air.convert` from `from` to `to` with `c`'s rounding: the plain /// conversion, which is exact for a widening and rounds to nearest even as Metal's `fptrunc` does -/// for a narrowing (`.rte` asks no more of it, but that a `double` narrowed to `float` rounds so -/// only where the entry declares `RoundingModeRTE` for 32 bits, [`ArithPlan::rte`]); or, for a -/// narrowing toward zero or an infinity, [`ConvertKind::FloatDirected`], which Vulkan has no -/// conversion of. +/// for a narrowing (`.rte` asks no more of it); or, for a narrowing toward zero or an infinity, +/// [`ConvertKind::FloatDirected`], which Vulkan has no conversion of. fn float_convert( cx: &mut SelectCx<'_>, c: &Convert, @@ -2852,22 +2848,15 @@ fn float_convert( }; let widening = matches!( (src, dst), - ( - FloatFmt::Half | FloatFmt::BFloat, - FloatFmt::F32 | FloatFmt::F64 - ) | (FloatFmt::F32, FloatFmt::F64) + (FloatFmt::Half | FloatFmt::BFloat, FloatFmt::F32) ); if src == dst || widening { return Ok(ConvertKind::Float); } match rounding::Directed::of(c.rounding) { - None => { - if c.rounding.is_some() && (src, dst) == (FloatFmt::F64, FloatFmt::F32) { - cx.families.arith.rte.insert(32); - } - Ok(ConvertKind::Float) - } - Some(dir) if rounding::narrows(src, dst) => Ok(ConvertKind::FloatDirected(dir)), + // Nearest even: the narrowing every `air.convert` to `half` takes, which rounds so. + None => Ok(ConvertKind::Float), + Some(dir) if rounding::narrows(from, to) => Ok(ConvertKind::FloatDirected(dir)), // A `bfloat` narrowing by a directed rounding: Apple's OpenCL compiler emits none. Some(_) => Err(not_lowered()), } @@ -2876,9 +2865,9 @@ fn float_convert( /// The kind of an integer-to-float `air.convert` (a `signed` source) from `from` to `to` with /// `c`'s rounding. One whose every source value is a value of the destination cannot round: the /// plain conversion, whatever the rounding. Elsewhere, nearest even is the plain conversion too, -/// which rounds so where the entry declares `RoundingModeRTE` for the result's width -/// ([`ArithPlan::rte`]; a `half` or `bfloat` result is rounded by integer arithmetic already); and -/// toward zero or an infinity is [`ConvertKind::ToFloatDirected`]. +/// which rounds so where the entry declares `RoundingModeRTE` for 32 bits +/// ([`ArithPlan::rounds_even`]; a `half` or `bfloat` result is rounded by integer arithmetic +/// already); and toward zero or an infinity is [`ConvertKind::ToFloatDirected`]. fn int_to_float_convert( cx: &mut SelectCx<'_>, c: &Convert, @@ -2894,19 +2883,16 @@ fn int_to_float_convert( return Ok(plain); } match (rounding::Directed::of(c.rounding), dst) { - (None, FloatFmt::F32 | FloatFmt::F64) => { - cx.families.arith.rte.insert(dst.container()); + (None, FloatFmt::F32) => { + cx.families.arith.rounds_even = true; Ok(plain) } (None, _) => Ok(plain), (Some(dir), FloatFmt::Half | FloatFmt::F32) => { Ok(ConvertKind::ToFloatDirected { signed, dir }) } - // Only a 64-bit integer rounds to `double`. - (Some(dir), FloatFmt::F64) if from.container() == 64 => { - Ok(ConvertKind::ToFloatDirected { signed, dir }) - } - // A `bfloat` result by a directed rounding: Apple's OpenCL compiler emits none. + // A `bfloat` result by a directed rounding: Apple's OpenCL compiler emits none (and + // `air.convert` names no `double`). (Some(_), _) => Err(not_lowered()), } } diff --git a/crates/metal2vulkan/src/lower/arith/narrow.rs b/crates/metal2vulkan/src/lower/arith/narrow.rs index af8e1b595..1dd9cb4f8 100644 --- a/crates/metal2vulkan/src/lower/arith/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/narrow.rs @@ -276,27 +276,24 @@ pub fn controlled_modes(p: &DeviceProfile) -> Option> { } /// The widths whose `RoundingModeRTE` an entry declares on profile `p` so that a conversion with a -/// `width`-bit float result rounds to nearest even, or `None` where the profile lacks one: `width` -/// itself, and the widths the device's rounding-mode independence ties to it (RuntimeSpirv VUIDs -/// 06291 and 06292): 16 and 64 together where rounding modes are independent for 32 bits only and -/// `width` is not 32, every width where they are not independent. The Vulkan spec ("Precision and -/// Operation of SPIR-V Instructions") rounds a correctly rounded operation "according to IEEE 754 -/// roundTiesToEven" in an entry that declares it, and to either neighbour, the device's choice, -/// elsewhere. -pub fn rte_modes(p: &DeviceProfile, width: u8) -> Option> { +/// `float` result rounds to nearest even, or `None` where the profile lacks one: 32, and 16 and 64 +/// with it where the device's rounding modes are not independent (RuntimeSpirv VUID 06292; with +/// independence for 32 bits only, 32 is free of the others, VUID 06291). The Vulkan spec +/// ("Precision and Operation of SPIR-V Instructions") rounds a correctly rounded operation +/// "according to IEEE 754 roundTiesToEven" in an entry that declares it, and to either neighbour, +/// the device's choice, elsewhere. +pub fn rte_modes(p: &DeviceProfile) -> Option> { let fc = p.fields().float_controls; let has = |w: u8| match w { 16 => fc.f16.rounding_rte, 32 => fc.f32.rounding_rte, - 64 => fc.f64.rounding_rte, - _ => false, + _ => fc.f64.rounding_rte, }; - let widths = match (fc.rounding_independence, width) { - (Independence::All, w) | (Independence::Only32Bit, w @ 32) => vec![w], - (Independence::Only32Bit, _) => vec![16, 64], - (Independence::None, _) => vec![16, 32, 64], + let widths = match fc.rounding_independence { + Independence::All | Independence::Only32Bit => vec![32], + Independence::None => vec![16, 32, 64], }; - (has(width) && widths.iter().all(|w| has(*w))).then_some(widths) + widths.iter().all(|w| has(*w)).then_some(widths) } /// Whether module `m` rounds to `half` through [`converted`] or [`controlled`]: an `OpFConvert` of diff --git a/crates/metal2vulkan/src/lower/arith/rounding.rs b/crates/metal2vulkan/src/lower/arith/rounding.rs index 85034d0e5..35ee6166e 100644 --- a/crates/metal2vulkan/src/lower/arith/rounding.rs +++ b/crates/metal2vulkan/src/lower/arith/rounding.rs @@ -15,11 +15,11 @@ //! conversion (its `FPRoundingMode` decoration is for 16-bit stores only, and an entry's //! `RoundingModeRTZ` would round its arithmetic too), so the result is computed exactly by //! integer arithmetic on the operand's bits ([`int_to_float`], [`narrow_float`]); a conversion -//! that cannot round is the plain one. +//! that cannot round is the plain one. `air.convert` names no `double`. use crate::air::intrinsics::ConvertRounding; use crate::lower::arith::legal::{FloatFmt, Form, Held}; -use crate::lower::arith::narrow::{self, u32_form}; +use crate::lower::arith::narrow::u32_form; use crate::lower::arith::ops::{self, fsplat, mask, sext, splat, ty}; use crate::spirv::builder::FunctionBuilder; use crate::spirv::error::{BResult, BuildError, OperandRule}; @@ -203,10 +203,10 @@ pub fn magnitude_f32( magnitude32(fb, u, to, m, a) } -/// Unsigned 64-bit magnitude `m` (`u64` lanes of form `w`) as a float of form `to` (`f32` or -/// `f64`) rounded toward zero, or away from it where `away`, as [`magnitude32`] does for 32 bits: -/// the most significant bit is found from the two halves, the bits below the format's significand -/// are dropped (one added where any was set and the lane rounds away), and the kept part converts +/// Unsigned 64-bit magnitude `m` (`u64` lanes of form `w`) as an `f32` (lanes of form `to`) +/// rounded toward zero, or away from it where `away`, as [`magnitude32`] does for 32 bits: the most +/// significant bit is found from the two halves, the bits below the 24 the significand keeps are +/// dropped (one added where any was set and the lane rounds away), and the kept part converts /// exactly and scales back exactly. fn magnitude64( fb: &mut FunctionBuilder<'_>, @@ -215,10 +215,10 @@ fn magnitude64( m: Value, away: Away, ) -> BResult { - let keep = match to.float_fmt() { - Some(fmt @ (FloatFmt::F32 | FloatFmt::F64)) => u64::from(significand(fmt)), - _ => return Err(unconverted()), - }; + if to.float_fmt() != Some(FloatFmt::F32) { + return Err(unconverted()); + } + let keep = u64::from(significand(FloatFmt::F32)); let u = u32_form(w); let ut = ty(fb, u)?; let wt = ty(fb, w)?; @@ -244,13 +244,13 @@ fn magnitude64( fb.glsl(GlslOp::Ldexp, &[f, s]) } -/// Integer `x` of form `from` (read as signed when `signed`) converted to float form `to` (`half`, -/// `float`, or `double` from a 64-bit integer), rounded `dir`, exactly on every device. +/// Integer `x` of form `from` (read as signed when `signed`) converted to float form `to` (`half` +/// or `float`), rounded `dir`, exactly on every device. /// /// A `half` is the integer's `f32` (exact below `2^24`, and at least `2^24` elsewhere, past /// `half`'s range whichever way the device rounds it) rounded to `half` by [`to_half`]. A `float` -/// or `double` is the magnitude rounded by integer arithmetic ([`magnitude32`], [`magnitude64`]), -/// given its sign. +/// is the magnitude rounded by integer arithmetic ([`magnitude32`], [`magnitude64`]), given its +/// sign. pub fn int_to_float( fb: &mut FunctionBuilder<'_>, (signed, dir): (bool, Directed), @@ -373,57 +373,8 @@ pub fn to_half(fb: &mut FunctionBuilder<'_>, f: Form, x: Value, dir: Directed) - fb.convert(ConvOp::Bitcast, r, ft) } -/// `x` (`double` lanes of form `from`) converted to `f32` rounded `dir`: the device's conversion, -/// which returns one of the two `f32`s around `x` (Vulkan's correctly rounded conversion in any -/// mode), stepped one `f32` to the other where it lies on the wrong side of `x` (the step from a -/// zero is to the smallest denormal of the direction's sign). A NaN is kept. -fn f64_to_f32(fb: &mut FunctionBuilder<'_>, from: Form, x: Value, dir: Directed) -> BResult { - let f32f = from.with(Held::Float(FloatFmt::F32)); - let u = u32_form(from); - let t32 = ty(fb, f32f)?; - let ut = ty(fb, u)?; - let t64 = ty(fb, from)?; - let r = fb.convert(ConvOp::FConvert, x, t32)?; - let back = fb.convert(ConvOp::FConvert, r, t64)?; - let rb = fb.convert(ConvOp::Bitcast, r, ut)?; - let one = splat(fb, u, 1)?; - let less = bin(fb, BinOp::ISub, rb, one)?; - let more = bin(fb, BinOp::IAdd, rb, one)?; - let rb = match dir { - Directed::Zero => { - let ab = fb.glsl(GlslOp::FAbs, &[back])?; - let ax = fb.glsl(GlslOp::FAbs, &[x])?; - let past = bin(fb, BinOp::FOrdGreaterThan, ab, ax)?; - fb.select(past, less, rb)? - } - Directed::Up | Directed::Down => { - let (wrong, from_zero, toward) = if dir == Directed::Up { - (BinOp::FOrdLessThan, 0x0000_0001, (less, more)) - } else { - (BinOp::FOrdGreaterThan, 0x8000_0001, (more, less)) - }; - let wrong = bin(fb, wrong, back, x)?; - let k31 = splat(fb, u, 31)?; - let sign = bin(fb, BinOp::ShiftRightLogical, rb, k31)?; - let z = splat(fb, u, 0)?; - let neg = bin(fb, BinOp::INotEqual, sign, z)?; - // Toward the direction's infinity: a negative's magnitude shrinks for `Up`. - let step = fb.select(neg, toward.0, toward.1)?; - let abs_mask = splat(fb, u, 0x7FFF_FFFF)?; - let mag = bin(fb, BinOp::BitwiseAnd, rb, abs_mask)?; - let zero = bin(fb, BinOp::IEqual, mag, z)?; - let from_zero = splat(fb, u, from_zero)?; - let step = fb.select(zero, from_zero, step)?; - fb.select(wrong, step, rb)? - } - }; - fb.convert(ConvOp::Bitcast, rb, t32) -} - /// Float `x` of form `from` narrowed to float form `to`, rounded `dir`, exactly: `float` to `half` -/// ([`to_half`]), `double` to `float` ([`f64_to_f32`]), and `double` to `half` through an `f32` -/// rounded to odd (`narrow::f64_to_f32_odd`), whose one rounding after it to `half` is the exact -/// value's, as any rounding to a format of at most 22 significand bits is. +/// ([`to_half`]), the one narrowing `air.convert` names a directed rounding for. pub fn narrow_float( fb: &mut FunctionBuilder<'_>, from: Form, @@ -431,25 +382,15 @@ pub fn narrow_float( x: Value, dir: Directed, ) -> BResult { - match (from.float_fmt(), to.float_fmt()) { - (Some(FloatFmt::F32), Some(FloatFmt::Half)) => to_half(fb, from, x, dir), - (Some(FloatFmt::F64), Some(FloatFmt::F32)) => f64_to_f32(fb, from, x, dir), - (Some(FloatFmt::F64), Some(FloatFmt::Half)) => { - let o = narrow::f64_to_f32_odd(fb, from, x)?; - to_half(fb, from.with(Held::Float(FloatFmt::F32)), o, dir) - } - _ => Err(unconverted()), + if !narrows(from, to) { + return Err(unconverted()); } + to_half(fb, from, x, dir) } -/// Whether [`narrow_float`] narrows from `from` to `to`. -pub fn narrows(from: FloatFmt, to: FloatFmt) -> bool { - matches!( - (from, to), - (FloatFmt::F32, FloatFmt::Half) - | (FloatFmt::F64, FloatFmt::F32) - | (FloatFmt::F64, FloatFmt::Half) - ) +/// Whether [`narrow_float`] narrows from `from` to `to`: `float` to `half`. +pub fn narrows(from: Form, to: Form) -> bool { + (from.float_fmt(), to.float_fmt()) == (Some(FloatFmt::F32), Some(FloatFmt::Half)) } /// The range of an integer of `bits` bits, signed or not. @@ -522,16 +463,13 @@ pub fn saturate( mask(fb, from, r) } -/// An upper estimate of the SPIR-V words of a directed conversion from `from` to `to` beyond the -/// conversion it replaces: [`to_half`]'s sixty instructions and twenty constants (a vector's -/// constants carry their lanes), a magnitude's rounding, or a `double`'s step. -pub fn directed_words(from: Form, to: Form) -> u32 { +/// An upper estimate of the SPIR-V words of a directed conversion to `to` beyond the conversion it +/// replaces: [`to_half`]'s sixty instructions and twenty constants (a vector's constants carry +/// their lanes), or a magnitude's rounding. +pub fn directed_words(to: Form) -> u32 { let lanes = u32::from(to.lanes); - let half = 360 + 20 * lanes; match to.float_fmt() { - Some(FloatFmt::Half) if from.float_fmt() == Some(FloatFmt::F64) => half + 96, - Some(FloatFmt::Half) => half + 32, - _ if from.is_float() => 160 + 8 * lanes, + Some(FloatFmt::Half) => 392 + 20 * lanes, _ => 200 + 10 * lanes, } } diff --git a/crates/metal2vulkan/src/lower/arith/wide.rs b/crates/metal2vulkan/src/lower/arith/wide.rs index 59b070b46..2fbe21ae4 100644 --- a/crates/metal2vulkan/src/lower/arith/wide.rs +++ b/crates/metal2vulkan/src/lower/arith/wide.rs @@ -1619,9 +1619,9 @@ impl WideOp { // A directed rounding's magnitude and `half` steps beyond the plain conversion. WideOp::Convert { kind: ConvertKind::ToFloatDirected { .. }, - from, to, - } => 1024 + rounding::directed_words(*from, *to), + .. + } => 1024 + rounding::directed_words(*to), WideOp::Cast { .. } | WideOp::Convert { .. } => { // The float paths: the collapse to 32 bits (two shifts, a compare) and rounding. 1024 @@ -2168,9 +2168,9 @@ fn select_convert( // Nearest even: the conversion's rounding where the entry declares // `RoundingModeRTE` for its width (a `half` or `bfloat` rounds by integer // arithmetic). - (None, Some(fmt @ (FloatFmt::F32 | FloatFmt::F64))) => { + (None, Some(FloatFmt::F32)) => { if c.rounding.is_some() { - cx.families.arith.rte.insert(fmt.container()); + cx.families.arith.rounds_even = true; } ConvertKind::ToFloat { signed } } @@ -2178,7 +2178,8 @@ fn select_convert( (Some(dir), Some(FloatFmt::F32 | FloatFmt::Half)) => { ConvertKind::ToFloatDirected { signed, dir } } - // A `double` or `bfloat` by a directed rounding from a wide integer. + // A `bfloat` by a directed rounding from a wide integer (`air.convert` names no + // `double`). (Some(_), _) => return Err(not_lowered()), } } diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 208f244ec..74cac5be1 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1962,10 +1962,9 @@ pub enum PrecisionNote { NativeHalfQuantize, /// Native conversion rounding: an `air.convert` whose name rounds to nearest even (`.rte`, /// Apple's OpenCL compiler's `convert_` and `convert__rte`) and that can round (a - /// 32- or 64-bit integer to `float`, a 64-bit integer to `double`, a `double` to `float`) is - /// the device's correctly rounded conversion, in an entry that cannot declare - /// `RoundingModeRTE` for the result's width: the device's float controls lack it, or its - /// rounding-mode independence ties it to a width that lacks it (MoltenVK, whose 64-bit + /// 32- or 64-bit integer to `float`) is the device's correctly rounded conversion, in an entry + /// that cannot declare `RoundingModeRTE` for 32 bits: the device's float controls lack it, or + /// its rounding-mode independence ties it to a width that lacks it (MoltenVK, whose 64-bit /// rounding mode it does not state). The Vulkan spec rounds such a conversion to either of /// the two values around the exact one, the device's choice, where Metal rounds to nearest /// even. Where the entry declares the mode, the conversion is Metal's and records nothing. The diff --git a/crates/metal2vulkan/src/tests/engine_convert_rounding.rs b/crates/metal2vulkan/src/tests/engine_convert_rounding.rs index e95d5f2ea..a7f45fe7d 100644 --- a/crates/metal2vulkan/src/tests/engine_convert_rounding.rs +++ b/crates/metal2vulkan/src/tests/engine_convert_rounding.rs @@ -620,7 +620,7 @@ fn every_rounding_is_exact_on_the_interpreter() { let out = run(&m, &Config::default(), res) .unwrap_or_else(|e| panic!("{what}: interpreter: {e}")) .buffers; - let got = &out[0].bytes; + let got = &out[0]; let n = c.dst.ty.bytes(); for (k, x) in xs.iter().enumerate() { let mut word = [0u8; 8]; @@ -739,7 +739,7 @@ fn nearest_even_declares_the_mode_or_records_the_note() { .precision .contains(&PrecisionNote::NativeConversionRounding); let widths = rounds_even_at(&a.module.words); - let modes = narrow::rte_modes(&p, 32); + let modes = narrow::rte_modes(&p); if rounds { match &modes { Some(want) => { @@ -757,12 +757,22 @@ fn nearest_even_declares_the_mode_or_records_the_note() { } } else { assert!(!noted_here, "{what}: noted"); - assert!( - widths.is_empty() || narrow::controlled_modes(&p).is_some(), - "{what}: declares {widths:?}" - ); + assert!(widths.is_empty(), "{what}: declares {widths:?}"); } } + // The float controls of the profiles the venues run, read from their descriptions: + // RADV's rounding modes are independent for 32 bits only, ANV's not at all (so 32 brings + // 16 and 64), lavapipe's fully; MoltenVK has no 64-bit RTE and no independence, and the + // floor states no float controls. + let known: Option>> = match stem.as_str() { + "lavapipe" | "amd-780m-radv" => Some(Some(vec![32])), + "intel-irisxe-anv" => Some(Some(vec![16, 32, 64])), + "moltenvk-m3" | "vk12-floor" => Some(None), + _ => None, + }; + if let Some(want) = known { + assert_eq!(narrow::rte_modes(&p), want, "{stem}"); + } } assert!( declared > 0 && noted > 0, diff --git a/docs/API.md b/docs/API.md index 1b4423ecc..0addde571 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4199,10 +4199,10 @@ bound. - `metal2vulkan-degrade-native-conversion-rounding` (precision). `PrecisionNote::NativeConversionRounding`: Native device maths (a conversion): an `air.convert` whose name rounds to nearest even (`.rte`: OpenCL's `convert_float` of an `int`, as Apple's - OpenCL compiler spells it) and that can round (a 32- or 64-bit integer to `float`, a 64-bit - integer to `double`, a `double` to `float`) is the device's correctly rounded conversion where - the entry cannot declare `RoundingModeRTE` for the result's width with the widths the device's - rounding-mode independence ties to it (MoltenVK, whose 64-bit rounding mode it does not state): + OpenCL compiler spells it) and that can round (a 32- or 64-bit integer to `float`) is the + device's correctly rounded conversion where the entry cannot declare `RoundingModeRTE` for 32 + bits with the widths the device's rounding-mode independence ties to it (MoltenVK, whose 64-bit + rounding mode it does not state): either value around the exact one, the device's choice, where Metal answers the nearer one, ties to even. Where the entry declares the mode the conversion is Metal's and records nothing. The conversions that round toward zero or an infinity (`.rtz`, `.rtp`, `.rtn`) are exact on every