tombox-xshader: translate s_getpc_b64 + s_add_u32 + s_addc_u32 + s_setpc_b64 as a direct branch #185

Closed
github-bot wants to merge 1 commit from gh-pr/185/head into gh-pr/185/base
Owner

@⁠MeNoSkills on GitHub · 2026-11-22 22:02 UTC · original
MeNoSkills:feat/pc-relative-far-jumps → main · +65 −0 · 3 files · 1 commits · closed without merging

A branch farther than s_branch's 16-bit range is encoded as s_getpc_b64 s[n:n+1]; s_add_u32 s[n], s[n], K; s_addc_u32 s[n+1], s[n+1], 0; s_setpc_b64 s[n:n+1] with a constant K. The translator rejected the s_setpc_b64 of that sequence ("unsupported SOP1") wherever it was not the last instruction before the end of the program, so any shader that needed a far branch failed to translate.

Change

resolve_far_jumps, run right after the disassembly, recognises the four-instruction pattern on the same register pair (the add's source and destination are s[n], the addc's are s[n+1] with a 0 operand, and the s_setpc_b64 reads s[n]) and turns the s_setpc_b64 into an s_branch whose offset points at (address after the s_getpc_b64) + K. The existing forward/backward branch handling then applies, and a target that is not an instruction start is reported the same way as before. A s_setpc_b64 without that pattern is still refused, and the "return to the chained stage" s_setpc_b64 at the end of a vertex shader is untouched. Only a K that is a multiple of 4 and non-negative is matched.

Tests

  • exec_mem::branch::pc_relative_jumps_are_direct_branches_near_and_beyond_the_16_bit_range: a compute shader that sets v10 = 7, jumps over v_mov v10, 63 plus 0, 3, 40 and 40,000 s_nop dwords, then sets v11 = 5; the last case is 160 KB, past the +-128 KB reach of s_branch. All four pass (also with runtime inputs) on an RTX 4070 Ti; without the change all fail with "unsupported SOP1 [s_setpc_b64]".
  • exec_mem::branch::a_jump_through_a_computed_register_is_still_refused.
  • Regression runs on the RTX (Windows 10 19045, MSVC): --lib 85, --test synthetic 22, --test isa_sweep 2 (541 mnemonics), --test mesh 24, --test exec -- scalar_ branch:: 16, all passing. pr_gate: 0 problems.

Not verified

No real shader with this pattern: I know the sequence only from a description of the retail GTA VI pack, not from a dump. Negative K (a backward far jump through s_sub_u32 or a wrapped add) is not recognised.

**@⁠MeNoSkills** on GitHub · 2026-11-22 22:02 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/185) `MeNoSkills:feat/pc-relative-far-jumps` → `main` · +65 −0 · 3 files · 1 commits · closed without merging A branch farther than s_branch's 16-bit range is encoded as s_getpc_b64 s[n:n+1]; s_add_u32 s[n], s[n], K; s_addc_u32 s[n+1], s[n+1], 0; s_setpc_b64 s[n:n+1] with a constant K. The translator rejected the s_setpc_b64 of that sequence ("unsupported SOP1") wherever it was not the last instruction before the end of the program, so any shader that needed a far branch failed to translate. ## Change `resolve_far_jumps`, run right after the disassembly, recognises the four-instruction pattern on the same register pair (the add's source and destination are s[n], the addc's are s[n+1] with a 0 operand, and the s_setpc_b64 reads s[n]) and turns the s_setpc_b64 into an s_branch whose offset points at (address after the s_getpc_b64) + K. The existing forward/backward branch handling then applies, and a target that is not an instruction start is reported the same way as before. A s_setpc_b64 without that pattern is still refused, and the "return to the chained stage" s_setpc_b64 at the end of a vertex shader is untouched. Only a K that is a multiple of 4 and non-negative is matched. ## Tests - `exec_mem::branch::pc_relative_jumps_are_direct_branches_near_and_beyond_the_16_bit_range`: a compute shader that sets v10 = 7, jumps over `v_mov v10, 63` plus 0, 3, 40 and 40,000 s_nop dwords, then sets v11 = 5; the last case is 160 KB, past the +-128 KB reach of s_branch. All four pass (also with runtime inputs) on an RTX 4070 Ti; without the change all fail with "unsupported SOP1 [s_setpc_b64]". - `exec_mem::branch::a_jump_through_a_computed_register_is_still_refused`. - Regression runs on the RTX (Windows 10 19045, MSVC): `--lib` 85, `--test synthetic` 22, `--test isa_sweep` 2 (541 mnemonics), `--test mesh` 24, `--test exec -- scalar_ branch::` 16, all passing. pr_gate: 0 problems. ## Not verified No real shader with this pattern: I know the sequence only from a description of the retail GTA VI pack, not from a dump. Negative K (a backward far jump through s_sub_u32 or a wrapped add) is not recognised.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:11 +00:00
github-bot closed this pull request 2026-11-26 06:13:11 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 23:23 UTC · original

Applied in cde6015 (merged in 42511d7) with your authorship kept, thanks.

**@⁠omonkeyz** on GitHub · 2026-11-22 23:23 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/185#issuecomment-7087537864) Applied in cde6015 (merged in 42511d7) with your authorship kept, thanks.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.