cpu: lower POPCNT, LZCNT and TZCNT in cpu_switch_aot #1203

Merged
dicnunz merged 2 commits from upstream-pr/1210 into main 2026-11-24 04:30:30 +00:00
dicnunz commented 2026-11-24 03:34:03 +00:00 (Migrated from github.com)

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


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 POPCNT, LZCNT and TZCNT. They now lower for 16-, 32- and 64-bit register and memory sources through three portable PRELUDE helpers (count_ones, leading_zeros, trailing_zeros), and act as flag producers:

  • LZCNT and TZCNT return the operand width for a zero source.
  • POPCNT sets ZF for a zero source and clears OF, SF, AF, CF and PF (Intel SDM Vol. 2B "POPCNT"), so all 16 conditions may read it.
  • LZCNT and TZCNT set CF for a zero source and ZF for a zero result, and leave OF, SF, AF and PF undefined (SDM "LZCNT", "TZCNT"). Only B, AE, E, NE, BE and A may read them; other conditions are refused with OF, SF and PF are undefined after LZCNT|TZCNT.
  • BSF and BSR, whose destination the SDM leaves undefined for a zero source, stay unsupported.

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

Verification

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

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

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_counts, 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_counts$' -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_counts_check.py <register_probe.dll> <switch_bit_counts.dll> <lifted>
9 rejection controls passed
...
19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits
  • Before the change, the same test fails at translation: unsupported flag producer at RVA 0x1003.
  • Native original = Python reference for all 19,734 calls before any translator change.
  • Planted faults, each detected: TZCNT of 0 one short of the width (lifted_tzcnt16_reg(0x0, 0x0)), CF taken from the result after LZCNT (lifted_lzcnt16_reg(0x0, 0x0)).
  • 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.

A host whose CPUID lacks POPCNT, BMI1 or ABM runs LZCNT/TZCNT as BSR/BSF, so the native original would not match the reference there. The fixture exports host_bit_counts (native only, CPUID leaves 1, 7 and 0x80000001), and the test returns 77 (SKIP_RETURN_CODE), as cpu_aot_bit_ops does.

Evidence and follow-up

