Skip to content

spirv_asm: optional native assembler (tools/spirv_asm_native) with Python fallback - #8751

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-spirv-asm-native
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-spirv-asm-native

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: PERF-SPIRV-ASM-NATIVE (claim). Same pattern as #6308 / #8447.

What

tools/spirv_asm_native is a zero-dependency Rust port of assemble() in tools/spirv_asm.py. It uses the same tokenizer, ID numbering, operand grammar walk, literal rules and header as the Python code, so the .spv bytes are identical. The literal rules cover integers of every width and signedness, decimal and hex floats, OpSwitch case widths and bit-mask parameters in ascending bit order.

Benchmarks

Timings cover assemble() over the 221 synthetic audit shaders: hand-written RDNA listings, no game data, translated with rdna2spirv.py --assembler python, 14.6 MB of .spvasm. Taken on an AMD Ryzen 9 5900X, Windows 11, Python 3.12.10, rustc 1.100.0, with the min of 3 Python runs and 7 native runs. The machine was shared and noisy, so three separate runs are shown:

run Python assemble() (before) native via the hook (after) speedup
1 864 ms (16.9 MB/s) 52.8 ms (276 MB/s) 16.4x
2 929 ms (15.7 MB/s) 56.4 ms (259 MB/s) 16.5x
3 1030 ms (14.2 MB/s) 56.0 ms (261 MB/s) 18.4x

Byte-identical: 221/221, none declined. The Python output also matches the .spv files rdna2spirv.py wrote. The hook overhead (ASCII encode plus copy-out) is about 6 ms. The Rust core alone runs at about 280–290 MB/s.

Tests

  • cargo build --release and cargo test --release (12 tests): 0 warnings.
  • python -I tests/test_spirv_asm_native.py (new, 14 tests), comparing native against Python:
    • a hand-written module over every target env, covering CRLF, lone CR, tabs, comments, escaped strings, masks with parameters, ExtInst by name and by number, a 64-bit OpSwitch, OpSpecConstantOp, and Inf/NaN hex floats;
    • the 52 translated tests/semantic fixtures that need no compute header;
    • 3,000 random literal tokens and 1,500 random numeric literals of every int and float width;
    • mixed numbered and sparse ID spellings;
    • error inputs: declined, with the same SpirvAsmError message;
    • inputs the port declines but Python accepts: same bytes through assemble();
    • concurrent threads;
    • the fallbacks: env var off, missing library, declined call.
      The comparisons skip when the library is not built.
  • python -I tests/test_spirv_asm.py with native on and with LEONIDA_SPIRV_ASM_NATIVE=0: OK (1 skip; spirv-as and spirv-val are not installed here).
  • python -I tests/test_spirv_asm_tables.py: OK. python -m compileall -q make_game.py tools tests: OK.
  • python -I scripts/check_no_game_data.py on every changed file: clean.
  • tests/semantic was not run, as asked; the translator is unchanged.
  • tests/test_text_write_line_endings.py fails on main already (tools/leonida_tui.py:753, tools/wave_diff.py:373); nothing here touches those files.

CPU-only change; no GPU or driver involved. Tested on Windows 11 (AMD Ryzen 9 5900X).

AI-assisted: written with Claude Code (Claude Opus 5.5) and reviewed by me.

🤖 Generated with Claude Code

…thon fallback

A zero-dependency Rust port of assemble(): same tokenizer, ID numbering, operand grammar walk,
literal rules and header, so the .spv bytes are identical. The grammar tables stay in
spirv_asm_tables.py; spirv_asm.py serialises them and passes them in once.

The native path declines (and assemble() goes on in Python, with its usual result or
SpirvAsmError) on every error and on what it does not reproduce exactly, when the library is
not built, or with LEONIDA_SPIRV_ASM_NATIVE=0. The hook is a loader plus three lines at the top
of assemble().

On the 221 synthetic audit shaders (14.6 MB of text): 221/221 byte-identical, none declined,
assemble() 0.86-0.93 s in Python and 53-56 ms native (16.4x-16.5x).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant