cpu: lower 32/64-bit BMI2 BZHI, MULX, PDEP, and PEXT in cpu_bmi2_bit_aot #2613

Merged
Ammmae0x merged 1 commit from upstream-pr/2621 into main 2026-11-25 23:33:07 +00:00
Ammmae0x commented 2026-11-25 23:29:32 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T23:29:32Z.


Change

Add a dedicated BMI2 bit-manipulation AOT lifter tools/cpu_bmi2_bit_aot.py lowering 32-bit and 64-bit register and flat zero-displacement [rcx|rdx|r8] memory-source forms of BZHI, MULX, PDEP, and PEXT plus MASM differential/refusal/mutation coverage in tests/cpu/bmi2_bit_aot.{asm,def,_check.py} (registered as cpu_aot_bmi2_bit in CMakeLists.txt with SKIP_RETURN_CODE 77 and FORM_COVERAGE_RECORD):

  • Model exact Intel SDM semantics: 32-bit zero-extension into [63:32]; BZHI 8-bit index masking (index >= width preserves source with CF=1; index == 0 yields 0 with CF=0; updates CF/ZF/SF/OF=0 and invalidates PF/AF); MULX implicit EDX/RDX operand read before writing dst_lo then dst_hi (so op0 == op1 receives the high half) with all flags preserved; PDEP/PEXT flagless bit deposit/extract; unconditional load_always() so /O2 preserves faulting discarded reads.
  • Refuse EVEX/XOP, all BMI2/legacy shifts, BMI1, nonvolatile GPRs, argument GPR writes, high-byte GPRs, displaced/RIP-relative/SIB/bad-base memory operands, non-[r8] stores, undefined GPR/flag reads (including PF/AF after BZHI), instruction prefixes, branches/calls, and missing/trailing ret.

AI-assisted.

Verification

Tested on commit d9536df4 (rebased on upstream/main) with MSVC 19.50 + ML64 14.50 and out\pe-env\Scripts\python.exe -B:

  • ctest --preset dev -N / ctest --preset release -N: Total Tests: 2085 (no dropped tests).
  • cmake --build --preset dev --target cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted: 0 errors, 0 warnings.
  • ctest --preset dev --output-on-failure --no-tests=error -R "^cpu_aot_bmi2_bit$" -E "cpu_native_archive_" -V: 1/1 passed (5 alt + 52 unit + 3 raw + 10 PE-patched + CLI rejections; 3,006 lifted + 3,006 /O2 /W4 /WX cases across 26 exports matched bit for bit; ALTER_BMI2_BIT diverged on all 10 altered exports; 0 capability skips on BMI2 host).
  • cmake --build --preset release --target cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted (/O2): 0 errors, 0 warnings; ctest --preset release -R "^cpu_aot_bmi2_bit$" -V: 1/1 passed.
  • Verified FORM_COVERAGE_RECORD in both dev and release (15 mnemonics: bzhi,cmp,mov,movzx,mulx,or,pdep,pext,ret,setb,sete,seto,setp,sets,xor).
  • Spot-mutation negative control: temporarily disabled 4 translator guards one by one in tools/cpu_bmi2_bit_aot.py (BZHI PF invalidation, displaced-memory rejection, argument-GPR write rejection, non-[r8] store rejection); 4/4 mutations killed by check_rejections() and restored cleanly.
  • out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py: passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests\docs_links_check.py: passed (exit 0).
  • out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py: passed (exit 0).

Evidence and follow-up