tests/cpu/switch_bit_counts.asm is original source written for this test (MIT), registered with ; coverage: source cpu_aot_switch_bit_counts. 22 exports: the three instructions at 16/32/64 bits with register and memory sources, every permitted condition through SETcc, JB, JE and JNE, and CMOVA and a 32-bit CMOVLE after a flag-neutral MOV. 9 refused exports, including BSF and BSR. docs/experiments.md documents the rule and the command:

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

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:6574011707 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1210). Originally posted by [dicnunz](https://github.com/dicnunz) on 2026-11-24T03:34:03Z. --- ## 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 `POPCNT`, `LZCNT` and `TZCNT`. They now lower for 16-, 32- and 64-bit register and memory sources through three portable `PRELUDE` helpers (`count_ones`, `leading_zeros`, `trailing_zeros`), and act as flag producers: - `LZCNT` and `TZCNT` return the operand width for a zero source. - `POPCNT` sets ZF for a zero source and clears OF, SF, AF, CF and PF (Intel SDM Vol. 2B "POPCNT"), so all 16 conditions may read it. - `LZCNT` and `TZCNT` set CF for a zero source and ZF for a zero result, and leave OF, SF, AF and PF undefined (SDM "LZCNT", "TZCNT"). Only `B`, `AE`, `E`, `NE`, `BE` and `A` may read them; other conditions are refused with `OF, SF and PF are undefined after LZCNT|TZCNT`. - `BSF` and `BSR`, whose destination the SDM leaves undefined for a zero source, stay unsupported. `tools/cpu_sse_aot.py` reuses `Lifter`, so its GPR path gets the same lowering. ## Verification Tested commit: `f1d2c95`, one commit on `4a47793` (upstream `main`). Windows CI on my fork, `Windows checks` workflow on `f1d2c95`, `windows-2025` runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311584822 | 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_counts`, `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_counts$' -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_counts_check.py <register_probe.dll> <switch_bit_counts.dll> <lifted> 9 rejection controls passed ... 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits ``` - Before the change, the same test fails at translation: `unsupported flag producer at RVA 0x1003`. - Native original = Python reference for all 19,734 calls before any translator change. - Planted faults, each detected: `TZCNT` of 0 one short of the width (`lifted_tzcnt16_reg(0x0, 0x0)`), CF taken from the result after `LZCNT` (`lifted_lzcnt16_reg(0x0, 0x0)`). - `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. A host whose CPUID lacks POPCNT, BMI1 or ABM runs `LZCNT`/`TZCNT` as `BSR`/`BSF`, so the native original would not match the reference there. The fixture exports `host_bit_counts` (native only, CPUID leaves 1, 7 and 0x80000001), and the test returns 77 (`SKIP_RETURN_CODE`), as `cpu_aot_bit_ops` does. ## Evidence and follow-up `tests/cpu/switch_bit_counts.asm` is original source written for this test (MIT), registered with `; coverage: source cpu_aot_switch_bit_counts`. 22 exports: the three instructions at 16/32/64 bits with register and memory sources, every permitted condition through `SETcc`, `JB`, `JE` and `JNE`, and `CMOVA` and a 32-bit `CMOVLE` after a flag-neutral `MOV`. 9 refused exports, including `BSF` and `BSR`. `docs/experiments.md` documents the rule and the command: ```powershell cmake --build --preset dev --target cpu_switch_bit_counts_lifted cpu_register_probe ctest --preset dev --no-tests=error -R '^cpu_aot_switch_bit_counts$' -V ``` ## 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:57 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

Diff audit

  • tools/cpu_switch_aot.py: lowers POPCNT, LZCNT, and TZCNT for 16/32/64-bit register or memory sources (note(1) hoists memory reads; self.write handles 32→64-bit zero-extension and 16-bit upper-byte preservation).
    • LZCNT/TZCNT of 0 return sizeof(T) * 8.
    • POPCNT sets ZF from source == 0 and clears OF/SF/AF/CF/PF (all 16 conditions supported). LZCNT/TZCNT set CF from source == 0 and ZF from result == 0, restricting conditions to B/AE/E/NE/BE/A and rejecting OF/SF/PF conditions. BSF/BSR remain unsupported.
  • CMakeLists.txt: configures SKIP_RETURN_CODE 77 for hosts lacking POPCNT/BMI1/ABM.
  • docs/experiments.md, tests/cpu/switch_bit_counts.{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 #1209; 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 (5 cases: popcnt, lzcnt, tzcnt, bsf refused, bsr refused): passed.
  • Windows build and ctest -R '^cpu_aot_switch_bit_counts$' not run locally.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033329245 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1210#pullrequestreview-6033329245). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T03:43:32Z. **Review:** COMMENTED --- Reviewed `f1d2c956` on Linux (Windows build/test and native/AOT execution were not run locally). ### Diff audit - `tools/cpu_switch_aot.py`: lowers `POPCNT`, `LZCNT`, and `TZCNT` for 16/32/64-bit register or memory sources (`note(1)` hoists memory reads; `self.write` handles 32→64-bit zero-extension and 16-bit upper-byte preservation). - `LZCNT`/`TZCNT` of `0` return `sizeof(T) * 8`. - `POPCNT` sets `ZF` from `source == 0` and clears `OF/SF/AF/CF/PF` (all 16 conditions supported). `LZCNT`/`TZCNT` set `CF` from `source == 0` and `ZF` from `result == 0`, restricting conditions to `B/AE/E/NE/BE/A` and rejecting `OF/SF/PF` conditions. `BSF`/`BSR` remain unsupported. - `CMakeLists.txt`: configures `SKIP_RETURN_CODE 77` for hosts lacking `POPCNT`/`BMI1`/`ABM`. - `docs/experiments.md`, `tests/cpu/switch_bit_counts.{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 #1209; 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 (5 cases: `popcnt`, `lzcnt`, `tzcnt`, `bsf` refused, `bsr` refused): passed. - Windows build and `ctest -R '^cpu_aot_switch_bit_counts$'` not run locally.
tedin7 commented 2026-11-24 06:50:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:21:53Z.


Independent Windows retest on commit f1d2c956592ec42d8741a2ee53b4ab0f386e07b2 (pull/1210/head):

Environment

  • OS: Windows Server 2025 Build 26100
  • Compiler: MSVC 14.44.35207 (cl.exe / ml64.exe x64)
  • CPU: AMD Ryzen 5 PRO 5650U (Zen 3)
  • GPU driver: Microsoft Basic Render Driver (WARP)

Commands & Results

  1. Debug (dev preset):

    cmake --build --preset dev --target cpu_register_probe cpu_switch_bit_counts cpu_switch_bit_counts_lifted cpu_switch_original cpu_switch_original_lifted cpu_switch_swapped_lifted -- -j 6
    ctest --preset dev -R "^(cpu_aot_switch_bit_counts|cpu_aot_jump_tables)$" --no-tests=error --output-on-failure -V
    

    Result: 2/2 passed:

    • cpu_aot_jump_tables: Passed (1.11 sec)
    • cpu_aot_switch_bit_counts: Passed (0.66 sec; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits)
  2. Release (/O2, release preset):

    cmake --build --preset release --target cpu_register_probe cpu_switch_bit_counts cpu_switch_bit_counts_lifted cpu_switch_original cpu_switch_original_lifted cpu_switch_swapped_lifted -- -j 6
    ctest --preset release -R "^(cpu_aot_switch_bit_counts|cpu_aot_jump_tables)$" --no-tests=error --output-on-failure -V
    

    Result: 2/2 passed:

    • cpu_aot_jump_tables: Passed (1.05 sec)
    • cpu_aot_switch_bit_counts: Passed (0.55 sec; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits)

0 failed, 0 skipped.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7121950271 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1210#issuecomment-7121950271). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T04:21:53Z. --- Independent Windows retest on commit `f1d2c956592ec42d8741a2ee53b4ab0f386e07b2` (`pull/1210/head`): **Environment** - OS: Windows Server 2025 Build 26100 - Compiler: MSVC `14.44.35207` (`cl.exe` / `ml64.exe` x64) - CPU: AMD Ryzen 5 PRO 5650U (Zen 3) - GPU driver: Microsoft Basic Render Driver (`WARP`) **Commands & Results** 1. **Debug (`dev` preset):** ```powershell cmake --build --preset dev --target cpu_register_probe cpu_switch_bit_counts cpu_switch_bit_counts_lifted cpu_switch_original cpu_switch_original_lifted cpu_switch_swapped_lifted -- -j 6 ctest --preset dev -R "^(cpu_aot_switch_bit_counts|cpu_aot_jump_tables)$" --no-tests=error --output-on-failure -V ``` Result: **2/2 passed**: - `cpu_aot_jump_tables`: Passed (`1.11 sec`) - `cpu_aot_switch_bit_counts`: Passed (`0.66 sec`; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits) 2. **Release (`/O2`, `release` preset):** ```powershell cmake --build --preset release --target cpu_register_probe cpu_switch_bit_counts cpu_switch_bit_counts_lifted cpu_switch_original cpu_switch_original_lifted cpu_switch_swapped_lifted -- -j 6 ctest --preset release -R "^(cpu_aot_switch_bit_counts|cpu_aot_jump_tables)$" --no-tests=error --output-on-failure -V ``` Result: **2/2 passed**: - `cpu_aot_jump_tables`: Passed (`1.05 sec`) - `cpu_aot_switch_bit_counts`: Passed (`0.55 sec`; 9 rejection controls passed, 19734 differential comparisons passed for POPCNT, LZCNT and TZCNT at 16/32/64 bits) 0 failed, 0 skipped.
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!1203
No description provided.