Skip to content

rdna2spirv: hoist constant-index %vgpr/%sgpr OpAccessChain to %entry (SHADER-789) - #4440

Open
SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:feat/hoist-reg-access-chains
Open

SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:feat/hoist-reg-access-chains

Conversation

@SweatierKey

Copy link
Copy Markdown
Contributor

Claim: #4257 (comment) (tracked as SHADER-789 in #4421).

What changes

Measured impact (synthetic shaders, spirv-as + spirv-val --target-env vulkan1.2)

Synthetic shader Before (03b09a08) After Delta
Divergent loop (2.5k RDNA2 insns, 40 s_cbranch_execz blocks) 35,738 lines / 631,184 B .spv / 7,846 reg OpAccessChain 27,943 lines / 475,284 B .spv / 51 reg OpAccessChain −21.8% lines, −24.7% .spv bytes, spirv-val 34→26 ms
Straight-line ALU (1k v_fma_f32) 16,264 lines / 296,540 B .spv / 4,040 reg OpAccessChain 12,272 lines / 216,700 B .spv / 48 reg OpAccessChain −24.5% lines, −26.9% .spv bytes, spirv-val 14→10 ms

Verification (Linux CPU, Python 3.13.5, SPIRV-Tools, no game files)

  • tests/semantic/test_reg_ptr_hoist.py: 5/5 OK (spirv-val --target-env vulkan1.2, %entry ordering after OpVariables, zero non-entry constant-index chains, 32-lane divergent run_wave with v_writelane_b32, v_movrels_b32 dynamic coexistence, --wave64-emulate CS validation, direct Gen() compatibility).
  • tests/test_semantic_translator_use.py: 3/3 OK.
  • Full tests/semantic suite (unittest discover -s tests/semantic, 7,456 tests): zero new failures (identical to untouched main 03b09a08: same 7 pre-existing failures + 2 errors in test_loop_ballot_barrier, test_cmp_class_f64, test_mutations).
  • All 74 non-pytest translator/SPIR-V tests in tests/test_*.py: 74/74 passed.
  • git diff --check clean.

AI-assisted.

@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 7c4a1679 on Windows 11: tests/semantic/test_reg_ptr_hoist.py -> 5 tests, OK; compileall and git diff --check clean.

Constant-index %vgpr/%sgpr OpAccessChains are hoisted to %entry, and the new tests cover the hoisted form plus the cases that must stay in place. No blocking issue.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 7c4a1679 merged onto main 03b09a08 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

  • scripts/run_gpu_tests.py: 192/209 passed, 15 skipped (plain main: 192/209 passed, 15 skipped).
  • Python tests this PR touches: 5 passed.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@mrbean56

Copy link
Copy Markdown

Tested 7c4a1679da on Linux (Debian 13), Python 3.13.5, SPIRV-Tools v2026.4 (tag ef96ed7): pass.

  • pytest tests/semantic/test_reg_ptr_hoist.py -q: 5 passed. Negative control: with main's tools/rdna2spirv.py four of the five fail, so they pin the hoisting.
  • Independent run: all 40 translator fixtures that produce a module here translate with hoisted %reg_* chains and pass spirv-val --target-env vulkan1.2 (the other 41 need inputs the harness does not supply).
  • Layout checked on a translated PS: the %reg_<v|s>N = OpAccessChain lines land at the end of %entry, after every OpVariable and before the first branch; the fn_vars insertion for %pc/%done still uses last_var + 1 from the variable lines, so it stays above them. Dedup is per (kind, r) with a constant index, so it is pure CSE of side-effect-free OpAccessChains.

No GPU, no game data. AI-assisted.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified 7c4a1679 on Windows 11 25H2 + AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, SPIRV-Tools v2026.4, llvm-mc 21.1.8).

GPU verification (LEONIDA_VK_VALIDATION=1)

Executed scripts/run_gpu_tests.py --build <RelWithDebInfo> --require-pass using wt-pr-4440's tools/rdna2spirv.py (0 validation errors across all suites):

  • cs_w32: 13 shaders translated & validated, 13/13 passed
  • cs_w64 (native 64-wide): 21 shaders translated & validated, 21/21 passed
  • cs_w64 (LEONIDA_W64_EMULATE=1): 21 shaders translated & validated, 21/21 passed
  • ps_: 96 shaders translated & validated, 85/93 passed, 8 skipped (ps_m15, ps_m2*, ps_m3*, and expected ps_w64x_readlane skip on subgroup 32..64)
  • vs_: 12 shaders translated & validated, 4/7 passed, 3 skipped (vs_m15/m2/m3)
  • ms_: 7 shaders translated & validated, 6/6 passed

Semantic, unit & docs checks (LEONIDA_REQUIRE_SPIRV_TOOLS=1)

  • python -I tests/semantic/test_reg_ptr_hoist.py -v: 5/5 OK (0.197 s / 0.234 s), and via unittest discover: 5/5 OK (0.193 s).
  • python -I tests/test_semantic_translator_use.py -v: 3/3 OK (5.921 s).
  • python -I tests/test_shader_checks.py: 75/75 OK (1.360 s).
  • All 7 tests/test_docs_*.py suites: 7/7 passed (py_compile exit 0).
  • Checked %reg_v*/%reg_s* placement in assemble(): emitted in %entry strictly after OpVariables and prologue stores, with no naming collisions elsewhere in tools/rdna2spirv.py.

AI-assisted (Gemini, Antigravity).

@akimfomin

akimfomin commented Nov 23, 2026 •

Copy link
Copy Markdown

NVIDIA Ada before/after on Windows 11 28000.3501, RTX 4070 Ti SUPER / 617.14. Exact 03b09a08 and 7c4a1679 with the same portable Vulkan harness from the unchanged runtime and llvm-mc 23.1.6.

  • cs_w64 with wave64 emulated on the 32-wide GPU: 21/21 on both revisions, including divergent branches, LDS and writelane.
  • cs_w32 and cs_movrelsd_2: 13/13 and 1/1 on both. All reported compute words match the base, including the dynamic operand fixture.
  • ps_legacy_mul and the two ps_f16_pack draws: 12,288/12,288 exact RGBA pixels; raw readback hashes match between revisions.

All 38 selected modules passed spirv-val on both versions. Their combined binary size was 1,019,992 → 958,992 bytes (6.0% less on this selected set, excluding companion shaders). The five new CPU tests also passed. Synthetic inputs, no runtime validation layer in the GPU runs. AI-assisted (Codex).

Follow-up with #4345 applied as well. Both merge orders produced the same tree on 03b09a08 (29a62d31). I measured four variants with the same synthetic fragment shaders on the same GPU, using different inputs and five measured groups per size.

At 1,024 repeated legacy multiplies Main #4345 #4440 Both
Median SPIR-V bytes 1,111,156 758,556 1,008,576 655,976
Median pipeline creation 659.96 ms 334.83 ms 654.03 ms 328.23 ms

The size savings compose. The extra size reduction from #4440 did not yield a consistent timing improvement over #4345 alone across all three sizes. All 19 four-way groups produced bit-identical RGBA32F readbacks at all 4,096 pixels. The combined tree passed the 11 focused semantic tests and all 80 modules validated. Synthetic benchmark only.

@SphyserX

Copy link
Copy Markdown

Tested 7c4a167 on Arch Linux (in a VM), CPython 3.14, LLVM 23.1.5, SPIRV-Tools v2026.4, Mesa llvmpipe: pass, no regression against main 03b09a0.

@chpeu

chpeu commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7c4a1679 (main is an ancestor, no rebase needed) on Linux, no game files. Both measurements below were
taken in the same session, back to back, so they are directly comparable.

GPU suite (RTX 3080, driver 615.71.09, Vulkan 1.4.351) — identical to main:

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → 195/210 passed, 15 skipped, 0 failed (same on main).
  • without the variable → 164/210 passed, 46 skipped, 0 failed (same on main).

Semantic side, which is what your change touches: python -m unittest discover -s tests/semantic

  • your branch: 7461 tests, 8 failures, 2 errors in 280 s;
  • main, same command, same machine, measured minutes earlier: 7456 tests, 8 failures, 2 errors in 288 s.

Five extra tests (yours) and the same failing set with the same names — test_loop_ballot_barrier (5),
test_cmp_class_f64.test_rejected_forms (2), the two test_mutations cases on the v_mul_legacy_f32 anchor
(already looked at in #3883), test_anchors_match_crlf_translator_copies, and test_fma_khr where this box's
Ubuntu spirv-as (SPIRV-Tools 2025.1~rc1) reports Invalid capability 'FMAKHR'. Running your file alone:
python -m unittest discover -s tests/semantic -p test_reg_ptr_hoist.py → 5 tests OK.

Translator pytest side: the 28 pytest-marked files as CI runs them: 1592 passed, 1 failed on your branch
and the same on main (the single failure is test_u16_high_half::test_d16_buffer_store_no_redundant_extract_after_shr16,
a spirv-val "previous definition" problem coming from aa2988cc, fixed by #3902). python -m compileall -q make_game.py tools tests: clean.

The RTX 5080 was not run in this pass: 186 MiB free on that card (another job holds it here). It matched the 3080
exactly (195/210, 0 failed) in the pass two hours ago, so I expect nothing different, but I am not claiming a
second card for this head.

Machine: Linux Mint 22 (Ubuntu 24.04 base), llvm-mc 20.1.2, SPIRV-Tools 2025.1~rc1, GCC 13.3, Python 3.12.3 with
pytest 9.2.0 and numpy 2.5.5, built with cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON.
AI-assisted testing, counts read through by hand.

@lunarminus

Copy link
Copy Markdown

Tested 7c4a1679 (base 03b09a08) with the full game corpus of my own 1.0.2.1 copy, plus the GPU suite on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass.

Corpus (CPU, counts only): rdna2spirv.py <work>/shaders/dxbc/disasm --shard i/8 ×8, then --merge, native mode as make_game.py picks it on this GPU. I ran it at 03b09a08 and on this PR:

translated spirv-val-valid
03b09a08 26,503 26,422
this PR 26,503 26,422
  • The 81 invalid modules are the same set with the same error on both sides ("requires a previous definition", the aa2988cc regression Translator: constants assemble asks for late are defined too #3902 fixes). No module became invalid.
  • 26,417 of the 26,422 valid modules change, as expected for a change to every register access.
  • The reflection data in index.json is identical for all 26,422 entries. That covers bindings, descriptor access, push-constant bytes, capabilities, extensions, features, inputs/outputs, interpolants, subgroup size and the wave64 fields. So the hoist changes nothing the runtime reads.

GPU (scripts/run_gpu_tests.py, test programs built from this branch): 196/210 passed, 14 skipped, 0 failed, both native and with LEONIDA_W64_EMULATE=1. Each is identical per test to 03b09a08 on the same machine.

Code read (independent, CPU, synthetic programs only): no problems found.

  • Domination: every %reg_v<r>/%reg_s<r> is defined in %main's %entry after the OpVariables and prologue stores, and dominates every use. That includes spirv_gs_to_compute's split, where the pointers land in %gsc_body.
  • Helper functions: none of the helpers built by swapping self.body reaches load_reg/store_reg/reg_ptr, so no other function uses a %main pointer.
  • Emulation spills: wave64 emulation's x_spill never sees the pointers.
  • Dynamic indices: movrel indices stay inline.
  • Fallbacks: runtime/test/shaders/build.py gives 234/234 valid on both trees, by default and with each fallback (W64_EMULATE, BARY_FALLBACK, PC_TAIL_UBO, F16_INTERP_FALLBACK). All 8 --mesh-compute/--wave64-emulate/--pc-tail-ubo combinations of the 6 mesh shaders are valid too. The fixtures' total .spv is 22.3 MB against 23.8 MB.

AI-assisted (Claude Code).

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.

9 participants