Skip to content

Recompiler: structured form for programs that needed the pc chain - #172

Draft
Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:t3/compile-time
Draft

Foniass wants to merge 1 commit into
xcl-dev:mainfrom
Foniass:t3/compile-time

Conversation

@Foniass

@Foniass Foniass commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Lane L6 of the compile-time handoff (emitter and DXC cost).

Update 19:35 UTC: measured again with retail_translation_survey, the DXC gain is 6–8 %, and there is none on top of #174 (second table below).

What was slow

3 325 of the retail pack's 26 698 programs fall back to the while (pc …) dispatcher. On a 1-in-100 sample (4 memory windows) they are 13 % of the programs and about two thirds of DXC's time. -ftime-report on the slowest one shows jump threading at 27 % and the loop passes (LCSSA, indvars, unroll, loop deletion) most of the rest: every value of the dispatcher loop is loop-carried.

structurable works on the layout: every backward jump is a loop to it. The common failing shape is not a loop at all: the console compiler puts blocks after the code they jump back into and enters them from before it (acyclic, the jumps only go backwards in memory). Loops with several heads or exits are the rest.

What changed

gpu/recomp/structure.rs structures any reducible program from its dominator tree (N. Ramsey, "Beyond Relooper", ICFP 2022):

  • a loop head becomes for (;;) (with LOOP_GUARD);
  • a block with several forward predecessors is reached by leaving a do { … } while (false);
  • every other block goes inline at its only predecessor's branch;
  • a loop's exits that it dominates are placed after the loop;
  • a jump out of several constructs goes through xcl_br, and each construct passes it on.

It runs only where structurable fails, after a dry run: a program it rejects is emitted exactly as before. Unreachable blocks still go through the emitter (their text is dropped), so the registers, samplers and unsupported counts are those of the other forms. Left on the pc chain: 21 irreducible programs and the ES + GS pairs (58 programs in all, was 3 325).

Measured (RTX 5070 Ti machine, 16 threads, DXC 1.9.2602.17, dxc.exe, 8 compiles at once, nothing else running)

DXC CPU time, 1-in-100 sample (253 programs, every 100th by name hash), translated with 4 windows. DXC got -O1 above 1 MB of HLSL, as dxc::compile does since 956a476. Runs interleaved:

run main 956a476 this PR
1 331.1 s 283.6 s
2 332.5 s 284.4 s
3 334.8 s 262.0 s
  • The 32 changed programs in the sample: 239 → 184 s.
  • The slowest program: 29 s → 16–19 s.
  • DXIL of the sample: 24.4 → 23.0 MB (at -O3).
  • Peak DXC commit: max 638 → 564 MB.

--precompile --limit 2650, cold cache, split wave64 form: main 276.1 s, this PR 284.2 s, 0 errors each. One run per side, so no spread. That slice holds only 5 of the 3 267 changed programs (11 s of their 21 326 s of DXC CPU), so it cannot show the change. An end-to-end run over a slice that holds them is not done yet.

Measured again with retail_translation_survey, alone and with #174 (19:05–19:32 UTC)

Same machine, release build, the test's own DXC (4 threads): XCL_SURVEY_EVERY=100, XCL_SURVEY_DXC=1, XCL_SURVEY_WINDOWS=4, two seeds. #174 was merged locally (it conflicts only at the end of tests.rs) and run with its linking off (XCL_LINK_MEM=0) and on. Runs interleaved; total DXC time the test prints:

seed 1 (294 programs) seed 2 (261 programs)
main 956a476 336 / 323 / 332 s 363 / 353 s
this PR 310 / 298 / 309 s 342 / 325 s
#174 288 / 282 / 275 s 309 / 251 s
this PR + #174 286 / 288 / 286 s 308 / 301 s

So if #174 is merged, what this PR still gives is fewer programs on the dispatcher loop. Whether that helps the driver's compile time or the GPU's run time is not measured.

Output and correctness

  • HLSL: native form, 4 windows, every program of the pack: 23 431 programs byte-identical to main. The 3 267 that differ were all on the pc chain.
  • DXC: all 3 267 changed programs compile, 0 errors.
  • Self-check: every structured program can be walked against its control flow graph, both with the same pseudo-random branch decisions (24 runs, several biases). The block traces must match. The whole pack passes. This is on in the retail survey and in the new tests; XCL_RECOMP_CHECK=1 turns it on elsewhere.
  • GPU: gpu_general_structure_matches_the_pc_chain runs synthetic programs with the retail shapes (bottom entries, a jump out of two loops, a two-entry loop, a plain loop) as wave32 and wave64 (split on this GPU). It compares SGPRs and VGPRs with the pc chain for 6 inputs each.
  • Workspace tests: plain, with XCL_SAMPLE_RPF and with XCL_SPLIT_WAVE64=1: 571 passed, 0 failed, 39 ignored each.
  • Forced: with XCL_RECOMP_GENERAL=1 (the general form for every program with branches) the GPU suite passes. Only the three tests that look for the first form's text fail.

Not tested

Also measured, not in this PR

  • Memory windows: with 4 windows the per-access window switch is about 26 % of what is left (the same sample with 1 window: 220 s vs 296 s). Removing it needs another way to bind the windows than root UAVs.
  • EXEC: a forward analysis of where EXEC holds every lane (compute / mesh) removed only 2.4 % of the EXEC selects (most code runs under a v_cmpx); dropped.
  • DXC options: -opt-disable gvn is slower; licm / sink: no change.

Gate A passed at 2026-11-23 17:59 UTC on main db5dc26; Gate B passed at 18:46 UTC on main 956a476. PRs read: #113, #134, #138, #139, #155, #162, #166 and the bmjask/playability branch: none structures control flow. Written with an AI assistant (Claude); measured on the owner's machine. No game material in this PR: the tests use synthetic programs.

🤖 Generated with Claude Code

`structurable` works on the layout: every backward jump is a loop to it. Code the console
compiler placed after the blocks it jumps back into, entered from before them, and loops
with several heads or exits made 3 325 of the retail pack's 26 698 programs fall back to
the `pc` dispatcher, about two thirds of their DXC time (every value of the dispatcher loop
is loop-carried; jump threading and the loop passes work on it).

`structure.rs` builds structured code for any reducible program from its dominator tree
(N. Ramsey, "Beyond Relooper"): a loop head becomes `for (;;)`, a merge is reached by
leaving a `do { } while (false)`, other blocks go inline, a loop's dominated exits go after
it, and a jump out of several constructs passes through `xcl_br`. It is used only where
`structurable` fails, after a dry run (a program it rejects is emitted as before), so
every other program's HLSL is unchanged. Left on the pc chain: 21 irreducible programs
and ES + GS pairs.

Checks: every structured program can be walked against its control flow graph with the
same random branch decisions (`XCL_RECOMP_CHECK=1`, the retail survey and the new tests do);
GPU tests compare the general form with the pc chain; `XCL_RECOMP_GENERAL=1` forces the
general form for every program (the GPU suite passes with it).

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

Copy link
Copy Markdown
Contributor

AMD run (no retail data, so not XDefiant / the retail game): RDNA3 Radeon RX 7800 XT (16 GB, native 64-lane waves, amdxc64), driver 32.0.32015.7029, Windows 11 Pro 10.0.26300, Rust 1.100.0 x86_64-pc-windows-msvc, DXC 1.9.2602.17, debug build, PR head ca66c4a. cargo test -p xcl --lib:

Run Result
plain 537 passed, 1 failed, 19 ignored
XCL_SPLIT_WAVE64=1 538 passed, 0 failed
XCL_RECOMP_GENERAL=1 (gpu:: tests) 254 passed, 2 failed
  • gpu_general_structure_matches_the_pc_chain passes in all three runs, natively at 64 lanes and split. No driver crash, no TDR.
  • The failure in the plain run is gpu_wave64_ps_whole_wave_reductions. It fails the same way on main on RDNA2/RDNA3 and is fixed by VS / PS: the 32-lane form on every GPU that can run 32-lane waves (RD… #155.
  • Under XCL_RECOMP_GENERAL=1 the failures are that test and hlsl_is_generated_for_straight_line_and_branches, which checks the first form's text. Only one text test failed here, not the three in the PR description.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

With default settings this PR changes no existing program's HLSL. Every program main's synthetic recompiler tests translate (160 distinct bytecode/options pairs) gave byte-identical HLSL on main and on the merge. Only the PR's own four programs change. Carried to main, they show the effect:

  • bottom entries and nested exits lose the pc dispatcher;
  • the ordinary loop stays structured;
  • the two-entry loop keeps its dispatcher.

The PR's GPU differential passes for wave32 and split wave64 over six inputs per program.

A small note: XCL_RECOMP_GENERAL is checked with var_os(..).is_some() (gpu/recomp/mod.rs:1388), so =0 also forces the general form, as most XCL_* switches on main do. Under the forced form, 15 of 156 pairs change HLSL, among them main's scalar-loop and EXEC-divergence programs, and both GPU tests still pass. The recompiler run was 183 / 1 / 18; the one failure is the HLSL shape assertion, which expects the default form.

Tested

  • Windows 11, RTX 5070 Ti (NVIDIA 610.74), SDK DXC 1.7.2308.16, release. A test-only observer saved the HLSL of each successful translation, keyed by bytecode and options, and the files were compared byte for byte.
  • Counts are passed / failed / ignored.
Revision Library gpu::recomp::
PR head ca66c4a 538 / 0 / 19 167 / 0 / 18
main 60565c4 611 / 0 / 20 182 / 0 / 18
Merge onto 60565c4 (clean, local) 613 / 0 / 20 184 / 0 / 18

Not tested

  • Retail shader packs.
  • Native wave64 hardware: this card runs wave64 as two wave32 halves.
  • Driver compile time.

🤖 GPT-6.2 Astra, Claude Opus 5.5

@mariuslozo-boop

Copy link
Copy Markdown

Retail NVIDIA driver numbers for this PR. Kalmarv showed that with default settings this PR leaves the HLSL of main's synthetic test programs byte-for-byte the same, and listed retail packs and driver time as not tested. On a retail compute program, the default path (XCL_RECOMP_GENERAL not set) removes the pc dispatcher and cuts driver time 5.7×.

Setup: RTX 5070 Ti (16 GB), driver 616.92, Windows 11 26300, DXC from SDK 10.0.26100. The records come from the retail shaders.rpf (1.0.2.1), passed in through XCL_SAMPLE_RPF. The method is the same local bench as in my #156 comment:

  • It translates each record the way the runtime does (split wave64, 4 memory windows in the switch form), then compiles it with DXC.
  • It times only CreateComputePipelineState, on one thread, with a driver that has never seen the program.

prop_batch_standard_fabric_dirt:CS_RaytracingResolve_b1139868:

build DXIL translate + DXC driver
main 38b05b7 (same recompiler as 956a476, this PR's base) 1,105,160 B 25.9 s 176.4 s
main with the exec-masked selects stripped (local experiment; changes what the program computes, so for timing only) 981,368 B 17.1 s 183.8 s
this PR ca66c4a 889,412 B 10.7 s 30.8 s
  • Before this PR, the program's HLSL has 2 while (pc …) dispatcher loops with 92 pc == cases. With this PR it has none. rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled_bfd3b062 goes from 17 loops with 377 cases to none.
  • Stripping the per-write exec selects shrinks the DXIL by 11% but does not lower driver time. So on this program the DXIL size does not explain the driver time; the drop came with removing the dispatcher.

The larger compute programs:

program DXIL, main → this PR driver, main 956a476 driver, this PR
rtdirectionalshadows:CS_RTShadowTrace32HQ_Tiled 5.02 → 4.68 MB not measured 451.9 s
rtdirectionalshadows:CS_RTShadowTrace32TemporalHQ_OneOver8RPP_Tiled 1.90 → 1.54 MB 347.4 s 107.5 s
cascadeshadows_rendering:CS_ShadowRenderTiledCascadesCBPPPC 1.54 → 1.44 MB 135.0 s not measured
ped_standard_garment_2lyr:CS_RaytracingResolve_ae7b7f3c 1.31 → 1.20 MB 188.8 s not measured

The run with this PR was stopped after its first two programs; I will add the other two. On main, the 5.3 MB …TemporalHQ_OneOver2RPP_Tiled did not finish in 25 minutes.

Not tested: whether the retail programs' output matches on the GPU (this is compile time only), AMD, DXC 1.9. Each time is a single run.

AI-assisted: Claude Code (Claude Opus 5.5) ran the measurements and wrote this comment. No game files attached.

@PHeitor1

Copy link
Copy Markdown

Tested ca66c4a merged onto main 9ab7d76 on RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), rustc 1.100.0 MSVC, debug, cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_*. Baseline main 9ab7d76: 651 passed, 0 failed, 40 ignored.

Pass: 653 passed, 0 failed, 40 ignored. GPU skips: the same 2 as on main (no wave64).

Run and written with Claude Code (Claude Opus 5.5).

@xstraok

xstraok commented Nov 24, 2026

Copy link
Copy Markdown

Retail run on Linux (GTA VI 1.0.2.1): the general form fixes the compute program from #611, but its sibling now stalls dxil-spirv (vkd3d-proton's DXIL → SPIR-V) in a different pass.

Setup: PR head ca66c4a applied (without the tests.rs hunk, which conflicts) on top of a local tree: main a6f5f7a + #257 (Linux kit) + #458 + #496 + #484 + #610 (heap-index clamp), uncommitted. Release build, x86_64-pc-windows-gnu, rustc 1.92.0. RX 9060 XT (RDNA4, RADV, Mesa 26.2.6), Ryzen 5 7600, Arch Linux 7.2.15, Proton Experimental, vkd3d-proton 3.1 (4522cc1a) + local patches, dxil-spirv 8b48f0e0. One 240 s run each, XCL_COHERENT_MB=512, compared with the same tree without this PR (run f).

Offline: native dxil-spirv CLI run on the DXBC that XCL cached in dxil/translated/:

In the title:

without #172 with #172
last submits 152 s 176 s
title watchdog (+0x23cf683, #505 / #534) 181 s 205 s
PSO thread stuck in find_selection_merges (RAM +500 MB/s) build_ladder_block_for_escaping_edge_handling (100 % CPU from ~178 s, no RAM growth)
peak RSS 18.7 GB 16.7 GB

With #172 the loading screen rendered correctly. Part of the 24 s gain may only be retranslation, because this was the PR exe's first run: DXC threads were busy from 168 s.

So the PR helps here, but one program's structured output still sends dxil-spirv into a loop. Both stalls are inside dxil-spirv (#611).

Not tested: Windows / native D3D12 (dxil-spirv is not on that path), cargo test (the PR's tests were not applied), smoke.ps1, other titles.

Tested and written with Claude Code (Claude Opus 5.5); the run and the measurements are from this machine.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

Thanks for this. I read structure.rs and hunted for counter-examples at ca66c4a (also merged onto main dd2ae9a: clean merge, same results). I found no miscompile in the new form. The tests are in a test-only commit you can take: 6e9c949 on QemmHD/xcl review/172-structure (recomp/tests/structure_fuzz.rs).

What held

  • 20 000 random programs (forward graphs plus jumps back to dominators, loop nests with jumps to every level's head and exit, blocks shuffled in memory): all 19 173 reducible ones get the general form, the 293 irreducible ones keep the pc chain, and none go the other way. The HLSL text, walked with 24 branch-decision sequences per program, runs the program's instructions in the program's order.
  • GPU (RTX 4070 SUPER): 1 200 random programs (600 wave32, 600 wave64 in the split form, 3 inputs each) with masks, s_and_saveexec, v_readfirstlane / v_readlane, DPP16, s_barrier and stores inside the loops. The general form matches a CPU reference run in every register and in memory.
  • Hand-picked cases: s_endpgm inside loops, unreachable blocks (also an irreducible unreachable loop), loop heads with several latches, an untranslated branch, an indirect s_setpc, a far jump as a latch, and 150 nested blocks (DXC compiles them; 200 fall back).

1. A LOOP_GUARD trip can put the split-wave64 halves out of step (structure.rs:610, x64_masks.rs:160-181)
x64_masks assumes a guard exit goes to the code after the loop in memory, or to the end of the program (as on the pc chain). Here the break lands on the block placed after the for, which can be anywhere:

[loop] for (;;) {
    if (xcl_budget == 0u) break; xcl_budget--;
    ...
    // 002c: v_cmp_gt_u32 8, v0
    uint2 t4 = xcl_x_ballot_local(...);   // half-local at the loop's top
    continue;
}
...
// 0004: s_and_saveexec_b64 s[2:3], vcc   <- after a trip: reads it whole
// 0008: s_cbranch_execz 0x14             <- the halves can differ here

This only happens after a trip, and I deliberately didn't run it on the GPU. Test: split_wave64_halves_stay_in_step_after_a_loop_guard_trip (should_panic until fixed). One fix that keeps the analysis: make a trip a jump to Label::Exit, which is what these programs did on the pc chain. Measured with a 100 000-iteration loop: pc chain s0 = 65536, s3 = 0; this form s0 = 65536, s3 = 1. Otherwise docs/wave64-on-wave32.md ("to the code after it or … the end of the program") needs the new case.

2. The GPU test's reference isn't the pc chain alone (tests.rs:6859, mod.rs:1053)
Options { structured: false } still emits the first structured form for single-entry stretches, and that form is wrong for two shapes. Both are on main, not from this PR, and are now reported as #701. The general form gets both right (gpu_first_form_counter_examples_are_right_in_the_general_form). In the random run the pc chain and the default path differed from the reference in 13 of 1 200 programs, the general form in none. The expected values (the CPU run in the commit) would be a safer reference.

3. check walks the tree, not the text (structure.rs:678)
refs, the elided tail jumps, falls_off and pass_on (497-659) are outside it: changing continue to break in pass_on still passes check. The text walk in the commit catches it.

4. A failed check is silent (structure.rs:466-471)
It runs after the emitting build, so the fallback emits into the same Em again, and gpu_general_structure_matches_the_pc_chain then compares the pc chain with itself. check only needs the dry run's tree; a failure could panic under cfg(test).

5. Not covered here
A wave32 program in 64-lane hardware waves (VS / PS on AMD, where the halves branch differently) in this form. A forced-general variant of gpu_wave32_program_in_wave64_hardware, with a loop the halves leave at different iterations, would cover it.

6. Small

  • size() recounts a Vec<bool> per comparison (structure.rs:191, 207).
  • MAX_DEPTH 160 needs between 256 and 512 KB of stack in a debug build, which is fine on the 64 MB workers. Is translate ever called on a title thread?
  • Options::structured says "always the guarded block chain" (mod.rs:491).

7. CONTRIBUTING

  • 1 034 lines, 779 in one file: the description should say why it can't be split (for example: structure.rs and its tests behind XCL_RECOMP_GENERAL, then turning it on, then XCL_RECOMP_CHECK).
  • No Regression run (smoke.ps1, main against the head) yet.
  • AI use should name the model.

Reviewed and run on an RTX 4070 SUPER (NVIDIA 621.24, wave32); the commit's 14 tests were re-run before posting (14 passed, including the GPU ones).
AI-assisted review (Claude Code; Claude Opus 5.5 and Claude Fable 5.2).
Discord: bigpeppa.

@t4l03

t4l03 commented Nov 24, 2026

Copy link
Copy Markdown

ActivateRays coverage on the exact draft head

Tested clean ca66c4aeb0def58bddf299c43557e3c5cb79c0fb, Windows 11 build 26300, RTX 4070 Ti driver 617.14, Ryzen 7 7800X3D and DXC 1.9.2609.5.

On the 76,699-instruction ActivateRays compute program discussed in #1269, the general structurer declines with nesting too deep (reported with XCL_RECOMP_GENERAL=1 XCL_RECOMP_WHY=1). Four-window output from the default and PC control was byte-identical: 14,297,985 bytes of HLSL, 15,795 pc guards. I timed the identical output once. DXC -O0 finished in 58.09 s, yielding 13,015,412 bytes of DXIL. Pipeline creation reached a 75-second time cap with a fresh XCL cache. This documents the fallback on one large program; a smaller 15,577-instruction retail compute control produced HLSL without PC guards and built its pipeline in 26.47 s.

Tested: cargo build --locked --workspace -j 4 followed by cargo test --locked --workspace -j 4 --no-fail-fast -- --test-threads=4 --show-output: 571 reported passed, 0 failed, 39 ignored. Two GPU tests were counted as passes but skipped for lack of native wave64: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware.

Not tested: retail shader output, a new structuring form for this program, or native wave64.

AI use: Codex (gpt-6.2-sol) ran the tests and wrote this report.

@jessenic

Copy link
Copy Markdown
Contributor

Driver compile time of ca66c4a on today's tree: on the retail CS_RaytracingResolve programs it still moves off the pc dispatcher, NVIDIA's compile is the same or slower than without it. This is the driver-time question the description leaves open. It follows @mariuslozo-boop's 5.7× on 956a476 (comment); the base has changed since.

Tested

Which programs change. Compute programs with 1 MB or more of HLSL in shaders.rpf (1.0.2.1):

  • 900 such programs. 159 are on the pc form without this PR, 1 with it.
  • The 157 whose form changes are all CS_RaytracingResolve, the family whose ~1 MB DXIL compiles hold the world load up here.
  • Their HLSL is 6–17 % larger with this PR (median 10 %).

Driver time: four of them, picked across the size range; passes alternated (stack, PR, stack, PR).

program DXIL, without → with driver without (pass 1, 3) driver with (pass 2, 4)
emissivenight:CS_RaytracingResolve_b1139868 624 → 603 KB 7.8 / 7.6 s 7.9 / 7.9 s
ped_standard_muv_secmo:CS_RaytracingResolve_b1139868 1061 → 1029 KB 19.6 / 17.4 s 23.2 / 23.1 s
standard_2lyr_doublesided_secmo:CS_RaytracingResolve_18a17e12 878 → 835 KB 13.7 / 13.2 s 13.8 / 14.4 s
standard_2lyr_cutout_hbb_ground:CS_RaytracingResolve_c5cb017e 975 → 933 KB 15.3 / 15.5 s 17.9 / 20.0 s
  • DXC time is about the same either way (5–11 s).
  • In @mariuslozo-boop's table, a program of the same family took 176 s on 956a476 and 31 s with this PR. On today's base the pc form of these programs already builds in 8–20 s. So most of that gain seems to have reached main by other routes (b8b1f49, and Recompiler: memory windows from the descriptor heap (no per-access switch) #680's heap windows here), and on these four programs this PR adds nothing measurable to driver time.

Not tested

  • This PR alone on current main without the rest of the stack, and without XCL_HEAP_WINDOWS.
  • The other 153 changed programs; the RTShadowTrace and cascade-shadow programs from the earlier comment (they are not on the pc form in our stack, so this PR does not change them there).
  • GPU correctness of the changed programs; run time; AMD; other NVIDIA drivers.

AI use: Claude Code (Claude Opus 5.5) built the trees, wrote the local timing mode, ran the measurements and wrote this report. No game data is attached; program names are the pack's record names.

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.