Skip to content

Faster shader compiles: one window switch per four dwords, every core, --precompile builds the cache titles use - #113

Merged
ivall merged 3 commits into
xcl-dev:mainfrom
Foniass:t3/faster-shader-compile
Nov 23, 2026
Merged

ivall merged 3 commits into
xcl-dev:mainfrom
Foniass:t3/faster-shader-compile

Conversation

@Foniass

@Foniass Foniass commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Shader compile time. A title's ~24 000 shaders took 30–40 min, so I looked at where DXC spends its time. Measured with the retail shaders.rpf, translated the way a title translates them.

What was slow

  1. Memory-window switches. With a title's unified memory, every mem access is a [branch] switch between the 4 windows (xcl_m*), and the translator emitted one per dword. An s_load_dwordx8 of a descriptor was 8 switches, and an s_buffer_load_dwordx16 was 16 switches plus 16 ?: branches (HLSL 2021's ?: is a branch). In a 1 % random sample (253 programs), 88 % of the switches came from scalar loads. Translated the way a title translates them (4 windows), the same programs took DXC 2.3× as long as with one window, and the slowest went from 24 s to 130 s.
  2. 6 compile threads. MAX_COMPILE_THREADS = 6 capped DXC and the PSO workers. A 16-thread CPU sat mostly idle.
  3. --precompile built the wrong cache. The DXIL cache is keyed by the HLSL. --precompile translated with 1 window and no split wave64 form, but a title translates VS / PS / CS with its 4 windows (and on a 32-lane GPU, compute in the split form). So a title asked for other HLSL and compiled everything again.

Changes

  • Emitter (emit.rs):
    • s_load: Load4 / Load2 for the contiguous dwords.
    • s_buffer_load and buffer_load: bounds checks as selects. A dword that is out of range, or belongs to an inactive lane, reads offset 0 (always inside the window) and is discarded. Four dwords share one switch (xcl_loadx4).
    • ld_raw: one Load4 plus one Load, with no branch.
    • Image results, v_interp, s_cselect / s_cmov / s_ff1 / s_flbit, v_bfm and DPP lane sources: select instead of if / ?:.
  • Threads (dxc.rs, pso_async.rs): every core but one (XCL_DXC_THREADS overrides). wait_for_memory now wants 6 GB of commit plus 1 GB per compile already running before it lets another start right away. Peak DXC commit on the pack's biggest translations is about 0.7 GB (typically 0.1–0.3 GB), so "gigabytes each" no longer holds. Priority stays normal: a title thread can wait for a pipeline (resolve_wait, a compute pipeline's first bind) and must not end up waiting behind its own threads. The driver compiles on the xcl-pso workers are still not memory-gated.
  • --precompile (shaders.rs): translates exactly as pso does: 4 windows for VS / PS / CS, 1 for MS, and the split form when the GPU runs only 32-lane waves. --windows N and --split 0|1 override. There are also measurement helpers: XCL_DUMP_HLSL=<dir> (with --recomp) writes every XCL_DUMP_EVERY-th translation, XCL_RECOMP_WINDOWS translates with that many windows, and XCL_RECOMP_ONLY=<hash> translates one program.

Numbers (Ryzen 7 7800X3D, 8 cores / 16 threads, DXC 1.9.2602)

main this PR
1 % random sample, standalone dxc.exe CPU time (4 windows) 771 s 424 s
slowest program in that sample 130 s 33 s
--precompile, first 10 % of the pack (2 650 records), 4 windows, wall time 327–378 s (6 threads) 176 s (15 threads)
same, CPU time at 6 threads 2 257 s 1 922 s
  • Thread count: most of the wall-time gain comes from here.
  • Emitter: how much it saves depends on the mix. It saves 45 % on the random sample, which the big compute programs dominate, and 15 % on the first 10 %.
  • Variance: main measured 327 s and 378 s on two runs of the same slice, so wall-time differences of a few percent between runs are noise.
  • Full pack: I didn't time it end to end.

Checked

  • Tests: cargo test --workspace --release on an RTX 5070 Ti: 527 passed, 0 failed (2 no wave64 skips, as on main). The GPU tests also pass with XCL_SPLIT_WAVE64=1.
  • New test: gpu_grouped_loads_bounds_and_inactive_lanes covers an unaligned s_load_dwordx8, s_buffer_load_dwordx8 / x16 / x2 straddling num_records, and buffer_load_dwordx3 (idxen) / x2 (offen) with lanes past the end and inactive lanes that keep their registers.
  • Window switch: gpu_memory_windows_above_4_gib gains an s_buffer_load_dwordx4 from window 1, which goes through the multi-window xcl_loadx4.
  • Whole pack: all 26 698 programs translate (100 %, 0 errors). Every shader in the 10 % precompile compiled with 0 DXC errors.
  • Rebase: rebased on 58309b9. v_interp keeps its (float) conversion as a select, and ps_interp_converts_integer_inputs_and_compiles passes.
  • Minecraft: menu renders correctly with the new translations, 60 fps.

Found along the way, not in this PR

  • 13 % of the programs (in the sample) cannot be structured and run as the pc dispatcher, which costs DXC about 1.5× per instruction. Most of them are cold blocks that the console compiler placed after the program, which jump back into it. In address order those look like crossing loops. I have a layout pass that moves them next to their branch; it is not measured enough yet to propose.
  • --precompile still differs from the runtime in two places that predate this PR. A PS's static samplers start after its VS's at run time, but at 0 in --precompile. And the runtime builds the mesh pass-through for its pixel shader, while --precompile builds it without one.
  • translate_mesh / translate_geometry translate with 1 memory window, while VS / PS / CS get the title's 4. Is it intended that NGG and GS programs only reach window 0?

🤖 Generated with Claude Code

Foniass and others added 3 commits November 23, 2026 13:44
…ches for loads

With a title's unified memory every access to it is a switch between the four
windows (xcl_m*), and HLSL 2021's ?: is a branch. Scalar loads made one switch
and one branch per dword (an s_load_dwordx8 of a descriptor: 8 switches), and
every vector load, image result and interpolation sat in an if (exec).

- s_load: Load4 / Load2 for the contiguous dwords
- s_buffer_load / buffer_load: bounds checks as selects; a dword out of range or
  of an inactive lane reads offset 0 (always in the window) and is dropped; four
  dwords share one switch (xcl_loadx4)
- ld_raw: one Load4 + one Load, no branch
- image results, v_interp, s_cselect / s_cmov / s_ff1 / s_flbit / v_bfm / DPP
  lane sources: select instead of if / ?:

