fix(shader): refuse s_setpc_b64 after s_sub with the immediate first #607

Merged
adriadam10 merged 1 commit from upstream-pr/607 into main 2026-10-05 03:10:53 +00:00
adriadam10 commented 2026-10-05 02:46:33 +00:00 (Migrated from github.com)

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


What

s_setpc_b64 only works in the patterns GraphBuilder resolves at build time: s_getpc_b64 + one s_add/s_sub with an immediate + s_setpc_b64 (static target), and the bounded PC-relative jump table from 1571ec69. Neither had an execution test.

One case gave a wrong target instead of throwing: s_sub_u32 sN, imm, sN (immediate first) computes imm - pc on hardware, but resolveSetpcTarget jumped to pc - imm. Subtractions are now only resolved when the PC register is src0. Any other operand order falls through to the existing "unsupported dynamic s_setpc_b64" error.

New test agc_driver_scalar_setpc (ScalarSetpc.cpp). It loops over the lanes, reads each lane's inputs with v_readlane, runs the scalar code and writes the result back with v_writelane. It covers:

  • jump table with s_min_u32 bound 3: indices 0-4, 5, 7, 8, 16, 0x7fffffff, 0x80000000, 0xfffffff8, 0xffffffff (all past 3 clamp to the last case)
  • a second jump table with bound 2 and a non-zero SMEM immediate offset (s_load_dwordx2 ..., s8 offset:0x8, displacement reduced by 8)
  • a forward skip with s_add_u32 over two instructions
  • a backward loop with s_add_i32 and a negative literal, run 1-8 times
  • a backward loop with s_sub_u32, run 1-4 times
  • refusal of the reversed s_sub_u32 form

It runs in wave32, native wave64 and wave64 split into two wave32 halves (ComputeTarget(32)).

Behaviour measured on HW

Run on gfx1035 (RDNA2), wave32 and wave64, 3 random input sets per wave size: the hardware output matched the model for every form above, with no differences. The test's expected values are the hardware output. s_getpc_b64 returns the address of the instruction right after it. That is why getpc directly followed by setpc loops forever on hardware (and in the recompiler, which models it the same way). The test doesn't include that form.

Tested

Linux, RADV (Mesa 26.2) and lavapipe (local and CI's Mesa 25.2.8): the new test passes in all three wave modes. It doesn't depend on subgroup size, so it also runs on lavapipe. Cold run on CI's lavapipe: under 1 s. Full Release ctest: everything passes except agc_driver_pixel_helper_lanes, which also fails on local RADV on main.

Negative checks: the test fails when the code is mutated in any of these ways:

  • main's behaviour (reversed s_sub accepted): refusal check fails
  • subtraction resolved as addition: loop_sub
  • static target based on the s_getpc_b64 address instead of the next instruction: skip_forward
  • SMEM immediate offset dropped from the table address: jump_table_offset
  • table entry stride 4 instead of 8: jump_table
  • first table entry dropped (falls to the last case): jump_table

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:4739558625 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/607). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T02:46:33Z. --- ### What `s_setpc_b64` only works in the patterns `GraphBuilder` resolves at build time: `s_getpc_b64` + one `s_add`/`s_sub` with an immediate + `s_setpc_b64` (static target), and the bounded PC-relative jump table from 1571ec69. Neither had an execution test. One case gave a wrong target instead of throwing: `s_sub_u32 sN, imm, sN` (immediate first) computes `imm - pc` on hardware, but `resolveSetpcTarget` jumped to `pc - imm`. Subtractions are now only resolved when the PC register is src0. Any other operand order falls through to the existing "unsupported dynamic s_setpc_b64" error. New test `agc_driver_scalar_setpc` (`ScalarSetpc.cpp`). It loops over the lanes, reads each lane's inputs with `v_readlane`, runs the scalar code and writes the result back with `v_writelane`. It covers: - jump table with `s_min_u32` bound 3: indices 0-4, 5, 7, 8, 16, 0x7fffffff, 0x80000000, 0xfffffff8, 0xffffffff (all past 3 clamp to the last case) - a second jump table with bound 2 and a non-zero SMEM immediate offset (`s_load_dwordx2 ..., s8 offset:0x8`, displacement reduced by 8) - a forward skip with `s_add_u32` over two instructions - a backward loop with `s_add_i32` and a negative literal, run 1-8 times - a backward loop with `s_sub_u32`, run 1-4 times - refusal of the reversed `s_sub_u32` form It runs in wave32, native wave64 and wave64 split into two wave32 halves (`ComputeTarget(32)`). #### Behaviour measured on HW Run on gfx1035 (RDNA2), wave32 and wave64, 3 random input sets per wave size: the hardware output matched the model for every form above, with no differences. The test's expected values are the hardware output. `s_getpc_b64` returns the address of the instruction right after it. That is why `getpc` directly followed by `setpc` loops forever on hardware (and in the recompiler, which models it the same way). The test doesn't include that form. ### Tested Linux, RADV (Mesa 26.2) and lavapipe (local and CI's Mesa 25.2.8): the new test passes in all three wave modes. It doesn't depend on subgroup size, so it also runs on lavapipe. Cold run on CI's lavapipe: under 1 s. Full Release ctest: everything passes except `agc_driver_pixel_helper_lanes`, which also fails on local RADV on `main`. Negative checks: the test fails when the code is mutated in any of these ways: - `main`'s behaviour (reversed `s_sub` accepted): refusal check fails - subtraction resolved as addition: `loop_sub` - static target based on the `s_getpc_b64` address instead of the next instruction: `skip_forward` - SMEM immediate offset dropped from the table address: `jump_table_offset` - table entry stride 4 instead of 8: `jump_table` - first table entry dropped (falls to the last case): `jump_table` ### 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:20 +00:00 (Migrated from github.com)

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


Thanks, this checks out. I disassembled the kernels with llvm-mc -mcpu=gfx1013: the two jump tables (bound 3, and bound 2 with offset:0x8), the forward skip, both backward loops and the reversed s_sub_u32 s16, 64, s16 all decode as described; the words after s_endpgm are the table data.

Requiring the PC register as src0 for a subtraction is the right fix, since imm - pc can't be resolved to a static target, and the reversed form now falls through to the existing "unsupported dynamic s_setpc_b64" error. The new test fails on main on exactly that check ("was not refused") and passes with the fix.

PPSA21564 has only one s_setpc_b64, after s_sub_u32 s52, s52, s36 (register operand), which this change doesn't touch. It merges cleanly with the other open PRs that touch GraphBuilder.cpp. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987417988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/607#issuecomment-5987417988). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T03:09:20Z. --- Thanks, this checks out. I disassembled the kernels with `llvm-mc -mcpu=gfx1013`: the two jump tables (bound 3, and bound 2 with `offset:0x8`), the forward skip, both backward loops and the reversed `s_sub_u32 s16, 64, s16` all decode as described; the words after `s_endpgm` are the table data. Requiring the PC register as src0 for a subtraction is the right fix, since `imm - pc` can't be resolved to a static target, and the reversed form now falls through to the existing "unsupported dynamic `s_setpc_b64`" error. The new test fails on main on exactly that check ("was not refused") and passes with the fix. PPSA21564 has only one `s_setpc_b64`, after `s_sub_u32 s52, s52, s36` (register operand), which this change doesn't touch. It merges cleanly with the other open PRs that touch `GraphBuilder.cpp`. Ready to merge from my side.
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!607
No description provided.