Skip to content

Recompiler: lift literals that differ inside a family (XCL_LIFT_LITERALS) - #3262

Draft
KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:recomp-lift-literals
Draft

KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:recomp-lift-literals

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Compute programs in a title's pack that are one program with different numeric constants get their literals that differ lifted into io, so the whole family compiles to one HLSL text, one DXC result and one driver blob. Behind XCL_LIFT_LITERALS=1 (off by default) until cold-run numbers are in. Related: #2711 (identical split pieces share a container, does not fold literal variants), #2088.

Why: in a cold run of a retail game (RTX 2070 SUPER), 13 repeat driver compiles of literal-only variants took 253 s of the 1,327 s spent in the driver (19 %). Pack-wide, 127 families (312 programs) make 156 extra compiles of 2,682 compute programs (5.8 %). Example: records b162ea7db32e1657 / 0ed9e58ef6dc9cdb, 462 KB of DXIL each, compiled twice. All 21 same-size repeats in that run took the HLSL path, which is where this works.

How:

  • Preparation (xcl shaders --precompile, launch preparation) finds the families. Programs with the same instruction sequence are translated, and those whose HLSL is equal once every literal is masked form a family.
  • Only the literal positions that differ inside a family are lifted. The other constants stay folded.
  • Liftable: plain u integer literals in main's body only. Never attributes, declarations, control-flow or preprocessor lines, [], texture offsets or quad operations. A family with a differing literal anywhere else is left alone.
  • A lifted literal is uint xcl_liftN = io.Load(512 + 4N), the same 32-bit value as a uint, so the program computes the same thing. Lifting is correct for any program; the family only decides what is worth lifting.
  • The lifted text drops comments, so every member has the same text and the DXC cache and driver blob cache share it. A pipeline keeps its member's values, and both compute dispatch paths write them after the SGPRs.
  • If DXC refuses a lifted text, the program compiles as it is (logged).

What is in it

  • crates/xcl/src/gpu/recomp/lift.rs: scanner, masked hash, family positions, rewrite, family key, table (built at preparation, kept next to the caches under the translation-cache identity), unit tests, and an XCL_SAMPLE_LIFT_HLSL test over exported HLSL.
  • crates/xcl/src/gpu/recomp/pso.rs: Compiled::lifted. The HLSL path compiles a family member lifted. A member skips the translated disk cache, which keeps no lifted values.
  • crates/xcl/src/hle/d3d12x/real.rs, bind.rs, console_list.rs: per-pipeline values, written after the SGPRs by both compute dispatch paths.
  • crates/xcl/src/shaders.rs: precompile builds the family table first when enabled, with a log line.
  • crates/xcl/src/gpu/recomp/tests.rs + runner.rs: GPU test. Two programs differing only in literals give one lifted text and one DXIL container, and each gives the same output as its plain translation. runner::dispatch keeps io words past the SGPRs.

Size: ~450 changed lines outside tests (679 with them). One PR because the parts only do something together: the table without the dispatch writes would produce programs that read zeros, and the dispatch writes without the table never run.

Tested

Regression run

main (fa20459):

xcl-smoke v1
commit: fa204595a488c60c6c5f905244f0172072c9c2a9
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1533 (PresentX 1533)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 17409 MB

this PR's head (1d3b1cb):

xcl-smoke v1
commit: 1d3b1cb934847faa3d11c428ee20b47c5e08b43c
gpu: NVIDIA GeForce RTX 2070 SUPER
title: retail game
seconds: 60
exit: 0x0
frames shown: 1500 (PresentX 1500)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17303 MB

Both: scripts/smoke.ps1 -Seconds 60, XCL_PREPARE=0, XCL_PRESS_AT=25:A, run through the local exclusive queue. XCL_LIFT_LITERALS is unset (off), so the lifting path is not exercised here.

These runs are on the head before the rebase (1d3b1cb); they have not been repeated on c0a0ec0.

Not tested

  • Cold-run gain with XCL_LIFT_LITERALS=1 (preparation time, driver compiles): numbers requested from contributors in a comment below.
  • Frame-time cost of lifted literals (uniform io loads instead of folded constants).
  • AMD / Intel GPUs, wave64 adapters.
  • Families of programs over 256 KB through DXC: covered only by the runtime fallback if DXC refuses one.
  • A launch whose preparation runs in a separate builder process: the title reads the table file the first time it looks it up.

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests and this description. The investigation that found the families was Claude Code, Claude Opus 5.5 too.

