fix(shader): return zero when an s_buffer_load offset passes 2^32 #604

Merged
adriadam10 merged 1 commit from upstream-pr/604 into main 2026-10-05 05:11:44 +00:00
adriadam10 commented 2026-10-05 02:30:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T02:30:10Z.


What

s_buffer_load_* computed the byte address of each dword (soffset + imm + 4*i) in 32 bits. When that sum passes 2^32 it wrapped and read the start of the buffer. Hardware does not wrap: those dwords are out of range and read 0. The fix adds a carry check to the bounds condition in EmitReadConstBuffer, for both the bound V# path and the GPU-computed V# path (gpuDescriptor, added in 21cb5676). The CPU-side SrtEvaluator already does this math in 64 bits.

Found by a test-only batch: s_load_dwordx16, s_buffer_load_dwordx4 and s_buffer_load_dwordx16 were decoded but no test executed them.

Behaviour measured on HW (gfx1035, wave32, 32 rows x 7 load groups, model matches 100%)

  • dword i reads from (soffset + imm + 4*i) & ~3. The low two offset bits are ignored. It reads 0 when that address is >= num_records, with the check done per dword, so a load that straddles the end returns its in-range dwords and zeros after them.
  • The sum does not wrap at 2^32. For example, soffset = 0xfffffffc with offset:0x14 reads 0, while main returned the dword at byte 0x10.
  • OOB_SELECT in the V# has no effect on SMEM (word3 0x01016fac and 0x31016fac give identical results).
  • s_load_dwordx16 with an unaligned immediate (0x26) reads from 0x24, which main already does.

Tested

Linux, new execution test agc_driver_scalar_wide_loads (tests/execution/ScalarWideLoads.cpp). Expected values come from the hardware capture. It covers s_buffer_load_dwordx4/x16 with SGPR, SGPR + immediate and immediate-only offsets (in range, unaligned, straddling num_records, past it, and past 2^32), one s_buffer_load_dword through a V# whose num_records is computed in the shader (GPU-descriptor path), and s_load_dwordx16 with a constant offset.

  • Passes on RADV, lavapipe and CI's lavapipe (Mesa 25.2.8), 1 s cold.
  • Fails on main (lane 2 buffer_load_dwordx4 s9 offset 0x14 dword 0 is 0x00000004, expected 0x00000000). Removing the carry check from either path alone also fails the test, on the matching row.
  • Full Release ctest: everything passes except agc_driver_pixel_helper_lanes, which already fails on main on local RADV.
  • The input buffer lives in a GuestAllocations block, as in FlatGlobalLoads.cpp, because the GPU-descriptor read goes through the BDA page table. Without it, lavapipe crashes in the shader, on main too.
  • s_load_dwordx16 with an SGPR offset goes through BDA reads. One such load takes 37 s cold on CI's lavapipe, so the test only runs the constant-offset form, which the SRT walker resolves. Its SGPR-offset results matched the hardware locally (RADV + lavapipe).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4739467250 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/604). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T02:30:10Z. --- ### What `s_buffer_load_*` computed the byte address of each dword (`soffset + imm + 4*i`) in 32 bits. When that sum passes 2^32 it wrapped and read the start of the buffer. Hardware does not wrap: those dwords are out of range and read 0. The fix adds a carry check to the bounds condition in `EmitReadConstBuffer`, for both the bound V# path and the GPU-computed V# path (`gpuDescriptor`, added in 21cb5676). The CPU-side `SrtEvaluator` already does this math in 64 bits. Found by a test-only batch: `s_load_dwordx16`, `s_buffer_load_dwordx4` and `s_buffer_load_dwordx16` were decoded but no test executed them. #### Behaviour measured on HW (gfx1035, wave32, 32 rows x 7 load groups, model matches 100%) - dword `i` reads from `(soffset + imm + 4*i) & ~3`. The low two offset bits are ignored. It reads 0 when that address is `>= num_records`, with the check done per dword, so a load that straddles the end returns its in-range dwords and zeros after them. - The sum does not wrap at 2^32. For example, `soffset = 0xfffffffc` with `offset:0x14` reads 0, while `main` returned the dword at byte 0x10. - `OOB_SELECT` in the V# has no effect on SMEM (word3 `0x01016fac` and `0x31016fac` give identical results). - `s_load_dwordx16` with an unaligned immediate (`0x26`) reads from `0x24`, which `main` already does. ### Tested Linux, new execution test `agc_driver_scalar_wide_loads` (`tests/execution/ScalarWideLoads.cpp`). Expected values come from the hardware capture. It covers `s_buffer_load_dwordx4`/`x16` with SGPR, SGPR + immediate and immediate-only offsets (in range, unaligned, straddling `num_records`, past it, and past 2^32), one `s_buffer_load_dword` through a V# whose `num_records` is computed in the shader (GPU-descriptor path), and `s_load_dwordx16` with a constant offset. - Passes on RADV, lavapipe and CI's lavapipe (Mesa 25.2.8), 1 s cold. - Fails on `main` (lane 2 `buffer_load_dwordx4 s9 offset 0x14 dword 0 is 0x00000004, expected 0x00000000`). Removing the carry check from either path alone also fails the test, on the matching row. - Full Release ctest: everything passes except `agc_driver_pixel_helper_lanes`, which already fails on `main` on local RADV. - The input buffer lives in a `GuestAllocations` block, as in `FlatGlobalLoads.cpp`, because the GPU-descriptor read goes through the BDA page table. Without it, lavapipe crashes in the shader, on `main` too. - `s_load_dwordx16` with an SGPR offset goes through BDA reads. One such load takes 37 s cold on CI's lavapipe, so the test only runs the constant-offset form, which the SRT walker resolves. Its SGPR-offset results matched the hardware locally (RADV + lavapipe). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-05 03:09:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T03:09:16Z.


Thanks, this checks out. I disassembled the test kernel with llvm-mc -mcpu=gfx1013 and it matches the description: s_buffer_load_dwordx4 and x16 with SGPR, SGPR plus immediate and immediate-only offsets (including offset:0xfffff and the straddling cases), the s_buffer_load_dword through the V# built in the shader, and s_load_dwordx16 at 0x26.

The carry check is right: each dword is its own ReadConstBuffer with soffset as the argument and imm + 4*i as mem.offset, so address >= soffset is exactly "the 32-bit add didn't wrap", and it's applied on both the bound V# path and the GPU-descriptor path. The new test fails on main with the row you quote (offset 0x14 dword 0 is 0x00000004) and passes with the fix; the full ctest passes with it merged.

One note for the merge order: PPSA21564 uses s_buffer_load_* in almost every shader (5,626 of the 5,688 I have dumped, 538 of them with an SGPR offset). None of them should come anywhere near 2^32, so I don't expect a visible change, but since the extra compare goes into nearly every shader I'll run it through my visual check of PPSA21564 before it goes in.

Ready to merge from my side once that run is done.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987417356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/604#issuecomment-5987417356). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T03:09:16Z. --- Thanks, this checks out. I disassembled the test kernel with `llvm-mc -mcpu=gfx1013` and it matches the description: `s_buffer_load_dwordx4` and `x16` with SGPR, SGPR plus immediate and immediate-only offsets (including `offset:0xfffff` and the straddling cases), the `s_buffer_load_dword` through the V# built in the shader, and `s_load_dwordx16` at `0x26`. The carry check is right: each dword is its own `ReadConstBuffer` with `soffset` as the argument and `imm + 4*i` as `mem.offset`, so `address >= soffset` is exactly "the 32-bit add didn't wrap", and it's applied on both the bound V# path and the GPU-descriptor path. The new test fails on main with the row you quote (`offset 0x14 dword 0 is 0x00000004`) and passes with the fix; the full ctest passes with it merged. One note for the merge order: PPSA21564 uses `s_buffer_load_*` in almost every shader (5,626 of the 5,688 I have dumped, 538 of them with an SGPR offset). None of them should come anywhere near 2^32, so I don't expect a visible change, but since the extra compare goes into nearly every shader I'll run it through my visual check of PPSA21564 before it goes in. Ready to merge from my side once that run is done.
oneandonlydean commented 2026-10-05 05:04:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:04:10Z.


Gated on PPSA21564: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988443368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/604#issuecomment-5988443368). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T05:04:10Z. --- Gated on PPSA21564: no visual or FPS change.
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
boykopovar/AnyPS5!604
No description provided.