feat(relinker): lower BLSI, BLSMSK and BLSR under --to-rosetta #10970

Merged
AdnanKhan-ak47 merged 5 commits from upstream-pr/10970 into main 2026-11-02 10:26:21 +00:00
AdnanKhan-ak47 commented 2026-10-25 11:38:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T11:38:41Z.


What

--to-rosetta lowers the three single-source BMI1 instructions that docs/dev/debt/relinker.md lists as faulting with SIGILL under Rosetta on an M2 with macOS 14.8.9: BLSR (x & (x - 1)), BLSMSK (x ^ (x - 1)) and BLSI (x & -x), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN uses (BlsLowering.cpp; VEX.0F38 F3 /1, /2, /3).

The stub spills two scratch registers below the 128-byte red zone, loads the source, and tests it. The nonzero branch computes the result with LEA and AND, XOR, or MOV/NEG/AND, so SF and ZF come from the result and CF and OF clear; the zero branch produces the SDM result directly (0, all ones, 0). STC on the branch whose CF the SDM defines as set gives CF for a zero source (BLSR, BLSMSK) or a nonzero source (BLSI); BLSMSK's ZF clears as the SDM states. RSP as source or destination and RIP-relative and stack-relative memory operands follow ANDN, including the throw for a stack displacement that no longer fits after the spill frame.

ScalarLowering.hpp holds the encoding, scratch and memory-operand helpers ANDN had in its own file, so both lowerings share them; AndnLowering.cpp produces the same bytes as before. tests/LoweringExecution.hpp holds the ANDN execution harness, generalised over the emitted stub, and BlsLoweringTests.cpp runs decoding for every register pair and width, the memory-operand forms, the rejections, Intel versus Rosetta matching for all three names, and on an x86-64 host executes 3 operations × 2 widths × 16 destinations × 4 sources (including RSP and destination = source) × 8 values plus 6 memory forms × 3 values, checking the result, every other register, CF/ZF/SF/OF and the red zone. The harness caught a first version that stored the RSP-destination result in the red zone.

docs/user/USAGE.md lists the lowering; docs/dev/debt/relinker.md records it and the BMI1/BMI2 instructions still not lowered.

Tested

macOS 15 arm64 relinker-only build, AppleClang 21: ctest 255/255 (decoding and matching; the execution checks are compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build: andn_lowering and bls_lowering pass with the execution checks on the host CPU. tools/check_conventions.py --base origin/main and tools/pr_overlap.py clean. Not run on a Rosetta host or against a title.

Current head 4ff00abf28 (rebased on main f7f03daf52), on an Apple M4 with macOS 27.2: the x86_64 relinker build run under Rosetta passes 514/514, with bls_lowering executing every lowered BLSI, BLSMSK and BLSR stub under Rosetta and comparing results, flags and untouched registers with the SDM model. Negative control: making the BLSMSK stub use OR instead of XOR fails bls_lowering there. to_rosetta checks that --to-rosetta output no longer contains the three encodings. The SIGILL itself is not reproduced: Rosetta on this M4 executes BLSI, BLSMSK and BLSR natively, as on the M5 Max in the debt entry, and no host with the faulting Rosetta (M2, macOS 14.8.9) was available. Not run against a title.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004384923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970). Originally posted by [AdnanKhan-ak47](https://github.com/AdnanKhan-ak47) on 2026-10-25T11:38:41Z. --- ### What `--to-rosetta` lowers the three single-source BMI1 instructions that `docs/dev/debt/relinker.md` lists as faulting with SIGILL under Rosetta on an M2 with macOS 14.8.9: BLSR (`x & (x - 1)`), BLSMSK (`x ^ (x - 1)`) and BLSI (`x & -x`), in their 32- and 64-bit register and memory forms, through the same out-of-line trampoline ANDN uses (`BlsLowering.cpp`; `VEX.0F38 F3 /1, /2, /3`). The stub spills two scratch registers below the 128-byte red zone, loads the source, and tests it. The nonzero branch computes the result with LEA and AND, XOR, or MOV/NEG/AND, so SF and ZF come from the result and CF and OF clear; the zero branch produces the SDM result directly (0, all ones, 0). STC on the branch whose CF the SDM defines as set gives CF for a zero source (BLSR, BLSMSK) or a nonzero source (BLSI); BLSMSK's ZF clears as the SDM states. RSP as source or destination and RIP-relative and stack-relative memory operands follow ANDN, including the throw for a stack displacement that no longer fits after the spill frame. `ScalarLowering.hpp` holds the encoding, scratch and memory-operand helpers ANDN had in its own file, so both lowerings share them; `AndnLowering.cpp` produces the same bytes as before. `tests/LoweringExecution.hpp` holds the ANDN execution harness, generalised over the emitted stub, and `BlsLoweringTests.cpp` runs decoding for every register pair and width, the memory-operand forms, the rejections, Intel versus Rosetta matching for all three names, and on an x86-64 host executes 3 operations × 2 widths × 16 destinations × 4 sources (including RSP and destination = source) × 8 values plus 6 memory forms × 3 values, checking the result, every other register, CF/ZF/SF/OF and the red zone. The harness caught a first version that stored the RSP-destination result in the red zone. `docs/user/USAGE.md` lists the lowering; `docs/dev/debt/relinker.md` records it and the BMI1/BMI2 instructions still not lowered. ### Tested macOS 15 arm64 relinker-only build, AppleClang 21: `ctest` 255/255 (decoding and matching; the execution checks are compiled out off x86-64). Arch Linux x86-64, GCC 16.2.1, relinker-only build: `andn_lowering` and `bls_lowering` pass with the execution checks on the host CPU. `tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py` clean. Not run on a Rosetta host or against a title. Current head `4ff00abf28` (rebased on `main` `f7f03daf52`), on an Apple M4 with macOS 27.2: the x86_64 relinker build run under Rosetta passes 514/514, with `bls_lowering` executing every lowered BLSI, BLSMSK and BLSR stub under Rosetta and comparing results, flags and untouched registers with the SDM model. Negative control: making the BLSMSK stub use OR instead of XOR fails `bls_lowering` there. `to_rosetta` checks that `--to-rosetta` output no longer contains the three encodings. The SIGILL itself is not reproduced: Rosetta on this M4 executes BLSI, BLSMSK and BLSR natively, as on the M5 Max in the debt entry, and no host with the faulting Rosetta (M2, macOS 14.8.9) was available. Not run against a title. ### 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
MarcZX10 commented 2026-10-25 11:48:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:48:24Z.

Review: APPROVED


Reviewed the VEX decoding, operand and prefix handling, scratch-register preservation, stack/RIP-relative memory rewriting, result values, and the defined CF/ZF/SF behavior for BLSI, BLSMSK, and BLSR. The regression suite exercises all register encodings and widths, aliasing, stack and memory operands, malformed encodings, flag results, and red-zone preservation. GitHub relinker checks passed on Ubuntu, macOS (including x86_64), Windows, and ASan. I did not run the suite locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643063816 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970#pullrequestreview-5643063816). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:48:24Z. **Review:** APPROVED --- Reviewed the VEX decoding, operand and prefix handling, scratch-register preservation, stack/RIP-relative memory rewriting, result values, and the defined CF/ZF/SF behavior for BLSI, BLSMSK, and BLSR. The regression suite exercises all register encodings and widths, aliasing, stack and memory operands, malformed encodings, flag results, and red-zone preservation. GitHub relinker checks passed on Ubuntu, macOS (including x86_64), Windows, and ASan. I did not run the suite locally.
ahmadmuj commented 2026-10-25 12:40:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T12:40:33Z.

Review: APPROVED


Checked head 1c6d7e4 merged onto main 29ce6d8 (clean merge), Linux x86-64 WSL2, GCC 13.3, relinker-only build.

What I ran:

  • bls_lowering, andn_lowering, amd64_only_converter pass; full relinker ctest 267/267 on the merge, 266/266 on main (the only diff is the new bls_lowering). andn_lowering passes on main too, so the ScalarLowering move kept ANDN green.
  • The test in the PR compares the stubs against a C model, so I also compared them against the real instructions. A throwaway probe (not in the repo) runs each site twice through your LoweringExecution::Execution harness: once as the original BLSR/BLSMSK/BLSI bytes on the host CPU (BMI1), once as EmitBls output. It compares all 16 registers, CF/ZF/SF/OF, and the red-zone sentinels.
    • Register forms: 3 ops x 32/64 x every dst 0..15 x every src 0..15, including rsp as src and as dst.
    • Memory forms: your 6 tails plus [rsp+d8], [rsp+rcx*8], [rsp+r12], [r12+d32], [r13+0], [r9+r10*8], no-base SIB with VEX.B 0/1, RIP with VEX.B=1, cs:, fs:, and two addr32 forms with garbage in the upper halves of the registers.
    • Values: 0, 1, 2^k, 2^k-1, ~2^k for every k, the 32/64 boundary values, and random values (10k per op/width on one register pair). Initial RFLAGS seeded 5 ways, so a flag that leaked through would show.
    • 4,948,320 cases, 0 mismatches. Negative controls (wrong op, a trailing cmc, an r11 clobber, a red-zone write) were each caught on every case, so the probe can see these failures.
  • tools/check_conventions.py --base origin/main: 0 errors, 1 warning (debt-length on relinker.md:38, which was already over the limit on main).
  • Read the decode against the SDM VEX layout (inverted R/X/B, vvvv as destination, W, L/pp throw) and both matcher paths (_matchRosetta, _emitOutOfLine); the Intel target is untouched.

Not checked: a Rosetta host or a title. AF/PF are not compared (undefined per the SDM).

Two small non-blocking things, take or leave:

  1. core/relinker/relinker/tests/test_to_rosetta.py:57 relinks ANDN through the CLI and checks that "ANDN" shows up in the report. A BLS case there, e.g. c4e2f8f3ce (blsr rax, rsi) + c3, would cover the converter/report path the unit tests don't.
  2. The new debt entry for BLS leaves out two caveats that the ANDN entry states and that apply here too: a stack-relative displacement that no longer fits disp32 after the 144-byte frame throws (your test checks this), and precise fault delivery inside the spill sequence is unverified.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643235646 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970#pullrequestreview-5643235646). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T12:40:33Z. **Review:** APPROVED --- Checked head 1c6d7e4 merged onto main 29ce6d8 (clean merge), Linux x86-64 WSL2, GCC 13.3, relinker-only build. What I ran: - `bls_lowering`, `andn_lowering`, `amd64_only_converter` pass; full relinker ctest 267/267 on the merge, 266/266 on main (the only diff is the new `bls_lowering`). `andn_lowering` passes on main too, so the ScalarLowering move kept ANDN green. - The test in the PR compares the stubs against a C model, so I also compared them against the real instructions. A throwaway probe (not in the repo) runs each site twice through your `LoweringExecution::Execution` harness: once as the original BLSR/BLSMSK/BLSI bytes on the host CPU (BMI1), once as `EmitBls` output. It compares all 16 registers, CF/ZF/SF/OF, and the red-zone sentinels. - Register forms: 3 ops x 32/64 x every dst 0..15 x every src 0..15, including rsp as src and as dst. - Memory forms: your 6 tails plus `[rsp+d8]`, `[rsp+rcx*8]`, `[rsp+r12]`, `[r12+d32]`, `[r13+0]`, `[r9+r10*8]`, no-base SIB with VEX.B 0/1, RIP with VEX.B=1, `cs:`, `fs:`, and two `addr32` forms with garbage in the upper halves of the registers. - Values: 0, 1, 2^k, 2^k-1, ~2^k for every k, the 32/64 boundary values, and random values (10k per op/width on one register pair). Initial RFLAGS seeded 5 ways, so a flag that leaked through would show. - 4,948,320 cases, 0 mismatches. Negative controls (wrong op, a trailing `cmc`, an r11 clobber, a red-zone write) were each caught on every case, so the probe can see these failures. - `tools/check_conventions.py --base origin/main`: 0 errors, 1 warning (debt-length on relinker.md:38, which was already over the limit on main). - Read the decode against the SDM VEX layout (inverted R/X/B, vvvv as destination, W, L/pp throw) and both matcher paths (`_matchRosetta`, `_emitOutOfLine`); the Intel target is untouched. Not checked: a Rosetta host or a title. AF/PF are not compared (undefined per the SDM). Two small non-blocking things, take or leave: 1. `core/relinker/relinker/tests/test_to_rosetta.py:57` relinks ANDN through the CLI and checks that "ANDN" shows up in the report. A BLS case there, e.g. `c4e2f8f3ce` (blsr rax, rsi) + `c3`, would cover the converter/report path the unit tests don't. 2. The new debt entry for BLS leaves out two caveats that the ANDN entry states and that apply here too: a stack-relative displacement that no longer fits disp32 after the 144-byte frame throws (your test checks this), and precise fault delivery inside the spill sequence is unverified.
MarcZX10 commented 2026-10-25 14:29:27 +00:00 (Migrated from github.com)

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

Review: APPROVED


Re-reviewed the latest head after the CLI/report test and debt clarification were added. The three encodings in test_to_rosetta.py match BLSR (/1), BLSMSK (/2) and BLSI (/3), and the test checks both Linux/macOS target selection and the three conversion modes, requiring unchanged instruction bytes except under --to-rosetta and a matching report name when lowered. The debt note now records the same disp32-overflow and fault-delivery limits as the ANDN lowering.

I configured a relinker-only Release build with GCC 12.2, built relinker, and ran ctest -R to_rosetta: 1/1 passed. git diff --check and tools/check_conventions.py pass. The hosted Ubuntu, macOS arm64/x86_64, Windows and ASan relinker jobs also pass. I found no remaining blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643669536 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970#pullrequestreview-5643669536). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T14:29:27Z. **Review:** APPROVED --- Re-reviewed the latest head after the CLI/report test and debt clarification were added. The three encodings in `test_to_rosetta.py` match BLSR (`/1`), BLSMSK (`/2`) and BLSI (`/3`), and the test checks both Linux/macOS target selection and the three conversion modes, requiring unchanged instruction bytes except under `--to-rosetta` and a matching report name when lowered. The debt note now records the same disp32-overflow and fault-delivery limits as the ANDN lowering. I configured a relinker-only Release build with GCC 12.2, built `relinker`, and ran `ctest -R to_rosetta`: 1/1 passed. `git diff --check` and `tools/check_conventions.py` pass. The hosted Ubuntu, macOS arm64/x86_64, Windows and ASan relinker jobs also pass. I found no remaining blocking issue.
MarcZX10 commented 2026-10-29 09:25:56 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Re-review — changes requested for current-main integration

The exact-head report on 8608ef692c addresses the platform test update: Rosetta reports 487/487 with execution checks enabled, and arm64 reports 484/485 with only progress_report failing for the same Python-version reason on main. I found no new source-level issue in the scoped BLSI/BLSMSK/BLSR lowering from this update; I did not rerun the relinker suite locally.

The branch is based on main 4c7d275bef, while current upstream main is aaccd0ba39b7. Please refresh this base and rerun the focused lowering tests and conventions on the exact new head. #10983 is stacked through this PR, so update that branch after this base is settled and preserve both debt entries and test registrations across the stack.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695456252 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970#pullrequestreview-5695456252). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T09:25:56Z. **Review:** CHANGES_REQUESTED --- # Re-review — changes requested for current-main integration The exact-head report on `8608ef692c` addresses the platform test update: Rosetta reports 487/487 with execution checks enabled, and arm64 reports 484/485 with only `progress_report` failing for the same Python-version reason on main. I found no new source-level issue in the scoped BLSI/BLSMSK/BLSR lowering from this update; I did not rerun the relinker suite locally. The branch is based on main `4c7d275bef`, while current upstream main is `aaccd0ba39b7`. Please refresh this base and rerun the focused lowering tests and conventions on the exact new head. #10983 is stacked through this PR, so update that branch after this base is settled and preserve both debt entries and test registrations across the stack.
CnRJay commented 2026-11-01 19:48:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:48:40Z.

Review: COMMENTED


The decoding and execution tests are solid, but the PR says "Not run on a Rosetta host or against a title". The debt entry this removes states the instructions fault with SIGILL under Rosetta, so the claim that the lowering fixes that is unconfirmed here. A run of the lowered output on a Rosetta host would settle it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729919740 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10970#pullrequestreview-5729919740). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:48:40Z. **Review:** COMMENTED --- The decoding and execution tests are solid, but the PR says "Not run on a Rosetta host or against a title". The debt entry this removes states the instructions fault with SIGILL under Rosetta, so the claim that the lowering fixes that is unconfirmed here. A run of the lowered output on a Rosetta host would settle it.
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!10970
No description provided.