Skip to content

spirv_metal_set0: specialize helper functions for resized set-0 descriptor arrays - #9167

Open
xDeadXD wants to merge 2 commits into
ItsNotRexed:mainfrom
xDeadXD:spirv-metal-set0-helper-params
Open

xDeadXD wants to merge 2 commits into
ItsNotRexed:mainfrom
xDeadXD:spirv-metal-set0-helper-params

Conversation

@xDeadXD

@xDeadXD xDeadXD commented Nov 23, 2026

Copy link
Copy Markdown

Claim: #9065 (comment) (finding by @fuzeofficlal).

Summary

  • Previously, when ools/spirv_metal_set0.py resized a set-0 runtime descriptor array and it was passed to a helper function via OpFunctionCall, the helper's OpTypeFunction and OpFunctionParameter still expected the original OpTypeRuntimeArray pointer, causing spirv-val to fail on OpFunctionCall argument/parameter type mismatch (even for DontInline and unused helpers).
  • Added _specialize_helper_calls in ools/spirv_metal_set0.py:
    • Clones each called helper function per distinct retyped argument-type signature (callee, call_sig) with fresh SSA IDs, updated OpFunctionParameter types, and matching OpTypeFunction (inserted before the first OpFunction so all resized pointer types are declared).
    • Preserves function control flags (e.g. DontInline), reuses existing/previously generated OpTypeFunction and helper clones when signatures match, leaves untouched set-1 / non-resized calls targeting the original helper, handles OpCopyObject inside callers/clones, handles transitive/chained helper calls and self-recursion via a worklist, and updates unused helpers.

Testing

Windows 11 2024 IoT LTSC (Ryzen 7 5700X, RTX 5070, Python 3.12, CPU-only; no spirv-val/spirv-as, no pytest, no game files):

  • Negative control on origin/main (867d922): python -B -I tests/test_spirv_metal_set0_helpers.py -v → 7/7 failures (%msl_ptr_b* != %ptr_*).
  • python -B -I tests/test_spirv_metal_set0_helpers.py -v → 7/7 OK.
  • python -B -I tests/test_spirv_metal_set0_aliases.py -v → 9/9 OK.
  • python -B -I tests/test_spirv_metal_set0_cli_errors.py -v → 7/7 OK.
  • python -B -I -m compileall -q tools/spirv_metal_set0.py tests/test_spirv_metal_set0_helpers.py → OK.
  • python -B -I scripts/check_no_game_data.py tools/spirv_metal_set0.py tests/test_spirv_metal_set0_helpers.py → OK.
  • git diff --check → clean.

AI-assisted.

@Grimkeh Grimkeh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Linux CPU review

What changed: spirv_metal_set0 specializes helper functions when set-0 descriptor arrays are resized; new synthetic SPIR-V helper suite.

Checked: tests/test_spirv_metal_set0.py / helpers — 8 passed.

Looks correct; no game files.

@catminou4

Copy link
Copy Markdown

Tested on Apple M1 Max, macOS 27.2.1 arm64, Python 3.12.16: python -I tests/test_spirv_metal_set0_helpers.py → 7 tests OK. No game files. AI-assisted (Devin).

@Barbital11111

Copy link
Copy Markdown
Contributor

Independent check at ef43720 on WSL Ubuntu, Python 3.12.3, SPIRV-Tools v2023.6. Real assemble -> wrapper -> spirv-val passed in all three synthetic cases: direct calls, forwarding through a second DontInline helper, and calls inside an unused function. Each mixed two resized set-0 signatures and an unchanged set-1 signature.

One semantic issue remains in the clones. I put a direct NonUniform decoration and grouped RelaxedPrecision on an OpIAdd index used by the helper. After rewriting, only 1 of the 3 copies of that index retains either decoration. The two resized variants lose both, even though spirv-val accepts the output. A local control that copies direct decorations and adds cloned IDs to the group preserved both decorations on 3 of 3 indices in all three cases.

Please carry the decorations on cloned local IDs over to the new IDs, including OpGroupDecorate targets. CPU only, synthetic modules. AI-assisted (Codex).

Earlier coordination claim

@check3D

check3D commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Reviewed ef43720c merged cleanly onto upstream/main (867d922f) on Ubuntu 24.04 / Python 3.12.16 (CPU only, no game files, no spirv-as/spirv-val).

Checks & tests:

  • git diff --check upstream/main..HEAD and python3 -I scripts/check_no_game_data.py tests/test_spirv_metal_set0_helpers.py tools/spirv_metal_set0.py: clean.
  • python3 -I tests/test_spirv_metal_set0_helpers.py -v: 7 passed.
  • python3 -I tests/test_spirv_metal_set0_aliases.py -v: 9 passed.
  • python3 -I tests/test_spirv_metal_set0_cli_errors.py -v: 7 passed.
  • python3 -I tests/test_ci_registration.py -v: 14 passed.
  • Negative control (reverting tools/spirv_metal_set0.py to upstream/main): tests/test_spirv_metal_set0_helpers.py ran 7 tests with 7 failures (%msl_ptr_b* != %ptr_*).

Bug found (tools/spirv_metal_set0.py:159):

  • Initializing id_map = {callee: clone_fn} unconditionally rewrites every recursive OpFunctionCall target inside the cloned body to clone_fn before the worklist inspects call-site argument types. Because the worklist only looks up orig_funcs.get(callee) (which does not contain clone_fn), those rewritten recursive calls are skipped even when the call passes a different descriptor-array signature, producing invalid SPIR-V that fails _assert_valid_calls_and_types:
    • A recursive helper specialized for %images0 that calls %rec %foreign_images (untouched set-1) is rewritten to %rec_msl %foreign_images (%ptr_img passed where %msl_ptr_b0 is expected).
    • A recursive helper specialized for %images0 that calls %rec %images3 (a different set-0 binding) is rewritten to %rec_msl_1 %images3 (%msl_ptr_b3 passed where %msl_ptr_b0 is expected).
    • A 2-parameter recursive helper swapping arguments (%rec2 %p1 %p0, called with %images0 %images3) is rewritten to %rec2_msl %p1_msl %p0_msl ((%msl_ptr_b3, %msl_ptr_b0) passed where (%msl_ptr_b0, %msl_ptr_b3) is expected).
  • Fix: start with id_map = {} (do not pre-map callee -> clone_fn) and let the worklist resolve recursive OpFunctionCalls via (callee, call_sig): same-signature recursion still hits clones[(callee, call_sig)], untouched set-1 calls keep the original callee, and changed-signature recursive calls specialize to the matching clone. Please also add a regression test covering a changed-signature or swapped-argument recursive call.
  • Nit (tools/spirv_metal_set0.py:174-175): for idx_p, (p_id, orig_pty) in enumerate(fn["params"]): is simpler than fn["params"].index((p_id, orig_pty)).

Cross-PR overlap notes:

Needs changes.

AI-assisted: yes.

@padhossein

Copy link
Copy Markdown
Contributor

AI-assisted independent verification of ef43720. Seven helper specialization unit tests passed on Linux. I separately ran all four existing descriptor-array CLI cases with SPIRV-Tools 2025.1, and both the input and rewritten binaries passed spirv-val for Vulkan 1.2. The helper-specific cases exercised text rewriting.

@xDeadXD

xDeadXD commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thank you @Barbital11111 for catching the missing cloned-ID decorations! Fixed in commit d62f6d7:

  • Carried direct OpDecorate lines and OpGroupDecorate group targets onto all cloned local IDs in tools/spirv_metal_set0.py.
  • Added test_cloned_helper_preserves_direct_and_group_decorations in tests/test_spirv_metal_set0_helpers.py (verifying both OpDecorate NonUniform and OpGroupDecorate RelaxedPrecision across all 3 copies).

Re-ran on Windows 11 2024 IoT LTSC (Python 3.12, CPU-only, no spirv-val): test_spirv_metal_set0_helpers.py 8/8 OK, test_spirv_metal_set0_aliases.py 9/9 OK, test_spirv_metal_set0_cli_errors.py 7/7 OK. AI-assisted.

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested ef43720c on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main 867d922f: pass

  • python -I tests/test_spirv_metal_set0_helpers.py: Ran 7 tests, OK. With only the non-test files reverted to main: FAILED (failures=7).
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

An agent checked that #8548 + this PR merge cleanly and their tests pass together.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

@Barbital11111

Copy link
Copy Markdown
Contributor

Rechecked d62f6d7 with the same WSL / SPIRV-Tools v2023.6 cases. Direct, forwarded and unused helper cases all passed spirv-val. The original and both resized helpers now retain NonUniform and grouped RelaxedPrecision on their index in every case (3 of 3 for each decoration). Synthetic modules only. AI-assisted (Codex).

@Sirwq

Sirwq commented Nov 23, 2026

Copy link
Copy Markdown

Note

🤖 GPT-6.2 Astra responding on behalf of Sirwq

Tested d62f6d7d1 on Apple M4 with the native Metal driver in macOS 27.2.1 (26B131). Pass.

I used a synthetic compute shader that calls the same helper twice, once for each texture array at set 0 bindings 0 and 3. On base 867d922f0, spirv-val --target-env vulkan1.2 rejected the rewritten module because the call argument and helper parameter had different pointer types.

On the PR head, the rewritten module passed validation. I converted it with SPIRV-Cross 1.4.363.0 using Metal argument buffers, compiled the generated source with Metal, and dispatched it on the M4. The GPU returned [17, 93] on the first dispatch and [93, 17] after I swapped the two textures. Both reads matched exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants