Read an i8 [[front_facing]] argument as the face, 1 or 0

What: a fragment argument whose `air.front_facing` metadata names the
builtin and whose type is an integer scalar (an older front end's ABI:
metalfe-32023.884 at air64-apple-ios13.0.0-simulator passes
`bool [[front_facing]]` as `i8`, read through `trunc i8 %0 to i1`) now reads
the FrontFacing builtin and holds it as 1 or 0 in the argument's integer
form (a select of the loaded bool). Before, `input_builtin` took the
argument only as a `bool`, so the i8 form was refused
`NotImplemented(StageIo)` at the argument's metadata. The decision is the
argument's type with the builtin's metadata; no name is read. The
`i1 zeroext` form is unchanged.

Test: tests::engine_front_facing_byte, a module written here (today's
compiler emits no such shape and regen-air-fixtures cannot make one) with
`ff_bool` (i1 zeroext) and `ff_byte` (i8 + trunc) side by side: both
translate, link and pass spirv-val on every committed profile with no
precision note; ff_byte's module selects 1 or 0 on the FrontFacing load,
ff_bool's selects nothing on it. Without the fix ff_byte is refused.

(cherry picked from commit 0950743006c6011c5c136f80f4b48d2d0a43b392)
This commit is contained in:
Anees Iqbal committed 2026-11-09 05:04:59 -08:00
1 parent fc668f8ec6
commit 7c6e9d1fcf
4 files changed
+145 -2

No files matched your search

