x64_recomp: add VEX BMI1 and BMI2 emitters #1368
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1368"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by quietfold on 2026-11-24T22:41:09Z.
Item ID:
X64-RECOMP-BMI1-BMI2Summary
tools/x64_recomp.pypreviously 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:_decode_vex_bmi, dispatched fromdecode_onefor non-EVEXmap in (2, 3)withVEX.L == 0):andn(0F38 F2),blsr(0F38 F3 /1),blsmsk(0F38 F3 /2),blsi(0F38 F3 /3),bextr(0F38 F7,pp=0).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 ibwithvvvv == 0).VEX.R,VEX.X, andVEX.Binto_modrmsor8..r15work acrossreg,vvvv, andr/moperands, resolves RIP-relativeabstargets pastimm8forrorx, leavesVEX.L == 1untouched, and marks invalid0F38 F3/regextensions asunknownwith exact length.FLAG_WRITERS&_emit_bmi):andn,blsr,blsmsk,blsi,bextr,bzhiinFLAG_WRITERS("NZCV"), setting exactSF,ZF,CF(src == 0forblsr/blsmsk,src != 0forblsi,index >= Wforbzhi,0forandn/bextr), andOF = 0on both ARMNZCVand materializedctx.eflags.shlx,sarx,shrx,rorx,mulx,pdep,pext) using strictly non-flag-setting ARMv8.0 instructions:LSLV/ASRV/LSRV,ROR,UMADDL/MUL+UMULH(writinglobeforehisohi == loyields the high half per Intel SDM), and lowest-set-bitpdep/pextloops — preserving all flags (CF/ZF/SF/OF/PF/AF).Tests (
tests/test_x64_recomp_bmi1_bmi2.py)clang -arch x86_64 -mbmi -mbmi2) decode/extended-reg/RIP-relative/rejection checks and full 13-instruction 32/64-bit translation verification.andn,blsr,blsmsk,blsi,bextr) across 32/64-bit, boundary counts/lengths, and ARMNZCV+ cross-blockctx.eflags.bzhi,shlx,sarx,shrx,rorx,mulx) including index wrapping (0x104),index >= WCF=1, count masking mod 32/64, 128-bit unsigned product, andmulx rdx, rdx, rcx(hi == lo == rdx).pdep/pextacross zero/all-ones/Morton/64-bit sparse masks + sequential flag-preservation check across all 7 flag-preserving BMI2 ops on both ARMNZCVand cross-blockctx.eflags.Verification
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— OKpython3 -m compileall -q make_game.py tools tests— OKpython3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_bmi1_bmi2.py— OKpython3 -I tests/test_text_write_line_endings.py— 3/3 OKtools/x64_recomp.pytoupstream/mainfails all static test methods (19 failures, 1 error).x64_recompPRs, 0 conflicts):tests/test_x64_recomp_bmi1_bmi2.py: 5/5 passed (0 skipped)test_x64_recomp_*.pysuites combined: 119/119 passed (0 skipped)AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-24T22:41:22Z.
Review: COMMENTED
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.
Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T03:27:47Z.
Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged
mainat0b11d86bcinto 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):
xr.run_kernelwherexr.run_resultis needed);RUNNERswapped forxr.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_pextwithmask=0xffffffffffffffff), which hangs inAsm.lionmain(theelif not nzc: movn(rd, 0)branch that #1339, #1205 and #1329 add);libranch also patched: 5/5.Beyond the file, generated cases under QEMU against a small Python model, register forms, destination
raxstarting with a marker value:andn,blsr,blsmsk,blsi,bextr,bzhi,shlx,sarx,shrx,rorx,pext,pdep,mulx;mulx;stc: SF/ZF/CF/OF forandn,blsr,blsmsk,blsi,bzhi; ZF/CF/OF forbextr; and for the seven that write no flags, that the carry is still set;bextrstart ≥ width, length 0 and length ≥ width;bzhiindex 0, width−1, width and above (with junk above bit 7 of the index); shift counts 0, width−1, width, width+1;rorxby 0 and by width at 32 bits.There is no
maincolumn:maindoes not decode these VEX forms.Counts.
tests/test_x64_recomp.py44/44 andtests/test_recomp_census.py11/11, the same asmain. Full standalone suite: 514 run, 512 pass; the 2 that fail aremain'stest_mod_pack_output_aliasand 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 forbextr). No game files used. AI-assisted.Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T14:32:45Z.
Red/green: classified READY by dada-lotF (red on main, green on head merged with main); head
6204164491unchanged since 2026-11-24, before that run.Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main
d48a957bbin GitHub Actions: no new failure.AI-assisted (Claude Code, Claude Opus 5.5)