shader: lower v_mul_hi_u32 in gfx2hlsl #1945

Closed
tedin7 wants to merge 2 commits from upstream-pr/1952 into main
tedin7 commented 2026-11-24 22:04:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T22:04:46Z.


Part of #1792

What this unblocks: v_mul_hi_u32 (register and literal forms) lowers instead of being refused, removing 1 of the loading-screen shader refusals under #870.

Changes:

  • tools/startup/gfx2hlsl.py: v_mul_hi_u32 lowers to a preamble helper mul_hi_u32 built from halfword products (no 64-bit multiply in these stages; a plain * would give the low half).
  • tests/startup_gfx2hlsl_mul_hi_u32_check.py (+ ctest startup_gfx2hlsl_mul_hi_u32): synthetic VOP3A words (opcode 0x16a) are decoded, then translated. It checks the register form, the literal form 0xaaaaaaab, that v_mul_lo_u32 is unchanged, and that the helper is in the preamble with halfword products.
  • docs/experiments.md: entry added.

Not in this slice: v_mul_hi_i32, the 24-bit _u24 forms, DXC/draw results.

Verification:

python3 tests/startup_gfx2hlsl_mul_hi_u32_check.py   -> all checks passed
(same test with the gfx2hlsl.py change reverted)      -> Unsupported: v_mul_hi_u32 (fails)
python3 tests/startup_gfx2hlsl_b64_shift_check.py    -> PASS
python3 tests/startup_gfx2hlsl_bit_alu_check.py      -> passes
python3 tests/startup_gfx2hlsl_op_refusals_check.py  -> 3 cases passed
python3 (200000 random pairs, helper formula vs (a*b)>>32) -> equal

Run on Linux. Not yet run on Win2025: ctest -R startup_gfx2hlsl_mul_hi_u32 and an FXC/DXC compile of the lowered output.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592327723 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1952). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T22:04:46Z. --- Part of #1792 What this unblocks: `v_mul_hi_u32` (register and literal forms) lowers instead of being refused, removing 1 of the loading-screen shader refusals under #870. Changes: - `tools/startup/gfx2hlsl.py`: `v_mul_hi_u32` lowers to a preamble helper `mul_hi_u32` built from halfword products (no 64-bit multiply in these stages; a plain `*` would give the low half). - `tests/startup_gfx2hlsl_mul_hi_u32_check.py` (+ ctest `startup_gfx2hlsl_mul_hi_u32`): synthetic VOP3A words (opcode `0x16a`) are decoded, then translated. It checks the register form, the literal form `0xaaaaaaab`, that `v_mul_lo_u32` is unchanged, and that the helper is in the preamble with halfword products. - `docs/experiments.md`: entry added. Not in this slice: `v_mul_hi_i32`, the 24-bit `_u24` forms, DXC/draw results. Verification: ``` python3 tests/startup_gfx2hlsl_mul_hi_u32_check.py -> all checks passed (same test with the gfx2hlsl.py change reverted) -> Unsupported: v_mul_hi_u32 (fails) python3 tests/startup_gfx2hlsl_b64_shift_check.py -> PASS python3 tests/startup_gfx2hlsl_bit_alu_check.py -> passes python3 tests/startup_gfx2hlsl_op_refusals_check.py -> 3 cases passed python3 (200000 random pairs, helper formula vs (a*b)>>32) -> equal ``` Run on Linux. Not yet run on Win2025: ctest `-R startup_gfx2hlsl_mul_hi_u32` and an FXC/DXC compile of the lowered output. AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-24 23:03:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:06:23Z.


Ran it, checked the helper arithmetic independently, and found one term the fixture does not pin

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_mul_hi_u32_check.py
gfx2hlsl mul_hi_u32: all checks passed      exit 0

The helper is correct

I evaluated the emitted body as HLSL uint arithmetic against (a * b) >> 32 for 200,144 pairs — 144 edge pairs
(0, 1, 0xFFFF, 0x10000, 0x7FFFFFFF, 0x80000000, 0xFFFF0000, 0xFFFFFFFF, …) plus 200,000 seeded random pairs:

