Skip to content

Recompiler: ds_bpermute_b32 in wave64 stays in each 32-lane half (#772) - #1022

Open
upipibi wants to merge 1 commit into
xcl-dev:mainfrom
upipibi:fix/bpermute-wave64
Open

upipibi wants to merge 1 commit into
xcl-dev:mainfrom
upipibi:fix/bpermute-wave64

Conversation

@upipibi

@upipibi upipibi commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Branch fix/bpermute-wave64, commit 212fb96 on main 6efdabc. Not pushed.

Fixes #772.

What the hardware does

In native wave64 on RDNA2, ds_bpermute_b32 lets lane l read lane (l & 32) | (((addr + offset) >> 2) & 31). If that lane is inactive, the result is 0. Two people measured it:

XCL used & 63 and read across the halves. That follows the opcode table's "mod 64". The ACO lowering and both measurements disagree with it.

The change

emit.rs, "ds_bpermute_b32"

  • In wave64, the index is (lane_id & 32u) | (((addr + offset) >> 2) & 31u). Wave32 is unchanged.
  • The read goes through rla_half_fn, as ds_swizzle does.
  • Split wave64 needs no exchange any more, so xcl_x_rla_any and its 32-word exchange slot are removed.
  • In a 32-lane wave of a wave64 VS / PS, the permute can no longer reach lanes 32..63. The upper_lanes constant case (select(and(xcl_hw32, l >= 32), c, …)) is therefore removed.

upper_lanes.rs

docs/wave64-on-wave32.md

  • The exchange table, the memory slot, the cost paragraph and the VS / PS section now list ds_bpermute with the reads that stay inside their half.

The emitter's lines for wave64 in the split form:

before: uint {t}_l = (({addr} + {offset}u) >> 2) & 63u;                   uint {t} = xcl_x_rla_any({data}, {t}_l);
after:  uint {t}_l = (lane_id & 32u) | ((({addr} + {offset}u) >> 2) & 31u); uint {t} = xcl_x_rla_half({data}, {t}_l);

Test

The new test is gpu_ds_bpermute_wave64_stays_in_its_half. It uses source data 1000 + lane, so a 0 from an inactive lane can't be mistaken for lane 0. It runs seven cases and compares each with the measured model. It also checks these lanes from luckyteagirl's report directly:

  • 63 - lane: lane 0 reads 31, lane 32 reads 63, lane 63 reads 32
  • (lane + 1) & 63: lane 31 reads 0, lane 63 reads 32

Before (main 6efdabc with the new test): 226 wrong lanes. The counts per case match the "full-wave" column of certified-retart's table.

case wrong lanes
4 * lane (identity) 0
4 * (63 - lane) 64
4 * ((lane + 1) & 63) 2
4 * lane, offset:4 2
4 * lane, offset:128 64
4 * (63 - lane), offset:4 62
4 * (lane ^ 32), EXEC 0xAAAAAAAA_55555555 32

After: all seven cases match.

Three existing assertions encoded the cross-half reading and now expect the half-wave lanes. These are the same three that @NathanTheophile listed on the issue:

test old expectation new expectation
gpu_format_stores_atomics_global_lds 63 - l l ^ 31
gpu_split_wave64_cross_half_operations 63 - l l ^ 31
gpu_split_wave64_waves_diverge l ^ 33 l ^ 1

outside_whole_wave_code_the_upper_lanes_stay_unwritten now expects one read where it used to expect two. ds_bpermute is no longer a read of lanes ≥ 32, even after whole-wave code has written them.

Tested

RTX 3070, driver 616.56, Windows 11, DXC 1.8.2502.11. The command was cargo test --workspace --no-fail-fast -- --test-threads=4, in its own target dir.

passed failed ignored
main 6efdabc 727 0 41
212fb96 728 0 41

On both, the usual two tests were skipped (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware.

This GPU has 32-lane waves only, so every wave64 case above ran in the split form. The native wave64 path is not run here.

  • @NathanTheophile (RTX 4060 Laptop) and @SlopTart (RX 7900 XT, which also covers split wave64 forced on wave64 hardware) offered to run it.
  • On RADV the native wave64 form goes through ACO's own full-wave emulation of WaveReadLaneAt, so the explicit (lane_id & 32) index is what keeps it half-wave there.

Notes

Regression run
Smoke pair by Kalmarv (RTX 5070 Ti, driver 610.74), main 6efdabc (this PR's base) against 212fb96: 1184 / 1248 frames, no crash, no device removal on either side (comment).

main:

xcl-smoke v1
commit: 6efdabcdaac93ce5cc123a85e85deba5d49cb294
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1248 (PresentX 1248)
presents per 2 s (last 3): 15 32 13
device removals: 0
crashes: 0
peak commit: 28204 MB

PR:

xcl-smoke v1
commit: 212fb961819b45d7bd4b903bf5a823af044674cb
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1184 (PresentX 1184)
presents per 2 s (last 3): 4 0 4
device removals: 0
crashes: 0
peak commit: 25996 MB

Not tested

AI use

  • AI-assisted (Claude Code): checked the hardware reports against the code, wrote the fix, the test and this description, and ran the tests above.

🤖 Generated with Claude Code

…-dev#772)

Measured on RDNA2 in native wave64, the index for lane l is
(l & 32) | (((addr + offset) >> 2) & 31):

- RX 6800M (gfx1031), luckyteagirl:
  xcl-dev#772 (comment)
- gfx1036 with partial EXEC and immediate offsets, certified-retart:
  xcl-dev#772 (comment)

XCL masked the index with 63 and read across the halves.

- emit.rs: wave64 uses the half-wave index and reads it through
  rla_half_fn, as ds_swizzle does. As a result:
  - The split form needs no exchange, so xcl_x_rla_any and its 32-word
    slot are gone.
  - In a 32-lane wave of a wave64 VS / PS, the permute can no longer
    reach lanes 32..63, so the upper_lanes constant case is gone.
- upper_lanes.rs: ds_bpermute is no longer a read of lanes >= 32.
  ds_permute is unchanged (xcl-dev#925).
- docs/wave64-on-wave32.md: ds_bpermute is now listed with the reads
  that stay inside their half.

New test gpu_ds_bpermute_wave64_stays_in_its_half: the three full-EXEC
indices, offsets 4 and 128, and EXEC 0xAAAAAAAA_55555555, expected
values from the measured model. On main 6efdabc it gives 226 wrong
lanes. Three existing assertions that encoded the cross-half reading
now expect the half-wave lanes:

- gpu_format_stores_atomics_global_lds: l ^ 31
- gpu_split_wave64_cross_half_operations: l ^ 31
- gpu_split_wave64_waves_diverge: l ^ 1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KENAJBBY

Copy link
Copy Markdown

Native wave64 run on RDNA2, the form Not tested lists: pass, and the test's seven cases measured on the hardware with the same inputs.

Tested: head 212fb96 (base main 6efdabc), debug. Windows 11 Pro 26200; AMD Radeon iGPU of the Ryzen 7 9800X3D (gfx1036, RDNA2, native 64-lane waves; driver 32.0.21030.2001); NVIDIA RTX 5070 Ti (driver 621.24, 32-lane waves); Rust 1.100.0 MSVC; DXC 1.9.2602.17. One test process at a time.

Radeon gfx1036, XCL_ADAPTER=radeon XCL_REQUIRE_GPU=wave64 (native wave64) RTX 5070 Ti, XCL_ADAPTER=nvidia (split wave64)
gpu_ds_bpermute_wave64_stays_in_its_half passed passed
the bpermute, ds_swizzle, ds_permute, split_wave64, upper_lanes and gpu_format_stores_atomics_global_lds tests (20) 20 passed 20 passed
the new test against main's translation (emit.rs of 6efdabc) fails: 226 wrong, e.g. lane 0 4 * (63 - lane): 1063, want 1031 –
cargo test --workspace --no-fail-fast -- --test-threads=4 728 passed, 0 failed, 41 ignored, 0 SKIPPED 728 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware

So the native wave64 path gives the half-wave result, and the native form tells the old and new translations apart by the same 226 lanes as your split run.

The test's expected values against the hardware. I ran each of the test's seven cases as raw ds_bpermute_b32 on the same gfx1036 with inline RDNA2 assembly through AMD's OpenCL driver (-cl-std=CL2.0 -Wf,-mwavefrontsize64; v_mbcnt gives lanes 0..63 and s_bcnt1_i32_b64 exec = 64 in the same wave; EXEC set with s_mov_b64 around the instruction; 3 runs each, identical). Each one matches the half-wave model in 64 of 64 lanes, which is the model the test checks:

case half-wave model full-wave (& 63)
4 * lane 64/64 64/64
4 * (63 - lane) 64/64 0/64
4 * ((lane + 1) & 63) 64/64 62/64
4 * lane, offset:4 64/64 62/64
4 * lane, offset:128 64/64 0/64
4 * (63 - lane), offset:4 64/64 2/64
4 * (lane ^ 32), EXEC 0xAAAAAAAA_55555555 (inactive lanes keep VDST) 64/64 32/64

The last two were only measured by @certified-retart so far; this is a second gfx1036, with the same result.

Not tested: the console itself (gfx1036 is a desktop RDNA2 APU of the same generation), RDNA1, a wave64 VS / PS in 32-lane waves (the removed upper_lanes constant case), pixel shaders, a release build, xcl-smoke or any title.

AI use: Claude Code (Claude Opus 5.5) ran the hardware probe and the tests on my PC and wrote this comment.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X Radeon iGPU (gfx1036, RDNA2, native wave64 — the hardware class this fix targets), RTX 3060 Ti also present; Rust stable MSVC, debug.

  • XCL_ADAPTER=radeon cargo test -p xcl --lib bpermute: 2 passed — gpu_ds_bpermute_wave64_stays_in_its_half and gpu_ds_swizzle_and_bpermute_zero_inactive_lanes both run on the Radeon (backend 'real' – AMD Radeon(TM) Graphics confirmed in output).
  • XCL_ADAPTER=radeon cargo test -p xcl --lib recomp -- --test-threads=4: 189 passed, 0 failed, 18 ignored (ignored = XCL_SAMPLE_* retail-survey tests; all GPU wave64 tests ran).

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification, merged onto current main 384f7f7 locally (clean merge).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib bpermute: 2 passed, 0 failed — including the new GPU test gpu_ds_bpermute_wave64_stays_in_its_half, which runs on this adapter (wave64 emitted as two wave32 halves with per-half lane fixup). gpu_ds_swizzle_and_bpermute_zero_inactive_lanes also passes.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 verification, following up on my #772 correction row.

Merged state: head onto current main (54f2c17 lane merge), clean merge. Full suite: 727 passed / 11 failed / 41 ignored - exactly the known Wine-lane set from my #42 rows plus this rig's flake family; no new failures.

For the wave64/bpermute thread specifically, I ran the bpermute tests three ways on this merge:

So the "stays in each 32-lane half" lowering is green end-to-end on RDNA3 native wave64, and composes correctly with the forced split path. That matches what the community measured in hardware terms in #772: GFX10-GFX11.5 already restrict bpermute to the 32-lane half, and this PR makes the recompiler stop assuming otherwise.

One honest side observation from this batch, flagged not blamed: the gpu_split_wave64_* suite on current main under XCL_SPLIT_WAVE64=1 is failing far more on this rig than when I posted my split numbers in #772 (four of the five tests 0/6 in solo reps today, vs 12/12 + 9/12 earlier). Same rig stack, same binaries, and the era-era pin 1ec4c98 shows the same mixed-or-red pattern today, so I am not reading it as a regression from this PR; full numbers and ruled-out causes are in my new comment on #772.

Counts only, no game data.

Not tested: retail titles (none here), Windows, RDNA4.

Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross x86_64-pc-windows-gnu, XCL_NETWORK=0, release profile, --test-threads=4. My Linux rows: #42 / #718.

AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Regression pair for 212fb96 against main 6efdabc (this PR's base), for the Regression run section. Both runs pass: no device removal, no crash, 95 % of main's frames, 92 % of its peak commit.

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (driver 610.74), AMD Ryzen 9 9950X, Windows 11 Pro 26200.
  • scripts/smoke.ps1, the same retail title for 60 s each: main at 6efdabc (this PR's base, 29 commits behind today's main) first, then this PR's head, one after the other on the same machine.

main 6efdabc:

xcl-smoke v1
commit: 6efdabcdaac93ce5cc123a85e85deba5d49cb294
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1248 (PresentX 1248)
presents per 2 s (last 3): 15 32 13
device removals: 0
crashes: 0
peak commit: 28204 MB

this PR, 212fb96:

xcl-smoke v1
commit: 212fb961819b45d7bd4b903bf5a823af044674cb
gpu: NVIDIA GeForce RTX 5070 Ti
title: GTA VI
seconds: 60
exit: 0x0
frames shown: 1184 (PresentX 1184)
presents per 2 s (last 3): 4 0 4
device removals: 0
crashes: 0
peak commit: 25996 MB

Presents per 2 s over the whole run (from the activity (2 s) lines):

  • main: 0 31 59 51 29 57 60 60 60 60 60 60 60 60 59 46 51 60 60 60 45 53 29 5 2 2 9 15 32 13
  • PR: 0 12 60 37 52 24 60 52 60 60 60 60 60 60 60 48 45 60 60 60 60 41 50 24 7 4 0 4 0 4

The 64-frame gap is a few single dips in the first 20 s of the PR's run, and a slow phase from about 46 s that both runs have (93 frames in the last 16 s against 107). Both runs built about the same compute pipelines (175 and 160 lines, 17 and 13 from the driver's blob of an earlier run). As the PR says, no wave64 program counted in the retail cache uses ds_bpermute_b32, so this run is a regression check only, and one pair can't separate 5 % from run-to-run spread.

Not tested

  • Whether any program the title built in these 60 s uses ds_bpermute_b32: not counted.
  • World loading: a 60 s run ends before it.
  • A second pair, AMD, native wave64.

AI use: Claude Code (Claude Opus 5.5)

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

This PR still needs a smoke pair for the PR description check: it changes emit.rs, and we have no title. Could someone with the title post the xcl-smoke blocks for main and 212fb96? @eizispark and @Kalmarv have made these for our earlier PRs. Per the count on #772, no retail wave64 program uses ds_bpermute_b32, so this is only a regression check.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@upipibi, the pair for this head is already in an earlier comment: #1022 (comment). That's main 6efdabc (this PR's merge-base) against 212fb96, 1184 / 1248 frames (95 %), no crash and no device removal on either side. If you paste those two xcl-smoke blocks under Regression run in the description, the check should pass; the head hasn't moved since.

AI use: Claude Code (Claude Opus 5.5)

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, I missed it. Your pair is now under Regression run in the description, credited.

@cindertheblue

Copy link
Copy Markdown
Contributor

Tested 212fb96 merged onto main 025c786 (git merges it cleanly) on an RDNA3 card running native wave64, on Windows. The native form passes. Two things on current main need the PR updated: XCL_DIRECT_DXIL has its own ds_bpermute_b32 that still reads across halves, and a test added after this PR's base fails with it.

Tested

  • AMD Radeon RX 7600 XT 16 GB (RDNA3, gfx1102, native wave64 and wave32), driver 32.0.31041.1004 (AMD Software 26.8.1), Ryzen 5 9600X, Windows 11 Home 26200.9974, rustc 1.99.1, DXC 1.8.2502.11 (SDK 10.0.26100), debug. Each tree built with cargo build --workspace in its own folder.
  • The five tests this PR adds or changes (gpu_ds_bpermute_wave64_stays_in_its_half, gpu_format_stores_atomics_global_lds, gpu_split_wave64_cross_half_operations, gpu_split_wave64_waves_diverge, outside_whole_wave_code_the_upper_lanes_stay_unwritten), cargo test -p xcl --lib -- <the five names>, 3 runs per row:
result
no variable (native wave64 on this card) 5 passed ×3
XCL_SPLIT_WAVE64=1 (split form forced on wave64 hardware) 5 passed ×3
XCL_DIRECT_DXIL=1 1 passed, 4 failed ×3
XCL_DIRECT_DXIL=1 XCL_SPLIT_WAVE64=1 1 passed, 4 failed ×3
  • cargo test --workspace --no-fail-fast -- --test-threads=4, once each:
main 025c786 + this PR
no variable 1132 passed, 1 failed, 46 ignored 1132 passed, 2 failed, 46 ignored
XCL_DIRECT_DXIL=1 1130 passed, 3 failed, 46 ignored 1126 passed, 8 failed, 46 ignored

Failing on both trees, not from this PR: fuzz_validate_and_gpu_1 (the AMD seed 722 mismatch from #1679); with XCL_DIRECT_DXIL=1 also gpu_image_get_resinfo_1d_array and offline_compute_pipeline_is_reused_by_a_second_process (#1674).

1. The direct DXIL backend still reads across the halves. #1426 (merged after this PR's base) translates ds_bpermute_b32 itself in crates/xcl/src/gpu/dxil/lower_mem.rs ("ds_bpermute_b32", line 2022 on 025c786): let m = if self.wave64 { 63 } else { 31 }; then read_lane_any. With XCL_DIRECT_DXIL=1:

  • gpu_ds_bpermute_wave64_stays_in_its_half reports 226 wrong lanes in every run, the same count as your "before" on main (e.g. lane 0 4 * (63 - lane): 1063, want 1031).
  • The three updated assertions fail the same way (lane 0: ds_bpermute of index 63 from lane 31, lane 0 ds_bpermute (index 63 - l, within its half), [256, 1, 1], group 0 thread 0: ds_bpermute). On main with XCL_DIRECT_DXIL=1 the old cross-half expectations of those three pass.

So the direct form probably needs the same index, (lane & 32) | (((addr + offset) >> 2) & 31), in wave64.

2. split_wave64_reduction_tail_rules fails with no variable set. It was added in 9e2c7a5, after this PR's base, and counts xcl_x_meet() ? in the HLSL:

assertion `left == right` failed: 1 folded meet + 3 in prelude:
  left: 3
 right: 4

This PR removes xcl_x_rla_any, whose body has one xcl_x_meet() ? (uint o = xcl_x_meet() ? xcl_x_get(l & 31u) : 0u;), so the prelude now has 2. After a rebase the expectation would be 1 + 2. It passes on main.

Not tested

  • This PR's head 212fb96 on its own base 6efdabc: everything above is the merge onto 025c786.
  • A release build; the RDNA2 iGPU on this machine (this PR already has native RDNA2 runs above); NVIDIA.

AI use

  • Claude Code (Claude Opus 5.5) ran the tests, traced the two failures and wrote this comment from the real output.

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.

Recompiler: ds_bpermute_b32 in wave64 may stay within each 32-lane half on RDNA2 (needs a hardware run)

6 participants