Read the embedded-texture use gate over the entry, not the module

The steps this packet landed as, in order:
- Keep EmbeddedTexture's doc off the now-private use gate

Content: the tree of `development` at 3dafee516a, unchanged.
This commit is contained in:
Anees Iqbal committed 2026-10-26 13:19:19 +02:00
1 parent 0c40e20539
commit 553e7984cf
7 files changed
+162 -12

No files matched your search

@@ -16,7 +16,7 @@ mod textures;
pub(crate) mod types;
mod vertex;
use embedded::{
body_uses_texture_intrinsic, detect_embedded_arguments, detect_embedded_textures,
detect_embedded_arguments, detect_embedded_textures, entry_uses_texture_intrinsic,
unsurfaced_embedded_resources, ArgumentBuffers,
};
pub use embedded::{ArgumentBufferResourceKind, EmbeddedArgument, EmbeddedTexture};
@@ -35,8 +35,9 @@ impl ArgumentBuffers {
}
/// A texture that lives inside an `air.indirect_buffer` argument buffer, marked
/// `air.indirect_argument` → `air.texture` in the buffer's `air.struct_type_info`, and used by the
/// shader body through an AIR texture intrinsic.
/// `air.indirect_argument` → `air.texture` in the buffer's `air.struct_type_info`, and used
/// through an AIR texture intrinsic by the code the reflected entry reaches: another entry of the
/// module declares the same member of the same buffer and surfaces none of it.
///
/// The translator materializes a synthetic UniformConstant sampled image for it and the validation
/// harness binds the SAME deterministically-seeded texture on both the Apple-oracle side (encoded
@@ -98,10 +99,32 @@ fn embedded_synthetic_texture_index(texture_locations: &[u32]) -> u32 {
texture_locations.iter().copied().max().map_or(0, |m| m + 1)
}
/// True iff the shader body calls an AIR texture intrinsic. These are stable AIR intrinsic
/// Whether the code `entry` reaches calls an AIR texture intrinsic, the gate on surfacing the
/// embedded argument-buffer textures of that entry's interface.
///
/// It reads the text the entry reaches (its projection), never the module's. An argument buffer's
/// layout belongs to its struct type, so every entry taking that buffer declares the same texture
/// member, while the image that binds one is materialized only where the entry's own body loads
/// the handle and hands it to a texture intrinsic -- `lower` walks one entry's function. Reading
/// the whole module's text therefore charges one entry's sample to another that never loads the
/// handle, and surfaces a descriptor the module translated for that entry does not declare.
/// Where the text has no projection, the module's calls, as before.
pub(super) fn entry_uses_texture_intrinsic(ll: &str, entry: Option<&str>) -> bool {
if !body_uses_texture_intrinsic(ll) {
return false;
}
let Some(entry) = entry else { return true };
match crate::air::entry_projection(ll, entry) {
Ok(reached) => body_uses_texture_intrinsic(&reached),
Err(_) => true,
}
}
/// True iff `ll` calls an AIR texture intrinsic. These are stable AIR intrinsic
/// symbols, not shader identifiers. A declaration line is only emitted when the body actually calls it,
/// so its presence in the `.ll` text is a sound structural signal.
pub(super) fn body_uses_texture_intrinsic(ll: &str) -> bool {
/// so its presence in the `.ll` text is a sound structural signal. The reader is
/// [`entry_uses_texture_intrinsic`], which scopes `ll` to one entry first.
fn body_uses_texture_intrinsic(ll: &str) -> bool {
ll.contains("@air.sample_")
|| ll.contains("@air.gather_")
|| ll.contains("@air.read_texture")
@@ -13,6 +13,39 @@ fn embedded_texture_use_gate_includes_sampling_and_gathering() {
));
}
/// The gate is the reflected entry's, not the module's: an argument buffer's texture member is
/// declared by every entry that takes the buffer, so the entry whose own reachable code samples is
/// the one that surfaces it. A module declaring one entry answers for that entry, and a text with
/// no projection answers as the module's calls do.
#[test]
fn the_texture_use_gate_reads_only_what_the_entry_reaches() {
let two_entries = r#"define void @samples() {
%v = tail call <4 x float> @air.sample_texture_2d.v4f32()
ret void
}
define void @plain() {
ret void
}
declare <4 x float> @air.sample_texture_2d.v4f32()
!air.kernel = !{!0, !3}
!0 = !{ptr @samples, !1, !2}
!1 = !{}
!2 = !{}
!3 = !{ptr @plain, !1, !2}
"#;
assert!(entry_uses_texture_intrinsic(two_entries, Some("samples")));
assert!(!entry_uses_texture_intrinsic(two_entries, Some("plain")));
// No entry named: the module's calls, as the gate read them before.
assert!(entry_uses_texture_intrinsic(two_entries, None));
// A module the slicer cannot project answers as the module's calls do.
assert!(entry_uses_texture_intrinsic(two_entries, Some("absent")));
// A module with no texture intrinsic at all never pays for a projection.
assert!(!entry_uses_texture_intrinsic(
"define void @k() {\n ret void\n}\n",
Some("k")
));
}
#[test]
fn embedded_texture_detection_includes_read_and_write_fields() {
let mut nodes = HashMap::new();
@@ -801,7 +801,7 @@ fn parse_air_fragment_meta_with_nodes(
texture_type_names,
declared_descriptor_counts,
color_input_type_names,
embedded_textures: if body_uses_texture_intrinsic(ll) {
embedded_textures: if entry_uses_texture_intrinsic(ll, entry) {
detect_embedded_textures(nodes, &argument_buffers, &top_level_texture_locations)
} else {
Vec::new()
@@ -455,12 +455,12 @@ fn parse_air_kernel_meta_with_nodes(
};
roles.push((idx, role));
}
// Detect argument-buffer-embedded textures that the body uses through AIR texture
// intrinsics. Gated purely on AIR structure/semantics — the `air.indirect_argument` →
// Detect argument-buffer-embedded textures that the code this entry reaches uses through AIR
// texture intrinsics. Gated purely on AIR structure/semantics — the `air.indirect_argument` →
// `air.texture` marker chain plus stable AIR intrinsic families — so it cannot key on any shader
// name. The body must actually use a texture intrinsic for us to surface it.
// name. A texture intrinsic this entry reaches is what surfaces it; another entry's is not.
let argument_buffers = ArgumentBuffers::new(indirect_buffer_struct_refs);
let embedded_textures = if body_uses_texture_intrinsic(ll) {
let embedded_textures = if entry_uses_texture_intrinsic(ll, entry) {
detect_embedded_textures(nodes, &argument_buffers, &top_level_texture_locations)
} else {
vec![]
@@ -528,7 +528,7 @@ fn parse_air_vertex_meta_with_nodes(
buffer_accesses,
texture_type_names,
declared_descriptor_counts,
embedded_textures: if body_uses_texture_intrinsic(ll) {
embedded_textures: if entry_uses_texture_intrinsic(ll, entry) {
detect_embedded_textures(nodes, &argument_buffers, &top_level_texture_locations)
} else {
Vec::new()
@@ -205,3 +205,97 @@ declare <4 x half> @air.load.implicit_imageblock.v4f16(i32, <2 x i16>, i32, i16)
.collect();
assert_eq!(planes, [(1, 0, 203), (1, 2, 205)]);
}
/// A module of several entries sharing one argument buffer: the buffer's layout declares its
/// embedded texture member for every entry that takes it, so the member is in each entry's
/// `argument_buffer_fields`, but only the entry whose own reachable code loads the handle and
/// samples it binds a descriptor for it. The entry that reads another member alone binds none, and
/// each entry's interface is its projection's.
#[test]
fn another_entrys_sample_binds_no_embedded_texture_for_this_one() {
let ll = r#"%struct.bundle = type <{ %"struct.metal::texture2d", ptr addrspace(1) }>
%"struct.metal::texture2d" = type { ptr addrspace(1) }
define void @samples(ptr addrspace(2) %bundle) {
%handle = getelementptr inbounds %struct.bundle, ptr addrspace(2) %bundle, i64 0, i32 0, i32 0
%texture = load ptr addrspace(1), ptr addrspace(2) %handle, align 8
%texel = tail call <4 x float> @air.sample_texture_2d.v4f32(ptr addrspace(1) %texture, <2 x float> zeroinitializer)
ret void
}
define void @writes(ptr addrspace(2) %bundle) {
%slot = getelementptr inbounds %struct.bundle, ptr addrspace(2) %bundle, i64 0, i32 1
%out = load ptr addrspace(1), ptr addrspace(2) %slot, align 8
store <4 x float> zeroinitializer, ptr addrspace(1) %out, align 16
ret void
}
declare <4 x float> @air.sample_texture_2d.v4f32(ptr addrspace(1), <2 x float>)
!air.kernel = !{!0, !7}
!0 = !{ptr @samples, !1, !2}
!1 = !{}
!2 = !{!3}
!3 = !{i32 0, !"air.indirect_buffer", !"air.buffer_size", i32 16, !"air.location_index", i32 0, i32 1, !"air.read", !"air.struct_type_info", !4, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"bundle", !"air.arg_name", !"bundle"}
!4 = !{i32 0, i32 8, i32 0, !"texture2d<float, sample>", !"tex", !"air.indirect_argument", !5, i32 8, i32 8, i32 0, !"float4", !"out", !"air.indirect_argument", !6}
!5 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<float, sample>", !"air.arg_name", !"tex"}
!6 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"}
!7 = !{ptr @writes, !1, !2}
"#;
let samples = inspect(ll, Stage::Kernel, "samples", &[]).unwrap();
let writes = inspect(ll, Stage::Kernel, "writes", &[]).unwrap();
// The ABI declares the texture member to both entries.
for interface in [&samples, &writes] {
assert_eq!(
interface
.argument_buffer_fields
.iter()
.map(|field| (field.argument_index, field.resource_kind))
.collect::<Vec<_>>(),
[
(0, Some(ArgumentBufferResourceKind::Texture)),
(1, Some(ArgumentBufferResourceKind::Buffer)),
],
"{interface:?}"
);
}
// Only the entry that reaches a texture intrinsic binds a descriptor for it.
let embedded_textures = |interface: &ShaderInterface| {
interface
.bindings
.iter()
.filter(|binding| binding.kind == ResourceKind::EmbeddedArgBufferTexture)
.map(|binding| (binding.metal_index, binding.descriptor, binding.access))
.collect::<Vec<_>>()
};
assert_eq!(
embedded_textures(&samples),
[(
0,
Some(DescriptorLocation {
set: 0,
binding: 32,
count: 1
}),
Some(ResourceAccess::Sampled)
)],
"{samples:?}"
);
assert_eq!(embedded_textures(&writes), [], "{writes:?}");
// Each entry's own buffer member binds either way: a buffer member comes from the layout
// alone, with no use gate of its own.
for interface in [&samples, &writes] {
assert!(
interface
.bindings
.iter()
.any(|binding| binding.kind == ResourceKind::EmbeddedArgBufferBuffer),
"{interface:?}"
);
}
for entry in ["samples", "writes"] {
let projected = crate::air::entry_projection(ll, entry).unwrap();
assert_eq!(
inspect(ll, Stage::Kernel, entry, &[]),
inspect(&projected, Stage::Kernel, entry, &[]),
"{entry}"
);
}
}