From 7d9aa2207f867e461fa7e4caf0ffc097ec9be1b4 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 3 Dec 2026 01:11:58 -0800 Subject: [PATCH] Tests: the half rounding test holds the rounding's pair to NoContraction, apart from the store's narrowing converted_half_rounding_is_the_exactly_rounded_half (red two commits back) asserted that every OpFConvert in its kernel is decorated NoContraction in both forms. Its kernel also stores the half result, and that store's narrowing to bits (narrow::to_bits) is no pair: it is decorated under the float controls alone, which a_half_encode_is_the_devices_narrowing_in_every_form holds and the fix keeps. So the assertion also failed at the fix, on the store's narrowing ("round Converted: [Allowed, Allowed, Allowed]" before the fix, job m3-max-82752). The test now finds the pair by structure (a narrowing of an f32 to 16 bits and a widening of its result back to 32 bits), asserts both are NoContraction in both forms, and asserts every other conversion takes its form's decoration: NoContraction under the float controls, none in Converted. Test only; no translation output changes. --- .../src/lower/arith/tests/narrow.rs | 67 +++++++++++++++---- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs index fd2e5768d..7e393f306 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/narrow.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/narrow.rs @@ -1141,10 +1141,12 @@ fn half_rounding_operands() -> Vec { /// and its widening alone, which the interpreter rounds to nearest even with half denormals kept, /// as the Vulkan spec makes it under the controlled form's float controls: over /// [`half_rounding_operands`], `round` gives the exactly rounded half on scalars and vectors, with -/// no `OpQuantizeToF16` and no `RoundEven` in the module. In both forms each conversion is -/// decorated `NoContraction`: without it Vulkan assumes `AllowTransform`, under which Mesa's NIR -/// (`nir_opt_algebraic`'s `~f2f32(f2f(a))`) folds the widening of the narrowing into the `f32` it -/// started from, so the value is never rounded. Metal's readings of A1's +/// no `OpQuantizeToF16` and no `RoundEven` in the module. In both forms the rounding's conversion +/// and its widening are each decorated `NoContraction`: without it Vulkan assumes +/// `AllowTransform`, under which Mesa's NIR (`nir_opt_algebraic`'s `~f2f32(f2f(a))`) folds the +/// widening of the narrowing into the `f32` it started from, so the value is never rounded. The +/// store's narrowing to bits, no pair, is decorated under the float controls alone, as +/// `a_half_encode_is_the_devices_narrowing_in_every_form` holds. Metal's readings of A1's /// `half-denormal-results-of-arithmetic-and-conversions` (dd2cb644) and /// `half-tanh-under-fast-math-at-every-sixteenth-pattern` (27353187) keep every half denormal a /// result rounds to, as this rounding does. @@ -1180,22 +1182,61 @@ fn converted_half_rounding_is_the_exactly_rounded_half() { }; let m = kernel_with(&[input], form, &body, true, false) .unwrap_or_else(|e| panic!("{name} {q:?}: build: {e:?}")); - let conversions: Vec = m + // The width of `v`'s float lanes, if it is a float or a vector of floats. + let width = |v: Value| -> Option { + let ty = m.value(v).ok()?.ty; + let ty = match m.types().get(ty).ok()? { + crate::spirv::ir::Type::Vector { elem, .. } => *elem, + _ => ty, + }; + match m.types().get(ty).ok()? { + crate::spirv::ir::Type::Float { width } => Some(*width), + _ => None, + } + }; + // Each `OpFConvert`: its result, its operand and its contraction. + let converts: Vec<(Value, Value, Contraction)> = m .blocks() .iter() .flat_map(|b| &b.insts) - .filter_map(|n| match n.inst { - Inst::Convert { - op: crate::spirv::ir::ConvOp::FConvert, - contract, - .. - } => Some(contract), + .filter_map(|n| match (&n.inst, n.result) { + ( + Inst::Convert { + op: crate::spirv::ir::ConvOp::FConvert, + a, + contract, + }, + Some(r), + ) => Some((r, *a, *contract)), _ => None, }) .collect(); + let narrowings: std::collections::HashMap = converts + .iter() + .filter(|(r, a, _)| width(*a) == Some(32) && width(*r) == Some(16)) + .map(|(r, _, c)| (*r, *c)) + .collect(); + // The rounding's pair: a narrowing to 16 bits and the widening of its result. + let widenings: Vec<(Value, Value)> = converts + .iter() + .filter(|(r, a, _)| width(*r) == Some(32) && narrowings.contains_key(a)) + .map(|(r, a, _)| (*r, *a)) + .collect(); + let in_pair = |v: Value| widenings.iter().any(|(r, a)| *r == v || *a == v); + let (pair, rest): (Vec<_>, Vec<_>) = converts.iter().partition(|(r, _, _)| in_pair(*r)); assert!( - conversions.len() >= 2 && conversions.iter().all(|c| *c == Contraction::Forbidden), - "{name} {q:?}: {conversions:?}" + !widenings.is_empty() && pair.iter().all(|c| c.2 == Contraction::Forbidden), + "{name} {q:?}: the pair {pair:?}" + ); + // The store's narrowing to bits is no pair: decorated under the float controls alone + // (`narrow::to_bits`, `a_half_encode_is_the_devices_narrowing_in_every_form`). + let held = match q { + QuantizeForm::Controlled => Contraction::Forbidden, + _ => Contraction::Allowed, + }; + assert!( + rest.iter().all(|c| c.2 == held), + "{name} {q:?}: the rest {rest:?}" ); let quantizes = crate::lower::arith::narrow::quantizes(&m); let rounds_even = m.blocks().iter().flat_map(|b| &b.insts).any(|n| {