Skip to content

rdna2spirv: compact SPIR-V modules on request (SHADER-COMPACT-SPIRV) - #1073

Closed
Moriz82 wants to merge 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/shader-compact-spirv
Closed

Moriz82 wants to merge 1 commit into
ProjectLeonida:mainfrom
Moriz82:split/shader-compact-spirv

Conversation

@Moriz82

@Moriz82 Moriz82 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Rebased onto bfef489c at 363fed0f. Main already exposes SPIR-V compaction. This diff adds the contract row and tests that the build command forwards the flag. The two focused suites ran 49 cases with five Windows-only skips.

Claimed in the coordination issue as: SHADER-COMPACT-SPIRV (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: #984 is the parent integration. #544 changes the existing Python optimizer. The two options can run independently. A merge-tree check against #1068 also completed without conflicts.

What and why

The translator and shader build rejected --compact-spirv on Linux. To try smaller modules, a developer had to run the optimizer separately and validate the result again. This adds the option to both entry points. It runs local load elimination, redundancy elimination and dead-code elimination on the assembled staging file before the normal validation and publication step. It preserves stage interfaces, descriptor bindings and specialization constants. Compaction is off by default. Only an opted-in build needs spirv-opt.

A hand-written pixel program produced a 4,076-byte module by default and a 908-byte module with compaction. Both the Python assembler and spirv-as produced those sizes. All four outputs passed Vulkan 1.2 validation and had matching shader index metadata. This is a synthetic size comparison.

Tested

Ran the compiler tests on Linux and cross-built the Windows runtime.

Applied only the two test additions to pristine main 32950551e, then ran the same commands on this child:

Command Main plus tests Child
python3 -I tests/test_native_tool_paths.py AssembleSpirv.test_compact_binary_before_publish 1 error. The assembler function rejected the compact keyword. 1 passed
python3 -I tests/test_make_game_translator_flags.py MainWiringTest.test_compact_spirv_reaches_the_shaders_stage 1 error. The build CLI rejected --compact-spirv. 1 passed
Command or check Child result
python3 -I tests/test_native_tool_paths.py 8 passed, 5 Windows-only skips
python3 -I tests/test_make_game_translator_flags.py 34 passed
python3 -I tests/test_run_gpu_tests_python_assembler.py 10 passed
python3 -I scripts/run_semantic_tests.py tests/semantic -j 2 6,168 tests in 470 files, zero failing files. Standalone run 643 s, full CI repeat 455 s.
python3 -I -m compileall -q make_game.py tools tests Passed
Python CI loop over 520 nonsemantic files, using python3 -I <file> or python3 -I -m pytest -q <file> One failing file on each tree, tests/test_imhex_patterns.py. The same 6 of its 9 tests failed. Zero new failing files.
python3 -I scripts/check_no_game_data.py Passed, 2,457 tracked entries
cmake -S runtime -B runtime/build/boot and cmake --build runtime/build/boot --target gta6rt -j6 Linked the x86-64 Windows executable using the configured cross-toolchain.

On a second synthetic module, spirv-dis retained the location, descriptor set, binding and specialization ID after compaction, and the dead add disappeared. An optimizer failure took the existing rejection path and left zero stale modules or staging files. With spirv-opt absent, the default CLI succeeded and the compact CLI reported the missing tool with exit code 2.

GPUs tested: none. The tests ran on the CPU. Native Windows, Wine, AMD GPU and Intel GPU runs were not performed.

AI-assisted: yes

Checklist

  • Based on upstream main at the cut, 32950551e. Merged cleanly in a temporary checkout of the newer 3aa975421. The native tool paths, build flag and build destination suites ran 56 cases there, with 51 passes and five Windows-only skips.
  • One topic in one commit.
  • Synthetic inputs and a passing game-data check.
  • The SPIR-V compaction contract and its confirmation are in the translator contract table in docs/design.md.

🤖 Generated with Claude Code

@sodove

sodove commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent Windows check of b22d5af4 on an RTX 5070 Ti (driver 610.74, Windows 11 build 26100.7623, SPIRV-Tools 2026.1). I used eight hand-written fixtures from the repository, with both spirv-as and the Python assembler.

  • For each assembler, --compact-spirv reduced the combined size from 64,976 to 49,528 bytes (23.8%). All eight modules got smaller. All 32 outputs passed spirv-val for Vulkan 1.2, and the eight-entry shader index matched across all four configurations.
  • Seven 64×64 draws per configuration ran on native Windows Vulkan with subgroup size 32. Each passed the fixture checks. All four channels of every pixel matched between plain and compact output for both assemblers. The ballot/atomic case also produced the expected counter and the same set of 1,024 writes, allowing for allocation order.
  • Reused one two-shard output directory with compaction off, on, then off again. All eight binaries and the merged index matched the corresponding clean translation at every step.
  • Both assemblers also worked with a Unicode output path. With spirv-opt removed from the tool path, plain translation still succeeded and a compact translation into a fresh directory exited 2, named the missing tool, and published no module or index.

Synthetic fixtures only. AI-assisted: yes (OpenAI Codex).

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed and verified. Introduces optional --compact-spirv flag running spirv-opt (local load/dead-code/redundancy elimination) on assembled SPIR-V modules before validation, while preserving interface decorations and descriptor bindings. Clean fallback handling when spirv-opt is absent. Verified with tests/test_native_tool_paths.py and test_make_game_translator_flags.py. 100% clean-room.

@iFeyz

iFeyz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

AMD run (the gap the description lists): RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11, MSVC 2022 RelWithDebInfo.

Head c86f50e87:

  • scripts/run_gpu_tests.py with this translator: 261 of 261 shaders translated and spirv-val-clean, 236 of 252 pass, 16 skipped, 0 failed (native, subgroup 64).
  • tests/test_make_game_translator_flags.py (34), tests/test_native_tool_paths.py (13): OK.

@whoalin1

Copy link
Copy Markdown
Contributor

GPU/tooling verification (AI-assisted, overnight worker w4; independent run, not the author's).

Head tested: c86f50e. Note: run_gpu_tests.py does not forward --compact-spirv, so I verified compacted modules by translating the full GPU-test corpus with the flag and A/B-running every module against its non-compacted twin on both GPUs.

Translator correctness (CPU)

  • tests/test_native_tool_paths.py: 13 tests OK (the new test_compact_binary_before_publish ran and passed; 1 unrelated 8.3-name skip).
  • tests/test_make_game_translator_flags.py: 34 tests OK.
  • scripts/run_semantic_tests.py tests/semantic: 6168 tests / 470 files / 0 failed in 552 s — matches the PR's claim.

Compact translation of the GPU corpus

  • tools/rdna2spirv.py <disasm> --out <spirv> --compact-spirv --pc-tail-ubo (Vulkan SDK 1.4.363 spirv-opt/spirv-val): 261/261 shaders translated, all pass spirv-val. Every module is smaller; corpus total ~15% smaller (6.44 MB → 5.50 MB).

GPU A/B, AMD Radeon iGPU (gfx1035, driver 24.10.24, Vulkan 1.3.280) — env: VK_LOADER_DRIVERS_SELECT=*amd-vulkan* LEONIDA_VK_DEVICE=radeon VK_LOADER_LAYERS_DISABLE=~implicit~ LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1

  • All 126 cs_ modules*: vk_compute_test <spv> 4 dump with VKC_DUMP — identical exit codes and byte-identical full buffer dumps vs the non-compacted modules.
  • 15 draw cases via vk_harness (9 ps, 3 vs, gs_tri, ms_tri, ms_v5_workgroup): byte-identical raw output.

GPU A/B, NVIDIA RTX 4070 SUPER — env: VK_LOADER_DRIVERS_SELECT=*nv-vk* LEONIDA_VK_DEVICE=4070 VK_LOADER_LAYERS_DISABLE=~implicit~

  • Same 126 cs_* dump comparison and same 15 draw cases: all identical.

Verdict: the compact pass preserves semantics on both GPUs and keeps every module valid; size win is real (~15%). LGTM from this hardware.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
Add --compact-spirv to make_game and the translator. Run the selected
SPIRV-Tools passes on the staging file before validation. Keep the default
output and the runtime interface contract.

Both new tests error on main without the option and pass here. The semantic
suite ran 6168 tests in 470 files with no failing files. The Windows
runtime cross-build and tracked-file content check passed.

AI-assisted (Codex and Claude Code agents).

Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Moriz82
Moriz82 force-pushed the split/shader-compact-spirv branch from c86f50e to 363fed0 Compare November 25, 2026 03:38
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
@Moriz82 Moriz82 closed this by deleting the head repository Nov 26, 2026
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.

5 participants