startup: lower s_addk_i32 and s_mulk_i32 in gfx2hlsl #2095
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2102"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-25T00:22:05Z.
Refs #1723
Change
tools/startup/gfx2hlsl.pynow lowers the SOPK accumulate formss_addk_i32ands_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 15SOPInstructions.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 emittedsccunchanged aftermulk. 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 onmulk, a later SCC reader would be wrong; that is not measured.Handling.
DbeforeDis written.Dfolds both the result and SCC.DinStage.masks, with descriptor or pointer tracking, or as a copy ofexecis refused before any mask flag or symbol changes.D, as in #1931.Spelling. LLVM 15 prints the immediate as unsigned hex, the same as
tools/disasm_rdna2.py:s_addk_i32 s0, 0x8000is00 80 80 b7, ands_mulk_i32 s0, -2becomes0xfffe(fe ff 00 b8). Only0x0–0xffffis accepted.Test
New device-free check
tests/startup_gfx2hlsl_sopk_accum_check.py, registered asstartup_gfx2hlsl_sopk_accumand listed in the pure-Python job andAGENTS.md.0x7fff,0x8000and0xffff;0x7fffffff + 1) and downward (0x80000000 + -1), and no overflow;mulkleaving an earlier SCC alone;exec-copy destinations.mulksetting SCC, missing mask refusal, missing descriptor refusal, and the lowering removed.main'sgfx2hlsl.pythe check fails withUnsupported: s_addk_i32.Results after rebasing on
32595645:tests/startup_gfx2hlsl_*_check.py,tests/startup_translator_check.pyandtests/docs_links_check.pypass;ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl'passed 46/46 before the rebase.Side note, not changed here. The existing
s_movk_i32path and thes_cmpk_*immediates do not appear to sign-extend SIMM16:s_movk_i32 s0, 0x8000would give0x00008000instead of0xffff8000. I'll check that separately rather than mix it in.AI-assisted (Claude Code).
🤖 Generated with Claude Code
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
scalar_accumulatelen(ops) != 2→> 2(a one-operand SOPK is accepted)simmsign extension dropped (0xFFFFbecomes 65535, not −1)s_mulk_i32lowered through thes_addk_i32pathdst[1] > 105SGPR bound droppedThe 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 SCCdifference (
addksets SCC to the signed overflow,mulkleaves it) is exactly the sort of thing a "same shape, share the code" refactor erases. Bothare 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 otherthan
Unsupported. Run against this branch, the two new handlers are clean:That is not an accident of my sweep being lenient — it is
if mods or len(ops) != 2:at the top ofscalar_accumulate, which is the same guard thecheck asks for. If #2104 lands before this PR, the new entries need no
PENDINGline; if this lands first, it adds two compliant handlers and the checkcount moves 78 → 80 with
PENDINGunchanged. Either order works, which is the property I was aiming for when I wrote it as a ratchet rather than asnapshot.
The
simmhandling is the part I would write into the descriptionSOPK 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 hererather 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
addkis the signed overflow on a device, and thatmulk's SCC really is unchanged there. The fixture asserts the emittedtext 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.
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_i32matches 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.+replaced by-in the emitted expression -> the test fails (red). Reverted withgit checkout -- ..(old ^ sum) & ~(old ^ imm) & 0x80000000equals ISAtmp[31]==SIMM16[15] && tmp[31]!=D[31](operands same sign, sum differs). Constant-fold branch uses the same formula.Findings
tools/startup/gfx2hlsl.py, mulk branch ofscalar_accumulate): LLVM 15 declaresDefs = [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.tests/startup_gfx2hlsl_sopk_accum_check.py): coverage is the integer evaluator fromstartup_gfx2hlsl_bit_alu_check.pyplus 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 with0x80000000u) verified end to end.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.