Skip to content

Recompiler: a program's HLSL has only the prelude functions it reaches - #2827

Open
Alox0099 wants to merge 1 commit into
xcl-dev:mainfrom
Alox0099:recompiler/prune-prelude
Open

Alox0099 wants to merge 1 commit into
xcl-dev:mainfrom
Alox0099:recompiler/prune-prelude

Conversation

@Alox0099

Copy link
Copy Markdown

Every translated program carries PRELUDE and PRELUDE_MEM whole – 91 helper functions, about 31 KB – and calls a few of them. The DXIL cache is keyed on the HLSL, so changing any one helper changes the key of every program, and a title then recompiles all of them, whether or not they use that helper. #587 (frexp_mant), #2334 (msad_u8, pk_sad_u16) and #2247 (rla) each did this on the retail pack. A program's HLSL now contains the globals and only the prelude functions it reaches. Changing a helper then recompiles only the programs that use it. DXC drops unreached functions, so the DXIL does not change. Part of #1269 and #2088.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: finish puts placeholders where the two preludes go. with_preludes replaces them with the globals and the functions the program reaches: those named outside the two preludes, and in turn those that the kept functions name. A name counts wherever it appears as a whole word, so a stray match keeps a function it does not need, never the other way round. prelude_items splits a prelude into its functions, each with the comment lines above it, and the rest. Unit tests: the split puts the prelude back together exactly and finds every braced block; a program gets the functions it reaches and their dependencies, no others, and the globals; a program that names every function gets the preludes whole.
  • crates/xcl/src/gpu/recomp/tests.rs: gpu_mad_nf_f16_rounds_intermediate_in_binary16 adds calls to mad_nf_f16 to a translation. RDNA2 has no instruction that emits it, so the test now adds the definitions it needs with the probe (missing_prelude_functions, test only). Its assertions are unchanged.

Tested

  • NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.2140), AMD Ryzen 7 5800X, Windows 26H2 build 26300.9974, rustc 1.100.0; tests in debug, the measurements below and the regression runs in release
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 1437 passed, 0 failed, 47 ignored
  • SKIPPED on this adapter: 22 subcases with "no wave64", 3 refused per-lane-form subcases (V_CVT_F16_F32, V_PK_MUL_F16, V_MUL_F16), and pso_measurement_compiles_an_exact_synthetic_record ("cannot disable the driver cache", no Developer Mode)
  • Retail shader pack, 26 698 programs, main b49be18 against this commit:
    • xcl shaders --recomp: the same report from both (the same programs translated, the same refusals); the whole pass took 544 s on main and 553 s here (one run each, run at the same time).
    • Every 40th program dumped by both (XCL_DUMP_HLSL, XCL_DUMP_EVERY=40: 671 programs, all stages) and compiled with dxc.exe 1.8.2502.11 with the same flags: 671 / 671 byte-identical DXIL.
    • Their HLSL is 6.6 % smaller in total, 27.6 % smaller for the 439 under 200 KB.
    • DXC on 100 of those small programs, one dxc.exe at a time: 18.3 s → 17.4 s (5 % less).
    • Of the 671 programs, frexp_mant and pk_sad_u16 are now in none (before: all 671), and rla is in 487.
  • Two of the 671 pixel shaders drop below the 1 MiB size at which XCL compiles at -O1, so DXC compiles them at -O3 instead: 0.5 s more each, 0.2–1 % smaller DXIL.

Regression run
The retail title on the RTX 3070 Ti, each run after a warm-up run of the same build:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 3070 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1568 (PresentX 1568)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15969 MB
xcl-smoke v1
commit: 3bdd5f2fa7bc5566fb633739765bb84882257573
gpu: NVIDIA GeForce RTX 3070 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1560 (PresentX 1560)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15270 MB

Not tested

  • AMD and Intel GPUs, wave64 hardware (the 22 subcases with "no wave64" above), the maintainer's RDNA4 run.
  • DXIL identity was checked on a 1-in-40 sample of the retail pack, not on every program, and not on the per-lane / direct DXIL paths (they do not use these preludes).
  • The program's own HLSL counts in the reach, comments included. A comment that names a helper keeps that helper; nothing that is used is dropped.
  • This PR itself changes the HLSL of every program once, so titles recompile once after it.
  • HANDOFF.md notes that a prelude change makes every title recompile (e.g. "Prelude changed: titles recompile"). After this PR that holds only for the programs that use the changed function.

AI use

  • Claude Code with Claude Opus 5.5: wrote the code, the tests, the measurement scripts and this description. It ran the tests and the measurements listed above on the hardware named.

🤖 Generated with Claude Code

Every translated program carried PRELUDE and PRELUDE_MEM whole: 91 helper
functions, about 31 KB, of which a program calls a few. The DXIL cache is
keyed on the HLSL, so a change to any one helper changed the key of every
program and a title recompiled all of them: xcl-dev#587 (frexp_mant), xcl-dev#2334
(msad_u8, pk_sad_u16) and xcl-dev#2247 (rla) each did on the retail pack.

finish() now puts placeholders where the two preludes go and replaces them
with the globals and only the functions the program reaches: those named
outside the two preludes and, in turn, those the kept functions name. A name
counts wherever it appears as a word, so a stray match keeps a function it
need not, never the other way round. DXC drops unreached functions, so the
DXIL does not change.

Measured on a retail shader pack (26 698 programs), main b49be18 against
this commit: `xcl shaders --recomp` gives the same report (same programs
translated, same refusals). 671 of its programs dumped by both (one in 40)
and compiled with dxc.exe with the same flags: 671 / 671 byte-identical
DXIL. Their HLSL is 6.6 % smaller (27.6 % for the 439 under 200 KB). Of
those 671, frexp_mant and pk_sad_u16 are now in none (before: all 671) and
rla in 487. Two pixel shaders drop below the 1 MiB line where XCL picks
-O1, so DXC compiles them at -O3: 0.5 s more each, 0.2 - 1 % smaller DXIL.

gpu_mad_nf_f16_rounds_intermediate_in_binary16 calls mad_nf_f16 in code it
adds to a translation; it now adds the definitions with it
(missing_prelude_functions, test only).

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SzczepanGrela

Copy link
Copy Markdown

RDNA4 / Wine check of the HLSL prelude pruning, including byte comparisons with the current main.

Tested

  • Exact head 3bdd5f2fa7bc5566fb633739765bb84882257573, whose parent is b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7.
  • Current main 8b957bec6f82495aec940944c6bf47faa02db590, and a separate copy of that main with this PR's two changed source files applied. Both files were unchanged between the parent and current main, so this integration needed no conflict resolution.
  • RX 9070 16 GB (RADV GFX1201), Ryzen 7 7700, CachyOS kernel 7.3.0-5-cachyos, Mesa 26.3.1-arch3.5, GE-Proton11-12 Wine 11.0 Staging, vkd3d-proton 8293130ebe370b1+ (local asserts-disabled build), DXC/DXIL 1.9.2602.17, Rust 1.97.1, release x86_64-pc-windows-gnu.
  • Identical local observation hooks on all three copies, active only in tests, save the HLSL/DXIL returned by dxc::compile and the kernel runner's readbacks. No original assertion was changed. Removing those hooks reconstructs the pinned source bytes.

Results

gpu::recomp::emit::tests::, at both one and four test threads:

Source Passed / failed / ignored
Current main 4 / 0 / 0
Exact PR head 6 / 0 / 0
Current main plus this PR 6 / 0 / 0

Eleven original GPU tests were then run serially in three fresh processes per source copy, alternating source order. Each process: 11 passed, 0 failed, 0 ignored. The set covers frexp f32/f16 sign handling, f64 exponent/mantissa, f32 denorm modes, QSAD/MQSAD, integer multiply/shift/BFE, format packing/saturation, global/LDS atomics, permlane/WQM and the f16 intermediate-rounding probe changed in this PR. Native wave64 ran; the tests that explicitly request wave32 also ran. gpu_qsad_and_mqsad additionally exercises its own direct-DXIL controls. No capability skips in any of the 15 top-level processes.

From those GPU processes:

  • 16 HLSL variants per process, 144 captured successful compilations in total. Every corresponding DXIL container is byte-identical across main, exact head, integrated head and all repetitions, checked with SHA-256 and complete file comparisons. The 18 additional compilations from the emitter-module runs also match.
  • Across the 16 variants, main HLSL totals 608,269 bytes, exact/integrated head 128,877 bytes. This is a 78.8% source reduction for this small synthetic set, not a shader-pack or DXC-time measurement. The head and integrated HLSL match exactly.
  • 20 raw kernel readbacks per process, 180 total. All corresponding raw register/memory data match after checking the atomic fixture's documented variable LDS values as below.

The atomic fixture deliberately does not initialize its LDS counter. Its 64 DS_ADD_RTN_U32 old values passed an independent consecutive-values check modulo 2^32 in every run; their absolute starting value varies on main too. Lane 63's second DS_READ2 neighbor is outside the LDS area written by the fixture, and its original oracle explicitly excludes that value. Only those 65 raw slots are omitted from cross-run byte equality. The other raw slots and final global memory compare exactly; final atomic add/max values remain 64/163. This does not change or relax the original test assertions.

All summaries, skip checks, captured files, source inventories and executable hashes were independently verified.

Build: cargo test --offline --locked --release --target x86_64-pc-windows-gnu -p xcl --lib --no-run. Run the libtest executable under GE-Proton Wine with the emitter-module filter and --nocapture --test-threads=1 / 4. The GPU matrix uses --exact --nocapture --test-threads=1 with these full gpu::recomp::tests:: selectors:

gpu_frexp_exp_f32_denorm_modes
gpu_frexp_f64_exp_and_mant
gpu_frexp_mant_f32_f16_keep_the_sign
gpu_qsad_and_mqsad
gpu_scalar_sext_mulhi_and_vector_i24_sub_co_ci_u64_shifts
gpu_signed_bfe_arithmetic_shift
gpu_fmt_encode_int_sat_and_f16_rtz
gpu_mad_nf_f16_rounds_intermediate_in_binary16
gpu_permlane16_wqm_and_lds_stride64
gpu_format_stores_atomics_global_lds
gpu_valu_16_and_64_bit

