Reject a builtin parameter the emitter cannot wire

`position`, `point_coord` and `front_facing` each declare a SPIR-V builtin of a
fixed shape, and a parameter of any other shape was bound to a zero. That put
every fragment of a mistyped `[[position]]` at the origin and called every
triangle of a mistyped `[[front_facing]]` back-facing -- in a module that
validated, bound the same descriptors and reflected as though the builtin were
connected.

The branch is unreachable on real AIR: Metal fixes each attribute's type, and
over 2880 corpus sources nothing changes by a byte. It rejects now so the silent
zero cannot come back.

One authored fixture declared `[[position]]` as a scalar `float` while testing
something else entirely; it now says `float4`, which is what AIR would say.
This commit is contained in:
Anees Iqbal committed 2026-09-01 03:00:44 +03:00
1 parent bbd66ec226
commit 74c0054375
3 files changed
+85 -8

No files matched your search

+3 -2
View File
@@ -749,8 +749,9 @@ source_filename = "synth_multi_return_fragment"
target datalayout = "e-p:64:64:64"
target triple = "air64-apple-macosx14.0.0"
define <4 x float> @frag(float %x) local_unnamed_addr #0 {
define <4 x float> @frag(<4 x float> %pos) local_unnamed_addr #0 {
entry:
%x = extractelement <4 x float> %pos, i32 0
%cond = fcmp olt float %x, 0.000000e+00
br i1 %cond, label %early, label %late
@@ -772,7 +773,7 @@ attributes #0 = { nounwind }
!1 = !{!2}
!2 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"float4"}
!3 = !{!4}
!4 = !{i32 0, !"air.position", !"air.center", !"air.arg_type_name", !"float"}
!4 = !{i32 0, !"air.position", !"air.center", !"air.arg_type_name", !"float4"}
"#;
let tmp = std::env::temp_dir().join(format!(
"metal2vulkan_native_fragment_multi_return_{}",
+12 -6
View File
@@ -1661,8 +1661,10 @@ pub(super) fn build_stage_input(
};
bindings.push((*pid, ParamBinding::LoadVar { var, ty: v4 }));
} else {
let z = ctx.const_zero(*pty, &defs);
bindings.push((*pid, ParamBinding::ZeroValue { val: z }));
return Err(format!(
"[[position]] parameter {idx} is not a float4; FragCoord is a 4-component \
float builtin and reading a zero in its place puts the fragment at the origin"
));
}
} else if role_is("point_coord") {
let v2 = ctx.ty_vecf(2);
@@ -1685,8 +1687,10 @@ pub(super) fn build_stage_input(
};
bindings.push((*pid, ParamBinding::LoadVar { var, ty: v2 }));
} else {
let z = ctx.const_zero(*pty, &defs);
bindings.push((*pid, ParamBinding::ZeroValue { val: z }));
return Err(format!(
"[[point_coord]] parameter {idx} is not a float2; PointCoord is a 2-component \
float builtin"
));
}
} else if role_is("barycentric_coord") {
// `[[barycentric_coord]]` is the primitive's barycentric weights at this fragment.
@@ -1757,8 +1761,10 @@ pub(super) fn build_stage_input(
};
bindings.push((*pid, ParamBinding::LoadVar { var, ty: bool_ty }));
} else {
let z = ctx.const_zero(*pty, &defs);
bindings.push((*pid, ParamBinding::ZeroValue { val: z }));
return Err(format!(
"[[front_facing]] parameter {idx} is not a bool; FrontFacing is a boolean \
builtin and reading a zero in its place claims every triangle is back-facing"
));
}
} else if role_is("primitive_id") {
let uint_ty = ctx.ty_uint();
+70
View File
@@ -471,6 +471,76 @@ entry:
);
}
/// A builtin entry parameter whose type is not the builtin's type.
///
/// The third face of the same hole. `position`, `point_coord` and `front_facing` each declare a
/// SPIR-V builtin of a fixed shape; a parameter of any other shape used to be bound to a zero, so a
/// `[[position]]` the emitter could not wire put every fragment at the origin and a
/// `[[front_facing]]` it could not wire called every triangle back-facing — in a module that
/// validated and reflected as though the builtin were connected.
///
/// Unlike an unmodelled role, this branch is unreachable on real AIR: Metal fixes each attribute's
/// type, and over 2880 corpus sources no module takes it. So the rejection costs nothing and exists
/// to keep the silent zero from coming back.
#[test]
fn a_builtin_parameter_of_the_wrong_type_fallbacks() {
const FRAGMENT: &str = r#"target triple = "air64_v28-apple-macosx26.5.0"
define <4 x float> @frag(TYPE %sys) {
entry:
%v0 = insertelement <4 x float> undef, float 0.000000e+00, i32 0
%v1 = insertelement <4 x float> %v0, float 0.000000e+00, i32 1
%v2 = insertelement <4 x float> %v1, float 0.000000e+00, i32 2
%v3 = insertelement <4 x float> %v2, float 1.000000e+00, i32 3
ret <4 x float> %v3
}
!air.fragment = !{!0}
!0 = !{ptr @frag, !1, !3}
!1 = !{!2}
!2 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"float4"}
!3 = !{!4}
!4 = !{i32 0, !"air.ROLE", !"air.center", !"air.arg_type_name", !"NAME"}
"#;
let build = |role: &str, name: &str, ty: &str| {
FRAGMENT
.replace("air.ROLE", &format!("air.{role}"))
.replace("NAME", name)
.replace("TYPE", ty)
};
// Each attribute's own type translates.
for (role, name, ty) in [
("position", "float4", "<4 x float>"),
("point_coord", "float2", "<2 x float>"),
("front_facing", "bool", "i1"),
] {
assert!(
translate_sanitized_native(&build(role, name, ty), Stage::Fragment, &tmp()).is_ok(),
"[[{role}]] of type {name} must translate"
);
}
// Any other type has no wiring, and a zero in its place is a lie about the geometry.
for (role, name, ty, needle) in [
("position", "float2", "<2 x float>", "float4"),
("point_coord", "float4", "<4 x float>", "float2"),
("front_facing", "uint", "i32", "bool"),
] {
match translate_sanitized_native(&build(role, name, ty), Stage::Fragment, &tmp()) {
Ok(spv) => panic!(
"expected a FALLBACK for [[{role}]] typed {name}, got {} bytes",
spv.len()
),
Err(e) => assert!(
e.contains(role) && e.contains(needle),
"the diagnostic should name the attribute and the type it needs; got: {e}"
),
}
}
}
#[test]
fn no_function_definitions_fallbacks() {
assert_fallback(