Skip to content

shader: lower v_mul_hi_u32 in gfx2hlsl - #1952

Closed
tedin7 wants to merge 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1792-mul-hi-u32
Closed

tedin7 wants to merge 2 commits into
R0Y00:mainfrom
tedin7:x2w-frontier-1792-mul-hi-u32

Conversation

@tedin7

@tedin7 tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
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.

@ChazY11

ChazY11 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

The new helper spelled its halfword shifts as '>> 16)', which the v_cvt_f32_ubyte2 mutation control also matches, so the control saw 11 hits instead of 4. Spell the shifts as '>> 16u)'.
@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

CI fix pushed at a0830b0: 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.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

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

@tedin7 tedin7 closed this Nov 25, 2026
tedin7 pushed a commit to tedin7/Xbox2Windows that referenced this pull request Nov 26, 2026
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.

2 participants