cpu: add dedicated cpu_bmi1_aot lifter for BLSI, BLSMSK, and BLSR #2401

Merged
Ammmae0x merged 3 commits from upstream-pr/2409 into main 2026-11-25 18:51:00 +00:00
Ammmae0x commented 2026-11-25 15:37:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T15:37:20Z.


Change

Add a dedicated, portable C++ AOT lifter tools/cpu_bmi1_aot.py for the three BMI1 bit-isolation/mask/reset instructions from docs/coverage/x64_zen2.tsv group BMI/ADX:

  • BLSI (VEX_BLSI_R32_RM32, VEX_BLSI_R64_RM64): dst = (-src) & src, CF=(src!=0), ZF=(dst==0), SF=msb(dst), OF=0.
  • BLSMSK (VEX_BLSMSK_R32_RM32, VEX_BLSMSK_R64_RM64): dst = (src-1) ^ src, CF=(src==0), ZF=0, SF=msb(dst), OF=0.
  • BLSR (VEX_BLSR_R32_RM32, VEX_BLSR_R64_RM64): dst = (src-1) & src, CF=(src==0), ZF=(dst==0), SF=msb(dst), OF=0.
  • Exact 32-bit zero-extension ([63:32]=0), flat [rcx|rdx|r8] memory reads via load_always() (fault-preserving under /O2), volatile GPR glue (mov, xor, or, shl, movzx, setcc, flat [r8] stores, ret), and host_has_bmi1() (CPUID.07H.00H:EBX[3]).
  • Actively invalidates PF/AF after BLSI/BLSMSK/BLSR per Intel SDM so reading PF/AF rejects; rejects BMI2 (RORX/SARX/SHLX/SHRX/BZHI), TZCNT/ANDN/BEXTR, EVEX/XOP, nonvolatile GPRs, argument GPR writes, high-byte GPRs, undefined GPR/flag reads, displaced/indexed/non-flat memory, stores outside [r8], prefixes, branches, and missing/trailing ret.
  • Add source-built MASM fixture tests/cpu/bmi1_aot.{asm,def,_check.py} (; coverage: record cpu_aot_bmi1_bit) and register cpu_aot_bmi1_bit in CMakeLists.txt.

Verification

Verified on commit 1ab618a6 (based on upstream/main 7092683a) across both dev (Debug) and release (/O2) presets:

  • cmake --build --preset dev --target cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted -> exit 0
  • ctest --preset dev -R "^cpu_aot_bmi1_bit$" -E "cpu_native_archive_" --output-on-failure -V -> 100% tests passed, 0 tests failed out of 1 (0 skipped, 0.68s):
    • rejections passed: 3 alt + 37 IR + 4 raw + 10 PE-patched + CLI
    • bmi1_aot passed: 2223 native + 2223 lifted + 2223 /O2 cases across 21 exports; ALTER_BMI1 diverged on 12 exports + 10 mutants caught
  • cmake --build --preset release --target cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted -> exit 0
  • ctest --preset release -R "^cpu_aot_bmi1_bit$" -E "cpu_native_archive_" --output-on-failure -V -> 100% tests passed, 0 tests failed out of 1 (0 skipped, 0.84s)
  • FORM_COVERAGE_RECORD verified in both out/build/dev/form_coverage/cpu_aot_bmi1_bit.txt and out/build/release/form_coverage/cpu_aot_bmi1_bit.txt: blsi, blsmsk, blsr, mov, movzx, or, ret, setb, sete, seto, sets, shl, xor
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 464 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1727 tracked source, script and build/CI files: no bidirectional controls. (exit 0)

Spot mutation checks against tools/cpu_bmi1_aot.py (8/8 killed by tests/cpu/bmi1_aot_check.py, restored cleanly):

  • Invert BLSI32 CF (/O2), set BLSMSK64 ZF=(s==0) (/O2), set BLSR64 ZF=(s==0) instead of (d==0) (/O2), preserve high 32 bits instead of zero-extending 32-bit BMI1 destination (/O2), drop SF in BLSI64 (/O2), leave PF defined after BMI1, allow writes to argument GPRs, and allow stores outside [r8] (all returned exit 1).