🤖 Generated with Claude Code

@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Results wanted: cold preparation with literal lifting on and off

Some compute programs in a pack differ only in numeric constants. This PR lifts those constants so a family compiles once (DXC and driver). On a retail game, RTX 2070 SUPER: 13 repeat driver compiles = 253 s of 1,327 s driver time in one cold run (19 %); pack-wide 156 extra compiles of 2,682 compute programs (5.8 %). Gain still unmeasured with the switch on: we need cold numbers from other hardware.

  • Baseline and candidate: the same build of this PR's head (KOLESNiii:recomp-lift-literals, commit in the PR), XCL_LIFT_LITERALS=0 vs XCL_LIFT_LITERALS=1.
  • Build: cargo build --release -p xcl, say your Rust version.
  • Run, cold each time:
    • fresh DXIL cache: set XCL_DXIL_CACHE to a new empty folder for each run;
    • clear the driver's shader cache (NVIDIA: %LOCALAPPDATA%\NVIDIA\DXCache; AMD: %LOCALAPPDATA%\AMD\DxcCache / DxCache);
    • then either xcl shaders <your title's shader pack> --precompile --pipelines, or a cold launch of your title with preparation on;
    • other programs closed.
  • Order: off, on, off, on, on the same title.
  • What to paste back:
    • preparation time (the precompiling … run's end time, or the launch's preparation line);
    • the literal lifting: N families, M compute programs lifted (T s) line (on runs);
    • number and total time of driver compile start / finish lines for compute PSOs (or XCL_ACTIVITY=1 totals);
    • if you play after: frame time or a 60 s scripts/smoke.ps1 -Seconds 60 block on both sides, with XCL_PRESS_AT=25:A;
    • GPU + driver, CPU, RAM, Windows build.
  • Our environment: NVIDIA GeForce RTX 2070 SUPER, driver 621.40, AMD Ryzen 5 3600XT, 32 GB, Windows 11 Home build 26300, rustc 1.100.0.
  • Most needed: AMD RDNA2/3/4, and any wave64 adapter.

Written by Claude Code, Claude Opus 5.5.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory 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:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test) needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 29, 2026
@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Cold full preparation, XCL_LIFT_LITERALS=1 vs off (one run each, NVIDIA)

Two cold runs of full shader preparation (XCL_PREPARE_ALL=1) on a retail game, each with a fresh XCL_DXIL_CACHE and the NVIDIA and D3D shader caches cleared. The only difference between them is this PR, switched on.

Lifting was active in B: literal lifting: 112 families, 268 compute programs lifted (10.8 s), and no fallbacks were logged.

A (lifting off) B (lifting on)
translation + DXC phase finished 54.4 min 56.1 min
driver phase, records checked at 60 min 22,474 22,337
at 90 min 23,003 22,970
at 115 min 23,601 23,448
at stop 23,624 of 26,503 (117 min) 23,614 of 26,503 (120 min, cap)
driver blobs in the cache at stop 4,981 4,868
  • Fewer compiles, no time saved. The lifted families did share driver compiles: 113 fewer distinct blobs at about the same point. But preparation got no faster. After about 60 minutes the driver phase is a slow tail of roughly 10–25 pipelines a minute in both runs.
  • Neither run finished. About 2,900 pipelines were still left in each, so total preparation time is not measured.
  • The 19 % estimate in the description doesn't hold for full preparation. It came from a cold gameplay run, where the 13 constant-only compiles happened to be expensive ones. Across the whole pack they are a small share of the driver phase.
  • This supports keeping lifting off by default. On NVIDIA preparation the gain is small, possibly zero.

Limits

  • One run per side. Run A also overlapped with low-priority cargo builds and was still marginally ahead, so a difference this small is within noise.
  • NVIDIA only. AMD's per-pipeline driver cost may differ, so results from RDNA would help.

Setup

  • Hardware: RTX 2070 SUPER (driver 32.0.16.2140), Ryzen 5 3600XT, Windows build 26300, release build.

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

@bmjask

bmjask commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Current-main audit (2026-11-29): discard as an experiment, not a merge candidate.

The PR's own cold full-preparation comparison is decisive: it produced 113 fewer blobs around the same point but saved no wall time (54.4 vs 56.1 min for translation/DXC, and the driver tail remained effectively unchanged). The change is 679 additions across nine files, adds per-PSO runtime metadata/ABI uploads and a custom HLSL scanner, and current main now defaults to direct DXIL, where this HLSL-only lifting does not apply.