Inherited XCL_* flags are cleared, then explicit RX 9070 selection, XCL_REQUIRE_GPU=wave64, XCL_DIRECT_DXIL=0, XCL_DXC_THREADS=1, XCL_NETWORK=0, the DXC folder and private enabled XCL/vkd3d caches are set. The observation directory contains only these synthetic test artifacts.

Not tested

Retail inputs, scripts/smoke.ps1, full suite, native Windows, other GPUs, every prelude function or shader stage, a changed-helper cache-invalidation experiment, DXC/driver timing, startup time or FPS. The emitter module's RLA reference tests build from the full prelude constant; the eleven translator GPU tests above exercise the changed emission path.

AI use

OpenAI Codex (GPT-6; exact serving variant not exposed) reviewed the diff, added the local observation hooks, ran the original tests on all three source copies, independently verified the evidence and drafted this report. Publication authorized by the account owner.

@DarkPanda69

Copy link
Copy Markdown

AMD run with native wave64 (listed as untested), and an RTX 3060 in the same PC: no change from main on either card.

Tested: pass, head 3bdd5f2 applied on main 8b957be, debug, Rust 1.100.0, Windows 11 Pro 26200, Ryzen 7 5800X, DXC 1.9.2602.17, XCL_DIRECT_DXIL unset (the HLSL path), card picked with XCL_ADAPTER.
cargo test -p xcl --lib -- gpu:: --test-threads=4 --nocapture, main and this PR one after the other on each card:

On both cards the only result changes are the two new tests, a_program_gets_the_helpers_it_reaches_and_no_others and the_preludes_split_into_their_functions_and_back, both ok. The SKIPPED sets are the same as on main.

Not tested: the retail shader pack or a title on AMD, XCL_DIRECT_DXIL=1, release, Intel.

AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@Decodere

Copy link
Copy Markdown
Contributor

Independent RTX 4060 (8 GB) / Windows 11 release verification of PR #2827 at 3bdd5f2fa7bc5566fb633739765bb84882257573 (exact parent & merge-base b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7). Measurements and targeted test results only; not a full source review.

Setup

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6 cores / 12 threads), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200
  • Toolchain / caches: rustc 1.100.0 (0ef1bc386 2026-11-09), DXC 1.9.2609.5, release builds, quiet machine (zero concurrent builds/tests during timed runs; note emit.rs changes XCL_TRANSLATOR_SOURCE_ID, so HLSL-path shaders translated under 3bdd5f2f compiled into a fresh translator namespace)
  • Workload: Retail workload title: 29640639 via scripts/smoke.ps1 -Seconds 60 (XCL_NETWORK=0, default flags, no input)

Targeted RTX 4060 release unit & GPU tests (3bdd5f2f)

  • cargo test --release -p xcl --lib the_preludes_split_into_their_functions_and_back -- --nocapture: 1 passed, 0 failed, 0 ignored (0.00 s, 0 SKIPPED).
  • cargo test --release -p xcl --lib a_program_gets_the_helpers_it_reaches_and_no_others -- --nocapture: 1 passed, 0 failed, 0 ignored (0.00 s, 0 SKIPPED).
  • cargo test --release -p xcl --lib gpu::recomp::: 403 passed, 0 failed, 20 ignored (4.87 s; 23 SKIPPED notices: 20 no wave64 on NVIDIA wave32 RTX 4060 + 3 pre-existing #1426 per-lane-form refusals for V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32).

Paired 60 s RTX 4060 smoke (exact parent b49be183 vs 3bdd5f2f)

Both runs completed cleanly at steady 60/2 s pacing with 0 device removals and 0 crashes (3bdd5f2f xcl.log: 6,176 / 6,116 MB peak/end local VRAM, 4,683 MB end non-local VRAM, 19,540 translated draws / 1,024 dispatches with 0 skipped).

Exact parent (b49be183):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1518 (PresentX 1518)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14018 MB

PR #2827 head (3bdd5f2f):

xcl-smoke v1
commit: 3bdd5f2fa7bc5566fb633739765bb84882257573
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1600 (PresentX 1600)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14028 MB

Limits of coverage

  • Full workspace test suite was not run on 3bdd5f2f (only the 2 new tests individually, all 403 passing tests in gpu::recomp::, release build, and exact-parent paired 60 s smokes); 20 wave64 GPU tests skipped on wave32 NVIDIA hardware.
  • No standalone DXC compile-time microbenchmark across large shader corpora was timed separately here; visual inspection beyond the 60 s no-input smoke was not performed.
  • Single discrete NVIDIA 8 GB GPU (RTX 4060) on native Windows 11; no AMD, Intel, or Wine/Linux run on this host.

AI use

Ran the targeted release unit/GPU tests, release build, and exact-parent paired quiet 60 s smoke runs with assistance from Google Antigravity (Gemini 4 Neon); all test counts, timings, memory figures, and smoke blocks were extracted directly from cargo test, xcl.log, and scripts/smoke.ps1 outputs.

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 26, 2026
…eludes reachability + split/whole round-trip tests)
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.

5 participants