A 1 % sample of a retail pack (253 programs) translated as a title translates
them (4 windows): DXC 771 s -> 424 s of CPU, the slowest program 130 s -> 33 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
DXC and the PSO workers were capped at 6 threads for memory. A retail pack's
biggest translations now take about 0.7 GB in DXC (typical 0.1-0.3 GB).
wait_for_memory now wants 6 GB of commit plus 1 GB per compile already running
before it lets one more in at once. XCL_DXC_THREADS sets the count. The workers
stay at normal priority: a title thread can wait for a pipeline (resolve_wait, a
compute pipeline's first bind) and must not wait behind its own threads.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The DXIL cache is keyed by the HLSL. --precompile translated with one memory
window and no split wave64 form; a title translates VS / PS / CS with its four
windows (pso::mem_windows) and, on a GPU with 32-lane waves, compute in the
split form, so it compiled everything again. --windows N and --split 0|1
override.

Also for measuring DXC on a pack: XCL_DUMP_HLSL=<dir> (with --recomp) writes
every XCL_DUMP_EVERY-th complete translation, XCL_RECOMP_WINDOWS translates
with that many windows, XCL_RECOMP_ONLY=<hash> translates one program.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Foniass
Foniass force-pushed the t3/faster-shader-compile branch from 500f4d2 to 517d983 Compare November 23, 2026 12:46
@ivall
ivall merged commit f311ca5 into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged (f311ca5) – great work, thanks.

Checked on an RX 9070 (16 threads), DXC from SDK 10.0.26100, same random sample (seed 12345, 1 in 100 records, 258 variants) translated with 4 windows (XCL_SURVEY_WINDOWS=4):

main (58309b9) this PR
DXC CPU time, total 1 018 s 422 s
programs with pc blocks (43) 900 s, mean 20.9 s 322 s, mean 7.5 s
slowest (CS_RaytracingResolve_c5cb017e) 170 s 42 s
errors 0 0
  • Whole pack still translates 26 698 / 26 698; cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1.
  • XDefiant renders its title screen (text included) with this merged.
  • The first version conflicted with 58309b9 (int / uint PS inputs converted again – XDefiant's UI text, GPU recomp: fix PS interp ints, wave op EXEC zeroing, ALU edge cases, and BVH det underflow #107 regression); your rebase kept the conversion in the select form, which is what I had resolved to as well.
  • Agreed on normal priority: a title thread can wait for a pipeline (resolve_wait).
  • On your question: I don't know of a reason for MS / GS to translate with 1 window – most likely an oversight from when the windows came in (an MS / GS reading title memory above window 0 would read the wrong memory). A PR with a test would be welcome; note it changes those programs' HLSL (cache).

@Foniass

Foniass commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

One more measurement: the NVIDIA driver's own compile (pipeline creation) next to DXC's, on the 1 % random sample's 137 compute programs. These are CS plus the compute halves of MS, translated as on a 32-lane GPU (4 windows, split wave64). Times are single-threaded on an RTX 5070 Ti, driver 621.14.

main emitter this PR
DXC 222 s 184 s (−17 %)
driver, CreateComputePipelineState, cold 80.5 s 73.6 s (−9 %)
  • Cold: a harmless salt store in each program kept the driver's shader cache from answering.
  • Excluded: 20 programs with static samplers didn't build, because the test root signature has none. That is my harness, not XCL; its root signature in a title has them.
  • Takeaway: on a cold cache the driver adds about 40 % on top of DXC for these programs, and it also gets a bit faster with this PR.
  • Measurement code: runner::compute_pso_seconds plus an ignored test that reads XCL_PSO_BENCH_DIR. It is not in this PR; I can add it if useful.

hexgeon1 added a commit to hexgeon1/xcl that referenced this pull request Nov 23, 2026
…de after a minute

`wait_for_memory` (xcl-dev#113) let a compile start with 6 GB of commit left plus
1 GB per compile already running. The available commit already excludes what
the running compiles use, so that counted them twice: on a desktop with
8.1 GB of commit free (a browser and chat open) about three compiles ran and
the other workers polled.

- `may_start`: in at once while commit covers 6 GB + 300 MB per running
  compile (their growth still to come; a typical retail compile peaks at
  0.1-0.3 GB per xcl-dev#113's measurements); with less, one at a time.
- After a minute's wait a compile still goes in when commit is short, but one
  per minute; before, every waiting worker went in at once - when commit was
  shortest.
- The running count only rises from the value the decision saw (compare and
  swap), so workers deciding on the same reading cannot all go in.
- Waiters wake when a compile ends (condition variable) instead of polling
  every 50 ms.

Minecraft Preview's 716 programs, 4 memory windows, 15 threads (default),
Ryzen 7 5700X, 8.1 GB of commit free, main and this interleaved, 3 runs each:
12.7-15.2 s -> 6.35-6.71 s wall (5.3-5.6 s with no gate at all); DXIL
digest identical. Tests: `gate_tests` (the rule, short commit / one escape
per interval, 32 threads never exceeding what the commit allows);
`cargo test --workspace --release` green on an RTX 3090 with the Agility SDK
(0 GPU skips). Driver PSO compiles are still not gated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants