Skip to content

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

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

Foniass wants to merge 1 commit into
ivall: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.

@RemingtonWilcox

Copy link
Copy Markdown
Contributor

Tested ca66c4a (main 956a476 + this PR) on an RTX 3090 (driver 616.92, 32-lane waves), Windows 11 26300, Ryzen 9 5950X.
cargo test --workspace --no-fail-fast -- --test-threads=2: 571 passed, 0 failed, 39 ignored (main: 569 / 0 / 39). general_structure_takes_what_the_first_form_cannot passes, and so does gpu_general_structure_matches_the_pc_chain, wave64 (split form here) and wave32. No regressions.

AI-assisted test report (Claude); tests run locally as stated; the diff was read in full before running.

@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).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants