Translator: a float predicate is half or float, and compared by value

Two follow-ups to 267d3163a6 (a float function constant gates an argument):

- The ABI check took a double predicate global too, but
  air.normalize_function_constant_predicate parses only .f16 and .f32 and
  the fold normalizes only half and float. A double predicate is now
  refused at the check (AbiError::BadValue at air.function_constant), as
  any other unhandled type is: unknown stays unknown.
- float_nonzero compared the global's bits with 0, so a predicate global
  holding -0.0 kept its argument, where the fold compares values and gives
  -0.0 the zero predicate. It now compares by value for the global's type
  (the sign bit masked: -0.0 is zero, a NaN is not), and a float predicate
  of any other type is not evaluable (a typed refusal) instead of absent.
  Values carries the type table for it.

Tests: the ABI test refuses a double predicate; the specialization test
runs the normalized shape and a shape that stores the constant unnormalized,
each with 1.5, 0.0, -0.0, NaN and unsupplied. Without the fixes the double
case and the unnormalized -0.0 case fail.
This commit is contained in:
Anees Iqbal committed 2026-11-26 17:04:59 -08:00
1 parent 7ccba50e38
commit 8f84a9c4fd
5 files changed
+60 -20

No files matched your search

+4 -2
View File
@@ -365,7 +365,9 @@ fn number_or_vector(m: &Module, ty: TypeId) -> bool {
/// A function-constant predicate names the global the static initializer stores the predicate
/// into: a defined, writable, non-externally-initialized constant-space global of integer type
/// (the frontend stores `bool` predicates as `i8`, and a few as `i16` or `i32`).
/// (the frontend stores `bool` predicates as `i8`, and a few as `i16` or `i32`), or of `half` or
/// `float` type for a constant of that type (the two float types
/// `air.normalize_function_constant_predicate` takes).
fn predicate(m: &Module, p: Option<&Predicate>, site: &Site) -> Result<(), Error> {
let Some(p) = p else {
return Ok(());
@@ -378,7 +380,7 @@ fn predicate(m: &Module, p: Option<&Predicate>, site: &Site) -> Result<(), Error
&& g.init.is_some()
&& matches!(
m.types().get(g.value_ty),
Some(Type::Int(_) | Type::Half | Type::Float | Type::Double)
Some(Type::Int(_) | Type::Half | Type::Float)
)
});
if ok {
+6
View File
@@ -1072,6 +1072,12 @@ fn predicates_name_a_static_initializer_integer_or_float_global() {
refused(kernel_with_predicate(|b| {
fc_global(b, "fc0", Some(Section::FcInitializer))
}));
// A `double`: the predicate's normalization takes `half` and `float` only.
refused(kernel_with_predicate(|b| {
let t = b.ty(Type::Double);
let zero = b.constant(t, Const::Float(0));
b.simple_global("pred", t, AddrSpace::Constant, Some(zero))
}));
// Not a number.
refused(kernel_with_predicate(|b| {
let t = b.ptr(AddrSpace::Device);
+19 -7
View File
@@ -59,6 +59,7 @@ use crate::budget::Meter;
use crate::error::{AbiError, CanonError, DiagName, InputError, InputErrorKind, Location};
use crate::intrinsics::{AbsentAccess, AtomicSpace, NumTy, Texture};
use crate::intrinsics::{AirIntrinsic, FunctionConstant, LlvmIntrinsic, Operand, TexOp, TexRole};
use crate::ir::TypeTable;
use crate::ir::{Arena, ArgIdx, Body, Callee, Const, ConstId, ConstTable, Constant, FuncId};
use crate::ir::{CastOp, FastMath, Inst, TailKind, TypeLayouts, Value};
use crate::ir::{Global, GlobalId, Idx, InstId, InstKind, Layout, ModuleParts, Type, TypeId};
@@ -75,6 +76,7 @@ use private::Private;
/// Read-only view of the globals once they hold their pipeline values.
pub(super) struct Values<'a> {
pub types: &'a TypeTable,
pub globals: &'a Arena<GlobalId, Global>,
pub consts: &'a ConstTable,
/// Function constants and the globals derived from them.
@@ -120,14 +122,24 @@ impl Values<'_> {
)
}
/// Whether the floating-point constant global `g` holds is non-zero; an unsupplied one's is
/// not.
fn float_nonzero(&self, g: GlobalId) -> bool {
/// 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.
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 self.fcs.get(&g).is_some_and(|f| !f.supplied) {
return false;
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),
_ => Err(not_evaluable()),
}
let init = self.globals.get(g).and_then(|g| g.init);
matches!(init.and_then(|c| self.consts.get(c)).map(|c| &c.value), Some(Const::Float(b)) if *b != 0)
}
fn resolve(&self, v: &mut AbiInt) -> Result<(), Error> {
@@ -148,7 +160,7 @@ impl Values<'_> {
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.
Some(p) if self.holds_float(p.global) => Ok(self.float_nonzero(p.global)),
Some(p) if self.holds_float(p.global) => self.float_nonzero(p.global),
Some(p) => Ok(self.int(p.global)? != 0),
None => Ok(true),
}
+2
View File
@@ -835,6 +835,7 @@ pub fn specialize_dynamic(
// ABI resolution, the absent arguments' values, then pruning.
let values = interface::Values {
types: &parts.types,
globals: &parts.globals,
consts: &parts.consts,
fcs: &fcs,
@@ -1315,6 +1316,7 @@ pub fn static_values(
);
}
let values = interface::Values {
types: &parts.types,
globals: &parts.globals,
consts: &parts.consts,
fcs: &fcs,
+29 -11
View File
@@ -1405,8 +1405,9 @@ fn float_constant(v: f32) -> ConstValue {
/// Apple's OpenGL front end's shape for a function constant that is a `float` (a point size):
/// the static initializer stores the constant's normalized predicate, a `float`, into `pred`,
/// which gates parameter 1.
fn float_predicate() -> (Module, AirAbi) {
/// which gates parameter 1. With `normalized` false it stores the constant itself, so `pred`
/// holds the value as supplied.
fn float_predicate(normalized: bool) -> (Module, AirAbi) {
let mut m = ModuleBuilder::new();
let void = m.void();
let ft = m.float();
@@ -1417,7 +1418,10 @@ fn float_predicate() -> (Module, AirAbi) {
let (size_addr, pred_addr) = (m.global_addr(size), m.global_addr(pred));
static_init(&mut m, "init", |f| {
let v = load(f, ft, size_addr);
let p = normalize_f32(f, ft, v);
let p = match normalized {
true => normalize_f32(f, ft, v),
false => v,
};
store(f, p, pred_addr);
});
let fty = m.fn_ty(void, &[dev, dev]);
@@ -1441,14 +1445,28 @@ fn float_predicate() -> (Module, AirAbi) {
#[test]
fn a_float_function_constant_gates_a_parameter_where_it_is_supplied_and_non_zero() {
let kept = |constants: &[(u32, ConstValue)]| -> Vec<u32> {
let (module, abi) = float_predicate();
let e = specialize(module, &abi, &request(constants), &mut meter()).unwrap();
e.ep.params.iter().map(|p| p.param).collect()
};
assert_eq!(kept(&[(0, float_constant(1.5))]), [0, 1], "non-zero is on");
assert_eq!(kept(&[(0, float_constant(0.0))]), [0], "zero is off");
assert_eq!(kept(&[]), [0], "unsupplied is off");
for normalized in [true, false] {
let kept = |constants: &[(u32, ConstValue)]| -> Vec<u32> {
let (module, abi) = float_predicate(normalized);
let e = specialize(module, &abi, &request(constants), &mut meter()).unwrap();
e.ep.params.iter().map(|p| p.param).collect()
};
let at = format!("normalized {normalized}");
assert_eq!(
kept(&[(0, float_constant(1.5))]),
[0, 1],
"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}");
assert_eq!(
kept(&[(0, float_constant(f32::NAN))]),
[0, 1],
"NaN is on, {at}"
);
assert_eq!(kept(&[]), [0], "unsupplied is off, {at}");
}
}
#[test]