shader: keep branches per lane around reads of running lanes #542

Open
github-bot wants to merge 5 commits from github-bot/gh-542 into main AGit
Owner

@⁠ManGuyNY on GitHub · 2026-12-05 01:05 UTC · original
ManGuyNY:perf/lane-reads-per-lane → main · +195 −2 · 2 files · 5 commits · open

shader: keep branches per lane around reads of running lanes

Problem

lane_op_splits took every per-lane branch that contains a v_readlane_b32 wave-wide (structure_wide). Inside a wave-wide branch, each write becomes a guarded select(guard, new, old), so far more values stay live at once.

In the heaviest compute program of the game I profiled, this alone raised register use from about 112 VGPRs to 256. Occupancy fell to 4 waves per SIMD, and that one dispatch was the most expensive event in the frame.

Change

When the console wave and the host wave are the same size, a lane read only needs the wave-wide branch if the lane it reads might not run it. Two common cases read a lane that does run it, and those now keep their branch per lane:

  • Whole-wave sections. A read inside a section, or of a value a section left (Section::reads), already gets the console's value from the section simulation.
  • First lane of a mask ANDed with the current EXEC. The pattern is s_and m, x, exec then s_ff1 l, m, in the same block. The compiler uses this to take one value of the running lanes, for WaveActiveAllEqual or an atomic's returned value. WaveReadLaneAt of that lane gives the console's value.

These cases still take their branch wave-wide:

  • v_writelane_b32
  • reads of any other lane
  • every lane read in a console wave64 that runs as two host waves of 32, since the read can cross to the other host wave

Empty mask: if the mask is empty, s_ff1 gives -1 and no lane is read. The idioms only use the read value in that mask's lanes, so nothing reads it.

Results

Translating every program from one game (26,084 programs):

Before After
Programs with wave-wide branches 930 51
Programs that fail to translate 195 (geometry shaders, unrelated) 195 (same)
Top compute program 256 VGPRs, 4 waves/SIMD 112 VGPRs, no spills, 9 waves/SIMD, passes DXIL validation

GPU busy time per frame, measured on a scripted, repeatable run of the game's intro sequence, went from 41.4 ms to 39.3 ms (about 5% less). With wave-wide branching turned off entirely, as an upper bound that is not a valid fix, it was 39.1 ms. So this change recovers nearly all of what wave-wide branching costs in this game.

Tested hardware: only one GPU, an AMD Radeon RX 6800 XT (Navi 21, RDNA2). It ran on Linux under Wine with vkd3d-proton and the repo's patched RADV. The top program runs as wave32 there. I haven't measured on RDNA3, NVIDIA or Windows drivers. The register savings should apply anywhere, but the frame-time gain on other GPUs is untested.

Tests

New tests in wave64_tests.rs:

  • a_lane_read_of_the_first_running_lane_keeps_its_branch_per_lane. It fails without the change.
  • a_lane_read_of_any_other_lane_takes_its_branch_wave_wide. This covers both an arbitrary lane and a mask that is not ANDed with EXEC.

On the same RX 6800 XT, victory-shader gives 421 passed, 1 failed, 4 ignored. The one failure, valu_tests::legacy_multiply_adds_fuse_their_product_and_flush_their_sources (hardware case 0), also fails without this change. That program has no lane reads.

The changed code passes rustfmt with the repo's rustfmt.toml and produces no clippy warnings. cargo xtask policy passes.

Not covered

The timing benchmark covers the intro sequence only, not gameplay. The translation counts above cover every program the game has. Scenes that use different compute programs may gain more or less.

I compared the game's output with and without the change by eye only, and saw no visual differences. I did not run a pixel-by-pixel comparison.

The translation cache records the translator source hash, so translations cached before this change are redone automatically.

🤖 Generated with Claude Code

EDIT: All testing was done with the 'high' preset with ray tracing disabled and render resolution manually set to 1.0 (1440p).

**@⁠ManGuyNY** on GitHub · 2026-12-05 01:05 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/542) `ManGuyNY:perf/lane-reads-per-lane` → `main` · +195 −2 · 2 files · 5 commits · open ## shader: keep branches per lane around reads of running lanes ### Problem `lane_op_splits` took every per-lane branch that contains a `v_readlane_b32` wave-wide (`structure_wide`). Inside a wave-wide branch, each write becomes a guarded `select(guard, new, old)`, so far more values stay live at once. In the heaviest compute program of the game I profiled, this alone raised register use from about 112 VGPRs to 256. Occupancy fell to 4 waves per SIMD, and that one dispatch was the most expensive event in the frame. ### Change When the console wave and the host wave are the same size, a lane read only needs the wave-wide branch if the lane it reads might not run it. Two common cases read a lane that does run it, and those now keep their branch per lane: - **Whole-wave sections.** A read inside a section, or of a value a section left (`Section::reads`), already gets the console's value from the section simulation. - **First lane of a mask ANDed with the current EXEC.** The pattern is `s_and m, x, exec` then `s_ff1 l, m`, in the same block. The compiler uses this to take one value of the running lanes, for `WaveActiveAllEqual` or an atomic's returned value. `WaveReadLaneAt` of that lane gives the console's value. These cases still take their branch wave-wide: - `v_writelane_b32` - reads of any other lane - every lane read in a console wave64 that runs as two host waves of 32, since the read can cross to the other host wave **Empty mask:** if the mask is empty, `s_ff1` gives -1 and no lane is read. The idioms only use the read value in that mask's lanes, so nothing reads it. ### Results Translating every program from one game (26,084 programs): | | Before | After | |---|---|---| | Programs with wave-wide branches | 930 | 51 | | Programs that fail to translate | 195 (geometry shaders, unrelated) | 195 (same) | | Top compute program | 256 VGPRs, 4 waves/SIMD | 112 VGPRs, no spills, 9 waves/SIMD, passes DXIL validation | GPU busy time per frame, measured on a scripted, repeatable run of the game's intro sequence, went from **41.4 ms to 39.3 ms (about 5% less)**. With wave-wide branching turned off entirely, as an upper bound that is not a valid fix, it was 39.1 ms. So this change recovers nearly all of what wave-wide branching costs in this game. **Tested hardware:** only one GPU, an AMD Radeon RX 6800 XT (Navi 21, RDNA2). It ran on Linux under Wine with vkd3d-proton and the repo's patched RADV. The top program runs as wave32 there. I haven't measured on RDNA3, NVIDIA or Windows drivers. The register savings should apply anywhere, but the frame-time gain on other GPUs is untested. ### Tests New tests in `wave64_tests.rs`: - `a_lane_read_of_the_first_running_lane_keeps_its_branch_per_lane`. It fails without the change. - `a_lane_read_of_any_other_lane_takes_its_branch_wave_wide`. This covers both an arbitrary lane and a mask that is not ANDed with EXEC. On the same RX 6800 XT, `victory-shader` gives 421 passed, 1 failed, 4 ignored. The one failure, `valu_tests::legacy_multiply_adds_fuse_their_product_and_flush_their_sources` (hardware case 0), also fails without this change. That program has no lane reads. The changed code passes rustfmt with the repo's `rustfmt.toml` and produces no clippy warnings. `cargo xtask policy` passes. ### Not covered The timing benchmark covers the intro sequence only, not gameplay. The translation counts above cover every program the game has. Scenes that use different compute programs may gain more or less. I compared the game's output with and without the change by eye only, and saw no visual differences. I did not run a pixel-by-pixel comparison. The translation cache records the translator source hash, so translations cached before this change are redone automatically. 🤖 Generated with [Claude Code](https://claude.com/claude-code) EDIT: All testing was done with the 'high' preset with ray tracing disabled and render resolution manually set to 1.0 (1440p).
A v_readlane_b32 inside a per-lane branch took that branch wave-wide, so
every write in it became a guarded select of old and new values. On the
host that holds many more values live at once: the heaviest compute
programs went from about 112 VGPRs to 256, with a matching loss of
occupancy.

Where the console wave is the host wave (not two host waves of 32 for one
console wave64), a lane read only needs the wave-wide branch when the lane
it reads may not run it. Two cases read a running lane and now keep their
branch per lane:

- reads inside a whole-wave section, or of a value a section left, which
  the section simulation already gives as the console does;
- reads of the first lane of a mask ANDed with the EXEC they run under
  (s_and m, x, exec; s_ff1 l, m), the compiler's idiom for taking one
  value of the running lanes. WaveReadLaneAt of that lane is the
  console's value.

Writelanes and lane reads of any other lane still take their branch
wave-wide, as do all lane reads in a console wave64 split over two host
waves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 01:10:10 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 02:20 UTC · original

Checked 45e5681 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).

cargo test -p victory-shader --lib gives 421 passed, 1 failed, 4 ignored on the PR and 419/1/4 on main. The one failure is size_tests::a_reused_descriptor_register_read_only_behind_its_flag on both, which already fails here on main. Clippy on victory-shader, with and without diagnostics, warns at the same places as main (the existing ones shifted by the 74 added lines). Nothing in the new code.

