Recompiler: swizzled buffer V#s address records in dword-interleaved groups #2315

Open
upipibi wants to merge 4 commits from upstream-pr/2325 into main
upipibi commented 2026-11-25 14:33:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by upipibi on 2026-11-25T14:33:05Z.


Depends on: #2066. Stacked on it: the branch is #2066's commit b81d47c, then this PR's commits: 4ee401e (the swizzle), a merge of main 06c9f550 that adapts the direct path to main's 32-bit window carry (547886ca), and 68b6034 (a test for swizzled accesses across memory windows).

A V# with its swizzle bit set (word 1 bit 31) and a nonzero stride interleaves records in dword groups. XCL addressed every buffer access linearly, as offset + stride * index, on both translators. That includes the per-lane index #2066 adds for ADD_TID. So a swizzled access read and wrote the wrong bytes: with stride 16 and ADD_TID, lane 1 reads byte offset 16 instead of 4.

The rule, as boykopovar/AnyPS5#21970 measured it on gfx1036 (RDNA2, compute loads and stores, through HIP; that PR is still open):

  • step = 8 << INDEX_STRIDE (word 3 [22:21]).
  • Address: ((index / step) * stride + (off & ~3)) * step + (index % step) * 4 + (off & 3), plus SOFFSET unscaled. Here off is the VGPR offset plus the instruction offset. ELEMENT_SIZE has no effect.
  • It applies with IDXEN, or with ADD_TID and no IDXEN. With IDXEN, ADD_TID is ignored. Anything else stays linear.
  • In a multi-dword access without IDXEN, each dword is swizzled at off + 4k. With IDXEN the dwords are contiguous from the first one's address.

What is in it

  • recomp/emit.rs (HLSL path): Em::buf_rel computes the relative address, including the 64-bit windowed form. Loads, stores, atomics and the D16 forms use it, and multi-dword accesses space their dwords by the swizzle step.
  • dxil/lower_mem.rs (direct path): Lw::buf_addr, the same rule. A multi-dword load without IDXEN keeps its single vector load when the V# is linear, and does a guarded per-dword load when it's swizzled. The V# is uniform, so only one of the two runs.
  • dxil/lower_mem.rs, windowed form: buf_addr computes the high half of the swizzled (or linear) offset with 32-bit carries, as main's window_at does since 547886ca, and adds it to the V# base, so the window is chosen from the swizzled offset and the direct path keeps no 64-bit integer ops. dxil/lower_window.rs is main's, unchanged.
  • recomp/tests.rs: gpu_buffer_swizzle_window_from_swizzled_offset, three swizzled loads with two memory windows: an offset carry from window 0 into window 1, an index * stride product that carries into window 1, and a product past 2^32 that must read 0.
  • recomp/tests.rs: six GPU tests (gpu_buffer_swizzle_*). They cover ADD_TID (lane 1 at +4, not +16), INDEX_STRIDE 3, IDXEN with a vindex and DWORDX2, DWORDX2 without IDXEN, a two-dword store, and controls (SWIZZLE_EN=0 stays linear; SWIZZLE_EN=1 with neither IDXEN nor ADD_TID is linear too).

Tested

At 68b6034 (this tree is main 06c9f550 + #2066 + this PR), RTX 3070 (616.56), Windows 11, debug:

  • On main + #2066 without the swizzle: of the 7 earlier gpu_buffer tests, 5 fail (the five swizzled cases), and 2 pass (#2066's ADD_TID test and the linear control). gpu_buffer_swizzle_window_from_swizzled_offset fails at its first case (lane 0 reads window 0 + 0x1000). Both on XCL_DIRECT_DXIL=0 and =1. With the PR: gpu_buffer 8 of 8 on both paths.
  • A mutation of the direct-path change, taking the window from the linear offset instead of the swizzled one, makes the new window test fail (XCL_DIRECT_DXIL=1). (Edited: I removed a second mutation I had listed, because it has no log.)
  • gpu_ suite (--skip gpu_sparse_vram, single-threaded): main 06c9f550 144 passed, 0 failed, 1 ignored on both paths; this PR 152 passed, 0 failed, 1 ignored on both paths (the +8 are #2066's test, the six swizzle tests and the window test). SKIPPED (counted as passed): gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64), on both.
  • window tests 32 of 32, including main's window_at_lowers_to_32bit_carry_without_int64 and gpu_window_at_32bit_carry_edge_cases, on both paths. gpu::dxil:: 157 passed, 3 ignored.
  • git merge-tree against current main 08294413: clean.
  • Earlier, at 4ee401e (before the merge): nnonno91 (comment, thanks) ran gpu_buffer 7 of 7 on an AMD Radeon iGPU (32.0.21042.62) and an RTX 5090 (32.0.16.1742) on both paths, plus a temporary nonzero-SOFFSET fixture, 128 of 128 readbacks matched. That run predates the 32-bit window change.

DXIL size (direct path, wave32, one access per program, insts = non-comment lines of the main body; main + #2066 → this PR):

program 1 window 2 windows
BUFFER_LOAD_DWORD 71 → 89 (+18) 94 → 121 (+27)
BUFFER_LOAD_DWORDX2 IDXEN OFFSET 4 81 → 99 (+18) 108 → 138 (+30)
BUFFER_LOAD_DWORDX2 OFFEN (no IDXEN) 87 → 126 (+39) 114 → 174 (+60)
BUFFER_STORE_DWORDX2 80 → 100 (+20) 106 → 135 (+29)
BUFFER_ATOMIC_ADD OFFEN 77 → 99 (+22) 100 → 134 (+34)
BUFFER_LOAD_DWORDX4 IDXEN 100 → 117 (+17) 126 → 152 (+26)

That's +17 to +39 instructions per access with one window (+17 % to +45 % of these small programs) and +26 to +60 with two windows (+21 % to +53 %), linear V#s included, because the V# is only known at run time. A multi-dword load without IDXEN costs the most: it carries a second, guarded per-dword load for the swizzled case (only one of the two runs, the V# is uniform). Static counts only; I didn't measure compile or run time.

A question for the maintainer, given #2088's DXIL-size work: is this cost acceptable, or should the swizzle math be emitted only where it can apply? Options, none done here: skip it when the V# bits are known at translation time (rare), share the per-V# values between accesses through the same SRSRC in a block, or branch on the uniform swizzle bit instead of selecting.

Regression run
I have no retail title here. Could someone with one post xcl-smoke blocks for main and this head? I'll paste them with credit.

Not tested

  • Whether the console's GPU swizzles like gfx1036. #21970 didn't measure gfx1013.
  • Atomics, D16 and byte accesses, and MTBUF/format accesses. They share the address code, so their starting address is swizzled now. #21970 didn't measure them, and no test here covers them.
  • A nonzero SOFFSET is covered only by nnonno91's temporary fixture above, not by a test in this PR. The (off & 3) term in DWORD alignment mode is not covered (#21970 measured it in UNALIGNED mode).
  • Across several memory windows, only swizzled loads are tested (gpu_buffer_swizzle_window_from_swizzled_offset). Swizzled stores and atomics across windows are not.
  • The bounds check is unchanged. #21970 notes the hardware reads past NUM_RECORDS * stride for swizzled groups, and this PR doesn't model that.
  • The expected values in the tests come from #21970's formula, not from a measurement of my own. Titles. On AMD, only nnonno91's iGPU run above.

AI use
Claude Code (Claude Opus 5.5) wrote the change and the tests and ran them. I reviewed the diff against the rule before opening this.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5493368153 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2325). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T14:33:05Z. --- Depends on: #2066. Stacked on it: the branch is #2066's commit `b81d47c`, then this PR's commits: `4ee401e` (the swizzle), a merge of `main` `06c9f550` that adapts the direct path to main's 32-bit window carry (547886ca), and `68b6034` (a test for swizzled accesses across memory windows). A V# with its swizzle bit set (word 1 bit 31) and a nonzero stride interleaves records in dword groups. XCL addressed every buffer access linearly, as `offset + stride * index`, on both translators. That includes the per-lane index #2066 adds for ADD_TID. So a swizzled access read and wrote the wrong bytes: with stride 16 and ADD_TID, lane 1 reads byte offset 16 instead of 4. **The rule**, as boykopovar/AnyPS5#21970 measured it on gfx1036 (RDNA2, compute loads and stores, through HIP; that PR is still open): - `step = 8 << INDEX_STRIDE` (word 3 [22:21]). - Address: `((index / step) * stride + (off & ~3)) * step + (index % step) * 4 + (off & 3)`, plus SOFFSET unscaled. Here `off` is the VGPR offset plus the instruction offset. ELEMENT_SIZE has no effect. - It applies with IDXEN, or with ADD_TID and no IDXEN. With IDXEN, ADD_TID is ignored. Anything else stays linear. - In a multi-dword access without IDXEN, each dword is swizzled at `off + 4k`. With IDXEN the dwords are contiguous from the first one's address. **What is in it** - `recomp/emit.rs` (HLSL path): `Em::buf_rel` computes the relative address, including the 64-bit windowed form. Loads, stores, atomics and the D16 forms use it, and multi-dword accesses space their dwords by the swizzle step. - `dxil/lower_mem.rs` (direct path): `Lw::buf_addr`, the same rule. A multi-dword load without IDXEN keeps its single vector load when the V# is linear, and does a guarded per-dword load when it's swizzled. The V# is uniform, so only one of the two runs. - `dxil/lower_mem.rs`, windowed form: `buf_addr` computes the high half of the swizzled (or linear) offset with 32-bit carries, as main's `window_at` does since 547886ca, and adds it to the V# base, so the window is chosen from the swizzled offset and the direct path keeps no 64-bit integer ops. `dxil/lower_window.rs` is main's, unchanged. - `recomp/tests.rs`: `gpu_buffer_swizzle_window_from_swizzled_offset`, three swizzled loads with two memory windows: an offset carry from window 0 into window 1, an `index * stride` product that carries into window 1, and a product past 2^32 that must read 0. - `recomp/tests.rs`: six GPU tests (`gpu_buffer_swizzle_*`). They cover ADD_TID (lane 1 at +4, not +16), INDEX_STRIDE 3, IDXEN with a vindex and DWORDX2, DWORDX2 without IDXEN, a two-dword store, and controls (SWIZZLE_EN=0 stays linear; SWIZZLE_EN=1 with neither IDXEN nor ADD_TID is linear too). **Tested** At `68b6034` (this tree is main `06c9f550` + #2066 + this PR), RTX 3070 (616.56), Windows 11, debug: - On main + #2066 without the swizzle: of the 7 earlier `gpu_buffer` tests, 5 fail (the five swizzled cases), and 2 pass (#2066's ADD_TID test and the linear control). `gpu_buffer_swizzle_window_from_swizzled_offset` fails at its first case (lane 0 reads window 0 + 0x1000). Both on `XCL_DIRECT_DXIL=0` and `=1`. With the PR: `gpu_buffer` 8 of 8 on both paths. - A mutation of the direct-path change, taking the window from the linear offset instead of the swizzled one, makes the new window test fail (`XCL_DIRECT_DXIL=1`). (Edited: I removed a second mutation I had listed, because it has no log.) - `gpu_` suite (`--skip gpu_sparse_vram`, single-threaded): main `06c9f550` 144 passed, 0 failed, 1 ignored on both paths; this PR 152 passed, 0 failed, 1 ignored on both paths (the +8 are #2066's test, the six swizzle tests and the window test). `SKIPPED` (counted as passed): `gpu_geometry_wave32_prefix_count_over_a_mask` and `gpu_wave32_program_in_wave64_hardware` (no wave64), on both. - `window` tests 32 of 32, including main's `window_at_lowers_to_32bit_carry_without_int64` and `gpu_window_at_32bit_carry_edge_cases`, on both paths. `gpu::dxil::` 157 passed, 3 ignored. - `git merge-tree` against current `main` `08294413`: clean. - Earlier, at `4ee401e` (before the merge): nnonno91 ([comment](https://github.com/xcl-dev/xcl/pull/2325#issuecomment-7165626170), thanks) ran `gpu_buffer` 7 of 7 on an AMD Radeon iGPU (32.0.21042.62) and an RTX 5090 (32.0.16.1742) on both paths, plus a temporary nonzero-SOFFSET fixture, 128 of 128 readbacks matched. That run predates the 32-bit window change. **DXIL size** (direct path, wave32, one access per program, `insts` = non-comment lines of the `main` body; main + #2066 → this PR): | program | 1 window | 2 windows | |---|---|---| | BUFFER_LOAD_DWORD | 71 → 89 (+18) | 94 → 121 (+27) | | BUFFER_LOAD_DWORDX2 IDXEN OFFSET 4 | 81 → 99 (+18) | 108 → 138 (+30) | | BUFFER_LOAD_DWORDX2 OFFEN (no IDXEN) | 87 → 126 (+39) | 114 → 174 (+60) | | BUFFER_STORE_DWORDX2 | 80 → 100 (+20) | 106 → 135 (+29) | | BUFFER_ATOMIC_ADD OFFEN | 77 → 99 (+22) | 100 → 134 (+34) | | BUFFER_LOAD_DWORDX4 IDXEN | 100 → 117 (+17) | 126 → 152 (+26) | That's +17 to +39 instructions per access with one window (+17 % to +45 % of these small programs) and +26 to +60 with two windows (+21 % to +53 %), linear V#s included, because the V# is only known at run time. A multi-dword load without IDXEN costs the most: it carries a second, guarded per-dword load for the swizzled case (only one of the two runs, the V# is uniform). Static counts only; I didn't measure compile or run time. **A question for the maintainer, given #2088's DXIL-size work:** is this cost acceptable, or should the swizzle math be emitted only where it can apply? Options, none done here: skip it when the V# bits are known at translation time (rare), share the per-V# values between accesses through the same SRSRC in a block, or branch on the uniform swizzle bit instead of selecting. **Regression run** I have no retail title here. Could someone with one post `xcl-smoke` blocks for `main` and this head? I'll paste them with credit. **Not tested** - Whether the console's GPU swizzles like gfx1036. #21970 didn't measure gfx1013. - Atomics, D16 and byte accesses, and MTBUF/format accesses. They share the address code, so their starting address is swizzled now. #21970 didn't measure them, and no test here covers them. - A nonzero SOFFSET is covered only by nnonno91's temporary fixture above, not by a test in this PR. The `(off & 3)` term in DWORD alignment mode is not covered (#21970 measured it in UNALIGNED mode). - Across several memory windows, only swizzled loads are tested (`gpu_buffer_swizzle_window_from_swizzled_offset`). Swizzled stores and atomics across windows are not. - The bounds check is unchanged. #21970 notes the hardware reads past `NUM_RECORDS * stride` for swizzled groups, and this PR doesn't model that. - The expected values in the tests come from #21970's formula, not from a measurement of my own. Titles. On AMD, only nnonno91's iGPU run above. **AI use** Claude Code (Claude Opus 5.5) wrote the change and the tests and ran them. I reviewed the diff against the rule before opening this. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
nnonno91 commented 2026-11-25 15:07:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by nnonno91 on 2026-11-25T14:40:00Z.


Independent Radeon and RTX 5090 check of 4ee401eff0c97db43d87c570a0a572f14e8ef900, including a nonzero scalar offset.

Tested

  • Windows 11 25H2 build 26200.9974, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, SDK DXC/DXIL 1.8.2502.11, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and RTX 5090 driver 32.0.16.1742.
  • cargo test --locked -p xcl --lib gpu_buffer -- --nocapture --test-threads=1 gave 7 passed, 0 failed, 0 ignored per adapter/backend pair. Selected adapters with XCL_ADAPTER, and ran XCL_DIRECT_DXIL=0 and 1 in separate processes on each.
  • The temporary local_swizzle_scalar_offset_stays_unscaled fixture used a DWORD load with instruction offset 4 and scalar offset 4. It covered both ADD_TID and reversed VGPR indices with IDXEN, checking all 64 lanes for each. It compared the readback to the swizzled address plus 4 unscaled bytes. cargo test --locked -p xcl --lib local_swizzle_scalar_offset_stays_unscaled -- --nocapture --test-threads=1 gave 1 passed, 0 failed, 0 ignored and 128 matching readbacks per adapter/backend pair. No skips in any run. The temporary fixture was removed.

Not tested

Retail shader inputs, the parent, title smoke, and the full workspace suite.

AI use

OpenAI Codex (GPT-6.2 Sol) added the temporary offset fixture, ran the tests and wrote this report.

<!-- greyfinch-sync:ivall/xcl:comment:7165626170 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2325#issuecomment-7165626170). Originally posted by [nnonno91](https://github.com/nnonno91) on 2026-11-25T14:40:00Z. --- Independent Radeon and RTX 5090 check of `4ee401eff0c97db43d87c570a0a572f14e8ef900`, including a nonzero scalar offset. **Tested** - Windows 11 25H2 build 26200.9974, Ryzen 7 9800X3D, Rust 1.100.0 MSVC, SDK DXC/DXIL 1.8.2502.11, unoptimized profile. AMD Radeon(TM) Graphics driver 32.0.21042.62 and RTX 5090 driver 32.0.16.1742. - `cargo test --locked -p xcl --lib gpu_buffer -- --nocapture --test-threads=1` gave 7 passed, 0 failed, 0 ignored per adapter/backend pair. Selected adapters with `XCL_ADAPTER`, and ran `XCL_DIRECT_DXIL=0` and `1` in separate processes on each. - The temporary `local_swizzle_scalar_offset_stays_unscaled` fixture used a DWORD load with instruction offset 4 and scalar offset 4. It covered both `ADD_TID` and reversed VGPR indices with `IDXEN`, checking all 64 lanes for each. It compared the readback to the swizzled address plus 4 unscaled bytes. `cargo test --locked -p xcl --lib local_swizzle_scalar_offset_stays_unscaled -- --nocapture --test-threads=1` gave 1 passed, 0 failed, 0 ignored and 128 matching readbacks per adapter/backend pair. No skips in any run. The temporary fixture was removed. **Not tested** Retail shader inputs, the parent, title smoke, and the full workspace suite. **AI use** OpenAI Codex (GPT-6.2 Sol) added the temporary offset fixture, ran the tests and wrote this report.
DavidM1911 commented 2026-11-25 19:08:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T18:09:08Z.


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

  • cargo test -p xcl --lib: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7170511530 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2325#issuecomment-7170511530). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T18:09:08Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun. Thanks!
DavidM1911 commented 2026-11-26 09:07:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T08:15:33Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: 1 failure under parallel scheduling; clean on --test-threads=1 rerun (1312 passed, 0 failed, 27 ignored, 82s). Intermittent single-test flake — tracked in #1978.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7186378242 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2325#issuecomment-7186378242). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T08:15:33Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: 1 failure under parallel scheduling; clean on `--test-threads=1` rerun (**1312 passed, 0 failed, 27 ignored**, 82s). Intermittent single-test flake — tracked in #1978. Thanks! — Devin
Foniass commented 2026-12-01 07:49:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:49:56Z.


Triage (#1607) – head 68b6034

Stacked on #2066, whose ADD_TID commit (b81d47c) is now on main; the swizzled addressing on top of it (Em::buf_rel in recomp/emit.rs, Lw::buf_addr in dxil/lower_mem.rs) is not on main and no other open PR makes it (#3421 and #3419 are about format DST_SEL swizzles, not V# address swizzling).
Checked the per-dword spacing without IDXEN (4 << sh bytes, matching (off + 4k) & ~3 times the step) and the 32-bit carry for the windowed high half; both translators use the same rule and the tests cover IDXEN, ADD_TID, INDEX_STRIDE, multi-dword loads/stores and the window carry. The branch conflicts with main and needs a rebase that drops the #2066 commit.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7304533252 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2325#issuecomment-7304533252). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T07:49:56Z. --- **Triage (#1607)** – head 68b6034 Stacked on #2066, whose ADD_TID commit (b81d47c) is now on main; the swizzled addressing on top of it (`Em::buf_rel` in recomp/emit.rs, `Lw::buf_addr` in dxil/lower_mem.rs) is not on main and no other open PR makes it (#3421 and #3419 are about format DST_SEL swizzles, not V# address swizzling). Checked the per-dword spacing without IDXEN (`4 << sh` bytes, matching `(off + 4k) & ~3` times the step) and the 32-bit carry for the windowed high half; both translators use the same rule and the tests cover IDXEN, ADD_TID, INDEX_STRIDE, multi-dword loads/stores and the window carry. The branch conflicts with main and needs a rebase that drops the #2066 commit. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2325:upstream-pr/2325'
git switch 'upstream-pr/2325'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2325'
git switch 'upstream-pr/2325'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2325'
git switch 'upstream-pr/2325'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2325'
git switch 'main'
git merge --squash 'upstream-pr/2325'
git switch 'main'
git merge --ff-only 'upstream-pr/2325'
git switch 'main'
git merge 'upstream-pr/2325'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2315
No description provided.