Skip to content

Recompiler: keep the raw-load shader flag guard in the DCE after block merging - #3664

Open
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-dce-raw-guard
Open

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-dce-raw-guard

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

#3503 made the optimizer's dead-code elimination keep one raw buffer load when every raw load in a shader is dead. The RawAndStructuredBuffers shader flag is set while the module is built, so a shader that loses all its raw loads no longer matches its flags. But Module::optimize runs a second DCE after simplify_cfg merges blocks, and that one had no such guard: it dropped the very load the first DCE had kept. Both DCEs now go through one Module::eliminate_dead, which applies the guard, so the load survives block merging too.

Related: #3503 added the guard to the first DCE; this adds it to the second. #3646 (open) also changes Module::optimize in opt.rs for speed; the two may conflict in text, not in behaviour.

What is in it

  • crates/xcl/src/gpu/dxil/module/opt.rs: the two DCE loops in Module::optimize become one Module::eliminate_dead, with the raw-load guard in both; a switch for the second one; a unit test that a dead raw load survives the DCE after block merging.
  • crates/xcl/src/gpu/recomp/pso.rs, crates/xcl/src/shaders.rs: the new switch bypasses the translated shader caches and is part of the warm pipeline settings key, like XCL_DXIL_NO_OPT.

Tested

  • RTX 3060 Ti 8 GB, Windows 11, release build: the optimizer change has been in our test builds since 30 Nov (no measured before/after on its own); the cache-key part is new on this branch.
  • This branch (on main 1c8b551): cargo check -p xcl --lib --tests --target x86_64-pc-windows-gnu passes. The test suite was not run on this exact branch.

Regression run

  • Not run yet: testers welcome.

Not tested

  • The suite and scripts/smoke.ps1 on this branch; AMD.
  • The new test a_dead_raw_buffer_load_survives_the_dce_after_block_merging was not run on this branch.

Switches

  • XCL_DXIL_MERGE_DCE_RAW_GUARD (default on): =0 leaves the guard out of the DCE after block merging, as before this change. While it is set (to any value), the shader caches are bypassed, as with XCL_DXIL_NO_OPT.

AI use

  • Claude Code wrote the change and this description.

🤖 Generated with Claude Code

…k merging

xcl-dev#3503 made the optimizer's DCE keep one reachable dx.op.rawBufferLoad
when every raw load is dead: RawAndStructuredBuffers is set while the
module is built, and a module that sets it without a raw load no longer
matches its flags. The second DCE, which runs when simplify_cfg merged
blocks, had no such guard and dropped the very load the first DCE kept.

Both DCEs now go through one Module::eliminate_dead, which applies the
guard. XCL_DXIL_MERGE_DCE_RAW_GUARD=0 leaves it out of the second DCE
(the behaviour before this change). Like XCL_DXIL_NO_OPT, the switch
bypasses the translated shader caches and is part of the warm pipeline
settings key, so a cached shader never stands in for the other setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Prime1997 Prime1997 self-assigned this Dec 2, 2026
@github-actions github-actions Bot added 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) labels Dec 2, 2026
@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Review (head cc6b6d1, diff read, not a merge-check; checks red):

  • PR description check refuses it. PR checks reports that Regression run has no xcl-smoke block made on head cc6b6d1 and none made on a commit of main. The description also says the suite and the new test were not run on this branch, and the only test listed is cargo check with x86_64-pc-windows-gnu. CONTRIBUTING requires the whole suite on a GPU with counts. Build and test (Windows, no GPU) is red too, but it looks unrelated: the lib test process aborts (exit code 130088963) right after hle::d3d12x::uma_driver logs "coherent window could not be restored". The new test shows ... ok in that log. A rerun should tell.
  • The switch only brings the bug back and pulls in two files. XCL_DXIL_MERGE_DCE_RAW_GUARD=0 (opt.rs ~1937) restores the old behaviour, which drops the raw load the first DCE kept. That switch is the only reason for the pso.rs ~1183 and shaders.rs ~36 changes, and for the textual conflict with Recompiler: bound a direct DXIL compute lane's loops by its dispatch's share #3669 in shaders.rs. Any value disables the shader caches, including =1, which is the same as the default. CONTRIBUTING puts env-var switches in their own PR. Without the switch this is a ~90-line change to opt.rs alone.
  • The consequence is not shown. The description says the flags "no longer match", but there is no shader, validator message or device error where this happened ("no measured before/after on its own"). The unit test counts calls and does not validate a module. By reading, the bug is real: on main the DCE after simplify_cfg treats every rawBufferLoad as removable and has no guard. It drops the load (and its handle chain) that Recompiler: drop dead raw loads/f16 convs (#2212), specialize const v_perm_b32 (#2267), fold const reversebits #3503's guard kept, and the next iteration finds no raw load left to keep.
  • Conflicts with Recompiler: outline repeated direct-DXIL helpers into internal functions #3654. Recompiler: outline repeated direct-DXIL helpers into internal functions #3654 widens the same flagged closure to SCALAR_HEAP_PAIR | SCALAR_HEAP_QUAD | RAW_HEAP_QUAD. Whichever PR lands second has to move that predicate into eliminate_dead (opt.rs ~1517). If it doesn't, the outlined helpers lose the guard in one of the two DCEs.

Fine: by reading, eliminate_dead keeps main's behaviour for both DCE passes: the same roots, keep_all stands in for skip("dce"), the second pass has the same !removable roots, and live is now sized for each call. The new test fails on main for merge=true (the second DCE drops both the raw load and its dx.op.unary operand) and passes with the fix. No cache version bump is needed: module/opt.rs is under src/gpu/dxil, which XCL_DIRECT_DXIL_SOURCE_ID hashes (build.rs ~141).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Test report (not a merge-check: PR description red)
base: 1c8b551
head: cc6b6d1
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read by one reviewer at this same head; the review is at #3664 (comment) and its findings stand. Most serious: the fix is correct by reading; the description lacks the suite run and smoke blocks.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (cc6b6d1): 1652 -> 1693 frames shown (102 %; last presents per 2 s: 61 60 60), peak commit 18228 -> 18028 MB, 0 device removals, 0 crashes.
assessment: merges, builds and passes the whole suite on current main, and the smoke run is clean; the review found no blocking problem, and the description still needs its suite counts and smoke blocks
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Edgaras0404

Copy link
Copy Markdown

test report (not a merge-check; requested AMD test of #3664)

base: df0cfef
head: cc6b6d1
merge: clean
build: cargo build --workspace -> ok; cargo build --release -p xcl -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> the -p xcl --lib binary aborted at exit 0x80004005 with no test result: line: 1383 ok, 4 failed, 25 ignored observed of 2,795 lib tests (the rest did not run); the 43 non-lib result lines: 147 passed, 2 failed, 20 ignored. SKIPPED 2 among the tests that ran, both no heap windows (hle::d3d12x::bind::tests::two_pass_binds_set_the_shared_view_heap, heap_window_out_of_memory_falls_back_to_the_switch_form). Failing: gpu::dxil::compact_tests::gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form (compact_tests.rs:184, left (50, 0, 1, 5) vs right (50, 10045, 1, 5)), gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_f32_compares_with_nan, gpu::recomp::tests::gpu_float_compares_random (all three fail in the base lib run too), and gpu_two_pass_cache_e2e::two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs (merge full run: child at gpu_two_pass_cache_e2e.rs:472, parent at :584; base alone fails at :584, left: 1, right: 0). The base lib run has the same abort (exit 0x80004005; 1382 ok, 4 failed, 25 ignored) and the same four failures, so the PR adds no failure; its new test gpu::dxil::module::opt::tests::a_dead_raw_buffer_load_survives_the_dce_after_block_merging passes. With XCL_DXIL_MERGE_DCE_RAW_GUARD=0 that test fails for merge=true (left: 0, right: 1), so it covers the fix.
review: most serious first: (1) the code fix is correct by reading: both DCEs now call Module::eliminate_dead; the second gets the same raw-load guard, order is refreshed after simplify_cfg (opt.rs:1455), repl2 is sized for the whole instruction list, and the first call keeps the skip("dce")/keep-all behaviour. The new test passes with the guard on and fails with it off, so it is a real regression test. (2) XCL_DXIL_MERGE_DCE_RAW_GUARD is scope for its own PR: =0 only restores the bug, while any value (including =1) bypasses the translated shader caches and changes the warm-pipeline settings key, and CONTRIBUTING asks for env switches separately; the fix itself is a ~90-line opt.rs change. (3) overlap, not duplicate: #3654 changes the same flagged predicate in opt.rs (the earlier review says it widens it to the scalar-heap pairs/quads), and #3646 also edits Module::optimize; whichever lands second needs the predicate moved into eliminate_dead. #3503 is the merged predecessor that added the first guard. No duplicate found in open/merged PRs or #1607. (4) the description has no suite counts and no xcl-smoke blocks, so PR description is red; Build and test is red; the linked review reports the new test logs ok in that run, so it is not the new test.
performance: 60 s retail smoke: base df0cfef6 1619 frames / 16,223 MB peak, merge 15614e38 1618 / 17,816 (99.9% frames, 109.8% peak; thresholds 80%/125%), 0 device removals and 0 crashes on both, exit 0x0 both; presents per 2 s last 3: base 60 60 61, merge 61 60 60.
verdict: not yet: the PR description check is red (no suite counts or xcl-smoke block) and the new switch is scope for its own PR; the code, tests and manual 60 s smoke are clean on AMD, so those are the only blockers.
hardware / tool and model: AMD Radeon RX 6700 XT 12 GB (RDNA2), driver 32.0.21045.29003; AMD Ryzen 5 5600X; Windows 10 build 19045 / OpenCode, DeepSeek V4.1 Flash 1017

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of cc6b6d1a: 1 finding(s) (1 medium), most severe first.

  1. Medium crates/xcl/src/gpu/dxil/module/opt.rs:1392: see below.

On lines outside the diff:

  1. Medium crates/xcl/src/gpu/dxil/module/opt.rs:1392: The raw-load guard only sees blocks that are still reachable when eliminate_dead runs, but constant branch folding (and the unreachable-block sweep at line 1153) empties blocks with self.blocks[b].clear() before any DCE, so raw loads that existed only in a folded-away branch are dropped with no guard. Take a shader whose only raw-buffer access is a rawBufferLoad inside if (CONST_FALSE): the CondBr folds to Br, the block becomes unreachable and is cleared, eliminate_dead finds no reachable rawBufferLoad, and the emitted DXIL still declares RawAndStructuredBuffers with no raw load left, the mismatch this PR is meant to prevent. Working out the flag from the final optimized code (on the writer side) instead of protecting loads in each DCE would cover every path.

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

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.

4 participants