The two new tests only call structure and lane_op_splits. They don't lower, validate or dispatch anything, so I wrote a throwaway test to run the case on the card. It's a console wave32 program where the threads with t < s0 && (t & s2) == 0 enter s_and_saveexec, then do s_and s13, s1, exec / s_ff1 s14, s13 / v_readlane s15, v1, s14 with v1 = 16t+7, and store the value. Each program is lowered twice, once per lane (what the PR picks) and once with the branch wave-wide (what main picks, branches_around on the readlane block). Both forms pass dxil.dll validation and create a PSO with the debug layer on. I ran 120 inputs: s0 in {32, 31, 16, 1, 0}, s2 in {0, 1, 2, 0x1E} (all, even lanes, pairs, lanes 0-1 only), and six s1 masks including 0x80000000, 2 and 0. Each result was compared with a CPU model of the console.

  • Wide form gives 120/120 equal to the console, so the harness and the model agree.
  • Per-lane form, first lane of (s1 & exec) gives 66/66 equal whenever the mask isn't empty. That includes a single active lane, alternating lanes, and masks whose first bit is past the first active lane. So WaveReadLaneAt of that lane inside the divergent branch gives the console's value on NVIDIA too. The lowering emits plain waveReadLaneAt(v, lane & 31) (lower.rs:14502-14508, 7635).
  • With an empty mask the per-lane form gives 0 where the console reads lane 31 (s_ff1 gives -1, masked to 31). That's the case the description says nothing reads. True for the WaveActiveAllEqual / atomic idioms, but reads_running_lane doesn't check how the read value is used, so a program that stores it would now differ from main. Probably fine, but worth one line in the doc comment.
  • Control, readlane of a fixed lane 5 forced per lane gives 60/120 wrong (0 whenever lane 5 is inactive). So NVIDIA really does return junk for an inactive lane, and the PR is right to keep that case wide (lane_op_splits returns the branch there).
  • Case (a): a wave32 s_or_saveexec -1 prefix-sum section inside the same branch, then v_readlane s15, v1, 31 after it. lane_op_splits now returns nothing, and the per-lane form gives 120/120 including the empty-exec case. It goes through shape() (lower.rs:13297-13308), never a lane read. The inside-a-section check start < i < end matches what section_inst covers (lower.rs:3565-3567).

On the gate. The code doesn't compare console and host wave. It only excludes p.wave64 && host_max_wave() == 32 (lower.rs:906). So on WARP (max 4) and with an unknown host the new path is taken too. It doesn't matter in practice, because any program with a lane read gets WaveSize(32) (lower.rs:2514-2527, 2648-2650), and WARP refuses that PSO for both forms ("wants wave lane count of 32 but maximum wave lane count for device is 4"). So nothing changes on WARP. On NVIDIA a console wave32 program takes the new path and a wave64 one keeps the halves path unchanged.

Pattern match for case (b) looks tight. EXEC and the mask are compared as SSA values, so an EXEC write or a redefinition of the mask between s_and and s_ff1 breaks the match (lower.rs:993-1002). The negative test confirms that a SALU AND without exec doesn't count. Two small things.

  • For s_ff1_i32_b64 only the low dword's writer is checked (ff1.read(Reg::S(m)) and and.writes.first(), lower.rs:993-1001). An s_and_b32 on the low half, or a write to m+1 in between, would still pass. That only matters for wave64 programs off a 32-lane host, so not on NVIDIA, but checking m+1 as well would be cheap.
  • sections_with_consts now runs for every block of every compute program (lower.rs:914-918), including blocks with no lane op at all. It could be skipped unless the block has a v_readlane_b32.

