[PR #1368] x64_recomp: add VEX BMI1 and BMI2 emitters #1398

Open
opened 2026-11-26 20:21:07 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1368
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-bmi1-bmi2


📝 Commits (1)

  • 6204164 x64_recomp: add VEX BMI1 and BMI2 emitters

📊 Changes

2 files changed (+737 additions, -0 deletions)

View changed files

➕ tests/test_x64_recomp_bmi1_bmi2.py (+475 -0)
📝 tools/x64_recomp.py (+262 -0)

📄 Description

Item ID: X64-RECOMP-BMI1-BMI2

Summary

tools/x64_recomp.py previously decoded all map-2 (0F 38) and map-3 (0F 3A) VEX instructions as generic ("simd", "V W") / ("simd", "V W Ib"), which misdecoded every Zen 2 / AVX2 BMI1 and BMI2 integer instruction with XMM operands and failed translation.

This PR adds decoding, flag tracking, and ARM64 lowering for all 13 VEX.LZ BMI1 and BMI2 instructions across 32-bit (VEX.W=0) and 64-bit (VEX.W=1) register and memory operands:

  • Decoder (_decode_vex_bmi, dispatched from decode_one for non-EVEX map in (2, 3) with VEX.L == 0):
    • BMI1: andn (0F38 F2), blsr (0F38 F3 /1), blsmsk (0F38 F3 /2), blsi (0F38 F3 /3), bextr (0F38 F7, pp=0).
    • BMI2: bzhi (0F38 F5, pp=0), pext (F3 0F38 F5), pdep (F2 0F38 F5), mulx (F2 0F38 F6), shlx (66 0F38 F7), sarx (F3 0F38 F7), shrx (F2 0F38 F7), rorx (F2 0F3A F0 /r ib with vvvv == 0).
    • Correctly threads VEX.R, VEX.X, and VEX.B into _modrm so r8..r15 work across reg, vvvv, and r/m operands, resolves RIP-relative abs targets past imm8 for rorx, leaves VEX.L == 1 untouched, and marks invalid 0F38 F3 /reg extensions as unknown with exact length.
  • Flag writers (FLAG_WRITERS & _emit_bmi):
    • Registers andn, blsr, blsmsk, blsi, bextr, bzhi in FLAG_WRITERS ("NZCV"), setting exact SF, ZF, CF (src == 0 for blsr/blsmsk, src != 0 for blsi, index >= W for bzhi, 0 for andn/bextr), and OF = 0 on both ARM NZCV and materialized ctx.eflags.
    • Lowers all 7 flag-preserving BMI2 instructions (shlx, sarx, shrx, rorx, mulx, pdep, pext) using strictly non-flag-setting ARMv8.0 instructions: LSLV/ASRV/LSRV, ROR, UMADDL/MUL+UMULH (writing lo before hi so hi == lo yields the high half per Intel SDM), and lowest-set-bit pdep/pext loops — preserving all flags (CF/ZF/SF/OF/PF/AF).
    • All 32-bit operations zero-extend their destination registers to 64 bits.

Tests (tests/test_x64_recomp_bmi1_bmi2.py)

  • 2 static tests: 18 LLVM-cross-checked (clang -arch x86_64 -mbmi -mbmi2) decode/extended-reg/RIP-relative/rejection checks and full 13-instruction 32/64-bit translation verification.
  • 3 hardware execution suites:
    • BMI1 (andn, blsr, blsmsk, blsi, bextr) across 32/64-bit, boundary counts/lengths, and ARM NZCV + cross-block ctx.eflags.
    • BMI2 (bzhi, shlx, sarx, shrx, rorx, mulx) including index wrapping (0x104), index >= W CF=1, count masking mod 32/64, 128-bit unsigned product, and mulx rdx, rdx, rcx (hi == lo == rdx).
    • pdep/pext across zero/all-ones/Morton/64-bit sparse masks + sequential flag-preservation check across all 7 flag-preserving BMI2 ops on both ARM NZCV and cross-block ctx.eflags.

Verification

  • Host: macOS (Apple M5, arm64), Python 3.
  • Standalone checks (no game files):
    • python3 -I tests/test_x64_recomp_bmi1_bmi2.py — 5 tests (2 static OK, 3 exec skipped without runner)
    • python3 -I tests/test_x64_recomp.py — OK
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_bmi1_bmi2.py — OK
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Mutation proof: reverting tools/x64_recomp.py to upstream/main fails all static test methods (19 failures, 1 error).
  • Native Apple M5 hardware execution (16-way scratch merge with #1174 and all open x64_recomp PRs, 0 conflicts):
    • tests/test_x64_recomp_bmi1_bmi2.py: 5/5 passed (0 skipped)
    • All 15 test_x64_recomp_*.py suites combined: 119/119 passed (0 skipped)

AI-assisted: yes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1368 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-bmi1-bmi2` --- ### 📝 Commits (1) - [`6204164`](https://github.com/ubiquitarianism/leonida/commit/6204164491224a0507a220a572942fce0c359614) x64_recomp: add VEX BMI1 and BMI2 emitters ### 📊 Changes **2 files changed** (+737 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_bmi1_bmi2.py` (+475 -0) 📝 `tools/x64_recomp.py` (+262 -0) </details> ### 📄 Description Item ID: `X64-RECOMP-BMI1-BMI2` ### Summary `tools/x64_recomp.py` previously decoded all map-2 (`0F 38`) and map-3 (`0F 3A`) VEX instructions as generic `("simd", "V W")` / `("simd", "V W Ib")`, which misdecoded every Zen 2 / AVX2 BMI1 and BMI2 integer instruction with XMM operands and failed translation. This PR adds decoding, flag tracking, and ARM64 lowering for all 13 VEX.LZ BMI1 and BMI2 instructions across 32-bit (`VEX.W=0`) and 64-bit (`VEX.W=1`) register and memory operands: - **Decoder (`_decode_vex_bmi`, dispatched from `decode_one` for non-EVEX `map in (2, 3)` with `VEX.L == 0`)**: - **BMI1**: `andn` (`0F38 F2`), `blsr` (`0F38 F3 /1`), `blsmsk` (`0F38 F3 /2`), `blsi` (`0F38 F3 /3`), `bextr` (`0F38 F7`, `pp=0`). - **BMI2**: `bzhi` (`0F38 F5`, `pp=0`), `pext` (`F3 0F38 F5`), `pdep` (`F2 0F38 F5`), `mulx` (`F2 0F38 F6`), `shlx` (`66 0F38 F7`), `sarx` (`F3 0F38 F7`), `shrx` (`F2 0F38 F7`), `rorx` (`F2 0F3A F0 /r ib` with `vvvv == 0`). - Correctly threads `VEX.R`, `VEX.X`, and `VEX.B` into `_modrm` so `r8..r15` work across `reg`, `vvvv`, and `r/m` operands, resolves RIP-relative `abs` targets past `imm8` for `rorx`, leaves `VEX.L == 1` untouched, and marks invalid `0F38 F3` `/reg` extensions as `unknown` with exact length. - **Flag writers (`FLAG_WRITERS` & `_emit_bmi`)**: - Registers `andn`, `blsr`, `blsmsk`, `blsi`, `bextr`, `bzhi` in `FLAG_WRITERS` (`"NZCV"`), setting exact `SF`, `ZF`, `CF` (`src == 0` for `blsr`/`blsmsk`, `src != 0` for `blsi`, `index >= W` for `bzhi`, `0` for `andn`/`bextr`), and `OF = 0` on both ARM `NZCV` and materialized `ctx.eflags`. - Lowers all 7 flag-preserving BMI2 instructions (`shlx`, `sarx`, `shrx`, `rorx`, `mulx`, `pdep`, `pext`) using strictly non-flag-setting ARMv8.0 instructions: `LSLV`/`ASRV`/`LSRV`, `ROR`, `UMADDL`/`MUL`+`UMULH` (writing `lo` before `hi` so `hi == lo` yields the high half per Intel SDM), and lowest-set-bit `pdep`/`pext` loops — preserving all flags (`CF`/`ZF`/`SF`/`OF`/`PF`/`AF`). - All 32-bit operations zero-extend their destination registers to 64 bits. ### Tests (`tests/test_x64_recomp_bmi1_bmi2.py`) - 2 static tests: 18 LLVM-cross-checked (`clang -arch x86_64 -mbmi -mbmi2`) decode/extended-reg/RIP-relative/rejection checks and full 13-instruction 32/64-bit translation verification. - 3 hardware execution suites: - BMI1 (`andn`, `blsr`, `blsmsk`, `blsi`, `bextr`) across 32/64-bit, boundary counts/lengths, and ARM `NZCV` + cross-block `ctx.eflags`. - BMI2 (`bzhi`, `shlx`, `sarx`, `shrx`, `rorx`, `mulx`) including index wrapping (`0x104`), `index >= W` `CF=1`, count masking mod 32/64, 128-bit unsigned product, and `mulx rdx, rdx, rcx` (`hi == lo == rdx`). - `pdep`/`pext` across zero/all-ones/Morton/64-bit sparse masks + sequential flag-preservation check across all 7 flag-preserving BMI2 ops on both ARM `NZCV` and cross-block `ctx.eflags`. ### Verification - **Host**: macOS (Apple M5, arm64), Python 3. - **Standalone checks (no game files)**: - `python3 -I tests/test_x64_recomp_bmi1_bmi2.py` — 5 tests (2 static OK, 3 exec skipped without runner) - `python3 -I tests/test_x64_recomp.py` — OK - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_bmi1_bmi2.py` — OK - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Mutation proof**: reverting `tools/x64_recomp.py` to `upstream/main` fails all static test methods (19 failures, 1 error). - **Native Apple M5 hardware execution (16-way scratch merge with #1174 and all open `x64_recomp` PRs, 0 conflicts)**: - `tests/test_x64_recomp_bmi1_bmi2.py`: **5/5 passed** (0 skipped) - All 15 `test_x64_recomp_*.py` suites combined: **119/119 passed** (0 skipped) AI-assisted: yes --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1398
No description provided.