Shaders: scalars leave divergent loops and branches as the wave's last path left them #269
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!269
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-269"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@TheHoracio on GitHub · 2026-11-24 01:55 UTC · original
TheHoracio:fix/scalars-leave-divergent-paths→main· +564 −2 · 2 files · 2 commits · openOn the console, a wave has one set of scalar registers. A divergent loop runs until its last lane leaves, and each path of a divergent branch that some lane takes runs in code order. So after the loop or the join, every lane reads the scalars that the last iteration or path left. The lowering runs scalar code per lane, so each lane left with its own path's values. That gives wrong numbers with no error, or a refused shader:
s_or_b64 s[2:3], vcc, s[2:3]) or a scalar counter (s_add_u32) read after a divergent loop came out as 1, 2, 3, … 64 across a 64-lane wave, instead of 64 in every lane.a lane mask holding lanes elsewhere read as data. In the run reported on #49, this was the second most common reason a shader did not translate (80 of 370).Change (
crates/victory-shader/src/emit/lower.rs, compute programs only)s_{and,or,xor,andn2,orn2,nand,nor,xnor,not,mov}_b{32,64}) get their value as a number from their sources' numbers, instead of stopping witha lane mask read as data.LOOP_LIMITalready keeps: onewaveActiveOpmax of the count, then one per value.Pixel, vertex and mesh programs are unchanged. Their host waves need not be the console's (an RX 7800 XT runs wave32 pixel programs 64 lanes wide, see #125), so a host wave's value could mix console waves.
Shortcuts, also in comments:
Tests
Four GPU tests in
valu_tests.rs. They use a newbuild_lanes_cs: a 64-lane wave64 compute fixture that stores v0 per lane. All synthetic, no game data.maind2a4baeloop_exit_mask_counted_after_the_loopscalar_counted_in_a_divergent_loop_read_after_itmask_grown_inside_a_branch_counted_after_ita lane mask holding lanes elsewhere read as datascalar_counted_in_nested_divergent_loopsTested
rust-toolchain.toml); debug.cargo test --workspace --no-fail-fast: 268 passed, 0 failed, 4 ignored (main: 264 / 0 / 4).cargo clippy -p victory-shader --all-targets: no warnings.cargo fmt --check -p victory-shader: clean.cargo xtask policy: 151 files ok.Not tested
victory translate --validateon your own pack, before and after, would show how many more compute shaders translate.AI use
🤖 Generated with Claude Code
@MisaXz on GitHub · 2026-11-24 02:08 UTC · original · edited 2026-11-24 02:11 UTC
Test report on NVIDIA, which the description lists as not tested: the four new tests fail on a 32-lane host.
Tested
9356bee(based2a4bae):cargo test --workspace --no-fail-fast: 264 passed, 4 failed, 4 ignored.maind2a4baeon the same machine: 264 passed, 0 failed, 4 ignored.cargo test -q -p victory-shader --lib(69 passed, 4 failed).scalar_counted_in_a_divergent_loop_read_after_itscalar_counted_in_nested_divergent_loopsloop_exit_mask_counted_after_the_loophost.compute_pipelinefails:pipeline: "The parameter is incorrect."(valu_tests.rs:780)mask_grown_inside_a_branch_counted_after_itThe two wrong-value results are what each 32-lane half would count on its own: lanes 0-31 loop at most 32 times (and at most 2 inner x 4 outer = 8 in the nested test), lanes 32-63 at most 64 (4 x 4 = 16). So the 64-thread group runs here as two host waves that each stop at their own last lane.
For the two pipeline failures I have only a guess, not checked: both programs count a 64-bit mask with
s_bcnt1_i32_b64, and the host cannot make a 64-lane pipeline.Two of the tests also
unwrap()Host::new(false)where the first one skips, in case that matters for hosts without an SM 6.6 device.Not tested
Follow-up: together with #233 (same machine, local only)
727e884alone:cargo test --workspace --no-fail-fast: 277 passed, 0 failed, 4 ignored.error[E0063]: missing fields console_cf and target in initializer of Parts<'_>inbuild_lanes_cs(#233 adds those fields). Whichever lands second needs that fixed.build_lanes_csto translate through #233'semit_for(&native, host.target())(host range 32..32 here) instead of lowering by hand.cargo test -p victory-shader --lib: 82 passed, 3 failed:emit_formask_grown_inside_a_branch_counted_after_itloop_exit_mask_counted_after_the_looptranslation: Unsupported { offset: 24, what: "a lane-mask join of an untracked mask" }scalar_counted_in_a_divergent_loop_read_after_itscalar_counted_in_nested_divergent_loopsSo on a 32-lane host the split in #233 does not by itself give the scalar-in-a-divergent-loop result this PR checks: those two programs read no other lane, so they are split in halves and each half stops at its own last lane. That experiment is my change to the test helper, not part of either PR.
AI use
@r4tedx on GitHub · 2026-11-24 02:23 UTC · original
A result from an AMD GPU that forms 32-lane waves, following the NVIDIA report above.
Tested
9356bee, with a local patch only so the test host picks an adapter by name:loop_exit_mask_counted_after_the_loopmask_grown_inside_a_branch_counted_after_itscalar_counted_in_a_divergent_loop_read_after_itscalar_counted_in_nested_divergent_loopsSo it is not only hosts without 64-lane waves: this Radeon driver runs a compute shader that declares no wave size in waves of 32 although it offers 64 (the RX 7800 XT in the description evidently forms 64). The two scalar programs read no mask as a number and no other lane, so they declare no wave size, and the maxima are taken per host wave. The two mask tests pass on the Radeon because reading a mask as data makes the program declare
WaveSize(64).A possible fix inside this PR: make a program the new code applies to declare the console's wave size (the same condition that sets
console_lanesfor masks read as data), so the host wave is the console wave wherever the pipeline is accepted. On a 32..32 host that then refuses the pipeline, as the two mask tests show.For those hosts there is an alternative I tried: #233 / #320 add a "console flow" (every lane runs every block, branches taken wave-wide), where scalars are the wave's own by construction. With a local switch sending these four programs through it, all four are exact on both adapters here, the RTX 5070 in two 32-lane halves. Details in my reply on #233. Neither PR selects that form for these programs today, so this is a note on how the two could fit together, not a claim that they already do.
Not tested: release build, the title (no game files used).
AI-assisted.
@BlueMew on GitHub · 2026-11-24 02:55 UTC · original
Test report on AMD RDNA4 and Windows 10, with the before/after count the description asks for.
Tested
rust-toolchain.toml; release builds.maind2a4baeand this PR at9356bee:victory translate --validate --listover a retail dump, validatordxil.dllfrom Windows SDK 10.0.26100.maind2a4bae9356beePer-program diff of the two
--listoutputs: 268 programs change outcome, all compute. None is newly validated and none loses validation.a lane mask holding lanes elsewhere read as data: 276 → 8.a descriptor read only as data, 10 ata structured buffer of unknown stride, 5 emit but fail validation (Entry function performs some operation that is incompatible with the shader stage or other entry properties), 4 ata select on an untracked mask, 3 ata descriptor's address or format read as data, 2 ata lane-mask join of an untracked mask, 1 ats_getpc_b64, 1 ata scalar memory read as data.So on this title the change clears this blocker, but on its own it brings no compute program to validation yet;
a descriptor read only as datais next for 242 of the 268. The count cannot show the PR's other effect: programs that already translate and whose scalars read after a divergent loop now get the wave's value.Not tested
valu_testsfail here onmaintoo.AI use
@r4tedx on GitHub · 2026-11-24 11:07 UTC · original
@BlueMew, your before/after counts clarify what to compare with the console-flow experiment on #233. The mask refusal clears for 268 compute programs, and 242 then stop at descriptor reads. Validation counts measure that next blocker. The scalar result after a divergent loop still needs the execution check from the four synthetic programs. Thanks for reporting the two measurements separately.
AI-assisted (Codex).
@qntic on GitHub · 2026-11-24 11:12 UTC · original
tested this on my rx 6600 xt. 2 of the 4 gpu tests failed because the translated compute shader ran in wave32 instead of the required wave64.
added a fix that requests the console wave size when emitting wave reductions. all 4 tests now pass across 10 consecutive runs.
fix:
https://github.com/qntic/VIctory/tree/pub/269-console-wave-size
@qntic on GitHub · 2026-11-24 13:25 UTC · original
tried integrating this pr with main
ffe310e. 7 waterfall tests from #307/#566 fail after the merge.gpu values are still correct, but unexpected wave operations appear in the translated code. reverted #269 from my integration branch for now.
something to check
@Marvisak on GitHub · 2026-11-25 00:24 UTC · original
Automated triage for the maintainers.
Needs rebase. This PR does not merge cleanly into current
main(ac2525d). Conflicting files:crates/victory-shader/src/emit/lower.rs,crates/victory-shader/src/emit/valu_tests.rs.The content has not been reviewed yet; it will be once the branch is rebased onto
main.Generated by Claude Code
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.