feat: 16-bit integer compares, s_pack_*_b32_b16 and global addtid in the RDNA 2 interpreter and HLSL — 1,138/1,490 #226

Manually merged
github merged 1 commit from gh-pr/226/head into gh-pr/226/base 2026-11-26 05:38:45 +00:00
Owner

@⁠KainanP on GitHub · 2026-11-25 19:15 UTC · original
KainanP:feat/i16-compares-s-pack → main · +582 −39 · 21 files · 1 commits · merged 2026-11-25 19:38 UTC

Supersedes #221 (closed by the maintainer for conflicts; rebased onto main cf84f8d as asked)

The import lines of rdna2_exec.py, rdna2_exec_vop3.py and rdna2_hlsl.py now carry both the f16 modules (#218, #219) and the 16-bit compare module; README numbers recomputed.

Change

Three groups of RDNA 2 instructions that the reference interpreter and the HLSL recompiler refused until now, all small 16-bit or lane-id data operations:

  1. 16-bit integer compares v_cmp[x]_{lt,eq,le,gt,ne,ge}_{i16,u16} (there are no f/t forms for these types on RDNA 2), in VOPC, VOP3 and SDWA encodings: S0[15:0] against S1[15:0], signed or unsigned; the high halves are ignored. The mask is written whole like the 32-bit compares (inactive lanes cleared, ISA §3.9); v_cmp writes VCC (VOPC) or the SGPR (VOP3), v_cmpx writes EXEC only.
    • Inline float constants (codes 240-248) give their f32 bit pattern, of which these 16-bit compares read bits 15:0 (0.5 = 0x3F000000 compares as 0x0000, 1/(2π) = 0x3E22F983 as 0xF983), not the f16 value. In SDWA the pattern goes through src_sel like a register. See "Inline floats on 16-bit integer sources" below; the first version of this PR used the f16 value and is changed by hardware data.
    • Refused by name instead of guessed (rule 3): VOP3 with op_sel set (ISA §6.2.5 says OPSEL is not usable for VOPC in VOP3 encoding, so which half the hardware reads is not documented).
    • DPP forms of VOPC stay refused, as for every VOPC on main.
  2. s_pack_ll_b32_b16 / s_pack_lh_b32_b16 / s_pack_hh_b32_b16 (SOP2 50-52): {S1[15:0], S0[15:0]}, {S1[31:16], S0[15:0]}, {S1[31:16], S0[31:16]}, SCC unchanged. An inline float is its f32 pattern on every source, including those AMD's RDNA 2 XML sizes at 16 bits (both sources of ll, S0 of lh): LLVM declares the packs SOP2_32, and the 16-bit integer VALU sources measured on gfx1036 read the f32 bits. This is assumed for s_pack (not measured directly; the earlier version refused it). An inline float on a source
  3. global_load_dword_addtid / global_store_dword_addtid: no VGPR address, SADDR(64) + inst_offset + lane × 4. SADDR = off is refused by name (the ISA gives no address for it). In HLSL the address uses lane; wave64 programs accept it (lane = SV_GroupIndex 0-63, the TID in the 64-lane wave), and graphics shaders using it take the multi-lane path (it is listed with the other lane-dependent operations).

Coverage of the reference interpreter (rdna2_exec.coverage()): 1,085 → 1,138 of 1,490 encodings (+48 compares, +3 packs, +2 addtid); all 9 READMEs go from 73% to 76%. The suite collects 1,329 tests, so the 9 READMEs say 1300+. docs/images/carte-avancee.svg and traduction.svg are not regenerated: they need a test package and still show 521/1487 from long before this PR (none of the recent RDNA 2 PRs regenerated them either).

Linked issue: none (follows the RDNA 2 interpreter work of #133-#151).

Verification

Full suite on this branch rebased onto d3344ed (cf84f8d only adds #225, Rust files under third_party/victory; after the final rebase test_readme_sync.py and ruff pass again and the suite still collects 1,329 tests). Runtime built from this branch (cmake -S runtime -B runtime/build -A x64, --config Release), ANYXBOXX_RUNTIME=<worktree>/runtime/build/bin. Adapters as reported by anyxboxx_gpu_run.exe --adaptateur:

default (NVIDIA GeForce RTX 4060 Laptop GPU): 1312 passed, 17 skipped
ANYXBOXX_GPU=warp (Microsoft Basic Render Driver): 1312 passed, 17 skipped
ANYXBOXX_GPU=Intel (Intel UHD Graphics): 1310 passed, 19 skipped
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!

The skips are the usual ones on this machine (Rust crates and TomBox DLLs not built, no Vulkan SDK for spirv-as; Intel also skips the FP64 tests).

Oracle and test data

  • HLSL checked against the reference interpreter, byte for byte, on RTX, WARP and Intel UHD:
    • test_16_bit_compares_match_interpreter_on_gpu: all 48 compare encodings (12 × v_cmp/v_cmpx × VOPC/VOP3) under a partial EXEC (0x0F0FF0F3), plus three SDWA compares (WORD_1/WORD_0 selects, byte selects with sext, an SGPR destination), on low halves at the boundaries 0x0000/0x0001/0x7FFE/0x7FFF/0x8000/0x8001/0xFFFE/0xFFFF under junk high halves; the 51 masks are stored and compared.
    • inline float constants (VOPC, VOP3, v_cmpx, and SDWA with src0_sel) and literals, a wave64 program (VOPC/VOP3/cmpx with a partial 64-bit EXEC), the packs (with SCC kept), and addtid loads and stores in wave32 and wave64 under partial EXEC.
  • Interpreter expectations are written by hand: the signed and unsigned relation of each (S0, S1) pair (RELATIONS in tests/test_rdna2_exec_cmp16.py), the packed words of 0x12345678/0x9ABCDEF0, and the addtid addresses.
  • All test data is built in the tests; no external data.
  • Failure case: the 62 new tests in tests/test_rdna2_exec_cmp16.py and tests/test_rdna2_exec_pack_addtid.py all fail on main (checked with main's PrototypeScarlett/: the compare file does not import, the pack/addtid file has 11 of 11 CPU tests failing).

Sources (labelled per docs/research/rdna2-sources.md; printed page numbers of the archived PDF, SHA-256 22aec03f…4795):

  • AMD, "RDNA 2" ISA, November 2020: table 61 "VOPC Compare Opcodes" (p. 156-159, 137 V_CMP_LT_I16 … 190 V_CMPX_GE_U16, "D[threadId] = (S0 < S1)"; table 60's hex ranges 0xA0-0xBF disagree with table 61 and LLVM, the opcodes here are LLVM's 0x89-0xBE); §3.9 (p. 19-20, "VCC is fully written"); §6.2.5 "OPSEL Field Restrictions" (p. 44); §12.1 SOP2 opcodes 50-52 (p. 106); §12.18.3 GLOBAL opcodes 22-23 (p. 230, "memory_Addr = sgpr_addr(64) + inst_offset(12) + tid*4").
  • AMD machine-readable ISA, amdgpu_isa_rdna2.xml (GPUOpen): V_CMP_*_I16/U16 sources have OperandSize 16; S_PACK_LL SSRC0/SSRC1 16, S_PACK_LH SSRC0 16 / SSRC1 32, S_PACK_HH 32/32; GLOBAL_LOAD_DWORD_ADDTID: "The memory base address is provided in a scalar register and the lane ID is used as an offset".
  • LLVM 20.1.8 (cross-check): SOPInstructions.td declares the packs SOP2_32; llvm/test/MC/AMDGPU/gfx10_asm_vopc*.s has no op_sel on VOPC in VOP3 for GFX10, and encodes v_cmpx_*_e64 with vdst = exec_lo (used in the tests).

Inline floats on 16-bit integer sources

  • LLVM 20.1.8, SIInstrInfo::isInlineConstant, case OPERAND_REG_*_INT16: "in the case of 16-bit integer operations, the "floating point" values appear to not work. It seems read the low 16-bits of 32-bit immediates" (bugzilla 46302). Its disassembler still prints the f16 value (decodeFPImmed width 16), which is what the first version of this PR followed.
  • Measured on RDNA 2 (gfx1036) by the Leonida project for two other 16-bit integer sources: v_cvt_f16_u16_sdwa d, 1.0 src0_sel:WORD_1 converts 0x3F80 (the f32 pattern, then the select), and an inline float in S2 of v_mad_i32_i16 adds its f32 pattern (1.0 adds 0x3F80): design.md L331, design.md L359.
  • The compares and s_pack themselves are not measured. The direct case is requested as PortBridge #83 (VOPC/VOP3 v_cmp_eq_u16 with 0.5, -4.0 and 1/(2π), and five s_pack forms); its expected outputs under the f32-bits rule are what this PR now gives.

Checklist

  • The PR covers a single reviewable behaviour. (Three small groups of 16-bit / lane-id data instructions in one PR, as one coverage step; I can split it if you prefer.)
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success. (Unsettled forms raise Untranslated / Untranslatable naming the instruction.)
  • README: the same change in all 9 languages (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none.

Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP.

🤖 Generated with Claude Code

**@⁠KainanP** on GitHub · 2026-11-25 19:15 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/226) `KainanP:feat/i16-compares-s-pack` → `main` · +582 −39 · 21 files · 1 commits · merged 2026-11-25 19:38 UTC Supersedes #221 (closed by the maintainer for conflicts; rebased onto main cf84f8d as asked) The import lines of `rdna2_exec.py`, `rdna2_exec_vop3.py` and `rdna2_hlsl.py` now carry both the f16 modules (#218, #219) and the 16-bit compare module; README numbers recomputed. ## Change Three groups of RDNA 2 instructions that the reference interpreter and the HLSL recompiler refused until now, all small 16-bit or lane-id data operations: 1. **16-bit integer compares** `v_cmp[x]_{lt,eq,le,gt,ne,ge}_{i16,u16}` (there are no `f`/`t` forms for these types on RDNA 2), in VOPC, VOP3 and SDWA encodings: `S0[15:0]` against `S1[15:0]`, signed or unsigned; the high halves are ignored. The mask is written whole like the 32-bit compares (inactive lanes cleared, ISA §3.9); `v_cmp` writes VCC (VOPC) or the SGPR (VOP3), `v_cmpx` writes EXEC only. - Inline float constants (codes 240-248) give their **f32 bit pattern**, of which these 16-bit compares read bits 15:0 (`0.5` = `0x3F000000` compares as `0x0000`, `1/(2π)` = `0x3E22F983` as `0xF983`), not the f16 value. In SDWA the pattern goes through `src_sel` like a register. See "Inline floats on 16-bit integer sources" below; the first version of this PR used the f16 value and is changed by hardware data. - Refused by name instead of guessed (rule 3): VOP3 with `op_sel` set (ISA §6.2.5 says OPSEL is not usable for VOPC in VOP3 encoding, so which half the hardware reads is not documented). - DPP forms of VOPC stay refused, as for every VOPC on main. 2. **`s_pack_ll_b32_b16` / `s_pack_lh_b32_b16` / `s_pack_hh_b32_b16`** (SOP2 50-52): `{S1[15:0], S0[15:0]}`, `{S1[31:16], S0[15:0]}`, `{S1[31:16], S0[31:16]}`, SCC unchanged. An inline float is its f32 pattern on every source, including those AMD's RDNA 2 XML sizes at 16 bits (both sources of `ll`, S0 of `lh`): LLVM declares the packs `SOP2_32`, and the 16-bit integer VALU sources measured on gfx1036 read the f32 bits. This is assumed for `s_pack` (not measured directly; the earlier version refused it). An inline float on a source 3. **`global_load_dword_addtid` / `global_store_dword_addtid`**: no VGPR address, `SADDR(64) + inst_offset + lane × 4`. `SADDR = off` is refused by name (the ISA gives no address for it). In HLSL the address uses `lane`; wave64 programs accept it (`lane` = `SV_GroupIndex` 0-63, the TID in the 64-lane wave), and graphics shaders using it take the multi-lane path (it is listed with the other lane-dependent operations). Coverage of the reference interpreter (`rdna2_exec.coverage()`): **1,085 → 1,138 of 1,490 encodings** (+48 compares, +3 packs, +2 addtid); all 9 READMEs go from 73% to 76%. The suite collects 1,329 tests, so the 9 READMEs say `1300+`. `docs/images/carte-avancee.svg` and `traduction.svg` are not regenerated: they need a test package and still show 521/1487 from long before this PR (none of the recent RDNA 2 PRs regenerated them either). Linked issue: none (follows the RDNA 2 interpreter work of #133-#151). ## Verification Full suite on this branch rebased onto `d3344ed` (`cf84f8d` only adds #225, Rust files under `third_party/victory`; after the final rebase `test_readme_sync.py` and ruff pass again and the suite still collects 1,329 tests). Runtime built from this branch (`cmake -S runtime -B runtime/build -A x64`, `--config Release`), `ANYXBOXX_RUNTIME=<worktree>/runtime/build/bin`. Adapters as reported by `anyxboxx_gpu_run.exe --adaptateur`: ``` default (NVIDIA GeForce RTX 4060 Laptop GPU): 1312 passed, 17 skipped ANYXBOXX_GPU=warp (Microsoft Basic Render Driver): 1312 passed, 17 skipped ANYXBOXX_GPU=Intel (Intel UHD Graphics): 1310 passed, 19 skipped python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! ``` The skips are the usual ones on this machine (Rust crates and TomBox DLLs not built, no Vulkan SDK for `spirv-as`; Intel also skips the FP64 tests). ## Oracle and test data - HLSL checked against the reference interpreter, byte for byte, on RTX, WARP and Intel UHD: - `test_16_bit_compares_match_interpreter_on_gpu`: all 48 compare encodings (12 × `v_cmp`/`v_cmpx` × VOPC/VOP3) under a partial EXEC (`0x0F0FF0F3`), plus three SDWA compares (`WORD_1`/`WORD_0` selects, byte selects with `sext`, an SGPR destination), on low halves at the boundaries `0x0000/0x0001/0x7FFE/0x7FFF/0x8000/0x8001/0xFFFE/0xFFFF` under junk high halves; the 51 masks are stored and compared. - inline float constants (VOPC, VOP3, `v_cmpx`, and SDWA with `src0_sel`) and literals, a wave64 program (VOPC/VOP3/cmpx with a partial 64-bit EXEC), the packs (with SCC kept), and addtid loads and stores in wave32 and wave64 under partial EXEC. - Interpreter expectations are written by hand: the signed and unsigned relation of each (S0, S1) pair (`RELATIONS` in `tests/test_rdna2_exec_cmp16.py`), the packed words of `0x12345678`/`0x9ABCDEF0`, and the addtid addresses. - All test data is built in the tests; no external data. - Failure case: the 62 new tests in `tests/test_rdna2_exec_cmp16.py` and `tests/test_rdna2_exec_pack_addtid.py` all fail on main (checked with main's `PrototypeScarlett/`: the compare file does not import, the pack/addtid file has 11 of 11 CPU tests failing). Sources (labelled per `docs/research/rdna2-sources.md`; printed page numbers of the archived PDF, SHA-256 `22aec03f…4795`): - AMD, "RDNA 2" ISA, November 2020: table 61 "VOPC Compare Opcodes" (p. 156-159, `137 V_CMP_LT_I16` … `190 V_CMPX_GE_U16`, "D[threadId] = (S0 < S1)"; table 60's hex ranges `0xA0-0xBF` disagree with table 61 and LLVM, the opcodes here are LLVM's `0x89-0xBE`); §3.9 (p. 19-20, "VCC is fully written"); §6.2.5 "OPSEL Field Restrictions" (p. 44); §12.1 SOP2 opcodes 50-52 (p. 106); §12.18.3 GLOBAL opcodes 22-23 (p. 230, "memory_Addr = sgpr_addr(64) + inst_offset(12) + tid*4"). - AMD machine-readable ISA, `amdgpu_isa_rdna2.xml` (GPUOpen): `V_CMP_*_I16/U16` sources have `OperandSize` 16; `S_PACK_LL` SSRC0/SSRC1 16, `S_PACK_LH` SSRC0 16 / SSRC1 32, `S_PACK_HH` 32/32; `GLOBAL_LOAD_DWORD_ADDTID`: "The memory base address is provided in a scalar register and the lane ID is used as an offset". - LLVM 20.1.8 (cross-check): `SOPInstructions.td` declares the packs `SOP2_32`; `llvm/test/MC/AMDGPU/gfx10_asm_vopc*.s` has no `op_sel` on VOPC in VOP3 for GFX10, and encodes `v_cmpx_*_e64` with `vdst = exec_lo` (used in the tests). ### Inline floats on 16-bit integer sources - LLVM 20.1.8, `SIInstrInfo::isInlineConstant`, case `OPERAND_REG_*_INT16`: "in the case of 16-bit integer operations, the "floating point" values appear to not work. It seems read the low 16-bits of 32-bit immediates" (bugzilla 46302). Its disassembler still prints the f16 value (`decodeFPImmed` width 16), which is what the first version of this PR followed. - Measured on RDNA 2 (gfx1036) by the Leonida project for two other 16-bit integer sources: `v_cvt_f16_u16_sdwa d, 1.0 src0_sel:WORD_1` converts `0x3F80` (the f32 pattern, then the select), and an inline float in S2 of `v_mad_i32_i16` adds its f32 pattern (`1.0` adds `0x3F80`): [design.md L331](https://github.com/ubiquitarianism/leonida/blob/9f9a7c5f30f8559c52675780cf84004571b8d6e5/docs/design.md#L331), [design.md L359](https://github.com/ubiquitarianism/leonida/blob/9f9a7c5f30f8559c52675780cf84004571b8d6e5/docs/design.md#L359). - The compares and `s_pack` themselves are not measured. The direct case is requested as [PortBridge #83](https://github.com/akimfomin/PortBridge/issues/83) (VOPC/VOP3 `v_cmp_eq_u16` with 0.5, -4.0 and 1/(2π), and five `s_pack` forms); its expected outputs under the f32-bits rule are what this PR now gives. ## Checklist - [x] The PR covers a single reviewable behaviour. (Three small groups of 16-bit / lane-id data instructions in one PR, as one coverage step; I can split it if you prefer.) - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. (Unsettled forms raise `Untranslated` / `Untranslatable` naming the instruction.) - [x] README: the same change in all 9 languages (`test_readme_sync.py` checks it). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none. Made with AI assistance: Claude Code with Claude Opus 5.5, for KainanP. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- v_cmp[x]_{lt,eq,le,gt,ne,ge}_{i16,u16} in VOPC, VOP3 and SDWA: S0[15:0]
  against S1[15:0], signed or unsigned, mask fully written (inactive lanes
  cleared). An inline float constant is its float32 pattern, of which the
  compare reads bits 15:0 (0.5 compares as 0x0000): LLVM's SIInstrInfo notes
  that 16-bit integer operations read the low 16 bits of the 32-bit
  immediate, and on gfx1036 v_cvt_f16_u16_sdwa and S2 of v_mad_i32_i16 read
  the float32 bits (ubiquitarianism/leonida docs/design.md at 9f9a7c5, lines
  331 and 359); the compares themselves are requested as PortBridge #83. In
  SDWA the pattern goes through src_sel like a register. VOP3 op_sel is
  refused by name (ISA section 6.2.5: OPSEL not usable on VOPC in VOP3).
  The SDWA forms take their operand types from the shared
  operand_float_flags (#215): 16-bit integer sources, so sext applies and
  neg/abs do not.
- s_pack_ll/lh/hh_b32_b16 (SOP2 50-52): D = {S1 half, S0 half}, SCC kept.
  An inline float is its float32 pattern on every source, also those AMD's
  RDNA 2 XML sizes at 16 bits: LLVM declares the packs SOP2_32, and the
  16-bit integer VALU sources measured on gfx1036 read the float32 bits
  (assumed for s_pack, PortBridge #83).
- global_load/store_dword_addtid: address = SADDR(64) + offset + lane x 4
  (ISA section 12.18.3); SADDR off refused by name. Marked lane-dependent for
  graphics shaders; allowed in wave64 HLSL (lane = SV_GroupIndex).

Interpreter coverage 1,085 -> 1,138 of 1,490 encodings (README 73% -> 76%);
test count 1300+. New tests in
tests/test_rdna2_exec_cmp16.py and tests/test_rdna2_exec_pack_addtid.py
(62 tests, all failing without this change), with byte-for-byte GPU parity
in wave32 and wave64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:38:43 +00:00
github manually merged commit a886815d5a into gh-pr/226/base 2026-11-26 05:38:45 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.