shader: keep branches per lane around reads of running lanes #542
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!542
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-542"
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?
@ManGuyNY on GitHub · 2026-12-05 01:05 UTC · original
ManGuyNY:perf/lane-reads-per-lane→main· +195 −2 · 2 files · 5 commits · openshader: keep branches per lane around reads of running lanes
Problem
lane_op_splitstook every per-lane branch that contains av_readlane_b32wave-wide (structure_wide). Inside a wave-wide branch, each write becomes a guardedselect(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:
Section::reads), already gets the console's value from the section simulation.s_and m, x, execthens_ff1 l, m, in the same block. The compiler uses this to take one value of the running lanes, forWaveActiveAllEqualor an atomic's returned value.WaveReadLaneAtof that lane gives the console's value.These cases still take their branch wave-wide:
v_writelane_b32Empty mask: if the mask is empty,
s_ff1gives -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):
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-shadergives 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.tomland produces no clippy warnings.cargo xtask policypasses.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).
@Kal0s-design on GitHub · 2026-12-05 02:20 UTC · original
Checked
45e5681on native Windows (one NVIDIA desktop card plus WARP, didn't run the game).cargo test -p victory-shader --libgives 421 passed, 1 failed, 4 ignored on the PR and 419/1/4 on main. The one failure issize_tests::a_reused_descriptor_register_read_only_behind_its_flagon both, which already fails here on main. Clippy on victory-shader, with and withoutdiagnostics, 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
structureandlane_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 witht < s0 && (t & s2) == 0enters_and_saveexec, then dos_and s13, s1, exec/s_ff1 s14, s13/v_readlane s15, v1, s14with 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_aroundon 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.WaveReadLaneAtof that lane inside the divergent branch gives the console's value on NVIDIA too. The lowering emits plainwaveReadLaneAt(v, lane & 31)(lower.rs:14502-14508, 7635).s_ff1gives -1, masked to 31). That's the case the description says nothing reads. True for theWaveActiveAllEqual/ atomic idioms, butreads_running_lanedoesn'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.lane_op_splitsreturns the branch there).s_or_saveexec -1prefix-sum section inside the same branch, thenv_readlane s15, v1, 31after it.lane_op_splitsnow returns nothing, and the per-lane form gives 120/120 including the empty-exec case. It goes throughshape()(lower.rs:13297-13308), never a lane read. The inside-a-section checkstart < i < endmatches whatsection_instcovers (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 getsWaveSize(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_andands_ff1breaks the match (lower.rs:993-1002). The negative test confirms that a SALU AND without exec doesn't count. Two small things.s_ff1_i32_b64only the low dword's writer is checked (ff1.read(Reg::S(m))andand.writes.first(), lower.rs:993-1001). Ans_and_b32on 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_constsnow 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 av_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
@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+REUSEfrom thert-7-reusestack), 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: framewindows.long framelines / longestlong framelines −25 %, mean worst window 60.3 → 52.7 ms.2561af95, cost about 2.3 ms/frame onmain, against 0.12 ms for the older translator's version. Its DXIL onmainhad a large wave-wide predicated region: about 780selects on one condition, pluswaveAllTrue/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:select2874 → 1486,phi778 → 509.@fsrajner on GitHub · 2026-12-05 18:19 UTC · original
Follow-up with per-dispatch GPU timestamps (diagnostics build, same scene,
HW_RTon, frame generation off). Timestamps serialize the GPU, so absolute times are inflated; compare the before/after columns.2561af955cf00dc30a5ecfee6697dff1Call counts are unchanged. The other hot shaders are unchanged, so the gain comes from these few modules that had the wave-wide predicated regions.
@ManGuyNY on GitHub · 2026-12-05 21:05 UTC · original
Thanks for the thorough check, I've pushed three follow-ups for your points:
ee759f3: the doc comment onreads_running_lanenow 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: fors_ff1_i32_b64, the mask's high dword must now come from the same AND, and the AND'sEXEC_HImust 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 ands_ff1, orEXEC_HIafter the AND, takes the branch wave-wide. With either half of the check disabled, the test fails.4d83087:lane_op_splitsnow skips blocks with nov_readlane_b32orv_writelane_b32before callingsections_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 at45e5681, 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
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.