feat(relinker): split unaligned 256-bit stores for Rosetta #4194
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4194"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T01:03:05Z.
What
On macOS,
--to-rosettaleaves unaligned 256-bit stores as they are, and Rosetta 2 ends the process when such a store crosses into a page it may not write instead of raising the fault. A store inside such a page, and a 128-bit store across one, fault normally. The macOS write watch keeps watched pages read-only between collections, so Hollow Knight: Silksong (PPSA12544) ends at its firstvmovups %ymm0, 0x68(%r15)(c4 c1 7c 11 47 68) across a watched page:maindoes not start guests on macOS yet; this line is from a macOS port of the runtime with a relinker that does not split the store. On currentmain(34a16b7f) the new converter tests do not build:codegen/x86/WideStoreOperands.hppdoes not exist.--to-rosettanow lowers VMOVUPS, VMOVUPD and VMOVDQU with a 256-bit memory destination to a stub that stores the low half with the same instruction at VEX.L=0 and the high half with VEXTRACTF128 at the operand plus 16, so each half faults like any other store. RIP-relative operands are relocated from the instruction's place in the site. A store whose upper half has no 32-bit displacement fails the conversion. Aligned stores (VMOVAPS, VMOVAPD, VMOVDQA and the non-temporal forms) cannot cross a page and are left alone.Every store is lowered; none is kept. A 4-byte store has no room for the jump to its stub, so the stub also takes one neighbouring instruction:
Register calls, LOOP, JRCXZ and branches with a prefix that changes them do not move. When neither neighbour can move, the store becomes a short jump to an island: a run of movable instructions within reach of a short jump moves to its own stub, and the bytes it frees hold the jump to the store's stub. A store with neither fails the conversion.
Tested
macOS on Apple silicon, relinker-only build for x86-64 (
ANYPS5_RELINKER_ONLY=ON, Release); Linux (Ubuntu 24.04, GCC 13) and Windows (WinLibs GCC 15.2) relinker-only builds.amd64_only_converterdoes not build onmainand passes with this change. It compares the halves with clang's encodings (base, SIB, disp8 overflowing to disp32, disp32, RSP-relative, FS-absolute, r13-based, RIP-relative) and checks that aligned stores, loads, register forms and masked stores are not split. It checks the stubs of a short store followed by a return, a call (direct, with an address-size prefix, through a RIP-relative pointer), short, near and RIP-relative jumps, short and near conditional branches and another store; that a following register call, LOOP, JRCXZ, prefixed branch or CLWB makes the store take the instruction before it; islands past instructions already moved, a store that needs its own stub, a branch target, a register call and the short-jump reach; and that a store with no room fails. Every line the change adds runs in these tests.--macos --to-rosetta: 55295 stores split in the executable, 278 of them in the stub of the store before them; every module converts with none kept. With the same conversion on the macOS port, the title boots, plays through the opening and into the first areas.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T02:45:48Z.
Pushed an update, rebased on
f49f9587. Going back over the change, I found the window placement was more general than it needed to be, so I simplified it:The relinked PPSA12544 output is byte-for-byte the same as before, and the relinker tests pass on macOS, Linux and Windows.
About the overlap with #3601: both PRs edit the same MatchSequence dispatch, but for different instructions (legacy RCPPS/RSQRTPS there, 256-bit stores here), and neither needs the other, so I've left Depends on as none. This PR moves that dispatch into _emitOutOfLine so MatchSequence and the store windows can share it. Whichever one lands second needs a small rebase: keep the _emitOutOfLine call and switch DecodeVexReciprocal to DecodeReciprocal inside it. I merged both locally that way and the relinker tests pass (115 of 115). If #3601 goes in first, I'll rebase this one.
Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T02:55:34Z.
That didn't take long...Rebased onto
34a16b7fwith the one-line fix, and all relinker tests pass on macOS, Linux and Windows.