fix(relinker): move in-place AMD-only replacements into a short instruction's stub #7999

Merged
mopga merged 1 commit from upstream-pr/7999 into main 2026-10-20 17:19:52 +00:00
mopga commented 2026-10-19 17:15:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mopga on 2026-10-19T17:15:15Z.


What

A short AMD-only instruction lowered through a stub takes the following instructions with it until its site fits a jump. When one of them is an AMD-only instruction that is replaced in place (MOVNTSS, MOVNTSD, MONITORX, MWAITX, and RDSEED and CLWB for Rosetta), MatchSequence found no out-of-line lowering for it and the whole conversion failed with AMD-only instruction sequence has no out-of-line lowering.

Reproduced with relinker --to-intel on current main for these code sequences, each followed by ret; extrq alone converts:

  • 66 0F 79 C1 F3 0F 2B 07 (extrq xmm0,xmm1; movntss [rdi],xmm0)
  • 0F 38 CC CA F2 0F 2B 0D <disp32> (sha256msg1 xmm1,xmm2; movntsd [rip+disp],xmm1)
  • 66 0F 79 C1 0F 01 FA (extrq xmm0,xmm1; monitorx)

A following instruction with an in-place replacement now has that replacement moved into the stub, with a RIP-relative displacement relocated like any moved instruction. This applies only to MatchSequence followers; out-of-line emission for 256-bit store windows (EmitOutOfLine) is unchanged, so a 256-bit store still does not take a following CLWB.

Tested

Windows 11, AMD Ryzen 7 7800X3D (Zen 4), MinGW-w64 GCC 15.2.0 (WinLibs r7), relinker-only build of this branch on current main:

  • New converterInPlaceFollower in Amd64OnlyConverterTests.cpp (one trampoline each; MOVSS store bytes, the MOVNTSD relocation, and the MONITORX NOP before the return jump) fails on main with AMD-only instruction sequence has no out-of-line lowering and passes with the fix.
  • The three sequences above convert with relinker --to-intel.
  • Differential run on this AMD host: the converted MOVNTSS and RIP-relative MOVNTSD sequences match native execution (all GPRs, flags, YMM registers, memory and stack) for 20 random register states each. MONITORX could not be executed natively in that harness, so only its conversion was checked.
  • ctest: 211/211 passed.
  • python3 tools/check_conventions.py --base origin/main passes. A trial merge with the open PRs that change these files (#2780, #7173, #7332, #7363, #7531, #7651, #7660) shows no conflict caused by this change.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4924644648 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7999). Originally posted by [mopga](https://github.com/mopga) on 2026-10-19T17:15:15Z. --- ### What A short AMD-only instruction lowered through a stub takes the following instructions with it until its site fits a jump. When one of them is an AMD-only instruction that is replaced in place (MOVNTSS, MOVNTSD, MONITORX, MWAITX, and RDSEED and CLWB for Rosetta), `MatchSequence` found no out-of-line lowering for it and the whole conversion failed with `AMD-only instruction sequence has no out-of-line lowering`. Reproduced with `relinker --to-intel` on current `main` for these code sequences, each followed by `ret`; `extrq` alone converts: - `66 0F 79 C1 F3 0F 2B 07` (`extrq xmm0,xmm1; movntss [rdi],xmm0`) - `0F 38 CC CA F2 0F 2B 0D <disp32>` (`sha256msg1 xmm1,xmm2; movntsd [rip+disp],xmm1`) - `66 0F 79 C1 0F 01 FA` (`extrq xmm0,xmm1; monitorx`) A following instruction with an in-place replacement now has that replacement moved into the stub, with a RIP-relative displacement relocated like any moved instruction. This applies only to `MatchSequence` followers; out-of-line emission for 256-bit store windows (`EmitOutOfLine`) is unchanged, so a 256-bit store still does not take a following CLWB. ### Tested Windows 11, AMD Ryzen 7 7800X3D (Zen 4), MinGW-w64 GCC 15.2.0 (WinLibs r7), relinker-only build of this branch on current `main`: - New `converterInPlaceFollower` in `Amd64OnlyConverterTests.cpp` (one trampoline each; MOVSS store bytes, the MOVNTSD relocation, and the MONITORX NOP before the return jump) fails on `main` with `AMD-only instruction sequence has no out-of-line lowering` and passes with the fix. - The three sequences above convert with `relinker --to-intel`. - Differential run on this AMD host: the converted MOVNTSS and RIP-relative MOVNTSD sequences match native execution (all GPRs, flags, YMM registers, memory and stack) for 20 random register states each. MONITORX could not be executed natively in that harness, so only its conversion was checked. - `ctest`: 211/211 passed. - `python3 tools/check_conventions.py --base origin/main` passes. A trial merge with the open PRs that change these files (#2780, #7173, #7332, #7363, #7531, #7651, #7660) shows no conflict caused by this change. ### Checklist - [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 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
wachi35676 commented 2026-10-19 23:18:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T23:18:05Z.


Verdict: confirmed on Windows and Linux. It merges and works together with #8001.

Environment: Windows 11 Home 10.0.26300, Ryzen 5 9600X (Zen 5), WinLibs GCC 15.2.0 r7, relinker-only Release build. This PR was merged onto main 9127ea36d, alone and together with #8001.

  • Through the CLI: each sequence is the body of a test_tls_function_coverage.py make_image image, converted with relinker --skip-sce-module.

    follower after a short extrq/sha256msg1 flag main this PR
    f3 0f 2b 07 movntss --to-intel rc 2 AMD-only instruction sequence has no out-of-line lowering (offset 0x1240) rc 0, 1 stub
    f2 0f 2b 0d 00 01 00 00 movntsd [rip] --to-intel rc 2, same rc 0, 1 stub
    0f 01 fa monitorx --to-intel rc 2, same rc 0, 1 stub
    0f 01 fb mwaitx --to-intel rc 2, same rc 0, 1 stub
    0f c7 f8 rdseed --to-rosetta rc 2, same rc 0, 1 stub
    66 0f ae 37 clwb --to-rosetta rc 2, same rc 0, 1 stub
  • Negative control: with only Amd64OnlyInstructionMatcher.cpp reverted to main, amd64_only_converter_tests fails with AMD-only instruction sequence has no out-of-line lowering.

  • RIP-relative handling: _emitInPlace passes offset + info.Length + displacement as the target relative to the instruction start. That matches the convention ScalarCompareOperands/WideStoreOperands use for RipRelative, and is correct here because each in-place replacement has the original length and keeps the original displacement.

  • The moved follower is added to consumed, so its own pending in-place item is skipped and it is not patched twice.

  • ctest:

    • Windows: 212/212, after a serial rerun of seven tests that timed out under -j4 because of host load.
    • WSL Ubuntu 24.04, GCC 13.3: 216/217. The one failure, shader_cache_runtime_sources, fails intermittently on main too.
    • With #8001: Windows 212/212 and WSL 217/217. Both sets of probe cases convert.
  • Homebrew: PS5 OpenGL Showcase 1.0.2 (PPSA99005), PPSA47402, PS5LM Plus 0.8.4 (PPSA99581), and Prospero Win v0.2.0 (PPSA99995) with its 16 Wine modules, relinked with --windows --to-intel, --macos --to-rosetta and Linux --to-intel. These produce up to 51131 Rosetta stubs. All 12 output sets are byte-identical to main, so no existing lowering changed.

Minor: a follower moved this way is not listed in Reports and is not counted in ReplacedCount, so the summary line counts only the stub. This matches how other consumed followers are handled.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6279480738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7999#issuecomment-6279480738). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T23:18:05Z. --- Verdict: confirmed on Windows and Linux. It merges and works together with #8001. Environment: Windows 11 Home 10.0.26300, Ryzen 5 9600X (Zen 5), WinLibs GCC 15.2.0 r7, relinker-only Release build. This PR was merged onto `main` 9127ea36d, alone and together with #8001. - Through the CLI: each sequence is the body of a `test_tls_function_coverage.py` `make_image` image, converted with `relinker --skip-sce-module`. | follower after a short `extrq`/`sha256msg1` | flag | `main` | this PR | |---|---|---|---| | `f3 0f 2b 07` movntss | `--to-intel` | rc 2 `AMD-only instruction sequence has no out-of-line lowering (offset 0x1240)` | rc 0, 1 stub | | `f2 0f 2b 0d 00 01 00 00` movntsd [rip] | `--to-intel` | rc 2, same | rc 0, 1 stub | | `0f 01 fa` monitorx | `--to-intel` | rc 2, same | rc 0, 1 stub | | `0f 01 fb` mwaitx | `--to-intel` | rc 2, same | rc 0, 1 stub | | `0f c7 f8` rdseed | `--to-rosetta` | rc 2, same | rc 0, 1 stub | | `66 0f ae 37` clwb | `--to-rosetta` | rc 2, same | rc 0, 1 stub | - Negative control: with only `Amd64OnlyInstructionMatcher.cpp` reverted to `main`, `amd64_only_converter_tests` fails with `AMD-only instruction sequence has no out-of-line lowering`. - RIP-relative handling: `_emitInPlace` passes `offset + info.Length + displacement` as the target relative to the instruction start. That matches the convention `ScalarCompareOperands`/`WideStoreOperands` use for `RipRelative`, and is correct here because each in-place replacement has the original length and keeps the original displacement. - The moved follower is added to `consumed`, so its own pending in-place item is skipped and it is not patched twice. - `ctest`: - Windows: 212/212, after a serial rerun of seven tests that timed out under `-j4` because of host load. - WSL Ubuntu 24.04, GCC 13.3: 216/217. The one failure, `shader_cache_runtime_sources`, fails intermittently on `main` too. - With #8001: Windows 212/212 and WSL 217/217. Both sets of probe cases convert. - Homebrew: PS5 OpenGL Showcase 1.0.2 (PPSA99005), PPSA47402, PS5LM Plus 0.8.4 (PPSA99581), and Prospero Win v0.2.0 (PPSA99995) with its 16 Wine modules, relinked with `--windows --to-intel`, `--macos --to-rosetta` and Linux `--to-intel`. These produce up to 51131 Rosetta stubs. All 12 output sets are byte-identical to `main`, so no existing lowering changed. Minor: a follower moved this way is not listed in `Reports` and is not counted in `ReplacedCount`, so the summary line counts only the stub. This matches how other consumed followers are handled. AI-assisted: yes (Claude Code); results above were run on this machine.
github-actions[bot] commented 2026-10-20 03:35:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T03:35:29Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 8042 core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6282728435 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7999#issuecomment-6282728435). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T03:35:29Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 8042](https://www.github.com/boykopovar/AnyPS5/pull/8042) | `core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp` |
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!7999
No description provided.