Recompiler: the DXIL cache key leaves out comments and indentation #3241
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3241"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-28T23:51:19Z.
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 andemit'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:-Olevel;__LINE__or a#linedirective;??/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
-Olevel but kept the full-text key. Independent of #2928, which skips repeated identical-HLSL jobs insideprecompile(). This change catches different HLSL with the same output and doesn't touchprecompile(). Part of #2088.What is in it
crates/xcl/src/gpu/recomp/dxc.rs:cache_key_for_toolchain/generated_cache_key: code-line key forcompile_cached, guarded bycode_lines_keyableandKEY_CODE_LINES_BELOW;cache_key: full-text key forcompile_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.cache_key_leaves_out_comments_and_indentation,cache_key_distinguishes_line_macro_after_commentandcache_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 throughcompile_cached_in.crates/xcl/src/shaders.rs: the pack proof recordsgenerated_cache_key, the keycompile_cacheduses.crates/xcl/src/gpu/recomp/emit.rs:mem_accessorsbecomespub(super)for the linked-program test.Tested
cargo build --workspace --tests && cargo test --workspace --no-fail-fast -- --test-threads=4on84bcc5e7(debug; rebased onto main81bfbf0d, run at the same time as the suites of #3262, #3242 and #3241): 2735 passed, 8 failed, 49 ignorede2etarget, whose test title had not been built (missing …xcl_testtitle.exe – build first: cargo build --workspace); the other 4 arehle::d3d12x::title_texturestests (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 timecargo 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 ignoredSKIPPEDon this adapter (28): 22 no wave64, 3 f16 instructions refused (#1426 semantics differ from the console's), 2 no heap windows (opt-inXCL_HEAP_WINDOWS=1), 1 no GPU_UPLOAD heaps9d8ae9bagave 0 passed, 2 failed; on65d6bbccthey pass (cargo test -p xcl --lib dxc::cache_tests: 18 passed, 0 failed, 0 ignored)xcl shaders shaders.rpf --precompile --limit 1500into a freshXCL_DXIL_CACHE(XCL_TRANSLATED_CACHE=0):7db2f391: 1,601 DXIL files, 919 distinct contents9d8ae9ba: 919 files, the same 919 contents (sha1, byte for byte)__LINE__,#line,??/or a backslash before a line break, so the65d6bbccguards leave all their keys as at9d8ae9ba.Regression run
main (
d8bd9a38):this PR's head (
65d6bbcc):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 (
65d6bbcc); they have not been repeated on84bcc5e7.Not tested
65d6bbcc: it follows from the key being unchanged for every one of those sources (see above).AI use
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-29T00:55:37Z.
At head
9d8ae9ba,hash_code_linescan give one DXIL cache key to HLSL that DXC compiles differently://line beforeO.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.//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=1andcargo test -q -p xcl --lib cache_key_distinguishes_whitespace_line_splice -- --test-threads=1each 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 publiccompile_cached_inpath 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
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.
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-29T10:05:23Z.
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:7db2f391+ this PR + #3242;XCL_DXIL_CACHE, NVIDIA and D3D shader caches cleared;Results
What this does not show
mainrun or run of this PR alone: both were planned but cancelled for time.A matched cold-run comparison against
mainfrom anyone would settle it.Measured and written with Claude Code (Claude Opus 5.5).
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-29T11:03:54Z.
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
8b957bec): 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.v_nop ; litlines.//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.exeon 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>.dxilis 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).
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-29T11:57:03Z.
Thanks @thehollowedone, both cases are real. I reproduced them and fixed them in
65d6bbcc(squashed into the PR's one commit, rebased on maind8bd9a38).Confirmed. I added your two tests unchanged at
9d8ae9ba:cargo test -p xcl --lib cache_key_distinguishes -- --test-threads=1gave 0 passed, 2 failed. Each pair got the same key (__LINE__after an inserted comment line;// note\+ space + newline).What changed
dxc::compile_cached: the recompiler's andemit's programs). Everything else keys on its full text, which is the same key as before this PR. That coverscompile_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).code_lines_keyable): it uses__LINE__or a#linedirective, or has a backslash before a line break, with or without spaces/tabs between, or a??/trigraph. The 960 KiB-Olimit is unchanged.9d8ae9ba. 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_commentandcache_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 thatcompile_cached_inkeys 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 independentcompileof B, and A's and B's DXIL differ. A pair that differs only in comments still hits and shares the entry. It callsskip_test("no DXC")without DXC.On the new head
65d6bbcc: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): maind8bd9a38gave 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.
Imported from ivall/xcl. Originally posted by bmjask on 2026-11-29T14:50:08Z.
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 redpublishjob 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.
Pull request closed