Skip to content

Recompiler: a wave64 compute program folds into one 32-lane wave - #3562

Open
Royalflamejlh wants to merge 6 commits into
xcl-dev:mainfrom
Royalflamejlh:recompiler/wave64-fold
Open

Royalflamejlh wants to merge 6 commits into
xcl-dev:mainfrom
Royalflamejlh:recompiler/wave64-fold

Conversation

@Royalflamejlh

@Royalflamejlh Royalflamejlh commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

On a GPU with 32-lane waves only, a wave64 compute program now runs folded where it can: each host lane carries two console lanes (t and t + 32), so the whole console wave is one host wave and nothing is exchanged between halves. The split form's exchange is what #3114 measures at 2.6x a program's GPU time, and its spin wait ended runs on Linux here in a GPU hang (NVIDIA Xid 109) at the title's world start.

The form is ported from VIctory-NVIDIA (crates/victory-shader/src/fold.rs at 5fabb46e, GPL-3.0-or-later, https://github.com/trustno1-coder/VIctory-NVIDIA), as a rewrite of the console code into a wave32 program before translation. The provenance note is in the file and THIRD_PARTY.md has the entry.

Folding is opt-in: XCL_WAVE64_FOLD=1 turns it on, and without it nothing changes. Its GPU time against the split form is not measured here; #3543, a port of the same idea, measured a median 1.27 times the split form's time on an RTX 4080 with a run-to-run spread as large as the difference.

What is in it

  • crates/xcl/src/gpu/dxil/fold.rs (new, 1,157 lines): the rewrite. VGPR v of console lane t + 32 is VGPR v + N of host lane t; a run of vector instructions is written twice, under each half of EXEC; a mask is the two copies' 32-bit ballots; v_readlane, v_writelane, v_readfirstlane and v_mbcnt name the copy that holds the lane. Scalar code is unchanged, since XCL keeps the console's scalar branches.
  • crates/xcl/src/gpu/dxil/fold_tests.rs (new, 713 lines): the tests, with a small RDNA2 interpreter that runs a console program in 64-lane waves and its folded code in 32-lane waves and compares.
  • gpu/dxil/kernel.rs (translate_compute_rodata): the folded form is tried for a split wave64 compute program; any refusal logs one line and the split form runs. It is tried only with XCL_WAVE64_FOLD=1, which is part of the translated cache's identity.
  • It refuses: more than 127 VGPRs, ds_bpermute / ds_permute, ds_append / ds_consume, GDS, scratch, s_movrel* / v_movrel*, calls and indirect jumps, EXEC or VCC as a memory operand, fewer than seven spare SGPRs, and a few rarer forms.
  • gpu/dxil/kernel.rs, compact.rs: a folded program whose compact plan is refused only for its table's size is not folded. On that other tree (compact table limit 12,288 words) two programs of about 122,000 instructions lost their compact form by folding and became 4.3 MB of DXIL each, which NVIDIA's compiler did not finish in ten minutes; with main's 32,768 words both still fit, so on main this changes no program of that pack.
  • fold.rs: a program folds only when its VGPRs and their twins fit 255 registers (2N <= 255); the rewrite uses no VGPR beyond the twins.
  • docs/wave64-on-wave32.md: a section on the form.

Why it is one pull request. 2,016 changed lines in six commits, over the ~800 the contributing guide asks a reason for. Of them, 1,158 are fold.rs and 741 its tests; the rest (117 lines) are the wiring behind XCL_WAVE64_FOLD=1, the compact-limit check, the warm setting, the docs and the provenance entry.

  • fold.rs is one rewrite pass over the instruction set: every instruction it accepts is rewritten in the same loop, against the same register plan and the same EXEC handling, so a part of it (say, the lane primitives without the driver) would be code nothing calls and nothing can test on its own: the tests run whole console programs through the interpreter and the folded form.
  • Splitting it as preparation / fold.rs not yet called / turning it on / the census would give three PRs whose diffs are each under 150 lines and one of ~1,900 that is this one: the review of fold.rs would be the same review.
  • It is a port: the provenance note names the source, and the closed Recompiler: half context and second VGPR bank for folded wave64 (no behaviour change) #3529 / Recompiler: folded wave64 lane primitives (thread hand-out, two-ballot masks, lane reads, v_mbcnt) #3530 / Recompiler: folded wave64 instruction driver and kernel entry (tests only) #3543 stack split the same port into context, lane primitives and driver and none of the parts could be merged without the others.
  • It changes nothing unless XCL_WAVE64_FOLD=1 is set: with the switch off the translation and the caches are as on main.
    The commits are kept apart so each part can be read on its own: the rewrite (ec2eff44a), its GPU tests and the pack count (aebe29efe), the docs (e1a954aab), the compact-limit check (74ba777fa), the register limit (e5558b14a) and the opt-in switch (913af192c).

Tested

  • RTX 4060 Ti 16 GB, NVIDIA 615.71.09, AMD Ryzen 9 9950X3D, Linux, Wine 11.21 Staging, vkd3d-proton master with local patches, x86_64-pc-windows-gnu, release. Head 913af192c (XCL_WAVE64_FOLD is also a warm pipeline library setting since; the fold and shaders:: tests there, under Wine on the CPU: 122 passed, 0 failed, 1 ignored. Earlier head 0c6bd4c0: the form made opt-in; the tests switch it on themselves).
  • cargo test -p xcl --release --lib gpu::dxil -- --test-threads=2: 315 passed, 1 failed, 4 ignored. SKIPPED: dispatch_chain_native_wave64_builds_whole (no wave64). The failure is round_tests::directed_reference_basics, which fails the same way on main.
  • On the GPU: gpu_folded_wave64_matches_the_console_wave (five programs: masks and lane reads; nested saveexec branches; LDS across a barrier; a loop with divergent exits; a carry chain with lane writes and v_cmpx; in groups of 64, 128, 8x8, 8x8x4, 40 and 16x6 threads, against a small RDNA2 interpreter of the console wave) and gpu_folded_wave64_keeps_the_thread_limits passed.
  • the_folded_form_is_taken_for_a_split_program_and_has_no_exchange: the folded DXIL validates and has no atomic, compare-exchange or group-shared storage.
  • A retail title, on another tree that carries these commits beside other unmerged pull requests: 1 run of 855 s with folding on, no crash, no device removal. That run had XCL_GPU_PROFILE=1, which makes a queue run its draws and dispatches one after the other; on a later head of that tree the title lost its device at the world start without the switch (2 of 2 runs, with folding on and with it off) and ran 600 s with it (1 of 1), so the run says nothing about this change with the GPU's work overlapping. In a cold launch pass of that tree, of the 1,151 wave64 compute programs it translated, 732 folded, 351 ran as lone halves (another unmerged change) and 68 stayed split. Translated offline, folded DXIL was 1.11 times the split form's at the median and 1.73 times at the 95th percentile.

Regression run
RTX 5070 Ti 16 GB (driver 621.24), Ryzen 7 9800X3D, Windows 11 Pro 26300, release builds by scripts/smoke.ps1, the same retail title for 60 s each.

main:

xcl-smoke v1
commit: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f
gpu: NVIDIA GeForce RTX 5070 Ti
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1620 (PresentX 1621)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15476 MB

this PR's head:

xcl-smoke v1
commit: 913af192c7c67e82bfd2daa5d8c50fc48f7e5173
gpu: NVIDIA GeForce RTX 5070 Ti
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1616 (PresentX 1616)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15858 MB

Not tested

  • Native Windows, AMD, any other NVIDIA card, the whole suite, a title from this branch.
  • The form assumes a hardware wave of a one-dimensional group holds 32 consecutive SV_GroupIndex values, as the existing wave32 path does.
  • SDWA and DPP copies, v_div_fmas, MIMG/NSA and buffer instructions are ported but no test program uses them. The reference interpreter is this PR's own.
  • GPU time against the split form: not measured. Folded code is about twice the instructions.

AI use

  • Claude Code. Claude Fable 5.2 ported the code and wrote the tests and this description. Claude Opus 5.5 found why two programs lost their compact form and wrote that change (6cec7afd) and the VGPR limit (bda4766f).

@github-actions github-actions Bot added area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 988b298, diff read, not a merge-check; draft with PR description red, so not built or run here):

Fine: the rewrites of v_readfirstlane / v_readlane / v_writelane / v_mbcnt, run ordering (stores and EXEC writes end a run, upper_first for scalar hazards), the s_getpc remap to the console address, the NSA remap, and the provenance note plus THIRD_PARTY entry. It covers more than the closed stack (memory, LDS, barriers, DPP).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

Thanks for the read. On the points:

  • VGPR limit: right in principle. The rewrite uses no VGPR beyond the twins (its scratch registers are SGPRs), so a host lane holds exactly 2N, and the old check let 2N = 256 through. It is now 2N <= 255 (bda4766fb), with a test at the boundary. In the pack tested no program has 120 to 128 VGPRs; the ones at 129 were already refused.
  • Default without a GPU-time measurement: true, it is not measured here. The description now says so and names Recompiler: folded wave64 instruction driver and kernel entry (tests only) #3543's figure. I can make it opt-in if that is what should merge.
  • New since your read (6cec7afd): a folded program whose compact plan is refused only for its table's size is not folded. On a tree with a 12,288-word table limit, two programs of about 122,000 instructions became 4.3 MB of DXIL each when folded and NVIDIA's compiler did not finish them in ten minutes. With main's 32,768 words both still fit.
  • A second reference in the GPU tests (the split form on the same GPU): not added yet.

gpu::dxil on the new head, RTX 4060 Ti under Wine: 315 passed, 1 failed (directed_reference_basics, as on main), 4 ignored.

AI use: Claude Code. Claude Opus 5.5 checked the limit against the code and wrote the two changes; Claude Fable 5.2 ran the tests on the card and wrote this reply.

@Royalflamejlh
Royalflamejlh marked this pull request as ready for review December 1, 2026 08:05
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Re-review (head bda4766, diff read, not a merge-check: PR description still fails (no xcl-smoke blocks), so not built or run here). Follow-up to my review of 988b298.

  • Still on by default without a GPU-time measurement against the split form, while Recompiler: folded wave64 instruction driver and kernel entry (tests only) #3543 measured the same idea at 1.27x; please make it opt-in until it's measured, as the description now offers.
  • VGPR limit: partly addressed. It's now 2N > 255 (fold.rs ~1112), and the commit argues the rewrite needs no extra VGPRs because its scratch registers are SGPRs (spare_sgprs), which matches the code. The description's "refuses: more than 128 VGPRs" is now stale (the limit is 127).
  • New (6cec7af): a folded program whose compact tables pass the limit now keeps the split compact form (kernel.rs ~454-456, compact::wanted_plan / fits), with a test; it refuses folding even when the split form's tables don't fit either.
  • Still open: about 2,000 lines now, tests that compare only with the PR's own interpreter, the smoke pair and a Windows suite run. The "owed before this leaves draft" wording and the missing Opus 5.5 in AI use need updating.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 0c6bd4c

No open PR makes the same change. #3543 (throny) was a port of the same fold and was closed by its author on 2026-11-30 because it measured no gain on an RTX 4080. #3561 (same author) is a different approach to the same exchange cost: a lane loop for small programs.
The provenance note and the THIRD_PARTY.md entry for VIctory-NVIDIA (GPL-3.0-or-later) are present. Since 0c6bd4c folding is opt-in (XCL_WAVE64_FOLD=1). It has not been run on Windows or on any GPU other than the RTX 4060 Ti, and its GPU time against the split form is not measured, so it should stay opt-in until that is settled.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

Folding is opt-in now (head 0c6bd4c0): XCL_WAVE64_FOLD=1 turns it on, and without it no program changes form. The description says so, and why: no GPU-time measurement against the split form here, and #3543's figure.

cargo test --lib gpu::dxil on that head, RTX 4060 Ti under Wine: 315 passed, 1 failed (directed_reference_basics, as on main), 4 ignored; both GPU tests of the form passed.

AI use: Claude Code with Claude Fable 5.2 made the change, ran the tests and wrote this.

Royalflamejlh and others added 6 commits December 1, 2026 13:31
On a GPU of 32-lane waves the direct translator ran a wave64 compute
program as two halves that exchange masks and lane reads through
group-shared counters with a spin wait. A program is now first rewritten
into a wave32 program in which each host lane runs console lanes t and
t + 32 (gpu/dxil/fold.rs, ported from VIctory-NVIDIA 5fabb46e,
GPL-3.0-or-later): VGPR v of the upper lane is VGPR v + N, a mask is two
32-bit ballots in its register pair, and the console's EXEC lives in a
spare SGPR pair. One host wave is one console wave, so nothing is
exchanged. The entry gives both lanes their thread ids and their EXEC
words inside the dispatch's thread limits.

What the rewrite does not cover (more than 128 VGPRs, ds_(b)permute,
GDS, loads into LDS, scratch, movrel, calls and indirect jumps, vccz /
execz / lds_direct operands) keeps the split form, and so does a folded
program the wave32 translation refuses. XCL_WAVE64_FOLD=0 turns the form
off; the translated cache's identity carries it.

Tests run the console's programs in waves of 64 lanes and their folded
code in waves of 32 on a small machine on the CPU, for ballots, lane
reads and writes, mbcnt, nested saveexec branches, a loop with divergent
exits, LDS across a barrier and a carry chain, in six group shapes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gpu_folded_wave64_matches_the_console_wave dispatches the folded DXIL of
the five test programs in six group shapes and compares each thread's
dwords with the console's waves run on the CPU;
gpu_folded_wave64_keeps_the_thread_limits does a dispatch with thread
limits that cut both halves. Both need a card with 32-lane waves and
have not run on one: the CPU Vulkan driver skips them.

wave64_fold_census (ignored, XCL_SAMPLE_RPF) counts a pack's wave64
compute programs that fold and those that do not, by reason.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Folded, the two copies of each repeated memory access are compact blocks
of their own, so a folded program's tables can pass the limit its split
form's stay under, and it then took the plain form. On a tree with
xcl-dev#3516's limit of 12,288 words, a retail wave64 compute program of 78,920
reachable instructions took the compact form split (11,311 table words)
and, folded (14,805 words), the plain form: 4,346,408 B of DXIL in one
function, which NVIDIA's compiler had not finished after ten minutes at
4.9 GB.

The folded translation now refuses a program whose compact form is
refused only for the size of its tables (compact::wanted_plan: the
automatic choice before its table check), and the split form takes the
compact form as it did before folding.

Under main's 32,768 words no program of that pack changes: two folded
programs want the compact form, with 14,776 and 14,805 table words. Under
xcl-dev#3516's 12,288 both do.

The test folds a program of 57,344 copies of a load whose split form's
tables fit and whose folded tables do not: the folded translation is
refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The folded form refused a program only when its twins could not be named
(2N > 256 VGPRs). A host lane holds both copies, and a thread of an NVIDIA
GPU has 255 registers: at 128 VGPRs the folded program names 256. The
rewrite needs no VGPR beyond the twins (its scratch registers are spare
SGPRs), so the refusal is now 2N > 255, VIctory-NVIDIA's rule
(fits_host_regs: 2N + 2, its two temporary VGPRs, within 255) for this
rewrite.

In a retail pack no distinct wave64 compute program has 120 to 128 VGPRs:
the largest that folds has 119, and the three of 129 (the programs
VIctory-NVIDIA measured at 2 x 129) were refused before as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
XCL_WAVE64_FOLD=1 turns it on. Its GPU time against the split form is
not measured, and xcl-dev#3543, a port of the same idea, measured a median
1.27 times the split form's time on an RTX 4080.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Royalflamejlh
Royalflamejlh force-pushed the recompiler/wave64-fold branch from 0c6bd4c to 913af19 Compare December 1, 2026 20:46
@github-actions github-actions Bot added the area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation label Dec 1, 2026
@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

XCL_WAVE64_FOLD is now a warm pipeline library setting as well (head 913af192c): with it left out, a warm library built with folding on was reused with it off. Found while checking bmjask's point on #3563.

AI use: Claude Code (Claude Opus 5.5).

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up review (head 913af19, since bda4766, diff read, not a merge-check; checks red):

New:

Not reviewed line by line again because of the size; the 20 lines changed since the last review (the opt-in switch, warm setting, test, docs) are correct. The provenance note and THIRD_PARTY.md entry are in place.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Royalflamejlh

Copy link
Copy Markdown
Contributor Author

@Foniass thanks for the follow-up. I'd rather keep it as one pull request, and the description now says why, under Why it is one pull request: fold.rs is one rewrite pass whose parts nothing could call or test alone (the tests run whole console programs through it), so the split would leave one ~1,900-line PR that is this one, beside three small ones; the closed #3529 / #3530 / #3543 stack split the same port and none of its parts could merge alone. The six commits are kept apart so each part reads on its own, and nothing changes without XCL_WAVE64_FOLD=1.

The other open points stand as you list them: the smoke pair and a Windows run are owed (no Windows machine of ours answered today), the GPU tests compare with the interpreter rather than the split form on the same GPU, and the compact_nest_tag conflict with #3561 / #3563.

AI use: Claude Code (Claude Opus 5.5) drafted this reply and the description section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs-ci Documentation, scripts, workflows, repo governance area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants