fix(relinker): zero dst[127:64] in EXTRQ and INSERTQ lowerings #433

Merged
adriadam10 merged 1 commit from upstream-pr/433 into main 2026-10-04 01:05:42 +00:00
adriadam10 commented 2026-10-03 06:22:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T06:22:04Z.


What

The --to-intel EXTRQ/INSERTQ lowerings kept the old dst[127:64]. Hardware zeroes it.

  • The APM, LLVM and MSVC docs say these bits are undefined. #185 measured that Zen 4 zeroes them for both INSERTQ forms and EXTRQ, and the register-form INSERTQ stub already does movq dst, dst for that reason. KytyPS5 does the same in its EXTRQ trampolines (KytyPS5/KytyPS5#839).
  • The other paths didn't: the shift-based EXTRQ, register-form EXTRQ, the AND/XOR INSERTQ, and the in-place forms. The PSHUFB stubs already zeroed the high qword through their mask.
  • This changes real results, not just undefined bits: a register-form INSERTQ that follows reads its length and index from bits 77:64 of its source. After extrq xmm0, xmm1 ; insertq xmm14, xmm0, the stub produced a different low qword in xmm14 than the hardware does.

Fix:

  • Every stub path that left the high qword now ends with movq dst, dst.
  • In-place forms:
    • The two that emitted nothing (INSERTQ with dst == src and index 0, EXTRQ with length 64 and index 0) now emit movq dst, dst.
    • PSRLQ (top-aligned EXTRQ), PMOVZX (EXTRQ of 8/16/32 bits at index 0) and PBLENDW (INSERTQ of a multiple of 16 bits at index 0) leave the high qword non-zero. They have no room for the movq in a 6-7 byte site, so those sites now take a stub.

Tested

Linux x86-64 (Ryzen 5 7535HS, Zen 3+, has SSE4a), no title.

  • Amd64OnlyConverterTests.cpp:
    • New immediateFormExecution: runs EXTRQ, INSERTQ and INSERTQ with equal operands for 9 length/index pairs, covering every in-place form and every stub path. It checks the low qword against the reference and dst[127:64] == 0, starting from a non-zero high qword.
    • registerFormExecution: the EXTRQ cases also check the high qword now.
    • Golden in-place encodings updated. The four removed in-place forms must now be rejected.
  • Fails on main. Negative checks: removing each of the three movq in the stubs, or either of the two in-place movq, makes the test fail again.
  • Differential against the native instructions (local harness, not committed): all 128 bits of the 16 XMM registers, flags, rax, rcx and rsp. Every register pair for each of EXTRQ/INSERTQ immediate and register forms and SHA256*, length/index grids, plus 3000 random 2-4 instruction sequences through MatchSequence. 313 420 runs:
    • On main: 15-17k mismatches per SSE4a form (dst[127:64]), plus 1 511 sequence runs with a wrong low qword.
    • With this PR: 0 mismatches. SHA-256 and register-form INSERTQ were already 0.
  • Full Release build + ctest: 125/125.

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 in TechnicalDebt (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4724585678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/433). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T06:22:04Z. --- ### What The `--to-intel` EXTRQ/INSERTQ lowerings kept the old `dst[127:64]`. Hardware zeroes it. - The APM, LLVM and MSVC docs say these bits are undefined. #185 measured that Zen 4 zeroes them for both INSERTQ forms and EXTRQ, and the register-form INSERTQ stub already does `movq dst, dst` for that reason. KytyPS5 does the same in its EXTRQ trampolines (KytyPS5/KytyPS5#839). - The other paths didn't: the shift-based EXTRQ, register-form EXTRQ, the AND/XOR INSERTQ, and the in-place forms. The PSHUFB stubs already zeroed the high qword through their mask. - This changes real results, not just undefined bits: a register-form INSERTQ that follows reads its length and index from bits 77:64 of its source. After `extrq xmm0, xmm1 ; insertq xmm14, xmm0`, the stub produced a different low qword in `xmm14` than the hardware does. Fix: - Every stub path that left the high qword now ends with `movq dst, dst`. - In-place forms: - The two that emitted nothing (INSERTQ with `dst == src` and index 0, EXTRQ with length 64 and index 0) now emit `movq dst, dst`. - PSRLQ (top-aligned EXTRQ), PMOVZX (EXTRQ of 8/16/32 bits at index 0) and PBLENDW (INSERTQ of a multiple of 16 bits at index 0) leave the high qword non-zero. They have no room for the `movq` in a 6-7 byte site, so those sites now take a stub. ### Tested Linux x86-64 (Ryzen 5 7535HS, Zen 3+, has SSE4a), no title. - `Amd64OnlyConverterTests.cpp`: - New `immediateFormExecution`: runs EXTRQ, INSERTQ and INSERTQ with equal operands for 9 length/index pairs, covering every in-place form and every stub path. It checks the low qword against the reference and `dst[127:64] == 0`, starting from a non-zero high qword. - `registerFormExecution`: the EXTRQ cases also check the high qword now. - Golden in-place encodings updated. The four removed in-place forms must now be rejected. - Fails on `main`. Negative checks: removing each of the three `movq` in the stubs, or either of the two in-place `movq`, makes the test fail again. - Differential against the native instructions (local harness, not committed): all 128 bits of the 16 XMM registers, flags, rax, rcx and rsp. Every register pair for each of EXTRQ/INSERTQ immediate and register forms and SHA256*, length/index grids, plus 3000 random 2-4 instruction sequences through `MatchSequence`. 313 420 runs: - On `main`: 15-17k mismatches per SSE4a form (`dst[127:64]`), plus 1 511 sequence runs with a wrong low qword. - With this PR: 0 mismatches. SHA-256 and register-form INSERTQ were already 0. - Full Release build + `ctest`: 125/125. ### 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 (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (n/a) - [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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!433
No description provided.