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

Closed
MeNoSkills wants to merge 1 commit from upstream-pr/185 into main
MeNoSkills commented 2026-11-22 22:02:33 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by MeNoSkills on 2026-11-22T22:02:33Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6548367050 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/185). Originally posted by [MeNoSkills](https://github.com/MeNoSkills) on 2026-11-22T22:02:33Z. --- 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.
omonkeyz commented 2026-11-22 23:23:27 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-22T23:23:27Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7087537864 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/185#issuecomment-7087537864). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-22T23:23:27Z. --- Applied in cde6015 (merged in 42511d7) with your authorship kept, thanks.

Pull request closed

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
omonkeyz/xbox-emulator!185
No description provided.