startup: lower s_mul_hi_u32 in gfx2hlsl #2168

Merged
NiiRoZz merged 2 commits from upstream-pr/2176 into main 2026-11-25 03:12:21 +00:00
NiiRoZz commented 2026-11-25 02:53:03 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T02:53:03Z.


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():

  • This PR uses the exact helper line and position from #1793 (directly after 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.
  • #1747 merges textually without a conflict and leaves two definitions, so HLSL would not compile. Whichever of this PR and #1747 lands second has to drop its helper. A reviewer should check for this.

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:

  • On main: the first refusal changes for 0 requests, because v_readfirstlane_b32 is reached first in all of them. 0 requests translated before, so there was no HLSL to compare byte for byte.
  • With #2153 (readfirstlane fix): the first refusal changes for 192 requests (64 pipelines × 3), all s_mul_hi_u32 → v_mul_hi_u32 (#1793/#1747).
  • Pipelines that reach an s_mul_hi_u32 refusal 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 wave32 vcc_hi used as a plain SGPR.

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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6596105417 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2176). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-25T02:53:03Z. --- ## 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()`: - This PR uses the exact helper line and position from #1793 (directly after `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. - **#1747 merges textually without a conflict and leaves two definitions**, so HLSL would not compile. Whichever of this PR and #1747 lands second has to drop its helper. A reviewer should check for this. `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: - On main: the first refusal changes for 0 requests, because `v_readfirstlane_b32` is reached first in all of them. 0 requests translated before, so there was no HLSL to compare byte for byte. - With #2153 (readfirstlane fix): the first refusal changes for 192 requests (64 pipelines × 3), all `s_mul_hi_u32` → `v_mul_hi_u32` (#1793/#1747). - Pipelines that reach an `s_mul_hi_u32` refusal 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 wave32 `vcc_hi` used as a plain SGPR. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed (`docs/experiments.md`, next to the `s_pack_hh_b32_b16` paragraph). - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 04:04:07 +00:00
tedin7 commented 2026-11-25 04:04:07 +00:00 (Migrated from github.com)

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_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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051246439 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2176#pullrequestreview-6051246439). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T03:04:38Z. **Review:** COMMENTED --- Review of head dd6306e69940ae3e56c1f15612f0fcebc8a934ac (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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2168
No description provided.