startup: lower s_mul_hi_u32 in gfx2hlsl #2168
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2176"
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 NiiRoZz on 2026-11-25T02:53:03Z.
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 #1793's head merges that helper into a single copy. The remaining conflicts are unrelated: #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(#1793/#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
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T03:04:38Z.
Review: COMMENTED
Review of head
dd6306e699(comment only).Logic:
mul_hi_u32splits both words into 16-bit halves. I checked the intermediate sums by hand:middleis 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 coverss_mul_hi_u32in 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.pyruns the emitted helper text through a Python interpreter, and its only draw mode is the existing cvt_alu one. Other gfx2hlsl lowerings have adraw <dxc> <offscreen_check> <warp|hardware>path with a GPU readback compared against a CPU reference (seestartup_gfx2hlsl_cvt_alu_check.pyand its_warp/_hardwaretests). 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(middle >> 16)changed to(middle >> 15)in gfx2hlsl.py. The check FAILS ("mul_hi result and untouched SCC"). Reverted.mul_hi_u32({0}, {1})changed to({1}, {1}). The check FAILS. Reverted.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.Build and test on Windows: SUCCESS.Not merged because of the P2 above.
AI-assisted; commands listed were executed locally.