All fixtures in tests/cpu/bmi2_bit_aot.{asm,def} are source-built synthetic MASM routines (MIT License). Native execution is CPUID leaf 7 subleaf 0 EBX[8] BMI2 gated (exit 77 on non-BMI2 hosts). Remaining BMI1/ADX forms remain follow-ups.

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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6615908594 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2621). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T23:29:32Z. --- ## Change Add a dedicated BMI2 bit-manipulation AOT lifter `tools/cpu_bmi2_bit_aot.py` lowering 32-bit and 64-bit register and flat zero-displacement `[rcx|rdx|r8]` memory-source forms of `BZHI`, `MULX`, `PDEP`, and `PEXT` plus MASM differential/refusal/mutation coverage in `tests/cpu/bmi2_bit_aot.{asm,def,_check.py}` (registered as `cpu_aot_bmi2_bit` in `CMakeLists.txt` with `SKIP_RETURN_CODE 77` and `FORM_COVERAGE_RECORD`): - Model exact Intel SDM semantics: 32-bit zero-extension into `[63:32]`; `BZHI` 8-bit index masking (`index >= width` preserves source with `CF=1`; `index == 0` yields `0` with `CF=0`; updates `CF/ZF/SF/OF=0` and invalidates `PF/AF`); `MULX` implicit `EDX`/`RDX` operand read before writing `dst_lo` then `dst_hi` (so `op0 == op1` receives the high half) with all flags preserved; `PDEP`/`PEXT` flagless bit deposit/extract; unconditional `load_always()` so `/O2` preserves faulting discarded reads. - Refuse EVEX/XOP, all BMI2/legacy shifts, BMI1, nonvolatile GPRs, argument GPR writes, high-byte GPRs, displaced/RIP-relative/SIB/bad-base memory operands, non-`[r8]` stores, undefined GPR/flag reads (including `PF`/`AF` after `BZHI`), instruction prefixes, branches/calls, and missing/trailing `ret`. AI-assisted. ## Verification Tested on commit `d9536df4` (rebased on `upstream/main`) with MSVC 19.50 + ML64 14.50 and `out\pe-env\Scripts\python.exe -B`: - `ctest --preset dev -N` / `ctest --preset release -N`: Total Tests: 2085 (no dropped tests). - `cmake --build --preset dev --target cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted`: 0 errors, 0 warnings. - `ctest --preset dev --output-on-failure --no-tests=error -R "^cpu_aot_bmi2_bit$" -E "cpu_native_archive_" -V`: 1/1 passed (5 alt + 52 unit + 3 raw + 10 PE-patched + CLI rejections; 3,006 lifted + 3,006 `/O2 /W4 /WX` cases across 26 exports matched bit for bit; `ALTER_BMI2_BIT` diverged on all 10 altered exports; 0 capability skips on BMI2 host). - `cmake --build --preset release --target cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted` (/O2): 0 errors, 0 warnings; `ctest --preset release -R "^cpu_aot_bmi2_bit$" -V`: 1/1 passed. - Verified `FORM_COVERAGE_RECORD` in both `dev` and `release` (15 mnemonics: `bzhi,cmp,mov,movzx,mulx,or,pdep,pext,ret,setb,sete,seto,setp,sets,xor`). - Spot-mutation negative control: temporarily disabled 4 translator guards one by one in `tools/cpu_bmi2_bit_aot.py` (`BZHI` `PF` invalidation, displaced-memory rejection, argument-GPR write rejection, non-`[r8]` store rejection); 4/4 mutations killed by `check_rejections()` and restored cleanly. - `out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py`: passed (exit 0). - `out\pe-env\Scripts\python.exe -B tests\docs_links_check.py`: passed (exit 0). - `out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py`: passed (exit 0). ## Evidence and follow-up All fixtures in `tests/cpu/bmi2_bit_aot.{asm,def}` are source-built synthetic MASM routines (MIT License). Native execution is CPUID leaf 7 subleaf 0 EBX[8] BMI2 gated (exit 77 on non-BMI2 hosts). Remaining BMI1/ADX forms remain follow-ups. ## 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.
upstream-sync closed this pull request 2026-11-26 00:01:01 +00:00
tedin7 commented 2026-11-26 00:01:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T23:32:01Z.

Review: COMMENTED


Review of head d9536df4 (comment only).

Checked:

  • tools/cpu_bmi2_bit_aot.py helpers against the Intel SDM: BZHI (index masked to 8 bits, index >= width keeps the source with CF=1, index == 0 gives 0 with CF=0, ZF/SF from result, OF=0, PF/AF not defined and refused on read), MULX (hi/lo operand order, implicit EDX/RDX, lo written before hi so op0 == op1 gets the high half, flags untouched), PDEP and PEXT (flagless, 32-bit results zero-extended). Operand order in the lifter (src/index/mask) matches the SDM forms.
  • Sibling slice: no other open PR covers these four opcodes; the shift forms live in the merged cpu_bmi2_shift_aot (#2367) and are refused here, so there is no overlap.
  • tests/cpu/bmi2_bit_aot_check.py covers differential, refusal and mutation cases, and CMake registers cpu_aot_bmi2_bit with SKIP_RETURN_CODE 77 like the sibling suite.

Commands run (Linux):

  • python3 tests/docs_links_check.py: ok
  • python3 tests/bidi_controls_check.py: ok
  • git diff --check origin/main...HEAD: clean

Not run on Linux: the new CTest needs MSVC/ML64 and the Windows Python environment, so it will be exercised on Windows only. No concerns found in the code.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6066450193 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2621#pullrequestreview-6066450193). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T23:32:01Z. **Review:** COMMENTED --- Review of head d9536df4 (comment only). Checked: - `tools/cpu_bmi2_bit_aot.py` helpers against the Intel SDM: BZHI (index masked to 8 bits, `index >= width` keeps the source with CF=1, `index == 0` gives 0 with CF=0, ZF/SF from result, OF=0, PF/AF not defined and refused on read), MULX (hi/lo operand order, implicit EDX/RDX, lo written before hi so op0 == op1 gets the high half, flags untouched), PDEP and PEXT (flagless, 32-bit results zero-extended). Operand order in the lifter (src/index/mask) matches the SDM forms. - Sibling slice: no other open PR covers these four opcodes; the shift forms live in the merged `cpu_bmi2_shift_aot` (#2367) and are refused here, so there is no overlap. - `tests/cpu/bmi2_bit_aot_check.py` covers differential, refusal and mutation cases, and CMake registers `cpu_aot_bmi2_bit` with `SKIP_RETURN_CODE 77` like the sibling suite. Commands run (Linux): - `python3 tests/docs_links_check.py`: ok - `python3 tests/bidi_controls_check.py`: ok - `git diff --check origin/main...HEAD`: clean Not run on Linux: the new CTest needs MSVC/ML64 and the Windows Python environment, so it will be exercised on Windows only. No concerns found in the code. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 00:01:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T23:32:58Z.


Win2025 WARP receipt for head d9536df4.

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

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted -- -j 2): 1.11s (configure 0.00s)
    • Test (ctest --preset dev -R 'cpu_aot_bmi2_bit' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (1.73s: cpu_aot_bmi2_bit 1.53s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted -- -j 2): 1.75s (configure 0.00s)
    • Test (ctest --preset release -R 'cpu_aot_bmi2_bit' --parallel 4 --no-tests=error --output-on-failure): 1/1 passed (1.67s: cpu_aot_bmi2_bit 1.48s)
  • sccache: 4/6 hits; wall 20.0s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7177716530 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2621#issuecomment-7177716530). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T23:32:58Z. --- Win2025 WARP receipt for head d9536df4. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `d9536df46656e4156e059c79fdd4ffe3c22db2c1`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted -- -j 2`): `1.11s` (configure `0.00s`) - Test (`ctest --preset dev -R 'cpu_aot_bmi2_bit' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`1.73s`: `cpu_aot_bmi2_bit` `1.53s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_bmi2_bit_altered cpu_bmi2_bit_altered_lifted cpu_bmi2_bit_original cpu_bmi2_bit_original_lifted -- -j 2`): `1.75s` (configure `0.00s`) - Test (`ctest --preset release -R 'cpu_aot_bmi2_bit' --parallel 4 --no-tests=error --output-on-failure`): **1/1 passed** (`1.67s`: `cpu_aot_bmi2_bit` `1.48s`) - `sccache`: 4/6 hits; wall `20.0s` AI-assisted; commands listed were executed locally.
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!2613
No description provided.