Repository navigation
startup: lower s_addk_i32 and s_mulk_i32 in gfx2hlsl - #2102
Conversation
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>
Ran it, broke the new handlers five ways — all caught — and swept them with the operand-arity check from #2104out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_sopk_accum_check.py
exit 0
The second and third are the two I cared about most: a missing sign extension is invisible for every immediate below Your operand-count guard is already in the shape the arity check wantsI have a sweep in flight (#2104) that feeds every That is not an accident of my sweep being lenient — it is The
|
tedin7
left a comment
There was a problem hiding this comment.
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 withgit checkout -- .. - Overflow formula check by hand:
(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
- P1: none.
- P2: none.
- Nit 1 (
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. - Nit 2 (
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. - 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.
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 startup: prove lane-pure v_readfirstlane_b32 sources uniform in straight-line code #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