feat(relinker): lower register-form INSERTQ #185

Merged
ljkx merged 1 commit from upstream-pr/185 into main 2026-09-30 16:37:18 +00:00
ljkx commented 2026-09-30 13:22:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ljkx on 2026-09-30T13:22:32Z.


What

Addresses an item from docs/dev/TechnicalDebt.md: --to-intel failed the relink on register-form INSERTQ (F2 0F 79). It is now lowered out of line with SSE2 only, following the register-form EXTRQ lowering from #54.

No title is currently known to use this form; this removes the listed relink failure.

  • Three scratch XMM registers, distinct from both operands, are spilled below the red zone.
  • The control is taken from the upper qword of the source (psrldq 8): length = bits 69:64 (0 means 64), index = bits 77:72, as in Sse4aEmulation.hpp.
  • The hole mask (~0 >> ((64 - length) & 63)) << index is built from all-ones restricted to the low qword, and the destination is merged as dst ^= ((src << index) ^ dst) & hole.
  • dst[127:64] is zeroed with movq dst, dst. The APM leaves it undefined, but on Zen 4 hardware both INSERTQ forms and EXTRQ zero it (see Tested). The existing lowerings and the libc emulation keep it; I left them unchanged since that is a separate topic, and can send a follow-up if wanted.
  • The matcher and MatchSequence no longer reject the form, so short sites absorb the following instructions the same way register-form EXTRQ does.
  • TechnicalDebt.md: INSERTQ register form is removed from the item. The remaining instructions are rejected by the matcher, so the link now points there.

Tested

  • Relinker test suite passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3).
  • amd64_only_windows: register-form INSERTQ is executed for every length/index pair with length + index ≤ 64, rotating over the existing register pairs (equal operands and xmm8–xmm15 included), with random bits in the unused control bits. Results are checked against the transcribed reference and against sse4a::Execute, which decodes the control from the register itself, plus the existing clobber / RFLAGS / rsp / red-zone checks. Runtime went from 13 s to 15 s.
  • amd64_only_converter (x86-64 Linux): the stub is executed with distinct and with equal operands. The matcher test now expects a trampoline named "INSERTQ register form".
  • Mutation check: swapping the length and index extraction makes both tests fail.
  • Differential run against real hardware (Ryzen 7 7800X3D, Zen 4; harness not included in this PR): all 256 destination/source pairs, 138,240 executions, including exhaustive length/index on five pairs and random controls with length + index > 64. The stub matched the native insertq on all 128 bits of all 16 XMM registers, the status flags and rsp.
  • Not verified on Zen 2 (PS5). No title tested.

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; silent stubs are listed in TechnicalDebt (n/a)
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes, assisted with Claude Code.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4691053013 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/185). Originally posted by [ljkx](https://github.com/ljkx) on 2026-09-30T13:22:32Z. --- ### What Addresses an item from `docs/dev/TechnicalDebt.md`: `--to-intel` failed the relink on register-form INSERTQ (`F2 0F 79`). It is now lowered out of line with SSE2 only, following the register-form EXTRQ lowering from #54. No title is currently known to use this form; this removes the listed relink failure. - Three scratch XMM registers, distinct from both operands, are spilled below the red zone. - The control is taken from the upper qword of the source (`psrldq 8`): length = bits 69:64 (0 means 64), index = bits 77:72, as in `Sse4aEmulation.hpp`. - The hole mask `(~0 >> ((64 - length) & 63)) << index` is built from all-ones restricted to the low qword, and the destination is merged as `dst ^= ((src << index) ^ dst) & hole`. - `dst[127:64]` is zeroed with `movq dst, dst`. The APM leaves it undefined, but on Zen 4 hardware both INSERTQ forms and EXTRQ zero it (see Tested). The existing lowerings and the libc emulation keep it; I left them unchanged since that is a separate topic, and can send a follow-up if wanted. - The matcher and `MatchSequence` no longer reject the form, so short sites absorb the following instructions the same way register-form EXTRQ does. - `TechnicalDebt.md`: INSERTQ register form is removed from the item. The remaining instructions are rejected by the matcher, so the link now points there. ### Tested - Relinker test suite passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3). - `amd64_only_windows`: register-form INSERTQ is executed for every length/index pair with length + index ≤ 64, rotating over the existing register pairs (equal operands and xmm8–xmm15 included), with random bits in the unused control bits. Results are checked against the transcribed reference and against `sse4a::Execute`, which decodes the control from the register itself, plus the existing clobber / RFLAGS / rsp / red-zone checks. Runtime went from 13 s to 15 s. - `amd64_only_converter` (x86-64 Linux): the stub is executed with distinct and with equal operands. The matcher test now expects a trampoline named "INSERTQ register form". - Mutation check: swapping the length and index extraction makes both tests fail. - Differential run against real hardware (Ryzen 7 7800X3D, Zen 4; harness not included in this PR): all 256 destination/source pairs, 138,240 executions, including exhaustive length/index on five pairs and random controls with length + index > 64. The stub matched the native `insertq` on all 128 bits of all 16 XMM registers, the status flags and rsp. - Not verified on Zen 2 (PS5). No title tested. ### 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 - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt (n/a) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes, assisted with Claude Code.
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!185
No description provided.