feat(relinker): add --to-avx to lower TZCNT, LZCNT and MOVBE for hosts without BMI #17084

Merged
ycbuzi merged 2 commits from upstream-pr/17084 into main 2026-11-12 23:17:44 +00:00
ycbuzi commented 2026-11-08 09:48:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-11-08T09:48:20Z.


What

Adds --to-avx, a relinker target for hosts that have AVX but not AVX2, BMI1, BMI2, LZCNT or MOVBE, such as the Sandy Bridge and Ivy Bridge CPUs of 2011 and 2012 Macs. It applies the BMI1 and BMI2 lowerings that --to-rosetta already has (ANDN, BLSI/BLSMSK/BLSR, SHLX/SHRX/SARX/RORX, BZHI/BEXTR, MULX) plus CLWB and RDPID, which Sandy Bridge also lacks, and adds two more:

  • TZCNT and LZCNT become BSF or BSR with CMOVZ for a zero source. CF is set for a zero source and ZF for a zero result. 16-bit forms keep bits 63:16 of the destination. Without this, a CPU that lacks LZCNT and BMI1 runs these encodings as BSR and BSF, and gives wrong counts without faulting.
  • MOVBE becomes MOV and BSWAP. The 16-bit form takes the upper half through a stack slot, so RFLAGS is never written.

It leaves out the Rosetta-only workarounds: 256-bit store splits, masked stores, scalar compares, RDTSCP, XSAVEOPT and RDSEED. A Sandy Bridge runs the first five natively, and it has no RDRAND for the RDSEED rewrite to target.

Both new stubs use relocatable-window placement. A 4-byte tzcnt eax, ecx followed by a jb in Celeste Classic's eboot failed with "too short for a jump" until the window could take the branch.

--to-avx requires --macos, because only the macOS runtime starts without the host CPU check. Not lowered yet, and listed in docs/dev/debt/relinker.md:

  • AVX2, F16C, FMA, PDEP and PEXT (#12678 adds opt-in PDEP/PEXT for Rosetta).
  • Letting the Linux and Windows runtimes accept a lowered conversion.

Tested

  • macOS 13.7.8 on a MacBook Pro 8,1 (i5-2415M, AVX but no AVX2/BMI/LZCNT/MOVBE), LLVM 19.1.7, relinker-only:
    • count_lowering, movbe_lowering, cli_args, andn_lowering, bls_lowering, shift_lowering, bitfield_lowering and mulx_lowering pass. The two new execution tests run their stubs on a CPU that lacks the instructions and compare result and flags against an SDM model.
    • amd64_only_converter stops with SIGILL on this host on main as well: it runs host AVX2 code, as #16236 describes.
  • Windows 11, MinGW GCC: amd64_only_converter (including the new converterAvxShortSites), count_lowering, movbe_lowering and cli_args pass. The execution parts are skipped on Windows.
  • Real code: converted Celeste Classic's eboot (homebrew) with --macos --to-avx --skip-sce-module. The --to-rosetta output still holds 1,674 TZCNT, 1,108 LZCNT and 111 MOVBE; the --to-avx output holds none of them, and at 31.4 MB it is smaller than the --to-rosetta output (31.7 MB) because it skips the store splits. The title does not run on this Mac yet, because it also uses AVX2.

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:5210329501 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17084). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-11-08T09:48:20Z. --- ### What Adds `--to-avx`, a relinker target for hosts that have AVX but not AVX2, BMI1, BMI2, LZCNT or MOVBE, such as the Sandy Bridge and Ivy Bridge CPUs of 2011 and 2012 Macs. It applies the BMI1 and BMI2 lowerings that `--to-rosetta` already has (ANDN, BLSI/BLSMSK/BLSR, SHLX/SHRX/SARX/RORX, BZHI/BEXTR, MULX) plus CLWB and RDPID, which Sandy Bridge also lacks, and adds two more: - **TZCNT and LZCNT** become BSF or BSR with CMOVZ for a zero source. CF is set for a zero source and ZF for a zero result. 16-bit forms keep bits 63:16 of the destination. Without this, a CPU that lacks LZCNT and BMI1 runs these encodings as BSR and BSF, and gives wrong counts without faulting. - **MOVBE** becomes MOV and BSWAP. The 16-bit form takes the upper half through a stack slot, so RFLAGS is never written. It leaves out the Rosetta-only workarounds: 256-bit store splits, masked stores, scalar compares, RDTSCP, XSAVEOPT and RDSEED. A Sandy Bridge runs the first five natively, and it has no RDRAND for the RDSEED rewrite to target. Both new stubs use relocatable-window placement. A 4-byte `tzcnt eax, ecx` followed by a `jb` in Celeste Classic's eboot failed with "too short for a jump" until the window could take the branch. `--to-avx` requires `--macos`, because only the macOS runtime starts without the host CPU check. Not lowered yet, and listed in `docs/dev/debt/relinker.md`: - AVX2, F16C, FMA, PDEP and PEXT (#12678 adds opt-in PDEP/PEXT for Rosetta). - Letting the Linux and Windows runtimes accept a lowered conversion. ### Tested - **macOS 13.7.8 on a MacBook Pro 8,1 (i5-2415M, AVX but no AVX2/BMI/LZCNT/MOVBE), LLVM 19.1.7, relinker-only:** - `count_lowering`, `movbe_lowering`, `cli_args`, `andn_lowering`, `bls_lowering`, `shift_lowering`, `bitfield_lowering` and `mulx_lowering` pass. The two new execution tests run their stubs on a CPU that lacks the instructions and compare result and flags against an SDM model. - `amd64_only_converter` stops with SIGILL on this host on `main` as well: it runs host AVX2 code, as #16236 describes. - **Windows 11, MinGW GCC:** `amd64_only_converter` (including the new `converterAvxShortSites`), `count_lowering`, `movbe_lowering` and `cli_args` pass. The execution parts are skipped on Windows. - **Real code:** converted Celeste Classic's eboot (homebrew) with `--macos --to-avx --skip-sce-module`. The `--to-rosetta` output still holds 1,674 TZCNT, 1,108 LZCNT and 111 MOVBE; the `--to-avx` output holds none of them, and at 31.4 MB it is smaller than the `--to-rosetta` output (31.7 MB) because it skips the store splits. The title does not run on this Mac yet, because it also uses AVX2. ### 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)
gabichuuuuu commented 2026-11-08 11:56:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T11:56:03Z.