+20 -1
View File
@@ -1064,6 +1064,19 @@ fn bool_as_uint(cx: &EmitCx<'_>, link: &Link) -> bool {
)
}
/// Whether `link` is a `bool` builtin (`FrontFacing`) read into an integer argument, as an older
/// front end's ABI passes `[[front_facing]]` (`i8`): the argument holds 1 or 0.
fn bool_builtin_as_int(cx: &EmitCx<'_>, link: &Link) -> bool {
matches!(link.form.held, Held::Int { .. })
&& matches!(
cx.plan.families.stage_io.objects.get(link.object),
Some(IoObject::Builtin {
shape: Shape::Bool,
..
})
)
}
/// The `uint` form with the lanes of `form`.
fn uint_lanes(form: Form) -> Form {
form.with(Held::Int {
@@ -1074,7 +1087,8 @@ fn uint_lanes(form: Form) -> Form {
/// `x`, a value loaded through `link`, as the lowered representation of its form: a narrow
/// integer masked to its width, a narrow float rounded to its format, a `bool` carried as `uint`
/// compared with 0, the weights of a [`Place::Weights`] read.
/// compared with 0, a `bool` builtin read into an integer as 1 or 0, the weights of a
/// [`Place::Weights`] read.
fn to_lowered(
cx: &EmitCx<'_>,
fb: &mut FunctionBuilder<'_>,
@@ -1087,6 +1101,11 @@ fn to_lowered(
let zero = ops::splat(fb, uint_lanes(form), 0)?;
return fb.binary(BinOp::INotEqual, x, zero);
}
if bool_builtin_as_int(cx, link) {
let one = ops::splat(fb, form, 1)?;
let zero = ops::splat(fb, form, 0)?;
return fb.select(x, one, zero);
}
let x = ops::mask(fb, form, x)?;
narrow::round(fb, form, x)
}
@@ -2026,9 +2026,11 @@ fn input_builtin(
None,
)
}
// A `bool` argument, or an integer one an older front end's ABI passes it in (`i8`,
// truncated to `i1` where read): the face as 1 or 0 (`to_lowered`).
Bi::FrontFacing if !vertex => {
if f.held != Held::Bool || f.lanes != 1 {
return Err(not_lowered());
int_scalar(f)?;
}
(
B::FrontFacing,
@@ -0,0 +1,121 @@
//! `[[front_facing]]` in an older front end's ABI: Apple's compiler at an iOS 13 simulator target
//! (metalfe-32023.884) passes a fragment's `bool [[front_facing]]` argument as `i8`, read through
//! `trunc i8 %0 to i1`, where today's passes `i1 zeroext`. The argument's `air.front_facing`
//! metadata names the builtin and its type says how it is held, so either form reads the
//! fragment's face: the `i8` one as 1 or 0. Today's compiler emits no such module and
//! `scripts/regen-air-fixtures` cannot make one, so the module is written here: both forms side
//! by side. `llvm-as` assembles; a missing tool skips visibly. No GPU is used.
use crate::pipeline::testing::{compile, link_and_validate, llvm_assemble_text, AssembleError};
use crate::spirv::testing::committed_profiles;
/// Two fragment functions of one module, each answering `float4(1)` for a front face and
/// `float4(0)` for a back one: `ff_bool` takes the face as `i1 zeroext`, `ff_byte` as `i8`.
const MODULE: &str = r#"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64-apple-ios13.0.0-simulator"
define <4 x float> @ff_bool(i1 zeroext %0) {
%r = select i1 %0, <4 x float> <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, <4 x float> zeroinitializer
ret <4 x float> %r
}
define <4 x float> @ff_byte(i8 %0) {
%b = trunc i8 %0 to i1
%r = select i1 %b, <4 x float> <float 1.000000e+00, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>, <4 x float> zeroinitializer
ret <4 x float> %r
}
!air.fragment = !{!0, !5}
!air.compile_options = !{!6, !7}
!air.version = !{!8}
!air.language_version = !{!9}
!0 = !{ptr @ff_bool, !1, !3}
!1 = !{!2}
!2 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"float4"}
!3 = !{!4}
!4 = !{i32 0, !"air.front_facing", !"air.arg_type_name", !"bool", !"air.arg_name", !"f"}
!5 = !{ptr @ff_byte, !1, !3}
!6 = !{!"air.compile.denorms_disable"}
!7 = !{!"air.compile.fast_math_enable"}
!8 = !{i32 2, i32 2, i32 0}
!9 = !{!"Metal", i32 2, i32 2, i32 0}
"#;
/// Each `OpSelect` of `words` whose condition is a load of the `FrontFacing` builtin and whose
/// operands are integer constants, as those constants' values.
fn front_facing_reads(words: &[u32]) -> Vec<(u64, u64)> {
use crate::spirv_binary::{parse_bytes, Operand};
use crate::spv::{BuiltIn, Decoration, Op};
let bytes: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let parsed = parse_bytes(&bytes).expect("parse");
let face: Vec<u32> = (parsed.instructions.iter())
.filter(|i| i.opcode == Op::Decorate)
.filter_map(|i| match i.operands.as_slice() {
[Operand::IdRef(id), Operand::Word(d), Operand::Word(b)]
if *d == Decoration::BuiltIn as u32 && *b == BuiltIn::FrontFacing as u32 =>
{
Some(*id)
}
_ => None,
})
.collect();
assert_eq!(face.len(), 1, "one FrontFacing variable");
let loads: Vec<u32> = (parsed.instructions.iter())
.filter(|i| i.opcode == Op::Load && i.operands.first() == Some(&Operand::IdRef(face[0])))
.filter_map(|i| i.result_id)
.collect();
let constant = |id: u32| -> Option<u64> {
let i = parsed
.instructions
.iter()
.find(|i| i.result_id == Some(id))?;
match (i.opcode, i.operands.as_slice()) {
(Op::Constant, [Operand::Word(v)]) => Some(u64::from(*v)),
(Op::Constant, [Operand::Wide(v)]) => Some(*v),
(Op::ConstantNull, []) => Some(0),
_ => None,
}
};
// Each select on a load of the face, as its two constant operands.
(parsed.instructions.iter())
.filter(|i| i.opcode == Op::Select)
.filter_map(|i| match i.operands.as_slice() {
[Operand::IdRef(c), Operand::IdRef(a), Operand::IdRef(b)] if loads.contains(c) => {
Some((constant(*a)?, constant(*b)?))
}
_ => None,
})
.collect()
}
/// An `i8` `[[front_facing]]` argument translates, links and validates on every committed
/// profile, as the `i1 zeroext` one does: the module reads the `FrontFacing` builtin and holds
/// the argument as 1 for a front face and 0 for a back one, which `trunc` reads as the face.
#[test]
fn an_i8_front_facing_argument_reads_the_face_as_one_or_zero() {
let bc = match llvm_assemble_text(MODULE) {
Ok(bc) => bc,
Err(AssembleError::Missing(why)) => {
eprintln!("SKIPPED: {why}");
return;
}
Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the module: {why}"),
};
let profiles = committed_profiles().expect("profiles");
for (stem, p) in &profiles {
for entry in ["ff_bool", "ff_byte"] {
let a = compile(&bc, entry, p).unwrap_or_else(|e| panic!("{entry} on {stem}: {e:?}"));
assert_eq!(a.precision, [], "{entry} on {stem}");
let (_, words) =
link_and_validate(&a, p).unwrap_or_else(|e| panic!("{entry} on {stem}: {e}"));
let selects = front_facing_reads(&words);
if entry == "ff_byte" {
assert_eq!(selects, [(1, 0)], "{entry} on {stem}");
} else {
assert_eq!(selects, [], "{entry} on {stem}");
}
}
}
}
+1
View File
@@ -60,6 +60,7 @@ mod engine_fpc_frames;
mod engine_fragment;
mod engine_fragment_depth_clamp;
mod engine_fragment_imageblock;
mod engine_front_facing_byte;
mod engine_function_constant_interface;
mod engine_function_constant_pointers;
mod engine_gated_texture_slots;