fix(relinker): lower legacy packed reciprocal register instructions #3601

Merged
Scramblrrrr merged 1 commit from upstream-pr/3601 into main 2026-10-12 02:43:21 +00:00
Scramblrrrr commented 2026-10-11 14:08:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-11T14:08:19Z.


What

--to-intel leaves legacy SSE register forms of RSQRTPS and RCPPS native, although it already replaces their VEX.128 equivalents to avoid reciprocal approximation differences. This PR extends the existing lowering to those legacy register forms, including REX-extended registers and equal source/destination operands.

The gap is recorded in TechnicalDebt: legacy forms retain the host approximation. The same entry documents why the existing VEX lowering uses division and square root: an approximate reciprocal square root of 1.0 can remain below 1.0 after refinement. This change applies the existing policy to SSE code; it does not reproduce AMD's approximation tables.

Instruction form Before After
Legacy register RSQRTPS Native approximation Existing SQRTPS then DIVPS lowering; upper YMM bits preserved
Legacy register RCPPS Native approximation Existing DIVPS lowering; upper YMM bits preserved
VEX.128 register equivalents Existing lowering; upper YMM bits cleared Same behavior

For example, a supported legacy RSQRTPS with all input lanes equal to 1.0 now produces exactly 1.0 in every lane, through the same arithmetic used for its VEX equivalent.

Implementation:

  • Rename the internal decoder to DecodeReciprocal, accept legacy register encodings with an optional REX prefix, and record whether the instruction uses VEX.
  • Reuse ReciprocalLowering and the existing scratch-register spill/restore path. Emit a legacy result move for RCPPS, and avoid the final VEX move for legacy RSQRTPS, so upper YMM bits remain intact.
  • Report RSQRTPS/RCPPS separately from their VEX equivalents in conversion diagnostics. Support mixed legacy/VEX sequences through the existing out-of-line sequence builder.
  • Update the corresponding TechnicalDebt entry to describe the additional supported forms and register behavior.

The substitution remains optional. Sites shorter than the five-byte jump absorb movable following instructions, or stay native when a jump cannot fit safely. Scalar, memory and 256-bit forms retain their existing behavior. No new dependencies or system-library exports are added.

Tested

Head 363d17985e, base main a11c8f7f70. Windows 11, Ryzen 7800X3D, Release MinGW GCC 15.2.

Fresh standalone build: amd64_only_converter and amd64_only_windows, 2/2 pass, 0.61 seconds. Covers legacy register forms, overlapping source/destination, extended registers, preserved upper YMM bits, and existing VEX behavior. Conventions and diff checks pass. The rebase retains main's optional Rosetta RDPID handling and its technical-debt entry. Linux and macOS were not run locally; no new title result is claimed.

Replaces closed #2377, whose historical branch cannot be reopened after it was recreated/force-pushed. Its earlier closure was intake, not a code review; the author now has accepted commits on main. No open equivalent reciprocal PR was found. This new PR contains the current tested topic, not the experimental 007 integration.

