Skip to content

rdna2spirv: v_writelane_b32 reads its destination (SHADER-768) - #4297

Open
LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:writelane-rbw
Open

LouisRiverstone wants to merge 2 commits into
ItsNotRexed:mainfrom
LouisRiverstone:writelane-rbw

Conversation

@LouisRiverstone

@LouisRiverstone LouisRiverstone commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

SHADER-768, from the "Up for grabs" table in #4257: read_before_write_vgprs counted v_writelane_b32 as a full write of its destination.

v_writelane_b32 vD, sSRC, sLANE writes one lane of vD and keeps every other lane's value. The translator's own emission loads the old vD and selects per lane. So the value vD held before is read, and a shader whose first touch of a VGPR is a writelane should report that VGPR as read before written.

Change:

  • What a writelane leaves: in read_before_write_vgprs, v_writelane_b32 no longer counts as a write of its destination. The kept lanes are carried and become a read only when something other than v_readlane_b32 reads the register before a full write.
  • Why not count every writelane destination as a read (this PR's first version): as @shdann showed, that rejects a VS that spills an SGPR into one lane of a VGPR around the fetch call (fetch mapping), a common compiler pattern.
  • Decisions that change: a PS whose first touch of v0/v1 is a writelane, and that then reads the register, now gets its barycentric input, which those lanes hold. On main they read a zeroed register. The VS spill translates, as on main.
  • Approximation: a v_readlane of a lane the writelane didn't write would read the entry value, and isn't counted.
  • reg_reads_writes: per slice, it counts the writelane destination as read. DST_ALSO_READ names the accumulating ops both functions share.

Tests: tests/semantic/test_writelane_read_before_write.py:

  • the kept lanes, when read afterwards, are a read
  • carried only (no read), and the readlane spill pattern, are not a read
  • a full write ends the carry
  • written first: no read
  • the lane operands are scalar
  • reg_reads_writes counts the destination
  • v_readlane/v_fmac are unchanged
  • @shdann's VS spill (and its written-first variant) translates
  • @shdann's PS case gets BaryCoord

With it, 9/9 pass. Against main's tools/, the four tests of the new reads fail, and the VS spill passes as on main. tests/test_shader_checks.py passes.

python -I -m unittest discover -s tests/semantic: 10 failures, the same ones main has without this change, each with an open PR (#3885/#3948 test_cmp_class_f64, #3883 the v_mul_legacy mutation anchor, #3995 test_loop_ballot_barrier, #4004 test_vcmp16_opsel). I didn't run tests/test_translate.py, because it needs pytest and this machine has only the standard library.

Tested on CPU only. No game files. Claimed on #4257 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

v_writelane_b32 writes one lane of vD and keeps the others, so the value vD
held before is read. read_before_write_vgprs and reg_reads_writes counted
only a write. Both now use one DST_ALSO_READ tuple (the accumulating ops
they listed separately, plus v_writelane_b32).

tests/semantic/test_writelane_read_before_write.py covers a first-touch
writelane, a written-first one, scalar lane operands, reg_reads_writes,
v_readlane_b32 unchanged and the existing fmac case.

@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 5033bf66 on Windows 11: python tests/semantic/test_writelane_read_before_write.py -> 6 tests, OK; compileall and git diff --check clean.

v_writelane_b32 now models the destination read (the old value in lanes other than the selected one), so a lane-spill pair that reads a destination before writing it round-trips. The new cases cover the read-before-write shape; the existing lane-select tests pass. No blocking issue.

@tunczzz

tunczzz commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Checked 5033bf66 with the FUZZ-8 control-flow fuzzer (CPU differential: ISA reference cf_ref vs translation + wave interpreter, lane by lane), using the lanes shape from #4134, which puts v_readlane/v_writelane inside divergent ifs and loops, at lanes EXEC has turned off and at wrapped lane selects. Result: pass, no change in behaviour, which fits the description's "no reject decision changes today".

No GPU run for this one (it changes analysis, not emission). No game files. AI-assisted (Claude Code).

@fromthemoonx

Copy link
Copy Markdown

GPU check: tested 5033bf6 (merged onto main 03b09a0) on NVIDIA GeForce RTX 3060 12 GB (Ampere), driver 610.88, Windows 11 Pro 26300, MSVC (VS 2022 Build Tools, scripts\build_runtime.ps1), Vulkan loader 1.4.363, SPIRV-Tools 2026.4, llvm-mc 23.1.6 (conda-forge): pass.

  • py -3.12 -I scripts\run_gpu_tests.py: 214/214 translated, 214 pass spirv-val. GPU: 164/210 passed, 0 failed, 46 skipped. These are the same counts and the same skips as plain main on this machine: 31 need a native 64-wide subgroup, and 15 are run by the d3d12_* ctest programs.
  • py -3.12 -I tests\semantic\test_writelane_read_before_write.py: OK.

As expected for an analysis-only change, no GPU result moved.

AI-assisted (Claude). No game files.

@Flyander Flyander 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.

Reviewed the current head 5033bf66. The translator now preserves the old destination value for lanes not selected by v_writelane_b32, which is required by the read-before-write spill pattern. The independent semantic tests cover that shape. No blocking issue found.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 5033bf6 on Windows, CPython 3.14.7, CPU only (no GPU, no game files).

python -I tests/semantic/test_writelane_read_before_write.py gives 6 tests, OK. With tools/rdna2spirv.py from the merge base (aa2988c), it gives 3 failures and 1 error.

I found one point. The description says "No reject decision changes today". This is not correct for all inputs.
read_before_write_vgprs has three callers in translate. Each caller makes a decision from the result:

  • At line 13810, a vertex shader that calls the fetch program is rejected with fetch mapping if it reads a VGPR that is not a fetched register.
  • At line 13748, a pixel shader that reads v0 or v1 gets a barycentric input. A read of a different system-input register gives PS system inputs.

Reproduction with hand-written listings: save this script as p4297.py in the repository root. Run python -I p4297.py at this head and at aa2988cc.

import os, sys
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.join(os.getcwd(), "tests", "semantic"))
sys.path.insert(0, os.path.join(os.getcwd(), "tools"))
import support, rdna2spirv

def run(label, name, lines):
    try:
        asm = support.translate_lines(name, lines)
        asm = asm[0] if isinstance(asm, tuple) else asm
        print(f"{label}: translated ({len(asm.splitlines())} lines, BaryCoord input: {'BaryCoord' in asm})")
    except rdna2spirv.Unsupported as e:
        print(f"{label}: rejected: {e}")

# A vertex shader that calls the fetch program and keeps an SGPR in one lane of v40 across the call.
vs = ["v_writelane_b32 v40, s2, 0",
      "s_swappc_b64 s[4:5], s[4:5]",
      "v_readlane_b32 s2, v40, 0",
      "v_mov_b32_e32 v1, s2",
      "exp pos0 v1, v1, v1, v1 done",
      "s_endpgm"]
run("VS, fetch call, v_writelane_b32 v40 first", "vs_writelane_spill", vs)
run("VS, fetch call, v40 written first", "vs_writelane_spill2", ["v_mov_b32_e32 v40, 0"] + vs)
# A pixel shader whose first operation on v0 is v_writelane_b32.
run("PS, v_writelane_b32 v0 first", "ps_writelane_v0", ["v_writelane_b32 v0, s2, 0", "v_mov_b32_e32 v20, v0",
                                                         "exp mrt0 v20, v20, v20, v20 done vm", "s_endpgm"])

Actual result at this head: the first vertex shader gives rejected: fetch mapping. The pixel shader has 276 lines and a BaryCoord input.
Result at aa2988cc: the first vertex shader is translated (288 lines). The pixel shader has 262 lines and no BaryCoord input.

Expected result: the first vertex shader is translated. It keeps an SGPR in one lane of v40 while the fetch call runs, and v40 is not a fetched attribute.
If the corpus has no shader of this shape, a count in the description is sufficient. I cannot make this count, because I have no game files.
The change for the pixel shader can be correct, because the other lanes of v0 keep the barycentric value. But it is a change in the emitted code.

AI-assisted.

@theswiss24

Copy link
Copy Markdown

Tested 5033bf66 merged onto main 03b09a08 (clean) on NVIDIA GeForce RTX 4070 (Ada), driver 621.24, Windows 11 (26300), Python 3.14.2, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8, with test programs built from main 03b09a08: pass.

  • python -I -m unittest discover -s tests/semantic -p test_writelane_read_before_write.py: 6 tests, OK.
  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build <main build>: 195/210 passed, 15 skipped, 0 failed, the same result on every test as main 03b09a08.

No game files.

AI-assisted (Claude Code).

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

GPU run as well: 5033bf66 on NVIDIA GeForce RTX 5070 Ti (Blackwell, driver 616.56, subgroup 32..32), Windows 11 10.0.26300; translator from the PR head, runtime test programs built from main aa2988cc (MSVC 19.44, Ninja RelWithDebInfo): scripts/run_gpu_tests.py 164/210 passed, 46 skipped, 0 failed, identical to main aa2988cc test by test. AI-assisted (Claude Code).

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @shdann. You're right, and the description was wrong: I took "no reject decision changes" from the table entry without checking the three callers. Counting every writelane destination as a read made your VS spill (v40 around the fetch call, read back with v_readlane) fail with fetch mapping, and that's a false rejection of a common compiler pattern.

Fixed in a35d9e55. The writelane's kept lanes are now carried: they become a read only when something other than v_readlane_b32 reads the register before a full write.

  • Your VS spill and the written-first variant: translate again, as on aa2988cc.
  • Your PS case (v_writelane_b32 v0, then v_mov_b32 v20, v0): still gets BaryCoord. The other lanes hold the barycentric i and the v_mov reads them, so this change in emitted code is a correction. On aa2988cc those lanes read a zeroed register.
  • The remaining approximation: a v_readlane of a lane the writelane didn't write would read the entry value, and isn't counted.
  • reg_reads_writes: per slice, it still counts the destination as read.

Both of your listings, plus the readlane-only spill and a full write ending the carry, are now in tests/semantic/test_writelane_read_before_write.py (9/9). Against main's tools/ (via LEONIDA_TOOLS_DIR), the four tests of the new reads fail and the VS spill passes, as on main. The full semantic suite shows only main's 10 known failures. I'll update the description.

…n used

@shdann showed on ItsNotRexed#4297 that counting every v_writelane_b32 destination as a
read changes decisions: a VS that spills an SGPR into one lane of v40 around
the fetch call (read back with v_readlane) was rejected with 'fetch mapping'.
The kept lanes are now carried: they become a read when something other than
v_readlane_b32 reads the register before a full write. A PS whose first
touch of v0 is a writelane and then reads v0 still gets its barycentric
input, which those lanes hold. reg_reads_writes (per slice) keeps counting
the destination as read; DST_ALSO_READ is back to the accumulating ops.

The test adds shdann's VS spill and PS cases end to end, the readlane-only
spill, and a full write ending the carry.
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.

7 participants