Skip to content

Recompiler: fix per-lane cross-lane mask lowering bugs - #2128

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-perlane-wave-bugs
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1269-perlane-wave-bugs

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

What is in it

Fixes four per-lane cross-lane wave lowering bugs in crates/xcl/src/gpu/recomp/lower/wave.rs:

  1. V_MBCNT_HI_U32_B32 half-wave ballot and mask addend (wave_valu): hi_half was unconditionally true for V_MBCNT_HI_U32_B32, calling ballot_half even when SRC0 was the lower dword of a wave64 mask (e.g. vcc_lo or exec_lo), which left lanes 32..63 reading their own half's ballot instead of lanes 0..31's bits. hi_half is now only set when SRC0 is the upper dword of a wave64 mask pair (matches!(self.mask_reg(b, s0), Some((_, true)))). SRC1 is also now read via self.word(b, d, s1)? instead of self.src(...), so a mask SGPR can be used as the addend.
  2. Wave32 64-bit SGPR pairs with a mask in either half (words, wave_salu): In wave32, masks are single 32-bit SGPRs, so a 64-bit operand s[r:r+1] (r in 0..=106, including vcc_lo/vcc_hi) can hold a mask in r with an independent scalar number in r+1, or a number in r with a mask in r+1. Previously words() zeroed the upper dword whenever r held a mask, and wave_salu() skipped instructions where only r+1 held a mask (causing the fallback path to fail with "a mask read as a number"). words() now lowers wave32 0..=106 pairs dword-by-dword via word(), and wave_salu() checks s0 + 1 in wave32 _B64 mask counts while keeping EXEC (126) mapped to [ballot(exec), 0].
  3. V_READFIRSTLANE_B32 and V_READLANE_B32 mask SRC0 (wave_valu): Both instructions read SRC0 via self.src(b, d, s0)?, rejecting mask SGPRs (such as vcc_lo), unlike V_WRITELANE_B32 which already used self.word(b, d, s0)?. Both now use self.word(b, d, s0)?.
  4. MaskOp::Bcnt stale-SCC tracking (wave_salu): S_BCNT{0,1}_I32_B{32,64} wrote SCC via raw self.ssa.write(b, Var::Scc, c) instead of self.write_scc(b, c), bypassing own() so a stale SCC written inside a partial block and read after a lane join was not detected or refused. It now calls self.write_scc(b, c).

Adds five regression tests in gpu::recomp::lower::wave::tests: four deterministic CPU tests covering all four bugs plus a WARP-guarded GPU parity test (gpu_wave_fixes_agree_with_hlsl) comparing per-lane direct DXIL against HLSL on both split-wave64 and wave32.

Part of #1269

Tested

Hardware/toolchain: Windows 11 (26200), NVIDIA GeForce RTX 4060 (32-lane waves, driver 621.14), DXC 1.9.2609.5 (01b62ad4), Rust 1.100.0.

  • Verified pre-fix failures on the regression tests before applying the fixes (s106: a mask read as a number, unsupported S_BCNT1_I32_B64 (s5: a mask read as a number), wrong upper-half counts / zeroed high SGPR, and unrefused stale SCC).
  • cargo test --release -p xcl --lib gpu::recomp::lower::wave::tests:: -- --nocapture --test-threads=1 on RTX 4060: 5 passed, 0 failed, 0 ignored (0 skipped; gpu_wave_fixes_agree_with_hlsl ran on GPU for split-wave64 mbcnt_hi_low_mask and wave32 wave32_b64_mixed_mask / readlane_of_mask).
  • CI WARP check (XCL_ADAPTER="Microsoft Basic Render Driver" and XCL_ADAPTER=warp) on gpu::recomp::lower::wave::tests::: 5 passed, 0 failed, 0 ignored (4 CPU tests passed; gpu_wave_fixes_agree_with_hlsl cleanly reported SKIPPED ...: no GPU with 32-lane waves).
  • cargo build --release --workspace: exit 0.
  • cargo test --release --workspace -- --test-threads=4: 1159 passed, 0 failed, 46 ignored across 21 test suites, with 25 pre-existing runtime SKIPPED lines (23 native wave64 / unsupported per-lane lowering skips on 32-lane RTX 4060, 1 Windows driver-cache skip, 1 heap-window skip; 0 new skips).

Regression run

Paired 60-second scripts/smoke.ps1 runs on the same retail title build (29640639) from clean origin/main (6020a33) and this branch (9e08a5a):

Base (origin/main):

xcl-smoke v1
commit: 6020a33b16ba2de25b3aef5947552f2dc1831f09
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 14532 MB

PR (fix-1269-perlane-wave-bugs):

xcl-smoke v1
commit: 9e08a5a82b408f3272f7e340f56795946ba249ac
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1578 (PresentX 1578)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14507 MB

Not tested

  • Native 64-lane wave64 GPU hardware (the test GPU is a 32-lane NVIDIA RTX 4060; split-wave64 V_MBCNT_HI_U32_B32 was verified on both the CPU interpreter and the RTX 4060 GPU against HLSL).
  • Non-Windows host platforms.

AI use

Drafted with Google Antigravity (Gemini 4 Neon) and verified locally by reproducing failures before the fix, checking CPU and RTX 4060 GPU parity against HLSL, checking WARP adapter skips, running the full release workspace test suite, and completing the paired 60-second smoke run above.

Fix four bugs in per-lane cross-lane wave lowering (recomp/lower/wave.rs):

- V_MBCNT_HI_U32_B32: only use half-wave ballot_half when SRC0 is the upper
  dword of a wave64 mask pair (so low-half masks like vcc_lo/exec_lo still
  exchange lanes 0..31 to lanes 32..63), and read SRC1 through word() so a
  mask SGPR can be passed as the addend.
- wave32 64-bit mask counts: let words() read wave32 SGPR pairs (0..=106)
  dword-by-dword via word() and let wave_salu() detect a mask in the high
  dword (s[code+1]) so independent high SGPRs are preserved instead of
  zero-extended or rejected as "a mask read as a number".
- V_READFIRSTLANE_B32 / V_READLANE_B32: read SRC0 through word() so mask
  SGPRs are accepted consistently with V_WRITELANE_B32.
- MaskOp::Bcnt: record SCC through write_scc() instead of raw ssa.write() so
  stale SCC writes inside partial blocks are tracked and refused.

Part of xcl-dev#1269

Assisted-by: Google Antigravity (Gemini 4 Neon)
@DarkPanda69

Copy link
Copy Markdown

Tested: gpu_wave_fixes_agree_with_hlsl passes on an AMD D3D12 driver and on an RTX 3060.

Head 9e08a5a applied on main 6e1e2be, cargo test -p xcl --lib -- gpu:: --test-threads=4 with XCL_ADAPTER set to each card.
The test's wave64 cases use the split form (kernel_split, split_wave64: true), so even on the 9070 XT they do not run in native 64-lane waves. This is an AMD driver result, not the native wave64 check the PR lists as untested.
Not tested: native wave64 per-lane, retail runs.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@JoaoPauluu

Copy link
Copy Markdown

A run with vkd3d-proton as the host D3D12. The DXIL goes through dxil-spirv and then the NVIDIA Vulkan driver, not the native-Windows path: this PR's 5 new tests pass, none skipped.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. 9e08a5a as it is (merge base with main 6020a33), no uncommitted changes. The lib test binary runs under Wine, one test per process.
    • bcnt_scc_written_in_partial_block_is_detected_as_stale: ok
    • gpu_wave_fixes_agree_with_hlsl: ok
    • mbcnt_hi_of_low_mask_and_mask_addend_on_cpu: ok
    • readfirstlane_and_readlane_accept_mask_sgpr_on_cpu: ok
    • wave32_b64_mask_counts_preserve_independent_high_sgpr_on_cpu: ok

Not tested

  • the whole suite on this head, a retail title, native Windows, AMD.

AI use

  • Claude Code (Claude Opus 5.5) built the commit, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@JJAutoCompare

Copy link
Copy Markdown

Tested

  • Clean detached 9e08a5a82b408f3272f7e340f56795946ba249ac on Windows 11 Enterprise Evaluation 25H2 (26200.9974), Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175. Rust/Cargo 1.100.0 (x86_64-pc-windows-msvc), DXC/DXIL 1.9.2609.5. Debug/test profile with debuginfo and incremental compilation disabled.
  • Prebuilt with cargo test --locked -p xcl --lib --no-run --jobs 2 --message-format=json-render-diagnostics. Under the Arc mutex with XCL_ADAPTER=A770, ran the pinned Cargo artifact as xcl-97fdf2b9ff8e1482.exe gpu::recomp::lower::wave::tests::gpu_wave_fixes_agree_with_hlsl --exact --nocapture --test-threads=1. The binary digest matched before and after lock acquisition and after exit.
  • 1 passed, 0 failed, 0 ignored, 0 GPU skips, 1128 filtered out, 3.22 s. Direct DXIL matched HLSL/DXC readback in all 133 comparisons. Split wave64 covered the low-mask source and mask addend for V_MBCNT_HI_U32_B32 across all 64 logical lanes. Wave32 covered mixed mask/scalar B64 counts and find-first-bit, plus V_READFIRSTLANE_B32 and V_READLANE_B32 with a mask source. The fixture reports equality without printing passing readback values.

Not tested

Native wave64, CPU tests including stale-SCC refusal, overflow or invalid inputs, the workspace suite, retail smoke, and other adapters.

AI use

T3 Code / Codex (gpt-6.2-sol) built and ran the existing fixture and wrote this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants