feat(relinker): lower BZHI and BEXTR under --to-rosetta #11107

Merged
AdnanKhan-ak47 merged 11 commits from upstream-pr/11107 into main 2026-11-02 10:26:58 +00:00
AdnanKhan-ak47 commented 2026-10-25 17:46:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T17:46:36Z.


What

--to-rosetta lowers BZHI and BEXTR (VEX.0F38 F5 and F7 with pp=0, both widths, register and memory sources, including RIP-relative and RSP operands), after ANDN, BLS (#10970) and the BMI2 shifts (#10983). Rosetta does not implement them, so a title reaching one stopped with an illegal instruction; docs/dev/debt/relinker.md listed both as not lowered.

core/relinker/codegen/src/x86/BitfieldLowering.cpp (DecodeBitfield, EmitBitfield, BitfieldName) uses the same ScalarLowering.hpp helpers and 160-byte frame as the shifts: RCX is spilled to carry the control operand, two scratch registers hold the result and the mask. The control's low byte is compared against the operand size, because the SDM's behaviour there is not a masked shift count: BZHI with an index at or past the operand size returns the source unchanged with CF set (test then stc), and BEXTR with a start at or past it returns zero (xor). On the normal path the mask is -1 << count inverted with not and applied with and, which also produces ZF and SF from the result and clears CF and OF; for BEXTR the mask is skipped when the length is at or past the operand size, and mov cl, ch moves the length into the count register. BEXTR's SF is undefined in the SDM and follows the result here. The pp=0 opcodes PDEP, PEXT and MULX, and the F7 shift forms, are left to their own decoders.

The matcher registers the decoder next to the shifts (both the trampoline and the out-of-line path), the three CMakeLists.txt source lists get the file and bitfield_lowering_tests is added. test_to_rosetta.py gains BZHI and BEXTR relinks (unchanged bytes except under --to-rosetta, report names present); docs/user/USAGE.md and docs/dev/debt/relinker.md record the lowering and remove both from the not-lowered lists.

BitfieldLoweringTests.cpp: decoding of all 2 × 2 × 16 × 16 × 16 register forms, a prefixed memory form, rejection of SHLX, PDEP, PEXT, MULX, BLSR, VEX.L=1, trailing bytes, a truncated ModRM and an overflowing stack displacement; matching in Intel and Rosetta modes; and, on an x86-64 host, execution of every destination against 9 source/control register pairs (overlaps, RSP, RCX), 6 values and 20 control patterns (index, start and length at 0, 1, 31, 32, 63, 64, 255, high garbage above the control byte) with the flags preloaded all set and all clear, plus six memory addressing forms. The expected result and flags come from a model of the SDM entries.

Tested

macOS 27 arm64, AppleClang: ctest --test-dir build-relinker -j8 257/257 (bitfield_lowering, to_rosetta included). Same tree built for x86_64 and run under Rosetta: ctest --test-dir build-rosetta -j8 257/257, with the execution checks enabled. Arch Linux x86-64 (Zen 2, GCC 16.2.1): ctest --test-dir build-relinker -j8 269/269, bitfield_lowering_tests natively with the execution checks enabled, no warnings from the new files.

The SDM model the test compares against was checked against the hardware instructions on the Zen 2 with a throwaway program: for both widths, 6 sources, the 20 control patterns and every value of the control's low byte, bzhi/bextr results and the defined flags (CF ZF SF OF for BZHI, CF ZF OF for BEXTR) matched the model on 122,880 cases, 0 mismatches. The stub itself was not compared with the hardware instruction in the same run; the debt entry says so. Not run against a title. tools/check_conventions.py --base origin/main 0 errors; tools/pr_overlap.py run before opening.

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: #10983 (stacked on it, which stacks on #10970); merge after #7173 and #7531 like those two (additive conflicts in the same three files, keep both sides; rebased when they land)
  • Fixes: none
  • Unblocks: none known; PPSA21402 #1142 is the title that reached the BMI2 shifts, and BZHI/BEXTR are the remaining BMI instructions a compiler emits for bitfield access
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5007131491 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11107). Originally posted by [AdnanKhan-ak47](https://github.com/AdnanKhan-ak47) on 2026-10-25T17:46:36Z. --- ### What `--to-rosetta` lowers BZHI and BEXTR (VEX.0F38 `F5` and `F7` with `pp=0`, both widths, register and memory sources, including RIP-relative and RSP operands), after ANDN, BLS (#10970) and the BMI2 shifts (#10983). Rosetta does not implement them, so a title reaching one stopped with an illegal instruction; `docs/dev/debt/relinker.md` listed both as not lowered. `core/relinker/codegen/src/x86/BitfieldLowering.cpp` (`DecodeBitfield`, `EmitBitfield`, `BitfieldName`) uses the same `ScalarLowering.hpp` helpers and 160-byte frame as the shifts: RCX is spilled to carry the control operand, two scratch registers hold the result and the mask. The control's low byte is compared against the operand size, because the SDM's behaviour there is not a masked shift count: BZHI with an index at or past the operand size returns the source unchanged with CF set (`test` then `stc`), and BEXTR with a start at or past it returns zero (`xor`). On the normal path the mask is `-1 << count` inverted with `not` and applied with `and`, which also produces ZF and SF from the result and clears CF and OF; for BEXTR the mask is skipped when the length is at or past the operand size, and `mov cl, ch` moves the length into the count register. BEXTR's SF is undefined in the SDM and follows the result here. The `pp=0` opcodes PDEP, PEXT and MULX, and the `F7` shift forms, are left to their own decoders. The matcher registers the decoder next to the shifts (both the trampoline and the out-of-line path), the three `CMakeLists.txt` source lists get the file and `bitfield_lowering_tests` is added. `test_to_rosetta.py` gains BZHI and BEXTR relinks (unchanged bytes except under `--to-rosetta`, report names present); `docs/user/USAGE.md` and `docs/dev/debt/relinker.md` record the lowering and remove both from the not-lowered lists. `BitfieldLoweringTests.cpp`: decoding of all 2 × 2 × 16 × 16 × 16 register forms, a prefixed memory form, rejection of SHLX, PDEP, PEXT, MULX, BLSR, VEX.L=1, trailing bytes, a truncated ModRM and an overflowing stack displacement; matching in Intel and Rosetta modes; and, on an x86-64 host, execution of every destination against 9 source/control register pairs (overlaps, RSP, RCX), 6 values and 20 control patterns (index, start and length at 0, 1, 31, 32, 63, 64, 255, high garbage above the control byte) with the flags preloaded all set and all clear, plus six memory addressing forms. The expected result and flags come from a model of the SDM entries. ### Tested macOS 27 arm64, AppleClang: `ctest --test-dir build-relinker -j8` 257/257 (`bitfield_lowering`, `to_rosetta` included). Same tree built for x86_64 and run under Rosetta: `ctest --test-dir build-rosetta -j8` 257/257, with the execution checks enabled. Arch Linux x86-64 (Zen 2, GCC 16.2.1): `ctest --test-dir build-relinker -j8` 269/269, `bitfield_lowering_tests` natively with the execution checks enabled, no warnings from the new files. The SDM model the test compares against was checked against the hardware instructions on the Zen 2 with a throwaway program: for both widths, 6 sources, the 20 control patterns and every value of the control's low byte, `bzhi`/`bextr` results and the defined flags (`CF ZF SF OF` for BZHI, `CF ZF OF` for BEXTR) matched the model on 122,880 cases, 0 mismatches. The stub itself was not compared with the hardware instruction in the same run; the debt entry says so. Not run against a title. `tools/check_conventions.py --base origin/main` 0 errors; `tools/pr_overlap.py` run before opening. ### 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: #10983 (stacked on it, which stacks on #10970); merge after #7173 and #7531 like those two (additive conflicts in the same three files, keep both sides; rebased when they land) - [x] Fixes: none - [x] Unblocks: none known; PPSA21402 #1142 is the title that reached the BMI2 shifts, and BZHI/BEXTR are the remaining BMI instructions a compiler emits for bitfield access - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 18:34:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T18:34:33Z.

Review: APPROVED


Reviewed exact head . The BZHI/BEXTR decoder checks VEX map/prefix/length and operand extent, and the lowering preserves register and stack operands while handling out-of-range index/start and length cases. The execution regression covers 32/64-bit forms, register overlap, memory addressing, flags where defined, and red-zone preservation. I ran the focused local suite (, , , ): 4/4 passed with execution checks enabled. Hosted relinker jobs for Linux, Windows, macOS arm64/x86-64 and ASan are green; product Linux/Windows/register-context/write-watch jobs are marked skipped, so this approval is based on the relinker coverage shown.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644669178 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11107#pullrequestreview-5644669178). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T18:34:33Z. **Review:** APPROVED --- Reviewed exact head . The BZHI/BEXTR decoder checks VEX map/prefix/length and operand extent, and the lowering preserves register and stack operands while handling out-of-range index/start and length cases. The execution regression covers 32/64-bit forms, register overlap, memory addressing, flags where defined, and red-zone preservation. I ran the focused local suite (, , , ): 4/4 passed with execution checks enabled. Hosted relinker jobs for Linux, Windows, macOS arm64/x86-64 and ASan are green; product Linux/Windows/register-context/write-watch jobs are marked skipped, so this approval is based on the relinker coverage shown.
MarcZX10 commented 2026-10-29 08:47:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:47:54Z.

Review: APPROVED


Re-reviewed exact head 313892fa5e7b6e78afa1d1280a1fabf6ae11b797. The author reports the lowering implementation is byte-identical to the previously reviewed head; the new delta is the debt entry updated to include BZHI/BEXTR and preserve the stack order through #10970/#10983. The current-main merge preview is clean and main has no newer changes in the PR’s paths. The author reports Rosetta execution tests 487/487 and arm64 484/485, with the sole failure progress_report also failing on main; Linux, Windows, and ASan hosted relinker jobs pass. The hosted macOS arm64 and x86_64 jobs remain queued. I did not rerun the suite locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5695007915 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11107#pullrequestreview-5695007915). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T08:47:54Z. **Review:** APPROVED --- Re-reviewed exact head `313892fa5e7b6e78afa1d1280a1fabf6ae11b797`. The author reports the lowering implementation is byte-identical to the previously reviewed head; the new delta is the debt entry updated to include BZHI/BEXTR and preserve the stack order through #10970/#10983. The current-main merge preview is clean and main has no newer changes in the PR’s paths. The author reports Rosetta execution tests 487/487 and arm64 484/485, with the sole failure `progress_report` also failing on main; Linux, Windows, and ASan hosted relinker jobs pass. The hosted macOS arm64 and x86_64 jobs remain queued. I did not rerun the suite locally.
MarcZX10 commented 2026-10-30 00:54:25 +00:00 (Migrated from github.com)

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

Review: APPROVED


Re-checked — new head, looks good

The requested stack-result epilogue now loads the computed RSP destination from its saved frame slot before leaving the frame, and the updated debt text accurately limits what the tests establish.

What I ran

Reviewed exact head f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e against current main f6fddd60d826831f70f7c62e25423610ee742bcc; the merge preview was clean (dada4b5f129f367a197cf188a48c15f2391a0fdb). Environment: Linux; I did not build or run the tests locally.

git merge-tree --write-tree upstream/main refs/remotes/upstream/pr-11107-latest
git diff --check upstream/main...refs/remotes/upstream/pr-11107-latest
python tools/check_conventions.py --base upstream/main --head refs/remotes/upstream/pr-11107-latest
gh api repos/boykopovar/AnyPS5/commits/f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e/check-runs

Results

Check Result
Current-main merge preview Clean.
Diff check Pass.
Hosted relinker checks Linux, Windows, macOS arm64, macOS x86_64, and ASan pass on this head; product Linux/Windows jobs are skipped.
Hosted conventions, labels, eligibility Pass on this head.
Local conventions Zero errors; one warning on the existing long --to-rosetta debt paragraph, 2,094 characters at this head (2,023 on current main). The separate BZHI/BEXTR entry is 997 characters.
Focused execution suites The author reports Linux x86-64 506/506, Rosetta 495/495, and macOS arm64 491/492 on this head; progress_report is the arm64 failure and is reported to fail on main too. I did not rerun these suites.
Unedited local check output
diff-check exit: 0
docs/dev/debt/relinker.md:48: warning: [debt-length] Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 2094 characters
1 findings, 0 errors; GitHub shows at most 10 error annotations, the job summary lists all
conventions exit: 0

Negative control

Assertion Mutation reported by author Result
RSP-destination lowering restores the computed value Used the wrong stack-result slot in the epilogue The author reports bitfield_lowering fails; I did not independently run this mutation.

Findings

Blocking

None found in the requested epilogue and debt-entry update.

Non-blocking

The convention warning is on the existing combined --to-rosetta paragraph, which is 2,023 characters on current main; this PR adds 71 characters to it. The new BZHI/BEXTR entry itself is below 1,000 characters.

Sources

Not verified

No local build, execution suite, or title run was performed. The hardware model comparison and cross-platform test results are author-reported; the hosted relinker matrix passed on the exact reviewed head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5706853182 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11107#pullrequestreview-5706853182). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T00:54:25Z. **Review:** APPROVED --- # Re-checked — new head, looks good The requested stack-result epilogue now loads the computed RSP destination from its saved frame slot before leaving the frame, and the updated debt text accurately limits what the tests establish. ## What I ran Reviewed exact head `f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e` against current `main` `f6fddd60d826831f70f7c62e25423610ee742bcc`; the merge preview was clean (`dada4b5f129f367a197cf188a48c15f2391a0fdb`). Environment: Linux; I did not build or run the tests locally. ```sh git merge-tree --write-tree upstream/main refs/remotes/upstream/pr-11107-latest git diff --check upstream/main...refs/remotes/upstream/pr-11107-latest python tools/check_conventions.py --base upstream/main --head refs/remotes/upstream/pr-11107-latest gh api repos/boykopovar/AnyPS5/commits/f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e/check-runs ``` ## Results | Check | Result | |---|---| | Current-main merge preview | Clean. | | Diff check | Pass. | | Hosted relinker checks | Linux, Windows, macOS arm64, macOS x86_64, and ASan pass on this head; product Linux/Windows jobs are skipped. | | Hosted conventions, labels, eligibility | Pass on this head. | | Local conventions | Zero errors; one warning on the existing long `--to-rosetta` debt paragraph, 2,094 characters at this head (2,023 on current main). The separate BZHI/BEXTR entry is 997 characters. | | Focused execution suites | The author reports Linux x86-64 506/506, Rosetta 495/495, and macOS arm64 491/492 on this head; `progress_report` is the arm64 failure and is reported to fail on main too. I did not rerun these suites. | <details> <summary>Unedited local check output</summary> ```text diff-check exit: 0 docs/dev/debt/relinker.md:48: warning: [debt-length] Technical debt entry over 1000 characters: state the gap first (what differs from the console, what is unverified, what throws) and keep only the behaviour it needs: 2094 characters 1 findings, 0 errors; GitHub shows at most 10 error annotations, the job summary lists all conventions exit: 0 ``` </details> ## Negative control | Assertion | Mutation reported by author | Result | |---|---|---| | RSP-destination lowering restores the computed value | Used the wrong stack-result slot in the epilogue | The author reports `bitfield_lowering` fails; I did not independently run this mutation. | ## Findings ### Blocking None found in the requested epilogue and debt-entry update. ### Non-blocking The convention warning is on the existing combined `--to-rosetta` paragraph, which is 2,023 characters on current `main`; this PR adds 71 characters to it. The new BZHI/BEXTR entry itself is below 1,000 characters. ## Sources - [RSP-result epilogue at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e/core/relinker/codegen/src/x86/BitfieldLowering.cpp#L151-L157) - [BZHI/BEXTR debt entry at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/f5e1bbf4a52ea48ec79adb08c01bb9eae7860c5e/docs/dev/debt/relinker.md#L57) - [Author's exact-head mutation and test report](https://github.com/boykopovar/AnyPS5/pull/11107#issuecomment-6497887930) ## Not verified No local build, execution suite, or title run was performed. The hardware model comparison and cross-platform test results are author-reported; the hosted relinker matrix passed on the exact reviewed head.
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!11107
No description provided.