Skip to content

Recompiler: the DXIL cache key leaves out comments and indentation - #3241

Closed
KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:recompiler-dxc-key-code-lines
Closed

KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:recompiler-dxc-key-code-lines

Conversation

@KOLESNiii

@KOLESNiii KOLESNiii commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

A cold shader preparation runs DXC on byte-identical output many times. On a retail game's pack, a cold full preparation wrote 16,152 DXIL files with only 9,271 distinct contents: 43 % of its DXC compiles produced DXIL the cache already had. Each console record starts with two per-record data words, shown in the HLSL as // 0000: v_nop ; lit 0x… / // 000c: … comments. The DXIL cache key hashed the full HLSL text, so programs with identical code got different keys.

For the HLSL XCL generates (dxc::compile_cached: the recompiler's and emit's programs), the key now hashes only the code lines: comments outside string literals become a space, lines are trimmed and empty lines dropped. DXC still compiles the full text, so its output is unchanged. A generated source keeps the full-text key (the same key as before) when:

  • it is 960 KiB or longer: from 1 MiB on, the source length, comments included, picks the -O level;
  • line numbers could reach the DXIL: it uses __LINE__ or a #line directive;
  • it has a backslash before a line break, with or without spaces/tabs between, or a ??/ trigraph: DXC splices the next line into a // comment.

