diff --git a/crates/metal2vulkan-air/src/canon/interface.rs b/crates/metal2vulkan-air/src/canon/interface.rs index 9ff2dfced..57b52b0e1 100644 --- a/crates/metal2vulkan-air/src/canon/interface.rs +++ b/crates/metal2vulkan-air/src/canon/interface.rs @@ -113,6 +113,16 @@ impl Values<'_> { } } + /// What [`static_values`](super::static_values) answers for global `g`: a `half` or `float` + /// predicate's truth as 1 or 0, by its bits as [`Self::present`] reads it, else the integer it + /// holds ([`Self::int`]). + pub(super) fn static_value(&self, g: GlobalId) -> Result { + match self.holds_float(g) { + true => self.float_nonzero(g).map(u64::from), + false => self.int(g), + } + } + /// Whether global `g`'s initializer is a floating-point constant. fn holds_float(&self, g: GlobalId) -> bool { let init = self.globals.get(g).and_then(|g| g.init); diff --git a/crates/metal2vulkan-air/src/canon/mod.rs b/crates/metal2vulkan-air/src/canon/mod.rs index 03cd24a15..47a7a9d42 100644 --- a/crates/metal2vulkan-air/src/canon/mod.rs +++ b/crates/metal2vulkan-air/src/canon/mod.rs @@ -1277,9 +1277,13 @@ fn recursion(body: &Body, parts: &ModuleParts, name: &DiagName) -> Error { /// undeclared index or a value of another type is `InvalidRequest`). With no `globals` the /// static initializers are not run: only the constants are checked. /// +/// A `half` or `float` global, the predicate of a function constant of that type, answers its +/// truth as the predicate gates: 1 where its bits are not all zero (`-0.0` and a NaN included), 0 +/// where they are or the constant is unsupplied. +/// /// Each entry of the result answers the global at the same position: `NeedsInput /// (FunctionConstant)` when it is computed from an unsupplied constant, and -/// `Unsupported{StaticInitializer}` when it does not hold an integer or is computed through a +/// `Unsupported{StaticInitializer}` when it holds neither an integer nor such a predicate or is computed through a /// pure operation the interpreter does not evaluate (float arithmetic outside the operations it /// models). Such an operation taints only the globals it reaches: a global computed without it /// still resolves. A branch on its result, an address computed from it, or any other construct the @@ -1325,7 +1329,7 @@ pub fn static_values( .iter() .map(|&g| match evaluated.opaque.contains(&g) { true => Err(interface::not_evaluable()), - false => values.int(g), + false => values.static_value(g), }) .collect()) } diff --git a/crates/metal2vulkan-air/src/canon/tests.rs b/crates/metal2vulkan-air/src/canon/tests.rs index c4fa56f35..560fcaeae 100644 --- a/crates/metal2vulkan-air/src/canon/tests.rs +++ b/crates/metal2vulkan-air/src/canon/tests.rs @@ -1607,6 +1607,42 @@ fn static_values_resolve_abi_globals_without_canonicalizing_a_body() { ); } +#[test] +fn static_values_answer_a_float_predicate_by_its_bits() { + for half in [false, true] { + let (module, abi) = float_predicate(half, true); + let pred = abi.entries[0].params[1].predicate.as_ref().unwrap().global; + let value = |constants: BTreeMap| { + static_values(module.clone(), &abi, &constants, &[pred], &mut meter()).unwrap() + }; + let c = |f: f32, h: u16| match half { + true => half_constant(h), + false => float_constant(f), + }; + assert_eq!( + value([(0, c(1.5, 0x3e00))].into()), + [Ok(1)], + "1.5, half {half}" + ); + assert_eq!( + value([(0, c(0.0, 0x0000))].into()), + [Ok(0)], + "0.0, half {half}" + ); + assert_eq!( + value([(0, c(-0.0, 0x8000))].into()), + [Ok(1)], + "-0.0, half {half}" + ); + assert_eq!( + value([(0, c(f32::NAN, 0x7e00))].into()), + [Ok(1)], + "NaN, half {half}" + ); + assert_eq!(value(BTreeMap::new()), [Ok(0)], "unsupplied, half {half}"); + } +} + /// A module whose static initializer computes `pred` from function constant 0 and `bits` through /// float arithmetic the interpreter does not evaluate (`frem`, then a bitcast), with `branch` /// choosing whether it then branches on the float result.