Skip to content

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

Open
upipibi wants to merge 4 commits into
xcl-dev:mainfrom
upipibi:buffer-swizzle
Open

upipibi wants to merge 4 commits into
xcl-dev:mainfrom
upipibi:buffer-swizzle

Conversation

@upipibi

@upipibi upipibi commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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 (547886c), 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 547886c, 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 + Recompiler: a V# with ADD_TID_ENABLE indexes buffers by lane #2066 without the swizzle: of the 7 earlier gpu_buffer tests, 5 fail (the five swizzled cases), and 2 pass (Recompiler: a V# with ADD_TID_ENABLE indexes buffers by lane #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 Recompiler: a V# with ADD_TID_ENABLE indexes buffers by lane #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

upipibi and others added 2 commits November 24, 2026 20:36
Both paths ignored ADD_TID_ENABLE (V# word 3 bit 23): every lane of a
buffer load without IDXEN read element 0. RDNA2 takes the lane as the
index when IDXEN is clear and ignores ADD_TID with IDXEN (AnyPS5#21970,
measured on gfx1036; #24781 applies it to graphics stages). Loads,
stores and atomics, both paths.

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

Neither path implemented the buffer swizzle: a V# with SWIZZLE_EN (word 1
bit 31) and a nonzero stride was addressed linearly, base + soffset +
offset + stride * index. Both paths now use the rule AnyPS5#21970 measured
on RDNA2 (gfx1036 through HIP, compute loads and stores, wave32 and
wave64):

  step = 8 << INDEX_STRIDE (word 3 [22:21])
  addr = ((index / step) * stride + (off & ~3)) * step
         + (index % step) * 4 + (off & 3) + soffset

where off is the VGPR offset plus the instruction offset and ELEMENT_SIZE
has no effect. The swizzle applies only with IDXEN, or with ADD_TID and no
IDXEN; anything else stays linear. Without IDXEN each dword of a
multi-dword access is swizzled at off + 4k (4 * step bytes apart); with
IDXEN the dwords are contiguous from the first one's swizzled address.

The V# is dynamic, so the address is a runtime select. On the direct
path a multi-dword load without IDXEN keeps its single vector load for
the linear case and takes a second guarded per-dword load when swizzled
(the V# is uniform, so one of the two runs); the window is chosen from
the swizzled 64-bit offset (new Ea::W term). The bounds check is
unchanged (index < num_records when the stride is nonzero).

Atomics, d16 and sub-dword accesses and MTBUF / format loads and stores
go through the same address code, so a swizzled V# swizzles their first
byte too, but #21970 did not measure atomics, tbuffer, typed accesses or
SOFFSET, and a multi-component format access still reads its components
contiguously. Not measured either: that the console GPU swizzles like
gfx1036, and the (off & 3) term in DWORD alignment mode (#21970 ran
UNALIGNED).

New GPU tests gpu_buffer_swizzle_* (ADD_TID with and without an
instruction offset, INDEX_STRIDE 3, IDXEN x2 with a VGPR index and
ADD_TID set, ADD_TID x2 load and store, linear controls) fail on the
parent and pass here with XCL_DIRECT_DXIL=0 and =1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
upipibi and others added 2 commits November 25, 2026 09:38
lower_window.rs from main (547886c: window_at carries in 32 bits without i64).
lower_mem.rs buf_addr computes the windowed swizzled offset's high half with
32-bit carries as window_at does, and takes the window from the swizzled
offset, so the direct path stays free of 64-bit integer ops.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he swizzled offset

gpu_buffer_swizzle_window_from_swizzled_offset: three swizzled loads with two
memory windows - an offset carry from below window 0 into window 1, an
index * stride product that carries into window 1, and a product past 2^32
that must read 0. Fails on main + xcl-dev#2066 on both paths, passes with the swizzle.

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

Copy link
Copy Markdown
Contributor

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

Thanks!

@throny throny added the off-goal Does not bear on the current goal (world loading); waits until it is reached label Nov 26, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

— Devin

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

Labels

off-goal Does not bear on the current goal (world loading); waits until it is reached

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants