Shaders: --explain reports the HLSL path: HLSL, DXC per pass, DXIL #1957

Open
github-bot wants to merge 6 commits from github-bot/gh-1957 into main AGit
Owner

@⁠sombraSoft on GitHub · 2026-11-24 23:51 UTC · original
sombraSoft:shaders/explain-hlsl → main · +2152 −21 · 12 files · 6 commits · draft

Stacked on #1955 and #1956; the top two commits belong here. Part of #1269.

--explain reports the HLSL path too: the HLSL by the console instruction that wrote it, DXC's time per pass and peak commit, and the DXIL's structure, next to the direct path. --path both|hlsl|direct, --opt, --top.

Pass times need a process per compile: XCL's in-process DXC prints -ftime-report only at exit, summed over every compile. So each program is compiled by the SDK's dxc.exe next to the dxcompiler.dll XCL loads, with dxc::arguments (#1956), in a job object with a commit limit (--mem-gb, default 8) and stopped after --dxc-timeout (default 600 s). Without a limit, on main at c160d86 the pack's largest compute program (then 147 MB of HLSL, -O0) ran DXC for over 40 minutes up to 13 GB and grew the page file by about 88 GB. A compile past a limit is now stopped and reported as unfinished.

What is in it

  • Shaders: --size-by-op splits translated HLSL bytes by instruction – by @⁠DTsordas (DTsordas/xcl@5529418, posted in #1269 and never opened as a PR), kept as their commit; shaders.rs
  • Shaders: --explain reports the HLSL path: HLSL, DXC per pass, DXIL – crates/xcl/src/shaders/explain/hlsl.rs, hooks in explain.rs, limits.rs; help and README

Tested

  • NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.2124), AMD Ryzen 9 5900X, Windows 11 Pro 10.0.26300, rustc 1.97.1; tests in debug, smoke run in release; DXC 1.8.2502.11 (Windows SDK 10.0.26100)
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on b73a434: 987 passed, 0 failed, 45 ignored
  • SKIPPED (all from existing tests): 18 × no wave64, 1 × no heap windows, 1 × cannot disable the driver cache (Developer Mode off), 3 × per-lane form refused (f16 / VOP3 modifiers)
  • unit tests: the pass table of a timing report is summed by pass name; dxc.exe past its time limit or under a 1 MB commit limit is stopped with timeout: / memory:; the determinism test now covers both paths
  • retail pack, on fb2eeec (the top of this stack): 20 sampled programs (--sample 20 --seed 1) take 56 s, 54 s of it in DXC; two runs give the same portable digest. The largest compute program: 15 MB of HLSL, DXC -O0 120 s at 2.7 GB of commit (Loop-Closed SSA 24 %, DXIL Remove Dead Blocks 16 % of the pass time), 80 295 DXIL blocks; the direct path gives 36 818

Regression run

main:

xcl-smoke v1
commit: 689f2db477c3c062cf162649781640ef8a977c18
gpu: NVIDIA GeForce RTX 3060 Ti
title: 55118d47
seconds: 60
exit: 0x0
frames shown: 1075 (PresentX 1075)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15431 MB

this PR (b73a434):

xcl-smoke v1
commit: b73a4343dd8b096ecdb008b157b6161bd64a635f
gpu: NVIDIA GeForce RTX 3060 Ti
title: 55118d47
seconds: 60
exit: 0x0
frames shown: 1156 (PresentX 1157)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15341 MB

Not tested

  • AMD and Intel GPUs, GPUs with native wave64, DXC 1.9, Linux / vkd3d-proton
  • the title run does not use --explain; it checks only that nothing else changed
  • dxc.exe missing next to dxcompiler.dll (e.g. XCL_DXC_DIR with the DLLs only): the in-process fallback without pass times was run, but not on the pack

AI use

  • Claude Code with Claude Opus 5.5: wrote the code (except the --size-by-op commit), the tests and this description. The tests, the pack runs and the smoke runs were run on the hardware above.
**@⁠sombraSoft** on GitHub · 2026-11-24 23:51 UTC · [original](https://github.com/xcl-dev/xcl/pull/1957) `sombraSoft:shaders/explain-hlsl` → `main` · +2152 −21 · 12 files · 6 commits · draft Stacked on #1955 and #1956; the top two commits belong here. Part of #1269. `--explain` reports the HLSL path too: the HLSL by the console instruction that wrote it, DXC's time per pass and peak commit, and the DXIL's structure, next to the direct path. `--path both|hlsl|direct`, `--opt`, `--top`. Pass times need a process per compile: XCL's in-process DXC prints `-ftime-report` only at exit, summed over every compile. So each program is compiled by the SDK's `dxc.exe` next to the `dxcompiler.dll` XCL loads, with `dxc::arguments` (#1956), in a job object with a commit limit (`--mem-gb`, default 8) and stopped after `--dxc-timeout` (default 600 s). Without a limit, on main at c160d86 the pack's largest compute program (then 147 MB of HLSL, `-O0`) ran DXC for over 40 minutes up to 13 GB and grew the page file by about 88 GB. A compile past a limit is now stopped and reported as unfinished. **What is in it** - `Shaders: --size-by-op splits translated HLSL bytes by instruction` – by @⁠DTsordas (DTsordas/xcl@5529418, posted in #1269 and never opened as a PR), kept as their commit; `shaders.rs` - `Shaders: --explain reports the HLSL path: HLSL, DXC per pass, DXIL` – `crates/xcl/src/shaders/explain/hlsl.rs`, hooks in `explain.rs`, `limits.rs`; help and README **Tested** - NVIDIA GeForce RTX 3060 Ti (driver 32.0.16.2124), AMD Ryzen 9 5900X, Windows 11 Pro 10.0.26300, rustc 1.97.1; tests in debug, smoke run in release; DXC 1.8.2502.11 (Windows SDK 10.0.26100) - `cargo test --workspace --no-fail-fast -- --test-threads=4` on b73a434: 987 passed, 0 failed, 45 ignored - `SKIPPED` (all from existing tests): 18 × no wave64, 1 × no heap windows, 1 × cannot disable the driver cache (Developer Mode off), 3 × per-lane form refused (f16 / VOP3 modifiers) - unit tests: the pass table of a timing report is summed by pass name; `dxc.exe` past its time limit or under a 1 MB commit limit is stopped with `timeout:` / `memory:`; the determinism test now covers both paths - retail pack, on fb2eeec (the top of this stack): 20 sampled programs (`--sample 20 --seed 1`) take 56 s, 54 s of it in DXC; two runs give the same portable digest. The largest compute program: 15 MB of HLSL, DXC `-O0` 120 s at 2.7 GB of commit (Loop-Closed SSA 24 %, DXIL Remove Dead Blocks 16 % of the pass time), 80 295 DXIL blocks; the direct path gives 36 818 **Regression run** main: ``` xcl-smoke v1 commit: 689f2db477c3c062cf162649781640ef8a977c18 gpu: NVIDIA GeForce RTX 3060 Ti title: 55118d47 seconds: 60 exit: 0x0 frames shown: 1075 (PresentX 1075) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15431 MB ``` this PR (b73a434): ``` xcl-smoke v1 commit: b73a4343dd8b096ecdb008b157b6161bd64a635f gpu: NVIDIA GeForce RTX 3060 Ti title: 55118d47 seconds: 60 exit: 0x0 frames shown: 1156 (PresentX 1157) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 15341 MB ``` **Not tested** - AMD and Intel GPUs, GPUs with native wave64, DXC 1.9, Linux / vkd3d-proton - the title run does not use --explain; it checks only that nothing else changed - `dxc.exe` missing next to `dxcompiler.dll` (e.g. `XCL_DXC_DIR` with the DLLs only): the in-process fallback without pass times was run, but not on the pack **AI use** - Claude Code with Claude Opus 5.5: wrote the code (except the --size-by-op commit), the tests and this description. The tests, the pack runs and the smoke runs were run on the hardware above.
A compile-cost report needs to read the DXIL that DXC emits, and the only
reader in the tree (bitreader_tests.rs) handles the unabbreviated records
our own writer produces. DXC's LLVM 3.7 writer uses abbreviations defined
in BLOCKINFO and locally, char6 arrays and blobs.

BitReader walks the bitstream one entry at a time (enter block, end block,
record) with the block stack on the heap, so nesting depth costs no
recursion. BLOCKINFO is consumed internally; skip_block jumps over blocks a
caller does not need. Every count read from the stream (operands, array
elements, blob bytes, block lengths) is checked against the bits left in
its block before anything is read or allocated, and bitcode that is not
whole 32-bit words is refused, so corrupt input is an Err, not a panic or
a large allocation.

Tested: a hand-assembled stream with a BLOCKINFO abbreviation (literal
code, fixed, VBR, char6 array) and a local one (fixed code, blob) decodes
to the expected records; every prefix and every single-bit flip of it
reads without a panic.
To explain why a shader is expensive for the driver's compiler, a report
needs the shape of its DXIL: functions and declarations, basic blocks,
instructions and SSA values, phis and their incoming edges, branches,
switches and back edges, selects, memory operations, and calls per
callee (dx.op.* name).

container_stats reads the DXIL part of a DXBC container, bitcode_stats
raw bitcode (optionally in the 0x0B17C0DE wrapper). One pass over the
bitstream: the type table, global, function and constant records give
the module-level value ids; function arguments, function constants and
value-producing instructions continue them per body, so the relative
callee operand of a call resolves to its function and name. A call
produces a value when its function type (explicit in the call or the
callee's) returns non-void. Metadata, attributes, use lists and
function symbol tables are skipped by block length. Output maps are
BTreeMaps, so identical input gives identical stats and JSON.

Tested:
- two modules built with our writer (branches, a loop with a forward-
  referencing phi, select, calls to a value and a void dx.op; a switch,
  global, alloca, gep, load, stores) give exactly the expected stats;
- three DXC 1.8.2502.11 compute shaders (a loop, nested branches with a
  three-way phi, a switch with buffer loads/stores) match counts read
  from DXC's disassembly: functions, declarations, blocks, instructions,
  values, phis, phi edges, br/cond br, switch cases, back edges, select,
  loads, stores, allocas, calls per callee;
- every prefix and 3000 byte-flipped copies of a DXC bitcode, its
  container and a writer module return Ok or Err without a panic;
- release build: 20.6 MB of writer bitcode (3M instructions) reads in
  86 ms; a 1.5 KB DXC module 10000 times in 97 ms.
Part of #1269. Numbers in that thread come from different commits, program
sets and cache states, and finding what makes one shader expensive is done
by hand. --explain reports, per console program picked by hash or as a
seeded sample: the guest program (instruction classes, blocks, loops, back
edges, indirect jumps) and the direct translator's time, memory and DXIL
structure (blocks, phis, values, calls; gpu::dxil::stats), or why it
refused the program.

Counts are portable: equal for the same commit and options, which a
fingerprint and a digest line state. Times and memory are local. Programs
are named by hash only. XCL caps its own commit for the run (--mem-gb,
default 8) with a job object, as the direct translator runs in-process; the
HLSL path, JSON output and comparing reports come in later changes.
compile_here built DXC's argument list inline (entry, profile, HLSL 2021,
-O0 above 8 MB of HLSL and -O1 above 1 MB, then the caller's). arguments()
returns that list, so xcl shaders --explain can run dxc.exe with exactly
the arguments a title's compile uses. No change in what DXC is given.

#1529 makes the same function (private, plus -disable-lifetime-markers);
the two merge by keeping its body.
`xcl shaders --recomp --size-by-op` attributes every byte of each translation
to the console instruction that produced it (the emitter's `// <pc>:` lines
mark where each instruction starts), plus the prologue, the listing lines
themselves and block structure. Part of #1269 (task A1 on the campaign board).

Retail pack (26 698 programs, 9 665.7 MB of HLSL): the `// <pc>:` listing
lines are 2 461 MB (25.5 %), the prologue 848 MB (8.8 %), v_mul_legacy_f32
7.5 %, v_fmac_f32 5.5 %, v_fma_f32 3.3 %, SMEM / buffer loads 400-1700 B each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The HLSL path's cost, per program: the HLSL by the console instruction
that wrote it (--size-by-op's attribution), DXC's time per pass and peak
commit, and the DXIL's structure. --path picks one or both paths; --opt
overrides the -O level.

Pass times need a process per compile: XCL's in-process DXC prints
-ftime-report only at exit, summed over every compile. So the program is
compiled by the SDK's dxc.exe next to the dxcompiler.dll XCL loads, with
dxc::arguments (what a title's compile passes), in a job object with a
commit limit (--mem-gb, default 8) and stopped after --dxc-timeout
seconds (default 600). Without the limit, a retail program of 147 MB of
HLSL ran DXC for over 40 minutes up to 13 GB of commit and grew the page
file by about 88 GB; with --mem-gb 4 it stops after 30 s and is reported
as unfinished.

Retail pack, RTX 3060 Ti, DXC 1.8.2502.11, 20 sampled programs: 58 s, 96 %
of it in DXC; two runs give the same portable digest.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:11:29 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 02:59 UTC · original

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main (README.md + crates/xcl/src/shaders.rs) — needs a rebase before it can be tested.

**@⁠DavidM1911** on GitHub · 2026-11-25 02:59 UTC · [original](https://github.com/xcl-dev/xcl/pull/1957#issuecomment-7151475886) Verification on RTX 3060 Ti (Windows 11): **merge conflict with current `main`** (`README.md` + `crates/xcl/src/shaders.rs`) — needs a rebase before it can be tested.
This pull request has changes conflicting with the target branch.
  • README.md
  • crates/xcl/src/gpu/recomp/dxc.rs
  • crates/xcl/src/shaders.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1957/head:github-bot/gh-1957'
git switch 'github-bot/gh-1957'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1957'
git switch 'github-bot/gh-1957'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1957'
git switch 'github-bot/gh-1957'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1957'
git switch 'main'
git merge --squash 'github-bot/gh-1957'
git switch 'main'
git merge --ff-only 'github-bot/gh-1957'
git switch 'main'
git merge 'github-bot/gh-1957'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.