Skip to content

make_game: the shaders stage translates with --opt unless --no-spirv-opt (-36.5% SPIR-V) - #4760

Open
mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:build-make-game-spirv-opt
Open

mrp1no wants to merge 2 commits into
ItsNotRexed:mainfrom
mrp1no:build-make-game-spirv-opt

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

New BUILD item. Claim: #4683 (comment)

tools/spirv_opt.py (#2298) is opt-in (rdna2spirv.py --opt), and make_game.py never passed it, so the game's modules were the unoptimized ones. #884 is about how large those modules get. This turns it on for the shaders stage. The gain is size, measured below and over the whole corpus in the comments; driver compile time of the largest modules does not improve (see @kapitche's timing in the comments).

Commits

  1. run_gpu_tests: LEONIDA_SPIRV_OPT=1 translates the fixtures with --opt, the same way LEONIDA_FMA adds --fma. Until now the GPU suite never ran on optimized modules.
  2. make_game: --opt by default, and --no-spirv-opt to turn it off.
    • --opt joins the forwarded translator flags, after the fallbacks. It reaches every shard and the FSPX translation.
    • It is recorded in translator_flags.txt and the shard plan, so turning it on or off translates again. spirv_opt.py was already in TRANSLATION_SOURCES, so a change to the optimizer re-translates too.
    • The make_game tests about the fallbacks pass --no-spirv-opt, so they keep checking the fallbacks alone.
    • test_spirv_opt_is_on_by_default checks the default, the opt-out, and the order with a fallback.
    • docs/testing.md explains what --opt does and how to run the GPU suite on optimized modules.

The tests that read raw translator output call the translator directly, without --opt, so they are unaffected.

Measured (main 98fa503f, Windows 11, RTX 3060 / NVIDIA 610.88)

Corpus: shard 0/40 of my own 1.0.2.1 copy, counts only (#884).

default --opt
translated / spirv-val 663 / 663 663 / 663
total SPIR-V 503.6 MiB 319.7 MiB (−36.5%)
translation time 509 s 527 s (+3.5% with other work running; +50% on an idle machine with --wave64-emulate, see the comments)

Every module is smaller: median −36.3%, from −11.0% to −68.8%.

GPU suite: the same test programs on both sides, with LEONIDA_SPIRV_OPT set to 0 and to 1.

without --opt with --opt
native 170/217 passed, 47 skipped 170/217 passed, 47 skipped
LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped 201/217 passed, 16 skipped

There were no failures in any of the four runs.

Tests: tests/test_make_game*.py gives 356 passed, and the docs tests pass.

Not measured: driver compile time and memory on the optimized modules. My run of that was stopped by memory pressure on this machine. A full-corpus pipelines run with this PR would settle it, and the largest modules shrink by about a third (12.4 → 7.8 MiB in this shard).

No game files. AI-assisted (Claude Code).

🤖 Generated with Claude Code

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 690b9711 on Linux (x86_64, Python 3.13.5): pass (code read + unit tests).

What I ran:

  • git diff --check main...HEAD: clean.
  • python3 -m unittest tests/test_make_game_argparse.py tests/test_make_game_translator_flags.py tests/test_make_game_wave64.py tests/test_make_game_preflight.py: 182 tests, OK (2 Windows-only skipped), including test_spirv_opt_is_on_by_default.

Code review:

  • Adding ("--opt", "spirv_opt") to FALLBACK_FLAGS with --no-spirv-opt (action="store_false") forwards --opt by default and records it in translator_flags.txt so toggling --no-spirv-opt cleanly triggers re-translation.
  • Confirmed spirv_opt.py is already in TRANSLATION_SOURCES (make_game.py:1297), so optimizer edits invalidate cached SPIR-V automatically.
  • Exposing LEONIDA_SPIRV_OPT=1 in scripts/run_gpu_tests.py and documenting both in docs/testing.md is tidy and consistent with LEONIDA_W64_EMULATE / LEONIDA_FMA.

@Kam471 Kam471 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.

Ran this at 690b97113 on Windows 11: pytest tests/test_make_game_translator_flags.py tests/test_make_game_argparse.py tests/test_make_game_wave64.py -q -> 101 passed, 37 subtests passed; git diff --check clean.

The shaders stage now passes --opt by default (the translator has it at rdna2spirv.py:15880) with --no-spirv-opt to opt out, and the flag joins the recorded translator settings so a change retranslates. The added tests cover the default, the opt-out and a flag change forcing a retranslation; the docs note the size/result expectation. No blocking issue.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 690b9711 on Windows 11 with the RTX 4090 and AMD Radeon iGPU. Both GPUs return all eight expected words for the repository’s synthetic cs_store fixture with optimization on and off.

I translated the fixture instructions with rdna2spirv.py and assembled the emitted text with spirv_asm.assemble. The binary is 275,508 bytes without --opt and 199,908 bytes with it. I ran vk_compute_test.exe cs_store.spv 1 dump for each binary on each adapter. The device log confirms each selection. Each run reports zero mismatches. This check covers one shader. The external spirv-val tool was unavailable.

With CPython 3.14.7, the make_game tests, GPU runner tests, and optimizer tests report 406 passed and one skipped. The new default-flag test fails with make_game.py from 98fa503f.

NVIDIA driver 32.0.16.2101. AMD driver 32.0.21043.5001.

AI-assisted.

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

The same shard with --wave64-emulate, which make_game.py picks for wave64 compute on 32-wide GPUs (NVIDIA, Intel). Shard 0/40 of my own 1.0.2.1 copy on main 98fa503f, counts only:

--wave64-emulate --wave64-emulate --opt
translated / spirv-val 663 / 663 663 / 663
total SPIR-V 505.9 MiB 323.5 MiB (−36.1%)
translation time 281 s 422 s

So the optimizer's output is valid on the emulated modules too, with the same saving.

The time is less clean than in the description. These two runs had the machine to themselves, while the native pair ran next to other work. Here --opt costs about 50% more translation time, which is roughly 2.4 more minutes for this 1/40 of the corpus on one core, and make_game spreads the shards over --jobs. Thanks @shdann and @Kam471 for the checks. AI-assisted (Claude Code).

@Flyander Flyander 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 the current head. On Python 3.12.10, the targeted -I suites passed: make_game translator flags (27), argparse (36), wave64 (38), GPU-runner behavior (41), GPU-runner paths (20), SPIR-V optimizer (9), docs CLI flags (3), and docs environment variables (4). The mocked translator failure text in the flags suite is expected; all tests passed. No GPU or game files were used in this local check.

@PingasMech

Copy link
Copy Markdown

Tested 690b9711 on an Intel UHD Graphics 770, Windows driver 32.0.101.7088. Enabling --opt preserved every GPU test outcome. In each wave mode, all 71 saved GPU readbacks matched byte for byte between settings.

Synthetic GPU suite Passed / skipped with either setting Total SPIR-V reduction
Native 150 / 67 41.9%
Wave64 emulation 180 / 37 40.5%

All four runs translated and passed spirv-val on the same 220 of 221 inputs. The computed I/J fixture hit the same translator limitation throughout.

For a first-use pipeline timing, I changed one stored alpha value in cs_store to make eight distinct shader pairs. Median vkCreateComputePipelines time fell from 70 ms to 50 ms. Median peak working set for the whole checker process was 58.6 MiB to 57.9 MiB. Each measurement used a fresh process, one worker and an empty application cache, with the run order balanced across pairs. The checker rounds to 10 ms. The driver’s private cache stayed enabled. All 16 dispatches returned their eight expected words. This measures one synthetic shader family. Full-corpus compile cost and memory pressure remain open.

Separately, the generic pipeline checker built the same 95 of 105 emulated compute pipelines in both configurations. The same nine descriptor-using pipelines failed creation, and one skipped for missing FP64. Seven of those nine passed in their dedicated GPU harnesses.

AI-assisted, using synthetic fixtures only.

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

FSPX as well, since --opt now also reaches the FidelityFX upscaler's 29 programs: rdna2spirv.py <work>/shaders/fspx/disasm on 98fa503f translates 26/26 and passes spirv-val 26/26 with and without --opt, both natively and with --wave64-emulate. The output goes from 15,920 to 10,639 KiB (−33%), and from 15,956 to 10,694 KiB emulated. The same 3 programs are refused in all four runs (the known v_fma_mix_f32 and dot4 encoding cases), so --opt changes nothing about which programs translate. Thanks @PingasMech for the Intel run and the pipeline-creation timing. My own copy, counts only. AI-assisted (Claude Code).

@Kev-Ball

Copy link
Copy Markdown

The full 1.0.2.1 DXBC translation comparison finished on 690b9711 with --wave64-emulate on CachyOS. With and without --opt, all 26,503/26,503 shaders translated and passed spirv-val. I checked that the merged indexes contain the same entries, source digests and stages before pairing the output sizes.

Every module was smaller with optimization. Total SPIR-V bytes went from 19,007,248,456 to 12,172,654,240 (35.96% reduction). The median per-module reduction was 35.08%, ranging from 8.39% to 86.12%. This extends the size and validation result from the original 663-shader slice to the whole DXBC corpus.

The RTX 3080 Ti driver compile-time and peak-RSS comparison has now started on those same module pairs. AI-assisted, aggregate results only.

@kapitche

Copy link
Copy Markdown
Contributor

Driver compile time for the --opt modules (RTX 4080 SUPER, one module per process, main 3faac55, counts only): SPIR-V -33% to -35% on four native wave32 compute modules, compile time 166k-instruction module 39.8 s vs 28-42 s base (within noise), 128k one 22-26 s vs 30-34 s, 146k one 10.9 s vs 12.1 s, 461k one 323 s vs 292 s (+11%, single run). So the size win does not carry to the slowest modules. Full ranking: #4933 (my finding today).

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@kapitche Thanks, that's the measurement I couldn't finish. It changes what this PR can claim. The gain is size: about 36% less SPIR-V on disk and to load, which @Kev-Ball now confirms for all 26,503 modules (19.0 → 12.2 GB, all valid). It is not driver compile time for the slowest modules: the driver's own optimizer apparently does the same work, and your 461k-instruction module was even 11% slower in one run. @PingasMech's 70 → 50 ms is for small modules. I've edited the description so it no longer implies a compile-time win on the large ones. If the +11% holds up with repeats, that would argue for keeping --no-spirv-opt visible in the docs, or for gating --opt by module size. Happy to add either if maintainers prefer. AI-assisted (Claude Code).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 690b971 on AMD Radeon 610M (RADV RAPHAEL_MENDOCINO, subgroup 32..64), Mesa 26.2.6, CachyOS Linux, LEONIDA_VK_DEVICE=RADV: pass.

  • Full scripts/run_gpu_tests.py after a full build, with LEONIDA_SPIRV_OPT=1: 202/217 passed, 15 skipped, no FAIL; the translator step reports "221 shaders: 221 translated, 221 pass spirv-val". With LEONIDA_SPIRV_OPT unset: 202/217 passed, 15 skipped, the same count.
  • tests/test_make_game*.py: 357 tests, OK (skipped=4, the Windows-only path cases), including test_spirv_opt_is_on_by_default. tests/test_docs_*.py: 30 tests, OK.
  • rdna2spirv.py --help lists "--opt run basic-block SPIR-V peephole optimization", the flag the stage forwards.

The -36.5% SPIR-V size and the compile-time claims were not measured here.

AI-assisted.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent CPU stack check at 690b9711 with #3045 dad0da40 and #3187 ccb572e3 on 3faac557, clean scratch tree adf33435. The normal translator CLI emitted sampled-only, storage-only, mixed and no-index controls with and without --opt through both assemblers. All 16 binaries passed Vulkan 1.2 and 1.3 validation (32 checks). Optimization preserved the actual SPIR-V capability sets and matching index.json feature and capability fields in every case. The no-index control declared neither dynamic indexing capability. For these four synthetic shaders the optimized total was 20,720 bytes vs. 35,172 bytes unoptimized per assembler. The 27 make_game translator-flag tests passed, including the default and opt-out. Windows, uv CPython 3.12.13, SPIRV-Tools from Vulkan SDK 1.4.363, CPU only, AI-assisted.

@memorypressure

Copy link
Copy Markdown

Merged cleanly with main. The three changed make_game suites pass under python -I (re-run), including the new test pinning --opt forwarded after the fallbacks and the recorded-flag retranslate behaviour. The docs flag reference is genuinely implemented — --opt at tools/rdna2spirv.py:15906 and tools/spirv_opt.py on main — so the cli-flags trap is clear, and --no-spirv-opt is added here. Note docs/testing.md also carries an edit from long-open #4212; this PRs section is disjoint, but someone should land them in some order. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 690b971 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_make_game_argparse.py / test_make_game_translator_flags.py / test_make_game_wave64.py -> all OK

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

mrp1no and others added 2 commits November 23, 2026 16:35
The GPU suite only ever ran on unoptimized modules, so tools/spirv_opt.py's output was never checked on a GPU.
LEONIDA_SPIRV_OPT=1 adds --opt to the translator command, the same way LEONIDA_FMA adds --fma.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tools/spirv_opt.py (ItsNotRexed#2298) was opt-in, so make_game's modules were the unoptimized ones. Over shard 0/40 of the
1.0.2.1 corpus --opt gives 503.6 -> 319.7 MiB (-36.5%), every one of the 663 modules smaller (median -36.3%, up to
-68.8%), all valid, for +3.5% translation time (ItsNotRexed#884). --opt joins the forwarded translator flags, after the
fallbacks, so it reaches every shard and the FSPX translation, and it is recorded in translator_flags.txt and the
shard plan: turning it on or off translates again.

The make_game tests about the fallbacks pass --no-spirv-opt so they keep checking the fallbacks alone, and
test_spirv_opt_is_on_by_default checks the default, the opt-out and the order with a fallback. docs/testing.md
says what --opt does and how to run the GPU suite on optimized modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kapitche

Copy link
Copy Markdown
Contributor

Tested ecedd93 merged with main 8575d36 on Linux (WSL2) / Python 3.12.16: pass
Real merge is clean (auto-merged docs/testing.md, make_game.py, scripts/run_gpu_tests.py); git diff --check clean. Earlier comments on this PR are on 690b971; none is on this head.
tests/test_make_game_argparse.py 36 OK (1 skipped), test_make_game_translator_flags.py 27 OK, test_make_game_wave64.py 38 OK (1 skipped), test_docs_table_rows.py 2 OK.
Negative control: make_game.py and scripts/run_gpu_tests.py reverted to main, argparse fails (1 error), translator_flags fails (1 failure, 9 errors), wave64 fails (12 errors); restored afterwards.
test_docs_links.py has 1 failure (docs/dev-tools.md -> ../runtime/README.md#a-600-frame-headless-guest-scene, anchor not found) that I also saw with the PR's docs reverted on another PR's merge, so it comes from main.
No GPU or corpus run on this head; the size numbers are not re-measured.

@ErayOzdayioglu

Copy link
Copy Markdown

Tested the current head ecedd93 on Windows, where the GPU suite hadn't been run on optimized modules yet (the earlier GPU run was RADV on Linux). Windows 11 26200, MSVC 14.51 (VS 2026), Vulkan SDK 1.4.363, llvm-mc 23.1.6. The test programs were built from this head. pass

Full scripts/run_gpu_tests.py, same build, with and without LEONIDA_SPIRV_OPT=1:

GPU without LEONIDA_SPIRV_OPT=1
RTX 5070 (617.42) 188/237 passed, 48 skipped, 1 FAIL 188/237 passed, 48 skipped, the same 1 FAIL
Radeon iGPU, Ryzen 5 7600 (RDNA2, 32.0.21042.62) 214/237 passed, 23 skipped 214/237 passed, 23 skipped

AI-assisted: yes

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.