Evidence and follow-up

  • tests/cpu/bmi1_aot.{asm,def,_check.py} is a clean-room, source-built MASM fixture (MIT, repo-authored; no game files, dumps, or binaries).
  • Unconditionally verifies lift, rejections, portable reference model, RFLAGS packing, 32-bit zero-extension, [r8] source-then-store, discarded faulting read, /O2 build, mutants, and ALTER_BMI1 before any host_has_bmi1() exit-77 capability skip (0 skips on this BMI1-capable host).

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6608132749 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2409). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T15:37:20Z. --- ## Change Add a dedicated, portable C++ AOT lifter `tools/cpu_bmi1_aot.py` for the three BMI1 bit-isolation/mask/reset instructions from `docs/coverage/x64_zen2.tsv` group `BMI/ADX`: - `BLSI` (`VEX_BLSI_R32_RM32`, `VEX_BLSI_R64_RM64`): `dst = (-src) & src`, `CF=(src!=0)`, `ZF=(dst==0)`, `SF=msb(dst)`, `OF=0`. - `BLSMSK` (`VEX_BLSMSK_R32_RM32`, `VEX_BLSMSK_R64_RM64`): `dst = (src-1) ^ src`, `CF=(src==0)`, `ZF=0`, `SF=msb(dst)`, `OF=0`. - `BLSR` (`VEX_BLSR_R32_RM32`, `VEX_BLSR_R64_RM64`): `dst = (src-1) & src`, `CF=(src==0)`, `ZF=(dst==0)`, `SF=msb(dst)`, `OF=0`. - Exact 32-bit zero-extension (`[63:32]=0`), flat `[rcx|rdx|r8]` memory reads via `load_always()` (fault-preserving under `/O2`), volatile GPR glue (`mov, xor, or, shl, movzx, setcc`, flat `[r8]` stores, `ret`), and `host_has_bmi1()` (`CPUID.07H.00H:EBX[3]`). - Actively invalidates `PF`/`AF` after `BLSI`/`BLSMSK`/`BLSR` per Intel SDM so reading `PF`/`AF` rejects; rejects BMI2 (`RORX`/`SARX`/`SHLX`/`SHRX`/`BZHI`), `TZCNT`/`ANDN`/`BEXTR`, EVEX/XOP, nonvolatile GPRs, argument GPR writes, high-byte GPRs, undefined GPR/flag reads, displaced/indexed/non-flat memory, stores outside `[r8]`, prefixes, branches, and missing/trailing `ret`. - Add source-built MASM fixture `tests/cpu/bmi1_aot.{asm,def,_check.py}` (`; coverage: record cpu_aot_bmi1_bit`) and register `cpu_aot_bmi1_bit` in `CMakeLists.txt`. ## Verification Verified on commit `1ab618a6` (based on `upstream/main` `7092683a`) across both `dev` (Debug) and `release` (`/O2`) presets: - `cmake --build --preset dev --target cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted` -> exit 0 - `ctest --preset dev -R "^cpu_aot_bmi1_bit$" -E "cpu_native_archive_" --output-on-failure -V` -> `100% tests passed, 0 tests failed out of 1` (0 skipped, 0.68s): - `rejections passed: 3 alt + 37 IR + 4 raw + 10 PE-patched + CLI` - `bmi1_aot passed: 2223 native + 2223 lifted + 2223 /O2 cases across 21 exports; ALTER_BMI1 diverged on 12 exports + 10 mutants caught` - `cmake --build --preset release --target cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted` -> exit 0 - `ctest --preset release -R "^cpu_aot_bmi1_bit$" -E "cpu_native_archive_" --output-on-failure -V` -> `100% tests passed, 0 tests failed out of 1` (0 skipped, 0.84s) - `FORM_COVERAGE_RECORD` verified in both `out/build/dev/form_coverage/cpu_aot_bmi1_bit.txt` and `out/build/release/form_coverage/cpu_aot_bmi1_bit.txt`: `blsi, blsmsk, blsr, mov, movzx, or, ret, setb, sete, seto, sets, shl, xor` - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 464 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1727 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) Spot mutation checks against `tools/cpu_bmi1_aot.py` (8/8 killed by `tests/cpu/bmi1_aot_check.py`, restored cleanly): - Invert `BLSI32` `CF` (`/O2`), set `BLSMSK64` `ZF=(s==0)` (`/O2`), set `BLSR64` `ZF=(s==0)` instead of `(d==0)` (`/O2`), preserve high 32 bits instead of zero-extending 32-bit BMI1 destination (`/O2`), drop `SF` in `BLSI64` (`/O2`), leave `PF` defined after BMI1, allow writes to argument GPRs, and allow stores outside `[r8]` (all returned exit 1). ## Evidence and follow-up - `tests/cpu/bmi1_aot.{asm,def,_check.py}` is a clean-room, source-built MASM fixture (MIT, repo-authored; no game files, dumps, or binaries). - Unconditionally verifies lift, rejections, portable reference model, RFLAGS packing, 32-bit zero-extension, `[r8]` source-then-store, discarded faulting read, `/O2` build, mutants, and `ALTER_BMI1` before any `host_has_bmi1()` exit-77 capability skip (0 skips on this BMI1-capable host). ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
KelboLive commented 2026-11-25 16:05:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-25T15:51:13Z.


Review: PR #2409 — cpu: add dedicated cpu_bmi1_aot lifter for BLSI, BLSMSK, and BLSR

Decision: APPROVED

Summary

Adds tools/cpu_bmi1_aot.py (584 lines) as a dedicated lifter for the three BMI1 bit-isolation/mask/reset instructions: BLSI, BLSMSK, and BLSR. Includes MASM fixture (tests/cpu/bmi1_aot.asm, 252 lines), DEF file, and 634-line test script.

Correctness

  • BLSI: dst = (~src + 1) & src with CF=(src!=0), ZF=(dst==0), SF=msb, OF=0 — correct per Intel SDM
  • BLSMSK: dst = (src-1) ^ src with CF=(src==0), ZF=false, SF=msb, OF=0 — correct
  • BLSR: dst = (src-1) & src with CF=(src==0), ZF=(dst==0), SF=msb, OF=0 — correct
  • 32-bit zero-extension via update_gpr_width(val, 32u) → val \& 0xFFFFFFFFull — correct
  • PF/AF correctly marked undefined after BMI1 ops and rejected when read — correct
  • Memory constraints: flat [rcx|rdx|r8] loads, stores only to [r8] — correct
  • GPR constraints: volatile-only (rax/rcx/rdx/r8–r11), no arg writes, no high-byte GPRs — correct
  • Comprehensive rejections: BMI2/RORX/SARX/SHLX/SHRX/BZHI, other BMI1/TZCNT/ANDN/BEXTR, EVEX/XOP, nonvolatile GPRs, undefined GPR/flag reads, non-flat memory, non-r8 stores, prefixes, branches — all correct

Test Coverage

  • 22 exports covering 32/64-bit register and flat [rcx|rdx|r8] memory forms
  • 2223 native + 2223 lifted + 2223 /O2 cases pass on dev and release presets
  • 8/8 mutation mutants killed
  • ALTER_BMI1 diverges on 12 exports as expected (24 total mutants expected per 12 exports)
  • 37 IR rejections + 4 raw + 10 PE-patched + CLI tests pass

Consistency

  • Follows existing cpu_leaf_aot patterns exactly (load_always/store_always, PRELUDE, bounded_code, export_rva)
  • Form coverage record includes correct set of exported symbols
  • No game files, source-built MASM fixture

No blocking issues found.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7167370344 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2409#issuecomment-7167370344). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-25T15:51:13Z. --- ## Review: PR #2409 — `cpu: add dedicated cpu_bmi1_aot lifter for BLSI, BLSMSK, and BLSR` **Decision: APPROVED** ### Summary Adds `tools/cpu_bmi1_aot.py` (584 lines) as a dedicated lifter for the three BMI1 bit-isolation/mask/reset instructions: BLSI, BLSMSK, and BLSR. Includes MASM fixture (`tests/cpu/bmi1_aot.asm`, 252 lines), DEF file, and 634-line test script. ### Correctness - BLSI: `dst = (~src + 1) & src` with CF=(src!=0), ZF=(dst==0), SF=msb, OF=0 — **correct per Intel SDM** - BLSMSK: `dst = (src-1) ^ src` with CF=(src==0), ZF=false, SF=msb, OF=0 — **correct** - BLSR: `dst = (src-1) & src` with CF=(src==0), ZF=(dst==0), SF=msb, OF=0 — **correct** - 32-bit zero-extension via `update_gpr_width(val, 32u) → val \& 0xFFFFFFFFull` — **correct** - PF/AF correctly marked undefined after BMI1 ops and rejected when read — **correct** - Memory constraints: flat [rcx|rdx|r8] loads, stores only to [r8] — **correct** - GPR constraints: volatile-only (rax/rcx/rdx/r8–r11), no arg writes, no high-byte GPRs — **correct** - Comprehensive rejections: BMI2/RORX/SARX/SHLX/SHRX/BZHI, other BMI1/TZCNT/ANDN/BEXTR, EVEX/XOP, nonvolatile GPRs, undefined GPR/flag reads, non-flat memory, non-r8 stores, prefixes, branches — **all correct** ### Test Coverage - 22 exports covering 32/64-bit register and flat [rcx|rdx|r8] memory forms - 2223 native + 2223 lifted + 2223 /O2 cases pass on dev and release presets - 8/8 mutation mutants killed - ALTER_BMI1 diverges on 12 exports as expected (24 total mutants expected per 12 exports) - 37 IR rejections + 4 raw + 10 PE-patched + CLI tests pass ### Consistency - Follows existing `cpu_leaf_aot` patterns exactly (load_always/store_always, PRELUDE, bounded_code, export_rva) - Form coverage record includes correct set of exported symbols - No game files, source-built MASM fixture ### No blocking issues found.
tedin7 commented 2026-11-25 17:01:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T16:25:45Z.

Review: COMMENTED


Review of head 1ab618a6 (comment only).

Checked:

  • git diff --stat origin/main...HEAD: 5 files, all new except a 24-line CMake block. Same shape as the sibling lifters (cpu_aot_bmi2_shift and others), so it is small and additive.
  • No other open or merged PR covers BLSI/BLSMSK/BLSR (gh pr list --state all --search "cpu_bmi1_aot BLSI BLSR BLSMSK").
  • Flag semantics in tests/cpu/bmi1_aot_check.py:53 match the instruction definitions: BLSI CF = (src != 0), ZF from result; BLSMSK CF = (src == 0), ZF = 0; BLSR CF = (src == 0), ZF from result; OF = 0, SF from result; 32-bit results zero-extend.
  • The test is a real differential: compiled original vs lifted executables are run on edge values (0, INT32_MIN, dirty high 32 bits), compared with the Python reference, plus mutant quirks (invert_cf, drop_sf, no_zext32, ...) and an altered-binary check, so a wrong lowering would fail.
  • Memory-source forms, flag-consuming variants and the discarded-read case are exported and covered.
  • Python compiles with python3 -I -m py_compile.

No blocking findings. Win2025 WARP dev+release will be run next.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6061125864 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2409#pullrequestreview-6061125864). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T16:25:45Z. **Review:** COMMENTED --- Review of head 1ab618a6 (comment only). Checked: - `git diff --stat origin/main...HEAD`: 5 files, all new except a 24-line CMake block. Same shape as the sibling lifters (`cpu_aot_bmi2_shift` and others), so it is small and additive. - No other open or merged PR covers BLSI/BLSMSK/BLSR (`gh pr list --state all --search "cpu_bmi1_aot BLSI BLSR BLSMSK"`). - Flag semantics in `tests/cpu/bmi1_aot_check.py:53` match the instruction definitions: BLSI CF = (src != 0), ZF from result; BLSMSK CF = (src == 0), ZF = 0; BLSR CF = (src == 0), ZF from result; OF = 0, SF from result; 32-bit results zero-extend. - The test is a real differential: compiled original vs lifted executables are run on edge values (0, INT32_MIN, dirty high 32 bits), compared with the Python reference, plus mutant quirks (`invert_cf`, `drop_sf`, `no_zext32`, ...) and an altered-binary check, so a wrong lowering would fail. - Memory-source forms, flag-consuming variants and the discarded-read case are exported and covered. - Python compiles with `python3 -I -m py_compile`. No blocking findings. Win2025 WARP dev+release will be run next. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 17:01:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T16:29:40Z.


Win2025 WARP receipt for head 0804956f after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 0804956f3faa2cebb59d6583b786cd8d3e4e663b:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted -- -j 2): 3.67s (configure 1.65s)
    • Test (ctest --preset dev -R 'cpu_aot_bmi1_bit' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (1.06s: cpu_aot_bmi1_bit 0.95s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted -- -j 2): 1.07s (configure 1.73s)
    • Test (ctest --preset release -R 'cpu_aot_bmi1_bit' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (1.06s: cpu_aot_bmi1_bit 0.96s)
  • sccache: 0/4 hits; wall 15.8s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7168069956 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2409#issuecomment-7168069956). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T16:29:40Z. --- Win2025 WARP receipt for head 0804956f after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `0804956f3faa2cebb59d6583b786cd8d3e4e663b`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted -- -j 2`): `3.67s` (configure `1.65s`) - Test (`ctest --preset dev -R 'cpu_aot_bmi1_bit' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`1.06s`: `cpu_aot_bmi1_bit` `0.95s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_bmi1_aot_altered cpu_bmi1_aot_altered_lifted cpu_bmi1_aot_original cpu_bmi1_aot_original_lifted -- -j 2`): `1.07s` (configure `1.73s`) - Test (`ctest --preset release -R 'cpu_aot_bmi1_bit' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`1.06s`: `cpu_aot_bmi1_bit` `0.96s`) - `sccache`: 0/4 hits; wall `15.8s` AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 19:03:37 +00:00
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
R0Y00/Xbox2Windows!2401
No description provided.