Repository navigation
startup: lower s_mul_hi_u32 in gfx2hlsl - #2176
Conversation
s_mul_hi_u32 writes the high 32 bits of the unsigned 64-bit product of two scalar words and leaves SCC unchanged (pixel_aot SCALAR["mul_hi_u32"], the LLVM SOP2 definition has no SCC def). Startup HLSL has no 64-bit integer, so a mul_hi_u32 helper in the common preamble builds the high word from halfword products, the same arithmetic as pixel_aot's scalar helper. The operation needs each source's whole word, so it joins the halfword pack's guard: wave-mask sources, vcc/exec destinations, symbolic descriptors and reserved encodings are refused. An SGPR that held a compare mask may be overwritten; set_s drops its mask mark. The SOP2 check runs the emitted helper text against (a * b) >> 32 over every value form and kills six mutants. The cvt_alu ubyte2 mutation pattern is anchored to its operand so it no longer also matches the helper's shifts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of head dd6306e (comment only).
Logic: mul_hi_u32 splits both words into 16-bit halves. I checked the intermediate sums by hand: middle is at most 0xfffeffff and the carry sum is at most 0xffff0000, so neither wraps in 32-bit HLSL. The result equals (a*b)>>32. SCC stays unchanged, and refusing vcc/exec destinations and mask sources is consistent with the sibling word ops. No concurrent PR covers s_mul_hi_u32 in gfx2hlsl (#604 and #884 are pixel_aot and disasm only).
P2 (should fix): the new check never compiles or draws the helper. tests/startup_gfx2hlsl_sop2_complement_check.py runs the emitted helper text through a Python interpreter, and its only draw mode is the existing cvt_alu one. Other gfx2hlsl lowerings have a draw <dxc> <offscreen_check> <warp|hardware> path with a GPU readback compared against a CPU reference (see startup_gfx2hlsl_cvt_alu_check.py and its _warp / _hardware tests). Please add an s_mul_hi_u32 fixture with edge pairs (0xffffffff squared, 0x80000000 x 2) to a draw path. This would catch a DXC or WARP difference in the uint multiply chain, which a Python simulation cannot.
Commands run locally:
python3 tests/startup_gfx2hlsl_sop2_complement_check.py: PASSpython3 tests/startup_gfx2hlsl_cvt_alu_check.py: controls PASSpython3 tests/docs_links_check.py: PASS (401 links)python3 tests/bidi_controls_check.py: PASSgit diff --check origin/main...HEAD: clean- Mutation 1:
(middle >> 16)changed to(middle >> 15)in gfx2hlsl.py. The check FAILS ("mul_hi result and untouched SCC"). Reverted. - Mutation 2: SCA template
mul_hi_u32({0}, {1})changed to({1}, {1}). The check FAILS. Reverted. - Win2025 (Server 2025, MSVC 14.44, D3D12 WARP):
ctest -R 'startup_gfx2hlsl_(sop2_complement|cvt_alu)'4/4 PASS in Debug and in Release /O2. These cover the existing cvt_alu draw tests, not the new opcode. - GitHub CI
Build and test on Windows: SUCCESS.
Not merged because of the P2 above.
AI-assisted; commands listed were executed locally.
Remove the duplicate mul_hi_u32 definition introduced during rebase so v_mul_hi_u32 reuses the shared helper from R0Y00#2176.
Change
Part of #1723.
s_mul_hi_u32returns the high 32 bits of the unsigned 64-bit product of two scalar words. Before this change it was refused by name intools/startup/gfx2hlsl.py. Compiled shaders use it for unsigned division by a constant (s_mul_hi_u32 sN, sM, 0x88888889, then a shift).SCA['s_mul_hi_u32'] = ('mul_hi_u32({0}, {1})', None). This is the same text and SCC rule aspixel_aot'sSCALAR["mul_hi_u32"]. SCC is left unchanged, as fors_mul_i32. LLVM's SOP2 definition for this instruction has no SCC def.mul_hi_u32helper inPipeline.common()builds the high word from halfword products. This is the arithmetic ofpixel_aot's scalar helper.s_pack_hh_b32_b16,s_sext_i32_i8/i16). The guard refuses wave-mask sources, symbolic descriptor sources and reserved encodings. There is one difference from its neighbours: a vcc/exec destination is refused, but an SGPR that last held a compare mask may be overwritten.set_salready drops the mask mark on any write. Without this, 181 of 266 pipelines in my local sample would have refused the instruction only because the destination SGPR earlier held a compare mask.tests/startup_gfx2hlsl_cvt_alu_check.py: thev_cvt_f32_ubyte2mutation regex>> 16\)also matched the shifts in the new helper. I anchored it to its operand ((v\d+) >> 16\)→\1 >> 8)). It still replaces exactly the four body occurrences, and itscount == 4assertion is unchanged.Scalar form only. The vector form
v_mul_hi_u32is in open PRs #1793 and #1747 (issue #1792), and this PR does not touch it. Both of those PRs add their ownuint mul_hi_u32(uint a, uint b)tocommon():med3). A test merge with startup: lower v_mul_hi_u32 to a halfword-product helper #1793's head merges that helper into a single copy. The remaining conflicts are unrelated: startup: lower v_mul_hi_u32 to a halfword-product helper #1793 has to be rebased on main anyway.s_mul_hi_i32is also still refused (no open PR found). It is not added here.Verification
On
dd6306e6(this commit merged with maine3e39c43):python -B tests/startup_gfx2hlsl_sop2_complement_check.py:PASS. The news_mul_hi_u32checks cover:0102809a;None;(a * b) >> 32with SCC unchanged. The pairs include 0×0, 1×1,0xffffffff²,0x80000000×2, halfword boundaries and 512 seeded random pairs.The emitted helper text is parsed from
common()and run in 32-bit arithmetic.Refusals: vcc_lo/exec_lo/vcc_hi sources, vcc_lo/exec_lo destinations, a mask-marked source SGPR, a
ptrdescriptor source and reserved sources209. A mask-marked destination SGPR is written and loses its mark.Mutants, each killed device-free by the same check:
{0} * {1});(la * lb) >> 16carry;python -B tests/startup_gfx2hlsl_sop2_complement_check.py binary <LLVM 15 llvm-mc>:llvm-mc assembles 123 decoded SOP2 forms to the same words: ok.Every
tests/startup_gfx2hlsl_*.py,tests/startup_translator_check.pyandtests/docs_links_check.py: exit 0.Not run:
decode <libLLVM>(no libLLVM in my local LLVM 15 install; the Linux job runs it). There is no device test, because the sibling SOP2 check has none.git diff --check origin/main...HEAD: clean. All files LF in the index.Evidence and follow-up
Offline, counts only, local indirect-draw request sample (266 pipelines × 3 request sets, 798 requests). Measured on main
94337881, and on #2153's head6d8ab3d3for the readfirstlane column:v_readfirstlane_b32is reached first in all of them. 0 requests translated before, so there was no HLSL to compare byte for byte.s_mul_hi_u32→v_mul_hi_u32(startup: lower v_mul_hi_u32 to a halfword-product helper #1793/startup: lower v_mul_hi_u32 in gfx2hlsl #1747).s_mul_hi_u32refusal anywhere: 254 before. After, 146 still reach it with a descriptor-derived source (desc×descnum) and 50 with a vcc_hi or vcc_lo source. These stay refused deliberately. Follow-ups are descriptor-numeric propagation and wave32vcc_hiused as a plain SGPR.Checklist
docs/experiments.md, next to thes_pack_hh_b32_b16paragraph).AI-assisted (Claude Code).
🤖 Generated with Claude Code