feat(relinker): lower MULX under --to-rosetta #12255

Merged
Mherberts merged 2 commits from upstream-pr/12255 into main 2026-10-29 04:34:58 +00:00
Mherberts commented 2026-10-28 05:31:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mherberts on 2026-10-28T05:31:00Z.


What

--to-rosetta currently leaves VEX MULX native, so conversion still depends on the host implementing this BMI2 instruction. This adds a scalar lowering for its 32- and 64-bit register and memory forms, including extended registers, SIB addressing, RSP operands and RIP-relative sources. It is registered for both single-instruction and combined stubs. Plain conversion and --to-intel keep MULX native.

The semantics follow Intel SDM volume 2B, MULX, section 4-153: unsigned multiplication by EDX/RDX, low and high results in the second and first destinations, high result winning when the destinations coincide, and unchanged arithmetic flags. The stub saves RAX, RDX and RFLAGS below the guest red zone, uses scalar MUL, then restores state and commits the two outputs, deferring an RSP destination until last.

Stack-displacement overflow and direct RSP reads overlapping the saved inputs are refused. Precise exception delivery, aliases of spill slots through other registers, and execution of a converted title under Rosetta remain unverified or unsupported as recorded in docs/dev/debt/relinker.md. This PR does not establish that a retail title is unblocked.

Tested

Review follow-up on head 77a4c6f0660453829c5abb77dafc84b31a11bbfb:

  • rooklz independently built the original implementation head 46f658e8dd on Apple M5, macOS 26.2, and reported 372/372 relinker-only CTests passing in the native arm64 build. An x86-64 build under Rosetta executed all 73,868 MULX cases successfully. This verifies the lowering test on that Rosetta host, not a converted title or exception delivery.
  • This follow-up changes only the debt text to reflect that result; production code and tests are unchanged. Conventions pass against current upstream/main. Merge previews against main 3a40945e62 and #12068 head b10f3d89150919c0ff730ebfc07959773d0d4929 exit 0.

The following results were obtained on the original head before the documentation-only follow-up:

Windows 11 x86-64 (10.0.26200), WinLibs GCC 15.2.0, Python 3.13, Release with ANYPS5_RELINKER_ONLY=ON and BUILD_TESTING=ON. Head 46f658e8dd9748c0912b06259012b251c83b3a29, based on main 895ead67f7c11a46a7a29378c802d11864bed37a.

  • Complete relinker-only build passed with cmake --build build-verify --parallel 2.
  • 10/10 focused CTests passed in 4.38 seconds: mulx_lowering, mulx_conversion, to_rosetta, andn_lowering, masked_store_lowering, amd64_only_converter, amd64_only_windows, guest_intel_trampolines, red_zone_framer and red_zone_framer_execution.
  • mulx_lowering executed 73,868 emitted-body cases on Windows: every register triple at both widths, shared destinations, RAX/RDX/RSP aliases, deterministic random and boundary inputs, all arithmetic-flag combinations, memory base registers, SIB, RIP-relative addressing, and red-zone preservation. It also checks invalid encodings, spill overlap boundaries, converter reports and relocation metadata. The final native test rerun passed in 0.26 seconds.
  • mulx_conversion passed 48 CLI conversions: executable and guest-module paths, Linux ELF and macOS Mach-O output, 32/64-bit register and memory sites, with plain/Intel/Rosetta mode controls. These checks inspect files; they do not execute Linux or macOS output on Windows.
  • Negative control: linked the same native regression test against main's unchanged instruction-matcher source (blob ecbe487147281a1e852ea874e32168b16345a48b) instead of the changed matcher. It exits 1 with Rosetta mode did not require a MULX trampoline; the changed matcher passes. This is a focused control, not a full baseline build.
  • git diff --check and check_conventions.py --base upstream/main pass. The required --base origin/main was also run: the fork's old main includes unrelated upstream differences, yielding 47 debt-length warnings and the existing non-Conventional upstream revert 97f1d41a2c; none belongs to this PR's commits.
  • Registration audits pass: 42 native test executables and 210 Python scripts are registered.
  • The official tools/pr_overlap.py --branch codex/feat-rosetta-mulx logic ran against a fresh public inventory of 1,358 open PRs through a local read-only GitHub-CLI adapter. No implementation overlap was found after moving registrations away from other pending additions. Manually inspected the uncomparable shared files in #5021, #6713, #7173, #7531, #9793, #10861 and #11192: they add unrelated macOS/Windows/SCE tests, ADX lowering or scalar reciprocal handling; the MULX source lists, registration, matcher sites and separate debt entry are disjoint.

No native macOS/Rosetta or Linux execution, full product suite, GPU run, or retail title run was performed locally; the Rosetta test above was independently run by the reviewer. No performance claim.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5045707430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12255). Originally posted by [Mherberts](https://github.com/Mherberts) on 2026-10-28T05:31:00Z. --- ### What `--to-rosetta` currently leaves VEX MULX native, so conversion still depends on the host implementing this BMI2 instruction. This adds a scalar lowering for its 32- and 64-bit register and memory forms, including extended registers, SIB addressing, RSP operands and RIP-relative sources. It is registered for both single-instruction and combined stubs. Plain conversion and `--to-intel` keep MULX native. The semantics follow [Intel SDM volume 2B, MULX, section 4-153](https://cdrdv2-public.intel.com/868141/253667-089-sdm-vol-2b.pdf#page=161): unsigned multiplication by EDX/RDX, low and high results in the second and first destinations, high result winning when the destinations coincide, and unchanged arithmetic flags. The stub saves RAX, RDX and RFLAGS below the guest red zone, uses scalar MUL, then restores state and commits the two outputs, deferring an RSP destination until last. Stack-displacement overflow and direct RSP reads overlapping the saved inputs are refused. Precise exception delivery, aliases of spill slots through other registers, and execution of a converted title under Rosetta remain unverified or unsupported as recorded in `docs/dev/debt/relinker.md`. This PR does not establish that a retail title is unblocked. ### Tested Review follow-up on head `77a4c6f0660453829c5abb77dafc84b31a11bbfb`: - `rooklz` independently built the original implementation head `46f658e8dd` on Apple M5, macOS 26.2, and reported 372/372 relinker-only CTests passing in the native arm64 build. An x86-64 build under Rosetta executed all 73,868 MULX cases successfully. This verifies the lowering test on that Rosetta host, not a converted title or exception delivery. - This follow-up changes only the debt text to reflect that result; production code and tests are unchanged. Conventions pass against current `upstream/main`. Merge previews against `main` `3a40945e62` and #12068 head `b10f3d89150919c0ff730ebfc07959773d0d4929` exit 0. The following results were obtained on the original head before the documentation-only follow-up: Windows 11 x86-64 (10.0.26200), WinLibs GCC 15.2.0, Python 3.13, Release with `ANYPS5_RELINKER_ONLY=ON` and `BUILD_TESTING=ON`. Head `46f658e8dd9748c0912b06259012b251c83b3a29`, based on `main` `895ead67f7c11a46a7a29378c802d11864bed37a`. - Complete relinker-only build passed with `cmake --build build-verify --parallel 2`. - 10/10 focused CTests passed in 4.38 seconds: `mulx_lowering`, `mulx_conversion`, `to_rosetta`, `andn_lowering`, `masked_store_lowering`, `amd64_only_converter`, `amd64_only_windows`, `guest_intel_trampolines`, `red_zone_framer` and `red_zone_framer_execution`. - `mulx_lowering` executed 73,868 emitted-body cases on Windows: every register triple at both widths, shared destinations, RAX/RDX/RSP aliases, deterministic random and boundary inputs, all arithmetic-flag combinations, memory base registers, SIB, RIP-relative addressing, and red-zone preservation. It also checks invalid encodings, spill overlap boundaries, converter reports and relocation metadata. The final native test rerun passed in 0.26 seconds. - `mulx_conversion` passed 48 CLI conversions: executable and guest-module paths, Linux ELF and macOS Mach-O output, 32/64-bit register and memory sites, with plain/Intel/Rosetta mode controls. These checks inspect files; they do not execute Linux or macOS output on Windows. - Negative control: linked the same native regression test against `main`'s unchanged instruction-matcher source (blob `ecbe487147281a1e852ea874e32168b16345a48b`) instead of the changed matcher. It exits 1 with `Rosetta mode did not require a MULX trampoline`; the changed matcher passes. This is a focused control, not a full baseline build. - `git diff --check` and `check_conventions.py --base upstream/main` pass. The required `--base origin/main` was also run: the fork's old main includes unrelated upstream differences, yielding 47 debt-length warnings and the existing non-Conventional upstream revert `97f1d41a2c`; none belongs to this PR's commits. - Registration audits pass: 42 native test executables and 210 Python scripts are registered. - The official `tools/pr_overlap.py --branch codex/feat-rosetta-mulx` logic ran against a fresh public inventory of 1,358 open PRs through a local read-only GitHub-CLI adapter. No implementation overlap was found after moving registrations away from other pending additions. Manually inspected the uncomparable shared files in #5021, #6713, #7173, #7531, #9793, #10861 and #11192: they add unrelated macOS/Windows/SCE tests, ADX lowering or scalar reciprocal handling; the MULX source lists, registration, matcher sites and separate debt entry are disjoint. No native macOS/Rosetta or Linux execution, full product suite, GPU run, or retail title run was performed locally; the Rosetta test above was independently run by the reviewer. No performance claim. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
rooklz commented 2026-10-28 05:51:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T05:51:51Z.

Review: APPROVED


Reviewed 46f658e8dd: approve.

I ran it on macOS 26.2, Apple M5 (arm64 host), with Homebrew Python 3.13 and Apple clang:

  • Merged onto main 0cd07dc4ab, arm64 relinker-only Release build: full build passed, ctest 372/372. On arm64, mulx_lowering runs only the encoding checks, because the execution block is gated on __x86_64__.
  • x86_64 slice (-DCMAKE_OSX_ARCHITECTURES=x86_64) run under Rosetta 2 (arch -x86_64): mulx_lowering_tests printed Executed 73,868 MULX cases with register, flag and red-zone checks and exited 0. So the emitted stub bodies do run correctly under Rosetta on this host. That covers part of the new debt line ("Execution ... under Rosetta ... not been verified"). It does not cover a converted title or fault delivery inside the spill sequence. Whether to narrow the debt line is your call.
  • Mutant probe (each change rebuilt and run under Rosetta), all 8 killed:
    • frame 192→128
    • low/high commit order swapped, so the high result no longer wins on equal destinations
    • deferred-RSP destination picks low instead of high
    • spill-overlap throw removed
    • overlap lower bound 24→16
    • PUSHF/POPF replaced with NOP
    • disp32 overflow throw removed
    • RSP-source LEA offset off by 8
  • Gates: check_conventions.py --base upstream/main passed; git merge-tree against main clean. pr_overlap.py lists only the shared files (CMakeLists.txt, Amd64OnlyInstructionMatcher.cpp, debt/relinker.md) with #7531/#9793/#10861/#11192, no shared hunks.

On inspection, the frame stays below the 128-byte red zone (192 > 128 + 24 spill bytes), and an RSP destination is committed last, after the frame is released. The semantics match the SDM text cited in the description: high result wins on equal destinations, flags are preserved.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5678384795 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12255#pullrequestreview-5678384795). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T05:51:51Z. **Review:** APPROVED --- Reviewed `46f658e8dd`: approve. I ran it on macOS 26.2, Apple M5 (arm64 host), with Homebrew Python 3.13 and Apple clang: - **Merged onto `main` `0cd07dc4ab`, arm64 relinker-only Release build:** full build passed, ctest 372/372. On arm64, `mulx_lowering` runs only the encoding checks, because the execution block is gated on `__x86_64__`. - **x86_64 slice (`-DCMAKE_OSX_ARCHITECTURES=x86_64`) run under Rosetta 2 (`arch -x86_64`):** `mulx_lowering_tests` printed `Executed 73,868 MULX cases with register, flag and red-zone checks` and exited 0. So the emitted stub bodies do run correctly under Rosetta on this host. That covers part of the new debt line ("Execution ... under Rosetta ... not been verified"). It does not cover a converted title or fault delivery inside the spill sequence. Whether to narrow the debt line is your call. - **Mutant probe** (each change rebuilt and run under Rosetta), all 8 killed: - frame 192→128 - low/high commit order swapped, so the high result no longer wins on equal destinations - deferred-RSP destination picks low instead of high - spill-overlap throw removed - overlap lower bound 24→16 - PUSHF/POPF replaced with NOP - disp32 overflow throw removed - RSP-source LEA offset off by 8 - **Gates:** `check_conventions.py --base upstream/main` passed; `git merge-tree` against `main` clean. `pr_overlap.py` lists only the shared files (`CMakeLists.txt`, `Amd64OnlyInstructionMatcher.cpp`, `debt/relinker.md`) with #7531/#9793/#10861/#11192, no shared hunks. On inspection, the frame stays below the 128-byte red zone (192 > 128 + 24 spill bytes), and an RSP destination is committed last, after the frame is released. The semantics match the SDM text cited in the description: high result wins on equal destinations, flags are preserved. AI-assisted: yes (Claude Code).
MarcZX10 commented 2026-10-28 09:00:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T09:00:12Z.

Review: APPROVED


I reviewed the decoder and lowering path, including VEX register extensions, memory operands, RSP-relative adjustment, and spill-frame overlap refusal. The emitted stub preserves RAX/RDX/RFLAGS, defers an RSP destination, and leaves normal conversion paths unchanged. The reported 73,868 executed lowering cases plus CLI conversions cover register aliases, memory/SIB/RIP-relative forms, flags, red-zone preservation and invalid encodings; the current cross-platform relinker/ASan CI matrix passes. Remaining unsupported alias/exception/Rosetta cases are recorded in debt and the PR makes no retail-title claim. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5680091820 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12255#pullrequestreview-5680091820). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T09:00:12Z. **Review:** APPROVED --- I reviewed the decoder and lowering path, including VEX register extensions, memory operands, RSP-relative adjustment, and spill-frame overlap refusal. The emitted stub preserves RAX/RDX/RFLAGS, defers an RSP destination, and leaves normal conversion paths unchanged. The reported 73,868 executed lowering cases plus CLI conversions cover register aliases, memory/SIB/RIP-relative forms, flags, red-zone preservation and invalid encodings; the current cross-platform relinker/ASan CI matrix passes. Remaining unsupported alias/exception/Rosetta cases are recorded in debt and the PR makes no retail-title claim. No blocking issue found.
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
boykopovar/AnyPS5!12255
No description provided.