Commit Graph
1 Commits
Author SHA1 Message Date
Anees Iqbal 7aac6cdb3a Stop shipping descriptor bindings no instruction reads
A translated module could declare a module-scope OpVariable that no
instruction anywhere references, list it in the entry point's interface, and
decorate it with a DescriptorSet and Binding. A consumer that builds a
VkDescriptorSetLayout from the interface -- the normal way to do it -- then has
to bind a descriptor the shader never touches.

Measured on the private corpus: 58 of 2880 sources, 1105 stranded variables
(1053 Private, 40 StorageBuffer, 12 UniformConstant). spirv-val accepts every
one of them, which is why nothing noticed.

The cause is a liveness snapshot that outlives the bodies it described.
`finalize` filters the entry-point interface to the variables the function
bodies reference, which is right -- and `function_referenced_ids` already says
so: "Entry-point interface variables should be live because code loads/stores/
access-chains them, not because an earlier pass registered them before a later
raw-alias rewrite replaced every use." But instruction-deleting work runs after
it at two boundaries: the pointer and access-chain closures at the tail of
`transform`, and dead-value elimination, constant-CFG pruning, and CFG
construction in `finish_module`. Once one of those deletes a variable's last
use, the arrangement is self-sustaining and nothing downstream can undo it:
`gc_dead_globals` roots liveness AT the entry-point interface, so the interface
entry keeps the variable alive and the variable justifies the interface entry.

`module_cleanup::drop_unreferenced_global_variables` breaks that cycle. It is
the rule stated once -- a global variable lives because an instruction names it
-- applied to the interface, the variable, its decorations, and its debug names
as one transaction, and it runs at each boundary after the last instruction
change there. It can only shrink the interface (an id it keeps is one an
instruction still names), so SPIR-V 1.4's requirement that the interface list
every global the entry point references is preserved. At the `finish_module`
boundary it is placed ahead of the existing null/undef constant sweep so the
initializers it strands are collected in the same step.

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

  - stranded module-scope variables: 58 modules -> 0 modules
  - 58 modules differ, every one still OK; the other 2822 are byte-identical,
    including all 204 unchanged FALLBACK messages
  - removed across those 58: 1105 OpVariable, 1050 OpConstantNull, 104
    OpDecorate, 2 OpName; 40,636 bytes. Nothing else changed.
  - spirv-val --target-env vulkan1.2: 2676 VALID / 204 FALLBACK, unchanged
  - corpus total 100.1s -> 100.7s, slowest source 2.54s -> 2.57s

Known residue, stated rather than hidden: removing the variables leaves 47
OpTypePointer and 2 OpConstantComposite declarations unreferenced across 29
modules. Those are inert declarations with no interface entry and no binding;
collecting the general type graph at that boundary is `gc_dead_globals`' job
and it does not run there.

Tests. The unit tests in `module_cleanup` are the pin: a module with two
descriptors in its interface and a load of only one must come out with the
stranded variable gone from the interface, the globals, its decoration, and its
name, with the live one untouched -- and the sweep must reach a fixed point in
one pass, since the pipeline runs it at more than one boundary.
`tests/live_module_scope_variables.rs` is the end-to-end net: no fixture, under
any stage it translates under, may produce a module declaring a global variable
no instruction references. That sweep is honest but currently vacuous on the
committed fixtures -- it still passes with both call sites removed. The trigger
needs a use deleted by SPIR-V-level constant folding, and llvm-reduce converged
only on inputs (a load through a null constant pointer) too artificial to
commit as an owned fixture. The corpus numbers above are the evidence that the
defect was real; the guard is what keeps a future fixture from reintroducing it
silently.

`fc_specialize::drop_unreferenced_entry_interface_globals` is a third statement
of the same rule, narrowed to variables that were already in the interface.
Consolidating it is a separate change; it is a public API's behavior.

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:09:01 +03:00