Skip to content

Shaders: a folded program takes every branch on a lane mask wave-wide - #3440

Open
Lyrnc wants to merge 2 commits into
trustno1-coder:mainfrom
Lyrnc:fix/w64-fold-lane-branches
Open

Lyrnc wants to merge 2 commits into
trustno1-coder:mainfrom
Lyrnc:fix/w64-fold-lane-branches

Conversation

@Lyrnc

@Lyrnc Lyrnc commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

No issue filed: found while checking the wave64 folding (crate::fold) against a wave64 card with hand-written programs.

What it changes

What was wrong. A folded wave64 program runs one console wave as one host wave, two console lanes per host lane, and keeps the console's scalar code. folded() still took its branches on lane masks per lane, unless the program uses LDS or barriers, sets EXEC to every lane again, or has lane conditions too large to follow. Per lane, a host lane whose two console lanes are both outside EXEC leaves the region, where the console wave runs it once for all its lanes:

  • a scalar written in the region (v_readlane_b32, v_readfirstlane_b32, any scalar instruction) keeps its old value in that lane after the join, where every console lane reads the new one;
  • a loop left on the SCC of a mask operation (v_cmpx, s_andn2_b64, s_cbranch_scc0) stops counting rounds in the lanes that left.

It is wrong only where both console lanes of a host lane (lanes t and t + 32) are out of EXEC while other lanes are in: alternate lanes, one lane of the upper half, sparse masks, or lanes leaving a loop one by one. An EXEC of all lanes or of one half does not show it.

What this does. folded() first builds the structure with every branch on a lane mask taken wave-wide: the EXEC branches, then every branch the structure names as one on a lane mask (Structured::wave_any: VCC, SCC of a mask operation). One host wave is one console wave, so that is the console's control flow. Where structure_wide fails for that set, the forms before this change are taken as they were. VICTORY_WAVE_FOLDED_LANES=1 takes them always. The switch is in the shader cache key (KEYED, second commit), so every_switch_of_the_translator_is_in_the_key still holds. Programs that are not folded do not change.

With folding off by default (5c262d7, 05d1c56). On current main a program is folded first only with VICTORY_WAVE_FOLDED=1. By default folded() is still reached for programs the exact forms do not take, before deep skips, wave-wide branches and the approximate forms, and once more as a last resort; VICTORY_WAVE_FOLDED=0 leaves it out. This change applies to every program that reaches folded(): by default to those fallback programs, with =1 to every folded program, with =0 to none. How many of the title's programs fold by default on current main was not measured here.

Merging. Rebased onto main 7995b81. Main's 1b4b6cd had already taken KEYED from 45 to 46 names (VICTORY_SCALAR_SPLITS), so the old second commit's [&str; 46] edit was empty there, and the array held 47 names in a 46-element array. The second commit (80e8527, "Shader cache: KEYED holds 47 names with VICTORY_WAVE_FOLDED_LANES") now makes it [&str; 47]; 47 is the count of names in the array on 7995b81 plus this branch. If #3210 or #3061 (which drop the stale VICTORY_SCALAR_SPLITS) or #3308 (which adds VICTORY_WIDE_SHADOW_TILES) lands first, the length has to be counted again: a one-line conflict.

Credit: no code from other pull requests.

How it was tested

  • GPU and profile: RTX 5080 and the Ryzen 7 9800X3D's integrated Radeon (gfx1036), Windows 11. Shader tests on the cards, not a run of the game; no Series X/S profile, resolution, DLSS, frame generation or ray tracing involved.

  • Oracle: 219 hand-written one-wave GFX10 wave64 programs (one 64-thread group, three sets of 151, 15 and 53), run natively in wave64 on the gfx1036 with hw_oracle. The same programs are translated by emit::emit for a host of 32-lane waves (every form it tries), run on the RTX 5080 and on the Radeon as a 32-lane host, and all 16 outputs of all 64 lanes are compared. The harness is local (emit::fw64_local_tests, not in this branch): VICTORY_ADAPTER=nvidia|radeon LOCAL_FW64_HOSTWAVE=32 LOCAL_FW64_OUT=<file> VICTORY_WAVE_WHY=1 <test exe> --ignored --exact --nocapture --test-threads=1 emit::fw64_local_tests::<local_fw64_wave_wide|local_fw64x_wave_wide|local_nx_cases>, with LOCAL_FW64_FOLD=1 for the forced folds below.

  • Before (829188e, where folding was still on by default): 156 exact, 51 differ, 12 refused, the same on both cards. 137 of the 219 are folded (118 + 15 + 4).

  • After (cbc69bb): 169 exact, 38 differ, 12 refused, the same on both cards. All 137 folded programs take the new structure; none falls back. 13 programs went from differing to exact: v_readlane_b32 inside an EXEC branch at lanes 0, 31, 32 and 63 with alternate lanes or lane 40 alone (8), and the loops left on s_cbranch_scc0 after s_andn2_b64 (5: all lanes, lower half, upper half, alternate lanes, sparse). None went the other way. The 38 that still differ differ the same way on 829188e (v_writelane_b32 in a branch among them).

  • Folded by force (the harness calling folded() directly; 204 programs, RTX 5080): 155 exact, 34 differ, 15 refused before; 181, 8, 15 after. The 26 that became exact add v_readfirstlane_b32 in a branch, vote loops, a vccz/vccnz vote on such a mask and a scalar instruction in a branch read after the join. None went the other way. This run was on the first build of cbc69bb, before its last amend; the run above was repeated on the final commit with the same counts.

  • New tests scalars_written_in_a_branch_on_exec_are_the_waves_after_the_join and a_loop_left_on_the_scc_of_a_mask_operation_counts_the_waves_rounds (seven masks over both halves, lane reads at 0, 32 and 63): pass on both cards. With VICTORY_WAVE_FOLDED_LANES=1 (the old forms) both fail on both cards (lane 1, v_readlane in the branch; lane 0, rounds).

  • For shader changes, the wave sizes tested (32 and/or 64 lanes): console wave64 programs on a host of 32-lane waves (RTX 5080, and the Radeon held to 32), checked against the gfx1036 in wave64. Not on a host of 64-lane waves.

  • cargo test passed for the crates it touches: partly. On 80e8527 (main 7995b81 + the two commits), RTX 5080:

    • cargo check -p victory-gpu: passes (the rebased KEYED builds).
    • cargo test -p victory-shader --lib -- folded_tests fold::: 19 passed, 0 failed (454 filtered out), with both new tests.
    • cargo test -p victory-gpu --lib -- every_switch_of_the_translator_is_in_the_key: fails, as it does on main 7995b81 without this branch: VICTORY_SCALAR_SPLITS is not read by the translator any more (the stale name gpu(shader_cache): drop stale VICTORY_SCALAR_SPLITS from KEYED #3210 and Tests: four races, the BCPack oracle, the shader key's stale names, and a loop-latch test #3061 drop). The test's first check, that every switch the translator reads is in the key, passes before that, so VICTORY_WAVE_FOLDED_LANES is keyed. With VICTORY_SCALAR_SPLITS taken out of KEYED by hand ([&str; 46], as gpu(shader_cache): drop stale VICTORY_SCALAR_SPLITS from KEYED #3210 would leave it; not committed), the test gets through every KEYED name, VICTORY_WAVE_FOLDED_LANES included, and stops at the next stale name on main, VICTORY_ORACLE_RUNS in its not-a-translation list (main has no oracle_tests.rs).
    • cargo clippy -p victory-shader --all-targets -- -D warnings and cargo clippy -p victory-gpu --all-targets -- -D warnings: both stop in victory-shader on main's own code (unused imports in emit/lower/analysis.rs:6 and emit/lower.rs:594, while_immutable_condition in emit/lower/analysis.rs:467, too_many_arguments in native.rs:1655). Without -D warnings, victory-gpu gives the same 25 lib and 24 test warnings as main. None of these is on a line this branch touches.
    • The whole victory-shader suite was not run on 80e8527.
  • Earlier, on cbc69bb (829188e base) plus a two-line mod for the local harness, built with cargo test -j 6 -p victory-shader --lib --no-run, then the test binary:

    • VICTORY_ADAPTER=nvidia|radeon <exe> --nocapture --test-threads=1 emit::folded_tests: 19 passed, 0 failed on each card;
    • VICTORY_WAVE_FOLDED_LANES=1 VICTORY_ADAPTER=nvidia|radeon <exe> --test-threads=1 emit::folded_tests::scalars_written emit::folded_tests::a_loop_left: 0 passed, 2 failed on each card, as meant;
    • <exe> --test-threads=4 structure emit::wave64_tests emit::folded_tests emit::lower fold:: emit::crosslane_tests (RTX 5080): 115 passed, 0 failed, 1 ignored.
  • Not tested: the game, with VICTORY_WAVE_FOLDED=1 or with the default fallback; the card time of folded programs with this change (5c262d7 turned folding off for its card time); the oracle counts on current main (they are on 829188e; only the cargo runs above are on 7995b81); Series S; cards other than the RTX 5080 and the gfx1036.

  • In game: pending. An earlier in-game A/B is void: its build reused a stale victory-shader from a shared target dir. It is being redone and the result will be posted here.

Checked for overlap

AI use

Claude Code (Claude Opus 5.5) found the cause with the oracle programs, wrote the change, the tests and the oracle programs, ran the measurements and wrote this description. No code from other authors.

🤖 Generated with Claude Code

Lyrnc and others added 2 commits November 30, 2026 00:42
A folded wave64 program (crate::fold) keeps the console's scalar branches,
and one host wave is one console wave, so the console's branches on lane
masks are exact taken wave-wide. They were taken per lane unless the
program uses LDS or barriers or sets EXEC to every lane again. Per lane, a
host lane whose two console lanes are both outside EXEC leaves the region:

- a scalar the region writes (v_readlane_b32, v_readfirstlane_b32, any
  scalar instruction) keeps its old value in that lane after the join,
  where the console ran the region and every lane reads the new value;
- a loop whose exit is the SCC of a mask operation (v_cmpx, s_andn2_b64,
  s_cbranch_scc0) stops counting rounds in the lanes that left.

Wrong wherever lanes of both halves are out of EXEC at once: alternate
lanes, one lane of the upper half, sparse masks. Measured against gfx1036
in wave64 (hand-written programs, hw_oracle) on the RTX 5080 and on the
Radeon iGPU as a 32-lane host: of 151 one-wave cases the translation
folds 118; 13 of them differed (v_readlane in a branch at lanes 0/31/32/63
with alternate lanes or lane 40, the break loops), 0 with this, and none
of the others changed. Folded by force, a vccz/vccnz vote on such a mask
and a scalar written in a branch read after the join are exact too.

Every branch on a lane mask (EXEC, VCC, SCC of a mask operation) is now
taken wave-wide in a folded program. Where that structure fails the
program takes the per-lane forms as before; VICTORY_WAVE_FOLDED_LANES=1
takes them always (in the shader cache key).

Tests: scalars_written_in_a_branch_on_exec_are_the_waves_after_the_join,
a_loop_left_on_the_scc_of_a_mask_operation_counts_the_waves_rounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nick0451

Copy link
Copy Markdown

Exact translation check and an in-game look. The build is v2026.11.29.05 (41adb551) with this PR merged onto it (head 80e8527f).

Translation (victory translate --list, VICTORY_HOST_WAVE=32, all of the title's shaders: 26,180 programs, 25,789 with emitted DXIL):

  • 37 programs change output, and none fails to translate. The summary counts are unchanged.
  • The changed programs:
    • cascadeshadows_rendering:CS_ShadowRenderTiled*: the Cascades and Misc variants (CB, Alpha, Hair, HairAlpha, PPPC); most of the 37.
    • tiled_lighting:CS_ClusterLightsWorldSpace and ...IntensityBased.
    • indirect_diffuse_lighting:CS_DeferredFallback_{BVH,Voxels,Fallback} and CS_ResolveVBuffer_Voxels.
    • sr_tracing:CS_ResolveVolumetrics and CS_DeferredFallbacksAlpha_SkyProbe.
    • scattering_transmittance_system:CS_Gather, CS_Gather_Reference and CS_GatherDensityAreaForReadBack.
    • firevbuffer*:CS_EntityFire* (4 programs).
    • vfx_proj_decal_vbuffer:CS_ProjDecalResolve_Vectorized.
    • ptx_prerender:ptfx_CS_PreRender{Sprite,Trail}.

In game. RTX 3090, Series S, frame generation and RT off. Frozen saves of the balcony (noon) and a city street. 2 starts per build per save, alternating, screenshots 45 s and 90 s into the world:

  • All 8 starts reached the world, with no crash and no new warnings.
  • Shadows, lighting and particles look the same. Screenshots between builds differ no more than screenshots between two starts of the same build (balcony: mean grey-level difference 3.8–7.1 between builds against 4.0–7.0 within a build; the city differs more in both because of traffic).
  • So nothing visibly changes or breaks in these scenes, which fits the change being a correctness fix for lane-mask branches that these frames don't happen to show.
  • No frame-rate claim: these runs weren't set up for it.

🤖 Generated with Claude Code

@dyshay

dyshay commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with --profile diag --features diagnostics. A run on an RTX 4090 is queued (Series S with ray tracing, DLSS Quality). Frame times will follow here.

@xiepeixuan xiepeixuan mentioned this pull request Nov 30, 2026
1 task done
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.

3 participants