Skip to content

OLD-10044: rdna2spirv: intra-block %vgpr/%sgpr value forwarding - #1652

Open
Tortoche wants to merge 6 commits into
ProjectLeonida:mainfrom
Tortoche:old-10044
Open

Tortoche wants to merge 6 commits into
ProjectLeonida:mainfrom
Tortoche:old-10044

Conversation

@Tortoche

Copy link
Copy Markdown
Contributor

Claim: #1568 (OLD-10044 / Draft #309)

Summary:

  • Implements intra-block %vgpr/%sgpr value forwarding in tools/rdna2spirv.py.
  • Updates semantic and unit test assertions to trace forwarded %vgpr writes and active-lane guarded OpSelect (tests/semantic/test_*, tests/test_*).

Verification (Linux):

  • python3 -I scripts/check_no_game_data.py on all modified files -> OK
  • python3 -m compileall -q make_game.py tools tests -> OK
  • PYTHONPATH=tests/semantic python3 -m pytest tests/test_cube_sample.py -> 18 passed in 1.28s
  • python3 -m unittest on the 10 remaining modified test files (test_buffer_oob_select, test_buffer_swizzle, test_image_sample_bias_nonps, test_reg_load_forwarding, test_sample_c, test_sample_cl, test_f64_transcendentals, test_image_load_typed, test_rcp_iflag_mods, test_translate) -> 112 passed, 1 skipped, 0 failed

AI-assisted: yes

@GeorgiyDemo

Copy link
Copy Markdown
Contributor

Tested 1e855350b on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main 3f81d46ac (translator-only PR; binaries from the build at 66d4f5aa3):

  • tests/semantic/test_reg_load_forwarding.py: OK at this head; copied onto main it fails 1 (the forwarded-read expectation). That is the only touched semantic file that distinguishes — test_buffer_swizzle, test_sample_c, test_sample_cl, test_buffer_oob_select pass on both.
  • Other touched suites at this head: test_image_sample_bias_nonps OK (1 skip); pytest on test_cube_sample, test_image_load_typed, test_rcp_iflag_mods, test_f64_transcendentals: 53 passed, 113 subtests passed.
  • GPU smoke, --filter ps_sample_ on MoltenVK 1.4.2: 6/6 PASS.
  • Not run: the full semantic/corpus suites; the touched files above are the evidence.

AI-assisted; synthetic fixtures only, no game data.

@adrianmjim

Copy link
Copy Markdown

Tested 1e855350b applied onto main 42537e7b6, on Windows 11 build 26300 with an AMD Radeon RX 7900 XT (RDNA3), AMD driver 32.0.32029.2010 (26.11.2), Vulkan SDK 1.4.363.0, llvm-mc 23.1.5. Test programs come from a main 42537e7b6 build. No game files.

main 42537e7b6 this PR
scripts/run_semantic_tests.py tests/semantic, with LEONIDA_REQUIRE_SPIRV_TOOLS=1 487 files, 6,279 tests, 0 files failed 488 files, 6,286 tests, 0 files failed
run_gpu_tests.py, whole suite 244/263 passed, 18 skipped, 1 failed 244/263, the same status for every fixture
LEONIDA_W64_EMULATE=1 --filter cs_ 125/130 passed, 4 skipped, 1 failed 125/130, the same status for every fixture

The full semantic run includes the untouched files under tests/semantic that trace %vgpr/%sgpr loads structurally (about 14), and they all still pass with forwarding. I did not run the similar untouched files under tests/ (for example test_f64_conversion, test_f64_output_modifiers, test_image_query_translate, test_spirv_opt). I didn't run the five changed files under tests/ either:

  • test_translate.py and test_cube_sample.py need pytest, which this machine does not have;
  • I skipped the unittest-based test_image_load_typed.py, test_rcp_iflag_mods.py and test_f64_transcendentals.py.

The one GPU failure is cs_imgatomic_ext on both trees, which #1518 fixes.

I read the forwarding through looking for a stale value and found none:

  • Every write still emits its OpStore, so direct loads, permlane's "old" operand, s_movrels and v_movrels see memory.
  • v_movreld/v_movrelsd/v_swaprel invalidate the VGPR cache.
  • dst() caches the masked result (the OpSelect, or the bitwise blend on the eager path), not the raw value. It caches the raw value only where no masking is needed: lane-mask writes, an EXEC that never changes, or inside an active run, whose merge label clears the cache.
  • The cache is cleared at every label, which covers active runs, folds, x_split rounds and the dispatch loop.

Two notes on the tests:

  • Weaker checks. The loosened tests (test_sample_c, test_sample_cl, test_image_sample_bias_nonps, test_image_load_typed, test_rcp_iflag_mods, test_translate) now accept an id that was OpStored into vk anywhere in the module, not the value vk holds at the use. Depending on the test, the trace takes the last store, the first, or any store (test_a16_sample_gradients). If one SSA id is stored into two VGPRs, which v_mov_b32 v5, v4 now does, the reported register depends on that choice rather than on what was read, so a swapped-operand bug could slip through. Tracking the last store to vk before the use in the same block would close most of that. test_cube_sample also now evaluates OpSelect %active val old as val, so it no longer checks the masked-lane path.
  • Weak w64 test. test_wave64_emulate_validates_with_reg_forwarding only validates the SPIR-V. Without spirv-as/spirv-val on PATH (and without LEONIDA_REQUIRE_SPIRV_TOOLS), it only checks that translation does not raise. The --wave64-emulate path has no semantic run in this PR. The GPU emulated run above covers it for the existing cs_ fixtures.

@ecox982745

Copy link
Copy Markdown

I ran the four additional CPU suites adrianmjim called out. On 1e855350b, python -I tests/test_f64_output_modifiers.py fails for both div:2 and clamp div:2. Both pass on c7e82e26e, the parent of the forwarding change. The other three suites pass on both revisions (1 f64 conversion test, 10 image query tests, 12 SPIR-V optimizer tests).

The assertion loses track of which 0.5 the multiply uses. With forwarding, both the second addend and the scale appear as a double constructed from the literal words for 0.5. On the PR head the add uses %t4 and the multiply uses the separately constructed %t8. The helper at tests/test_f64_output_modifiers.py:64 takes the first match and requires OpFMul %double %t5 %t4. The emitted instruction is OpFMul %double %t5 %t8, with the correct bits.

I tested a local repair that starts at the multiply consuming the add, follows its actual scale operand through OpBitcast and OpCompositeConstruct, and checks the low and high constant words. All seven modifier cases pass on both revisions. Changing the emitted multiply to use the 0.75 addend makes the repaired assertion fail on both.

RTX 4090, Windows, NVIDIA 616.64: I drew the stock ps_f64_mods fixture and a second hand-written shader that exports both 32-bit words of each of the two failing cases. At both revisions, all 4,096 pixels in each draw matched every expected word. Both operations produced exactly 0.625 (0x3fe4000000000000). All four modules passed Vulkan 1.2 SPIR-V validation. AI-assisted verification.

@whoalin1

Copy link
Copy Markdown
Contributor

Independent verification (native Windows; RTX 4070 SUPER + AMD gfx1036 iGPU): authorship preserved (SweatierKey author). compileall clean; all 6 touched semantic test files pass; FULL semantic suite 488 files / 6,286 tests / 0 failed. GPU filters (sample/buf/oob/movrelsd/f64/bias/img): identical verdicts on both drivers — all pass except cs_imgatomic_ext, proven pre-existing by re-running with merge-base translator (identical failure). Code review: _reg_val cache cleared at every label/block boundary and invalidated on dynamic-index writes — sound. (Env note: 2 pytest cs_dpp_cmp.asm failures are WSL llvm-21 missing gfx1013 DPP encoding — environmental.) No design.md change needed — pure codegen optimisation. AI-assisted verification.

@MoonEyes MoonEyes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Corpus numbers for this PR, which AGENTS.md asks for ("translated and spirv-val-valid counts before and after"). Windows 11, Python 3.14, Vulkan SDK 1.4.363 spirv-val, a full 1.0.2.1 work folder (shaders/dxbc/disasm), 12 shards then --merge. This PR merged locally onto main 3f81d46 (no conflict), compared with a9f8dc8 (3f81d46 plus one test-only commit; translator code identical):

main a9f8dc8 3f81d46 + this PR
translated 26,503 / 26,503 26,503 / 26,503
pass spirv-val 26,503 / 26,503 26,503 / 26,503
.spv files that change - 26,497 of 26,546
of those, smaller / larger - 26,497 / 0
total .spv bytes 12,891,133,980 12,112,842,476 (-6.04 %)

So no shader is lost or made invalid, and every changed module gets smaller.

Translator suite on the same merge: python -I scripts/run_semantic_tests.py tests/semantic gives 488 files, 6,286 tests, 1 file failed (test_lds_src2). It fails the same way without this PR (run on a9f8dc8: 1 files, 5 tests, 1 files failed: test_lds_src2): it hands llvm-mc a Windows temp path, which my LLVM_MC=wsl ... llvm-mc cannot open. This PR does not touch that file; #1733 fixes it.

What this does not show: spirv-val checks that the modules are valid, not that they compute the same values. I did not run the GPU tests on this branch.

AI-assisted: yes (Claude Code ran the translations and the suite; I read the results).

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants