startup: lower s_addk_i32 and s_mulk_i32 in gfx2hlsl #2095

Merged
JB2WIN merged 1 commit from upstream-pr/2102 into main 2026-11-25 02:31:04 +00:00
JB2WIN commented 2026-11-25 00:22:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-25T00:22:05Z.


Refs #1723

Change

tools/startup/gfx2hlsl.py now lowers the SOPK accumulate forms s_addk_i32 and s_mulk_i32. A local census of refused pairs (same normalised counting as --refusals) counts them in 77 and 79 pairs, and no open PR covered them.

Semantics. The source is the AMD RDNA 2 ISA (30 November 2020, section 12.2, SOPK opcodes 15 and 16):

  • s_addk_i32: D = D + signext(SIMM16), and SCC is set to the signed overflow (tmp[31] == SIMM16[15] && tmp[31] != D[31]). The PDF text is split across columns, so I checked the formula against LLVM 15 SOPInstructions.td.
  • s_mulk_i32: D = D * signext(SIMM16). The ISA lists no SCC write.

LLVM 15 declares Defs = [SCC] for both forms. The lowering follows the ISA and leaves the emitted scc unchanged after mulk. It does drop the SCC uniformity proof there, the same way the existing unmodelled-SCC cases (s_wqm) do, so a later uniform-SCC use is not trusted. If hardware does clobber SCC on mulk, a later SCC reader would be wrong; that is not measured.

Handling.

  • addk computes SCC from the old D before D is written.
  • A known-constant D folds both the result and SCC.
  • D in Stage.masks, with descriptor or pointer tracking, or as a copy of exec is refused before any mask flag or symbol changes.
  • Uniformity follows the old D, as in #1931.

Spelling. LLVM 15 prints the immediate as unsigned hex, the same as tools/disasm_rdna2.py: s_addk_i32 s0, 0x8000 is 00 80 80 b7, and s_mulk_i32 s0, -2 becomes 0xfffe (fe ff 00 b8). Only 0x0–0xffff is accepted.

Test

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

  • Cases: 68 checks against an independent Python reference:
    • immediates 0, 1, 0x7fff, 0x8000 and 0xffff;
    • overflow upward (0x7fffffff + 1) and downward (0x80000000 + -1), and no overflow;
    • a later SCC consumer;
    • mulk leaving an earlier SCC alone;
    • constant folding;
    • refusals for mask, descriptor/pointer and exec-copy destinations.
  • Mutations: 7, each failing the check: no sign extension, unsigned carry as SCC, wrong constant SCC, mulk setting SCC, missing mask refusal, missing descriptor refusal, and the lowering removed.
  • Against main: with main's gfx2hlsl.py the check fails with Unsupported: s_addk_i32.

Results after rebasing on 32595645:

  • 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_gfx2hlsl' passed 46/46 before the rebase.

