Skip to content

Shaders: xcl shaders --explain reports a program's guest code and direct DXIL - #1955

Draft
sombraSoft wants to merge 3 commits into
xcl-dev:mainfrom
sombraSoft:shaders/explain
Draft

sombraSoft wants to merge 3 commits into
xcl-dev:mainfrom
sombraSoft:shaders/explain

Conversation

@sombraSoft

@sombraSoft sombraSoft commented Nov 24, 2026 •

Copy link
Copy Markdown

Stacked on #1954; only the top commit belongs here. Part of #1269.

Numbers in #1269 come from different commits, program sets and cache states, so they rarely compare, and finding what makes one shader expensive is done by hand. xcl shaders <pack> --explain reports, per console program picked by --hash or a seeded --sample: the guest program (instruction classes, blocks, loops, back edges, indirect jumps) and the direct translator's time, memory and DXIL structure (#1954), or why it refused. Counts are portable (equal for the same commit and options; a fingerprint and a digest line say so), times and memory local. Programs are named by hash only, so a report can be posted. XCL caps its own commit during the run (--mem-gb, default 8) with a job object, since the direct translator runs in-process. The HLSL path (#1957) and JSON / comparing reports (#1958) follow.

What is in it

  • Shaders: xcl shaders --explain reports a program's guest code and direct DXIL – crates/xcl/src/shaders/explain.rs, explain/direct.rs, explain/limits.rs; the hook in shaders.rs; help in main.rs; README.md

About 480 lines of code and 100 of tests; the core without a translation path would report the guest program only, so the direct path is in the same PR.

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 21aee9d: 983 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: instruction classes; selection by hash (and an unknown hash is an error); the same program explained twice gives the same portable numbers
  • retail pack (shaders.rpf), on fb2eeec (the top of this stack): the largest compute program goes through the direct translator in 0.97 s and 172 MB, 3.8 MB of DXIL bitcode with 36 818 blocks

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 (21aee9d):

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

Not tested

  • AMD and Intel GPUs, GPUs with native wave64, DXC 1.9, Linux / vkd3d-proton
  • the title run does not use --explain (a CLI report); it checks only that nothing else changed
  • the driver cost of the direct DXIL (36 818 blocks above): --measure-pso needs Windows Developer Mode, off on this machine

AI use

  • Claude Code with Claude Opus 5.5: wrote the code, 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.
…ect DXIL

Part of xcl-dev#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.
@DavidM1911

Copy link
Copy Markdown
Contributor

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.

@throny throny added the goal:world-load Bears on the retail title getting past world loading label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants