feat(relinker): lower SHLX, SHRX, SARX and RORX under --to-rosetta #10983

Merged
AdnanKhan-ak47 merged 9 commits from upstream-pr/10983 into main 2026-11-02 10:27:01 +00:00
AdnanKhan-ak47 commented 2026-10-25 12:21:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T12:21:39Z.


What

--to-rosetta lowers the BMI2 shifts SHLX, SHRX and SARX (VEX.0F38 F7, pp 1, 3, 2) and the rotate RORX (VEX.F2.0F3A F0 ib), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN and BLS use (ShiftLowering.cpp). Balatro (PPSA21402, #1142) stopped on shlxq %rax, %rdx, %r8 right after the ANDN lowering landed (#5784); this is that instruction, but the title has not been run with this change.

Unlike ANDN and BLS, these four leave RFLAGS untouched where shl, shr, sar and ror write them. The stub reserves a 160-byte frame, two slots more than the 144-byte one so that four slots sit below the red zone, saves the flags with pushfq into the third slot, spills the scratch result register and RCX, loads the source (memory and RSP forms as ANDN), moves the count into RCX, shifts by CL or rotates by the immediate, writes the destination (through the RCX spill slot when the destination is RCX, through the fourth slot when it is RSP), then popfqs the saved flags and restores the spills. The source is loaded before RCX is overwritten, so a memory operand based on or indexed by RCX reads the caller's value. Both the BMI2 instruction and the scalar one mask the count to the operand size. BEXTR is the pp=0 form of the same opcode and is left unmatched.

ScalarLowering.hpp's Memory takes the frame size so a lowering can use a larger frame. tests/LoweringExecution.hpp now loads State::InputFlags into RFLAGS before the stub and takes the number of bytes after a RIP-relative displacement, so RORX's immediate relocates correctly; the ANDN and BLS tests now run with all four defined flags set on entry and still pass, which proves they define rather than inherit them. ShiftLoweringTests.cpp decodes every register triple and width, the memory and RORX immediate forms, the rejections (VEX.L, BEXTR, wrong map, non-1111 vvvv on RORX, missing or extra bytes), checks Intel versus Rosetta matching, and on an x86-64 host executes the three shifts × 2 widths × 16 destinations × 9 source/count pairs (RSP and RCX as source, count and destination, aliases) × 6 values × 7 counts (0, 1, 31, 32, 63, 64, 255) × 2 entry flag states, RORX × 6 immediates, and 6 memory addressing forms × 4 operations × 3 values, checking the result, every other register, that CF/ZF/SF/OF equal the entry flags, and the red zone.

docs/user/USAGE.md lists the lowering; docs/dev/debt/relinker.md records it and the BMI instructions still not lowered (BEXTR, BZHI, PDEP and PEXT; MULX is lowered since #12255). The summary entries list MULX with the lowered instructions.

Tested

macOS 15 arm64 relinker-only build, AppleClang 21: ctest 256/256 (decoding and matching; execution compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build: andn_lowering, bls_lowering and shift_lowering pass with the execution checks on the host CPU; the harness caught, in order, a flags slot inside the red zone, a stale expected value for an RSP source, and the RORX displacement patch before anything was pushed as final. Not run against a title.

Current head 7f0b73575b (rebased on main f7f03daf52), on the stack top 6ad23bffdd, which contains this PR: macOS 27.2 (M4) x86_64 under Rosetta 516/516 with the execution checks on; arm64 513/513. Arch Linux not rerun after the rebase (506/506 on the previous head f5e1bbf4a5). tools/check_conventions.py --base origin/main: 0 errors, one warning, the length of the --to-rosetta entry in relinker.md, which is already over the limit on main.

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: #10970; merge after #7173 and #7531 (merge order only: additive conflicts in core/relinker/CMakeLists.txt, Amd64OnlyInstructionMatcher.cpp and docs/dev/debt/relinker.md, resolved by keeping both sides; this branch is rebased when they land)
  • Fixes: none
  • Unblocks: PPSA21402 #1142 (the shlxq stop reported there; not run on the title)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004679638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10983). Originally posted by [AdnanKhan-ak47](https://github.com/AdnanKhan-ak47) on 2026-10-25T12:21:39Z. --- ### What `--to-rosetta` lowers the BMI2 shifts SHLX, SHRX and SARX (`VEX.0F38 F7`, `pp` 1, 3, 2) and the rotate RORX (`VEX.F2.0F3A F0 ib`), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN and BLS use (`ShiftLowering.cpp`). Balatro (PPSA21402, #1142) stopped on `shlxq %rax, %rdx, %r8` right after the ANDN lowering landed (#5784); this is that instruction, but the title has not been run with this change. Unlike ANDN and BLS, these four leave RFLAGS untouched where `shl`, `shr`, `sar` and `ror` write them. The stub reserves a 160-byte frame, two slots more than the 144-byte one so that four slots sit below the red zone, saves the flags with `pushfq` into the third slot, spills the scratch result register and RCX, loads the source (memory and RSP forms as ANDN), moves the count into RCX, shifts by CL or rotates by the immediate, writes the destination (through the RCX spill slot when the destination is RCX, through the fourth slot when it is RSP), then `popfq`s the saved flags and restores the spills. The source is loaded before RCX is overwritten, so a memory operand based on or indexed by RCX reads the caller's value. Both the BMI2 instruction and the scalar one mask the count to the operand size. BEXTR is the `pp=0` form of the same opcode and is left unmatched. `ScalarLowering.hpp`'s `Memory` takes the frame size so a lowering can use a larger frame. `tests/LoweringExecution.hpp` now loads `State::InputFlags` into RFLAGS before the stub and takes the number of bytes after a RIP-relative displacement, so RORX's immediate relocates correctly; the ANDN and BLS tests now run with all four defined flags set on entry and still pass, which proves they define rather than inherit them. `ShiftLoweringTests.cpp` decodes every register triple and width, the memory and RORX immediate forms, the rejections (`VEX.L`, BEXTR, wrong map, non-1111 `vvvv` on RORX, missing or extra bytes), checks Intel versus Rosetta matching, and on an x86-64 host executes the three shifts × 2 widths × 16 destinations × 9 source/count pairs (RSP and RCX as source, count and destination, aliases) × 6 values × 7 counts (0, 1, 31, 32, 63, 64, 255) × 2 entry flag states, RORX × 6 immediates, and 6 memory addressing forms × 4 operations × 3 values, checking the result, every other register, that CF/ZF/SF/OF equal the entry flags, and the red zone. `docs/user/USAGE.md` lists the lowering; `docs/dev/debt/relinker.md` records it and the BMI instructions still not lowered (BEXTR, BZHI, PDEP and PEXT; MULX is lowered since #12255). The summary entries list MULX with the lowered instructions. ### Tested macOS 15 arm64 relinker-only build, AppleClang 21: `ctest` 256/256 (decoding and matching; execution compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build: `andn_lowering`, `bls_lowering` and `shift_lowering` pass with the execution checks on the host CPU; the harness caught, in order, a flags slot inside the red zone, a stale expected value for an RSP source, and the RORX displacement patch before anything was pushed as final. Not run against a title. Current head `7f0b73575b` (rebased on `main` `f7f03daf52`), on the stack top `6ad23bffdd`, which contains this PR: macOS 27.2 (M4) x86_64 under Rosetta 516/516 with the execution checks on; arm64 513/513. Arch Linux not rerun after the rebase (506/506 on the previous head `f5e1bbf4a5`). `tools/check_conventions.py --base origin/main`: 0 errors, one warning, the length of the `--to-rosetta` entry in `relinker.md`, which is already over the limit on `main`. ### 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: #10970; merge after #7173 and #7531 (merge order only: additive conflicts in `core/relinker/CMakeLists.txt`, `Amd64OnlyInstructionMatcher.cpp` and `docs/dev/debt/relinker.md`, resolved by keeping both sides; this branch is rebased when they land) - [x] Fixes: none - [x] Unblocks: PPSA21402 #1142 (the `shlxq` stop reported there; not run on the title) - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 12:41:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T12:41:54Z.

Review: CHANGES_REQUESTED


The macOS x86_64 hosted job currently fails at the new shift_lowering test: the other 257 tests in that run pass, but CTest reports Subprocess aborted for test 90 and exits 8. I built and ran shift_lowering_tests from this exact head (b52a9b04ba718666e3982dc701a962f73b4d3bc5) on Linux x86_64; it passes there, so I cannot reproduce the abort locally.

Since this PR explicitly has not been tested on a Rosetta host and this new test is what makes the macOS x86_64 job fail, please investigate the abort under that runner and either fix the incompatibility or gate only the execution portion when the host cannot run it, while retaining decoding/matching coverage. Please rerun the macOS x86_64 check before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643239774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10983#pullrequestreview-5643239774). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T12:41:54Z. **Review:** CHANGES_REQUESTED --- The macOS x86_64 hosted job currently fails at the new `shift_lowering` test: the other 257 tests in that run pass, but CTest reports `Subprocess aborted` for test 90 and exits 8. I built and ran `shift_lowering_tests` from this exact head (`b52a9b04ba718666e3982dc701a962f73b4d3bc5`) on Linux x86_64; it passes there, so I cannot reproduce the abort locally. Since this PR explicitly has not been tested on a Rosetta host and this new test is what makes the macOS x86_64 job fail, please investigate the abort under that runner and either fix the incompatibility or gate only the execution portion when the host cannot run it, while retaining decoding/matching coverage. Please rerun the macOS x86_64 check before merge.
MarcZX10 commented 2026-10-25 14:04:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T14:04:15Z.

Review: CHANGES_REQUESTED


The RORX immediate/register-set fix resolves my earlier macOS x86_64 test concern. I independently configured and built the relinker-only Release tree; andn_lowering, bls_lowering and shift_lowering pass locally. Conventions and whitespace pass. The full local suite completed with 266/268 passing; masked_store_lowering and amd64_only_converter both terminate with Illegal on this host. Hosted Linux, Windows, macOS arm64, macOS x86_64, ASan, intake, eligibility, labels and conventions checks now pass; product-only jobs were skipped.

One integration blocker remains. I reran tools/pr_overlap.py against current main e05ba6fe and confirmed git merge-tree reports content conflicts with open #7173 in core/relinker/CMakeLists.txt, Amd64OnlyInstructionMatcher.cpp and docs/dev/debt/relinker.md; #7531 reports the same locations because it is stacked on #7173. #10861 conflicts in docs/dev/debt/relinker.md. I manually checked the scopes: ADX lowering and code-scan-boundary analysis are separate from these BMI shifts, so this is merge-order/integration work, not duplicate implementation. Please coordinate an order with the affected authors and record the merge-only dependencies (or the agreed integration plan) before merge, preserving the matcher registrations, test registrations and debt entries from each branch. #7531 already depends on #7173, so account for that follow-up when choosing the sequence.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643587997 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10983#pullrequestreview-5643587997). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:04:15Z. **Review:** CHANGES_REQUESTED --- The RORX immediate/register-set fix resolves my earlier macOS x86_64 test concern. I independently configured and built the relinker-only Release tree; `andn_lowering`, `bls_lowering` and `shift_lowering` pass locally. Conventions and whitespace pass. The full local suite completed with 266/268 passing; `masked_store_lowering` and `amd64_only_converter` both terminate with `Illegal` on this host. Hosted Linux, Windows, macOS arm64, macOS x86_64, ASan, intake, eligibility, labels and conventions checks now pass; product-only jobs were skipped. One integration blocker remains. I reran `tools/pr_overlap.py` against current main `e05ba6fe` and confirmed `git merge-tree` reports content conflicts with open #7173 in `core/relinker/CMakeLists.txt`, `Amd64OnlyInstructionMatcher.cpp` and `docs/dev/debt/relinker.md`; #7531 reports the same locations because it is stacked on #7173. #10861 conflicts in `docs/dev/debt/relinker.md`. I manually checked the scopes: ADX lowering and code-scan-boundary analysis are separate from these BMI shifts, so this is merge-order/integration work, not duplicate implementation. Please coordinate an order with the affected authors and record the merge-only dependencies (or the agreed integration plan) before merge, preserving the matcher registrations, test registrations and debt entries from each branch. #7531 already depends on #7173, so account for that follow-up when choosing the sequence.
XairRrR commented 2026-10-29 12:54:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T12:54:09Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The SHLX/SHRX/SARX/RORX lowering merges cleanly, builds, passes its tests, and the tests fail under four mutations. One blocking item remains: the new debt entry says MULX is not lowered, but MULX lowering (#12255) is already in this branch's base.

What I ran

  • Head: 25714e1ca83e81848e23ad8a31c72acc4d02da94 (stacked on #10970 at 3e29bdc6506d85d8d2875a344352646a504beced)
  • main: aab650b4d39785f5af1402612d419bc52b839c07; the merge with the head was clean.
  • Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release, Ninja, BUILD_TESTING=ON, in a clean worktree.
git fetch -q origin main pull/10983/head:refs/review/10983
git checkout -q --detach origin/main
git merge -q --no-ff --no-edit refs/review/10983
git diff --check origin/main...refs/review/10983
python3 tools/check_conventions.py --base origin/main
nice -n 10 cmake --build build -j10 --target shift_lowering_tests bls_lowering_tests andn_lowering_tests mulx_lowering_tests amd64_only_converter_tests relinker
nice -n 10 ctest --test-dir build -R '^(shift_lowering|bls_lowering|andn_lowering|mulx_lowering|amd64_only_converter|to_rosetta)$' --output-on-failure -j4

Results

Check Result
Merge onto main Clean
git diff --check Clean
check_conventions.py 0 errors, 2 warnings (debt-length on relinker.md:48, which predates this PR, and on the new entry at relinker.md:56)
Build of the affected targets OK
shift_lowering, bls_lowering, andn_lowering, mulx_lowering, amd64_only_converter, to_rosetta 6/6 pass; shift_lowering takes 0.09 s with the execution checks on
ctest
1/6 Test #2690: andn_lowering ....................   Passed    0.00 sec
2/6 Test #2691: bls_lowering .....................   Passed    0.01 sec
3/6 Test #2722: amd64_only_converter .............   Passed    0.01 sec
4/6 Test #2882: to_rosetta .......................   Passed    0.07 sec
5/6 Test #2694: mulx_lowering ....................   Passed    0.08 sec
6/6 Test #2692: shift_lowering ...................   Passed    0.09 sec
100% tests passed out of 6
check_conventions.py
docs/dev/debt/relinker.md:48: warning: [debt-length] Technical debt entry over 1000 characters: ... 2075 characters
docs/dev/debt/relinker.md:56: warning: [debt-length] Technical debt entry over 1000 characters: ... 1123 characters
2 findings, 0 errors

Negative control

The production code here is all new, so restoring it from main would only break the build. I applied four separate mutations to the production files instead, rebuilt shift_lowering_tests after each one, and restored the file before the next:

Mutation Test result
Removed the DecodeShift hooks from _matchRosetta and _emitOutOfLine Fails: Rosetta mode did not require a trampoline for SHLX
Replaced pushfq / pop [rsp+16] / push [rsp+16] / popfq with non-flag moves Fails: SHLX did not preserve the flags
Restored the pre-25714e1ca8 RORX register set (immediate passed to UsedRegisters) Fails: register index 16 is out of range
SAR /7 changed to SHR /5 Fails: SARX 32 dst 0 src 6 count 2: register 0 is 1073741824, wanted 3221225472

After the files were restored, shift_lowering passed again.

Findings

Blocking

  • The debt entries say MULX is not lowered, but this branch lowers it. docs/dev/debt/relinker.md:55-56 both end with "BEXTR, BZHI, MULX, PDEP and PEXT are not lowered". The PR description says the same. MULX lowering landed in #12255, and that merge (4004c97114) is an ancestor of this branch's base 5454cd2e45. At this head the matcher already dispatches it (Amd64OnlyInstructionMatcher.cpp:287), and relinker.md:5 has its own entry. Line 56 is new in this PR, and line 55 is the BLS entry this PR rewrites. Both now give a wrong list of remaining gaps. Fix: drop MULX from both lists ("BEXTR, BZHI, PDEP and PEXT are not lowered"), and from the description.

Non-blocking

  • The two summary entries this PR edits still leave MULX out of the lowered set. relinker.md:48 reads "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX and RORX are lowered, but … the other BMI instructions still depend on the host". Line 49 reads "other than ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX, TZCNT and LZCNT". This gap comes from #12255, not from this PR. Since both lines are already rewritten here, adding MULX would make them match the code.
  • The new debt entry is over the length limit, and part of it is a title note. relinker.md:56 is 1123 characters, so check_conventions.py warns on it. The description mentions only the relinker.md:38 warning. The sentence "Balatro (PPSA21402, #1142) stopped on shlxq after the ANDN lowering; this lowering has not been run on that title." is a note about one title, which belongs in the PR, not the debt file. Removing it leaves 1006 characters, so a little more trimming would clear the warning.
  • When the destination is RSP, the result is read back from below the red zone after RSP is restored. ShiftLowering.cpp:126-128 emits lea rsp, [rsp+160] and then mov rsp, [rsp-136]. Between those two instructions the slot lies 8 bytes below the 128-byte red zone, so an asynchronous signal frame delivered there could overwrite it before it is read. One instruction, mov rsp, [rsp+24] (mov esp, [rsp+24] for the 32-bit form), produces the same final RSP with no such window. The ANDN and BLS stubs use the same two-instruction pattern (AndnLowering.cpp:59-61, BlsLowering.cpp:129-131), and an RSP destination for SHLX/RORX is unlikely in compiled code, so this need not block. It could be fixed here for the new stub or in a follow-up for all three.

I checked the rest of the code against the Intel SDM encodings and found nothing wrong:

  • the pp mapping (66 = SHLX, F3 = SARX, F2 = SHRX, F2.0F3A F0 = RORX), with BEXTR at pp=0 left unmatched;
  • the /4 /5 /7 /1 digits;
  • count masking that matches the scalar forms;
  • the source being loaded before RCX is overwritten (the test's memory forms use [rdx+rcx*8] with the count in RCX);
  • the RCX- and RSP-destination paths;
  • zero-extension of the 32-bit destination;
  • pop m64 / push m64 with an RSP base (the effective address is taken after or before the RSP update respectively).

Regarding the earlier review, I confirm the RORX UsedRegisters fix from MarcZX10's first review: the third mutation above reproduces the original out-of-range index as a clean CodegenException. The open merge-order coordination with #7173 and #7531 I leave to that thread.

Not verified

  • Execution under Rosetta or on any macOS host; I ran only on native x86-64 Linux.
  • Relinking or running PPSA21402 or any other title.
  • Fault delivery inside the spill sequence, and the signal-window concern above, which I reasoned about but did not reproduce.
  • The full test suite; only the six relinker tests above.

To get this to a good state

  1. Remove MULX from the "not lowered" lists in docs/dev/debt/relinker.md:55-56 and from the PR description.
  2. Optionally add MULX to the summary lists on lines 48 and 49, and move the Balatro sentence out of line 56 to bring the entry under 1000 characters.
  3. Optionally replace the lea rsp + mov rsp, [rsp-136] pair with a single mov rsp, [rsp+24] for the RSP destination.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5697993315 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10983#pullrequestreview-5697993315). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T12:54:09Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The SHLX/SHRX/SARX/RORX lowering merges cleanly, builds, passes its tests, and the tests fail under four mutations. One blocking item remains: the new debt entry says MULX is not lowered, but MULX lowering (#12255) is already in this branch's base. ## What I ran - Head: `25714e1ca83e81848e23ad8a31c72acc4d02da94` (stacked on #10970 at `3e29bdc6506d85d8d2875a344352646a504beced`) - `main`: `aab650b4d39785f5af1402612d419bc52b839c07`; the merge with the head was clean. - Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release, Ninja, `BUILD_TESTING=ON`, in a clean worktree. ```sh git fetch -q origin main pull/10983/head:refs/review/10983 git checkout -q --detach origin/main git merge -q --no-ff --no-edit refs/review/10983 git diff --check origin/main...refs/review/10983 python3 tools/check_conventions.py --base origin/main nice -n 10 cmake --build build -j10 --target shift_lowering_tests bls_lowering_tests andn_lowering_tests mulx_lowering_tests amd64_only_converter_tests relinker nice -n 10 ctest --test-dir build -R '^(shift_lowering|bls_lowering|andn_lowering|mulx_lowering|amd64_only_converter|to_rosetta)$' --output-on-failure -j4 ``` ## Results | Check | Result | |---|---| | Merge onto `main` | Clean | | `git diff --check` | Clean | | `check_conventions.py` | 0 errors, 2 warnings (`debt-length` on `relinker.md:48`, which predates this PR, and on the new entry at `relinker.md:56`) | | Build of the affected targets | OK | | `shift_lowering`, `bls_lowering`, `andn_lowering`, `mulx_lowering`, `amd64_only_converter`, `to_rosetta` | 6/6 pass; `shift_lowering` takes 0.09 s with the execution checks on | <details><summary>ctest</summary> ``` 1/6 Test #2690: andn_lowering .................... Passed 0.00 sec 2/6 Test #2691: bls_lowering ..................... Passed 0.01 sec 3/6 Test #2722: amd64_only_converter ............. Passed 0.01 sec 4/6 Test #2882: to_rosetta ....................... Passed 0.07 sec 5/6 Test #2694: mulx_lowering .................... Passed 0.08 sec 6/6 Test #2692: shift_lowering ................... Passed 0.09 sec 100% tests passed out of 6 ``` </details> <details><summary>check_conventions.py</summary> ``` docs/dev/debt/relinker.md:48: warning: [debt-length] Technical debt entry over 1000 characters: ... 2075 characters docs/dev/debt/relinker.md:56: warning: [debt-length] Technical debt entry over 1000 characters: ... 1123 characters 2 findings, 0 errors ``` </details> ## Negative control The production code here is all new, so restoring it from `main` would only break the build. I applied four separate mutations to the production files instead, rebuilt `shift_lowering_tests` after each one, and restored the file before the next: | Mutation | Test result | |---|---| | Removed the `DecodeShift` hooks from `_matchRosetta` and `_emitOutOfLine` | Fails: `Rosetta mode did not require a trampoline for SHLX` | | Replaced `pushfq` / `pop [rsp+16]` / `push [rsp+16]` / `popfq` with non-flag moves | Fails: `SHLX did not preserve the flags` | | Restored the pre-`25714e1ca8` RORX register set (immediate passed to `UsedRegisters`) | Fails: `register index 16 is out of range` | | SAR `/7` changed to SHR `/5` | Fails: `SARX 32 dst 0 src 6 count 2: register 0 is 1073741824, wanted 3221225472` | After the files were restored, `shift_lowering` passed again. ## Findings ### Blocking - **The debt entries say MULX is not lowered, but this branch lowers it.** [`docs/dev/debt/relinker.md:55-56`](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/docs/dev/debt/relinker.md#L55-L56) both end with "BEXTR, BZHI, MULX, PDEP and PEXT are not lowered". The PR description says the same. MULX lowering landed in #12255, and that merge (`4004c97114`) is an ancestor of this branch's base `5454cd2e45`. At this head the matcher already dispatches it ([`Amd64OnlyInstructionMatcher.cpp:287`](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp#L287-L288)), and [`relinker.md:5`](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/docs/dev/debt/relinker.md#L5) has its own entry. Line 56 is new in this PR, and line 55 is the BLS entry this PR rewrites. Both now give a wrong list of remaining gaps. Fix: drop MULX from both lists ("BEXTR, BZHI, PDEP and PEXT are not lowered"), and from the description. ### Non-blocking - **The two summary entries this PR edits still leave MULX out of the lowered set.** [`relinker.md:48`](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/docs/dev/debt/relinker.md#L48) reads "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX and RORX are lowered, but … the other BMI instructions still depend on the host". [Line 49](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/docs/dev/debt/relinker.md#L49) reads "other than ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX, TZCNT and LZCNT". This gap comes from #12255, not from this PR. Since both lines are already rewritten here, adding MULX would make them match the code. - **The new debt entry is over the length limit, and part of it is a title note.** `relinker.md:56` is 1123 characters, so `check_conventions.py` warns on it. The description mentions only the `relinker.md:38` warning. The sentence "Balatro (PPSA21402, #1142) stopped on `shlxq` after the ANDN lowering; this lowering has not been run on that title." is a note about one title, which belongs in the PR, not the debt file. Removing it leaves 1006 characters, so a little more trimming would clear the warning. - **When the destination is RSP, the result is read back from below the red zone after RSP is restored.** [`ShiftLowering.cpp:126-128`](https://github.com/boykopovar/AnyPS5/blob/25714e1ca83e81848e23ad8a31c72acc4d02da94/core/relinker/codegen/src/x86/ShiftLowering.cpp#L126-L128) emits `lea rsp, [rsp+160]` and then `mov rsp, [rsp-136]`. Between those two instructions the slot lies 8 bytes below the 128-byte red zone, so an asynchronous signal frame delivered there could overwrite it before it is read. One instruction, `mov rsp, [rsp+24]` (`mov esp, [rsp+24]` for the 32-bit form), produces the same final RSP with no such window. The ANDN and BLS stubs use the same two-instruction pattern (`AndnLowering.cpp:59-61`, `BlsLowering.cpp:129-131`), and an RSP destination for SHLX/RORX is unlikely in compiled code, so this need not block. It could be fixed here for the new stub or in a follow-up for all three. I checked the rest of the code against the Intel SDM encodings and found nothing wrong: - the `pp` mapping (66 = SHLX, F3 = SARX, F2 = SHRX, F2.0F3A F0 = RORX), with BEXTR at `pp=0` left unmatched; - the `/4` `/5` `/7` `/1` digits; - count masking that matches the scalar forms; - the source being loaded before RCX is overwritten (the test's memory forms use `[rdx+rcx*8]` with the count in RCX); - the RCX- and RSP-destination paths; - zero-extension of the 32-bit destination; - `pop m64` / `push m64` with an RSP base (the effective address is taken after or before the RSP update respectively). Regarding the earlier review, I confirm the RORX `UsedRegisters` fix from MarcZX10's first review: the third mutation above reproduces the original out-of-range index as a clean `CodegenException`. The open merge-order coordination with #7173 and #7531 I leave to that thread. ## Not verified - Execution under Rosetta or on any macOS host; I ran only on native x86-64 Linux. - Relinking or running PPSA21402 or any other title. - Fault delivery inside the spill sequence, and the signal-window concern above, which I reasoned about but did not reproduce. - The full test suite; only the six relinker tests above. ## To get this to a good state 1. Remove MULX from the "not lowered" lists in `docs/dev/debt/relinker.md:55-56` and from the PR description. 2. Optionally add MULX to the summary lists on lines 48 and 49, and move the Balatro sentence out of line 56 to bring the entry under 1000 characters. 3. Optionally replace the `lea rsp` + `mov rsp, [rsp-136]` pair with a single `mov rsp, [rsp+24]` for the RSP destination.
XairRrR commented 2026-10-29 13:34:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T13:34:06Z.

Review: APPROVED


Re-checked — approved

The new commit 4e207d1264 drops MULX from the "not lowered" lists and adds it to the two summary entries. It also shortens the shift debt entry to 993 characters with the title note removed, and restores an RSP destination with a single load. Every finding from my previous review is resolved, and the merged tree builds and passes the relinker tests.

What changed since my last review

  • The branch was rebased onto the new #10970 head 8dddfe4200, which adds the same RSP-epilogue change and MULX corrections to the BLS stub. git range-diff shows the three shift commits unchanged apart from the debt text, plus one new fix commit: 4e207d1264.
  • 4e207d1264 changes ShiftLowering.cpp (epilogue) and docs/dev/debt/relinker.md (lines 48, 49, 55, 56).
  • The description now reads "BEXTR, BZHI, PDEP and PEXT; MULX is lowered since #12255". It names only the relinker.md:48 warning, and the Tested section has runs on the stack top f5e1bbf4a5.

What I ran

  • Head: 4e207d12648ff3e06b2c42673e9d2022829d2248
  • Previously reviewed head: 25714e1ca83e81848e23ad8a31c72acc4d02da94
  • main: be2242b8c817895cf5ebc556b945d90e1c1198c7. The merge with the head was clean.
  • Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release, Ninja, BUILD_TESTING=ON, in a clean worktree.
git fetch -q origin main pull/10983/head:refs/review/10983
git range-diff 5454cd2e45..25714e1ca8 5454cd2e45..4e207d1264
git checkout -q --detach origin/main
git merge -q --no-ff --no-edit refs/review/10983
git diff --check origin/main...refs/review/10983
python3 tools/check_conventions.py --base origin/main
nice -n 10 cmake --build build -j10 --target shift_lowering_tests bls_lowering_tests andn_lowering_tests mulx_lowering_tests amd64_only_converter_tests relinker
nice -n 10 ctest --test-dir build -R '^(shift_lowering|bls_lowering|andn_lowering|mulx_lowering|amd64_only_converter|to_rosetta)$' --output-on-failure -j4
Check Result
Merge onto main Clean
git diff --check Clean
check_conventions.py 0 errors, 1 warning (debt-length on relinker.md:48, which main already has)
Build of the affected targets OK
shift_lowering, bls_lowering, andn_lowering, mulx_lowering, amd64_only_converter, to_rosetta 6/6 pass
ctest
1/6 Test #2694: andn_lowering ....................   Passed    0.00 sec
2/6 Test #2695: bls_lowering .....................   Passed    0.00 sec
3/6 Test #2726: amd64_only_converter .............   Passed    0.01 sec
4/6 Test #2887: to_rosetta .......................   Passed    0.05 sec
5/6 Test #2698: mulx_lowering ....................   Passed    0.08 sec
6/6 Test #2696: shift_lowering ...................   Passed    0.11 sec
100% tests passed out of 6

Mutations of the new epilogue: I rebuilt after each one and restored the file before the next.

Mutation Result
ShiftLowering.cpp:127 loads RSP from the flags slot instead of kStackResultSlot shift_lowering fails: SHLX 32 dst 4 src 6 count 2: register 4 is 2755, wanted 0
BlsLowering.cpp:130 loads RSP from slot 0 instead of 8 bls_lowering fails
ShiftLowering.cpp:126-129 reverted to the previous lea rsp + mov rsp, [rsp-136] pair shift_lowering passes, as expected: the two forms give the same final RSP, and the signal window cannot be observed in this test

After the restores, both tests passed again. This confirms the author's report that a wrong-slot mutation fails the lowering tests.

Previous findings

  • Blocking, MULX listed as not lowered: Resolved. relinker.md:55-56 now ends with "BEXTR, BZHI, PDEP and PEXT are not lowered" (the shift entry adds "(the pp=0 form of the same opcode)" after BEXTR). The description is corrected.
  • Summary entries leave out MULX: Resolved. Line 48 now reads "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX and MULX are lowered", and line 49 lists MULX among the exceptions.
  • Length and title note: Resolved. The shift entry is 993 characters with no Balatro sentence, and the only debt-length warning left is the one on line 48 that main already has.
  • RSP destination read below the red zone: Resolved. ShiftLowering.cpp:126-129 now emits a single mov rsp, [rsp+24] (mov esp for the 32-bit form, which zero-extends as the instruction requires) while the frame is still reserved, and skips the lea. For a stack destination, the result is written to slot 24 with the operand width at line 117 and read back with the same width, so the final RSP is unchanged. BlsLowering.cpp:129-132 gets the same change through #10970. Leaving ANDN on main for a separate change is reasonable.

New findings

Blocking

None.

Non-blocking

None.

Not verified

  • Execution under Rosetta or on macOS. The author reports Rosetta 495/495 and arm64 491/492 on the stack top f5e1bbf4a5; I accept these as author-reported.
  • Relinking or running PPSA21402 or any other title.
  • The merge-order coordination with #7173 and #7531, which is still under discussion with MarcZX10 on this PR. I leave it to that thread.
  • The full test suite. I ran only the six relinker tests above.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5698537753 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10983#pullrequestreview-5698537753). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T13:34:06Z. **Review:** APPROVED --- # Re-checked — approved The new commit `4e207d1264` drops MULX from the "not lowered" lists and adds it to the two summary entries. It also shortens the shift debt entry to 993 characters with the title note removed, and restores an RSP destination with a single load. Every finding from my previous review is resolved, and the merged tree builds and passes the relinker tests. ## What changed since my last review - The branch was rebased onto the new #10970 head `8dddfe4200`, which adds the same RSP-epilogue change and MULX corrections to the BLS stub. `git range-diff` shows the three shift commits unchanged apart from the debt text, plus one new fix commit: `4e207d1264`. - `4e207d1264` changes `ShiftLowering.cpp` (epilogue) and `docs/dev/debt/relinker.md` (lines 48, 49, 55, 56). - The description now reads "BEXTR, BZHI, PDEP and PEXT; MULX is lowered since #12255". It names only the `relinker.md:48` warning, and the Tested section has runs on the stack top `f5e1bbf4a5`. ## What I ran - Head: `4e207d12648ff3e06b2c42673e9d2022829d2248` - Previously reviewed head: `25714e1ca83e81848e23ad8a31c72acc4d02da94` - `main`: `be2242b8c817895cf5ebc556b945d90e1c1198c7`. The merge with the head was clean. - Arch Linux 7.2.11-zen, GCC 16.2.1, Intel Core i9-14900KF, Release, Ninja, `BUILD_TESTING=ON`, in a clean worktree. ```sh git fetch -q origin main pull/10983/head:refs/review/10983 git range-diff 5454cd2e45..25714e1ca8 5454cd2e45..4e207d1264 git checkout -q --detach origin/main git merge -q --no-ff --no-edit refs/review/10983 git diff --check origin/main...refs/review/10983 python3 tools/check_conventions.py --base origin/main nice -n 10 cmake --build build -j10 --target shift_lowering_tests bls_lowering_tests andn_lowering_tests mulx_lowering_tests amd64_only_converter_tests relinker nice -n 10 ctest --test-dir build -R '^(shift_lowering|bls_lowering|andn_lowering|mulx_lowering|amd64_only_converter|to_rosetta)$' --output-on-failure -j4 ``` | Check | Result | |---|---| | Merge onto `main` | Clean | | `git diff --check` | Clean | | `check_conventions.py` | 0 errors, 1 warning (`debt-length` on `relinker.md:48`, which `main` already has) | | Build of the affected targets | OK | | `shift_lowering`, `bls_lowering`, `andn_lowering`, `mulx_lowering`, `amd64_only_converter`, `to_rosetta` | 6/6 pass | <details><summary>ctest</summary> ``` 1/6 Test #2694: andn_lowering .................... Passed 0.00 sec 2/6 Test #2695: bls_lowering ..................... Passed 0.00 sec 3/6 Test #2726: amd64_only_converter ............. Passed 0.01 sec 4/6 Test #2887: to_rosetta ....................... Passed 0.05 sec 5/6 Test #2698: mulx_lowering .................... Passed 0.08 sec 6/6 Test #2696: shift_lowering ................... Passed 0.11 sec 100% tests passed out of 6 ``` </details> Mutations of the new epilogue: I rebuilt after each one and restored the file before the next. | Mutation | Result | |---|---| | `ShiftLowering.cpp:127` loads RSP from the flags slot instead of `kStackResultSlot` | `shift_lowering` fails: `SHLX 32 dst 4 src 6 count 2: register 4 is 2755, wanted 0` | | `BlsLowering.cpp:130` loads RSP from slot 0 instead of 8 | `bls_lowering` fails | | `ShiftLowering.cpp:126-129` reverted to the previous `lea rsp` + `mov rsp, [rsp-136]` pair | `shift_lowering` passes, as expected: the two forms give the same final RSP, and the signal window cannot be observed in this test | After the restores, both tests passed again. This confirms the author's report that a wrong-slot mutation fails the lowering tests. ## Previous findings - **Blocking, MULX listed as not lowered:** Resolved. [`relinker.md:55-56`](https://github.com/boykopovar/AnyPS5/blob/4e207d12648ff3e06b2c42673e9d2022829d2248/docs/dev/debt/relinker.md#L55-L56) now ends with "BEXTR, BZHI, PDEP and PEXT are not lowered" (the shift entry adds "(the `pp=0` form of the same opcode)" after BEXTR). The description is corrected. - **Summary entries leave out MULX:** Resolved. [Line 48](https://github.com/boykopovar/AnyPS5/blob/4e207d12648ff3e06b2c42673e9d2022829d2248/docs/dev/debt/relinker.md#L48-L49) now reads "ANDN, BLSI, BLSMSK, BLSR, SHLX, SHRX, SARX, RORX and MULX are lowered", and line 49 lists MULX among the exceptions. - **Length and title note:** Resolved. The shift entry is 993 characters with no Balatro sentence, and the only `debt-length` warning left is the one on line 48 that `main` already has. - **RSP destination read below the red zone:** Resolved. [`ShiftLowering.cpp:126-129`](https://github.com/boykopovar/AnyPS5/blob/4e207d12648ff3e06b2c42673e9d2022829d2248/core/relinker/codegen/src/x86/ShiftLowering.cpp#L126-L129) now emits a single `mov rsp, [rsp+24]` (`mov esp` for the 32-bit form, which zero-extends as the instruction requires) while the frame is still reserved, and skips the `lea`. For a stack destination, the result is written to slot 24 with the operand width at line 117 and read back with the same width, so the final RSP is unchanged. [`BlsLowering.cpp:129-132`](https://github.com/boykopovar/AnyPS5/blob/4e207d12648ff3e06b2c42673e9d2022829d2248/core/relinker/codegen/src/x86/BlsLowering.cpp#L129-L132) gets the same change through #10970. Leaving ANDN on `main` for a separate change is reasonable. ## New findings ### Blocking None. ### Non-blocking None. ## Not verified - Execution under Rosetta or on macOS. The author reports Rosetta 495/495 and arm64 491/492 on the stack top `f5e1bbf4a5`; I accept these as author-reported. - Relinking or running PPSA21402 or any other title. - The merge-order coordination with #7173 and #7531, which is still under discussion with MarcZX10 on this PR. I leave it to that thread. - The full test suite. I ran only the six relinker tests above.
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!10983
No description provided.