diff --git a/crates/metal2vulkan-air/src/canon/fold.rs b/crates/metal2vulkan-air/src/canon/fold.rs index 30d3499e0..668b7d301 100644 --- a/crates/metal2vulkan-air/src/canon/fold.rs +++ b/crates/metal2vulkan-air/src/canon/fold.rs @@ -327,9 +327,11 @@ impl Folder<'_> { self.mk_int(ty, v, m) } - /// `air.normalize_function_constant_predicate.(x)`: 1.0 when `x` is a known non-zero - /// float, 0.0 when it is zero or undefined (`None`: a value computed from an unsupplied - /// function constant). `None` where `x` is no float or `ty` is neither `half` nor `float`. + /// `air.normalize_function_constant_predicate.(x)`: 1.0 when `x`'s bits are not all + /// zero, 0.0 when they are (`+0.0`) or `x` is undefined (`None`: a value computed from an + /// unsupplied function constant). By the bits, as Metal keeps what the predicate gates: `-0.0` + /// and a NaN are on, as 1.5 is. `None` where `x` is no float constant or `ty` is neither `half` + /// nor `float`. pub fn normalize_predicate_float( &mut self, ty: TypeId, @@ -344,11 +346,9 @@ impl Folder<'_> { let nonzero = match x { None => false, Some(c) => match self.value(c) { - Some(Const::Undef | Const::Poison) => false, - _ => match self.float(c) { - Some(v) => v != 0.0, - None => return Ok(None), - }, + Some(Const::Undef | Const::Poison | Const::Null) => false, + Some(Const::Float(bits)) => *bits != 0, + _ => return Ok(None), }, }; self.mk(ty, Const::Float(if nonzero { one } else { 0 }), m) diff --git a/crates/metal2vulkan-air/src/canon/interface.rs b/crates/metal2vulkan-air/src/canon/interface.rs index 053986f02..9ff2dfced 100644 --- a/crates/metal2vulkan-air/src/canon/interface.rs +++ b/crates/metal2vulkan-air/src/canon/interface.rs @@ -122,22 +122,23 @@ impl Values<'_> { ) } - /// Whether the floating-point constant global `g` holds is non-zero, by value as the - /// predicate's normalization compares it (`-0.0` is zero, a NaN is not); an unsupplied one's - /// is not. A global of neither `half` nor `float` type is not evaluable. + /// Whether the floating-point constant global `g` holds has a bit set, as Metal reads a float + /// predicate (`-0.0` and a NaN are non-zero, as the predicate's normalization makes them); an + /// unsupplied one's is not. A global of neither `half` nor `float` type is not evaluable. fn float_nonzero(&self, g: GlobalId) -> Result { let global = self.globals.get(g).ok_or_else(not_evaluable)?; - let magnitude = match self.types.get(global.value_ty) { - Some(Type::Half) => 0x7fff, - Some(Type::Float) => 0x7fff_ffff, - _ => return Err(not_evaluable()), - }; + if !matches!( + self.types.get(global.value_ty), + Some(Type::Half | Type::Float) + ) { + return Err(not_evaluable()); + } if self.fcs.get(&g).is_some_and(|f| !f.supplied) { return Ok(false); } let init = global.init.and_then(|c| self.consts.get(c)); match init.map(|c| &c.value) { - Some(Const::Float(bits)) => Ok((bits & magnitude) != 0), + Some(Const::Float(bits)) => Ok(*bits != 0), _ => Err(not_evaluable()), } } @@ -159,7 +160,8 @@ impl Values<'_> { fn present(&self, p: &Option) -> Result { match p { // A function constant that is not a `bool`, of floating-point type, is normalized to - // 1.0 where it is supplied and non-zero and to 0.0 otherwise. + // 1.0 where it is supplied and its bits are not all zero (`-0.0` and a NaN included) + // and to 0.0 otherwise. Some(p) if self.holds_float(p.global) => self.float_nonzero(p.global), Some(p) => Ok(self.int(p.global)? != 0), None => Ok(true), diff --git a/crates/metal2vulkan-air/src/canon/tests.rs b/crates/metal2vulkan-air/src/canon/tests.rs index fba51397a..c4fa56f35 100644 --- a/crates/metal2vulkan-air/src/canon/tests.rs +++ b/crates/metal2vulkan-air/src/canon/tests.rs @@ -1472,8 +1472,13 @@ fn a_float_function_constant_gates_a_parameter_where_it_is_supplied_and_non_zero "non-zero is on, {at}" ); assert_eq!(kept(&[(0, float_constant(0.0))]), [0], "zero is off, {at}"); - // By value, as the normalization compares: -0.0 is zero. - assert_eq!(kept(&[(0, float_constant(-0.0))]), [0], "-0.0 is off, {at}"); + // By its bits, as Metal reads it (an authored case's gated sample mask on the Apple M3 + // Max, job m3-max-75714): -0.0 keeps the argument. + assert_eq!( + kept(&[(0, float_constant(-0.0))]), + [0, 1], + "-0.0 is on, {at}" + ); assert_eq!( kept(&[(0, float_constant(f32::NAN))]), [0, 1], @@ -1504,8 +1509,8 @@ fn a_half_function_constant_gates_a_parameter_where_it_is_supplied_and_non_zero( ); assert_eq!( kept(&[(0, half_constant(0x8000))]), - [0], - "-0.0 is off, {at}" + [0, 1], + "-0.0 is on, {at}" ); assert_eq!( kept(&[(0, half_constant(0x7e00))]), diff --git a/crates/metal2vulkan-air/src/intrinsics/stage.rs b/crates/metal2vulkan-air/src/intrinsics/stage.rs index ec72701b1..364c35a0e 100644 --- a/crates/metal2vulkan-air/src/intrinsics/stage.rs +++ b/crates/metal2vulkan-air/src/intrinsics/stage.rs @@ -93,8 +93,8 @@ pub enum FunctionConstant { /// `air.is_function_constant_defined`: `i1 (ptr addrspace(2))`, the constant's global. IsDefined, /// `air.normalize_function_constant_predicate.`: `iN (iN)`; of a float constant, `.f16` or - /// `.f32`: the same over a float, 1.0 where it is a known non-zero and 0.0 where it is zero or - /// undefined. + /// `.f32`: the same over a float, 1.0 where its bits are not all zero (`-0.0` and a NaN + /// included) and 0.0 where it is `+0.0` or undefined. NormalizePredicate(Elem), /// The texture operation `t` (a read, sample, gather, query, or atomic) of a texture argument /// the pipeline's function constants removed, or through a sampler argument they removed, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 807c912ae..83cfca5bb 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 = 2306; +pub const SEMANTICS_VERSION: u32 = 2307; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another