Report only the constexpr samplers the module binds
`StaticSampler` is a descriptor translation invents. Nothing in Metal's API puts one there, so the only reason a consumer creates a `VkSampler` at that binding is that this reflection asked for one. Reflection named one per `!air.sampler_states` entry -- every constexpr sampler state the source wrote -- and the interface pass gives each the same binding in the same order. But a state no sample reads leaves a variable nothing references, and the module drops it, so reflection went on describing a sampler that is not there. Over the 2880-source sample: 112 such bindings in 60 modules. `StaticSampler` was the last translator-invented kind still reported without the module's agreement; `reconcile_buffer_address_table` and `report_synthesized_placeholders` already close the same gap for the other three. Sweeping all four kinds over the sample now finds none. Nothing moves. The interface pass allocates a binding per global before the unused ones are dropped, so the states that survive are still on the bindings that carry them -- confirmed by a reflection A/B over the sample, where 60 modules lose exactly those 112 entries, nothing is added, and no other field of any binding changes. The new fixture is three constexpr samplers with two samples, which reports a ghost at binding 161 on HEAD~. `assert_invented_descriptors_are_declared` now runs over every public fixture as well: it is the direction `assert_reflection_covers_declarations` does not check, and for a resource the translator invented the module is the only thing that can say a consumer has to bind it. `REFLECTION_VERSION` is now 41.
This commit is contained in:
1 parent
b6e10bf14c
commit
9e0e99a41d
5 files changed
+192
-2
No files matched your search
@@ -9,6 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- Reflected translation no longer asks a consumer to create a `VkSampler` at a binding the module
|
||||
does not declare. `StaticSampler` is a descriptor translation invents -- nothing in Metal's API
|
||||
puts one there, so the only reason a consumer creates it is that this reflection asked. Reflection
|
||||
named one per `!air.sampler_states` entry, which is every constexpr sampler state the source
|
||||
wrote, but a state no sample reads leaves a variable nothing references and the module drops it.
|
||||
112 such bindings in 60 of the 2880-source sample; `StaticSampler` was the last translator-
|
||||
invented kind still reported without the module's agreement, the other three already being
|
||||
reconciled by `reconcile_buffer_address_table` and `report_synthesized_placeholders`. The
|
||||
surviving samplers keep their bindings and their states -- the interface pass allocates before the
|
||||
drop -- and `tests/reflection_covers_declared_bindings.rs` now checks that direction for every
|
||||
invented kind over every public fixture, which is the one
|
||||
`assert_reflection_covers_declarations` does not. `REFLECTION_VERSION` is now 41.
|
||||
|
||||
- A color attachment whose Location is a function constant is read from the operand after its
|
||||
marker, like every other declared slot. `!"air.render_target", ptr addrspace(2) @loc, i32 0`
|
||||
states the Location as a global and the dual-source index after it; when the global was one the
|
||||
|
||||
+1
-1
@@ -230,7 +230,7 @@ binding them as ordinary vertex attributes.
|
||||
| `texture_shape` | Dim / arrayed / MS / component / writable / storage format, plus fixed handle-array length when present (`descriptor.count` is derived from it, so the two never disagree). Reflected translation corrects these to the `OpTypeImage` the module declares at the binding, so the view a consumer creates matches the image variable the descriptor is read through — a texel-read `texturecube` binds as a `D2` array, since SPIR-V has no cube texel fetch. A binding whose image variables do not all declare the same type (a function-constant-gated texture argument can produce that) keeps the type-name-derived shape, as does `reflect_sanitized`, which builds no module. |
|
||||
| `embedded_source` | For arg-buffer textures: owning buffer index, field byte offset, and Metal `[[id(n)]]` argument-encoder index |
|
||||
| `access` | When known: `Unused` / `ReadOnly` / `WriteOnly` / `ReadWrite` / `Sampled` / `Storage`. For a descriptor-backed buffer, reflected translation widens the declared classification to cover the loads and stores the finished module performs through it, so it is safe to barrier and stage from. It only ever widens: the analysis follows the pointer graph it can attribute, so a buffer it saw no write through may still be written via a device address. `reflect_sanitized` builds no module and reports the declared classification alone. |
|
||||
| `static_sampler` | Decoded immutable state for `StaticSampler`; `None` for other kinds |
|
||||
| `static_sampler` | Decoded immutable state for `StaticSampler`; `None` for other kinds. A `StaticSampler` binding is invented by translation, not declared by Metal, so reflected translation reports only the ones the finished module binds -- a constexpr sampler state no sample reads is dropped rather than asked for. `reflect_sanitized` builds no module and reports every state the AIR declares. |
|
||||
|
||||
### Buffer extent and access
|
||||
|
||||
|
||||
@@ -702,6 +702,10 @@ fn translate_sanitized_native_reflected_with_layout(
|
||||
// The image the emitter binds is not always the one the AIR type name implies -- a texel-read
|
||||
// cube binds as a 2D array -- and the view a consumer creates has to match the module.
|
||||
reflection.reconcile_texture_shapes(&finished.module);
|
||||
// A constexpr sampler no sample reads leaves a variable nothing references, and the module
|
||||
// drops it. Asking a consumer for a `VkSampler` at a binding the module does not declare is
|
||||
// asking for a descriptor nothing can read.
|
||||
reflection.retract_unbound_static_samplers(&finished.module);
|
||||
reflection.add_buffer_footprints(&finished.module)?;
|
||||
reflection.validate_descriptor_abi()?;
|
||||
Ok((finished.bytes, reflection))
|
||||
|
||||
+47
-1
@@ -84,7 +84,10 @@ mod footprint;
|
||||
/// descriptor-ABI ceiling, and the module declares the same `OpTypeArray`. Both now derive from
|
||||
/// `TextureShape::descriptor_count`. Only a runtime `array_ref`, whose length is not in the type
|
||||
/// name, still reports the ceiling.
|
||||
pub const REFLECTION_VERSION: u32 = 40;
|
||||
/// v41 stops reporting a constexpr sampler the finished module does not bind. A state no sample
|
||||
/// reads leaves a variable nothing references, which the module drops; reflection still asked a
|
||||
/// consumer to create a `VkSampler` there. 112 such bindings in 60 of the 2880-source sample.
|
||||
pub const REFLECTION_VERSION: u32 = 41;
|
||||
|
||||
/// Size in bytes of the twelve tightly packed `u32` values used by exact-thread dispatches: thread
|
||||
/// grid, thread base, threadgroup base, and total threadgroup grid (three dimensions each).
|
||||
@@ -2281,6 +2284,49 @@ impl ShaderReflection {
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a constexpr sampler the finished module does not bind.
|
||||
///
|
||||
/// `StaticSampler` is a descriptor the translator invents: nothing in Metal's API declares it,
|
||||
/// so the only reason a consumer creates a `VkSampler` at that binding is that this reflection
|
||||
/// asked for one. `add_static_samplers` names one per entry of the `!air.sampler_states` root,
|
||||
/// which is every constexpr sampler state the source wrote, and the interface pass gives each
|
||||
/// the same binding in the same order -- but a state no sample reads leaves a variable nothing
|
||||
/// references, and the module drops it. Reflection then describes a sampler that is not there.
|
||||
///
|
||||
/// Measured over the 2880-source sample: 112 such bindings in 60 modules, and `StaticSampler`
|
||||
/// is the ONLY translator-invented kind still reported without the module declaring it --
|
||||
/// `reconcile_buffer_address_table` and `report_synthesized_placeholders` already close the
|
||||
/// same gap for the other three. In all 60 the surviving samplers keep their bindings, so this
|
||||
/// removes ghosts without moving anything: the pass allocates before the drop, so the states
|
||||
/// that remain are still on the bindings that carry them.
|
||||
///
|
||||
/// `reflect_sanitized` builds no module and keeps the whole prediction, as it does elsewhere.
|
||||
pub(crate) fn retract_unbound_static_samplers(&mut self, module: &Module) {
|
||||
let types = module
|
||||
.types_global_values
|
||||
.iter()
|
||||
.filter_map(|instruction| Some((instruction.result_id?, instruction)))
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
let bound = module
|
||||
.types_global_values
|
||||
.iter()
|
||||
.filter(|instruction| instruction.class.opcode == spirv::Op::Variable)
|
||||
.filter_map(|instruction| {
|
||||
let variable = instruction.result_id?;
|
||||
let pointee = pointee_of(&types, instruction.result_type?)?;
|
||||
(types.get(&pointee)?.class.opcode == spirv::Op::TypeSampler)
|
||||
.then(|| descriptor_binding_of(module, variable))
|
||||
.flatten()
|
||||
})
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
self.bindings.retain(|resource| {
|
||||
resource.kind != ResourceKind::StaticSampler
|
||||
|| resource
|
||||
.descriptor
|
||||
.is_none_or(|location| bound.contains(&location.binding))
|
||||
});
|
||||
}
|
||||
|
||||
/// Report the descriptors the passes synthesized with no Metal argument behind them.
|
||||
///
|
||||
/// `air.get_read_sampler()` and `air.get_null_texture_*()` each make the interface pass bind a
|
||||
|
||||
@@ -626,6 +626,7 @@ fn every_public_fixture_reflects_every_descriptor_it_declares() {
|
||||
};
|
||||
declared += assert_reflection_covers_declarations(&label, &spirv, &reflection).len();
|
||||
assert_counts_cover_declared_arrays(&label, &spirv, &reflection);
|
||||
assert_invented_descriptors_are_declared(&label, &spirv, &reflection);
|
||||
checked += 1;
|
||||
}
|
||||
assert!(
|
||||
@@ -754,6 +755,7 @@ fn every_public_fixture_reflects_every_descriptor_it_declares_under_a_shifted_la
|
||||
);
|
||||
let module = assert_reflection_covers_declarations(&label, &spirv, &reflection);
|
||||
assert_counts_cover_declared_arrays(&label, &spirv, &reflection);
|
||||
assert_invented_descriptors_are_declared(&label, &spirv, &reflection);
|
||||
for (set, binding) in module
|
||||
.iter()
|
||||
.copied()
|
||||
@@ -978,3 +980,128 @@ fn each_constexpr_sampler_binding_reports_the_state_its_sample_reads() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Three constexpr sampler states where only two are ever sampled with.
|
||||
///
|
||||
/// The interface pass binds one sampler variable per `__air_sampler_state` global and reflection
|
||||
/// names one per `!air.sampler_states` entry, in the same order -- but a state no sample reads
|
||||
/// leaves a variable nothing references, and the module drops it. Reflection then asks a consumer
|
||||
/// for a `VkSampler` at a binding the module never declares. Nothing in Metal's API puts a
|
||||
/// descriptor there: the only reason to create one is that this reflection asked.
|
||||
const UNREAD_CONSTEXPR_SAMPLER: &str = r#"target triple = "spirv-unknown-vulkan1.2"
|
||||
|
||||
@__air_sampler_state.117 = internal addrspace(2) constant i64 -9188470239253747127, align 8
|
||||
@__air_sampler_state.118 = internal addrspace(2) constant i64 -9188470239253755319, align 8
|
||||
@__air_sampler_state.119 = internal addrspace(2) constant i64 -9188470239253757806, align 8
|
||||
|
||||
define <4 x float> @frag(<4 x float> %position, <2 x float> %coord, ptr addrspace(1) %tex, ptr addrspace(2) %runtime_sampler) {
|
||||
entry:
|
||||
%sample0 = tail call { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1) %tex, ptr addrspace(2) @__air_sampler_state.118, <2 x float> %coord, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0)
|
||||
%value0 = extractvalue { <4 x float>, i8 } %sample0, 0
|
||||
%sample1 = tail call { <4 x float>, i8 } @air.sample_texture_2d.v4f32(ptr addrspace(1) %tex, ptr addrspace(2) @__air_sampler_state.119, <2 x float> %coord, i1 true, <2 x i32> zeroinitializer, i1 false, float 0.000000e+00, float 0.000000e+00, i32 0)
|
||||
%value1 = extractvalue { <4 x float>, i8 } %sample1, 0
|
||||
%value = fadd <4 x float> %value0, %value1
|
||||
ret <4 x float> %value
|
||||
}
|
||||
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.fragment = !{!0}
|
||||
!air.sampler_states = !{!8, !9, !10}
|
||||
!0 = !{ptr @frag, !1, !3}
|
||||
!1 = !{!2}
|
||||
!2 = !{!"air.render_target", i32 0, i32 0, !"air.arg_type_name", !"float4"}
|
||||
!3 = !{!4, !5, !6, !7}
|
||||
!4 = !{i32 0, !"air.position", !"air.center", !"air.arg_type_name", !"float4"}
|
||||
!5 = !{i32 1, !"air.fragment_input", !"generated(coord)", !"air.center", !"air.perspective", !"air.arg_type_name", !"float2"}
|
||||
!6 = !{i32 2, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<float, sample>"}
|
||||
!7 = !{i32 3, !"air.sampler", !"air.location_index", i32 0, i32 1}
|
||||
!8 = !{!"air.sampler_state", ptr addrspace(2) @__air_sampler_state.117}
|
||||
!9 = !{!"air.sampler_state", ptr addrspace(2) @__air_sampler_state.118}
|
||||
!10 = !{!"air.sampler_state", ptr addrspace(2) @__air_sampler_state.119}
|
||||
"#;
|
||||
|
||||
/// Descriptor kinds nothing in Metal's API declares -- the translator invents the binding, so a
|
||||
/// consumer creates a descriptor there for no reason other than this reflection asking.
|
||||
const TRANSLATOR_INVENTED_KINDS: [ResourceKind; 4] = [
|
||||
ResourceKind::StaticSampler,
|
||||
ResourceKind::BufferAddressTable,
|
||||
ResourceKind::SynthesizedNullTexture,
|
||||
ResourceKind::SynthesizedReadSampler,
|
||||
];
|
||||
|
||||
/// No invented descriptor may be reported at a binding the module does not declare, and return how
|
||||
/// many were checked.
|
||||
///
|
||||
/// This is the direction `assert_reflection_covers_declarations` does not check. For a resource
|
||||
/// Metal declared there is an argument to report it whatever the module did with it; for one the
|
||||
/// translator invented there is none, and the module is the only thing that can say a consumer has
|
||||
/// to bind it. Over the 2880-source sample `StaticSampler` was the last kind still reported
|
||||
/// without that -- 112 bindings in 60 modules.
|
||||
fn assert_invented_descriptors_are_declared(
|
||||
label: &str,
|
||||
spirv: &[u8],
|
||||
reflection: &ShaderReflection,
|
||||
) -> usize {
|
||||
let declared = bindings_the_module_declares(spirv);
|
||||
let mut checked = 0;
|
||||
for resource in reflection
|
||||
.bindings
|
||||
.iter()
|
||||
.filter(|resource| TRANSLATOR_INVENTED_KINDS.contains(&resource.kind))
|
||||
{
|
||||
let Some(location) = resource.descriptor else {
|
||||
continue;
|
||||
};
|
||||
assert!(
|
||||
declared.contains(&(location.set, location.binding)),
|
||||
"{label} reports {:?} at ({}, {}), which the module declares no variable at, so a \
|
||||
consumer would create a descriptor nothing can read",
|
||||
resource.kind,
|
||||
location.set,
|
||||
location.binding
|
||||
);
|
||||
checked += 1;
|
||||
}
|
||||
checked
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_constexpr_sampler_no_sample_reads_is_not_reported() {
|
||||
let (spirv, reflection) = translate_sanitized_native_reflected(
|
||||
UNREAD_CONSTEXPR_SAMPLER,
|
||||
Stage::Fragment,
|
||||
&scratch("unread_constexpr_sampler"),
|
||||
TransformOptions::default(),
|
||||
)
|
||||
.expect("the three-sampler fragment translates");
|
||||
|
||||
assert_reflection_covers_declarations("the unread-sampler fragment", &spirv, &reflection);
|
||||
assert_eq!(
|
||||
assert_invented_descriptors_are_declared(
|
||||
"the unread-sampler fragment",
|
||||
&spirv,
|
||||
&reflection
|
||||
),
|
||||
2,
|
||||
"the two sampled states are reported and the unread one is not"
|
||||
);
|
||||
|
||||
// The two that survive keep the bindings their samples read, with their own states: retracting
|
||||
// the ghost must not renumber what is left.
|
||||
let reported = reflection
|
||||
.bindings
|
||||
.iter()
|
||||
.filter(|binding| binding.kind == ResourceKind::StaticSampler)
|
||||
.filter_map(|binding| {
|
||||
Some((
|
||||
binding.descriptor?.binding,
|
||||
binding.static_sampler?.raw_words[0],
|
||||
))
|
||||
})
|
||||
.collect::<std::collections::HashMap<_, _>>();
|
||||
let sampled = sampler_bindings_in_sample_order(&spirv);
|
||||
assert_eq!(sampled.len(), 2, "both samples reach a static sampler");
|
||||
for (binding, expected) in sampled.iter().zip(CONSTEXPR_SAMPLER_WORDS) {
|
||||
assert_eq!(reported.get(binding).copied(), Some(expected));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user