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

Manually merged
github merged 2 commits from gh-pr/1209/head into gh-pr/1209/base 2026-11-26 22:49:06 +00:00
Owner

@⁠dicnunz on GitHub · 2026-11-24 03:33 UTC · original
dicnunz:cpu/switch-bit-tests → main · +808 −1 · 6 files · 2 commits · merged 2026-11-24 04:22 UTC

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

**@⁠dicnunz** on GitHub · 2026-11-24 03:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1209) `dicnunz:cpu/switch-bit-tests` → `main` · +808 −1 · 6 files · 2 commits · merged 2026-11-24 04:22 UTC ## 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)
BT, BTS, BTR and BTC now lower for 16-, 32- and 64-bit register and
memory bit bases with a register or immediate offset. The offset is
taken modulo the operand width and CF is the selected bit before the
write, as the Intel SDM defines them. ZF is unaffected and OF, SF, AF
and PF are undefined, so only B and AE may read the flags. A register
offset into memory addresses a bit string beyond the operand and stays
refused, as does LOCK.

cpu_aot_switch_bit_tests compares 39 exports with the original and a
Python reference over 31,356 inputs and checks 11 rejection controls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uAfgu4RHYzwuB2pDNtbFe
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:49:05 +00:00
github manually merged commit 74001fdcfa into gh-pr/1209/base 2026-11-26 22:49:06 +00:00
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 03:43 UTC · original · reviewed

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.
**@⁠tedin7** on GitHub · 2026-11-24 03:43 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1209#pullrequestreview-6033329107) · **reviewed** 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.