Read the descriptor count the ABI states beside a location index
`air.location_index` always carries TWO operands: the resource's Metal slot and how many descriptors the argument occupies there. Nothing read the second one. An `array<sampler, 8>` therefore bound as a single sampler. Its element loads then resolved to nothing, so the sample fell back to the synthesized read sampler -- the shader's chosen filter, address modes and compare function dropped without trace, in a module that validates, binds and reflects as though one sampler at that slot were the whole story. There is no sampler descriptor-array lowering, so this is now a FALLBACK naming the parameter and the count. Two of 14579 local corpus sources declare one. The same count is what makes the texture-array length checkable. AIR states that length twice -- as this operand and inside `array<texture..., N>` -- and all 76 corpus declarations agree, so a name the ABI contradicts, or a count above one on a name that is not an array at all, is refused rather than silently sized from the name alone. It costs nothing today and fails the moment the parse drifts. Reading the pair also has to be POSITIONAL. Either operand may be a literal or a pointer to a function-constant global, and all four combinations occur -- 112946 `(i32, i32)`, 3404 `(ptr, i32)`, 1447 `(ptr, ptr)`, 439 `(i32, ptr)`, and no other shape. The slot decode looked for the first global after the marker, which IS the count whenever the slot is a literal: a texture at `[[texture(1)]]` with a function-constant array extent could bind at the extent's value. 69 corpus texture nodes are that shape. The buffer half of it was already dodged by a type-name special case for `array_ref<void>` -- whose comment describes exactly this confusion -- and the positional read makes that case unnecessary, so it is gone. Latent today: no corpus module resolves the count global. Full-corpus A/B over all 14579 sources: 0 status changes. The two sampler-array sources already fell back for unrelated reasons, so the hole is pinned by an authored fixture instead, which translates cleanly at HEAD~ and refuses here. Both new must-FALLBACK cases carry their positive control, so the boundary is what is pinned rather than one side of it.
This commit is contained in:
1 parent
a1ffb61707
commit
e84ea3fa48
6 files changed
+456
-34
No files matched your search
@@ -9,6 +9,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- A sampler argument AIR states is an ARRAY is refused instead of sampled through a default sampler
|
||||
nothing asked for. `air.location_index` carries two operands -- the Metal slot and the descriptor
|
||||
count -- and Metal spells `array<sampler, 8>` with a count of 8. Nothing read the count, so the
|
||||
argument bound as a single sampler; the element loads then resolved to nothing and the sample fell
|
||||
back to the synthesized read sampler, dropping the state the shader selected in a module that
|
||||
validates, binds and reflects as though one sampler at that slot were the whole story. Two of
|
||||
14579 local corpus sources declare one. There is no sampler descriptor-array lowering, so the
|
||||
honest answer is a `FALLBACK` naming the parameter and the count.
|
||||
|
||||
The same count now checks the texture-array length the type name states. AIR states that length
|
||||
twice and all 76 corpus declarations agree, which is what makes the ABI usable as a standing check
|
||||
on the name parse: a `array<texture..., N>` whose two statements disagree, or a count above one on
|
||||
a name that is not an array at all, is refused rather than sized from the name alone.
|
||||
|
||||
- The `air.location_index` operand pair is decoded by POSITION rather than by scanning forward for
|
||||
the next operand of a given kind. Either operand may be a literal or a pointer to a
|
||||
function-constant global, and all four combinations occur (112946 / 3404 / 1447 / 439 over 14579
|
||||
local corpus sources). The slot decode looked for the first global after the marker, which is the
|
||||
COUNT whenever the slot is a literal -- so an argument at `[[texture(1)]]` with a
|
||||
function-constant array extent could bind at the extent's value instead of at 1. 439 corpus nodes
|
||||
are that shape, 69 of them textures; the buffer half was already dodged by a type-name special
|
||||
case for `array_ref<void>`, which the positional read makes unnecessary and which is now gone.
|
||||
Latent today -- no corpus module resolves the count global -- and no longer reachable.
|
||||
|
||||
- A fixed texture-handle array binds and reflects at the length AIR declares, not at the
|
||||
descriptor-ABI ceiling. `array<texture2d<half, sample>, 32>` states its length twice -- in the type
|
||||
name and as the second `air.location_index` operand -- and the decoded `TextureShape` already
|
||||
|
||||
+13
-15
@@ -1,25 +1,23 @@
|
||||
use super::location_index;
|
||||
use super::location_operands;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// The Metal slot an argument node declares, resolving a function-constant slot through the
|
||||
/// module's static initializers.
|
||||
///
|
||||
/// The slot is the FIRST operand of the `air.location_index` pair; `fallback` (the parameter
|
||||
/// ordinal) stands in when it is a function-constant global this module does not initialize.
|
||||
pub(super) fn location_index_with_static(
|
||||
body: &str,
|
||||
fallback: u32,
|
||||
static_int_globals: &HashMap<String, u32>,
|
||||
) -> u32 {
|
||||
location_index_global(body)
|
||||
.and_then(|global| static_int_globals.get(&global).copied())
|
||||
.unwrap_or_else(|| location_index(body, fallback))
|
||||
}
|
||||
|
||||
fn location_index_global(body: &str) -> Option<String> {
|
||||
global_after_marker(body, "air.location_index")
|
||||
}
|
||||
|
||||
fn global_after_marker(body: &str, marker: &str) -> Option<String> {
|
||||
let marker = format!("!\"{marker}\"");
|
||||
let pos = body.find(&marker)?;
|
||||
let after = &body[pos + marker.len()..];
|
||||
parse_global_name(after)
|
||||
match location_operands(body).map(|operands| operands.index) {
|
||||
Some(super::LocationOperand::Literal(index)) => index,
|
||||
Some(super::LocationOperand::Global(global)) => {
|
||||
static_int_globals.get(&global).copied().unwrap_or(fallback)
|
||||
}
|
||||
None => fallback,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_global_name(s: &str) -> Option<String> {
|
||||
|
||||
+137
-19
@@ -236,6 +236,12 @@ pub struct FragMeta {
|
||||
pub buffer_accesses: HashMap<u32, BufferAccess>,
|
||||
/// `param_idx -> AIR texture argument type name`, e.g. `texture2d<uint, read>`.
|
||||
pub texture_type_names: HashMap<u32, String>,
|
||||
/// `param_idx -> the descriptor count `air.location_index` states, when it states more than one.
|
||||
///
|
||||
/// A texture or sampler argument above `1` is a handle ARRAY occupying that many descriptors at
|
||||
/// its Metal slot. Absent for the ordinary single-descriptor argument and for a count spelled as
|
||||
/// a function-constant global.
|
||||
pub declared_descriptor_counts: HashMap<u32, u32>,
|
||||
/// Framebuffer-fetch color input Location -> AIR render-target type name, e.g. `float4`.
|
||||
pub color_input_type_names: HashMap<u32, String>,
|
||||
pub embedded_textures: Vec<EmbeddedTexture>,
|
||||
@@ -298,6 +304,10 @@ impl FragMeta {
|
||||
pub fn texture_type_name(&self, idx: u32) -> Option<&str> {
|
||||
self.texture_type_names.get(&idx).map(String::as_str)
|
||||
}
|
||||
/// See [`Self::declared_descriptor_counts`].
|
||||
pub fn declared_descriptor_count(&self, idx: u32) -> Option<u32> {
|
||||
self.declared_descriptor_counts.get(&idx).copied()
|
||||
}
|
||||
pub fn color_input_type_name(&self, location: u32) -> Option<&str> {
|
||||
self.color_input_type_names
|
||||
.get(&location)
|
||||
@@ -486,6 +496,12 @@ pub struct VertMeta {
|
||||
pub buffer_accesses: HashMap<u32, BufferAccess>,
|
||||
/// `param_idx -> AIR texture argument type name`, e.g. `texture2d<uint, read>`.
|
||||
pub texture_type_names: HashMap<u32, String>,
|
||||
/// `param_idx -> the descriptor count `air.location_index` states, when it states more than one.
|
||||
///
|
||||
/// A texture or sampler argument above `1` is a handle ARRAY occupying that many descriptors at
|
||||
/// its Metal slot. Absent for the ordinary single-descriptor argument and for a count spelled as
|
||||
/// a function-constant global.
|
||||
pub declared_descriptor_counts: HashMap<u32, u32>,
|
||||
pub embedded_textures: Vec<EmbeddedTexture>,
|
||||
pub embedded_arguments: Vec<EmbeddedArgument>,
|
||||
pub tessellation: Option<TessellationMeta>,
|
||||
@@ -542,6 +558,10 @@ impl VertMeta {
|
||||
pub fn texture_type_name(&self, idx: u32) -> Option<&str> {
|
||||
self.texture_type_names.get(&idx).map(String::as_str)
|
||||
}
|
||||
/// See [`Self::declared_descriptor_counts`].
|
||||
pub fn declared_descriptor_count(&self, idx: u32) -> Option<u32> {
|
||||
self.declared_descriptor_counts.get(&idx).copied()
|
||||
}
|
||||
pub fn output_role_of(&self, idx: u32) -> Option<&VertOutRole> {
|
||||
self.output_roles.get(idx as usize)
|
||||
}
|
||||
@@ -685,6 +705,12 @@ pub struct KernMeta {
|
||||
pub buffer_accesses: HashMap<u32, BufferAccess>,
|
||||
/// `param_idx -> AIR texture argument type name`, e.g. `texture2d<uint, read>`.
|
||||
pub texture_type_names: HashMap<u32, String>,
|
||||
/// `param_idx -> the descriptor count `air.location_index` states, when it states more than one.
|
||||
///
|
||||
/// A texture or sampler argument above `1` is a handle ARRAY occupying that many descriptors at
|
||||
/// its Metal slot. Absent for the ordinary single-descriptor argument and for a count spelled as
|
||||
/// a function-constant global.
|
||||
pub declared_descriptor_counts: HashMap<u32, u32>,
|
||||
/// `param_idx -> AIR kernel stage-input scalar/vector type name`.
|
||||
pub stage_input_type_names: HashMap<u32, String>,
|
||||
/// Textures EMBEDDED inside an `air.indirect_buffer` argument buffer (via `air.indirect_argument`
|
||||
@@ -729,6 +755,10 @@ impl KernMeta {
|
||||
pub fn texture_type_name(&self, idx: u32) -> Option<&str> {
|
||||
self.texture_type_names.get(&idx).map(String::as_str)
|
||||
}
|
||||
/// See [`Self::declared_descriptor_counts`].
|
||||
pub fn declared_descriptor_count(&self, idx: u32) -> Option<u32> {
|
||||
self.declared_descriptor_counts.get(&idx).copied()
|
||||
}
|
||||
|
||||
/// Synthetic buffer slots used for kernel `[[stage_in]]` attributes.
|
||||
///
|
||||
@@ -837,6 +867,7 @@ fn parse_air_kernel_meta_with_nodes(
|
||||
let mut buffer_type_names = HashMap::new();
|
||||
let mut buffer_accesses = HashMap::new();
|
||||
let mut texture_type_names = HashMap::new();
|
||||
let mut declared_descriptor_counts = HashMap::new();
|
||||
let mut stage_input_type_names = HashMap::new();
|
||||
// `air.location_index` of every top-level `air.texture` arg — the basis for the synthetic
|
||||
// embedded-texture index K (see `embedded_synthetic_texture_index`).
|
||||
@@ -849,6 +880,9 @@ fn parse_air_kernel_meta_with_nodes(
|
||||
let Some(idx) = first_i32(node) else { continue };
|
||||
let layout = struct_info_ref(node).and_then(|sref| parse_struct_info(nodes, sref, 0));
|
||||
let strs = role_strings(node);
|
||||
if let Some(count) = declared_descriptor_count(node) {
|
||||
declared_descriptor_counts.insert(idx, count);
|
||||
}
|
||||
if strs.first().map(String::as_str) == Some("function_constant")
|
||||
&& primary_role(&strs) == Some("buffer")
|
||||
{
|
||||
@@ -900,7 +934,7 @@ fn parse_air_kernel_meta_with_nodes(
|
||||
if let Some(access) = declared_buffer_access(node) {
|
||||
buffer_accesses.insert(idx, access);
|
||||
}
|
||||
KernRole::Buffer(buffer_location_index(node, idx, &static_int_globals))
|
||||
KernRole::Buffer(location_index_with_static(node, idx, &static_int_globals))
|
||||
}
|
||||
"texture" => {
|
||||
if let Some(name) = arg_type_name(node) {
|
||||
@@ -993,6 +1027,7 @@ fn parse_air_kernel_meta_with_nodes(
|
||||
buffer_type_names,
|
||||
buffer_accesses,
|
||||
texture_type_names,
|
||||
declared_descriptor_counts,
|
||||
stage_input_type_names,
|
||||
embedded_textures,
|
||||
embedded_arguments,
|
||||
@@ -1429,27 +1464,100 @@ fn i32_after_marker(body: &str, marker: &str) -> Option<u32> {
|
||||
}
|
||||
|
||||
fn location_index(body: &str, fallback: u32) -> u32 {
|
||||
i32_after_marker(body, "air.location_index").unwrap_or(fallback)
|
||||
match location_operands(body).map(|operands| operands.index) {
|
||||
Some(LocationOperand::Literal(index)) => index,
|
||||
_ => fallback,
|
||||
}
|
||||
}
|
||||
|
||||
fn buffer_location_index(
|
||||
body: &str,
|
||||
fallback: u32,
|
||||
static_int_globals: &HashMap<String, u32>,
|
||||
) -> u32 {
|
||||
if arg_type_name(body)
|
||||
.as_deref()
|
||||
.is_some_and(is_device_buffer_array_type_name)
|
||||
{
|
||||
// Metal flattens `array_ref<void>` into consecutive buffer-table arguments beginning at
|
||||
// the literal metadata location. Its static initializer computes the array extent/next
|
||||
// free slot, not a replacement binding for element zero.
|
||||
location_index(body, fallback)
|
||||
} else {
|
||||
location_index_with_static(body, fallback, static_int_globals)
|
||||
/// How many descriptors an argument node states it occupies, when AIR states more than one.
|
||||
///
|
||||
/// `None` for the ordinary single-descriptor argument, and for a count spelled as a
|
||||
/// function-constant global -- the consumer picks that length at pipeline creation, so there is no
|
||||
/// compile-time array to size.
|
||||
fn declared_descriptor_count(body: &str) -> Option<u32> {
|
||||
match location_operands(body)?.count? {
|
||||
LocationOperand::Literal(count) if count > 1 => Some(count),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// One operand of the `air.location_index` pair: either a compile-time value or a pointer to a
|
||||
/// function-constant global whose value the consumer chooses at pipeline creation.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum LocationOperand {
|
||||
Literal(u32),
|
||||
Global(String),
|
||||
}
|
||||
|
||||
/// The two operands `air.location_index` always carries: the resource's Metal slot, and how many
|
||||
/// descriptors the argument occupies at that slot.
|
||||
///
|
||||
/// `!"air.location_index", i32 4, i32 2` is `[[texture(4)]]` holding two texture handles. The count
|
||||
/// is `1` for an ordinary resource; above `1` the argument is a handle ARRAY, and every corpus
|
||||
/// declaration above `1` agrees with the `array<..., N>` length in the type name.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct LocationOperands {
|
||||
pub index: LocationOperand,
|
||||
pub count: Option<LocationOperand>,
|
||||
}
|
||||
|
||||
/// Decode the `air.location_index` operand pair POSITIONALLY.
|
||||
///
|
||||
/// Either operand may be a literal or a global, and all four combinations occur -- measured over
|
||||
/// 14579 corpus sources: 112946 `(i32, i32)`, 3404 `(ptr, i32)`, 1447 `(ptr, ptr)`, 439
|
||||
/// `(i32, ptr)`, and no other shape. Scanning forward for "the next `i32`" or "the next `@`"
|
||||
/// therefore reads the COUNT whenever the slot is spelled the other way: a texture argument at
|
||||
/// `[[texture(1)]]` with a function-constant array count would be bound at the count's value
|
||||
/// instead of at 1. 69 corpus texture nodes are that shape.
|
||||
pub(crate) fn location_operands(body: &str) -> Option<LocationOperands> {
|
||||
let marker = "!\"air.location_index\"";
|
||||
let position = body.find(marker)?;
|
||||
let mut operands = split_metadata_operands(&body[position + marker.len()..])
|
||||
.into_iter()
|
||||
.map(|operand| {
|
||||
if let Some(literal) = operand.strip_prefix("i32 ") {
|
||||
return literal.trim().parse().ok().map(LocationOperand::Literal);
|
||||
}
|
||||
let at = operand.find('@')?;
|
||||
let name = operand[at..]
|
||||
.chars()
|
||||
.take_while(|character| {
|
||||
!character.is_whitespace() && !matches!(*character, ',' | ')' | '(' | '[' | ']')
|
||||
})
|
||||
.collect::<String>();
|
||||
(name.len() > 1).then_some(LocationOperand::Global(name))
|
||||
});
|
||||
Some(LocationOperands {
|
||||
index: operands.next().flatten()?,
|
||||
count: operands.next().flatten(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Split an AIR metadata operand list on the commas that separate operands. A `!"..."` string
|
||||
/// operand may itself contain commas (`!"array<texture2d<half, sample>, 2>"`), so a plain
|
||||
/// `split(',')` would tear one operand into three and shift every position after it.
|
||||
fn split_metadata_operands(tail: &str) -> Vec<String> {
|
||||
let mut operands = vec![];
|
||||
let mut current = String::new();
|
||||
let mut quoted = false;
|
||||
for character in tail.chars() {
|
||||
match character {
|
||||
'"' => {
|
||||
quoted = !quoted;
|
||||
current.push(character);
|
||||
}
|
||||
',' if !quoted => {
|
||||
operands.push(std::mem::take(&mut current).trim().to_string());
|
||||
}
|
||||
_ => current.push(character),
|
||||
}
|
||||
}
|
||||
operands.push(current.trim().to_string());
|
||||
operands.retain(|operand| !operand.is_empty());
|
||||
operands
|
||||
}
|
||||
|
||||
fn render_target_location(
|
||||
body: &str,
|
||||
fallback: u32,
|
||||
@@ -2058,6 +2166,7 @@ fn parse_air_fragment_meta_with_nodes(
|
||||
let mut varying_user_semantics = HashMap::new();
|
||||
let mut varying_interpolation = HashMap::new();
|
||||
let mut texture_type_names = HashMap::new();
|
||||
let mut declared_descriptor_counts = HashMap::new();
|
||||
let mut color_input_type_names = HashMap::new();
|
||||
let mut varying_loc = 0u32;
|
||||
let mut buffer_address_spaces = HashMap::new();
|
||||
@@ -2081,6 +2190,9 @@ fn parse_air_fragment_meta_with_nodes(
|
||||
}
|
||||
}
|
||||
let strs = role_strings(node);
|
||||
if let Some(count) = declared_descriptor_count(node) {
|
||||
declared_descriptor_counts.insert(idx, count);
|
||||
}
|
||||
let Some(role_str) = primary_role(&strs) else {
|
||||
continue;
|
||||
};
|
||||
@@ -2174,7 +2286,7 @@ fn parse_air_fragment_meta_with_nodes(
|
||||
if let Some(access) = declared_buffer_access(node) {
|
||||
buffer_accesses.insert(idx, access);
|
||||
}
|
||||
FragRole::Buffer(buffer_location_index(node, idx, &static_int_globals))
|
||||
FragRole::Buffer(location_index_with_static(node, idx, &static_int_globals))
|
||||
}
|
||||
// an air.render_target in the INPUT list = framebuffer fetch ([[color(n)]]).
|
||||
"render_target" => {
|
||||
@@ -2223,6 +2335,7 @@ fn parse_air_fragment_meta_with_nodes(
|
||||
buffer_type_names,
|
||||
buffer_accesses,
|
||||
texture_type_names,
|
||||
declared_descriptor_counts,
|
||||
color_input_type_names,
|
||||
embedded_textures: if body_uses_texture_intrinsic(ll) {
|
||||
detect_embedded_textures(
|
||||
@@ -2358,6 +2471,7 @@ fn parse_air_vertex_meta_with_nodes(
|
||||
let mut buffer_type_names = HashMap::new();
|
||||
let mut buffer_accesses = HashMap::new();
|
||||
let mut texture_type_names = HashMap::new();
|
||||
let mut declared_descriptor_counts = HashMap::new();
|
||||
let mut indirect_buffer_struct_refs = Vec::new();
|
||||
let mut patch_control_point = None;
|
||||
let param_address_spaces = entry
|
||||
@@ -2376,6 +2490,9 @@ fn parse_air_vertex_meta_with_nodes(
|
||||
}
|
||||
}
|
||||
let strs = role_strings(node);
|
||||
if let Some(count) = declared_descriptor_count(node) {
|
||||
declared_descriptor_counts.insert(idx, count);
|
||||
}
|
||||
let Some(mut first) = fc_promoted_role(&strs, false) else {
|
||||
continue;
|
||||
};
|
||||
@@ -2436,7 +2553,7 @@ fn parse_air_vertex_meta_with_nodes(
|
||||
if let Some(access) = declared_buffer_access(node) {
|
||||
buffer_accesses.insert(idx, access);
|
||||
}
|
||||
VertRole::Buffer(buffer_location_index(node, idx, &static_int_globals))
|
||||
VertRole::Buffer(location_index_with_static(node, idx, &static_int_globals))
|
||||
}
|
||||
"texture" => {
|
||||
if let Some(name) = arg_type_name(node) {
|
||||
@@ -2522,6 +2639,7 @@ fn parse_air_vertex_meta_with_nodes(
|
||||
buffer_type_names,
|
||||
buffer_accesses,
|
||||
texture_type_names,
|
||||
declared_descriptor_counts,
|
||||
embedded_textures: if body_uses_texture_intrinsic(ll) {
|
||||
detect_embedded_textures(
|
||||
nodes,
|
||||
|
||||
@@ -347,6 +347,13 @@ fn kernel_function_constant_buffer_locations_preserve_shared_bindings() {
|
||||
);
|
||||
}
|
||||
|
||||
/// Metal flattens `array_ref<void>` into consecutive buffer-table arguments beginning at the
|
||||
/// literal slot; its global is the array extent, not a replacement binding for element zero.
|
||||
///
|
||||
/// This used to need a type-name special case in the buffer decode. It does not any more: the slot
|
||||
/// is the FIRST `air.location_index` operand whatever the second one is, so the general positional
|
||||
/// read gets it right and nothing keys on the spelling `array_ref<void>`. Kept as the case that
|
||||
/// motivated the fix.
|
||||
#[test]
|
||||
fn device_buffer_array_uses_literal_base_instead_of_static_next_location() {
|
||||
let ll = r#"
|
||||
@@ -1333,3 +1340,72 @@ define { { <4 x half>, i16 } } @f({ <4 x half>, i16 } %tile) { ret { { <4 x half
|
||||
assert_eq!(imageblock.inputs[0].members[1].master_member, 1);
|
||||
assert_eq!(imageblock.outputs[0].members[0].master_member, 0);
|
||||
}
|
||||
|
||||
/// `air.location_index` carries a PAIR of operands -- the Metal slot and the descriptor count --
|
||||
/// and either may be a literal or a pointer to a function-constant global. All four combinations
|
||||
/// occur in the corpus (112946 / 3404 / 1447 / 439 over 14579 sources), so a decode that scans
|
||||
/// forward for "the next i32" or "the next @" reads the COUNT whenever the slot is spelled the
|
||||
/// other way.
|
||||
#[test]
|
||||
fn the_location_index_operand_pair_is_read_by_position() {
|
||||
use super::{location_operands, LocationOperand};
|
||||
let global = "@_ZL32__metal_implicit_attr_int_expr_2.150".to_string();
|
||||
for (body, index, count) in [
|
||||
(
|
||||
r#"i32 0, !"air.texture", !"air.location_index", i32 4, i32 2, !"air.sample""#,
|
||||
LocationOperand::Literal(4),
|
||||
Some(LocationOperand::Literal(2)),
|
||||
),
|
||||
(
|
||||
r#"i32 0, !"air.buffer", !"air.location_index", i32 7, ptr addrspace(2) @_ZL32__metal_implicit_attr_int_expr_2.150, !"air.read_write""#,
|
||||
LocationOperand::Literal(7),
|
||||
Some(LocationOperand::Global(global.clone())),
|
||||
),
|
||||
(
|
||||
r#"i32 0, !"air.texture", !"air.location_index", ptr addrspace(2) @_ZL32__metal_implicit_attr_int_expr_2.150, i32 1, !"air.sample""#,
|
||||
LocationOperand::Global(global.clone()),
|
||||
Some(LocationOperand::Literal(1)),
|
||||
),
|
||||
(
|
||||
r#"i32 0, !"air.texture", !"air.location_index", ptr addrspace(2) @_ZL32__metal_implicit_attr_int_expr_2.150, ptr addrspace(2) @_ZL32__metal_implicit_attr_int_expr_2.150, !"air.write""#,
|
||||
LocationOperand::Global(global.clone()),
|
||||
Some(LocationOperand::Global(global.clone())),
|
||||
),
|
||||
] {
|
||||
let decoded = location_operands(body).expect("every shape decodes");
|
||||
assert_eq!(decoded.index, index, "slot operand of `{body}`");
|
||||
assert_eq!(decoded.count, count, "count operand of `{body}`");
|
||||
}
|
||||
|
||||
// A string operand may itself contain commas, so splitting the list on every comma would shift
|
||||
// every position after it.
|
||||
let quoted = r#"i32 0, !"air.texture", !"air.arg_type_name", !"array<texture2d<half, sample>, 2>", !"air.location_index", i32 4, i32 2"#;
|
||||
let decoded = location_operands(quoted).expect("decodes past a comma-bearing string");
|
||||
assert_eq!(decoded.index, LocationOperand::Literal(4));
|
||||
assert_eq!(decoded.count, Some(LocationOperand::Literal(2)));
|
||||
}
|
||||
|
||||
/// The slot of an argument whose COUNT is a function-constant global is still the literal slot.
|
||||
///
|
||||
/// `array_ref<void>` is spelled `air.location_index, i32 N, ptr @extent`: a fixed buffer slot with
|
||||
/// a runtime array extent. Resolving "the first global after the marker" through the static
|
||||
/// initializers answers with the extent, binding the argument at a slot AIR never named. 439 corpus
|
||||
/// nodes are that shape, 69 of them textures.
|
||||
#[test]
|
||||
fn a_function_constant_count_does_not_become_the_location_index() {
|
||||
let ll = FRAG_LL.replace(
|
||||
r#"!"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"texture2d<float, sample>""#,
|
||||
r#"!"air.location_index", i32 0, ptr addrspace(2) @extent.1, !"air.arg_type_name", !"texture2d<float, sample>""#,
|
||||
);
|
||||
let ll = format!(
|
||||
"@extent.1 = internal addrspace(2) global i32 9, align 4\n\
|
||||
define internal void @init() section \"air.static_init\" {{\n\
|
||||
entry:\n store i32 9, ptr addrspace(2) @extent.1, align 4\n ret void\n}}\n{ll}"
|
||||
);
|
||||
let meta = parse_air_fragment_meta(&ll).unwrap();
|
||||
assert_eq!(
|
||||
meta.role_of(2),
|
||||
Some(&FragRole::Texture(0)),
|
||||
"the texture binds at the slot AIR states, not at the value of its count global"
|
||||
);
|
||||
}
|
||||
@@ -235,6 +235,71 @@ pub(in crate::passes) enum ParamBinding {
|
||||
ZeroPointer { var: Word },
|
||||
}
|
||||
|
||||
/// Refuse an argument whose declared descriptor count this translator cannot honour.
|
||||
///
|
||||
/// The count is the second `air.location_index` operand -- the stable ABI statement of how many
|
||||
/// descriptors the argument occupies at its Metal slot. Only a texture handle array has a lowering
|
||||
/// (`ParamBinding::ImageArray`), and it sizes itself from the type name, so the two statements of
|
||||
/// the length have to agree; the ABI is what makes the name parse checkable at all. Over 14579
|
||||
/// corpus sources every declaration above one agrees, and only textures and samplers carry one.
|
||||
fn declared_descriptor_counts(
|
||||
stage: Stage,
|
||||
frag: Option<&FragMeta>,
|
||||
vert: Option<&VertMeta>,
|
||||
kern: Option<&KernMeta>,
|
||||
) -> Result<(), String> {
|
||||
let counts = match stage {
|
||||
Stage::Fragment => frag.map(|meta| &meta.declared_descriptor_counts),
|
||||
Stage::Vertex => vert.map(|meta| &meta.declared_descriptor_counts),
|
||||
Stage::Kernel => kern.map(|meta| &meta.declared_descriptor_counts),
|
||||
};
|
||||
let Some(counts) = counts else {
|
||||
return Ok(());
|
||||
};
|
||||
let mut declared = counts.iter().collect::<Vec<_>>();
|
||||
declared.sort();
|
||||
for (&idx, &count) in declared {
|
||||
let is_sampler = match stage {
|
||||
Stage::Fragment => matches!(
|
||||
frag.and_then(|m| m.role_of(idx)),
|
||||
Some(FragRole::Sampler(_))
|
||||
),
|
||||
Stage::Vertex => matches!(
|
||||
vert.and_then(|m| m.role_of(idx)),
|
||||
Some(VertRole::Sampler(_))
|
||||
),
|
||||
Stage::Kernel => matches!(
|
||||
kern.and_then(|m| m.role_of(idx)),
|
||||
Some(KernRole::Sampler(_))
|
||||
),
|
||||
};
|
||||
if is_sampler {
|
||||
return Err(format!(
|
||||
"entry parameter {idx} is an array of {count} samplers, which this translator has \
|
||||
no descriptor-array lowering for; emitting the module would sample with a \
|
||||
synthesized default sampler instead of the one the shader selected"
|
||||
));
|
||||
}
|
||||
let texture_type_name = match stage {
|
||||
Stage::Fragment => frag.and_then(|m| m.texture_type_name(idx)),
|
||||
Stage::Vertex => vert.and_then(|m| m.texture_type_name(idx)),
|
||||
Stage::Kernel => kern.and_then(|m| m.texture_type_name(idx)),
|
||||
};
|
||||
let Some(name) = texture_type_name else {
|
||||
// Not a texture, and no other role has a descriptor-array lowering to check against.
|
||||
continue;
|
||||
};
|
||||
if crate::meta::texture_shape_from_name(name).array_length != Some(count) {
|
||||
return Err(format!(
|
||||
"entry parameter {idx} occupies {count} descriptors per `air.location_index`, \
|
||||
which its type name `{name}` does not state; emitting the module would size the \
|
||||
descriptor array from a type name the ABI contradicts"
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// How a buffer param's body uses are lowered.
|
||||
pub(in crate::passes) enum BufWrap {
|
||||
/// The AIR pointee is a heterogeneous struct emitted as a struct pointer. The body's
|
||||
@@ -352,6 +417,14 @@ pub(super) fn build_stage_input(
|
||||
));
|
||||
}
|
||||
|
||||
// `air.location_index` states how many descriptors an argument occupies. Above one the argument
|
||||
// is a handle ARRAY, and binding a single descriptor where the shader indexes several is silent:
|
||||
// the module validates, and the consumer's layout has one descriptor at a slot the shader reads
|
||||
// eight of. Textures have an array lowering and are checked against it; a sampler array has no
|
||||
// lowering at all, and today its element loads resolve to nothing, so the sample falls back to a
|
||||
// synthesized default sampler and the shader's chosen sampler state is dropped without trace.
|
||||
declared_descriptor_counts(*stage, frag, vert, kern)?;
|
||||
|
||||
// An `air.patch` node the decode could not read is not the same as no patch node at all. The
|
||||
// first is a post-tessellation evaluation shader; dropping its shape emits it as an ordinary
|
||||
// vertex shader, without the domain, spacing and winding execution modes Vulkan requires of a
|
||||
|
||||
@@ -626,3 +626,136 @@ define fastcc void @store_vec(ptr %0) {
|
||||
!9 = !{i32 4, !"air.buffer", !"air.location_index", i32 7, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 1, !"air.arg_type_align_size", i32 1, !"air.arg_type_name", !"uchar", !"air.arg_name", !"data"}"#;
|
||||
assert_fallback(ll, "no dynamic-struct-index rewrite repaired");
|
||||
}
|
||||
|
||||
/// A sampler argument AIR states occupies more than one descriptor.
|
||||
///
|
||||
/// `air.location_index` carries two operands, the Metal slot and the descriptor count, and Metal
|
||||
/// spells `array<sampler, 8>` with a count of 8. There is no sampler descriptor-array lowering:
|
||||
/// the element loads resolve to nothing, so the sample falls back to the synthesized default
|
||||
/// sampler and the state the shader selected is dropped -- in a module that validates, binds and
|
||||
/// reflects as though one sampler at that slot were the whole story. Two of 14579 local corpus
|
||||
/// sources declare one.
|
||||
///
|
||||
/// Pinned from both sides over one template, so the boundary is what is fixed: the ordinary
|
||||
/// single-sampler spelling of the same kernel must keep translating.
|
||||
#[test]
|
||||
fn a_sampler_descriptor_array_fallbacks() {
|
||||
const KERNEL: &str = r#"target triple = "spirv-unknown-vulkan1.2"
|
||||
|
||||
define void @k(ptr addrspace(1) %tex, ptr readonly byval([8 x ptr addrspace(2)]) captures(none) %samps, ptr addrspace(1) %out) {
|
||||
entry:
|
||||
%slot = getelementptr inbounds [8 x ptr addrspace(2)], ptr %samps, i32 0, i32 5
|
||||
%s = load ptr addrspace(2), ptr %slot, align 8
|
||||
%sample = call { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1) %tex, ptr addrspace(2) %s, <2 x float> zeroinitializer, i1 false, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0)
|
||||
%color = extractvalue { <4 x float>, i8 } %sample, 0
|
||||
store <4 x float> %color, ptr addrspace(1) %out, align 16
|
||||
ret void
|
||||
}
|
||||
declare { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1), ptr addrspace(2), <2 x float>, i1, <2 x i32>, i1, float, float, i32)
|
||||
!air.kernel = !{!0}
|
||||
!0 = !{ptr @k, !1, !2}
|
||||
!1 = !{}
|
||||
!2 = !{!3, !4, !5}
|
||||
!3 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<float, sample>", !"air.arg_name", !"tex"}
|
||||
!4 = !{i32 1, !"air.sampler", !"air.location_index", i32 0, i32 COUNT, !"air.arg_type_name", !"SAMPLER_TYPE", !"air.arg_name", !"samps"}
|
||||
!5 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.write", !"air.address_space", i32 1, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"}
|
||||
"#;
|
||||
|
||||
let array = KERNEL
|
||||
.replace("i32 COUNT", "i32 8")
|
||||
.replace("SAMPLER_TYPE", "array<sampler, 8>");
|
||||
match translate_sanitized_native(&array, Stage::Kernel, &tmp()) {
|
||||
Ok(spv) => panic!(
|
||||
"expected a clean FALLBACK but translate succeeded ({} bytes); a sample through a \
|
||||
default sampler the shader never asked for is a silently wrong module",
|
||||
spv.len()
|
||||
),
|
||||
Err(e) => {
|
||||
assert!(
|
||||
e.contains("array of 8 samplers"),
|
||||
"the diagnostic should name the declared count; got: {e}"
|
||||
);
|
||||
assert!(
|
||||
e.contains("entry parameter 1"),
|
||||
"and the parameter it sits on; got: {e}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let single = KERNEL
|
||||
.replace("i32 COUNT", "i32 1")
|
||||
.replace("SAMPLER_TYPE", "sampler");
|
||||
assert!(
|
||||
translate_sanitized_native(&single, Stage::Kernel, &tmp()).is_ok(),
|
||||
"the same kernel with an ordinary single sampler must translate"
|
||||
);
|
||||
}
|
||||
|
||||
/// A texture handle array whose two statements of its own length disagree.
|
||||
///
|
||||
/// AIR states the length twice: as the `air.location_index` count operand and inside the
|
||||
/// `array<texture..., N>` type name. The interface pass sizes its `OpTypeArray` from the name and
|
||||
/// reflection reports the same number, so a name the ABI contradicts would bind and publish a
|
||||
/// descriptor array of the wrong size. Over 14579 local corpus sources all 76 declarations agree,
|
||||
/// which is exactly what makes the ABI usable as a check on the name parse: it costs nothing today
|
||||
/// and fails the moment the parse drifts.
|
||||
#[test]
|
||||
fn a_texture_array_whose_declared_length_contradicts_its_type_name_fallbacks() {
|
||||
const KERNEL: &str = r#"target triple = "spirv-unknown-vulkan1.2"
|
||||
|
||||
define void @k(ptr readonly captures(none) %imgs, ptr addrspace(1) %out) {
|
||||
entry:
|
||||
%slot = getelementptr inbounds ptr addrspace(1), ptr %imgs, i32 3
|
||||
%tex = load ptr addrspace(1), ptr %slot, align 8
|
||||
%w = tail call i32 @air.get_width_texture_2d(ptr addrspace(1) %tex, i32 0)
|
||||
store i32 %w, ptr addrspace(1) %out, align 4
|
||||
ret void
|
||||
}
|
||||
declare i32 @air.get_width_texture_2d(ptr addrspace(1), i32)
|
||||
!air.kernel = !{!0}
|
||||
!0 = !{ptr @k, !1, !2}
|
||||
!1 = !{}
|
||||
!2 = !{!3, !4}
|
||||
!3 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 COUNT, !"air.sample", !"air.arg_type_name", !"TEXTURE_TYPE", !"air.arg_name", !"imgs"}
|
||||
!4 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 0, i32 1, !"air.write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"}
|
||||
"#;
|
||||
let fixed = |count: &str, name: &str| {
|
||||
KERNEL
|
||||
.replace("i32 COUNT", count)
|
||||
.replace("TEXTURE_TYPE", name)
|
||||
};
|
||||
|
||||
for (count, name, why) in [
|
||||
(
|
||||
"i32 3",
|
||||
"array<texture2d<float, sample>, 4>",
|
||||
"a length the ABI contradicts",
|
||||
),
|
||||
(
|
||||
"i32 4",
|
||||
"texture2d<float, sample>",
|
||||
"a count on a name that is not an array at all",
|
||||
),
|
||||
] {
|
||||
match translate_sanitized_native(&fixed(count, name), Stage::Kernel, &tmp()) {
|
||||
Ok(spv) => panic!(
|
||||
"expected a clean FALLBACK for {why} but translate succeeded ({} bytes)",
|
||||
spv.len()
|
||||
),
|
||||
Err(e) => assert!(
|
||||
e.contains("descriptors per `air.location_index`"),
|
||||
"the diagnostic should name the ABI count for {why}; got: {e}"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
assert!(
|
||||
translate_sanitized_native(
|
||||
&fixed("i32 4", "array<texture2d<float, sample>, 4>"),
|
||||
Stage::Kernel,
|
||||
&tmp()
|
||||
)
|
||||
.is_ok(),
|
||||
"the same kernel whose two statements of the length agree must translate"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user