Translator: a float predicate is non-zero by its bits, as Metal reads it

Metal's answer, from lane fcfloat's authored case (our MSL fixture
fc_float_gate: a fragment output's [[sample_mask]] gated by a float
function constant, so whether the output is present shows in the target;
Apple M3 Max, job m3-max-75714, three runs each):
  s = 1.5 present, +0.0 absent, -0.0 (0x80000000) PRESENT,
  quiet NaN (0x7fc00000) present, unsupplied absent.
So Metal keeps what a float predicate gates wherever the supplied value's
bits are not all zero. 267d3163a6's fold compared values (v != 0.0) and so
dropped -0.0's argument; 8f84a9c4fd then made float_nonzero compare values
too. Both now compare bits: normalize_predicate_float gives 1.0 for any
float constant with a bit set (-0.0 and NaN included) and 0.0 for +0.0,
null or undefined; float_nonzero is true for any bit set. A body that reads
the constant itself (if (s)) still reads -0.0 as false: Metal's kernel
variant wrote its sentinel there, and the body's compare is not this path.

SEMANTICS_VERSION 2307: the outcome for a -0.0 float or half predicate
moved again under 2306 (never published).

Tests: the specialization tests' -0.0 rows (float and half, normalized and
unnormalized) now expect the argument kept.
This commit is contained in:
Anees Iqbal committed 2026-11-26 17:18:49 -08:00
1 parent a395153366
commit e2b5b29c07
5 files changed
+32 -25

No files matched your search

+8 -8
View File
@@ -327,9 +327,11 @@ impl Folder<'_> {
self.mk_int(ty, v, m)
}
/// `air.normalize_function_constant_predicate.<f16|f32>(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.<f16|f32>(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)
+12 -10
View File
@@ -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<bool, Error> {
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<Predicate>) -> Result<bool, Error> {
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),
+9 -4
View File
@@ -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))]),
@@ -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 (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,
+1 -1
View File
@@ -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