Checklist

  • Based on current main; no equivalent open PR found
  • One topic per PR
  • No new code comments
  • Unsupported paths retain explicit behavior
  • No investigation files, title data or images
  • No new third-party code
  • Generic behavior
  • Depends on: none
  • AI-assisted: yes, Codex; author responsible for review
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819652245 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3601). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-11T14:08:19Z. --- ### What `--to-intel` leaves legacy SSE register forms of `RSQRTPS` and `RCPPS` native, although it already replaces their VEX.128 equivalents to avoid reciprocal approximation differences. This PR extends the existing lowering to those legacy register forms, including REX-extended registers and equal source/destination operands. The gap is recorded in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/d70b89989473ba1f6ae13e44e079e67f1f8a44b0/docs/dev/TechnicalDebt.md#functional): legacy forms retain the host approximation. The same entry documents why the existing VEX lowering uses division and square root: an approximate reciprocal square root of `1.0` can remain below `1.0` after refinement. This change applies the existing policy to SSE code; it does not reproduce AMD's approximation tables. | Instruction form | Before | After | |---|---|---| | Legacy register `RSQRTPS` | Native approximation | Existing `SQRTPS` then `DIVPS` lowering; upper YMM bits preserved | | Legacy register `RCPPS` | Native approximation | Existing `DIVPS` lowering; upper YMM bits preserved | | VEX.128 register equivalents | Existing lowering; upper YMM bits cleared | Same behavior | For example, a supported legacy `RSQRTPS` with all input lanes equal to `1.0` now produces exactly `1.0` in every lane, through the same arithmetic used for its VEX equivalent. Implementation: - Rename the internal decoder to `DecodeReciprocal`, accept legacy register encodings with an optional REX prefix, and record whether the instruction uses VEX. - Reuse `ReciprocalLowering` and the existing scratch-register spill/restore path. Emit a legacy result move for `RCPPS`, and avoid the final VEX move for legacy `RSQRTPS`, so upper YMM bits remain intact. - Report `RSQRTPS`/`RCPPS` separately from their VEX equivalents in conversion diagnostics. Support mixed legacy/VEX sequences through the existing out-of-line sequence builder. - Update the corresponding TechnicalDebt entry to describe the additional supported forms and register behavior. The substitution remains optional. Sites shorter than the five-byte jump absorb movable following instructions, or stay native when a jump cannot fit safely. Scalar, memory and 256-bit forms retain their existing behavior. No new dependencies or system-library exports are added. ### Tested Head **363d17985e2551dfbe3d3ec81ca3f4bb893b4fc7**, base main **a11c8f7f70a0ed4e71504a89de69e8cdae687817**. Windows 11, Ryzen 7800X3D, Release MinGW GCC 15.2. Fresh standalone build: **amd64_only_converter and amd64_only_windows, 2/2 pass, 0.61 seconds**. Covers legacy register forms, overlapping source/destination, extended registers, preserved upper YMM bits, and existing VEX behavior. Conventions and diff checks pass. The rebase retains main's optional Rosetta RDPID handling and its technical-debt entry. Linux and macOS were not run locally; no new title result is claimed. Replaces closed **#2377**, whose historical branch cannot be reopened after it was recreated/force-pushed. Its earlier closure was intake, not a code review; the author now has accepted commits on main. No open equivalent reciprocal PR was found. This new PR contains the current tested topic, not the experimental 007 integration. ### Checklist - [x] Based on current main; no equivalent open PR found - [x] One topic per PR - [x] No new code comments - [x] Unsupported paths retain explicit behavior - [x] No investigation files, title data or images - [x] No new third-party code - [x] Generic behavior - [x] Depends on: none - [x] AI-assisted: yes, Codex; author responsible for review
blawesom commented 2026-10-11 15:20:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-11T15:20:12Z.


Independent pre-review on Linux (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON.

  • Builds; tools/check_conventions.py --base origin/main passes.
  • Full suite: 832/832 passed (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110521601 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3601#issuecomment-6110521601). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-11T15:20:12Z. --- Independent pre-review on **Linux** (full CI runs only happen on manual trigger, so recording one): CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`. - Builds; `tools/check_conventions.py --base origin/main` passes. - Full suite: **832/832 passed** (all tests including the Vulkan suites on AMD RX 9070 XT / RADV).
Scramblrrrr commented 2026-10-11 19:42:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-11T19:42:25Z.


Thank you for the independent Linux/RADV cross-test and the full 832/832 result. That complements my focused Windows reciprocal-converter checks; I have kept this PR scoped to reciprocal decoding and preserved main's Rosetta matcher entry. No additional code changes since 363d17985e.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6112964278 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3601#issuecomment-6112964278). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-11T19:42:25Z. --- Thank you for the independent Linux/RADV cross-test and the full 832/832 result. That complements my focused Windows reciprocal-converter checks; I have kept this PR scoped to reciprocal decoding and preserved main's Rosetta matcher entry. No additional code changes since 363d17985e2551dfbe3d3ec81ca3f4bb893b4fc7.
github-actions[bot] commented 2026-10-12 01:16:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T01:16:00Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4194 core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp L51
core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp L222-250, L257-258
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116025376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3601#issuecomment-6116025376). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T01:16:00Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4194 | `core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp` [L51](https://github.com/boykopovar/AnyPS5/blob/f49f95873c46acc499dc4f53475b411cfb6f7e2f/core/relinker/codegen/include/codegen/x86/Amd64OnlySubstitutionTable.hpp#L51)<br>`core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp` [L222-250](https://github.com/boykopovar/AnyPS5/blob/f49f95873c46acc499dc4f53475b411cfb6f7e2f/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp#L222-L250), [L257-258](https://github.com/boykopovar/AnyPS5/blob/f49f95873c46acc499dc4f53475b411cfb6f7e2f/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp#L257-L258) | | |
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!3601
No description provided.