fix(relinker): lower scalar RCPSS and RSQRTSS #11192

Merged
RINNECODER merged 4 commits from upstream-pr/11192 into main 2026-11-15 02:23:02 +00:00
RINNECODER commented 2026-10-25 21:17:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by RINNECODER on 2026-10-25T21:17:25Z.


What

--to-intel (and --to-rosetta on top of it) lowered only the packed reciprocal register forms. The scalar RCPSS, RSQRTSS, VRCPSS and VRSQRTSS were left native, so they kept the host's approximation, while the packed forms right next to them got the exact value. docs/dev/debt/relinker.md listed this gap.

The register forms of all four now go through the same stub as the packed forms: SQRTSS (RSQRTSS only), then DIVSS into 1.0. The upper lanes are merged the way the instructions define them. The legacy forms keep the destination's lanes 1-3 and its upper YMM bits. The VEX forms take lanes 1-3 from the first source (VEX.vvvv) and clear the upper YMM bits. VEX.L is ignored, as it is for these LIG instructions. Memory forms and the 256-bit packed forms still keep the native instruction, as before. The debt entry is cut to match. The two VEX scalar names are added to the per-name stub summary in Conversion.cpp, the same as VRSQRTPS and VRCPPS.

Title evidence: PPSA04203 (Sid Meier's Civilization VII) has 231 of these in its eboot, all VEX register forms (225 VRSQRTSS, 6 VRCPSS, no memory forms, no legacy forms). With this change, --macos --to-rosetta on that eboot reports:

Intel substitution: VRCPSS -> stub at 6 sites
Intel substitution: VRSQRTSS -> stub at 225 sites

On main these sites stay native. I have not observed a visible wrong result in that title that this change fixes, so the Unblocks line is none.

tools/check_conventions.py reports one warning, debt-length, on the edited entry: 1068 characters. On main the entry is already 1065 characters, and this change makes it 3 characters longer.

Tested

macOS 27 arm64, Apple clang, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON):

  • amd64_only_converter (native arm64) runs the decoder and converter checks. It fails on main with the new cases (Scalar reciprocal was not lowered) and passes with this change.
  • amd64_only_converter built with -DCMAKE_OSX_ARCHITECTURES=x86_64 and run under Rosetta also executes the stubs. It fails on main (Register form stub was not produced) and passes with this change. The execution checks cover legacy and VEX encodings, distinct and identical registers, a VEX first source equal to the destination and equal to the source, and 1/sqrt(1.0) == 1.0. Each lane is compared bit for bit with 1.0f / std::sqrt(x) or 1.0f / x. I also broke the stub on purpose twice (MULSS in place of DIVSS, and MOVAPS in place of the final MOVSS), and each break failed the test.
  • tools/check_conventions.py --base upstream/main: 0 errors and 1 warning (see above).
  • The relinker target builds. Not tested on Linux or Windows, and not tested in a title run.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008804852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11192). Originally posted by [RINNECODER](https://github.com/RINNECODER) on 2026-10-25T21:17:25Z. --- ### What `--to-intel` (and `--to-rosetta` on top of it) lowered only the packed reciprocal register forms. The scalar RCPSS, RSQRTSS, VRCPSS and VRSQRTSS were left native, so they kept the host's approximation, while the packed forms right next to them got the exact value. `docs/dev/debt/relinker.md` listed this gap. The register forms of all four now go through the same stub as the packed forms: SQRTSS (RSQRTSS only), then DIVSS into 1.0. The upper lanes are merged the way the instructions define them. The legacy forms keep the destination's lanes 1-3 and its upper YMM bits. The VEX forms take lanes 1-3 from the first source (VEX.vvvv) and clear the upper YMM bits. VEX.L is ignored, as it is for these LIG instructions. Memory forms and the 256-bit packed forms still keep the native instruction, as before. The debt entry is cut to match. The two VEX scalar names are added to the per-name stub summary in `Conversion.cpp`, the same as VRSQRTPS and VRCPPS. Title evidence: PPSA04203 (Sid Meier's Civilization VII) has 231 of these in its eboot, all VEX register forms (225 VRSQRTSS, 6 VRCPSS, no memory forms, no legacy forms). With this change, `--macos --to-rosetta` on that eboot reports: ``` Intel substitution: VRCPSS -> stub at 6 sites Intel substitution: VRSQRTSS -> stub at 225 sites ``` On `main` these sites stay native. I have not observed a visible wrong result in that title that this change fixes, so the Unblocks line is none. `tools/check_conventions.py` reports one warning, `debt-length`, on the edited entry: 1068 characters. On `main` the entry is already 1065 characters, and this change makes it 3 characters longer. ### Tested macOS 27 arm64, Apple clang, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`): - `amd64_only_converter` (native arm64) runs the decoder and converter checks. It fails on `main` with the new cases (`Scalar reciprocal was not lowered`) and passes with this change. - `amd64_only_converter` built with `-DCMAKE_OSX_ARCHITECTURES=x86_64` and run under Rosetta also executes the stubs. It fails on `main` (`Register form stub was not produced`) and passes with this change. The execution checks cover legacy and VEX encodings, distinct and identical registers, a VEX first source equal to the destination and equal to the source, and 1/sqrt(1.0) == 1.0. Each lane is compared bit for bit with `1.0f / std::sqrt(x)` or `1.0f / x`. I also broke the stub on purpose twice (MULSS in place of DIVSS, and MOVAPS in place of the final MOVSS), and each break failed the test. - `tools/check_conventions.py --base upstream/main`: 0 errors and 1 warning (see above). - The `relinker` target builds. Not tested on Linux or Windows, and not tested in a title run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-25 21:23:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T21:23:50Z.

Review: COMMENTED


Independent local check at head 825fed94f823600bbffba440d8d5e26a7b7b14a7: the relinker-only Release build completes, and the isolated reciprocalOperands, reciprocalExecution and converterReciprocal tests pass, including execution of the generated scalar stubs. The full amd64_only_converter CTest stops earlier in the existing xsaveoptExecution test with SIGILL on this host; /proc/cpuinfo exposes XSAVE but not OSXSAVE, so I cannot count that full-suite run as evidence against this change. Conventions report zero errors and the disclosed debt-length warning (1,068 characters). Hosted Ubuntu relinker and ASan checks pass; Windows and macOS checks are still pending.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5645387040 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11192#pullrequestreview-5645387040). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T21:23:50Z. **Review:** COMMENTED --- Independent local check at head `825fed94f823600bbffba440d8d5e26a7b7b14a7`: the relinker-only Release build completes, and the isolated `reciprocalOperands`, `reciprocalExecution` and `converterReciprocal` tests pass, including execution of the generated scalar stubs. The full `amd64_only_converter` CTest stops earlier in the existing `xsaveoptExecution` test with SIGILL on this host; `/proc/cpuinfo` exposes XSAVE but not OSXSAVE, so I cannot count that full-suite run as evidence against this change. Conventions report zero errors and the disclosed debt-length warning (1,068 characters). Hosted Ubuntu relinker and ASan checks pass; Windows and macOS checks are still pending.
roboterspinne commented 2026-10-26 18:07:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-26T18:07:35Z.

Review: COMMENTED


Reviewed 825fed94f merged onto main 570089a80 (2026-10-26); the merge is clean. Windows 11, i9-12900H, WinLibs GCC 15.2, relinker-only Release.

Results

  • Full ctest: 308 of 309 pass; patch_advisor_unit fails the same way on plain main. amd64_only_converter passes, but on Windows it runs only the decode and convert checks. The execution checks compile only for Linux and macOS x86-64 (Amd64OnlyConverterTests.cpp:1505), so this run executes no stub.
  • To run the stubs natively, I linked a probe against the built converter objects. It runs each lowered site and the original instruction from the same state: all 16 YMM registers with distinct patterns, RFLAGS, and MXCSR 0x1F80 and 0x9FC0. That gave 27 scalar encodings × 21 inputs × 2 MXCSR values. Outside lane 0 of the destination and MXCSR, the stub matches the native instruction everywhere:
    • legacy keeps lanes 1-3 and bits 128-255;
    • VEX takes lanes 1-3 from vvvv and zeroes the upper bits;
    • xmm8-15 via REX.R/B, VEX.R/B and vvvv ≥ 8;
    • identical registers, and vvvv equal to the stub's scratch register xmm0;
    • VEX.L=1 and VEX.W=1.
  • The scratch register is spilled and restored with its upper bits, and RFLAGS, rsp and rbx are unchanged.
  • (V)RCPSS gives the correctly rounded 1/x in every case, and NaN handling matches native.
  • The matcher leaves memory forms, VEX pp=1/3 and the 256-bit forms alone.
  • Counter-check: with main's ReciprocalOperands.cpp, ReciprocalLowering.cpp and Amd64OnlyInstructionMatcher.cpp (headers kept), the test fails at "VEX2 VRSQRTSS was not decoded". Reverting only the matcher gives your "Scalar reciprocal was not lowered", and reverting the headers too doesn't compile. With your files it passes.

Should fix (text): see the inline comment on the debt entry. The RSQRT forms are not correctly rounded.

Minor: see the inline comment on ReciprocalLowering.cpp. The stub differs from the native instruction in MXCSR behaviour, and the debt entry doesn't record it.

Nits

  • Several wrong variants pass the execution checks, because those use only xmm2 and xmm5 and read only the low 128 bits:

    • masking vvvv with 7;
    • swapping or dropping the R/B bits in _vex;
    • emitting the legacy form so that it clears bits 128-255.

    Sites with vvvv ≥ 8 (e.g. C5 A2 53 D5), a VEX3 destination and source ≥ 8 (e.g. C4 C1 0A 52 D7), and a check of bits 128-255 would catch them.

  • Debt wording:

    • The scalar forms are VEX.LIG and are lowered with L=1 too, so "VEX.128 register forms" fits only the packed ones.
    • "SQRTPS/DIVPS" doesn't match the scalar stub, which emits SQRTSS or MOVSS, then DIVSS and MOVSS.
  • Body:

    • The red message holds only when just the matcher is reverted (see above).
    • The entry is 1064 characters on main and 1068 here, so the PR adds 4, not 3.

Not checked: what Zen 2's RCPSS and RSQRTSS return; the MXCSR that ordinary threads of relinked output run with on Windows and Linux; PPSA04203 or any title run; Linux and macOS x86-64, where the execution checks compile.

For disclosure: my open PRs #11061, #11045, #10971 and #10947 also edit docs/dev/debt/relinker.md; each merges cleanly with this one.

Written with AI assistance (Claude Code). I ran the build, the counter-check and the native probes above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5656752045 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11192#pullrequestreview-5656752045). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-26T18:07:35Z. **Review:** COMMENTED --- Reviewed 825fed94f merged onto `main` 570089a80 (2026-10-26); the merge is clean. Windows 11, i9-12900H, WinLibs GCC 15.2, relinker-only Release. **Results** - Full `ctest`: 308 of 309 pass; `patch_advisor_unit` fails the same way on plain `main`. `amd64_only_converter` passes, but on Windows it runs only the decode and convert checks. The execution checks compile only for Linux and macOS x86-64 (`Amd64OnlyConverterTests.cpp:1505`), so this run executes no stub. - To run the stubs natively, I linked a probe against the built converter objects. It runs each lowered site and the original instruction from the same state: all 16 YMM registers with distinct patterns, RFLAGS, and MXCSR 0x1F80 and 0x9FC0. That gave 27 scalar encodings × 21 inputs × 2 MXCSR values. Outside lane 0 of the destination and MXCSR, the stub matches the native instruction everywhere: - legacy keeps lanes 1-3 and bits 128-255; - VEX takes lanes 1-3 from vvvv and zeroes the upper bits; - xmm8-15 via REX.R/B, VEX.R/B and vvvv ≥ 8; - identical registers, and vvvv equal to the stub's scratch register xmm0; - VEX.L=1 and VEX.W=1. - The scratch register is spilled and restored with its upper bits, and RFLAGS, rsp and rbx are unchanged. - (V)RCPSS gives the correctly rounded 1/x in every case, and NaN handling matches native. - The matcher leaves memory forms, VEX pp=1/3 and the 256-bit forms alone. - Counter-check: with `main`'s `ReciprocalOperands.cpp`, `ReciprocalLowering.cpp` and `Amd64OnlyInstructionMatcher.cpp` (headers kept), the test fails at "VEX2 VRSQRTSS was not decoded". Reverting only the matcher gives your "Scalar reciprocal was not lowered", and reverting the headers too doesn't compile. With your files it passes. **Should fix (text):** see the inline comment on the debt entry. The RSQRT forms are not correctly rounded. **Minor:** see the inline comment on `ReciprocalLowering.cpp`. The stub differs from the native instruction in MXCSR behaviour, and the debt entry doesn't record it. **Nits** - Several wrong variants pass the execution checks, because those use only xmm2 and xmm5 and read only the low 128 bits: - masking vvvv with 7; - swapping or dropping the R/B bits in `_vex`; - emitting the legacy form so that it clears bits 128-255. Sites with vvvv ≥ 8 (e.g. `C5 A2 53 D5`), a VEX3 destination and source ≥ 8 (e.g. `C4 C1 0A 52 D7`), and a check of bits 128-255 would catch them. - Debt wording: - The scalar forms are VEX.LIG and are lowered with L=1 too, so "VEX.128 register forms" fits only the packed ones. - "SQRTPS/DIVPS" doesn't match the scalar stub, which emits SQRTSS or MOVSS, then DIVSS and MOVSS. - Body: - The red message holds only when just the matcher is reverted (see above). - The entry is 1064 characters on `main` and 1068 here, so the PR adds 4, not 3. **Not checked:** what Zen 2's RCPSS and RSQRTSS return; the MXCSR that ordinary threads of relinked output run with on Windows and Linux; PPSA04203 or any title run; Linux and macOS x86-64, where the execution checks compile. For disclosure: my open PRs #11061, #11045, #10971 and #10947 also edit `docs/dev/debt/relinker.md`; each merges cleanly with this one. Written with AI assistance (Claude Code). I ran the build, the counter-check and the native probes above on my Windows 11 machine.
MarcZX10 commented 2026-10-29 09:40:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:40:57Z.

Review: APPROVED


Re-checked — approved

The previously reviewed reciprocal-lowering implementation is unchanged in the current branch; the latest merge brings in current main and retains the debt update. The register/flag probe and focused implementation review remain applicable, and the exact current head's hosted relinker matrix passes on Linux, macOS (including x86_64), and Windows. The conventions, labels and eligibility checks pass as well.

The full Linux product job is skipped. The Windows product job passes, but its amd64_only_converter target does not execute the scalar stubs; the earlier reviewer probe is the reported native execution comparison. I did not rerun the probe locally. No blocking issue found; approval covers exact head 2750fe2657c3b723cc82b12691586eaf10d29cce.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695643021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11192#pullrequestreview-5695643021). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:40:57Z. **Review:** APPROVED --- # Re-checked — approved The previously reviewed reciprocal-lowering implementation is unchanged in the current branch; the latest merge brings in current `main` and retains the debt update. The register/flag probe and focused implementation review remain applicable, and the exact current head's hosted relinker matrix passes on Linux, macOS (including x86_64), and Windows. The conventions, labels and eligibility checks pass as well. The full Linux product job is skipped. The Windows product job passes, but its `amd64_only_converter` target does not execute the scalar stubs; the earlier reviewer probe is the reported native execution comparison. I did not rerun the probe locally. No blocking issue found; approval covers exact head `2750fe2657c3b723cc82b12691586eaf10d29cce`.
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!11192
No description provided.