pairs checked: 200144   mismatches: 0

Including the carry case that halfword decompositions usually get wrong:

a=ffffffff b=ffffffff   full=fffffffe  reference=fffffffe   ok
a=0001ffff b=0001ffff   full=00000003  reference=00000003   ok

Mutations

mutation result
table: mul_hi_u32({0}, {1}) → {0} * {1} (low half) caught
helper: (la * lb) >> 16 → (la * lb) << 16 caught
helper: the final + (((middle & 0xffffu) + la * (b >> 16)) >> 16) removed not caught

The survivor is a real arithmetic change, not an equivalent mutant:

a=ffffffff b=ffffffff   with the term: fffffffe   without: fffeffff
a=0001ffff b=0001ffff   with the term: 00000003   without: 00000002

Both differ from the reference by exactly the carry the term supplies. The fixture asserts that the helper is present and that it
contains halfword products — which is what the description says it checks — but nothing evaluates the helper, so a dropped carry
passes.

Suggested check, in the style of the other value-level tests in tests/

def mul_hi(a, b):                      # the emitted body, as uint arithmetic
    la, lb = a & 0xffff, b & 0xffff
    middle = ((a >> 16) * lb + ((la * lb) >> 16)) & 0xffffffff
    return (((a >> 16) * (b >> 16)) + (middle >> 16)
            + ((((middle & 0xffff) + la * (b >> 16)) & 0xffffffff) >> 16)) & 0xffffffff

PAIRS = [(0xffffffff, 0xffffffff), (0x0001ffff, 0x0001ffff), (0, 0xffffffff), (0x80000000, 0x80000000)]
rng = random.Random(0x1952)
PAIRS += [(rng.getrandbits(32), rng.getrandbits(32)) for _ in range(1000)]
assert all(mul_hi(a, b) == ((a * b) >> 32) & 0xffffffff for a, b in PAIRS)

That fails on the dropped-carry mutation and on the <<16 mutation, and it would also catch a change to the emitted body that
keeps the "halfword products" text but alters the arithmetic.

Scope

v_mul_hi_i32, the _u24 forms, DXC and draws are out of scope in the description, and I did not touch them either. The
v_mul_lo_u32 line staying as {0} * {1} is correct: it is a register-only table entry, and the fixture checks it is unchanged.

Verdict

Reviewed. The lowering is right; one term of it is unverified by the fixture.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145810576 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1952#issuecomment-7145810576). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:06:23Z. --- ## Ran it, checked the helper arithmetic independently, and found one term the fixture does not pin Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_gfx2hlsl_mul_hi_u32_check.py gfx2hlsl mul_hi_u32: all checks passed exit 0 ``` ## The helper is correct I evaluated the emitted body as HLSL `uint` arithmetic against `(a * b) >> 32` for 200,144 pairs — 144 edge pairs (`0`, `1`, `0xFFFF`, `0x10000`, `0x7FFFFFFF`, `0x80000000`, `0xFFFF0000`, `0xFFFFFFFF`, …) plus 200,000 seeded random pairs: ```text pairs checked: 200144 mismatches: 0 ``` Including the carry case that halfword decompositions usually get wrong: ```text a=ffffffff b=ffffffff full=fffffffe reference=fffffffe ok a=0001ffff b=0001ffff full=00000003 reference=00000003 ok ``` ## Mutations | mutation | result | |---|---| | table: `mul_hi_u32({0}, {1})` → `{0} * {1}` (low half) | **caught** | | helper: `(la * lb) >> 16` → `(la * lb) << 16` | **caught** | | **helper: the final `+ (((middle & 0xffffu) + la * (b >> 16)) >> 16)` removed** | **not caught** | The survivor is a real arithmetic change, not an equivalent mutant: ```text a=ffffffff b=ffffffff with the term: fffffffe without: fffeffff a=0001ffff b=0001ffff with the term: 00000003 without: 00000002 ``` Both differ from the reference by exactly the carry the term supplies. The fixture asserts that the helper is present and that it contains halfword products — which is what the description says it checks — but nothing evaluates the helper, so a dropped carry passes. ## Suggested check, in the style of the other value-level tests in `tests/` ```python def mul_hi(a, b): # the emitted body, as uint arithmetic la, lb = a & 0xffff, b & 0xffff middle = ((a >> 16) * lb + ((la * lb) >> 16)) & 0xffffffff return (((a >> 16) * (b >> 16)) + (middle >> 16) + ((((middle & 0xffff) + la * (b >> 16)) & 0xffffffff) >> 16)) & 0xffffffff PAIRS = [(0xffffffff, 0xffffffff), (0x0001ffff, 0x0001ffff), (0, 0xffffffff), (0x80000000, 0x80000000)] rng = random.Random(0x1952) PAIRS += [(rng.getrandbits(32), rng.getrandbits(32)) for _ in range(1000)] assert all(mul_hi(a, b) == ((a * b) >> 32) & 0xffffffff for a, b in PAIRS) ``` That fails on the dropped-carry mutation and on the `<<16` mutation, and it would also catch a change to the emitted body that keeps the "halfword products" text but alters the arithmetic. ## Scope `v_mul_hi_i32`, the `_u24` forms, DXC and draws are out of scope in the description, and I did not touch them either. The `v_mul_lo_u32` line staying as `{0} * {1}` is correct: it is a register-only table entry, and the fixture checks it is unchanged. ## Verdict Reviewed. The lowering is right; one term of it is unverified by the fixture. ## Notes - AI-assisted review.
chatgpt-codex-connector[bot commented 2026-11-24 23:03:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T22:04:57Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T22:07:42.156706Z ba94004 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7145778001 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1952#issuecomment-7145778001). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T22:04:57Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"ba94004fbe9d77025dabca22d6a4e99a88f6f1b0","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T22:07:42.156706Z">2026-11-24T22:07:42.156706Z</relative-time> | `ba94004` | PR opened | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
tedin7 commented 2026-11-24 23:03:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T22:19:57Z.


CI fix pushed at a0830b07: the new mul_hi_u32 helper spelled its halfword shifts as >> 16), which the v_cvt_f32_ubyte2 mutation control in startup_gfx2hlsl_cvt_alu_check.py also matches (11 hits instead of 4). The shifts are now >> 16u); the generated math is unchanged.

Verification:

  • python3 tests/startup_gfx2hlsl_cvt_alu_check.py failed before (AssertionError: ('v_cvt_f32_ubyte2', 11)), passes after.
  • python3 tests/startup_gfx2hlsl_mul_hi_u32_check.py passes.
  • All tests/startup_gfx2hlsl*.py pass locally.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146098517 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1952#issuecomment-7146098517). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T22:19:57Z. --- CI fix pushed at a0830b07: the new `mul_hi_u32` helper spelled its halfword shifts as `>> 16)`, which the `v_cvt_f32_ubyte2` mutation control in `startup_gfx2hlsl_cvt_alu_check.py` also matches (11 hits instead of 4). The shifts are now `>> 16u)`; the generated math is unchanged. Verification: - `python3 tests/startup_gfx2hlsl_cvt_alu_check.py` failed before (`AssertionError: ('v_cvt_f32_ubyte2', 11)`), passes after. - `python3 tests/startup_gfx2hlsl_mul_hi_u32_check.py` passes. - All `tests/startup_gfx2hlsl*.py` pass locally. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 01:00:53 +00:00
tedin7 commented 2026-11-25 01:02:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:30:52Z.


Closing as superseded by earlier open PRs #1747 (includes draw test) and #1793 covering v_mul_hi_u32.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148586345 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1952#issuecomment-7148586345). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:30:52Z. --- Closing as superseded by earlier open PRs #1747 (includes draw test) and #1793 covering v_mul_hi_u32.

Pull request closed

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!1945
No description provided.