Files
metal2vulkan/tests
Anees Iqbal cae8971fb7 Do not bind a sampler for a read that never samples
Metal's `texture.read(coord)` takes no sampler. AIR threads one in anyway, from
`air.get_read_sampler()`, and `lower_read` discards it -- so `default_read_sampler`
materialized a real `OpTypeSampler` UniformConstant variable, decorated it with a
DescriptorSet and Binding, and put it on the entry-point interface purely to give
that discarded value a type.

The result was a descriptor the shader never samples with, and one no consumer
could learn about: no Metal argument corresponds to it, so `ShaderReflection` has
nothing to report, while the module demands the binding regardless. A consumer
building a `VkDescriptorSetLayout` from the reflection produces a layout that does
not cover its own shader.

Measured over 2880 corpus sources: 267 bindings the module declared and reflection
omitted; 250 of them were this sampler, and in every single case the load of it had
no consumer at all -- 0 of 250. (The other 17 are two smaller classes, unchanged
here and stated below.)

The fix retracts the synthesis rather than papering over it in reflection: after
lowering, drop the loads of the default sampler that feed nothing, and the sweep
added in 7aac6cd removes the now-unreferenced variable, its decorations, and its
interface entry. This is not general dead-code elimination, which this pipeline
deliberately does not do -- it withdraws one descriptor the translator itself
synthesized, only where the synthesis turned out to be unnecessary. A load whose
result IS consumed keeps the sampler.

Verified over 2880 corpus sources, release build, rebuilt for the run:

  - reflection-omitted bindings: 267 -> 17
  - 251 sources differ: 250 with output changes, plus one whose FALLBACK
    diagnostic names %383 instead of %384 because one fewer id is allocated. Same
    failure, same class. The remaining 2629 are byte-identical.
  - removed across those 250: 250 OpVariable, 500 OpDecorate, 297 OpLoad, 180
    OpTypeSampler, 180 OpTypePointer; 22,072 bytes. Nothing else changed.
  - spirv-val --target-env vulkan1.2: 2676 VALID / 204 FALLBACK, unchanged

`tests/reflection_covers_declared_bindings.rs` states the contract: every
`(set, binding)` a module decorates on a variable must appear in reflection, from
any of the three places reflection carries one -- `bindings`,
`implicit_imageblock_attachments`, and `fragment_imageblock.members`. The reverse
is allowed and deliberate: Metal may declare a resource the shader never touches,
and dropping it would renumber a consumer's expectations.

The authored fixture is a `texture.read` kernel and it is not vacuous: before this
change it emitted `OpDecorate %28 Binding 160` and an `OpTypeSampler`, and now it
emits neither. The fixture sweep runs at each fixture's declared stage.

Residue, stated rather than hidden. 17 declared-but-unreflected bindings remain on
the corpus: 13 StorageBuffer blocks in the band above 608 and 4 synthesized
textures. Separately, `from_fragment` and `from_vertex` hardcode an empty
`implicit_imageblock_attachments` while the detector that fills `from_kernel`'s is
stage-agnostic and the passes emit the plane regardless of stage -- so translating
`kernel_implicit_imageblock_half2.ll` as fragment declares Binding 200 and reflects
nothing. All five corpus users of those intrinsics are kernels, so nothing here is
evidence about real fragment AIR; it is a reporting gap, and the reason this file's
sweep does not vary the stage.

Gate: fmt, clippy -D warnings, warnings-denied all-features rustdoc, cargo test
-p metal2vulkan (debug and release), cargo test -p metal2vulkan-validation.
2026-08-31 22:36:48 +03:00
..
2026-07-22 13:15:24 +03:00