Skip to content

Recompiler: read the whole per-lane S# in collapsed sampler waterfalls - #3663

Open
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-waterfall-lane-swords
Open

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/up-waterfall-lane-swords

Conversation

@Prime1997

Copy link
Copy Markdown
Collaborator

Requested by Prime

#3500 collapses a sampler (S#) waterfall loop into one pass where each lane reads its own sampler from v[vb..vb+3]. It removes the four v_readfirstlane writes to s[sb..sb+3] and points the MIMG at v[vb+3] for S# word 3 only. The emulated explicit-LOD gather (image_gather4_l*, mimg_gather4_lod) also reads S# words 0-2 (address modes, FORCE_UNORMALIZED, LOD bias, min/max LOD, mip filter), and it still took them from s[sb..sb+2]. After the collapse nothing writes those SGPRs, so each lane got stale or unrelated values. This change makes every S# word read of a MIMG in a collapsed waterfall come from the lane's own v[vb+k].

Related: #3643 also changes waterfall::collapse and the compact.rs block key for collapsed waterfalls; whichever lands second needs a rebase, and its 4-dword collapse should then record the S# base. #3621 keys translations on XCL_WATERFALL_COLLAPSE; the new XCL_WATERFALL_LANE_SWORDS would need the same tag. #3298 adds an explicit-LOD comparison sample that reads S# words 0-2; if both land, those reads should go through mimg_sword too.

What is in it

  • crates/xcl/src/gpu/dxil/waterfall.rs: collapse now maps each MIMG to the S# base v[vb] instead of v[vb+3]. With XCL_WATERFALL_LANE_SWORDS=0, a loop whose body has an image_gather4* is not collapsed. Two tests: the gather4 loop collapses only with per-lane S# words, and a collapsed image_gather4_l translates the same whichever (dead) SGPRs the waterfall used.
  • crates/xcl/src/gpu/dxil/lower_image.rs: mimg_sword(pc, ssamp, k) reads S# word k from v[vb+k] for a collapsed waterfall and from s[ssamp+k] otherwise. mimg_s3 and mimg_gather4_lod use it.
  • crates/xcl/src/gpu/dxil/compact.rs: the block key includes the S# base, since the MIMG's words do not name it. Two collapsed waterfalls over different VGPRs could otherwise share one lowering.
  • crates/xcl/src/gpu/dxil/lower.rs: doc comment on waterfall_ssamp.

Tested

  • RTX 3060 Ti 8 GB, Windows 11, release build: the change has been in our test builds since 30 Nov (no measured before/after on its own).
  • 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.
  • No visible symptom was pinned to this on our hardware; the fix comes from reading the lowering.

Switches

  • XCL_WATERFALL_LANE_SWORDS, default on. =0 leaves sampler waterfalls whose body has an image_gather4* uncollapsed (the loop runs as on the console). XCL_WATERFALL_COLLAPSE=0 still collapses none. Like XCL_WATERFALL_COLLAPSE on main, it is not part of the translation cache key, so clear the cache when changing it.

AI use

  • Claude Code wrote the change and this description.

🤖 Generated with Claude Code

xcl-dev#3500 collapses an S# waterfall loop (v_readfirstlane s[sb..sb+3] from
v[vb..vb+3], compare, s_and_saveexec, MIMG, s_xor exec) into one pass
over a non-uniform heap sampler. It drops the four v_readfirstlane
writes and maps the MIMG to v[vb+3] for S# word 3 only, but the
emulated explicit-LOD gather (mimg_gather4_lod, image_gather4_l*) also
reads S# words 0-2 (address modes, FORCE_UNORMALIZED, LOD bias, min/max
LOD, mip filter) and still took them from s[sb..sb+2], which are then
never written: stale or unrelated values per lane.

The collapse now records the S# base v[vb] and every S# word read of a
MIMG goes through Lw::mimg_sword, which takes v[vb+k] for a collapsed
waterfall and s[ssamp+k] otherwise. The compact form's block key now
includes that base too: the MIMG's words do not name vb, so two
collapsed waterfalls over different VGPRs (or a collapsed and an
uncollapsed one) with the same MIMG words could share one lowering.

XCL_WATERFALL_LANE_SWORDS=0 instead leaves waterfalls whose body has an
image_gather4* uncollapsed (the other correct form);
XCL_WATERFALL_COLLAPSE=0 still collapses none.

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 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 0324e7c, diff read, not a merge-check; checks red):

  • The new GPU-free test fails, so CI is red. Build and test (Windows, no GPU) on this head: 2762 passed, 1 failed, 31 ignored. The failure is waterfall::tests::collapsed_gather4_l_reads_no_stale_s_sharp_sgprs at crates/xcl/src/gpu/dxil/waterfall.rs ~573. The waterfall is collapsed in the dumped DXIL (one pass, no loop), but the test uses translate_compute, and the compute prologue emits its own waveReadLaneFirst for the wave slot (%14 in the CI dump). The "should be collapsed" assert can never pass on a compute entry. Check for the collapse some other way (the waterfall's waveActiveBallot/back edge, or kernel::translate as the existing test does). The description says only cargo check ran, which is consistent with this.
  • The test of the actual bug never gets to its real check. The assert_eq!(da, db) comparison (S# at s[8..11] vs s[12..15], same v[40..43]) is the part that would fail on main: by reading, main's mimg_gather4_lod loads user SGPRs s8..s10 vs s12..s14. It sits after the broken assert, so as written the test fails the same way with and without the fix. gather4_waterfall_collapses_only_with_lane_s_words only checks the new map convention (vb instead of vb+3) and the new switch, not what the lowering reads.
  • PR description check refuses it. There is no xcl-smoke block for head 0324e7c and none for a commit of main. This is a runtime change (crates/xcl/src/gpu/dxil).
  • Overlap with Recompiler: collapse 1-dword bindless waterfall loops #3643, conflicting in the same lines. Recompiler: collapse 1-dword bindless waterfall loops #3643 rewrites waterfall::collapse as try_collapse_4dword_sampler and keeps mimg_pcs.push((d.pc, vb + 3)), so this bug stays there. It also adds the same compact.rs key line (waterfall_ssamp ... v + 1). If Recompiler: collapse 1-dword bindless waterfall loops #3643 lands first, this PR becomes about 3 lines on top of it: the map value becomes vb, mimg_sword, and dropping the duplicate key line. If this one lands first, Recompiler: collapse 1-dword bindless waterfall loops #3643 has to change vb + 3 to vb and its test's Some(&88) when it rebases. Either way, only one of the two should carry the compact key.
  • XCL_WATERFALL_LANE_SWORDS is an extra switch that is not in the cache key. Its =0 setting only gives back the loop for gathers. CONTRIBUTING asks for env switches in their own PR, and toggling it reuses cached translations, which the description itself says. Consider dropping it from this fix.

Fine: by reading, the fix is right. Main maps a collapsed MIMG to v[vb+3] for word 3 only, while mimg_gather4_lod (lower_image.rs ~521) took words 0-2 (address modes, FORCE_UNORMALIZED, LOD bias, min/max LOD, mip filter) from s[sb..sb+2]. Nothing writes those after the collapse. collapse already enforces contiguous v[vb+k] -> s[sb+k] with sb % 4 == 0, so v[vb..vb+3] is the lane's whole S# in order. mimg_gather4_lod is the only lowering that reads words 0-2. Adding vb to the compact.rs block key also closes a sharing hole that already exists on main for word 3.

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: 0324e7c
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 1 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: gpu::dxil::waterfall::tests::collapsed_gather4_l_reads_no_stale_s_sharp_sgprs
review: read by one reviewer at this same head; the review is at #3663 (comment) and its findings stand. Most serious: its own new test fails, so the real check in it never runs.
performance: 60 s smoke of a retail title, main 1c8b551 vs this merge (0324e7c): 1652 -> 1685 frames shown (101 %; last presents per 2 s: 60 60 60), peak commit 18228 -> 17102 MB, 0 device removals, 0 crashes.
assessment: its own new test fails here too: gpu::dxil::waterfall::tests::collapsed_gather4_l_reads_no_stale_s_sharp_sgprs panics at waterfall.rs ~573, "the waterfall should be collapsed" (the emitted DXIL keeps the loop), the same failure as in CI, so the stale-S# check the test is meant to make never runs. The rest of the suite passes and the smoke run is clean
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 #3663)

base: df0cfef
head: 0324e7c
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, 5 failed, 25 ignored observed of 2,796 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); the lib aborted before later modules. Failing: (1) gpu::dxil::waterfall::tests::collapsed_gather4_l_reads_no_stale_s_sharp_sgprs (new in the PR; waterfall.rs:573, compute prologue %14 = waveReadLaneFirst; base has no such test); (2) 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); fails on base too, alone and in the base lib run); (3) 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); (4) 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), so the PR adds only the waterfall failure among the tests that ran; the compact-form and f32 failures are wave64 paths the NVIDIA report skips (20 no wave64); the two-pass cache failure and the abort reproduce on base here.
review: most serious first: (1) the PR's own new test can never pass: it asserts no waveReadLaneFirst in a translate_compute result, but the compute prologue emits %14 = waveReadLaneFirst; the dumped DXIL has no waterfall loop (no backedge or phi; the remaining branches and the two waveActiveBallots are the invalid-sampler guard), so the assert trips on the prologue, not on a loop. Reproduced at waterfall.rs:573, before assert_eq!(da, db), so the check that covers the fix never runs (use kernel::translate like the existing test, or test the waterfall's own map/ballot instead of the whole compute dump). (2) XCL_WATERFALL_LANE_SWORDS is an extra env switch outside the translation cache key (the description says to clear the cache); its =0 form only affects gathers, and CONTRIBUTING asks for switches in their own PR. (3) overlap, not duplicate: #3643 rewrites waterfall::collapse and adds the same compact.rs key line, so whichever lands second needs a rebase and only one should carry the key; #3298 adds explicit-LOD comparison samples that read S# 0-2, and if both land those reads should also go through mimg_sword (as the description says). (4) no test covers the compact.rs key addition (two collapsed waterfalls over different VGPRs, same S#/SSAMP, must not share a lowering). (5) by reading the fix is right: mimg_gather4_lod is the only lowering that reads S# words 0-2, every such read now goes through mimg_sword, collapse proves v[vb..vb+3] contiguous and not overwritten, and compact::plan runs after waterfall::collapse so it sees vb; reads_sword_0_2 counting every IMAGE_GATHER4* is a conservative superset. The description also has no suite counts and no xcl-smoke blocks, which is why PR description is red. Searched open and merged PRs and #1607: no duplicate; the nearest are #3643 and #3298 (overlap, above).
performance: 60 s retail smoke, base df0cfef6 1621 frames / 17,375 MB peak, merge a3407f72 1621 frames / 17,983 MB peak (100% frames, 103.5% peak; thresholds 80%/125%), 0 device removals and 0 crashes on both, exit 0x0 both; presents per 2 s last 3: base 60 59 60, merge 60 60 61. The title needed Enter to leave the menu. This does not measure the changed path (no visible symptom was pinned to it and the title may not reach a collapsed gather4_l); by reading, non-gather waterfalls do the same work with a different VGPR word, gather waterfalls collapse instead of looping, and the compact.rs key is compile-time only, so no frame-time or memory cost is expected.
verdict: not yet: its own new test fails on this AMD machine (and Build and test/PR description are red), so the da == db assertion that covers the fix never runs; the other failures reproduce on base and the smoke is clean.
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

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.

3 participants