Shaders: v_writelane_b32 writes its lane whenever the wave runs it #139

Open
marketingwithlux wants to merge 2 commits from upstream-pr/139 into main
marketingwithlux commented 2026-11-24 21:48:15 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by marketingwithlux on 2026-11-24T21:48:15Z.


Fixes #111. Found while looking into #62; not yet shown to be its cause.

What was wrong

v_writelane_b32 ignores EXEC on the console, but it was lowered under the guard of the lane that runs it. A scalar the compiler parks in a lane that is outside EXEC was lost and came back stale from the later v_readlane_b32. unspill hides this for a VGPR used for parking alone; a parking VGPR with any other use keeps its lane writes.

The change

crates/victory-shader/src/emit/lower.rs, the "v_writelane_b32" arm (28 lines):

  • a guarded lane write asks the wave whether some lane's guard holds (waveAnyTrue) and is written without the guard, so a wave with no lane in EXEC still writes nothing, as when the console takes the skip;
  • two host waves of 32 exchange their answers (exchange_halves). That needs every lane, so a lane write in a block not every lane runs is refused with "a lane write where not every lane runs", as a lane read already is, and emit goes on to the forms without skips.

Tests

a_lane_write_reaches_its_lane_outside_exec (synthetic GFX10 words), run on an RTX 3090 (32-lane waves, Windows 10 with the runtime from #69):

threads inside expected before now
11..63, lane 0 outside 5 7 5
63, lane 0 outside 5 7 5
none (the wave skips) 7 7 7
lane 0 inside (control) 5 5 5

cargo test --workspace: everything passes except emit::rect::tests::the_rectangle_stage_is_a_geometry_shader and rectangles_are_drawn_whole, which fail the same way on main at 4e17478 (PSV0 validation, #12). cargo clippy -p victory-shader --all-targets is clean.

Not done: help wanted

docs/fork-writelane-outside-exec.md in this branch has the details, written so a person or an agent can pick it up. In short:

  • Not run over a title's shaders (no copy on the machine this was written on). If you have one: VICTORY_HOST_WAVE=32 victory translate --validate before and after, and compare the translated and validated counts. A program that newly stops with "a lane write where not every lane runs" in every form is a regression; please report its name.
  • Not run on a 64-lane card. There the change is the waveAnyTrue alone. Running the test above on an AMD card and posting its lines would settle it.
  • Not checked in the game. An A/B of the prison cell would show whether it changes the tiled shadows of #62.
  • Cost: two group barriers per guarded lane write on a 32-lane host, none elsewhere; not measured in a frame.

The expected values are the instruction's documented behaviour, not a console measurement. Nothing from the game is attached. AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:fxceless/victory-fork:issue:5480894277 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/139). Originally posted by [marketingwithlux](https://github.com/marketingwithlux) on 2026-11-24T21:48:15Z. --- Fixes #111. Found while looking into #62; not yet shown to be its cause. ## What was wrong `v_writelane_b32` ignores EXEC on the console, but it was lowered under the guard of the lane that runs it. A scalar the compiler parks in a lane that is outside EXEC was lost and came back stale from the later `v_readlane_b32`. `unspill` hides this for a VGPR used for parking alone; a parking VGPR with any other use keeps its lane writes. ## The change `crates/victory-shader/src/emit/lower.rs`, the `"v_writelane_b32"` arm (28 lines): - a guarded lane write asks the wave whether some lane's guard holds (`waveAnyTrue`) and is written without the guard, so a wave with no lane in EXEC still writes nothing, as when the console takes the skip; - two host waves of 32 exchange their answers (`exchange_halves`). That needs every lane, so a lane write in a block not every lane runs is refused with "a lane write where not every lane runs", as a lane read already is, and `emit` goes on to the forms without skips. ## Tests `a_lane_write_reaches_its_lane_outside_exec` (synthetic GFX10 words), run on an RTX 3090 (32-lane waves, Windows 10 with the runtime from #69): | threads inside | expected | before | now | |---|---|---|---| | 11..63, lane 0 outside | 5 | 7 | 5 | | 63, lane 0 outside | 5 | 7 | 5 | | none (the wave skips) | 7 | 7 | 7 | | lane 0 inside (control) | 5 | 5 | 5 | `cargo test --workspace`: everything passes except `emit::rect::tests::the_rectangle_stage_is_a_geometry_shader` and `rectangles_are_drawn_whole`, which fail the same way on `main` at 4e17478 (PSV0 validation, #12). `cargo clippy -p victory-shader --all-targets` is clean. ## Not done: help wanted `docs/fork-writelane-outside-exec.md` in this branch has the details, written so a person or an agent can pick it up. In short: - **Not run over a title's shaders** (no copy on the machine this was written on). If you have one: `VICTORY_HOST_WAVE=32 victory translate --validate` before and after, and compare the translated and validated counts. A program that newly stops with "a lane write where not every lane runs" in every form is a regression; please report its name. - **Not run on a 64-lane card.** There the change is the `waveAnyTrue` alone. Running the test above on an AMD card and posting its lines would settle it. - **Not checked in the game.** An A/B of the prison cell would show whether it changes the tiled shadows of #62. - Cost: two group barriers per guarded lane write on a 32-lane host, none elsewhere; not measured in a frame. The expected values are the instruction's documented behaviour, not a console measurement. Nothing from the game is attached. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ezbyDev commented 2026-11-24 22:07:41 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by ezbyDev on 2026-11-24T22:05:30Z.


The 64-lane run asked for in the description: passes on AMD, and the test catches the bug there.

Setup: AMD Radeon RX 9060 XT 16 GB (RDNA4, native 64-lane waves), Adrenalin 32.0.32029.2010; i7-9700K; Windows 11 Pro 26300; Rust 1.98.1 (pinned); DXC from the Windows SDK 10.0.26100.

cargo test -p victory-shader --lib a_lane_write_reaches_its_lane_outside_exec -- --nocapture

lane 0 outside (s0 = 10, 62) wave skips (s0 = 63) lane 0 inside (controls) result
this PR, 3c408b4 5, 5 7 5, 5, 7 passes
main 4e17478 + only this PR's test file 7, 7 ("as is", "without skips" and "scalars followed" alike) 7 5, 5, 7 fails, as expected
#119 481569b + only this PR's test file 5, 5 7 5, 5, 7 passes

On this PR the "as is" form prints no lines, so it is refused and the forms without skips run, as the description says.

  • cargo test -p victory-shader --lib on this PR: 211 passed, 0 failed, 2 ignored.
  • #119 overlaps: it also fixes #111, and gives the same values on this test on this card. It may help whoever reviews to compare the two.

Not done here: the VICTORY_HOST_WAVE=32 victory translate --validate comparison over a title's shaders and the prison cell A/B (#62). Both need the title's files, so they're for whoever has them; on a 64-lane card the first would only cover the 32-lane host path anyway.

AI-assisted (Claude Code, Claude Opus 5.5): ran the tests on my machine and wrote this.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7145790609 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/139#issuecomment-7145790609). Originally posted by [ezbyDev](https://github.com/ezbyDev) on 2026-11-24T22:05:30Z. --- The 64-lane run asked for in the description: passes on AMD, and the test catches the bug there. **Setup:** AMD Radeon RX 9060 XT 16 GB (RDNA4, native 64-lane waves), Adrenalin 32.0.32029.2010; i7-9700K; Windows 11 Pro 26300; Rust 1.98.1 (pinned); DXC from the Windows SDK 10.0.26100. `cargo test -p victory-shader --lib a_lane_write_reaches_its_lane_outside_exec -- --nocapture` | | lane 0 outside (s0 = 10, 62) | wave skips (s0 = 63) | lane 0 inside (controls) | result | |---|---|---|---|---| | this PR, `3c408b4` | 5, 5 | 7 | 5, 5, 7 | passes | | `main` `4e17478` + only this PR's test file | **7, 7** ("as is", "without skips" and "scalars followed" alike) | 7 | 5, 5, 7 | fails, as expected | | #119 `481569b` + only this PR's test file | 5, 5 | 7 | 5, 5, 7 | passes | On this PR the "as is" form prints no lines, so it is refused and the forms without skips run, as the description says. - `cargo test -p victory-shader --lib` on this PR: 211 passed, 0 failed, 2 ignored. - **#119 overlaps:** it also fixes #111, and gives the same values on this test on this card. It may help whoever reviews to compare the two. **Not done here:** the `VICTORY_HOST_WAVE=32 victory translate --validate` comparison over a title's shaders and the prison cell A/B (#62). Both need the title's files, so they're for whoever has them; on a 64-lane card the first would only cover the 32-lane host path anyway. AI-assisted (Claude Code, Claude Opus 5.5): ran the tests on my machine and wrote this.
kaayzouee commented 2026-11-25 02:10:07 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by kaayzouee on 2026-11-25T01:27:31Z.


Duplicate of #119?

<!-- greyfinch-sync:fxceless/victory-fork:comment:7149568071 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/139#issuecomment-7149568071). Originally posted by [kaayzouee](https://github.com/kaayzouee) on 2026-11-25T01:27:31Z. --- Duplicate of #119?
Revens2 commented 2026-11-25 06:06:36 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Revens2 on 2026-11-25T05:13:30Z.


Build check only: this PR merges cleanly on main c96b4f2 and the integration build of eight PRs (#115 #66 #36 #144 #147 #141 #139 #130) compiles (diagnostics profile). In the game, that combo was run alone 3 times (900 s, VICTORY_RELAY=2, scripted input): one run to the end (median 4.95 fps, about 4.9k draws per frame, against 4.84-5.04 fps on main), one title breakpoint 0x80000003 at about 157 s after a failed dstorage request (the pattern is explained on #115), one device removed (0x887A0005) at about 160 s that did not repeat in the two other runs. Nothing is proven for this PR alone: it was not run on its own, cargo test was not run on the combo, and nothing was checked on the title side of its change. Not verified.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7153565214 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/139#issuecomment-7153565214). Originally posted by [Revens2](https://github.com/Revens2) on 2026-11-25T05:13:30Z. --- Build check only: this PR merges cleanly on main c96b4f2 and the integration build of eight PRs (#115 #66 #36 #144 #147 #141 #139 #130) compiles (diagnostics profile). In the game, that combo was run alone 3 times (900 s, `VICTORY_RELAY=2`, scripted input): one run to the end (median 4.95 fps, about 4.9k draws per frame, against 4.84-5.04 fps on main), one title breakpoint `0x80000003` at about 157 s after a failed dstorage request (the pattern is explained on #115), one device removed (`0x887A0005`) at about 160 s that did not repeat in the two other runs. Nothing is proven for this PR alone: it was not run on its own, `cargo test` was not run on the combo, and nothing was checked on the title side of its change. Not verified.
Lyrnc commented 2026-11-27 00:46:18 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Lyrnc on 2026-11-27T00:46:18Z.


Tested this on an RTX 5080 against hardware rows from a Radeon (gfx1036) that runs the same kernels natively in wave64 and wave32. #119 and this PR gave the same value in every lane of every case below.

Setup

  • Fork main at 52ac4ec with this branch merged locally. No conflict in lower.rs; the test file conflicted only because both sides appended at its end, so both were kept.
  • Kernels: hand-written GFX10 code, nothing from a title. 151 earlier wave64 cases, 18 of them writelane_branch/<lane>/<mask> (a lane write inside an s_and_saveexec / s_cbranch_execz branch, then a read back after the join). Also 45 more wave64 lane-write cases and 33 wave32 ones: no skip, early-out, a loop whose lanes leave, a small lane array, a read inside the branch, nested branches, and a lane in an SGPR.
  • Expected values: the same kernels run natively on the gfx1036. An independent Python model of the documented behaviour agrees with every hardware row: 45/45 and 33/33.
  • Translator: super::emit with a 32-lane host forced, every form it tries, dxil.dll validation, then dispatched on the 5080. Wide on = with_wave_wide(true); wide off = the default.

Results (exact / differ; nothing was refused)

set main, wide on this PR, wide on main, wide off this PR, wide off
151 wave64 cases 120 / 31 128 / 23 48 / 103 56 / 95
45 wave64 lane-write cases 15 / 30 35 / 10 15 / 30 33 / 12
33 wave32 lane-write cases 13 / 20 14 / 19 13 / 20 14 / 19
  • The 8 of the 151 that become exact are the writelane_branch cases whose lane is outside the branch (0/hi32, 0/lane40, 33/lo32, 33/alt, 33/lane40, 63/lo32, 63/alt, 63/lane40). They now go through "without skips". No other case changed value.
  • Your test a_lane_write_reaches_its_lane_outside_exec passes on the 5080: 5, 5, 7 in both forms without skips, and the control with lane 0 inside gives 5, 5, 7.
  • "Not run on a 64-lane card": the gfx1036 rows answer it for the instruction itself. A lane write in a taken branch lands in a lane outside EXEC: lane 0 holds 0x777 with the branch over lanes 32..63. With the branch skipped (no lane inside), nothing is written.

A regression: a lane write in nested branches is now silently zero

if (lane bit of mask) { if (lane < 48) { v_writelane_b32 v50, s38, K } } (v50 = 0x1234, s38 = 0x777):

case hardware main this PR
wl_nested/0/alt (lane 0 inside) lane 0 = 0x777 exact lane 0 reads 0; the 24 inner lanes hold 0
wl_nested/0/hi32 lane 0 = 0x777 0x1234 0x1234, and lanes 32..47 hold 0
wl_nested/63/hi32, 63/alt lane 63 = 0x777 0x1234 0x1234, and the inner lanes hold 0

All four translate as they are, with no refusal. The refusal comes from an instruction without a side effect, so Lower::inst (lower.rs:5051 on main) only records v50 as having no value. v50 is read after two joins. The inner join is read only by the outer one, and outside VICTORY_STRICT_JOINS (lower.rs:11550) such a join takes the missing value as zero (lower.rs:11662). emit never sees the refusal. With VICTORY_STRICT_JOINS=1 all four are exact with the same change (#119's binary, which gives the same values as this one: wave64 lane-write set 39 / 6). A fix is to make the refusal immediate for the lane write: in Lower::inst, v_writelane_b32 whose error satisfies needs_every_lane returns Err(unsupported(offset, why)). I measured that on top of #219, where it makes the four exact.

What else still differs

  1. EXEC empty with no skip around the write (s_and_saveexec_b64 to no lane, then v_writelane_b32, no s_cbranch_execz). The gfx1036 writes the lane (lanes 0 and 33: 0x777). The PR asks waveAnyTrue of the guard and keeps 0x1234 (wl_noskip/0/none, wl_noskip/33/none). The comment "a wave with none took the skip around it" is right only where there was a skip. Where no skip was left out, the write needs no condition and no exchange.
  2. Lanes 71 / 103 in an SGPR, which the hardware wraps to 7 / 39: not this PR's subject (fork #298).
  3. wl_loop/*: exact with wide on, approximate local forms with wide off.
  4. Wave32 programs are unchanged by this PR: 19 of 33 still lose a lane write whose lane is outside the branch (#219 covers these).

None of the 8 refused wave64 game programs keeps a v_writelane_b32 after spill::build (ir::unspill turns all their parking writes into scalar moves), so this does not touch them.

AI use: Claude Code (Claude Opus 5.5) wrote the kernels and the harness, ran the hardware oracle and the translator, and drafted this comment; the numbers come from those runs.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7206739198 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/139#issuecomment-7206739198). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-27T00:46:18Z. --- Tested this on an RTX 5080 against hardware rows from a Radeon (gfx1036) that runs the same kernels natively in wave64 and wave32. #119 and this PR gave the same value in every lane of every case below. **Setup** - Fork `main` at 52ac4ec with this branch merged locally. No conflict in `lower.rs`; the test file conflicted only because both sides appended at its end, so both were kept. - Kernels: hand-written GFX10 code, nothing from a title. 151 earlier wave64 cases, 18 of them `writelane_branch/<lane>/<mask>` (a lane write inside an `s_and_saveexec` / `s_cbranch_execz` branch, then a read back after the join). Also 45 more wave64 lane-write cases and 33 wave32 ones: no skip, early-out, a loop whose lanes leave, a small lane array, a read inside the branch, nested branches, and a lane in an SGPR. - Expected values: the same kernels run natively on the gfx1036. An independent Python model of the documented behaviour agrees with every hardware row: 45/45 and 33/33. - Translator: `super::emit` with a 32-lane host forced, every form it tries, `dxil.dll` validation, then dispatched on the 5080. Wide on = `with_wave_wide(true)`; wide off = the default. **Results** (exact / differ; nothing was refused) | set | main, wide on | this PR, wide on | main, wide off | this PR, wide off | |---|---|---|---|---| | 151 wave64 cases | 120 / 31 | 128 / 23 | 48 / 103 | 56 / 95 | | 45 wave64 lane-write cases | 15 / 30 | 35 / 10 | 15 / 30 | 33 / 12 | | 33 wave32 lane-write cases | 13 / 20 | 14 / 19 | 13 / 20 | 14 / 19 | - The 8 of the 151 that become exact are the `writelane_branch` cases whose lane is outside the branch (`0/hi32`, `0/lane40`, `33/lo32`, `33/alt`, `33/lane40`, `63/lo32`, `63/alt`, `63/lane40`). They now go through "without skips". No other case changed value. - Your test `a_lane_write_reaches_its_lane_outside_exec` passes on the 5080: 5, 5, 7 in both forms without skips, and the control with lane 0 inside gives 5, 5, 7. - "Not run on a 64-lane card": the gfx1036 rows answer it for the instruction itself. A lane write in a taken branch lands in a lane outside EXEC: lane 0 holds 0x777 with the branch over lanes 32..63. With the branch skipped (no lane inside), nothing is written. **A regression: a lane write in nested branches is now silently zero** `if (lane bit of mask) { if (lane < 48) { v_writelane_b32 v50, s38, K } }` (v50 = 0x1234, s38 = 0x777): | case | hardware | main | this PR | |---|---|---|---| | `wl_nested/0/alt` (lane 0 inside) | lane 0 = 0x777 | exact | lane 0 reads 0; the 24 inner lanes hold 0 | | `wl_nested/0/hi32` | lane 0 = 0x777 | 0x1234 | 0x1234, and lanes 32..47 hold 0 | | `wl_nested/63/hi32`, `63/alt` | lane 63 = 0x777 | 0x1234 | 0x1234, and the inner lanes hold 0 | All four translate as they are, with no refusal. The refusal comes from an instruction without a side effect, so `Lower::inst` (lower.rs:5051 on main) only records v50 as having no value. v50 is read after two joins. The inner join is read only by the outer one, and outside `VICTORY_STRICT_JOINS` (lower.rs:11550) such a join takes the missing value as zero (lower.rs:11662). `emit` never sees the refusal. With `VICTORY_STRICT_JOINS=1` all four are exact with the same change (#119's binary, which gives the same values as this one: wave64 lane-write set 39 / 6). A fix is to make the refusal immediate for the lane write: in `Lower::inst`, `v_writelane_b32` whose error satisfies `needs_every_lane` returns `Err(unsupported(offset, why))`. I measured that on top of #219, where it makes the four exact. **What else still differs** 1. EXEC empty with no skip around the write (`s_and_saveexec_b64` to no lane, then `v_writelane_b32`, no `s_cbranch_execz`). The gfx1036 writes the lane (lanes 0 and 33: 0x777). The PR asks `waveAnyTrue` of the guard and keeps 0x1234 (`wl_noskip/0/none`, `wl_noskip/33/none`). The comment "a wave with none took the skip around it" is right only where there was a skip. Where no skip was left out, the write needs no condition and no exchange. 2. Lanes 71 / 103 in an SGPR, which the hardware wraps to 7 / 39: not this PR's subject (fork #298). 3. `wl_loop/*`: exact with wide on, approximate local forms with wide off. 4. Wave32 programs are unchanged by this PR: 19 of 33 still lose a lane write whose lane is outside the branch (#219 covers these). None of the 8 refused wave64 game programs keeps a `v_writelane_b32` after `spill::build` (`ir::unspill` turns all their parking writes into scalar moves), so this does not touch them. AI use: Claude Code (Claude Opus 5.5) wrote the kernels and the harness, ran the hardware oracle and the translator, and drafted this comment; the numbers come from those runs.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/139:upstream-pr/139'
git switch 'upstream-pr/139'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/139'
git switch 'upstream-pr/139'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/139'
git switch 'upstream-pr/139'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/139'
git switch 'main'
git merge --squash 'upstream-pr/139'
git switch 'main'
git merge --ff-only 'upstream-pr/139'
git switch 'main'
git merge 'upstream-pr/139'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
fxceless/victory-fork!139
No description provided.