x64_recomp: add VEX BMI1 and BMI2 emitters #1368

Open
github-bot wants to merge 1 commit from github-bot/gh-1368 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 22:41 UTC · original
IlyassRS3:feat/x64-recomp-bmi1-bmi2 → main · +737 −0 · 2 files · 1 commits · open

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

**@⁠IlyassRS3** on GitHub · 2026-11-24 22:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1368) `IlyassRS3:feat/x64-recomp-bmi1-bmi2` → `main` · +737 −0 · 2 files · 1 commits · open 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:06 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 22:41 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 22:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1368#pullrequestreview-6048540353) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 03:27 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 0b11d86bc into the branch locally (929 commits behind, no conflicts). Head tested: 620416449.

Against a reference model all 13 BMI1/BMI2 instructions are right in 1,068 checks at 32 and 64 bits. The new test file is 5/5 once two things outside these emitters are accounted for.

  • The test file, step by step (nothing changed on disk; patched in memory):

    • as shipped under QEMU: 5 run, 39 failures, 6 errors. The failures are the wiring bug from #1409 (xr.run_kernel where xr.run_result is needed);
    • with RUNNER swapped for xr.run_result: no failures, 7 errors left, all QEMU timeouts. The ones with a visible operand use a 64-bit all-ones constant (e.g. pdep_pext with mask=0xffffffffffffffff), which hangs in Asm.li on main (the elif not nzc: movn(rd, 0) branch that #1339, #1205 and #1329 add);
    • with that one li branch also patched: 5/5.
  • Beyond the file, generated cases under QEMU against a small Python model, register forms, destination rax starting with a marker value:

    • andn, blsr, blsmsk, blsi, bextr, bzhi, shlx, sarx, shrx, rorx, pext, pdep, mulx;
    • the full 64-bit destination (zero-extension at 32 bits; junk in the upper half of 32-bit sources must be ignored), and both outputs for mulx;
    • flags read after an stc: SF/ZF/CF/OF for andn, blsr, blsmsk, blsi, bzhi; ZF/CF/OF for bextr; and for the seven that write no flags, that the carry is still set;
    • edge counts: bextr start ≥ width, length 0 and length ≥ width; bzhi index 0, width−1, width and above (with junk above bit 7 of the index); shift counts 0, width−1, width, width+1; rorx by 0 and by width at 32 bits.
    tool checks wrong
    this branch 1,068 0

    There is no main column: main does not decode these VEX forms.

  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the reasons above.

Not tested: memory operands and extended registers (r8–r15) beyond the file's own cases, AF and PF, and the flags x86 leaves undefined (SF for bextr). No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 03:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1368#issuecomment-7181740548) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `0b11d86bc` into the branch locally (929 commits behind, no conflicts). Head tested: `620416449`. **Against a reference model all 13 BMI1/BMI2 instructions are right in 1,068 checks at 32 and 64 bits. The new test file is 5/5 once two things outside these emitters are accounted for.** - The test file, step by step (nothing changed on disk; patched in memory): - as shipped under QEMU: 5 run, 39 failures, 6 errors. The failures are the wiring bug from #1409 (`xr.run_kernel` where `xr.run_result` is needed); - with `RUNNER` swapped for `xr.run_result`: no failures, 7 errors left, all QEMU timeouts. The ones with a visible operand use a 64-bit all-ones constant (e.g. `pdep_pext` with `mask=0xffffffffffffffff`), which hangs in `Asm.li` on `main` (the `elif not nzc: movn(rd, 0)` branch that #1339, #1205 and #1329 add); - with that one `li` branch also patched: **5/5**. - Beyond the file, generated cases under QEMU against a small Python model, register forms, destination `rax` starting with a marker value: - `andn`, `blsr`, `blsmsk`, `blsi`, `bextr`, `bzhi`, `shlx`, `sarx`, `shrx`, `rorx`, `pext`, `pdep`, `mulx`; - the full 64-bit destination (zero-extension at 32 bits; junk in the upper half of 32-bit sources must be ignored), and both outputs for `mulx`; - flags read after an `stc`: SF/ZF/CF/OF for `andn`, `blsr`, `blsmsk`, `blsi`, `bzhi`; ZF/CF/OF for `bextr`; and for the seven that write no flags, that the carry is still set; - edge counts: `bextr` start ≥ width, length 0 and length ≥ width; `bzhi` index 0, width−1, width and above (with junk above bit 7 of the index); shift counts 0, width−1, width, width+1; `rorx` by 0 and by width at 32 bits. | tool | checks | wrong | |---|---|---| | this branch | 1,068 | 0 | There is no `main` column: `main` does not decode these VEX forms. - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the reasons above. Not tested: memory operands and extended registers (`r8`–`r15`) beyond the file's own cases, AF and PF, and the flags x86 leaves undefined (SF for `bextr`). No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1368/head:github-bot/gh-1368'
git switch 'github-bot/gh-1368'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1368'
git switch 'github-bot/gh-1368'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1368'
git switch 'github-bot/gh-1368'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1368'
git switch 'main'
git merge --squash 'github-bot/gh-1368'
git switch 'main'
git merge --ff-only 'github-bot/gh-1368'
git switch 'main'
git merge 'github-bot/gh-1368'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.