cpu: produce flags from SHL, SHR and SAR by an immediate in cpu_switch_aot #1201

Merged
dicnunz merged 1 commit from upstream-pr/1208 into main 2026-11-24 04:15:08 +00:00
dicnunz commented 2026-11-24 03:33:23 +00:00 (Migrated from github.com)

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


Change

tools/cpu_switch_aot.py (the lifter behind tools/exe_aot.py's console-stdio profile and the CPU fuzz smoke from #894) refused every shift as a flag producer, so shr eax, 1; jb, shl rax, 1; jo or sar ecx, 4; js stopped a translation. Jcc, SETcc and CMOVcc may now read the flags of SHL, SHR and SAR by an immediate whose masked count (five bits, six for 64-bit) is 1 to the operand width minus 1:

  • CF is the last bit shifted out; ZF, SF and PF come from the truncated result.
  • OF is modelled only for a count of 1: MSB(result) XOR CF for SHL, MSB(operand) for SHR, 0 for SAR (Intel SDM Vol. 2B "SAL/SAR/SHL/SHR—Shift", AMD APM Vol. 3 SAL/SAR/SHL/SHR).
  • Still refused, each with a message that keeps the existing unsupported flag producer prefix: a shift by CL (a masked count of 0 would pass the earlier flags through), a masked count of 0, and a count of at least the operand width (CF undefined for SHL/SHR; SAR is refused with them). O, NO, L, GE, LE and G after a count above 1 are refused with OF is undefined after a shift by more than 1. Rotates are unchanged.

tools/cpu_sse_aot.py reuses Lifter and FLAG_PRODUCERS, so its GPR path accepts the same immediate shifts before a Jcc; CL shifts there are still refused with unsupported flag producer.

One existing control changes: tests/cpu/switch_check.py placed SHL EAX, 1 between CMP and CMOVGE and expected a refusal. That shift is now a valid producer, so the control places ROL EAX, 1 (d1 c0), which also writes CF and OF and is still refused, keeping what the control checks.

Verification

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

Windows CI on my fork, Windows checks workflow on e91c53e, windows-2025 runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311577260 The first attempt's Build and test on Windows job was cancelled by the job's 20-minute timeout at test 460/482 with no failure (every test it reached, cpu_aot_switch_shift_flags included, had passed); attempt 2 reran it. The same timeout cancelled both Windows jobs of the fork's main run at 4a47793 that started alongside, so it is runner time, not this change.

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_shift_flags, 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_shift_flags$' -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_shift_flags_check.py <register_probe.dll> <switch_shift_flags.dll> <lifted>
...
28548 differential comparisons passed for SHL, SHR and SAR flags at 8/16/32/64 bits
16 rejection controls passed
  • Before the change, the same test fails at translation: unsupported flag producer at RVA 0x1003.
  • Native original = Python reference for all 28,548 calls before any translator change.
  • Planted faults, each detected: CF one bit off for SHR/SAR (lifted_shr1_8(0x0, 0x1)), OF taken as the result MSB for SHL (lifted_shl1_8(0x0, 0x40)).
  • Same harness, existing tests: cpu_aot_switch_rotates (8,363 comparisons, 12 controls) passes; tools/fuzz_cpu.py 0xA902 20 leaf (12,520 comparisons) and 0xF4A3 16 frames (10,016 comparisons, reload_shift mutant 4,945/5,008 mismatches) pass.
  • SHL RAX, CL (the flag_conditions_check.py control) still raises unsupported flag producer ....
  • All 35 checks of the Linux Python job pass, including docs_links_check.py and form_coverage_check.py.
  • An independent review generated every accepted shift/condition form at 8/16/32/64 bits (counts 0–69, 0x81, 0xff) and compared with pushfq on hardware: 2,388,000 comparisons, 0 mismatches, also under -fsanitize=undefined.

Evidence and follow-up

tests/cpu/switch_shift_flags.asm is original source written for this test (MIT), registered with ; coverage: source cpu_aot_switch_shift_flags. 36 exports: every condition after a count of 1 and the ten OF-free conditions after larger counts up to the width minus 1, 8/16/32/64-bit register and memory destinations, masked counts (sar al, 33, sar eax, 37, shl r10, 65), seven Jcc consumers and CMOVcc after a flag-neutral MOV, with a 32-bit destination and a memory source. 13 refused exports plus 3 direct Lifter controls. docs/experiments.md documents the rule and the reproduction command:

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

Follow-up for another contributor: shifts by CL as producers would need the earlier producer's flags when the masked count is 0.

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:6574004537 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1208). Originally posted by [dicnunz](https://github.com/dicnunz) on 2026-11-24T03:33:23Z. --- ## Change `tools/cpu_switch_aot.py` (the lifter behind `tools/exe_aot.py`'s console-stdio profile and the CPU fuzz smoke from #894) refused every shift as a flag producer, so `shr eax, 1; jb`, `shl rax, 1; jo` or `sar ecx, 4; js` stopped a translation. `Jcc`, `SETcc` and `CMOVcc` may now read the flags of `SHL`, `SHR` and `SAR` by an immediate whose masked count (five bits, six for 64-bit) is 1 to the operand width minus 1: - CF is the last bit shifted out; ZF, SF and PF come from the truncated result. - OF is modelled only for a count of 1: MSB(result) XOR CF for `SHL`, MSB(operand) for `SHR`, 0 for `SAR` (Intel SDM Vol. 2B "SAL/SAR/SHL/SHR—Shift", AMD APM Vol. 3 SAL/SAR/SHL/SHR). - Still refused, each with a message that keeps the existing `unsupported flag producer` prefix: a shift by `CL` (a masked count of 0 would pass the earlier flags through), a masked count of 0, and a count of at least the operand width (CF undefined for `SHL`/`SHR`; `SAR` is refused with them). `O`, `NO`, `L`, `GE`, `LE` and `G` after a count above 1 are refused with `OF is undefined after a shift by more than 1`. Rotates are unchanged. `tools/cpu_sse_aot.py` reuses `Lifter` and `FLAG_PRODUCERS`, so its GPR path accepts the same immediate shifts before a `Jcc`; CL shifts there are still refused with `unsupported flag producer`. One existing control changes: `tests/cpu/switch_check.py` placed `SHL EAX, 1` between `CMP` and `CMOVGE` and expected a refusal. That shift is now a valid producer, so the control places `ROL EAX, 1` (`d1 c0`), which also writes CF and OF and is still refused, keeping what the control checks. ## Verification Tested commit: `e91c53e`, one commit on `4a47793` (upstream `main`). Windows CI on my fork, `Windows checks` workflow on `e91c53e`, `windows-2025` runner, MSVC 19.52.37029.0 and ML64 14.52.37029.0: https://github.com/dicnunz/Xbox2Windows/actions/runs/45311577260 The first attempt's `Build and test on Windows` job was cancelled by the job's 20-minute timeout at test 460/482 with no failure (every test it reached, `cpu_aot_switch_shift_flags` included, had passed); attempt 2 reran it. The same timeout cancelled both Windows jobs of the fork's `main` run at `4a47793` that started alongside, so it is runner time, not this change. | 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_shift_flags`, `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_shift_flags$' -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_shift_flags_check.py <register_probe.dll> <switch_shift_flags.dll> <lifted> ... 28548 differential comparisons passed for SHL, SHR and SAR flags at 8/16/32/64 bits 16 rejection controls passed ``` - Before the change, the same test fails at translation: `unsupported flag producer at RVA 0x1003`. - Native original = Python reference for all 28,548 calls before any translator change. - Planted faults, each detected: CF one bit off for `SHR`/`SAR` (`lifted_shr1_8(0x0, 0x1)`), OF taken as the result MSB for `SHL` (`lifted_shl1_8(0x0, 0x40)`). - Same harness, existing tests: `cpu_aot_switch_rotates` (8,363 comparisons, 12 controls) passes; `tools/fuzz_cpu.py` `0xA902 20 leaf` (12,520 comparisons) and `0xF4A3 16 frames` (10,016 comparisons, `reload_shift` mutant 4,945/5,008 mismatches) pass. - `SHL RAX, CL` (the `flag_conditions_check.py` control) still raises `unsupported flag producer ...`. - All 35 checks of the Linux Python job pass, including `docs_links_check.py` and `form_coverage_check.py`. - An independent review generated every accepted shift/condition form at 8/16/32/64 bits (counts 0–69, 0x81, 0xff) and compared with `pushfq` on hardware: 2,388,000 comparisons, 0 mismatches, also under `-fsanitize=undefined`. ## Evidence and follow-up `tests/cpu/switch_shift_flags.asm` is original source written for this test (MIT), registered with `; coverage: source cpu_aot_switch_shift_flags`. 36 exports: every condition after a count of 1 and the ten OF-free conditions after larger counts up to the width minus 1, 8/16/32/64-bit register and memory destinations, masked counts (`sar al, 33`, `sar eax, 37`, `shl r10, 65`), seven `Jcc` consumers and `CMOVcc` after a flag-neutral `MOV`, with a 32-bit destination and a memory source. 13 refused exports plus 3 direct `Lifter` controls. `docs/experiments.md` documents the rule and the reproduction command: ```powershell cmake --build --preset dev --target cpu_switch_shift_flags_lifted cpu_register_probe ctest --preset dev --no-tests=error -R '^cpu_aot_switch_shift_flags$' -V ``` Follow-up for another contributor: shifts by `CL` as producers would need the earlier producer's flags when the masked count is 0. ## 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:55 +00:00
tedin7 commented 2026-11-24 06:46:55 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

Diff audit

  • tools/cpu_switch_aot.py: SHL, SHR, and SAR by an immediate are added to FLAG_PRODUCERS when the masked count (& 31, or & 63 for 64-bit) is 1 .. width * 8 - 1.
    • CF captures the last bit shifted out ((wide >> (bits - n)) & 1u for SHL, (wide >> (n - 1)) & 1u for SHR/SAR); ZF, SF, and PF are computed from the width-truncated fa.
    • OF is modeled only for count == 1 (MSB(result) ^ CF for SHL, MSB(operand) for SHR, 0 for SAR), and O/NO/L/GE/LE/G after count > 1 are rejected with OF is undefined after a shift by more than 1.
    • Shifts by CL, masked count 0, and counts >= width * 8 remain rejected with unsupported flag producer.
  • tests/cpu/switch_check.py: updates the CMP+CMOVGE negative control from SHL EAX, 1 (d1 e0) to ROL EAX, 1 (d1 c0), preserving the unsupported flag producer check.
  • docs/experiments.md, CMakeLists.txt, tests/cpu/switch_shift_flags.{asm,def,_check.py}: consistent and title-neutral.
  • Note: overlaps in tools/cpu_switch_aot.py, CMakeLists.txt, and docs/experiments.md with #1209 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 (6 cases: shl eax,1, shl eax,2, shr eax,32, shl al,8, shl eax,cl, rol eax,1): passed.
  • Windows build and ctest -R '^cpu_aot_switch_shift_flags$' not run locally.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033328949 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1208#pullrequestreview-6033328949). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T03:43:29Z. **Review:** COMMENTED --- Reviewed `e91c53ed` on Linux (Windows build/test and native/AOT execution were not run locally). ### Diff audit - `tools/cpu_switch_aot.py`: `SHL`, `SHR`, and `SAR` by an immediate are added to `FLAG_PRODUCERS` when the masked count (`& 31`, or `& 63` for 64-bit) is `1 .. width * 8 - 1`. - `CF` captures the last bit shifted out (`(wide >> (bits - n)) & 1u` for `SHL`, `(wide >> (n - 1)) & 1u` for `SHR`/`SAR`); `ZF`, `SF`, and `PF` are computed from the width-truncated `fa`. - `OF` is modeled only for `count == 1` (`MSB(result) ^ CF` for `SHL`, `MSB(operand)` for `SHR`, `0` for `SAR`), and `O`/`NO`/`L`/`GE`/`LE`/`G` after `count > 1` are rejected with `OF is undefined after a shift by more than 1`. - Shifts by `CL`, masked count `0`, and counts `>= width * 8` remain rejected with `unsupported flag producer`. - `tests/cpu/switch_check.py`: updates the `CMP`+`CMOVGE` negative control from `SHL EAX, 1` (`d1 e0`) to `ROL EAX, 1` (`d1 c0`), preserving the `unsupported flag producer` check. - `docs/experiments.md`, `CMakeLists.txt`, `tests/cpu/switch_shift_flags.{asm,def,_check.py}`: consistent and title-neutral. - Note: overlaps in `tools/cpu_switch_aot.py`, `CMakeLists.txt`, and `docs/experiments.md` with #1209 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 (6 cases: `shl eax,1`, `shl eax,2`, `shr eax,32`, `shl al,8`, `shl eax,cl`, `rol eax,1`): passed. - Windows build and `ctest -R '^cpu_aot_switch_shift_flags$'` 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!1201
No description provided.