Skip to content

startup: lower v_mul_hi_u32 to a halfword-product helper - #1793

Closed
triggered02 wants to merge 2 commits into
R0Y00:mainfrom
triggered02:startup/mul-hi
Closed

triggered02 wants to merge 2 commits into
R0Y00:mainfrom
triggered02:startup/mul-hi

Conversation

@triggered02

Copy link
Copy Markdown
Contributor

Fixes #1792. Next group from the refreshed refusal census (#870): 1 shader, and the operation had no lowering at all.

Change

v_mul_hi_u32 — the high 32 bits of an unsigned 32x32 product — was refused, including the census form with a literal operand:

v_mul_hi_u32 v1, v0, 0xaaaaaaab    before -> REFUSED: v_mul_hi_u32
                                   after  -> v1 = mul_hi_u32(v0, 0xaaaaaaabu);
v_mul_hi_u32 v1, v1, v2            after  -> v1 = mul_hi_u32(v1, v2);
v_mul_lo_u32 v1, v1, v2            -> v1 = v1 * v2;   (unchanged)

tools/pixel_aot.py already had this for the pixel AOT path, so the helper is ported from there rather than reinvented:

uint mul_hi_u32(uint a, uint b) {
    uint la = a & 0xffffu, lb = b & 0xffffu;
    uint middle = (a >> 16) * lb + ((la * lb) >> 16);
    return (a >> 16) * (b >> 16) + (middle >> 16) + (((middle & 0xffffu) + la * (b >> 16)) >> 16);
}

It computes the high word from halfword products because these stages have no 64-bit multiply, and the low half of a * b is not the high half — a plain * here would be silently wrong. The helper sits in Pipeline.common() next to mul_legacy and med3, so no separate stubs header is involved.

Test

startup_gfx2hlsl_mul_hi builds VOP3A encodings from the checked-in decoder's layout (0b110101 prefix, opcode 0x16a, source 255 followed by its literal dword), decodes them, then translates. Seven checks: both forms decode and lower through the helper, the helper is present in the common preamble and uses halfword products, and v_mul_lo_u32 still lowers to a plain product.

dev:     12/12 startup_gfx2hlsl* + startup_translator passed
release: startup_gfx2hlsl_mul_hi Passed 0.14 s

Fail-first, both mutations caught:

lower the op to a plain multiply   -> "the literal form uses the helper" FAIL
remove the helper                  -> "the helper uses halfword products, not a 64-bit multiply" FAIL

What this does not establish

  • That the emitted HLSL compiles or produces the right value on a device — the test compares text; DXC/WARP were not run here (no built binaries in this dev tree).
  • v_mul_hi_i32 (signed correction), the _u24 forms, and any use of the value in a draw are out of scope.

AI-assisted (Claude Code).

Fixes R0Y00#1792. The high half of an unsigned 32x32 product had no lowering, so v_mul_hi_u32 was refused, including the census form with a literal operand (v_mul_hi_u32 v1, v0, 0xaaaaaaab) - one of the remaining refused shaders in the loading-screen cache (R0Y00#870). tools/pixel_aot.py already had the halfword-product helper; it is ported into the gfx2hlsl preamble because these stages have no 64-bit multiply and the low half of a*b is not the high half. Both the register and literal forms lower; v_mul_lo_u32 is unchanged. Test builds VOP3A encodings (opcode 0x16a, literal source 255 followed by its dword), decodes, then translates, and also checks the helper is present in the common preamble and uses halfword products. Fail-first: lowering to a plain multiply fails 'the literal form uses the helper'; removing the helper fails 'the helper uses halfword products, not a 64-bit multiply'. Both presets pass.
@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-24T18:47:25.795750Z 11652e9 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 11652e9050

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread CMakeLists.txt
add_test(NAME startup_gfx2hlsl_sdwa COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sdwa_check.py")
# The high half of an unsigned 32x32 product (#1781).
add_test(NAME startup_gfx2hlsl_mul_hi COMMAND "${INTAKE_PYTHON}" -B

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the multiply-high check to PR CI

startup_gfx2hlsl_mul_hi is registered for the full CTest suite, but the PR test preset’s include filter in CMakePresets.json excludes it, and the python-linux run_checks_parallel.py list in .github/workflows/windows.yml omits this standalone script. As a result, no PR job runs this regression check; it is only selected by the full dev CTest path on non-PR runs. Add it to the platform-independent workflow and its documented command list so lowering regressions are caught before merge. The repository identifies that list as the platform-independent Python suite.

AGENTS.md reference: AGENTS.md:L98-L101

Useful? React with 👍 / 👎.

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

Peer review of head 11652e9050ec96472ec16ab23a8ebe967477bc9e (PRODUCTION_REVIEW_ONLY, technical comment only).

No P1. One P2 (test coverage of the helper's arithmetic).

Lowering: no defect found.

  • mul_hi_u32 (tools/startup/gfx2hlsl.py, common preamble) is exact. With a = ah2^16 + la and b = bh2^16 + lb: middle = ah*lb + ((la*lb) >> 16) is at most (2^16-1)^2 + (2^16-1) < 2^32, and (middle & 0xffff) + la*bh is at most 65535 + (2^16-1)^2 < 2^32. No intermediate overflows 32 bits, including the multiple-carry case 0xffffffff * 0xffffffff.
  • I compiled the helper text as C and compared it with the 64-bit product. Result: 0 differences over all 169 pairs of 13 edge values (0, 1, 2, 0xffff, 0x10000, 0x10001, 0x7fffffff, 0x80000000, 0x80000001, 0xfffeffff, 0xffff0000, 0xffffffff, 0x12345678) and 200 million pseudo-random pairs.
  • The opcode is added to the unsigned VEC_U table only, so v_mul_hi_i32 stays refused instead of being lowered with unsigned semantics. v_mul_lo_u32 is unchanged.

P2: tests/startup_gfx2hlsl_mul_hi_check.py never evaluates the helper.
The checks compare emitted text only (v1 = mul_hi_u32(v1, v2);, and 'a & 0xffffu' in common and 'b >> 16' in common). I applied three one-line mutations to tools/startup/gfx2hlsl.py, one at a time:

  • drop the low-product carry (+ ((la * lb) >> 16) replaced with + 0): the check still passes (rc=0).
  • replace the middle carry term (middle >> 16) with (middle & 0xffffu): rc=0.
  • replace the call mul_hi_u32({0}, {1}) with a plain product {0} * {1}: rc=1, the only one caught.
    The Windows run does not cover the arithmetic either: its ctest entry is the same Python text check, and offscreen_check does not draw with this operation.

Suggestion: evaluate the emitted helper body against (a*b) >> 32 for the edge values above. The existing shift and bit-ALU checks in the tree already evaluate emitted helpers against a Python reference. A pure-Python port of the helper kept in sync by extracting it from Pipeline.common() would be enough.

Commands run (Linux, in the prepared worktree):

  • git diff --check <merge-base>...HEAD: clean
  • tests/docs_links_check.py, tests/bidi_controls_check.py, tests/startup_gfx2hlsl_mul_hi_check.py, tests/startup_translator_check.py: all pass
  • C port of the helper against the 64-bit product: 0 differences
  • Mutations above, each restored with git checkout -- .; the tree is clean
  • Windows Server 2025 (build 26100, MSVC 14.44, WARP) at the same commit 11652e90, from the coordinator's log:
    • Debug (--preset dev): startup_gfx2hlsl_mul_hi and startup_translator 2/2 passed
    • Release /O2 (--preset release): 2/2 passed
    • Python check startup_gfx2hlsl_mul_hi_check.py: rc=0
  • Against current main the merge has no content conflicts (git merge-tree).

AI-assisted; commands listed were executed locally.

Review on R0Y00#1793 (P2): the checks compared emitted text only, so a wrong carry passed. The test now carries a small recursive-descent evaluator for the integer HLSL the helper emits (C precedence for * + - >> << & ^ |), extracts the helper body from Pipeline.common() and runs it for 20169 input pairs: all pairs of the 13 edge values from the review plus 20000 pseudo-random pairs from a fixed LCG, compared against the exact (a*b) >> 32. Fail-first: dropping the low-product carry, or replacing the middle carry term with its low half, each fails this check while the old text checks still passed.
@triggered02

Copy link
Copy Markdown
Contributor Author

P2 addressed in HEAD. You were right that the checks never evaluated the helper — and your two mutations (dropping the low-product carry, and replacing (middle >> 16) with (middle & 0xffffu)) both passed the old test.

What the test does now: it extracts the mul_hi_u32 body from Pipeline.common() and runs it through a small recursive-descent evaluator for the integer HLSL subset the helper uses (* + - >> << & ^ |, C precedence), then compares against the exact (a*b) >> 32.

Inputs: 20,169 pairs — all pairs of the 13 edge values you listed, plus 20,000 pseudo-random pairs from a fixed LCG (deterministic, no cache/game data).

the emitted helper agrees with the exact product on 20169 pairs: ok

Fail-first — your two mutations:

drop the low-product carry            -> "the emitted helper agrees with the exact product ..." FAIL
middle carry term -> its low half     -> "the emitted helper agrees with the exact product ..." FAIL

Both now fail on values while the text checks still pass, which is the point.

Debug startup_gfx2hlsl_mul_hi + startup_translator 2/2, release startup_gfx2hlsl_mul_hi Passed (1.61 s).

Thanks for the independent C port of the helper against the 64-bit product (0 differences over 169 edge pairs and 200 million random pairs) — that is stronger evidence for the arithmetic than my evaluator provides, and the two now agree.

The remaining limitation is unchanged and worth restating: nothing here compiles the emitted HLSL or runs it on a device, so this pins the algorithm, not the codegen or the driver behaviour.

AI-assisted (Claude Code).

@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 6782e7248adcbd144ae5af49891a476863692341 (startup: check the mul_hi helper by value, not by text, on top of 11652e90 startup: lower v_mul_hi_u32 to a halfword-product helper), checked out at exactly that SHA. Verdict: no P1 and no code P2. One process P2: this duplicates open #1747 and the two conflict.

Correctness of the helper (the point you asked about)

mul_hi_u32(a, b) with la, lb the low halves and ah = a >> 16, bh = b >> 16:

  • middle = ah*lb + ((la*lb) >> 16) is at most 65535*65535 + 65534 = 4294901759, below 2^32, so it cannot wrap.
  • (middle & 0xffff) + la*bh is at most 65535 + 4294836225 = 4294901760, below 2^32, so the second carry term cannot wrap either.
  • ah*bh + (middle >> 16) + (((middle & 0xffff) + la*bh) >> 16) is then the exact high word.
    I also ran the helper text, copied one to one into C++ with 32-bit uint, against ((uint64_t)a*b)>>32: the 21 x 21 pairs of boundary values (0, 1, 0xffff, 0x10000, 0x7fffffff, 0x80000000, 0xffff0000, 0xffffffff, 0xaaaaaaab, ...) plus 400,000,000 xorshift pairs, a quarter of them with a forced 0xffff.... high half or ....ffff low half: 400,000,441 cases, 0 mismatches.
    The lowering is the VEC_U entry 'mul_hi_u32({0}, {1})' (VOP3 only; the register and the literal form are covered), and v_mul_lo_u32 is untouched.

Commands run (Linux)

python3 -B tests/startup_gfx2hlsl_mul_hi_check.py     -> PASS (7 checks, incl. the helper evaluated on 20169 pairs)
for t in tests/startup_gfx2hlsl*_check.py             -> all pass
git diff --check origin/main...HEAD                   -> clean
python3 -B tests/docs_links_check.py                  -> Checked 17 Markdown files and 367 links
python3 -B tests/bidi_controls_check.py               -> no bidirectional controls
g++ -O2 (helper vs 64-bit product, 400,000,441 cases) -> 0 mismatches

Mutations of tools/startup/gfx2hlsl.py (each restored; git status clean)

mutation PR test
second cross product la*(b>>16) replaced by lb*(a>>16) FAILS (value check)
lowering mapped to a plain {0} * {1} FAILS (register and literal form)
middle carry shifted by 15 instead of 16 FAILS (value check)
low product carry dropped FAILS (value check)
The by-value evaluator does what the commit says: a wrong carry fails even though the emitted text is plausible.

P2 (process): overlap with #1747

Open #1747 (startup: lower v_mul_hi_u32 in gfx2hlsl, head 6c6b638e) adds the same VEC_U entry, the same helper (same arithmetic, other variable names) and a test of the same file name, tests/startup_gfx2hlsl_mul_hi_check.py. git merge-tree --write-tree of the two heads reports CONFLICT (add/add) on that test file; the other files merge. Only one of the two should land; the other should be closed or rebased onto it.

P3

  • The CMake comment says (#1781); the issue named in the test and the docs is #1792.
  • The test builds the instruction by hand, which is fine, but nothing checks that v_mul_hi_i32 (the signed neighbour) stays refused; one refusal assertion would pin it.

Limits

The generated HLSL was not compiled (no shader compiler on this host); the PR changes no C++, so I did not run the Windows pipeline.

AI-assisted; commands listed were executed locally.

@thepatrickworld

Copy link
Copy Markdown
Contributor

This and #1747 both lower v_mul_hi_u32, and they cannot both land: git merge-tree pr1747 pr1793 conflicts in tools/startup/gfx2hlsl.py, CMakeLists.txt, and add/add on tests/startup_gfx2hlsl_mul_hi_check.py. Both are also behind main (mergeable_state: dirty), so a rebase is needed either way.

What I checked before writing this:

  • the two helper bodies are the same expression, only formatted differently; evaluated as 32-bit unsigned against the exact (a * b) >> 32 on 300 000 random pairs plus the 0 / 1 / 0xffffffff corners, 0 divergences.
  • startup_gfx2hlsl_mul_hi_check.py passes on both branches here (exit 0); this one reports 20 169 helper pairs.
  • the difference is the test surface, not the translation: startup: lower v_mul_hi_u32 in gfx2hlsl #1747 carries binary and draw modes plus a mutation control, and its draw passes on an RX 9070 XT under both fxc ps_5_1 and dxc ps_6_0.

So nothing is lost whichever is kept — same line, same helper. @R0Y00, which one do you want to land? If it is #1747, the parts worth porting from here are the instruction-byte construction note and the edge-case list.

AI-assisted.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@triggered02 could you rebase this branch onto current main and force-push?

GitHub reports head 6782e724 as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (4aaea119):

  • CMakeLists.txt
  • tools/startup/gfx2hlsl.py

Commands run locally:

git fetch origin main pull/1793/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 1793 --json mergeable   # CONFLICTING

Once it is rebased and CI is green it will be re-checked right away.

AI-assisted; commands listed were executed locally.

@triggered02

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #1747 — it is the earlier PR for the same lowering, carries binary and draw modes, and its draw passes on hardware under both fxc ps_5_1 and dxc ps_6_0. Mine only has the translator-level test, so nothing is lost by keeping theirs.

@thepatrickworld's read is right: the helper bodies are the same expression and the difference is the test surface, not the translation.

The two bits worth porting from here, if useful:

  1. Instruction-byte construction note (in tests/startup_gfx2hlsl_mul_hi_check.py): the VOP3A form is prefix 0b110101, opcode 0x16a in w0[25:16], vdst in w0[7:0], src0 in w1[8:0], src1 in w1[17:9], and source 255 followed by its literal dword — which is how the census form v_mul_hi_u32 v1, v0, 0xaaaaaaab is built without any game data.
  2. Edge-case list for the value check: all pairs of 0, 1, 2, 0xffff, 0x10000, 0x10001, 0x7fffffff, 0x80000000, 0x80000001, 0xfffeffff, 0xffff0000, 0xffffffff, 0x12345678 plus deterministic pseudo-random pairs, compared against the exact (a*b) >> 32. That is 20,169 pairs and it caught both of the carry mutations a review had shown my text-only test missed.

I have not pushed anything into #1747's branch; happy to send either as a patch or a comment there if the author wants it.

AI-assisted (Claude Code).

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.

startup: v_mul_hi_u32 has no lowering (1 of the remaining loading-screen shader refusals)

3 participants