Side note, not changed here. The existing s_movk_i32 path and the s_cmpk_* immediates do not appear to sign-extend SIMM16: s_movk_i32 s0, 0x8000 would give 0x00008000 instead of 0xffff8000. I'll check that separately rather than mix it in.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6594218162 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2102). Originally posted by [JB2WIN](https://github.com/JB2WIN) on 2026-11-25T00:22:05Z. --- Refs #1723 ## Change `tools/startup/gfx2hlsl.py` now lowers the SOPK accumulate forms `s_addk_i32` and `s_mulk_i32`. A local census of refused pairs (same normalised counting as `--refusals`) counts them in 77 and 79 pairs, and no open PR covered them. **Semantics.** The source is the AMD RDNA 2 ISA (30 November 2020, section 12.2, SOPK opcodes 15 and 16): - `s_addk_i32`: `D = D + signext(SIMM16)`, and SCC is set to the signed overflow (`tmp[31] == SIMM16[15] && tmp[31] != D[31]`). The PDF text is split across columns, so I checked the formula against LLVM 15 `SOPInstructions.td`. - `s_mulk_i32`: `D = D * signext(SIMM16)`. The ISA lists no SCC write. LLVM 15 declares `Defs = [SCC]` for both forms. The lowering follows the ISA and leaves the emitted `scc` unchanged after `mulk`. It does drop the SCC uniformity proof there, the same way the existing unmodelled-SCC cases (`s_wqm`) do, so a later uniform-SCC use is not trusted. If hardware does clobber SCC on `mulk`, a later SCC reader would be wrong; that is not measured. **Handling.** - addk computes SCC from the old `D` before `D` is written. - A known-constant `D` folds both the result and SCC. - `D` in `Stage.masks`, with descriptor or pointer tracking, or as a copy of `exec` is refused before any mask flag or symbol changes. - Uniformity follows the old `D`, as in #1931. **Spelling.** LLVM 15 prints the immediate as unsigned hex, the same as `tools/disasm_rdna2.py`: `s_addk_i32 s0, 0x8000` is `00 80 80 b7`, and `s_mulk_i32 s0, -2` becomes `0xfffe` (`fe ff 00 b8`). Only `0x0`–`0xffff` is accepted. ## Test New device-free check `tests/startup_gfx2hlsl_sopk_accum_check.py`, registered as `startup_gfx2hlsl_sopk_accum` and listed in the pure-Python job and `AGENTS.md`. - **Cases:** 68 checks against an independent Python reference: - immediates 0, 1, `0x7fff`, `0x8000` and `0xffff`; - overflow upward (`0x7fffffff + 1`) and downward (`0x80000000 + -1`), and no overflow; - a later SCC consumer; - `mulk` leaving an earlier SCC alone; - constant folding; - refusals for mask, descriptor/pointer and `exec`-copy destinations. - **Mutations:** 7, each failing the check: no sign extension, unsigned carry as SCC, wrong constant SCC, `mulk` setting SCC, missing mask refusal, missing descriptor refusal, and the lowering removed. - **Against main:** with `main`'s `gfx2hlsl.py` the check fails with `Unsupported: s_addk_i32`. Results after rebasing on `32595645`: - 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_gfx2hlsl'` passed 46/46 before the rebase. **Side note, not changed here.** The existing `s_movk_i32` path and the `s_cmpk_*` immediates do not appear to sign-extend SIMM16: `s_movk_i32 s0, 0x8000` would give `0x00008000` instead of `0xffff8000`. I'll check that separately rather than mix it in. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ChazY11 commented 2026-11-25 01:02:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:27:42Z.


Ran it, broke the new handlers five ways — all caught — and swept them with the operand-arity check from #2104

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sopk_accum_check.py
exit 0
mutation in scalar_accumulate result
len(ops) != 2 → > 2 (a one-operand SOPK is accepted) caught
the simm sign extension dropped (0xFFFF becomes 65535, not −1) caught
s_mulk_i32 lowered through the s_addk_i32 path caught
the lane-mask destination refusal dropped caught
the dst[1] > 105 SGPR bound dropped caught

The second and third are the two I cared about most: a missing sign extension is invisible for every immediate below 0x8000, and the addk/mulk SCC
difference (addk sets SCC to the signed overflow, mulk leaves it) is exactly the sort of thing a "same shape, share the code" refactor erases. Both
are pinned.

Your operand-count guard is already in the shape the arity check wants

I have a sweep in flight (#2104) that feeds every op_* handler operand shapes no decoder produces and fails if a handler answers with anything other
than Unsupported. Run against this branch, the two new handlers are clean:

handlers on this PR: 80
s_addk_i32    escapes=[]
s_mulk_i32    escapes=[]

That is not an accident of my sweep being lenient — it is if mods or len(ops) != 2: at the top of scalar_accumulate, which is the same guard the
check asks for. If #2104 lands before this PR, the new entries need no PENDING line; if this lands first, it adds two compliant handlers and the check
count moves 78 → 80 with PENDING unchanged. Either order works, which is the property I was aiming for when I wrote it as a ratchet rather than a
snapshot.

The simm handling is the part I would write into the description

simm = int(ops[1], 16)
imm = (simm - 0x10000 if simm & 0x8000 else simm) & 0xffffffff

SOPK carries a 16-bit signed immediate, and the disassembly prints it as 0x[0-9a-f]{1,4} regardless of sign, so the extension has to be done here
rather than trusted from the text. Your docstring says the destination is also the source; it does not say the immediate is signed. One clause there
would stop a future reader from "simplifying" it to int(ops[1], 16) — which the mutation above shows is a real failure, not a theoretical one.

What I did not check

That the emitted SCC for addk is the signed overflow on a device, and that mulk's SCC really is unchanged there. The fixture asserts the emitted
text and the model; the ISA citation is yours, and I have no way to test it without RDNA 2 hardware.

AI-assisted; the five mutations and the sweep were run locally on Windows 11 build 26300.9974.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148533639 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2102#issuecomment-7148533639). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:27:42Z. --- ## Ran it, broke the new handlers five ways — all caught — and swept them with the operand-arity check from #2104 ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sopk_accum_check.py exit 0 ``` | mutation in `scalar_accumulate` | result | |---|---| | `len(ops) != 2` → `> 2` (a one-operand SOPK is accepted) | **caught** | | the `simm` sign extension dropped (`0xFFFF` becomes 65535, not −1) | **caught** | | `s_mulk_i32` lowered through the `s_addk_i32` path | **caught** | | the lane-mask destination refusal dropped | **caught** | | the `dst[1] > 105` SGPR bound dropped | **caught** | The second and third are the two I cared about most: a missing sign extension is invisible for every immediate below `0x8000`, and the addk/mulk SCC difference (`addk` sets SCC to the signed overflow, `mulk` leaves it) is exactly the sort of thing a "same shape, share the code" refactor erases. Both are pinned. ## Your operand-count guard is already in the shape the arity check wants I have a sweep in flight (#2104) that feeds every `op_*` handler operand shapes no decoder produces and fails if a handler answers with anything other than `Unsupported`. Run against this branch, the two new handlers are clean: ```text handlers on this PR: 80 s_addk_i32 escapes=[] s_mulk_i32 escapes=[] ``` That is not an accident of my sweep being lenient — it is `if mods or len(ops) != 2:` at the top of `scalar_accumulate`, which is the same guard the check asks for. If #2104 lands before this PR, the new entries need no `PENDING` line; if this lands first, it adds two compliant handlers and the check count moves 78 → 80 with `PENDING` unchanged. Either order works, which is the property I was aiming for when I wrote it as a ratchet rather than a snapshot. ## The `simm` handling is the part I would write into the description ```python simm = int(ops[1], 16) imm = (simm - 0x10000 if simm & 0x8000 else simm) & 0xffffffff ``` SOPK carries a **16-bit signed** immediate, and the disassembly prints it as `0x[0-9a-f]{1,4}` regardless of sign, so the extension has to be done here rather than trusted from the text. Your docstring says the destination is also the source; it does not say the immediate is signed. One clause there would stop a future reader from "simplifying" it to `int(ops[1], 16)` — which the mutation above shows is a real failure, not a theoretical one. ## What I did not check That the emitted SCC for `addk` is the signed overflow on a device, and that `mulk`'s SCC really is unchanged there. The fixture asserts the emitted text and the model; the ISA citation is yours, and I have no way to test it without RDNA 2 hardware. AI-assisted; the five mutations and the sweep were run locally on Windows 11 build 26300.9974.
upstream-sync closed this pull request 2026-11-25 03:00:55 +00:00
tedin7 commented 2026-11-25 03:00:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:01:08Z.

Review: COMMENTED


Review of head 92903092a4bb0fd5ea16f45cc32254dcca082924 (comment only).

Summary. The lowering of s_addk_i32 / s_mulk_i32 matches the RDNA 2 ISA 12.2 semantics: sign-extended SIMM16, signed-overflow SCC for addk computed from the old D before D is written, constant folding for SCC and result, refusal of mask / descriptor / pointer sources before state changes, uniformity dropped after mulk. No correctness bug found.

Commands executed locally (worktree at the PR head)

  • git diff --stat origin/main...HEAD: 5 files, +379 (gfx2hlsl.py +50, new test +324, CMake/CI/AGENTS registration).
  • python3 -B tests/startup_gfx2hlsl_sopk_accum_check.py: PASS (all 7 built-in mutations reported caught).
  • python3 tests/docs_links_check.py: self-test passed, 18 files / 394 links.
  • python3 tests/bidi_controls_check.py: pass, 1421 files.
  • git diff --check origin/main...HEAD: clean.
  • gh pr checks 2102: Linux checks, gfx1013 fixtures and Windows build/test pass; /O2 and chart publish skipped.
  • My own mutation: addk + replaced by - in the emitted expression -> the test fails (red). Reverted with git checkout -- ..
  • Overflow formula check by hand: (old ^ sum) & ~(old ^ imm) & 0x80000000 equals ISA tmp[31]==SIMM16[15] && tmp[31]!=D[31] (operands same sign, sum differs). Constant-fold branch uses the same formula.

Findings

  • P1: none.
  • P2: none.
  • Nit 1 (tools/startup/gfx2hlsl.py, mulk branch of scalar_accumulate): LLVM 15 declares Defs = [SCC] for both forms while the ISA names no SCC write for mulk. The PR documents this and drops the uniformity proof, which is the safe direction. Consider a hardware or reference-model measurement later; until then a later reader of SCC after mulk relies on the ISA reading.
  • Nit 2 (tests/startup_gfx2hlsl_sopk_accum_check.py): coverage is the integer evaluator from startup_gfx2hlsl_bit_alu_check.py plus translation controls. There is no compiled-HLSL (WARP) readback of the emitted expressions. The expressions are plain uint arithmetic and the Windows CI build passed, so I judge the risk low; a WARP readback would still make the signed-overflow expression (uint xor/and with 0x80000000u) verified end to end.
  • Nit 3: the mutation harness anchors on exact source text (assert source.count(old) == 1), so a cosmetic edit of the overflow line makes it fail with an assertion instead of a clean "mutation caught" message.

Windows WARP was not run by me: the change is scalar integer lowering with passing Windows CI.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050504660 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2102#pullrequestreview-6050504660). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:01:08Z. **Review:** COMMENTED --- Review of head `92903092a4bb0fd5ea16f45cc32254dcca082924` (comment only). **Summary.** The lowering of `s_addk_i32` / `s_mulk_i32` matches the RDNA 2 ISA 12.2 semantics: sign-extended SIMM16, signed-overflow SCC for addk computed from the old D before D is written, constant folding for SCC and result, refusal of mask / descriptor / pointer sources before state changes, uniformity dropped after mulk. No correctness bug found. **Commands executed locally (worktree at the PR head)** - `git diff --stat origin/main...HEAD`: 5 files, +379 (gfx2hlsl.py +50, new test +324, CMake/CI/AGENTS registration). - `python3 -B tests/startup_gfx2hlsl_sopk_accum_check.py`: PASS (all 7 built-in mutations reported caught). - `python3 tests/docs_links_check.py`: self-test passed, 18 files / 394 links. - `python3 tests/bidi_controls_check.py`: pass, 1421 files. - `git diff --check origin/main...HEAD`: clean. - `gh pr checks 2102`: Linux checks, gfx1013 fixtures and Windows build/test pass; /O2 and chart publish skipped. - My own mutation: addk `+` replaced by `-` in the emitted expression -> the test fails (red). Reverted with `git checkout -- .`. - Overflow formula check by hand: `(old ^ sum) & ~(old ^ imm) & 0x80000000` equals ISA `tmp[31]==SIMM16[15] && tmp[31]!=D[31]` (operands same sign, sum differs). Constant-fold branch uses the same formula. **Findings** - P1: none. - P2: none. - Nit 1 (`tools/startup/gfx2hlsl.py`, mulk branch of `scalar_accumulate`): LLVM 15 declares `Defs = [SCC]` for both forms while the ISA names no SCC write for mulk. The PR documents this and drops the uniformity proof, which is the safe direction. Consider a hardware or reference-model measurement later; until then a later reader of SCC after mulk relies on the ISA reading. - Nit 2 (`tests/startup_gfx2hlsl_sopk_accum_check.py`): coverage is the integer evaluator from `startup_gfx2hlsl_bit_alu_check.py` plus translation controls. There is no compiled-HLSL (WARP) readback of the emitted expressions. The expressions are plain uint arithmetic and the Windows CI build passed, so I judge the risk low; a WARP readback would still make the signed-overflow expression (uint xor/and with `0x80000000u`) verified end to end. - Nit 3: the mutation harness anchors on exact source text (`assert source.count(old) == 1`), so a cosmetic edit of the overflow line makes it fail with an assertion instead of a clean "mutation caught" message. Windows WARP was not run by me: the change is scalar integer lowering with passing Windows CI. AI-assisted; commands listed were executed locally.
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!2095
No description provided.