Skip to content

startup: lower s_mul_hi_u32 in gfx2hlsl - #2176

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-s-mul-hi-u32
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:startup/gfx2hlsl-s-mul-hi-u32

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Change

Part of #1723.

s_mul_hi_u32 returns the high 32 bits of the unsigned 64-bit product of two scalar words. Before this change it was refused by name in tools/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 as pixel_aot's SCALAR["mul_hi_u32"]. SCC is left unchanged, as for s_mul_i32. LLVM's SOP2 definition for this instruction has no SCC def.
  • Startup HLSL has no 64-bit integer, so a mul_hi_u32 helper in Pipeline.common() builds the high word from halfword products. This is the arithmetic of pixel_aot's scalar helper.
  • The operation needs each source's whole word, so it joins the existing whole-word guard (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_s already 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: the v_cvt_f32_ubyte2 mutation 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 its count == 4 assertion is unchanged.

Scalar form only. The vector form v_mul_hi_u32 is in open PRs #1793 and #1747 (issue #1792), and this PR does not touch it. Both of those PRs add their own uint mul_hi_u32(uint a, uint b) to common():

s_mul_hi_i32 is also still refused (no open PR found). It is not added here.

Verification

On dd6306e6 (this commit merged with main e3e39c43):

  • python -B tests/startup_gfx2hlsl_sop2_complement_check.py: PASS. The new s_mul_hi_u32 checks cover:

    • the LLVM 15 encoding 0102809a;
    • table text equal to pixel_aot's, with SCC None;
    • 11 SGPR/in-place/inline/literal/m0 forms × 2,707 pairs, against (a * b) >> 32 with 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 ptr descriptor source and reserved source s209. A mask-marked destination SGPR is written and loses its mark.

  • Mutants, each killed device-free by the same check:

    • the low word ({0} * {1});
    • the signed high word;
    • the helper without the (la * lb) >> 16 carry;
    • the helper without the final carry limb;
    • an added SCC write;
    • a deleted table entry (refused by name).
  • 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.py and tests/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 head 6d8ab3d3 for the readfirstlane column:

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed (docs/experiments.md, next to the s_pack_hh_b32_b16 paragraph).
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

NiiRoZz and others added 2 commits November 25, 2026 03:48
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 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 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: PASS
  • python3 tests/startup_gfx2hlsl_cvt_alu_check.py: controls PASS
  • python3 tests/docs_links_check.py: PASS (401 links)
  • python3 tests/bidi_controls_check.py: PASS
  • git 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.

@R0Y00
R0Y00 merged commit 166d866 into R0Y00:main Nov 25, 2026
5 checks passed
R0D4X added a commit to R0D4X/Xbox2Windows that referenced this pull request Nov 25, 2026
Remove the duplicate mul_hi_u32 definition introduced during rebase so v_mul_hi_u32 reuses the shared helper from R0Y00#2176.
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.

3 participants