Skip to content

spirv_asm: pure-Python Vulkan 1.2 SPIR-V binary assembler - #2251

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
SweatierKey:spirv-pure-python-asm
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
SweatierKey:spirv-pure-python-asm

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Adds a standard-library-only, pure-Python SPIR-V binary assembler (tools/spirv_asm.py + Khronos MIT grammar tables in tools/spirv_asm_tables.py) and wires --assembler {spirv-as,python} into tools/rdna2spirv.py (claim: #1902 (comment)).

What this adds

  • tools/spirv_asm_tables.py: compact tables (891 core opcodes, 42 ValueEnums, 16 BitEnums, 81 GLSL.std.450 extended instructions) generated directly from Khronos Group SPIRV-Headers (MIT License: spirv.core.grammar.json and extinst.glsl.std.450.grammar.json).
  • tools/spirv_asm.py: encodes SPIR-V assembly text directly to binary .spv words in memory for vulkan1.0..vulkan1.4 / spv1.0..spv1.6 (default vulkan1.2 -> SPIR-V 1.5), handling all operand categories (forward %id refs, ValueEnum/BitEnum ascending-bit extra params, int/float 16/32/64 OpConstant including -0.0 and hex floats, OpSwitch, OpPhi, OpExtInst GLSL.std.450, OpSpecConstantOp, escaped strings) with a fast path for pure-ID instructions.
  • tools/rdna2spirv.py: adds --assembler {spirv-as,python} (default stays spirv-as for full backwards compatibility). With --assembler python --no-asm, rdna2spirv.py encodes in memory without spawning spirv-as and skips writing the temporary .spvasm file to disk unless validation fails.

Verification

  • python3 -m unittest tests/test_spirv_asm.py -v: 5/5 OK.
  • Full sweep over all translatable runtime/test/shaders/*.asm (native + --wave64-emulate): 164 / 164 modules (1,590,154 SPIR-V words) passed spirv-val --target-env vulkan1.2 --allow-offset-texture-operand (331.6 ms in-process vs 354.8 ms spawning spirv-as on Linux, with zero temporary .spvasm disk I/O).
  • python3 -m unittest tests/test_tool_startup.py tests/test_text_write_line_endings.py: 4/4 OK.
  • Synthetic tests only; no game files.

AI-assisted.

@FreddyxS

Copy link
Copy Markdown
Contributor

Independent Windows review of 5a35326e merged cleanly onto main 49475796. Coordination claim.

Runtime fixtures: eight synthetic compute fixtures (cs_buf_swizzle, cs_oob_tail, cs_lds_b64_cmpst, cs_wqm_w64, cs_wm_bfm64, cs_wm_half_ops, cs_fmt_1010102, cs_barrier_loop), built natively and with --wave64-emulate. All 32 modules passed spirv-val for Vulkan 1.2. The 16 binary pairs differ only in the five-word header’s generator field. All 64 dispatches on the RX 9070 XT and Ryzen 7 7700X iGPU (AMD 26.10.1) matched the fixtures’ expected words and gave byte-identical full readbacks between assemblers. The native run used the recorded 32/64-wide subgroups and the emulated run used 32. The Python path also produced eight validated modules with --no-asm and no spirv-as on PATH. Nine focused CPU tests passed. Python 3.13.15, LLVM MC 23.1.6, SPIRV-Tools 2026.4 and MSVC 19.44.

Compatibility gaps in the standalone assembler:

  1. A minimal compute module with %f32 = OpTypeFloat 32 and %x = OpConstant %f32 0x1p+128 or 0x1.8p+128 assembles and validates with SPIRV-Tools. The words are 0x7f800000 and 0x7fc00000. Both forms raise an unhandled OverflowError in the Python path at struct.pack("<f", fv). These are documented spellings for infinity and a NaN payload. I would add a width-aware hex-float test and encode those bits directly. SPIRV-Tools syntax.
  2. In OpName %main "left\nright" (one backslash followed by n in the assembly text), SPIRV-Tools encodes leftnright, while _unquote_string emits an LF between left and right. Both binaries validate, so comparing the encoded string catches the difference that validation misses.

Synthetic inputs only. AI-assisted (Codex).

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Tested 5a35326e15b1f9696136184c20b27dd8d1d59b57 on Windows with Python 3.11.0 and SPIRV-Tools 2026.4. All five test_spirv_asm.py tests passed with reference tools on PATH. I also assembled the unmodified two-output fragment module from #2362 with both assemblers. Both binaries passed Vulkan 1.2 validation and produced identical, expected RGBA32F readbacks on RTX 5070 / 621.24, covering 256 pixels per output.

I found two integer-literal differences in _encode_context_number, in addition to the special-float issue already reported:

  • %i16 = OpTypeInt 16 1 then %v = OpConstant %i16 0xffff: spirv-as sign-extends the hex bit pattern to 0xffffffff; the Python path emits 0x0000ffff. spirv-val rejects the latter because the high bits must be sign extended. Decimal -1 works in both.
  • %u32 = OpTypeInt 32 0 then %v = OpConstant %u32 4294967296: spirv-as rejects the out-of-range value; the Python path silently emits zero, and the result validates. Range checking before word emission would catch this even when validation is enabled.

For the existing floating-literal finding, the same unhandled OverflowError occurs for %f16 0x1.004p+16 and %f64 0x1.0000000000001p+1024. The reference preserves the FP16 NaN payload as 0x7c01 and FP64 payload as words 0x00000001 0x7ff00000. These are synthetic standalone-assembler cases. I did not observe the translator emitting any of these literals in the GPU fixture. AI-assisted verification.

…ng escapes

Addresses standalone compatibility review findings on PR ItsNotRexed#2251 (@FreddyxS):
- Encode width-aware IEEE-754 hex-float literals (16/32/64-bit) directly into bit
  patterns, including SPIRV-Tools Inf and NaN payload spellings (0x1p+128 -> 0x7f800000,
  0x1.8p+128 -> 0x7fc00000, and f16/f64 equivalents) without OverflowError.
- Match SPIRV-Tools literal string decoding in _unquote_string: \<ch> yields <ch>
  verbatim (e.g. \n -> 'n', not LF).
- Add regression test comparing assembled words against spirv-as in tests/test_spirv_asm.py.
@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thank you @FreddyxS and @duudiis for the thorough RX 9070 XT / 7700X iGPU / RTX 5070 hardware verification and standalone review!

Both compatibility gaps reported by @FreddyxS are fixed in 45a7cbba:

  1. Width-aware hex-float Inf / NaN payloads: _parse_float_bits() now directly encodes 16/32/64-bit IEEE-754 hex-float literals, including 0x1p+128 (0x7f800000), -0x1p+128 (0xff800000), 0x1.8p+128 (0x7fc00000), and the f16/f64 equivalents without OverflowError.
  2. Literal string escapes: _unquote_string() now matches SPIRV-Tools verbatim (\<ch> → <ch>, e.g. \n → n, not LF).
  3. Added test_hex_float_inf_nan_and_string_escapes_match_spirv_as in tests/test_spirv_asm.py, asserting exact word-for-word parity with spirv-as (excluding the header generator word): 6/6 OK.

vitkoviy pushed a commit to vitkoviy/Leonida that referenced this pull request Nov 22, 2026
@FreddyxS

Copy link
Copy Markdown
Contributor

Independent Windows retest of 45a7cbb on main ec8ec4c: both review findings are resolved. Original review and coordination claim.

  • All 6 assembler tests passed with spirv-as and spirv-val on PATH. An independent synthetic module with 234 hex-float constants across f16/f32/f64 and seven escaped OpName strings matched spirv-as word for word other than the generator word. It covered both signs, zeros, subnormals, finite boundary values, infinities, and signed quiet/signaling NaNs with varied payloads. Both binaries passed Vulkan 1.2 validation.
  • Rebuilt the same eight synthetic compute fixtures with the round-40 translator in native and wave64-emulated configurations. All 32 modules passed spirv-val. All 16 assembler pairs matched outside the generator word. All 64 dispatches across the RX 9070 XT and Ryzen 7 7700X iGPU matched the fixtures’ expected words, with byte-identical full-buffer readbacks between assemblers.

Windows 11 build 26200, AMD driver 26.10.1, Python 3.13.15, LLVM MC 23.1.6 and SPIRV-Tools 2026.4. Synthetic inputs only. AI-assisted (Codex).

@ItsNotRexed
ItsNotRexed merged commit 0a9874a into ItsNotRexed:main Nov 22, 2026
2 of 4 checks passed
@zneel

zneel commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 45a7cbba on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: pass.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 167/180, 13 skipped, 0 failed (main 49475796: 170/185)
same with LEONIDA_W64_EMULATE=1 167/180, 13 skipped, 0 failed
pytest tests/test_spirv_asm.py tests/test_translate.py 504 passed, 2 skipped in 57.59s

On Windows, --assembler python matches spirv-as: I translated the suite's 185 listings with both (--no-asm, native and with --wave64-emulate). All 370 modules are byte-identical from the bound word onward. The one difference is header word 2, the generator magic: spirv-as writes 0x00070000 (Khronos SPIR-V Tools Assembler) and the Python assembler writes 0. Nothing depends on that word, but if byte-for-byte equality with spirv-as output is the goal, writing the same generator would give it.

The GPU rows use the default spirv-as path, so they show no regression for that path; since the modules are identical, they carry over to --assembler python too. The suite count is lower than main's because this branch predates the newer tests.

AI-assisted (Claude Code); I checked the results.

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