rdna2spirv: v_writelane_b32 keeps its destination's other lanes in read_before_write_vgprs (OLD-9910, SHADER-768) #658

Manually merged
github merged 1 commit from gh-pr/658/head into gh-pr/658/base 2026-11-26 09:14:28 +00:00
Owner

@⁠LouisRiverstone on GitHub · 2026-11-24 11:10 UTC · original
LouisRiverstone:carry-old-9910 → main · +112 −12 · 2 files · 1 commits · merged 2026-11-24 15:50 UTC

Claimed in the coordination issue as: OLD-9910 (#14)

Replaces the carried-over draft #175. I'm the original author of old upstream PR 9910.

What and why

SHADER-768: read_before_write_vgprs in tools/rdna2spirv.py treated v_writelane_b32 as a full write of its destination.

v_writelane_b32 vD, sSRC, sLANE writes one lane of vD and leaves the other lanes as they were. The translator's own emission already loads the old vD and selects per lane. So when a shader's first touch of a VGPR is a writelane and it reads that register later, the value from shader entry is read in the lanes that weren't written.

Treating every writelane destination as a read goes too far the other way. On the old PR, shdann showed a VS that spills an SGPR into one lane of v40 around the fetch call and reads it back with v_readlane; that VS would be rejected with fetch mapping. So the kept lanes are now carried: they count as a read only when something other than v_readlane_b32 reads the register before a full write. reg_reads_writes (per slice) counts the destination as read. DST_ALSO_READ holds the accumulating ops that both functions used to list separately.

Known approximation, also written in the docstring: a v_readlane of a lane the writelane didn't write reads the entry value, and that read isn't counted.

Diff: the old branch's final tree diff applied on current main (21625bca8), one commit. It applied cleanly. main's v_swap_b32 entry and the d16 format exclusion are kept.

Tested

  • python -I tests/semantic/test_writelane_read_before_write.py: 9 tests OK (carried vs used lanes, the readlane spill, a full write ending the carry, shdann's VS spill and PS v0 barycentric cases translated end to end).
  • python -I scripts/run_semantic_tests.py tests/semantic: 466 files, 6117 tests, 0 files failed (Linux, Python 3.14.4).
  • python -m compileall -q make_game.py tools tests: OK.

GPUs tested: n/a (CPU-only change, no GPU fixture)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: this fixes a register-use analysis, not a translation rule)
**@⁠LouisRiverstone** on GitHub · 2026-11-24 11:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/658) `LouisRiverstone:carry-old-9910` → `main` · +112 −12 · 2 files · 1 commits · merged 2026-11-24 15:50 UTC Claimed in the coordination issue as: OLD-9910 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) Replaces the carried-over draft #175. I'm the original author of old upstream PR 9910. ### What and why SHADER-768: `read_before_write_vgprs` in `tools/rdna2spirv.py` treated `v_writelane_b32` as a full write of its destination. `v_writelane_b32 vD, sSRC, sLANE` writes one lane of `vD` and leaves the other lanes as they were. The translator's own emission already loads the old `vD` and selects per lane. So when a shader's first touch of a VGPR is a writelane and it reads that register later, the value from shader entry is read in the lanes that weren't written. Treating every writelane destination as a read goes too far the other way. On the old PR, shdann showed a VS that spills an SGPR into one lane of `v40` around the fetch call and reads it back with `v_readlane`; that VS would be rejected with `fetch mapping`. So the kept lanes are now *carried*: they count as a read only when something other than `v_readlane_b32` reads the register before a full write. `reg_reads_writes` (per slice) counts the destination as read. `DST_ALSO_READ` holds the accumulating ops that both functions used to list separately. Known approximation, also written in the docstring: a `v_readlane` of a lane the writelane didn't write reads the entry value, and that read isn't counted. Diff: the old branch's final tree diff applied on current `main` (21625bca8), one commit. It applied cleanly. `main`'s `v_swap_b32` entry and the d16 `format` exclusion are kept. ### Tested - `python -I tests/semantic/test_writelane_read_before_write.py`: 9 tests OK (carried vs used lanes, the readlane spill, a full write ending the carry, shdann's VS spill and PS `v0` barycentric cases translated end to end). - `python -I scripts/run_semantic_tests.py tests/semantic`: 466 files, 6117 tests, 0 files failed (Linux, Python 3.14.4). - `python -m compileall -q make_game.py tools tests`: OK. GPUs tested: n/a (CPU-only change, no GPU fixture) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: this fixes a register-use analysis, not a translation rule)
v_writelane_b32 writes one lane and keeps the others. read_before_write_vgprs
counted it as a full write. Counting every writelane destination as a read is
too much the other way (@shdann on old PR 4297: a VS spilling an SGPR into one lane
of v40 around the fetch call 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. reg_reads_writes (per
slice) counts the destination as read. DST_ALSO_READ names the accumulating
ops both functions list.

tests/semantic/test_writelane_read_before_write.py: carried and used lanes,
the readlane spill, a full write ending the carry, shdann's VS spill and PS
cases end to end.

Carried over from old-pr/9910 (old upstream PR 9910, draft #175 here), rebased on main.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:14:27 +00:00
github manually merged commit f05bc53865 into gh-pr/658/base 2026-11-26 09:14:28 +00:00
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-24 11:25 UTC · original

Checked f05bc538 on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main d42773d07:

  • tests/semantic/test_writelane_read_before_write.py: 9 tests OK at this head; the same file against main's translator fails 4 of 9 (AssertionError: Items in the second set but not the first), so the new assertions do catch the gap.
  • GPU with LEONIDA_W64_EMULATE=1, MoltenVK 1.4.2 (subgroup 4..32), binaries from main: cs_w64x_writelane PASS, words 4660 77 99 2748 0 0 4660 99; same on main on the same filter, so no regression.

llvm-mc 23.1.6, SPIRV-Tools v2026.4. Synthetic fixtures only, no game data. AI-assisted.

**@⁠GeorgiyDemo** on GitHub · 2026-11-24 11:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/658#issuecomment-7130072493) Checked `f05bc538` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `d42773d07`: - `tests/semantic/test_writelane_read_before_write.py`: 9 tests OK at this head; the same file against `main`'s translator fails 4 of 9 (`AssertionError: Items in the second set but not the first`), so the new assertions do catch the gap. - GPU with `LEONIDA_W64_EMULATE=1`, MoltenVK 1.4.2 (subgroup 4..32), binaries from `main`: `cs_w64x_writelane` PASS, words `4660 77 99 2748 0 0 4660 99`; same on `main` on the same filter, so no regression. llvm-mc 23.1.6, SPIRV-Tools v2026.4. Synthetic fixtures only, no game data. AI-assisted.
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-24 11:40 UTC · original · reviewed

Reviewed on head f05bc5386 (Linux, no GPU: CPU only).

Checked:

  • The carry logic: a writelane destination not yet written becomes carried; any read other than v_readlane_b32 turns it into a read-before-write; the DST_ALSO_READ ops and d16 loads on a carried register still count as reads. The other reg_reads_writes user (the computed I/J pair check, ~line 16914) is unaffected: a writelane of the p1 register between the ops was already refused through ww.
  • Pass: test_writelane_read_before_write.py (9), test_store_liveins.py, test_wave64_halves.py, test_wave64_cross_half.py, test_wave32_lane_select.py, test_sdwa_dst_mask.py, tests/test_vs_input_survey_rbw.py, tests/test_lane_masks.py, tests/test_crosslane_fuzz.py.
  • tools/semantic_mutants.py gives 0 mutants for these lines (it mutates SPIR-V ops and constants, not Python analysis), so I ran six hand-written logic mutations against the new test file:
    • caught: dropping the v_readlane exemption; treating writelane as a full write again; dropping writelane from reg_reads_writes.
    • survived, equivalent: removing carried.difference_update(dst), and carrying a writelane destination even after a full write. r in written is checked first, so once a register is written its carried membership never matters; both lines could go, or stay as documentation.
    • survived, a gap: making lane_read also match v_readfirstlane_b32. Treating readfirstlane as a real read is the safe choice (the first active lane need not be the one written), but nothing pins it; one case like rbw("v_writelane_b32 v5, s1, 3", "v_readfirstlane_b32 s6, v5") == {5} would.

A possible refinement for the documented approximation, not needed here: when both lane operands are immediates, a v_readlane of a different lane than the writelane wrote reads the entry value and could count as a read; the spill pattern always reads back the same lane.

Looks good to me.

**@⁠qhukz13** on GitHub · 2026-11-24 11:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/658#pullrequestreview-6038538548) · **reviewed** Reviewed on head f05bc5386 (Linux, no GPU: CPU only). Checked: - The carry logic: a writelane destination not yet written becomes carried; any read other than `v_readlane_b32` turns it into a read-before-write; the DST_ALSO_READ ops and d16 loads on a carried register still count as reads. The other `reg_reads_writes` user (the computed I/J pair check, ~line 16914) is unaffected: a writelane of the p1 register between the ops was already refused through `ww`. - Pass: `test_writelane_read_before_write.py` (9), `test_store_liveins.py`, `test_wave64_halves.py`, `test_wave64_cross_half.py`, `test_wave32_lane_select.py`, `test_sdwa_dst_mask.py`, `tests/test_vs_input_survey_rbw.py`, `tests/test_lane_masks.py`, `tests/test_crosslane_fuzz.py`. - `tools/semantic_mutants.py` gives 0 mutants for these lines (it mutates SPIR-V ops and constants, not Python analysis), so I ran six hand-written logic mutations against the new test file: - caught: dropping the `v_readlane` exemption; treating writelane as a full write again; dropping writelane from `reg_reads_writes`. - survived, equivalent: removing `carried.difference_update(dst)`, and carrying a writelane destination even after a full write. `r in written` is checked first, so once a register is written its `carried` membership never matters; both lines could go, or stay as documentation. - survived, a gap: making `lane_read` also match `v_readfirstlane_b32`. Treating readfirstlane as a real read is the safe choice (the first active lane need not be the one written), but nothing pins it; one case like `rbw("v_writelane_b32 v5, s1, 3", "v_readfirstlane_b32 s6, v5") == {5}` would. A possible refinement for the documented approximation, not needed here: when both lane operands are immediates, a `v_readlane` of a different lane than the writelane wrote reads the entry value and could count as a read; the spill pattern always reads back the same lane. Looks good to me.
This discussion has been locked. Commenting is limited to contributors.
No description provided.