Any other HLSL keeps the full-text key: compile_cached_in (a caller's own source) and generated HLSL that a debug hook edited (XCL_DEBUG_PS_SUB, XCL_DEBUG_CS_SUB, XCL_DEBUG_PS_EXPR, XCL_DEBUG_LOD0, the debug colour). The line-number and splice cases were found in review by @thehollowedone. The driver cache was already keyed by the DXIL bytes, so this removes repeated DXC work only, not driver compiles.

Builds on the idea of #1582 (closed), which removed comments to choose the -O level but kept the full-text key. Independent of #2928, which skips repeated identical-HLSL jobs inside precompile(). This change catches different HLSL with the same output and doesn't touch precompile(). Part of #2088.

What is in it

  • crates/xcl/src/gpu/recomp/dxc.rs:
    • cache_key_for_toolchain / generated_cache_key: code-line key for compile_cached, guarded by code_lines_keyable and KEY_CODE_LINES_BELOW;
    • cache_key: full-text key for compile_cached_in;
    • XCL_DXC_SOURCE_DUMP=<dir> (diagnostic, off unless set): writes each source DXC is given to <dir>/<key>.<profile>.hlsl. This is how the offline check's 1,601 sources were dumped, so anyone can recheck which sources share a key.
    • tests: cache_key_leaves_out_comments_and_indentation, cache_key_distinguishes_line_macro_after_comment and cache_key_distinguishes_whitespace_line_splice (the reviewer's), only_sources_whose_line_structure_cannot_reach_the_dxil_drop_their_comments, a_program_compiled_differently_misses_the_cache_of_its_comment_twin (DXC: B compiled after A into the same empty cache misses, and gets the DXIL of an independent compile), cache_key_of_a_long_source_keeps_its_comments, a_linked_program_stays_within_the_code_line_key_margin.
  • crates/xcl/src/gpu/recomp/pso.rs: HLSL edited by a debug hook goes through compile_cached_in.
  • crates/xcl/src/shaders.rs: the pack proof records generated_cache_key, the key compile_cached uses.
  • crates/xcl/src/gpu/recomp/emit.rs: mem_accessors becomes pub(super) for the linked-program test.

Tested

  • NVIDIA GeForce RTX 2070 SUPER (driver 32.0.16.2140), AMD Ryzen 5 3600XT, Windows build 26300, rustc 1.100.1
  • cargo build --workspace --tests && cargo test --workspace --no-fail-fast -- --test-threads=4 on 84bcc5e (debug; rebased onto main 81bfbf0, run at the same time as the suites of Recompiler: lift literals that differ inside a family (XCL_LIFT_LITERALS) #3262, D3D12.X: port @Saksukella's write-watch changes #2754 and #2891 to main #3242 and Recompiler: the DXIL cache key leaves out comments and indentation #3241): 2735 passed, 8 failed, 49 ignored
    • 4 of the failures are the e2e target, whose test title had not been built (missing …xcl_testtitle.exe – build first: cargo build --workspace); the other 4 are hle::d3d12x::title_textures tests (a page-count assertion: 11 MiB measured, 16 MiB expected), in code this PR does not touch, which passed in the other two suites run at the same time
    • rerun alone after cargo build --workspace: cargo test -p xcl --test e2e: 5 passed, 0 failed; cargo test -p xcl --lib -- hle::d3d12x::title_textures::tests: 65 passed, 0 failed, 1 ignored
    • Together: 2743 passed, 0 failed, 49 ignored
  • SKIPPED on this adapter (28): 22 no wave64, 3 f16 instructions refused (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426 semantics differ from the console's), 2 no heap windows (opt-in XCL_HEAP_WINDOWS=1), 1 no GPU_UPLOAD heaps
  • Review cases: the reviewer's two tests at the previous head 9d8ae9b gave 0 passed, 2 failed; on 65d6bbc they pass (cargo test -p xcl --lib dxc::cache_tests: 18 passed, 0 failed, 0 ignored)
  • Offline, retail game's pack, xcl shaders shaders.rpf --precompile --limit 1500 into a fresh XCL_DXIL_CACHE (XCL_TRANSLATED_CACHE=0):
    • main 7db2f39: 1,601 DXIL files, 919 distinct contents
    • 9d8ae9b: 919 files, the same 919 contents (sha1, byte for byte)
    • The new key over all 1,601 dumped sources gives 919 keys, none with two outputs.
    • None of those sources contains __LINE__, #line, ??/ or a backslash before a line break, so the 65d6bbc guards leave all their keys as at 9d8ae9b.

Regression run

main (d8bd9a3):

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

this PR's head (65d6bbc):

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

Both: scripts/smoke.ps1 -Seconds 60, XCL_PREPARE=0, XCL_PRESS_AT=25:A, run through the local exclusive queue. With preparation off the run compiles few programs, so it checks that nothing breaks, not the dedupe.

These runs are on the head before the rebase (65d6bbc); they have not been repeated on 84bcc5e.

Not tested

  • AMD GPUs; wave64 hardware
  • Cold full-preparation wall time, main vs this change
  • The offline 1,601 → 919 count was not rerun on 65d6bbc: it follows from the key being unchanged for every one of those sources (see above).
  • Keys of generated sources under 960 KiB change once, so their DXIL is compiled again on the first run after this change.

AI use

  • Claude Code with Claude Opus 5.5: investigation, code, tests, the fix for the review findings, and this description. The author has reviewed the change.

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 28, 2026
@thehollowedone

Copy link
Copy Markdown
Contributor

At head 9d8ae9ba, hash_code_lines can give one DXIL cache key to HLSL that DXC compiles differently:

  • Inserting a // line before O.Store(0, __LINE__) leaves the normalized key unchanged. With DXC 1.9.0.5512 (-T cs_6_6 -E main -HV 2021), the store's immediate changes 3 → 4.
  • A backslash followed by a space and newline still splices the next line into a // comment in DXC, but the key's splice guard checks only an adjacent backslash/newline. The normalized key is unchanged while the stored value changes 3 → 1.

I added the two assertions below in a temporary checkout of this exact head. cargo test -q -p xcl --lib cache_key_distinguishes_line_macro_after_comment -- --test-threads=1 and cargo test -q -p xcl --lib cache_key_distinguishes_whitespace_line_splice -- --test-threads=1 each failed 0 passed, 1 failed, 0 ignored because its pair received the same key. These are synthetic sources; I did not find these forms in ordinary generated HLSL. The public compile_cached_in path and debug HLSL substitutions can accept other sources, so a warm cache could return the first shader's DXIL for the second.

Please keep exact-source keys for arbitrary/debug HLSL and enable normalized keys only for a source class whose preprocessor behavior is constrained, or make the normalization preserve all DXC-sensitive line and splice behavior. A regression should compile A into an empty cache, compile B with that cache, assert a miss, and compare B's DXIL with an independent compile. The existing corpus result shows useful deduplication; this check is about preserving its correctness.

Two failing regression tests
diff --git a/crates/xcl/src/gpu/recomp/dxc.rs b/crates/xcl/src/gpu/recomp/dxc.rs
index 45d787f8..19d250a9 100644
--- a/crates/xcl/src/gpu/recomp/dxc.rs
+++ b/crates/xcl/src/gpu/recomp/dxc.rs
@@ -1812,6 +1812,20 @@ mod cache_tests {
         assert_ne!(key(spliced), key("uint a = 1u; // other \\\nuint b = 2u;\n"));
     }
 
+    #[test]
+    fn cache_key_distinguishes_line_macro_after_comment() {
+        let a = "RWByteAddressBuffer O : register(u0);\n[numthreads(1, 1, 1)]\nvoid main() { O.Store(0, __LINE__); }\n";
+        let b = "RWByteAddressBuffer O : register(u0);\n// differing comment line\n[numthreads(1, 1, 1)]\nvoid main() { O.Store(0, __LINE__); }\n";
+        assert_ne!(cache_key_for_toolchain(a, "main", "cs_6_6", "tc"), cache_key_for_toolchain(b, "main", "cs_6_6", "tc"));
+    }
+
+    #[test]
+    fn cache_key_distinguishes_whitespace_line_splice() {
+        let a = "RWByteAddressBuffer O : register(u0);\n[numthreads(1, 1, 1)]\nvoid main() {\nuint n = 1 // note\n+ 2\n; O.Store(0, n); }\n";
+        let b = a.replacen("// note\n", "// note\\ \n", 1);
+        assert_ne!(cache_key_for_toolchain(a, "main", "cs_6_6", "tc"), cache_key_for_toolchain(&b, "main", "cs_6_6", "tc"));
+    }
+
     #[test]
     fn cache_key_of_a_long_source_keeps_its_comments() {
         // from 1 MiB the length (comments included) picks the -O level (`link_opt_args`)

AI use: Codex (GPT-6.2 Sol) reproduced both cases with DXC and ran the exact-head Rust tests; Codex (GPT-6.2 Astra) independently checked the cache-key path.

@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Cold full preparation data point, this PR together with #3242 (NVIDIA)

One cold run of full shader preparation (XCL_PREPARE_ALL=1) on a retail game:

Results

  • Translation and DXC phase: all 26,503 records done in 54.4 min.
  • Driver pipeline phase: 22,474 records checked at 60 min, 23,003 at 90 min and 23,624 at 117 min. I stopped the run there, with about 2,900 pipelines left.
  • At the stop the cache held 14,716 DXIL files and 4,981 driver blobs.

What this does not show

  • This PR's effect on its own. There is no matched main run or run of this PR alone: both were planned but cancelled for time.
  • Where this PR helps. Its dedupe only touches the DXC phase, and that phase is the shorter one here. Most of the time goes to the driver's slow tail, which is most likely the very large pipelines.
  • Other GPUs. One run, NVIDIA only.

A matched cold-run comparison against main from anyone would settle it.

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

@KOLESNiii
KOLESNiii force-pushed the recompiler-dxc-key-code-lines branch from 9d8ae9b to 65d6bbc Compare November 29, 2026 11:01
@github-actions github-actions Bot added the area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation label Nov 29, 2026
@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Independent measurement of the same cause, plus one suggestion. #3373 duplicates this PR and will be closed in favour of it.

Measured (RTX 2070 SUPER, retail game, the DXIL cache of a full preparation built by 8b957be): I dumped the 25,797 HLSL sources the preparation hands to DXC under their cache keys (dump-only hook, nothing compiled); all 25,797 matched cache entries.

  • 9,876 of 25,797 compiles produced a byte-identical copy of another entry. Every such pair has the same line count and differs only in comment lines, nearly always the two v_nop ; lit lines.
  • A key without whole-line // comments gives 15,955 keys for 15,921 distinct DXIL. The other 34 are the linked and whole-program forms of one source, which share the cache by design (XCL_LINK_MEM), not collisions.
  • dxc.exe on 150 random kept + 150 random redundant sources (one thread): 2.14 s vs 0.71 s mean, so the copies are about 17 % of a cold DXC pass (~7,000 of ~41,000 CPU-s here).

Suggestion: adopt entries written under the old key. With a new key, every existing cache goes cold once after merge: every source is compiled again by DXC (~41,000 CPU-s on this pack), and warm_pack's startup proof, which checks <key>.dxil, misses until then. #3373 avoids that on a miss: compute the old full-text key; if <old>.dxil is a valid container, hard-link it to the new name (copy if linking fails) and count a hit. In its smoke run from the same cache this gave 106,238 hits / 12 misses, against main's 107,906 / 7. The function is ~20 lines plus a test, at https://github.com/xcl-dev/xcl/pull/3373/files (adopt_legacy_entry, legacy_cache_key_for_toolchain, entries_of_the_comment_hashing_key_are_adopted). It could go here or in a follow-up stacked on this one.

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

@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Thanks @thehollowedone, both cases are real. I reproduced them and fixed them in 65d6bbc (squashed into the PR's one commit, rebased on main d8bd9a3).

Confirmed. I added your two tests unchanged at 9d8ae9b: cargo test -p xcl --lib cache_key_distinguishes -- --test-threads=1 gave 0 passed, 2 failed. Each pair got the same key (__LINE__ after an inserted comment line; // note\ + space + newline).

What changed

  • The normalized key is now used only for the HLSL XCL generates (dxc::compile_cached: the recompiler's and emit's programs). Everything else keys on its full text, which is the same key as before this PR. That covers compile_cached_in (a caller's own source) and generated HLSL that a debug hook edited (XCL_DEBUG_PS_SUB, XCL_DEBUG_CS_SUB, XCL_DEBUG_PS_EXPR, XCL_DEBUG_LOD0, the debug colour).
  • A generated source also keeps the full-text key when line structure could reach the DXIL (code_lines_keyable): it uses __LINE__ or a #line directive, or has a backslash before a line break, with or without spaces/tabs between, or a ??/ trigraph. The 960 KiB -O limit is unchanged.
  • None of the 1,601 HLSL sources dumped in the offline check contains any of these, so their keys are the same as at 9d8ae9b. The offline result stands: 1,601 DXIL files → 919, the same 919 contents.

Tests (debug, RTX 2070 SUPER, DXC from the Windows SDK): cargo test -p xcl --lib dxc::cache_tests: 18 passed, 0 failed, 0 ignored, including:

  • cache_key_distinguishes_line_macro_after_comment and cache_key_distinguishes_whitespace_line_splice (yours, unchanged);
  • only_sources_whose_line_structure_cannot_reach_the_dxil_drop_their_comments: the guard cases (__LINE__, #line / # line, backslash + tab + CRLF, backslash + spaces at end of file, ??/), and that compile_cached_in keys on full text;
  • a_program_compiled_differently_misses_the_cache_of_its_comment_twin: the regression you asked for. For both pairs, A goes into an empty cache through the generated path, then B is compiled with that cache: B misses, B's DXIL equals an independent compile of B, and A's and B's DXIL differ. A pair that differs only in comments still hits and shares the entry. It calls skip_test("no DXC") without DXC.

On the new head 65d6bbc:

  • cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 2625 passed, 0 failed, 49 ignored, with 27 SKIPPED (no wave64 / f16 refusals / heap windows / GPU_UPLOAD heaps on this adapter).
  • scripts/smoke.ps1 (60 s): main d8bd9a3 gave 1538 frames, the head 1576; 0 removals and 0 crashes on both.

The description is updated with both blocks.

AI use: Claude Code, Claude Opus 5.5, reproduced the cases, wrote the fix and tests, and drafted this reply; the author checked each point.

@KOLESNiii
KOLESNiii marked this pull request as ready for review November 29, 2026 12:36
@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Nov 29, 2026
@bmjask

bmjask commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Current-main audit (2026-11-29): strongest merge candidate in this set.

I read the full four-file diff at 65d6bbcc. The earlier correctness findings around __LINE__ and whitespace before a spliced newline are addressed in the current head: normalization is limited to XCL-generated HLSL, guarded against line-sensitive/spliced sources, and caller-provided/debug-substituted HLSL keeps its exact-text key. The Windows build/test check is green; the red publish job is a queue-feed HTTP 504, not a code/test failure.

Cache impact: this changes DXIL cache filenames, so an exact old generated-HLSL entry may miss once after the update. It does not intentionally change the HLSL passed to DXC or the resulting DXIL bytes. Because driver PSO blobs are keyed from the DXIL/descriptor identity, existing PSO blobs should remain reusable when DXC reproduces the same bytes. That makes this substantially safer than an optimizer that changes DXIL.

Disposition: mergeable in principle after normal maintainer review; no additional code blocker found in this audit.

Review assisted by Codex; exact model name was not exposed in this session. I did not rerun the suite or a full preparation.

A cold preparation compiled byte-identical DXIL many times: 43 % of its
DXC compiles. Each console record starts with two data words, shown in
the HLSL as `// 0000: v_nop ; lit 0x...` / `// 000c: ...` comments;
they differ per record (an identifier, not code), so programs with the
same code had different HLSL text and the key, the full text, differed.

Measured (`xcl shaders shaders.rpf --precompile --limit 1500`, a retail
game's pack, fresh cache): main wrote 1,601 DXIL files with 919 distinct
contents (CS 538 and VS 144 copies); in every identical group the
sources differed only in those comment lines. With this change: 919
files, the same 919 contents byte for byte.

For programs XCL generates (compile_cached: the recompiler's and emit's
HLSL) the key now hashes the code lines: comments (outside string
literals) become a space, lines are trimmed, empty lines dropped; line
breaks and spaces inside a line stay. DXC still compiles the full text,
so its output is unchanged. A source keeps the full-text key (the same
key as before) when:
- it is 960 KiB or longer: from 1 MiB the length, comments included,
  picks the -O level (compile_with, link_opt_args);
- line numbers could reach the DXIL: it uses __LINE__ or a #line
  directive (removing a comment line moves the lines below);
- it has a backslash before a line break, also with spaces or tabs
  between, or a ??/ trigraph: DXC splices the next line into a // comment.
Any other HLSL keys on its full text: compile_cached_in (a caller's own
source) and HLSL a debug hook edited (XCL_DEBUG_PS_SUB, XCL_DEBUG_CS_SUB,
XCL_DEBUG_PS_EXPR, XCL_DEBUG_LOD0, the debug colour). Keys of shorter
generated sources change once: their DXIL is compiled again on the first
run.

Builds on the idea of xcl-dev#1582 (closed), which stripped comments for the
-O choice but kept the full-text key. The line-number and splice cases
were found in review by thehollowedone (tests
cache_key_distinguishes_line_macro_after_comment and
cache_key_distinguishes_whitespace_line_splice are theirs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:shaders Shader cache, PSO compilation, pipeline libraries, precompilation gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants