startup: sign-extend the SOPK i32 immediates in gfx2hlsl #2116

Manually merged
github merged 3 commits from gh-pr/2116/head into gh-pr/2116/base 2026-11-26 23:57:47 +00:00
Owner

@⁠JB2WIN on GitHub · 2026-11-25 00:47 UTC · original
JB2WIN:startup/gfx2hlsl-sopk-signext → main · +151 −1 · 6 files · 3 commits · merged 2026-11-25 01:19 UTC

Problem

tools/startup/gfx2hlsl.py does not sign-extend the 16-bit SOPK immediates of the i32 forms, so it computes wrong values without any refusal:

Input main Correct
s_movk_i32 s0, 0x8000 (constant path) 0x00008000 0xffff8000
s_movk_i32 m0, 0x8000 (run-time path) 0x00008000 0xffff8000
s_cmpk_lt_i32 s5, 0xffff with s5 = 0 SCC = 1 SCC = 0 (0 < -1 is false)

The cause is how the immediates are read:

  • tools/disasm_rdna2.py prints SOPK immediates as unsigned hex, as LLVM 15 does.
  • gfx2hlsl read s_movk_i32 through int(srcs[0], 0) and the {0} scalar-table entry.
  • The s_cmpk_* handlers came from the same factory as s_cmp_*.

So none of them applied the sign extension the i32 forms define.

Semantics

  • AMD RDNA 2 ISA (30 November 2020, section 12.2, SOPK): S_MOVK_I32 is D.i32 = signext(SIMM16[15:0]). The S_CMPK_*_I32 forms compare against signext(SIMM16[15:0]), and the S_CMPK_*_U32 forms against SIMM16[15:0] without extension.
  • LLVM 15.0.7 SOPInstructions.td: s_movk_i32 and s_cmovk_i32 take s16imm. Every s_cmpk_*_i32 is SOPK_SCC <…, 1> (isSignExt, s16imm), and every s_cmpk_*_u32 is SOPK_SCC <…, 0> (u16imm).
  • tools/pixel_aot.py (sopk_imm16) already applies the same rule.

Change

  • Helper: one new helper, sopk_imm(token, signed), accepts a 16-bit immediate and sign- or zero-extends it. It refuses values outside 16 bits and register operands; the translator accepted both before.
  • s_movk_i32: it uses the helper through scalar(), which covers both the constant and the run-time path.
  • s_cmpk_*: these get their own handler factory: _i32 is signed, _u32 unsigned.
  • s_cmovk_i32: not lowered, and it stays refused; the check asserts this.

Test

New device-free check tests/startup_gfx2hlsl_sopk_signext_check.py, registered as startup_gfx2hlsl_sopk_signext and listed in the pure-Python job and AGENTS.md.

  • Coverage: 1,152 comparisons against an independent Python reference. They cover s_movk_i32 in both paths with 0x0, 0x7fff, 0x8000 and 0xffff, and every s_cmpk_*_i32 and _u32 form with boundary and negative immediates. There are also 10 refusals.
  • On main: the check fails with 130 mismatches and also reports the accepted out-of-range and register operands.
  • Mutations:
    • Without the sign extension the check fails with 124 mismatches.
    • With the sign extension also applied to _u32, it fails with 117.

Results:

  • After rebasing on c75f6659, all tests/startup_gfx2hlsl_*_check.py, tests/startup_translator_check.py and tests/docs_links_check.py pass.
  • ctest --preset dev --no-tests=error -R '^startup_' was run on an un-rebuilt tree before the rebase. The new test passed, and the remaining failures were draw tests whose binaries were not rebuilt. They fail the same way with main's gfx2hlsl.py.

Related: #2102 (s_addk_i32/s_mulk_i32, from me) already sign-extends its immediate and is checked for it. Once both have landed, it could switch to this helper.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠JB2WIN** on GitHub · 2026-11-25 00:47 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2116) `JB2WIN:startup/gfx2hlsl-sopk-signext` → `main` · +151 −1 · 6 files · 3 commits · merged 2026-11-25 01:19 UTC ## Problem `tools/startup/gfx2hlsl.py` does not sign-extend the 16-bit SOPK immediates of the i32 forms, so it computes wrong values without any refusal: | Input | `main` | Correct | |---|---|---| | `s_movk_i32 s0, 0x8000` (constant path) | `0x00008000` | `0xffff8000` | | `s_movk_i32 m0, 0x8000` (run-time path) | `0x00008000` | `0xffff8000` | | `s_cmpk_lt_i32 s5, 0xffff` with `s5 = 0` | SCC = 1 | SCC = 0 (0 < -1 is false) | The cause is how the immediates are read: - `tools/disasm_rdna2.py` prints SOPK immediates as unsigned hex, as LLVM 15 does. - gfx2hlsl read `s_movk_i32` through `int(srcs[0], 0)` and the `{0}` scalar-table entry. - The `s_cmpk_*` handlers came from the same factory as `s_cmp_*`. So none of them applied the sign extension the i32 forms define. ## Semantics - **AMD RDNA 2 ISA** (30 November 2020, section 12.2, SOPK): `S_MOVK_I32` is `D.i32 = signext(SIMM16[15:0])`. The `S_CMPK_*_I32` forms compare against `signext(SIMM16[15:0])`, and the `S_CMPK_*_U32` forms against `SIMM16[15:0]` without extension. - **LLVM 15.0.7 `SOPInstructions.td`:** `s_movk_i32` and `s_cmovk_i32` take `s16imm`. Every `s_cmpk_*_i32` is `SOPK_SCC <…, 1>` (isSignExt, `s16imm`), and every `s_cmpk_*_u32` is `SOPK_SCC <…, 0>` (`u16imm`). - **`tools/pixel_aot.py`** (`sopk_imm16`) already applies the same rule. ## Change - **Helper:** one new helper, `sopk_imm(token, signed)`, accepts a 16-bit immediate and sign- or zero-extends it. It refuses values outside 16 bits and register operands; the translator accepted both before. - **`s_movk_i32`:** it uses the helper through `scalar()`, which covers both the constant and the run-time path. - **`s_cmpk_*`:** these get their own handler factory: `_i32` is signed, `_u32` unsigned. - **`s_cmovk_i32`:** not lowered, and it stays refused; the check asserts this. ## Test New device-free check `tests/startup_gfx2hlsl_sopk_signext_check.py`, registered as `startup_gfx2hlsl_sopk_signext` and listed in the pure-Python job and `AGENTS.md`. - **Coverage:** 1,152 comparisons against an independent Python reference. They cover `s_movk_i32` in both paths with `0x0`, `0x7fff`, `0x8000` and `0xffff`, and every `s_cmpk_*_i32` and `_u32` form with boundary and negative immediates. There are also 10 refusals. - **On `main`:** the check fails with 130 mismatches and also reports the accepted out-of-range and register operands. - **Mutations:** - Without the sign extension the check fails with 124 mismatches. - With the sign extension also applied to `_u32`, it fails with 117. Results: - After rebasing on `c75f6659`, all `tests/startup_gfx2hlsl_*_check.py`, `tests/startup_translator_check.py` and `tests/docs_links_check.py` pass. - `ctest --preset dev --no-tests=error -R '^startup_'` was run on an un-rebuilt tree before the rebase. The new test passed, and the remaining failures were draw tests whose binaries were not rebuilt. They fail the same way with `main`'s `gfx2hlsl.py`. **Related:** #2102 (`s_addk_i32`/`s_mulk_i32`, from me) already sign-extends its immediate and is checked for it. Once both have landed, it could switch to this helper. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gfx2hlsl decoded the 16-bit SOPK immediate with int(token, 0) and no
sign extension. s_movk_i32 s0, 0x8000 produced 0x00008000 instead of
0xffff8000, and s_cmpk_*_i32 compared against the zero-extended field,
so s_cmpk_lt_i32 s0, 0xffff with s0 = 0 set SCC (0 < 65535) where the
hardware compares against -1 and clears it.

Semantics: RDNA 2 ISA SOPK, S_MOVK_I32 and S_CMPK_*_I32 use
signext(SIMM16[15:0]); S_CMPK_*_U32 use SIMM16[15:0] zero-extended.
LLVM 15 SOPInstructions.td agrees: the _I32 forms and s_movk_i32 take
s16imm (isSignExt = 1), the _U32 forms take u16imm (isSignExt = 0).
The _U32 forms were already correct and stay zero-extended.

One helper, sopk_imm(), now decodes the field for s_movk_i32 (constant
and run-time paths) and for both s_cmpk families, and refuses values
outside 16 bits and non-literal operands, as pixel_aot already does.
s_cmovk_i32 has no lowering here and is still refused.

The new check evaluates the emitted text against a Python reference
(1152 compare cases, both movk paths, 10 refusals). It fails on main
(130 mismatches) and passes with this change; removing the sign
extension or applying it to the _U32 forms turns it red again.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 23:57:46 +00:00
github manually merged commit 02f358b1d3 into gh-pr/2116/base 2026-11-26 23:57:47 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-25 01:16 UTC · original

I combined this with current main and retained the scalar arity diagnostics. Empty s_movk now refuses before reading its destination. The AMD RDNA 2 SOPK table specifies signed extension for I32 and zero extension for U32. Retested 1,152 comparisons, both movk paths, 11 refusals, and the existing arity, mask-overwrite and startup translator checks.

**@⁠R0Y00** on GitHub · 2026-11-25 01:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/2116#issuecomment-7149376875) I combined this with current main and retained the scalar arity diagnostics. Empty s_movk now refuses before reading its destination. The AMD RDNA 2 SOPK table specifies signed extension for I32 and zero extension for U32. Retested 1,152 comparisons, both movk paths, 11 refusals, and the existing arity, mask-overwrite and startup translator checks.
This discussion has been locked. Commenting is limited to contributors.
No description provided.