Review: COMMENTED


Windows run of this head as part of #17132's head a7614d3fcf, which contains 32ccea1fd0 unchanged, merged onto main 105c9e4d91. Setup: Windows 11, i7-12800H (BMI1/2, LZCNT, MOVBE), WinLibs GCC 15.2, Release, relinker-only.

  • As pushed, these pass: count_lowering, movbe_lowering, cli_args, amd64_only_converter, and the existing ANDN, BLS, shift, bitfield and MULX lowering tests. The execution parts are compiled out on Windows.
  • With a local, unpushed _WIN32 branch in LoweringExecution.hpp (VirtualAlloc/VirtualProtect and a sysv_abi call; details on #17132), the execution parts of count_lowering and movbe_lowering run here and pass against your SDM model.
  • Native oracle for TZCNT/LZCNT. This CPU has both, so count_lowering's execution also ran each site as the original instruction, using the same wrapper with the site bytes emitted raw. I compared all 16 GPRs, CF and ZF, and the red zone against the stub. That covers every register case and every memory form in the test, both flag inputs each time: 13,272 runs, all identical. So the BSF/BSR + CMOVZ stubs match real TZCNT/LZCNT on these inputs, including the zero source, the 16-bit forms keeping bits 63:16, and RIP-relative memory.

MOVBE was checked only against the SDM model, not natively.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5817776934 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17084#pullrequestreview-5817776934). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T11:56:03Z. **Review:** COMMENTED --- Windows run of this head as part of #17132's head `a7614d3fcf`, which contains `32ccea1fd0` unchanged, merged onto `main` `105c9e4d91`. Setup: Windows 11, i7-12800H (BMI1/2, LZCNT, MOVBE), WinLibs GCC 15.2, Release, relinker-only. - As pushed, these pass: `count_lowering`, `movbe_lowering`, `cli_args`, `amd64_only_converter`, and the existing ANDN, BLS, shift, bitfield and MULX lowering tests. The execution parts are compiled out on Windows. - With a local, unpushed `_WIN32` branch in `LoweringExecution.hpp` (`VirtualAlloc`/`VirtualProtect` and a `sysv_abi` call; details on #17132), the execution parts of `count_lowering` and `movbe_lowering` run here and pass against your SDM model. - **Native oracle for TZCNT/LZCNT.** This CPU has both, so `count_lowering`'s execution also ran each site as the original instruction, using the same wrapper with the site bytes emitted raw. I compared all 16 GPRs, CF and ZF, and the red zone against the stub. That covers every register case and every memory form in the test, both flag inputs each time: 13,272 runs, all identical. So the BSF/BSR + CMOVZ stubs match real TZCNT/LZCNT on these inputs, including the zero source, the 16-bit forms keeping bits 63:16, and RIP-relative memory. MOVBE was checked only against the SDM model, not natively.
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!17084
No description provided.