Skip to content

Shaders: the DXC cache key leaves out whole-line HLSL comments - #3373

Closed
KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:dxc-key-ignores-comments
Closed

KOLESNiii wants to merge 1 commit into
xcl-dev:mainfrom
KOLESNiii:dxc-key-ignores-comments

Conversation

@KOLESNiii

Copy link
Copy Markdown
Contributor

The DXC cache key hashed the full HLSL text, including the translator's per-instruction comments. Each console program starts with two v_nop whose 32-bit literal differs between programs with the same code, and the translator writes them as // 0000: v_nop ; lit 0x…. Those comment lines alone gave identical code a new key, so DXC compiled it again. The key now leaves out whole-line // comments; the source DXC compiles is unchanged. Part of #2088.

Measured on a retail game's DXIL cache from a full preparation (25,797 DXC sources, dumped with a throwaway hook under their cache keys):

  • 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.
  • Keyed without whole-line comments: 15,955 keys for 15,921 distinct DXIL. The other 34 are the linked and whole-program forms of the same source, which already share one cache by design (XCL_LINK_MEM, dxc.rs).
  • 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. The driver blob store already dedups identical DXIL, so this saves DXC time only.
  • A translator change that only edits annotations no longer invalidates every DXC entry.

What is in it

  • crates/xcl/src/gpu/recomp/dxc.rs: cache_key_for_toolchain hashes the source without whole-line // comments, plus the optimisation level the source length selects (-O3 / -O1 / -O0), so sources whose comments push them over a length threshold do not share an entry. A comment line ending in \, or following a line that does, stays in the key (line splicing makes it code). The key has a new domain tag.
  • Same file: on a miss, an entry written under the old full-text key is adopted (hard-linked to the new name, copied if linking fails), so existing caches stay warm and the startup pack proof (warm_pack), which checks <key>.dxil, finds them.
  • Tests: comment-only differences share a key, code differences and same-line comments do not, spliced lines count, the -O1 bracket splits keys, and an old-key entry is adopted without compiling.

Tested

  • RTX 2070 SUPER (driver 621.40), AMD Ryzen 5 3600XT, Windows 11 Home 10.0.26300, Rust 1.100.1, debug (suite) / release (smoke)
  • cargo test -p xcl --lib gpu::recomp::dxc::: 37 passed, 0 failed, 0 ignored
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 2621 passed, 0 failed, 49 ignored
  • SKIPPED (27): 21 × no wave64, 3 × the per-lane form refusing f16 ops (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), 2 × no heap windows (opt-in), 1 × no GPU_UPLOAD heaps

Regression run
Retail game, 60 s, XCL_PREPARE=0 and XCL_PRESS_AT=25:A on both sides, same DXIL cache.

main (d8bd9a38):

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

this PR (39d97735):

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

DXIL cache in report.json: main 107,906 hits / 7 misses, this PR 106,238 hits / 12 misses from the same cache (entries under the old key are adopted, nothing went cold).

Not tested

  • AMD / RDNA4.
  • A cold full preparation with the new key (hours here); the redundant-compile and timing numbers come from the dumped sources and dxc.exe, not from an XCL run.
  • Both smoke runs skipped startup preparation (XCL_PREPARE=0).

AI use

  • Claude Code with Claude Opus 5.5: found the cause from the cache analysis, wrote the change, the tests and this description, and ran the tests and smoke runs listed above.

🤖 Generated with Claude Code

The translator annotates every instruction with a comment, and each console
program starts with two v_nop whose 32-bit literal differs between programs
with the same code. The DXC cache key hashed the full text, so those
annotations alone gave identical code a new key and a new compile.

On a retail game's 25,797 DXC sources (the DXIL cache of a full
preparation), 9,876 compiles produced a byte-identical copy of another
entry; every such pair differed only in comment lines. A key over the source
without whole-line `//` comments gives 15,955 keys for 15,921 distinct DXIL
(the rest are the linked and whole forms, which share the cache by design).
Timing 150 + 150 sampled sources with dxc.exe puts the copies at ~17 % of a
cold DXC pass. A translator change that only edits annotations no longer
invalidates every DXC entry either.

Comment lines that line splicing would turn into code (ending in `\`, or
after a line that does) stay in the key, as does the optimisation level the
source length selects (-O3 / -O1 / -O0). Entries written under the old key
are adopted on a miss (hard-linked to the new name), so existing caches stay
warm and the startup pack proof finds them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 29, 2026
@KOLESNiii

Copy link
Copy Markdown
Contributor Author

Duplicate of #3241 (same cause, opened earlier), which this should be closed in favour of. The measurements and the old-key adoption from this PR are offered there: #3241

Written with Claude Code (Claude Opus 5.5).

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 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.

1 participant