Not covered here: a lane read inside a per-lane loop (where NVIDIA reconvergence differs most from AMD), real game programs, and any frame timing.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 02:20 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/542#issuecomment-7430150753) Checked 45e5681 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game). `cargo test -p victory-shader --lib` gives 421 passed, 1 failed, 4 ignored on the PR and 419/1/4 on main. The one failure is `size_tests::a_reused_descriptor_register_read_only_behind_its_flag` on both, which already fails here on main. Clippy on victory-shader, with and without `diagnostics`, warns at the same places as main (the existing ones shifted by the 74 added lines). Nothing in the new code. The two new tests only call `structure` and `lane_op_splits`. They don't lower, validate or dispatch anything, so I wrote a throwaway test to run the case on the card. It's a console wave32 program where the threads with `t < s0 && (t & s2) == 0` enter `s_and_saveexec`, then do `s_and s13, s1, exec` / `s_ff1 s14, s13` / `v_readlane s15, v1, s14` with v1 = 16t+7, and store the value. Each program is lowered twice, once per lane (what the PR picks) and once with the branch wave-wide (what main picks, `branches_around` on the readlane block). Both forms pass dxil.dll validation and create a PSO with the debug layer on. I ran 120 inputs: s0 in {32, 31, 16, 1, 0}, s2 in {0, 1, 2, 0x1E} (all, even lanes, pairs, lanes 0-1 only), and six s1 masks including 0x80000000, 2 and 0. Each result was compared with a CPU model of the console. - Wide form gives 120/120 equal to the console, so the harness and the model agree. - Per-lane form, first lane of (s1 & exec) gives 66/66 equal whenever the mask isn't empty. That includes a single active lane, alternating lanes, and masks whose first bit is past the first active lane. So `WaveReadLaneAt` of that lane inside the divergent branch gives the console's value on NVIDIA too. The lowering emits plain `waveReadLaneAt(v, lane & 31)` (lower.rs:14502-14508, 7635). - With an empty mask the per-lane form gives 0 where the console reads lane 31 (`s_ff1` gives -1, masked to 31). That's the case the description says nothing reads. True for the `WaveActiveAllEqual` / atomic idioms, but `reads_running_lane` doesn't check how the read value is used, so a program that stores it would now differ from main. Probably fine, but worth one line in the doc comment. - Control, readlane of a fixed lane 5 forced per lane gives 60/120 wrong (0 whenever lane 5 is inactive). So NVIDIA really does return junk for an inactive lane, and the PR is right to keep that case wide (`lane_op_splits` returns the branch there). - Case (a): a wave32 `s_or_saveexec -1` prefix-sum section inside the same branch, then `v_readlane s15, v1, 31` after it. `lane_op_splits` now returns nothing, and the per-lane form gives 120/120 including the empty-exec case. It goes through `shape()` (lower.rs:13297-13308), never a lane read. The inside-a-section check `start < i < end` matches what `section_inst` covers (lower.rs:3565-3567). On the gate. The code doesn't compare console and host wave. It only excludes `p.wave64 && host_max_wave() == 32` (lower.rs:906). So on WARP (max 4) and with an unknown host the new path is taken too. It doesn't matter in practice, because any program with a lane read gets `WaveSize(32)` (lower.rs:2514-2527, 2648-2650), and WARP refuses that PSO for both forms ("wants wave lane count of 32 but maximum wave lane count for device is 4"). So nothing changes on WARP. On NVIDIA a console wave32 program takes the new path and a wave64 one keeps the halves path unchanged. Pattern match for case (b) looks tight. EXEC and the mask are compared as SSA values, so an EXEC write or a redefinition of the mask between `s_and` and `s_ff1` breaks the match (lower.rs:993-1002). The negative test confirms that a SALU AND without exec doesn't count. Two small things. - For `s_ff1_i32_b64` only the low dword's writer is checked (`ff1.read(Reg::S(m))` and `and.writes.first()`, lower.rs:993-1001). An `s_and_b32` on the low half, or a write to m+1 in between, would still pass. That only matters for wave64 programs off a 32-lane host, so not on NVIDIA, but checking m+1 as well would be cheap. - `sections_with_consts` now runs for every block of every compute program (lower.rs:914-918), including blocks with no lane op at all. It could be skipped unless the block has a `v_readlane_b32`. Not covered here: a lane read inside a per-lane loop (where NVIDIA reconvergence differs most from AMD), real game programs, and any frame timing. AI-assisted: yes
Author
Owner

@⁠fsrajner on GitHub · 2026-12-05 18:06 UTC · original

A/B data point on RDNA 4. In this scene the PR gives a clear, consistent win.

Setup: RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, repo-patched Mesa 26.3.1 RADV + vkd3d-proton, proton-cachyos-slr. Series X profile, 4K output with FSR 4.1.2 Quality, RT on (HW_RT + LEAN_BUILDER + REUSE from the rt-7-reuse stack), frame generation off, headless gamescope. Base: main @ 8bf4878 + rt-7-reuse. PR arm: the same base with this PR (45e5681) merged in (no conflicts).

Method: same intro snapshot every run, 120 s after the first A press. One warm-up run per arm first, then the arms interleaved (base, PR, base, PR). Metrics come from the 5 s stats: frame windows.

Run ms/frame windows > 40 ms worst window gpu_wait long frame lines / longest
base (warm-up) 44 15/18 57.8 ms 18.7 ms 366 / 390 ms
PR (warm-up) 40 11/18 51.8 ms 16.4 ms 275 / 148 ms
base 1 43 15/18 53.7 ms 20.3 ms 349 / 161 ms
PR 1 40 10/18 47.2 ms 18.4 ms 265 / 162 ms
base 2 45 16/18 69.4 ms 18.7 ms 353 / 151 ms
PR 2 41 11/18 59.0 ms 16.8 ms 257 / 152 ms
  • The PR wins in all three pairs: ~44 → ~40 ms/frame, slow windows 85 % → 59 %, long frame lines −25 %, mean worst window 60.3 → 52.7 ms.
  • Why it helps here: with per-dispatch GPU timestamps, the most expensive non-RT compute shader in this scene, 2561af95, cost about 2.3 ms/frame on main, against 0.12 ms for the older translator's version. Its DXIL on main had a large wave-wide predicated region: about 780 selects on one condition, plus waveAllTrue/ballot. That pushes VGPR use way up and makes inactive lanes do the loads too. With this PR the module drops back to roughly the old structure: select 2874 → 1486, phi 778 → 509.
  • No device loss, errors or visual problems in these runs.
**@⁠fsrajner** on GitHub · 2026-12-05 18:06 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/542#issuecomment-7442826004) A/B data point on RDNA 4. In this scene the PR gives a clear, consistent win. **Setup:** RX 9070 XT 16 GB, Ryzen 7 7800X3D, CachyOS, repo-patched Mesa 26.3.1 RADV + vkd3d-proton, proton-cachyos-slr. Series X profile, 4K output with FSR 4.1.2 Quality, RT on (`HW_RT` + `LEAN_BUILDER` + `REUSE` from the `rt-7-reuse` stack), frame generation off, headless gamescope. Base: `main` @ `8bf4878` + `rt-7-reuse`. PR arm: the same base with this PR (`45e5681`) merged in (no conflicts). **Method:** same intro snapshot every run, 120 s after the first A press. One warm-up run per arm first, then the arms interleaved (base, PR, base, PR). Metrics come from the 5 s `stats: frame` windows. | Run | ms/frame | windows > 40 ms | worst window | gpu_wait | `long frame` lines / longest | |---|---|---|---|---|---| | base (warm-up) | 44 | 15/18 | 57.8 ms | 18.7 ms | 366 / 390 ms | | PR (warm-up) | 40 | 11/18 | 51.8 ms | 16.4 ms | 275 / 148 ms | | base 1 | 43 | 15/18 | 53.7 ms | 20.3 ms | 349 / 161 ms | | PR 1 | 40 | 10/18 | 47.2 ms | 18.4 ms | 265 / 162 ms | | base 2 | 45 | 16/18 | 69.4 ms | 18.7 ms | 353 / 151 ms | | PR 2 | 41 | 11/18 | 59.0 ms | 16.8 ms | 257 / 152 ms | - The PR wins in all three pairs: ~44 → ~40 ms/frame, slow windows 85 % → 59 %, `long frame` lines −25 %, mean worst window 60.3 → 52.7 ms. - Why it helps here: with per-dispatch GPU timestamps, the most expensive non-RT compute shader in this scene, `2561af95`, cost about 2.3 ms/frame on `main`, against 0.12 ms for the older translator's version. Its DXIL on `main` had a large wave-wide predicated region: about 780 `select`s on one condition, plus `waveAllTrue`/ballot. That pushes VGPR use way up and makes inactive lanes do the loads too. With this PR the module drops back to roughly the old structure: `select` 2874 → 1486, `phi` 778 → 509. - No device loss, errors or visual problems in these runs.
Author
Owner

@⁠fsrajner on GitHub · 2026-12-05 18:19 UTC · original

Follow-up with per-dispatch GPU timestamps (diagnostics build, same scene, HW_RT on, frame generation off). Timestamps serialize the GPU, so absolute times are inflated; compare the before/after columns.

shader before with this PR
2561af95 2.36 ms/frame 0.18 ms
5cf00dc3 2.10 ms 0.83 ms
0a5ecfee 0.79 ms 0.26 ms
6697dff1 0.46 ms 0.27 ms
all non-RT dispatches 49.9 ms 44.4 ms

Call counts are unchanged. The other hot shaders are unchanged, so the gain comes from these few modules that had the wave-wide predicated regions.