That complexity is not justified by the measured result. Keep the analysis/data, but close the implementation unless a narrowly targeted gameplay-critical family demonstrates a reproducible critical-path win.

Cache impact: not safe for affected cached PSOs; it intentionally rewrites HLSL/DXIL and shares a different driver blob.

Review assisted by Codex; exact model name was not exposed in this session. No additional cold run was performed.

…ALS)

A title's pack has compute programs that are one program with different
numeric constants. Their HLSL differs only in those literals, so DXC and
the driver compile each again. In a cold run of a retail game on an
RTX 2070 SUPER, 13 such repeat driver compiles took 253 s of the 1,327 s
spent in the driver (19 %). Pack-wide: 127 families, 312 programs, 156
extra compiles out of 2,682 compute programs (5.8 %), e.g. records
b162ea7db32e1657 / 0ed9e58ef6dc9cdb (462 KB of DXIL each).

With XCL_LIFT_LITERALS=1, preparation (`xcl shaders --precompile` and
launch preparation) finds the families. Programs with the same
instruction sequence are translated, and those whose HLSL is the same
once literals are masked form a family. Only the literal positions that
differ inside a family are lifted: plain `u` integer literals in main's
body, never attributes, declarations, control flow, `[]` or texture
offsets. A lifted literal reads the same uint from io after the SGPRs
(lift::IO_LIFTED), so the program computes the same thing. Each member's
lifted text is one text without comments, so the DXC cache (by text)
and the driver blob cache (by DXIL) share it. A pipeline keeps its
values, and both compute dispatch paths (bind.rs, console_list.rs)
write them after the SGPRs. The table is kept next to the caches under
the translation cache identity. A family member skips the translated
disk cache, which keeps no lifted values. If DXC refuses a lifted text,
the program compiles as it is.

Off by default until cold-run numbers show the gain and no frame-time
cost (a lifted literal is a uniform io load instead of a folded
constant).

Related: xcl-dev#2711 (identical split pieces share a container), xcl-dev#2088.

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head c0a0ec0, diff read, not a merge-check; draft, conflicts with main, PR description red):

  • No measured benefit, and less reach than when it was written. The author's own cold run made 113 fewer driver blobs in about the same wall time. Since then, main sends compute through direct DXIL by default (kernel::enabled()), and lifting only works on the HLSL path.
  • The conflict isn't only textual. Main added an edited -> compile_cached_in branch in pso.rs compile_console_shader that lifting has to respect, and removed finish_two_pass. After that, shaders.rs ~2440 (a new Compiled { on main) still lacks lifted and won't compile.
  • The lifted path has never run in a title. The values are written only in bind.rs ~5265 and console_list.rs ~1065, and no test covers those dispatch writes. Any other writer of a compute program's io would leave the lifted words unset (unverified that one exists).
  • Unmeasured cost: up to 64 io loads per program, DXC loses constant folding, and lifted members bypass the disk cache (pso.rs ~272), so they retranslate on every launch.
  • The lift table is read once per process (lift.rs ~318). A title whose first lookup comes before the preparation process writes the table never lifts in that run.
  • The red CI is a 0xc0000005 in the eh / agility tests, before any lift test ran, which looks like Intermittent access violations during parallel library tests on Windows 11 #1487 (unverified). The smoke runs are from 1d3b1cb with lifting off.

Fine: the design holds. Only differing u integer literals in plain statements of main are lifted, and values come from each member's own HLSL under a masked hash. A uint stays a uint, constant-required contexts are excluded, a lifted text DXC rejects falls back to the plain one, the sample test uses XCL_SAMPLE_*, and it's off by default. If it goes on, a split fits: the Compiled.lifted and dispatch plumbing, then lift.rs with its tests, then the wiring.

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 c0a0ec0

No open or merged PR lifts differing literals of a compute family. #2711 (named in the description) shares identical split pieces only.
No problems found in the parts I read: the lifted literals go at io word KERNEL_SGPRS (512 bytes), and both dispatch paths (bind.rs and console_list.rs) append them after io has been resized to KERNEL_SGPRS. The switch is off by default. The branch conflicts with main and its checks fail.
Recommendation: keep open.

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

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

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory 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:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants