Sign-extend SOPK immediates and preserve SALU SCC semantics in ffx_dispatch_aot #231

Merged
KomaGuy merged 1 commit from upstream-pr/234 into main 2026-11-21 19:15:08 +00:00
KomaGuy commented 2026-11-21 18:52:38 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KomaGuy on 2026-11-21T18:52:38Z.


Change

Align RDNA 2 scalar ALU (SALU) lowering in tools/ffx_dispatch_aot.py and tools/ffx_scratch_reference.py with the AMD RDNA 2 ISA specification for 16-bit signed SOPK immediates and SCC status-flag updates:

  • Sign-extend 16-bit SOPK immediates (simm16): LLVM's AMDGPU disassembler prints negative 16-bit SOPK immediates as unsigned 16-bit hex tokens (0x8000–0xffff). Previously word() zero-extended them (s_movk_i32 s0, -1 emitted 0xffffu, s_addk_i32 s4, -2 added +65534, and s_mulk_i32 s7, -3 multiplied by +65533), corrupting bits [31:16]. simm16() now sign-extends 16-bit immediates to 32-bit two's complement in both ffx_dispatch_aot.py and ffx_scratch_reference.py and rejects out-of-range immediates.
  • Preserve RDNA 2 SCC semantics across scalar ALU instructions:
    • Unsigned carry / 36-bit shift-add overflow: s_add_u32 (sadd_u) and s_lshl4_add_u32 (slshl4_add_u, capturing both shifted-out bits a >> 28 and post-shift carry r < (a << 4)).
    • Signed 32-bit overflow: s_addk_i32 (sadd_i) and s_sub_i32 (ssub_i).
    • Relational winner (S0 < S1 / S0 > S1): s_min_u32, s_max_u32, s_min_i32, and s_max_i32 (scc = 0 on ties or when S1 wins).
    • Nonzero result (scc = dest != 0u): s_and_b32, s_or_b32, s_xor_b32, s_lshl_b32 / s_lshr_b32 (with 5-bit shift-count masking), s_abs_i32 (sabs_i, two's-complement wrap-safe on 0x80000000), and s_bfe_u32 / s_bfe_i32.
    • Non-SCC operations: s_mov_b32, s_mov_b64, s_movk_i32, s_mulk_i32, s_mul_i32, s_mul_hi_u32, s_brev_b32, s_cselect_b32, and all vector (v_*) instructions leave scc untouched.
    • Added s_cmp_eq_i32 / s_cmp_lg_i32 handling in predecessor() matching translate().

Verification

Tested on Linux x86_64 (Python 3.13.5, LLVM 19.1.7, Clang 19.1.7) on top of 03c61dc:

  • python3 -B tests/ffx_dispatch_aot_check.py llvm-mc-19 llvm-objcopy-19 libLLVM-19.so — passed (boundary cross-checks against 64-bit/arbitrary-precision RDNA 2 ISA models, negative controls rejecting out-of-range SOPK immediates, assembled gfx1013 fixtures verifying 1 -> 0 and 0 -> 1 SCC transitions and SGPR aliasing through s_cselect_b32 and v_cndmask_b32_e64 ..., src_scc in both translate and predecessor, and ffx_scratch_reference.network SOPK parity).
  • python3 -B tests/pixel_aot_check.py binary llvm-mc-19 llvm-objcopy-19 libLLVM-19.so — passed.
  • python3 -B tests/script_state_check.py, tests/bcpack_check.py, tests/resource_ranges_check.py, tests/dxcontainer_part_offsets_check.py, tests/layout_chunks_check.py — all passed.
  • Compiled the tests/ffx_dispatch_io.hlsli integer helpers as C++20 with clang++-19 -std=c++20 -O2 -Wall -Wextra -Wpedantic -Werror -fsanitize=undefined,address across all signed/unsigned boundary pairs (including 0x80000000, 0x10000000 << 4, and aliased destinations) — 0 warnings, 0 sanitizer errors.
  • Windows C++, DXC HLSL (cs_6_0), WARP, and hardware checks were not run locally on Linux; relying on the build-and-test Windows CI job to verify desktop compilation and WARP execution.

Evidence and follow-up

  • All new fixtures in tests/ffx_dispatch_aot_check.py are original MIT-licensed gfx1013 assembly snippets assembled from source at test time with llvm-mc.
  • Instruction and SCC definitions follow the AMD RDNA 2 Instruction Set Architecture Reference Guide.
  • This contribution was AI-assisted.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532999465 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/234). Originally posted by [KomaGuy](https://github.com/KomaGuy) on 2026-11-21T18:52:38Z. --- ## Change Align RDNA 2 scalar ALU (`SALU`) lowering in `tools/ffx_dispatch_aot.py` and `tools/ffx_scratch_reference.py` with the AMD RDNA 2 ISA specification for 16-bit signed `SOPK` immediates and `SCC` status-flag updates: - **Sign-extend 16-bit `SOPK` immediates (`simm16`):** LLVM's AMDGPU disassembler prints negative 16-bit `SOPK` immediates as unsigned 16-bit hex tokens (`0x8000`–`0xffff`). Previously `word()` zero-extended them (`s_movk_i32 s0, -1` emitted `0xffffu`, `s_addk_i32 s4, -2` added `+65534`, and `s_mulk_i32 s7, -3` multiplied by `+65533`), corrupting bits `[31:16]`. `simm16()` now sign-extends 16-bit immediates to 32-bit two's complement in both `ffx_dispatch_aot.py` and `ffx_scratch_reference.py` and rejects out-of-range immediates. - **Preserve RDNA 2 `SCC` semantics across scalar ALU instructions:** - **Unsigned carry / 36-bit shift-add overflow:** `s_add_u32` (`sadd_u`) and `s_lshl4_add_u32` (`slshl4_add_u`, capturing both shifted-out bits `a >> 28` and post-shift carry `r < (a << 4)`). - **Signed 32-bit overflow:** `s_addk_i32` (`sadd_i`) and `s_sub_i32` (`ssub_i`). - **Relational winner (`S0 < S1` / `S0 > S1`):** `s_min_u32`, `s_max_u32`, `s_min_i32`, and `s_max_i32` (`scc = 0` on ties or when `S1` wins). - **Nonzero result (`scc = dest != 0u`):** `s_and_b32`, `s_or_b32`, `s_xor_b32`, `s_lshl_b32` / `s_lshr_b32` (with 5-bit shift-count masking), `s_abs_i32` (`sabs_i`, two's-complement wrap-safe on `0x80000000`), and `s_bfe_u32` / `s_bfe_i32`. - **Non-`SCC` operations:** `s_mov_b32`, `s_mov_b64`, `s_movk_i32`, `s_mulk_i32`, `s_mul_i32`, `s_mul_hi_u32`, `s_brev_b32`, `s_cselect_b32`, and all vector (`v_*`) instructions leave `scc` untouched. - Added `s_cmp_eq_i32` / `s_cmp_lg_i32` handling in `predecessor()` matching `translate()`. ## Verification Tested on Linux x86_64 (Python 3.13.5, LLVM 19.1.7, Clang 19.1.7) on top of `03c61dc`: - `python3 -B tests/ffx_dispatch_aot_check.py llvm-mc-19 llvm-objcopy-19 libLLVM-19.so` — passed (boundary cross-checks against 64-bit/arbitrary-precision RDNA 2 ISA models, negative controls rejecting out-of-range `SOPK` immediates, assembled `gfx1013` fixtures verifying `1 -> 0` and `0 -> 1` `SCC` transitions and SGPR aliasing through `s_cselect_b32` and `v_cndmask_b32_e64 ..., src_scc` in both `translate` and `predecessor`, and `ffx_scratch_reference.network` `SOPK` parity). - `python3 -B tests/pixel_aot_check.py binary llvm-mc-19 llvm-objcopy-19 libLLVM-19.so` — passed. - `python3 -B tests/script_state_check.py`, `tests/bcpack_check.py`, `tests/resource_ranges_check.py`, `tests/dxcontainer_part_offsets_check.py`, `tests/layout_chunks_check.py` — all passed. - Compiled the `tests/ffx_dispatch_io.hlsli` integer helpers as C++20 with `clang++-19 -std=c++20 -O2 -Wall -Wextra -Wpedantic -Werror -fsanitize=undefined,address` across all signed/unsigned boundary pairs (including `0x80000000`, `0x10000000 << 4`, and aliased destinations) — 0 warnings, 0 sanitizer errors. - Windows C++, DXC HLSL (`cs_6_0`), WARP, and hardware checks were not run locally on Linux; relying on the `build-and-test` Windows CI job to verify desktop compilation and WARP execution. ## Evidence and follow-up - All new fixtures in `tests/ffx_dispatch_aot_check.py` are original MIT-licensed `gfx1013` assembly snippets assembled from source at test time with `llvm-mc`. - Instruction and `SCC` definitions follow the [AMD RDNA 2 Instruction Set Architecture Reference Guide](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf). - This contribution was AI-assisted. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-24 06:41:56 +00:00
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
R0Y00/Xbox2Windows!231
No description provided.