Skip to content

fix(translator): fix reaching_write at insn 0 and across loop back-edges - #3287

Open
evilclanker9000 wants to merge 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:ag/interp-reaching-write
Open

evilclanker9000 wants to merge 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:ag/interp-reaching-write

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Claimed in coordination issue #3232 as EGEN-INTERP-REACHING-WRITE (finding: #3232 (comment)).

What and why

interp_ij_analysis::reaching_write in tools/rdna2spirv.py had two control-flow bugs when checking whether a VGPR write reaches a v_interp_p1 / v_interp_p2 site from predecessor blocks or loops:

  1. Instruction index 0 dropped (any(w for w in ws if blk[w] == p)): testing the truthiness of the matching instruction index w returned False whenever the only write in predecessor block p was instruction 0 (bool(0) == False).
    • A write to v0/v1 at instruction 0 followed by a branch into the interpolation block was silently misclassified as pristine (emitting a plain OpLoad with hardware barycentrics instead of PerVertexKHR with the computed value).
    • A write to another VGPR at instruction 0 followed by a branch was falsely refused as Unsupported instead of being treated as a computed I/J pair.
  2. Loop back-edge writes missed (seen, work = {b}, [b]): seeding seen = {b} prevented revisiting block b via predecessors across a back-edge (b -> ... -> b), so a write at or after the interpolation site in b was not seen as reaching subsequent iterations.

Fix: Check any(blk[w] == p for w in ws) for predecessor blocks p reached from seen = set(), work = [b] (without re-queuing p == b, since b was already seeded).

Tested

  • Targeted regression suite (tests/semantic/test_interp_computed_ij.py):
    python -m unittest discover -s tests/semantic -p test_interp_computed_ij.py -v
    
    • Before fix (against upstream/main 16282101):
      • test_write_at_insn_zero_before_branch_is_computed: FAIL (AssertionError: 'PerVertexKHR' not found)
      • test_non_v0_write_at_insn_zero_before_branch_is_computed: ERROR (rdna2spirv.Unsupported: v_interp_p1_f32 I source v5: only v0 (or a plain v_mov copy of it) is modelled)
      • test_loop_backedge_write_after_interp_in_same_block_is_computed: FAIL (AssertionError: 'PerVertexKHR' not found)
      • test_straight_line_self_dest_write_stays_pristine and all 15 existing tests: PASS (Ran 19 tests in 0.217s, FAILED (failures=2, errors=1)).
    • After fix (7b4c021d):
      • All 19/19 tests PASS (Ran 19 tests in 0.195s, OK), emitting PerVertexKHR + OpFSub %float interpolation for all 3 branched/looped cases while keeping straight-line self-destination writes pristine.
  • SPIR-V validation (spirv-as + spirv-val --target-env vulkan1.2 from SPIRV-Tools):
    • ps_ij_insn0_v0: OK (PerVertexKHR=True)
    • ps_ij_insn0_v5: OK (PerVertexKHR=True)
    • ps_ij_loop_backedge: OK (PerVertexKHR=True)
  • Related suites and lint checks on 7b4c021d:
    • python -m unittest discover -s tests/semantic -p "test_*interp*.py" -v: 181/181 OK (1.517s)
    • python -m unittest discover -s tests/semantic -p "test_ps_semantics.py" -v: 7/7 OK
    • python -m unittest discover -s tests/semantic -p "test_value_semantics.py" -v: 33/33 OK
    • python -m unittest discover -s tests/semantic -p "test_fixtures.py" -v: 1/1 OK
    • python -I tests/test_ps_interp_modes.py: 26/26 OK
    • python -I tests/test_text_write_line_endings.py: 2/2 OK
    • python -m compileall -q make_game.py tools tests and git diff --check: clean (exit 0)
  • Hardware / game data: No GPU required (CPU translator analysis + Vulkan 1.2 spirv-val; synthetic PS inputs only, no game data).

Notes

  • When a CFG cycle returns to block b, any write to the I/J register in b reaches the start of b on the next iteration (even a write at the interpolation site itself, if the block loops back to itself and overwrites its I/J VGPR with the partial sum); a straight-line self-destination write without a back-edge stays pristine, pinned by test_straight_line_self_dest_write_stays_pristine.
  • docs/design.md is unchanged because this fixes the control-flow bug in the existing SHADER-177 computed-I/J analysis rather than adding a new instruction rule.

(Antigravity)

AI-assisted.

@evilclanker9000
evilclanker9000 force-pushed the ag/interp-reaching-write branch from 7b4c021 to d96f330 Compare November 22, 2026 20:15
@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (f847748) to resolve merge conflict in tests/semantic/test_interp_computed_ij.py (now d96f330). Kept all 3 new upstream tests alongside the 4 tests added here, and strengthened both forward-branched insn-0 tests (test_write_at_insn_zero_before_branch_is_computed and test_non_v0_write_at_insn_zero_before_branch_is_computed) to also execute via support.mrt0_words(...) and check the interpolated pixel output. Verified 22/22 in tests/semantic/test_interp_computed_ij.py, 235/235 in test_interp.py, and spirv-val --target-env vulkan1.2 on ps_ij_insn0_v0, ps_ij_insn0_v5, and ps_ij_loop_backedge. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

In case it's useful for this PR: while #3309 (now closed in favour of this one) was open, we wrote two GPU fixtures for the reaching_write cases, ps_ij_reach_zero.asm (a write at instruction index 0) and ps_ij_reach_loop.asm (a loop back-edge write after the site). On an RTX 4080 SUPER both pass all 4096 pixels with the fix and fail without it (zero gives u, loop gives 2u). They're on branch kap-interp-reaching of kapitche/Leonida; see #3309 (comment). Feel free to take them.

@ttd0x

ttd0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested d96f3301 on NVIDIA GeForce RTX 2080 Ti (Turing, subgroup 32..32, driver 610.88), Windows 11: pass, no regression.

  • tests/semantic/test_interp_computed_ij.py: 22 tests OK.
  • scripts/run_gpu_tests.py (default, no emulation), 204 shaders translated, all pass spirv-val: 152/199 passed, 46 skipped (native wave64, expected on a 32-wide device), 1 failed. Main f847748c gives the same result lines; the one failure (ps_sdwa_f16_const) also fails on main.

Synthetic fixtures only, no game data. AI-assisted (Claude Code).

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested d96f330 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 185/199 passed, 14 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base f847748 on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 7326 run, OK (expected failures=1)

No game files. AI-assisted.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested d96f3301 (base f847748c) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host): pass.

  • tests/semantic/test_interp_computed_ij.py (Python 3.12): 22 tests OK on this head. With tools/ from f847748c it gives 2 failures and 1 error: test_write_at_insn_zero_before_branch_is_computed, test_loop_backedge_write_after_interp_in_same_block_is_computed and test_non_v0_write_at_insn_zero_before_branch_is_computed. In the two failures the old translator emits a plain in_attr0 load with no PerVertexKHR. In the error it refuses the shader (only v… is modelled in interp_pair_src). So the new tests catch both bugs.
  • scripts/run_gpu_tests.py, whole suite: 152/199 passed, 46 skipped, 1 failed, identical per test to main f847748c. The failure is ps_sdwa_f16_const, the NVIDIA f16 denormal flush on main (rdna2spirv: widen f16 denormal operands without UnpackHalf2x16 (NVIDIA reads them as 0) #3220 / rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) #3040), not this PR.
  • ps_ij_computed, ps_interp_f32_mods, ps_interp_mov_mods and ps_interp_f16_high pass, so the existing computed-I/J and interpolation paths still work on the GPU.
  • ctest on the Linux build (CMake + Ninja, GCC): 41/41 passed.
  • Not covered: no GPU fixture has an I/J write at instruction 0 or across a loop back-edge, so the two fixed cases ran on the CPU only.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested d96f330 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • python3 -m pytest tests/semantic/test_interp_computed_ij.py -q -> 22 passed
  • python3 -I scripts/run_gpu_tests.py --build <build> -> 184/199 passed, 14 skipped, 0 failed (expected D3D12-owned skips)

No runtime C++ changes; GPU suite run from the PR worktree against the existing main runtime build.

AI-assisted (OpenCode).

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (head merged with main f847748, MSVC 14.51, Python 3.13.1):

  • tests/semantic/test_interp_computed_ij.py passes.
  • Negative control: with tools/rdna2spirv.py reverted to main, the three new tests fail (write at instruction 0 before a branch, loop back-edge write; PerVertexKHR not found in the module).
  • GPU: RTX 4080 SUPER ps_ij_computed PASS; Intel UHD 770 (VK_LOADER_DRIVERS_DISABLE=*nv*) skips it as on main (needs PerVertexKHR inputs). The two GPU fixtures for these cases from rdna2spirv: interp_ij_analysis reaching_write misses a write at instruction 0 and loop back-edge writes #3309 (kapitche's earlier comment above) are the ones that fail without the fix on the RTX.
  • Whole run_gpu_tests.py suite with the PR's translator against main: RTX 4080 SUPER 153/199 passed, 46 skipped, identical to main. Intel UHD 770: 135/199 in one run, one program different from main, cs_bpermute32 FAIL (subgroup 8..32 on that device). It did not reproduce: cs_bpermute32 passed 8 of 8 times alone on this PR's tree and 8 of 8 on main, and it is a compute shader unrelated to interpolation, so I read it as a one-off Intel flake, not this PR. The second full run was not repeated.

The vk_* programs came from a runtime built from main.

@dandasma

Copy link
Copy Markdown
Contributor

Tested d96f330 on AMD Radeon RX 9070 (RDNA4, subgroup 32..64), AMD 26.10.1 (LLPC), Vulkan 1.4.359, Windows 11 26300, with @kapitche's two GPU fixtures from kap-interp-reaching (ec7f9c5) applied on main f847748: pass.

  • With the fix (main f847748 + d96f330 + the fixtures): scripts/run_gpu_tests.py --filter ps_ij_reach gives 2/2 passed in three runs (ps_ij_reach_zero and ps_ij_reach_loop: every pixel within 1e-6 in all four channels). ps_ij_computed passes 1/1.
  • Without the fix (main f847748 + the fixtures, main's tools/rdna2spirv.py): 0/2 in three runs, with the same lines each time. Both shaders translate and pass spirv-val, then fail with largest error 0.492. ps_ij_reach_zero gives pixel (0,0).x = 0.0078125 = u = (0 + 0.5)/64 instead of 0.5. ps_ij_reach_loop gives 0.015625 = 2u instead of u + 0.5 = 0.5078125.
  • The fixtures were applied with git cherry-pick -n ec7f9c5e. The two .asm files are unchanged and the build.py line applied cleanly. The conflicts were trivial: in runtime/test/shaders/check.py the two names were added to main's PIXEL_TESTS list, in scripts/run_gpu_tests.py the line from check import IJ_REACH_TESTS, ij_reach_problem was added after main's import, and docs/design.md was kept as main's (a doc row only).
  • Harness: the runtime/ build of main f847748 (the PR and the fixtures change no C++), LEONIDA_VK_DEVICE=9070, Python 3.12.10.

This matches @kapitche's RTX 4080 SUPER result on #3309, which was run against #3309's own fix (d7c0d4b). Here the fixtures run against this PR's head, on AMD.

AI-assisted (Claude Code).

@bielfreedom

Copy link
Copy Markdown

Tested d96f3301 (on its base f847748c) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass, no regression.

  • python -m unittest discover -s tests/semantic (includes this PR's test_interp_computed_ij.py): 7326 tests OK (1 expected failure).
  • scripts/run_gpu_tests.py: 204 shaders, 204 translated, 204 pass spirv-val; 185/199 passed, 14 skipped, 0 failures. The skips are the shaders run by the d3d12 tests and ps_w64x_readlane.
  • Full ctest: 129/129, 0 validation errors.

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

@hah346

hah346 commented Nov 23, 2026 •

Copy link
Copy Markdown

I checked a hand-written pixel shader that writes v0 after interpolation, branches back through a separate block, then exits after the second interpolation. With the existing ATTR0 and BARY inputs, the CPU interpreter produced these results.

  • f847748c emitted no PerVertexKHR decoration for the input and returned RGBA (0.5, 0.5, 0.5, 0.5).
  • d96f3301 emitted PerVertexKHR and returned the expected RGBA (0.0, 0.0, 0.0, 0.0).

The loop uses these instructions with the same four-byte fixture encoding as the PR tests.

s_mov_b32 s0, 0
s_branch 0
v_interp_p1_f32_e32 v2, v0, attr0.x
v_interp_p2_f32_e32 v2, v1, attr0.x
v_mov_b32_e32 v0, 0
s_cmp_eq_u32 s0, 0
s_cbranch_scc0 2
s_mov_b32 s0, 1
s_branch -7
exp mrt0 v2, v2, v2, v2 done vm
s_endpgm

The interpolation suite also passed 235/235 tests on d96f3301 (Windows 11 build 26300, Python 3.14.6).

GPU follow-up: I ran the same loop through vk_harness on an NVIDIA GeForce RTX 2060 (Windows 11 build 26300, driver 32.0.16.1664 / 616.64, subgroup 32). The vertex module and both fragment modules passed spirv-val --target-env vulkan1.2 with SPIRV-Tools 2026.4. f847748c returned the horizontal gradient in all 4,096 pixels, from 0.0078125 to 0.9921875. d96f3301 returned (0.0, 0.0, 0.0, 0.0) in all 4,096 pixels.

Synthetic inputs. AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Review of head d96f3301 (parent f847748c), CPU only: pass, with one status note.

Status note. main (0e337dc8) has this fix already: commit d7c0d4b1 ("reaching_write counts a write at instruction 0 and loop back-edge writes") is an ancestor of main. A merge of this head with main conflicts in tools/rdna2spirv.py (the same lines of reaching_write) and in tests/semantic/test_interp_computed_ij.py. The two versions of the function do the same thing: seen starts empty, the test is membership and not truthiness. This pull request needs a rebase, and after it only the tests are new.

What I ran. The earlier comments cover the write at instruction 0, the back-edge write in the same block and in a separate block, and the GPU. I did not repeat them. I wrote 8 more pixel listings by hand, translated each with the tools of the parent, of this head and of main, and ran each on the CPU interpreter (support.mrt0_words, attribute (0,0)/(2,0)/(0,2), weights (0.25, 0.25, 0.5)):

Listing Expected Parent Head main
A redefinition of v0 in a latch block, read by the site in the second iteration computed, 0.0 ok ok ok
The same with the site at instruction 0 computed, 0.0 ok ok ok
Nested loops, the outer latch writes v1 computed, 0.0 ok ok ok
Nested loops, the inner latch writes v0 computed, 0.0 ok ok ok
Two redefinitions in the loop (0, then 0.5) computed, 1.0 ok ok ok
A write after the loop exit only pristine, 0.5, no PerVertexKHR ok ok ok
A write in a later branch arm, no path back pristine, 0.5, no PerVertexKHR ok ok ok
A write in a block that skips the site pristine, 0.5, no PerVertexKHR ok ok ok
  • The head and the parent give the same SPIR-V text for all 8 (equal SHA-256). Thus the change does not move a case that was correct, and the three not-reaching cases stay pristine.
  • tests/semantic/test_interp_computed_ij.py of this head: 22 pass with the tools of this head, 22 pass with the tools of main, 2 failures and 1 error with the tools of the parent (the three new tests, as the description says).

One example, the nested loop where the outer latch writes J (each instruction 4 bytes in the fake encoding of the tests):

s_mov_b32 s0, 0
s_mov_b32 s1, 0
v_interp_p1_f32_e32 v2, v0, attr0.y
v_interp_p2_f32_e32 v2, v1, attr0.y
s_cmp_eq_u32 s1, 0
s_mov_b32 s1, 1
s_cbranch_scc1 -5
s_cmp_eq_u32 s0, 0
s_cbranch_scc0 3
s_mov_b32 s0, 1
v_mov_b32_e32 v1, 0
s_branch -11
exp mrt0 v2, v2, v2, v2 done vm
s_endpgm

Windows 11, Python 3.14.7. Hand-written listings only, no game files. 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.

10 participants