**@⁠fsrajner** on GitHub · 2026-12-05 18:19 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/542#issuecomment-7443024990) Follow-up with per-dispatch GPU timestamps (diagnostics build, same scene, `HW_RT` on, frame generation off). Timestamps serialize the GPU, so absolute times are inflated; compare the before/after columns. | shader | before | with this PR | |---|---|---| | `2561af95` | 2.36 ms/frame | **0.18 ms** | | `5cf00dc3` | 2.10 ms | **0.83 ms** | | `0a5ecfee` | 0.79 ms | 0.26 ms | | `6697dff1` | 0.46 ms | 0.27 ms | | all non-RT dispatches | 49.9 ms | 44.4 ms | Call counts are unchanged. The other hot shaders are unchanged, so the gain comes from these few modules that had the wave-wide predicated regions.
With an empty mask s_ff1 gives -1 and the console's v_readlane takes the
wave's last lane; the lane form gives 0. The compiler's idioms only use
what is read in the lanes of the mask, but nothing checks that, so say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
For s_ff1_i32_b64 only the mask's low dword was checked to come from the
AND with EXEC, and only EXEC_LO to match the EXEC the read runs under. A
write to the high dword between the AND and s_ff1, or to EXEC_HI after
the AND, could then name a lane that does not run the read. Check that
the high dword comes from the same AND and that EXEC_HI matches too.

This matters for console wave64 programs on hosts that run them as one
wave of 64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sections_with_consts ran for every block of every compute program. Only
blocks with a v_readlane_b32 or v_writelane_b32 need it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠ManGuyNY on GitHub · 2026-12-05 21:05 UTC · original

Checked 45e5681 on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).

cargo test -p victory-shader --lib gives 421 passed, 1 failed, 4 ignored on the PR and 419/1/4 on main. The one failure is size_tests::a_reused_descriptor_register_read_only_behind_its_flag on both, which already fails here on main. Clippy on victory-shader, with and without diagnostics, warns at the same places as main (the existing ones shifted by the 74 added lines). Nothing in the new code.

The two new tests only call structure and lane_op_splits. They don't lower, validate or dispatch anything, so I wrote a throwaway test to run the case on the card. It's a console wave32 program where the threads with t < s0 && (t & s2) == 0 enter s_and_saveexec, then do s_and s13, s1, exec / s_ff1 s14, s13 / v_readlane s15, v1, s14 with v1 = 16t+7, and store the value. Each program is lowered twice, once per lane (what the PR picks) and once with the branch wave-wide (what main picks, branches_around on the readlane block). Both forms pass dxil.dll validation and create a PSO with the debug layer on. I ran 120 inputs: s0 in {32, 31, 16, 1, 0}, s2 in {0, 1, 2, 0x1E} (all, even lanes, pairs, lanes 0-1 only), and six s1 masks including 0x80000000, 2 and 0. Each result was compared with a CPU model of the console.

* Wide form gives 120/120 equal to the console, so the harness and the model agree.

* Per-lane form, first lane of (s1 & exec) gives 66/66 equal whenever the mask isn't empty. That includes a single active lane, alternating lanes, and masks whose first bit is past the first active lane. So `WaveReadLaneAt` of that lane inside the divergent branch gives the console's value on NVIDIA too. The lowering emits plain `waveReadLaneAt(v, lane & 31)` (lower.rs:14502-14508, 7635).

* With an empty mask the per-lane form gives 0 where the console reads lane 31 (`s_ff1` gives -1, masked to 31). That's the case the description says nothing reads. True for the `WaveActiveAllEqual` / atomic idioms, but `reads_running_lane` doesn't check how the read value is used, so a program that stores it would now differ from main. Probably fine, but worth one line in the doc comment.

* Control, readlane of a fixed lane 5 forced per lane gives 60/120 wrong (0 whenever lane 5 is inactive). So NVIDIA really does return junk for an inactive lane, and the PR is right to keep that case wide (`lane_op_splits` returns the branch there).

* Case (a): a wave32 `s_or_saveexec -1` prefix-sum section inside the same branch, then `v_readlane s15, v1, 31` after it. `lane_op_splits` now returns nothing, and the per-lane form gives 120/120 including the empty-exec case. It goes through `shape()` (lower.rs:13297-13308), never a lane read. The inside-a-section check `start < i < end` matches what `section_inst` covers (lower.rs:3565-3567).

On the gate. The code doesn't compare console and host wave. It only excludes p.wave64 && host_max_wave() == 32 (lower.rs:906). So on WARP (max 4) and with an unknown host the new path is taken too. It doesn't matter in practice, because any program with a lane read gets WaveSize(32) (lower.rs:2514-2527, 2648-2650), and WARP refuses that PSO for both forms ("wants wave lane count of 32 but maximum wave lane count for device is 4"). So nothing changes on WARP. On NVIDIA a console wave32 program takes the new path and a wave64 one keeps the halves path unchanged.

Pattern match for case (b) looks tight. EXEC and the mask are compared as SSA values, so an EXEC write or a redefinition of the mask between s_and and s_ff1 breaks the match (lower.rs:993-1002). The negative test confirms that a SALU AND without exec doesn't count. Two small things.

* For `s_ff1_i32_b64` only the low dword's writer is checked (`ff1.read(Reg::S(m))` and `and.writes.first()`, lower.rs:993-1001). An `s_and_b32` on the low half, or a write to m+1 in between, would still pass. That only matters for wave64 programs off a 32-lane host, so not on NVIDIA, but checking m+1 as well would be cheap.

* `sections_with_consts` now runs for every block of every compute program (lower.rs:914-918), including blocks with no lane op at all. It could be skipped unless the block has a `v_readlane_b32`.

Not covered here: a lane read inside a per-lane loop (where NVIDIA reconvergence differs most from AMD), real game programs, and any frame timing.

AI-assisted: yes

Thanks for the thorough check, I've pushed three follow-ups for your points:

  • ee759f3: the doc comment on reads_running_lane now says that how the read value is used isn't checked. With an empty mask the lane form gives 0 where the console reads the wave's last lane (lane 31 in your wave32 harness, 63 for wave64).
  • d35b6e2: for s_ff1_i32_b64, the mask's high dword must now come from the same AND, and the AND's EXEC_HI must match the EXEC the read runs under. Hosts that run a console wave64 as one 64-lane wave take this path (my RX 6800 XT does), so I'd rather not rely on the compiler's ordering. Two new wave64 tests: the normal pattern keeps its branch per lane, and overwriting the mask's high half between the AND and s_ff1, or EXEC_HI after the AND, takes the branch wave-wide. With either half of the check disabled, the test fails.
  • 4d83087: lane_op_splits now skips blocks with no v_readlane_b32 or v_writelane_b32 before calling sections_with_consts.

Tests: victory-shader on Linux, 294 passed. On the Windows target under Wine, 423 passed and 1 failed (valu_tests::legacy_multiply_adds_fuse_their_product_and_flush_their_sources), which fails the same way at 45e5681, so it isn't from these changes.

Agreed on what's still uncovered: a lane read inside a per-lane loop, and output correctness on real game programs. My 41.4 → 39.3 ms GPU measurement only speaks to timing.

🤖 Generated with Claude Code

**@⁠ManGuyNY** on GitHub · 2026-12-05 21:05 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/542#issuecomment-7445360493) > Checked [45e5681](https://github.com/lobomfz/VIctory-xmem/commit/45e5681beb5cec728749ad8997abde7e39067db2) on native Windows (one NVIDIA desktop card plus WARP, didn't run the game). > > `cargo test -p victory-shader --lib` gives 421 passed, 1 failed, 4 ignored on the PR and 419/1/4 on main. The one failure is `size_tests::a_reused_descriptor_register_read_only_behind_its_flag` on both, which already fails here on main. Clippy on victory-shader, with and without `diagnostics`, warns at the same places as main (the existing ones shifted by the 74 added lines). Nothing in the new code. > > The two new tests only call `structure` and `lane_op_splits`. They don't lower, validate or dispatch anything, so I wrote a throwaway test to run the case on the card. It's a console wave32 program where the threads with `t < s0 && (t & s2) == 0` enter `s_and_saveexec`, then do `s_and s13, s1, exec` / `s_ff1 s14, s13` / `v_readlane s15, v1, s14` with v1 = 16t+7, and store the value. Each program is lowered twice, once per lane (what the PR picks) and once with the branch wave-wide (what main picks, `branches_around` on the readlane block). Both forms pass dxil.dll validation and create a PSO with the debug layer on. I ran 120 inputs: s0 in {32, 31, 16, 1, 0}, s2 in {0, 1, 2, 0x1E} (all, even lanes, pairs, lanes 0-1 only), and six s1 masks including 0x80000000, 2 and 0. Each result was compared with a CPU model of the console. > > * Wide form gives 120/120 equal to the console, so the harness and the model agree. > > * Per-lane form, first lane of (s1 & exec) gives 66/66 equal whenever the mask isn't empty. That includes a single active lane, alternating lanes, and masks whose first bit is past the first active lane. So `WaveReadLaneAt` of that lane inside the divergent branch gives the console's value on NVIDIA too. The lowering emits plain `waveReadLaneAt(v, lane & 31)` (lower.rs:14502-14508, 7635). > > * With an empty mask the per-lane form gives 0 where the console reads lane 31 (`s_ff1` gives -1, masked to 31). That's the case the description says nothing reads. True for the `WaveActiveAllEqual` / atomic idioms, but `reads_running_lane` doesn't check how the read value is used, so a program that stores it would now differ from main. Probably fine, but worth one line in the doc comment. > > * Control, readlane of a fixed lane 5 forced per lane gives 60/120 wrong (0 whenever lane 5 is inactive). So NVIDIA really does return junk for an inactive lane, and the PR is right to keep that case wide (`lane_op_splits` returns the branch there). > > * Case (a): a wave32 `s_or_saveexec -1` prefix-sum section inside the same branch, then `v_readlane s15, v1, 31` after it. `lane_op_splits` now returns nothing, and the per-lane form gives 120/120 including the empty-exec case. It goes through `shape()` (lower.rs:13297-13308), never a lane read. The inside-a-section check `start < i < end` matches what `section_inst` covers (lower.rs:3565-3567). > > > On the gate. The code doesn't compare console and host wave. It only excludes `p.wave64 && host_max_wave() == 32` (lower.rs:906). So on WARP (max 4) and with an unknown host the new path is taken too. It doesn't matter in practice, because any program with a lane read gets `WaveSize(32)` (lower.rs:2514-2527, 2648-2650), and WARP refuses that PSO for both forms ("wants wave lane count of 32 but maximum wave lane count for device is 4"). So nothing changes on WARP. On NVIDIA a console wave32 program takes the new path and a wave64 one keeps the halves path unchanged. > > Pattern match for case (b) looks tight. EXEC and the mask are compared as SSA values, so an EXEC write or a redefinition of the mask between `s_and` and `s_ff1` breaks the match (lower.rs:993-1002). The negative test confirms that a SALU AND without exec doesn't count. Two small things. > > * For `s_ff1_i32_b64` only the low dword's writer is checked (`ff1.read(Reg::S(m))` and `and.writes.first()`, lower.rs:993-1001). An `s_and_b32` on the low half, or a write to m+1 in between, would still pass. That only matters for wave64 programs off a 32-lane host, so not on NVIDIA, but checking m+1 as well would be cheap. > > * `sections_with_consts` now runs for every block of every compute program (lower.rs:914-918), including blocks with no lane op at all. It could be skipped unless the block has a `v_readlane_b32`. > > > Not covered here: a lane read inside a per-lane loop (where NVIDIA reconvergence differs most from AMD), real game programs, and any frame timing. > > AI-assisted: yes Thanks for the thorough check, I've pushed three follow-ups for your points: - ee759f3: the doc comment on `reads_running_lane` now says that how the read value is used isn't checked. With an empty mask the lane form gives 0 where the console reads the wave's last lane (lane 31 in your wave32 harness, 63 for wave64). - d35b6e2: for `s_ff1_i32_b64`, the mask's high dword must now come from the same AND, and the AND's `EXEC_HI` must match the EXEC the read runs under. Hosts that run a console wave64 as one 64-lane wave take this path (my RX 6800 XT does), so I'd rather not rely on the compiler's ordering. Two new wave64 tests: the normal pattern keeps its branch per lane, and overwriting the mask's high half between the AND and `s_ff1`, or `EXEC_HI` after the AND, takes the branch wave-wide. With either half of the check disabled, the test fails. - 4d83087: `lane_op_splits` now skips blocks with no `v_readlane_b32` or `v_writelane_b32` before calling `sections_with_consts`. Tests: victory-shader on Linux, 294 passed. On the Windows target under Wine, 423 passed and 1 failed (`valu_tests::legacy_multiply_adds_fuse_their_product_and_flush_their_sources`), which fails the same way at 45e5681, so it isn't from these changes. Agreed on what's still uncovered: a lane read inside a per-lane loop, and output correctness on real game programs. My 41.4 → 39.3 ms GPU measurement only speaks to timing. 🤖 Generated with Claude Code
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/542/head:github-bot/gh-542'
git switch 'github-bot/gh-542'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-542'
git switch 'github-bot/gh-542'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-542'
git switch 'github-bot/gh-542'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-542'
git switch 'main'
git merge --squash 'github-bot/gh-542'
git switch 'main'
git merge --ff-only 'github-bot/gh-542'
git switch 'main'
git merge 'github-bot/gh-542'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.