Recompiler: build the wide unsigned multiply without UMul on a Mac #3525

Open
vakaobr wants to merge 2 commits from upstream-pr/3525 into main
vakaobr commented 2026-11-30 13:20:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vakaobr on 2026-11-30T13:20:37Z.


Stacked on #2361; only the top commit belongs here. Part of #258.

Apple's metal-shaderconverter, which D3DMetal runs on every pipeline, does not handle DXIL UMul (binaryWithTwoOuts, opcode 42) in every program: on some it overflows its stack and the compile never returns. The direct translator emits UMul for the stride * index term of a windowed buffer address, so on a Mac those pipelines can stop there. On a Mac (util::mac_host), umul_wide now builds the high half from 16-bit parts, as the HLSL kernel's mul_hi_u32 does, so the window code still needs no Int64Ops. Windows keeps UMul. #2172 would use umul_wide in more places; on a Mac those go through the same halves.

What is in it

  • crates/xcl/src/gpu/dxil/ops.rs: umul_wide takes umul_wide_halves on a Mac.
  • crates/xcl/src/gpu/dxil/ops_tests.rs: new umul_wide_without_umul_matches_cpu.
  • crates/xcl/src/gpu/dxil/core_tests.rs: window_at_lowers_to_32bit_carry_without_int64 expects no UMul on a Mac and UMul elsewhere.

Tested

  • M5 Pro, macOS 27.2.1, CrossOver 26.3 (Wine) with D3DMetal, Rust 1.100, x86_64-pc-windows-gnu, debug; this branch on main 9dfbe3e with #2361.
  • gpu_memory_windows_above_4_gib, gpu_memory_windows_reject_out_of_uma_aliasing, gpu_sparse_vram_descriptors_buffers_and_xmem: on main each reports "has overflowed its stack" and hangs; with this PR all three pass.
  • umul_wide_without_umul_matches_cpu (new), window_at_lowers_to_32bit_carry_without_int64, gpu_window_at_32bit_carry_edge_cases, a_mac_host_is_darwin_under_wine_unless_forced: pass. With XCL_MAC_HOST=0 the window_at test still finds UMul.

Regression run

  • None yet: I have no Windows machine for scripts/smoke.ps1. On Windows mac_host() is false, so the code path there is unchanged.

Not tested

  • Windows (any GPU), the whole suite, a retail title. IMul's high half is also wrong on D3DMetal (1 * 0x9e377939 gives 0); the runtime does not emit IMul today, and this PR does not change it.

AI use

  • Claude Code with Claude Opus 5.5: found the converter's stack overflow, wrote the change and the tests, ran them on my Mac, and drafted this description. I checked it before opening.
<!-- greyfinch-sync:ivall/xcl:issue:5566089146 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3525). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-11-30T13:20:37Z. --- Stacked on #2361; only the top commit belongs here. Part of #258. Apple's metal-shaderconverter, which D3DMetal runs on every pipeline, does not handle DXIL `UMul` (`binaryWithTwoOuts`, opcode 42) in every program: on some it overflows its stack and the compile never returns. The direct translator emits `UMul` for the `stride * index` term of a windowed buffer address, so on a Mac those pipelines can stop there. On a Mac (`util::mac_host`), `umul_wide` now builds the high half from 16-bit parts, as the HLSL kernel's `mul_hi_u32` does, so the window code still needs no `Int64Ops`. Windows keeps `UMul`. #2172 would use `umul_wide` in more places; on a Mac those go through the same halves. **What is in it** - `crates/xcl/src/gpu/dxil/ops.rs`: `umul_wide` takes `umul_wide_halves` on a Mac. - `crates/xcl/src/gpu/dxil/ops_tests.rs`: new `umul_wide_without_umul_matches_cpu`. - `crates/xcl/src/gpu/dxil/core_tests.rs`: `window_at_lowers_to_32bit_carry_without_int64` expects no `UMul` on a Mac and `UMul` elsewhere. **Tested** - M5 Pro, macOS 27.2.1, CrossOver 26.3 (Wine) with D3DMetal, Rust 1.100, `x86_64-pc-windows-gnu`, debug; this branch on `main` `9dfbe3e` with #2361. - `gpu_memory_windows_above_4_gib`, `gpu_memory_windows_reject_out_of_uma_aliasing`, `gpu_sparse_vram_descriptors_buffers_and_xmem`: on `main` each reports "has overflowed its stack" and hangs; with this PR all three pass. - `umul_wide_without_umul_matches_cpu` (new), `window_at_lowers_to_32bit_carry_without_int64`, `gpu_window_at_32bit_carry_edge_cases`, `a_mac_host_is_darwin_under_wine_unless_forced`: pass. With `XCL_MAC_HOST=0` the `window_at` test still finds `UMul`. **Regression run** - None yet: I have no Windows machine for `scripts/smoke.ps1`. On Windows `mac_host()` is false, so the code path there is unchanged. **Not tested** - Windows (any GPU), the whole suite, a retail title. `IMul`'s high half is also wrong on D3DMetal (`1 * 0x9e377939` gives 0); the runtime does not emit `IMul` today, and this PR does not change it. **AI use** - Claude Code with Claude Opus 5.5: found the converter's stack overflow, wrote the change and the tests, ran them on my Mac, and drafted this description. I checked it before opening.
Foniass commented 2026-12-01 07:43:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T07:43:00Z.


Review (head 3ebde06f, diff read, not a merge-check; draft with PR description red, so not built or run):

  • No xcl-smoke blocks (main or this head), which is why PR description fails, and the full suite hasn't been run anywhere. It's stacked on #2361 (open draft), which provides util::mac_host().
  • The test pairs each value with a derived one, so the largest-carry case (0xFFFFFFFF x 0xFFFFFFFF) isn't covered; worth adding.

Fine: umul_wide_halves is correct for all inputs (the middle sum stays below 2^18, and since the true high word fits in 32 bits no addition wraps). It's gated on mac_host() (ops.rs ~444), so Windows still emits UMul and the existing Windows test still asserts it; the only caller is lower_window.rs ~130. The new GPU test skips correctly without a GPU, and AI use names the model.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7304394327 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3525#issuecomment-7304394327). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T07:43:00Z. --- Review (head 3ebde06f, diff read, not a merge-check; draft with `PR description` red, so not built or run): - No xcl-smoke blocks (main or this head), which is why `PR description` fails, and the full suite hasn't been run anywhere. It's stacked on #2361 (open draft), which provides `util::mac_host()`. - The test pairs each value with a derived one, so the largest-carry case (`0xFFFFFFFF x 0xFFFFFFFF`) isn't covered; worth adding. Fine: `umul_wide_halves` is correct for all inputs (the middle sum stays below 2^18, and since the true high word fits in 32 bits no addition wraps). It's gated on `mac_host()` (`ops.rs` ~444), so Windows still emits `UMul` and the existing Windows test still asserts it; the only caller is `lower_window.rs` ~130. The new GPU test skips correctly without a GPU, and AI use names the model. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Foniass commented 2026-12-01 10:09:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T10:09:29Z.


Triage (#1607) – head 3ebde06

Stacked on #2361: the first commit (69c0b04, util::mac_host) is #2361's; only 3ebde06 belongs here. No other open or merged PR avoids UMul in umul_wide; #2363, #2402 and #2492 use mac_host for other changes.
No problems found in the diff: ops.rs::umul_wide_halves sums the two cross products' low halves with the carry out of ll before shifting, which gives the exact high word, and umul_wide_without_umul_matches_cpu checks it against the CPU product. On Windows mac_host() is false, so the emitted DXIL is unchanged there.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7307758535 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3525#issuecomment-7307758535). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T10:09:29Z. --- **Triage (#1607)** – head 3ebde06 Stacked on #2361: the first commit (69c0b04, `util::mac_host`) is #2361's; only 3ebde06 belongs here. No other open or merged PR avoids `UMul` in `umul_wide`; #2363, #2402 and #2492 use `mac_host` for other changes. No problems found in the diff: `ops.rs::umul_wide_halves` sums the two cross products' low halves with the carry out of `ll` before shifting, which gives the exact high word, and `umul_wide_without_umul_matches_cpu` checks it against the CPU product. On Windows `mac_host()` is false, so the emitted DXIL is unchanged there. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3525:upstream-pr/3525'
git switch 'upstream-pr/3525'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3525'
git switch 'upstream-pr/3525'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3525'
git switch 'upstream-pr/3525'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3525'
git switch 'main'
git merge --squash 'upstream-pr/3525'
git switch 'main'
git merge --ff-only 'upstream-pr/3525'
git switch 'main'
git merge 'upstream-pr/3525'
git push origin 'main'
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
xcl-dev/xcl!3525
No description provided.