Skip to content

startup: lower s_addk_i32 and s_mulk_i32 in gfx2hlsl - #2102

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
JB2WIN:startup/gfx2hlsl-sopk-addk-mulk
Nov 25, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
JB2WIN:startup/gfx2hlsl-sopk-addk-mulk

Conversation

@JB2WIN

@JB2WIN JB2WIN commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

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

Refs R0Y00#1723.

Both SOPK forms use D as destination and source: D = D op signext(SIMM16).

- s_addk_i32 (RDNA 2 ISA 12.2, SOPK opcode 15): SCC is the signed
  overflow, tmp[31] == SIMM16[15] && tmp[31] != D[31]. SCC is computed
  from the old D before D is written.
- s_mulk_i32 (opcode 16): the ISA names no SCC write, so the emitted
  scc is left alone. LLVM 15 SOPInstructions.td lists SCC under Defs for
  both forms (one `let Defs = [SCC]` block), so the SCC uniformity proof
  is dropped after s_mulk_i32.

The decoder prints the field as unsigned hex; llvm-mc 15 (gfx1013)
accepts that spelling and the encodings are pinned in the new check.

A known-constant D is folded. D holding a lane mask, descriptor or
pointer data is refused before D or its mask flag changes. Uniformity
follows D. Other destinations and immediates are refused.

New device-free check tests/startup_gfx2hlsl_sopk_accum_check.py runs
the emitted lines against an integer reference (immediates 0, 1, 0x7fff,
0x8000, 0xffff; overflow up and down; a later s_cselect_b32 reading SCC;
mulk keeping an earlier SCC) and carries seven mutations that must turn
it red. Registered in CMakeLists.txt, AGENTS.md and windows.yml.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

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.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@R0Y00
R0Y00 merged commit b7b29b6 into R0Y00:main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants