cpu: lower BT, BTS, BTR and BTC in cpu_switch_aot #1202

Merged
dicnunz merged 2 commits from upstream-pr/1209 into main 2026-11-24 04:22:27 +00:00
dicnunz commented 2026-11-24 03:33:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by dicnunz on 2026-11-24T03:33:44Z.


Change

tools/cpu_switch_aot.py (the lifter behind tools/exe_aot.py's console-stdio profile and the CPU fuzz smoke from #894) rejected BT, BTS, BTR and BTC as unsupported instructions. They now lower for 16-, 32- and 64-bit register and memory bit bases with a register or immediate bit offset, and act as flag producers for CF:

  • The offset is taken modulo the operand width, and CF is the selected bit before BTS sets it, BTR clears it or BTC complements it (Intel SDM Vol. 2A "BT/BTC/BTR/BTS"). A 32-bit register destination zero-extends, a 16-bit one keeps bits 16 to 63, and BT writes nothing.
  • The SDM leaves ZF unaffected and OF, SF, AF and PF undefined, so only B and AE (JC, SETNC, CMOVC, ...) may read the flags. Other conditions are refused with only CF is defined after BT|BTS|BTR|BTC.
  • A register offset into a memory base addresses a bit string beyond the operand. It is refused with unsupported bit-string operand. LOCK stays refused.

tools/cpu_sse_aot.py reuses Lifter, so its GPR path gets the same lowering.

Verification

Tested commit: 2a175a8, one commit on 4a47793 (upstream main).

Windows CI on my fork, Windows checks workflow on 2a175a8, windows-2025 runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311580908

Job Result
Build and test on Windows 482 tests, 475 passed, 7 skipped, 0 failed
Build and test on Windows at /O2 482 tests, 475 passed, 7 skipped, 0 failed
Run platform-independent Python checks on Linux passed
Assemble and translate gfx1013 fixtures passed

In both Windows jobs cpu_aot_switch_bit_tests, cpu_aot_switch_rotates, cpu_aot_jump_tables, cpu_aot_flag_conditions, cpu_aot_fuzz_smoke and cpu_aot_fuzz_frames passed. The 7 skips are the capability skips xg_swizzle_select, xg_swizzle_select_mutation, xg_swizzle_factory, xg_swizzle_factory_no_write, bvh_wave64_warp, wave_shuffle_warp and bc7_mode6_multiblock_warp, the same in every run.

ctest --preset dev --no-tests=error -R '^cpu_aot_switch_bit_tests$' -V

Also measured on Linux x86-64, which gives the counts below: the original fixture assembled by llvm-ml 18 and linked by lld-link into a PE32+ DLL, mapped into executable memory and called through a SysV-to-Windows-x64 thunk and the unchanged register_probe.asm; the translation built by clang++ 18 (-fms-extensions, exports ms_abi) at -O0 and -O2; the check script run unchanged:

python -B tests/cpu/switch_bit_tests_check.py <register_probe.dll> <switch_bit_tests.dll> <lifted>
...
31356 differential comparisons passed for BT, BTS, BTR and BTC at 16/32/64 bits
11 rejection controls passed
  • Before the change, the same test fails at translation: unsupported flag producer at RVA 0x1003.
  • Native original = Python reference for all 31,356 calls before any translator change.
  • Planted faults, each detected: immediate offsets masked to six bits at every width (lifted_bt16_imm(0x0, 0xffff)), CF read one bit high (lifted_bt16_reg(0x0, 0x1)).
  • tools/fuzz_cpu.py 0xA902 20 leaf (12,520 comparisons) and 0xF4A3 16 frames (10,016, reload_shift mutant detected) pass with this change.
  • All 35 checks of the Linux Python job pass.

Evidence and follow-up

tests/cpu/switch_bit_tests.asm is original source written for this test (MIT), registered with ; coverage: source cpu_aot_switch_bit_tests. 39 exports: the four instructions at 16/32/64 bits with register offsets (offsets above the width and above 2^32 included), immediate offsets of 0, width minus 1 and above the width, memory bases with an immediate above the width at 32 and 64 bits, JB, JAE, and CMOVC/CMOVNC after a flag-neutral MOV with a 32-bit destination. 10 refused exports (bit string, LOCK, undefined source, and SETE, SETO, SETP, CMOVS, CMOVL, JBE) plus a direct Lifter control. docs/experiments.md documents the rule and the command:

cmake --build --preset dev --target cpu_switch_bit_tests_lifted cpu_register_probe
ctest --preset dev --no-tests=error -R '^cpu_aot_switch_bit_tests$' -V

Follow-up for another contributor: the bit-string form (register offset into memory) needs a signed offset added to the address at operand granularity.

Overlap

The three PRs below touch the same lines of tools/cpu_switch_aot.py (the docstring paragraph after the flag rules, FLAG_PRODUCERS, the start of Lifter.condition(), the branch before shifts in Lifter.lower()), CMakeLists.txt after cpu_aot_switch_rotates and docs/experiments.md after the rotates command. Each is independent and based on main; whichever lands later needs a rebase that keeps both sides (FLAG_PRODUCERS = {..., *SHIFTS, *BIT_TESTS, *BIT_COUNTS}). The combination (e6c6b1e on 4a47793, branch claude/blissful-darwin-l83vcq of my fork) passes all three fixtures and the leaf fuzz area on Linux.

  • cpu: produce flags from SHL, SHR and SAR by an immediate in cpu_switch_aot
  • cpu: lower BT, BTS, BTR and BTC in cpu_switch_aot
  • cpu: lower POPCNT, LZCNT and TZCNT in cpu_switch_aot

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.

This change was AI-assisted (Claude Code), including the fixture, the reference model and the review.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6574008412 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1209). Originally posted by [dicnunz](https://github.com/dicnunz) on 2026-11-24T03:33:44Z. --- ## Change `tools/cpu_switch_aot.py` (the lifter behind `tools/exe_aot.py`'s console-stdio profile and the CPU fuzz smoke from #894) rejected `BT`, `BTS`, `BTR` and `BTC` as unsupported instructions. They now lower for 16-, 32- and 64-bit register and memory bit bases with a register or immediate bit offset, and act as flag producers for CF: - The offset is taken modulo the operand width, and CF is the selected bit before `BTS` sets it, `BTR` clears it or `BTC` complements it (Intel SDM Vol. 2A "BT/BTC/BTR/BTS"). A 32-bit register destination zero-extends, a 16-bit one keeps bits 16 to 63, and `BT` writes nothing. - The SDM leaves ZF unaffected and OF, SF, AF and PF undefined, so only `B` and `AE` (`JC`, `SETNC`, `CMOVC`, ...) may read the flags. Other conditions are refused with `only CF is defined after BT|BTS|BTR|BTC`. - A register offset into a memory base addresses a bit string beyond the operand. It is refused with `unsupported bit-string operand`. `LOCK` stays refused. `tools/cpu_sse_aot.py` reuses `Lifter`, so its GPR path gets the same lowering. ## Verification Tested commit: `2a175a8`, one commit on `4a47793` (upstream `main`). Windows CI on my fork, `Windows checks` workflow on `2a175a8`, `windows-2025` runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311580908 | Job | Result | |---|---| | Build and test on Windows | 482 tests, 475 passed, 7 skipped, 0 failed | | Build and test on Windows at /O2 | 482 tests, 475 passed, 7 skipped, 0 failed | | Run platform-independent Python checks on Linux | passed | | Assemble and translate gfx1013 fixtures | passed | In both Windows jobs `cpu_aot_switch_bit_tests`, `cpu_aot_switch_rotates`, `cpu_aot_jump_tables`, `cpu_aot_flag_conditions`, `cpu_aot_fuzz_smoke` and `cpu_aot_fuzz_frames` passed. The 7 skips are the capability skips `xg_swizzle_select`, `xg_swizzle_select_mutation`, `xg_swizzle_factory`, `xg_swizzle_factory_no_write`, `bvh_wave64_warp`, `wave_shuffle_warp` and `bc7_mode6_multiblock_warp`, the same in every run. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_switch_bit_tests$' -V ``` Also measured on Linux x86-64, which gives the counts below: the original fixture assembled by `llvm-ml` 18 and linked by `lld-link` into a PE32+ DLL, mapped into executable memory and called through a SysV-to-Windows-x64 thunk and the unchanged `register_probe.asm`; the translation built by `clang++` 18 (`-fms-extensions`, exports `ms_abi`) at `-O0` and `-O2`; the check script run unchanged: ``` python -B tests/cpu/switch_bit_tests_check.py <register_probe.dll> <switch_bit_tests.dll> <lifted> ... 31356 differential comparisons passed for BT, BTS, BTR and BTC at 16/32/64 bits 11 rejection controls passed ``` - Before the change, the same test fails at translation: `unsupported flag producer at RVA 0x1003`. - Native original = Python reference for all 31,356 calls before any translator change. - Planted faults, each detected: immediate offsets masked to six bits at every width (`lifted_bt16_imm(0x0, 0xffff)`), CF read one bit high (`lifted_bt16_reg(0x0, 0x1)`). - `tools/fuzz_cpu.py` `0xA902 20 leaf` (12,520 comparisons) and `0xF4A3 16 frames` (10,016, `reload_shift` mutant detected) pass with this change. - All 35 checks of the Linux Python job pass. ## Evidence and follow-up `tests/cpu/switch_bit_tests.asm` is original source written for this test (MIT), registered with `; coverage: source cpu_aot_switch_bit_tests`. 39 exports: the four instructions at 16/32/64 bits with register offsets (offsets above the width and above 2^32 included), immediate offsets of 0, width minus 1 and above the width, memory bases with an immediate above the width at 32 and 64 bits, `JB`, `JAE`, and `CMOVC`/`CMOVNC` after a flag-neutral `MOV` with a 32-bit destination. 10 refused exports (bit string, `LOCK`, undefined source, and `SETE`, `SETO`, `SETP`, `CMOVS`, `CMOVL`, `JBE`) plus a direct `Lifter` control. `docs/experiments.md` documents the rule and the command: ```powershell cmake --build --preset dev --target cpu_switch_bit_tests_lifted cpu_register_probe ctest --preset dev --no-tests=error -R '^cpu_aot_switch_bit_tests$' -V ``` Follow-up for another contributor: the bit-string form (register offset into memory) needs a signed offset added to the address at operand granularity. ## Overlap The three PRs below touch the same lines of `tools/cpu_switch_aot.py` (the docstring paragraph after the flag rules, `FLAG_PRODUCERS`, the start of `Lifter.condition()`, the branch before shifts in `Lifter.lower()`), `CMakeLists.txt` after `cpu_aot_switch_rotates` and `docs/experiments.md` after the rotates command. Each is independent and based on `main`; whichever lands later needs a rebase that keeps both sides (`FLAG_PRODUCERS = {..., *SHIFTS, *BIT_TESTS, *BIT_COUNTS}`). The combination (`e6c6b1e` on `4a47793`, branch `claude/blissful-darwin-l83vcq` of my fork) passes all three fixtures and the leaf fuzz area on Linux. - cpu: produce flags from SHL, SHR and SAR by an immediate in cpu_switch_aot - cpu: lower BT, BTS, BTR and BTC in cpu_switch_aot - cpu: lower POPCNT, LZCNT and TZCNT in cpu_switch_aot ## 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. This change was AI-assisted (Claude Code), including the fixture, the reference model and the review. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:46:56 +00:00
tedin7 commented 2026-11-24 06:46:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T03:43:31Z.

Review: COMMENTED


Reviewed 2a175a8d on Linux (Windows build/test and native/AOT execution were not run locally).

Diff audit

  • tools/cpu_switch_aot.py: lowers BT, BTS, BTR, and BTC for 16/32/64-bit register bases (register or immediate offset masked to bits - 1) and memory bases with immediate offset (masked to bits - 1).
    • Correctly rejects memory + register operands (unsupported bit-string operand) rather than indexing a signed bit string beyond the loaded operand, and LOCK remains rejected (unsupported prefix).
    • Captures CF in fb before modifying the bit; BT clears writes. condition() restricts consumers to B/AE (only CF is defined after ...).
  • docs/experiments.md, CMakeLists.txt, tests/cpu/switch_bit_tests.{asm,def,_check.py}: consistent and title-neutral.
  • Note: overlaps in tools/cpu_switch_aot.py, CMakeLists.txt, and docs/experiments.md with #1208 and #1210; whichever lands later will need a rebase.

Linux checks (/root/.local/share/x2w-rev-venv/bin/python -B)

  • tests/form_coverage_check.py: passed
  • tests/docs_links_check.py: passed (15 Markdown files, 257 links)
  • tests/bidi_controls_check.py: passed (957 files)
  • tests/intake/{import_forms,paths,routing}_check.py: passed
  • cpu_switch_aot.Lifter().lower(..., sets_flags=True) spot checks (4 cases: bt eax,ecx, bt [rax],ecx refused, bt dword ptr [rax], 39, bts eax,ecx): passed.
  • Windows build and ctest -R '^cpu_aot_switch_bit_tests$' not run locally.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033329107 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1209#pullrequestreview-6033329107). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T03:43:31Z. **Review:** COMMENTED --- Reviewed `2a175a8d` on Linux (Windows build/test and native/AOT execution were not run locally). ### Diff audit - `tools/cpu_switch_aot.py`: lowers `BT`, `BTS`, `BTR`, and `BTC` for 16/32/64-bit register bases (register or immediate offset masked to `bits - 1`) and memory bases with immediate offset (masked to `bits - 1`). - Correctly rejects `memory + register` operands (`unsupported bit-string operand`) rather than indexing a signed bit string beyond the loaded operand, and `LOCK` remains rejected (`unsupported prefix`). - Captures `CF` in `fb` before modifying the bit; `BT` clears `writes`. `condition()` restricts consumers to `B`/`AE` (`only CF is defined after ...`). - `docs/experiments.md`, `CMakeLists.txt`, `tests/cpu/switch_bit_tests.{asm,def,_check.py}`: consistent and title-neutral. - Note: overlaps in `tools/cpu_switch_aot.py`, `CMakeLists.txt`, and `docs/experiments.md` with #1208 and #1210; whichever lands later will need a rebase. ### Linux checks (`/root/.local/share/x2w-rev-venv/bin/python -B`) - `tests/form_coverage_check.py`: passed - `tests/docs_links_check.py`: passed (15 Markdown files, 257 links) - `tests/bidi_controls_check.py`: passed (957 files) - `tests/intake/{import_forms,paths,routing}_check.py`: passed - `cpu_switch_aot.Lifter().lower(..., sets_flags=True)` spot checks (4 cases: `bt eax,ecx`, `bt [rax],ecx` refused, `bt dword ptr [rax], 39`, `bts eax,ecx`): passed. - Windows build and `ctest -R '^cpu_aot_switch_bit_tests$'` not run 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!1202
No description provided.