Shaders: in a pixel loop, a helper lane's v_readfirstlane_b32 reads its own value #224

Open
murillojndem wants to merge 1 commit from upstream-pr/224 into main
murillojndem commented 2026-11-29 10:23:14 +00:00 (Migrated from github.com)

Imported from DexNom/VIctory. Originally posted by murillojndem on 2026-11-29T10:23:14Z.


On the console, whole-quad mode puts a pixel shader's helper lanes in EXEC, so a loop over the lanes' values (read the first lane's with v_readfirstlane_b32, run the lanes holding it, take them out of EXEC, as the console compiler's waterfall loops do) serves the helper lanes too, each in the pass that reads its value. The host's wave operations leave helper lanes out (DXIL without WaveOpsIncludeHelperLanes), and in the tests below the helpers of such a loop leave it with the values they held before it. Their quads then read those values after the loop: a DPP quad read here, and an implicit derivative reads the same lanes.

Marvisak/VIctory@0601955 (by Vax; upstream PR 1733, for upstream issue 908) reports the effect in the title, with the loop limit then at 1<<20: draws of 0.3 to 1.3 s each until DEVICE_HUNG, about 200 s into the world. This tree cuts pixel loops at 128 passes, so the cost is bounded here; the wrong values after the loop remain.

Change: inside a loop of a pixel shader, a helper lane (dx.op.isHelperLane) takes its own value as v_readfirstlane_b32's result. It matches in its first pass and leaves with the value it leaves with on the console. Lanes that are not helpers read the first lane's value as before. Two kinds of read are unchanged:

  • reads outside loops: there the host's first-lane value reaches the helpers, and every quad holds one value as on the console (measured below);
  • a read whose value a scalar memory read takes as an address or offset, directly or through scalar instructions. This is, for example, a waterfall over a table offset that resource::Waterfall did not prove, which the translation then loads with one index for all lanes; a helper's own value would make that index differ between lanes.

Waterfalls that resource::Waterfall proves are already read per lane and do not reach this code.

This restores 0601955 for loops only. Upstream's form applies to every read, which in a test below gives the helpers of a triangle's edge quads their own value outside a loop, where the console gives them the first lane's. 0601955 is in this repository's history: 570a941 ("Publish DexNom Runtime Radeon sources with upstream credits and build instructions") removed it, with no note on why. If that was deliberate for the Radeon runtime, this PR is not wanted. For traceability: #114, #115, #130 and #173 carry a commit (9fea743) with upstream's form applied to every first-lane read of a pixel shader, code-table addresses included, on unless VICTORY_HELPER_OWN=0. This PR is the change alone, limited to loops, with tests.

Tests, in emit::helper_lane_tests. Each draws two triangles that share a diagonal into a 16x16 target, so the quads on the diagonal hold helper lanes, and runs on WARP and on the hardware adapter:

  • helper_lanes_leave_a_first_lane_loop_with_their_own_key: a whole-quad pixel program runs a first-lane loop over a key unique to each pixel, then reads its quad's four keys.
  • helpers_sharing_their_quads_key_leave_the_loop_with_it: the same with the quad's index as the key, which the quad's lanes share (a primitive's or a material's index in a title).
  • drawn_lanes_take_their_turn_in_a_first_lane_loop: with a key a row shares, the triangle's own lanes still leave in different passes.
  • a_first_lane_read_reaches_the_helper_lanes: outside a loop, every quad reads one first-lane value, edge quads included.
  • emit::lower::tests::a_first_lane_read_feeding_a_scalar_load_is_found: a first-lane read shifted into a scalar load's offset is found, one only a compare reads is not.

Results on 4ad8692, on WARP and an RX 7900 XT (driver 32.0.32015.7029):

unique key shared key turns outside a loop
main fails on both: 32 pixels, all in the 8 quads on the diagonal passes on WARP; fails on the RX 7900 XT, 24 pixels whose helpers read 0 passes passes
this change passes on both passes on both passes on both passes on both
upstream's form, every read passes passes passes fails on both: 8 and 24 edge pixels
every lane its own value in a loop passes passes fails on both passes
  • The victory-shader suite on WARP goes from 234 passed, 5 failed, 4 ignored to 239, 5, 4. The same five fail on both: two rect tests fail PSV0 validation with the SDK 10.0.22621 dxil.dll, and three valu_tests fail pipeline creation on WARP only. On the RX 7900 XT it gives 242 passed, 2 failed (the rect tests), 4 ignored.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean with and without --all-features, cargo xtask policy passes, and rustfmt reports nothing new.

Limits:

  • A helper runs the loop's body in its first pass, not in the pass of its value. So a body whose quad lanes read each other in that pass (a quad read, an implicit derivative) gets nothing from the helper there. On the RX 7900 XT main already gives such a body nothing from these helpers: in a test that counted passes inside the body (not in this PR), their count stayed 0 even when they shared their quad's key.
  • In that first pass the read's value differs between the helpers and the other lanes. Scalar loads that take it are excluded as above; another use that assumes it the same in every lane would see it differ.
  • In a loop that is not such a loop, with a value read each pass and used as a broadcast, helpers take their own value too.
  • The narrower rule was not used: a helper taking its own value only once no other lane is left. On the RX 7900 XT none of five forms of it got the helpers through the loop: an any-lane test of true, or of the lanes that are not helpers; a first-lane read of the pass count, plain or made odd in helpers; and a bound of the passes counted on entry. WARP followed all five.
  • Untouched: the s_ff1 plus v_readlane_b32 form of such a loop, and code-table addresses read through v_readfirstlane_b32 (code_smem).
  • Not measured here: the cost of the bounded loop before the change, and how many of the title's pixel programs have such loops.

Claude (Anthropic) ported the change, wrote the tests, and ran them on WARP and on the RX 7900 XT under the adaptive GPU rules.

Generated with Claude Code

<!-- greyfinch-sync:DexNom/VIctory:issue:5549795917 --> Imported from [DexNom/VIctory](https://github.com/DexNom/VIctory/pull/224). Originally posted by [murillojndem](https://github.com/murillojndem) on 2026-11-29T10:23:14Z. --- On the console, whole-quad mode puts a pixel shader's helper lanes in EXEC, so a loop over the lanes' values (read the first lane's with `v_readfirstlane_b32`, run the lanes holding it, take them out of EXEC, as the console compiler's waterfall loops do) serves the helper lanes too, each in the pass that reads its value. The host's wave operations leave helper lanes out (DXIL without `WaveOpsIncludeHelperLanes`), and in the tests below the helpers of such a loop leave it with the values they held before it. Their quads then read those values after the loop: a DPP quad read here, and an implicit derivative reads the same lanes. Marvisak/VIctory@0601955 (by Vax; upstream PR 1733, for upstream issue 908) reports the effect in the title, with the loop limit then at 1<<20: draws of 0.3 to 1.3 s each until DEVICE_HUNG, about 200 s into the world. This tree cuts pixel loops at 128 passes, so the cost is bounded here; the wrong values after the loop remain. Change: inside a loop of a pixel shader, a helper lane (`dx.op.isHelperLane`) takes its own value as `v_readfirstlane_b32`'s result. It matches in its first pass and leaves with the value it leaves with on the console. Lanes that are not helpers read the first lane's value as before. Two kinds of read are unchanged: - reads outside loops: there the host's first-lane value reaches the helpers, and every quad holds one value as on the console (measured below); - a read whose value a scalar memory read takes as an address or offset, directly or through scalar instructions. This is, for example, a waterfall over a table offset that `resource::Waterfall` did not prove, which the translation then loads with one index for all lanes; a helper's own value would make that index differ between lanes. Waterfalls that `resource::Waterfall` proves are already read per lane and do not reach this code. This restores 0601955 for loops only. Upstream's form applies to every read, which in a test below gives the helpers of a triangle's edge quads their own value outside a loop, where the console gives them the first lane's. 0601955 is in this repository's history: 570a941 ("Publish DexNom Runtime Radeon sources with upstream credits and build instructions") removed it, with no note on why. If that was deliberate for the Radeon runtime, this PR is not wanted. For traceability: #114, #115, #130 and #173 carry a commit (9fea743) with upstream's form applied to every first-lane read of a pixel shader, code-table addresses included, on unless `VICTORY_HELPER_OWN=0`. This PR is the change alone, limited to loops, with tests. Tests, in `emit::helper_lane_tests`. Each draws two triangles that share a diagonal into a 16x16 target, so the quads on the diagonal hold helper lanes, and runs on WARP and on the hardware adapter: - `helper_lanes_leave_a_first_lane_loop_with_their_own_key`: a whole-quad pixel program runs a first-lane loop over a key unique to each pixel, then reads its quad's four keys. - `helpers_sharing_their_quads_key_leave_the_loop_with_it`: the same with the quad's index as the key, which the quad's lanes share (a primitive's or a material's index in a title). - `drawn_lanes_take_their_turn_in_a_first_lane_loop`: with a key a row shares, the triangle's own lanes still leave in different passes. - `a_first_lane_read_reaches_the_helper_lanes`: outside a loop, every quad reads one first-lane value, edge quads included. - `emit::lower::tests::a_first_lane_read_feeding_a_scalar_load_is_found`: a first-lane read shifted into a scalar load's offset is found, one only a compare reads is not. Results on `4ad8692`, on WARP and an RX 7900 XT (driver 32.0.32015.7029): | | unique key | shared key | turns | outside a loop | |---|---|---|---|---| | `main` | fails on both: 32 pixels, all in the 8 quads on the diagonal | passes on WARP; fails on the RX 7900 XT, 24 pixels whose helpers read 0 | passes | passes | | this change | passes on both | passes on both | passes on both | passes on both | | upstream's form, every read | passes | passes | passes | fails on both: 8 and 24 edge pixels | | every lane its own value in a loop | passes | passes | fails on both | passes | - The victory-shader suite on WARP goes from 234 passed, 5 failed, 4 ignored to 239, 5, 4. The same five fail on both: two `rect` tests fail PSV0 validation with the SDK 10.0.22621 `dxil.dll`, and three `valu_tests` fail pipeline creation on WARP only. On the RX 7900 XT it gives 242 passed, 2 failed (the `rect` tests), 4 ignored. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` is clean with and without `--all-features`, `cargo xtask policy` passes, and rustfmt reports nothing new. Limits: - A helper runs the loop's body in its first pass, not in the pass of its value. So a body whose quad lanes read each other in that pass (a quad read, an implicit derivative) gets nothing from the helper there. On the RX 7900 XT `main` already gives such a body nothing from these helpers: in a test that counted passes inside the body (not in this PR), their count stayed 0 even when they shared their quad's key. - In that first pass the read's value differs between the helpers and the other lanes. Scalar loads that take it are excluded as above; another use that assumes it the same in every lane would see it differ. - In a loop that is not such a loop, with a value read each pass and used as a broadcast, helpers take their own value too. - The narrower rule was not used: a helper taking its own value only once no other lane is left. On the RX 7900 XT none of five forms of it got the helpers through the loop: an any-lane test of true, or of the lanes that are not helpers; a first-lane read of the pass count, plain or made odd in helpers; and a bound of the passes counted on entry. WARP followed all five. - Untouched: the `s_ff1` plus `v_readlane_b32` form of such a loop, and code-table addresses read through `v_readfirstlane_b32` (`code_smem`). - Not measured here: the cost of the bounded loop before the change, and how many of the title's pixel programs have such loops. Claude (Anthropic) ported the change, wrote the tests, and ran them on WARP and on the RX 7900 XT under the adaptive GPU rules. Generated with [Claude Code](https://claude.com/claude-code)
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/224:upstream-pr/224'
git switch 'upstream-pr/224'

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/224'
git switch 'upstream-pr/224'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/224'
git switch 'upstream-pr/224'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/224'
git switch 'main'
git merge --squash 'upstream-pr/224'
git switch 'main'
git merge --ff-only 'upstream-pr/224'
git switch 'main'
git merge 'upstream-pr/224'
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
DexNom/VIctory!224
No description provided.