cpu: lift BSWAP in the switch translator #1367

Merged
tedin7 merged 1 commit from upstream-pr/1374 into main 2026-11-24 09:57:47 +00:00
tedin7 commented 2026-11-24 09:40:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T09:40:08Z.


Change

tools/cpu_switch_aot.py now lifts BSWAP of a 32- or 64-bit register. The bytes are reversed in place, a 32-bit result zero-extends into the 64-bit register like any 32-bit write, and no flag changes, so a SETcc, Jcc or CMOVcc after it reads the flags of the instruction before it. The 16-bit form (operand-size prefix) is refused because the Intel SDM leaves its result undefined, as are BSWAP RSP (a dynamic frame), an unwritten register and a repeat prefix.

switch_bit_counts used BSWAP as a rejection control; it now uses XLATB, which the translator still refuses. No other behavior changes.

AI-assisted. No store or atomic is involved, so there is no memory-window question.

Verification

Windows (not run by me): ctest --preset dev -R '^cpu_aot_switch_(bswap|bit_counts)$' and the same with --preset release. This test is outside --preset pr.

Linux x86-64 (what ran), with llvm-ml-19, lld-link-19 and clang++-19 at -O0 and -O2. The original PE was mapped into memory and called through the Windows x64 convention together with the register probe, since Wine has no 32-bit support on this machine:

  • switch_bswap_check.py: 33 exports x 829 inputs = 27,357 comparisons of the original, the translation and a Python reference (int.to_bytes / int.from_bytes), 4 rejection controls, 0 mismatches at both optimization levels. Register probe sentinels (RBX, RSI) match.
  • Fails on origin/main: the same fixture is refused with unsupported flag producer because BSWAP is not lifted.
  • Five faults run as references each disagree with the original in their own words only: swap of 32 bits for a 64-bit register, upper half kept for a 32-bit register, 16-bit words swapped instead of bytes, register left unswapped (words 0 and 4); flags taken from the result (words 2 and 3).
  • The same five faults planted in the generated C++ (32-bit-only swap for r64, no zero extension for r32, 16-bit word order, a wrong destination register in bswap64_rdx, flags overwritten after BSWAP) each fail at the first input that exposes them.
  • switch_bit_counts_check.check_rejections: 8 of 8 controls pass.
  • py_compile of the three Python files, tests/docs_links_check.py, tests/bidi_controls_check.py, git diff --check, x2w_pipe.py preflight: pass.

Not run: MSVC, ML64, Wine, Intel hardware, the other cpu_aot_* Windows tests.

Evidence and follow-up

The fixture is original assembly written for this change (MIT, no external input). It covers 15 registers (RAX to R15 without RSP) at 32 and 64 bits, each between two CMPs whose sixteen conditions are stored, plus a 64-then-32-bit sequence and JB and CMOVL after BSWAP. Inputs include zero, one, 0x80, 0x0123456789abcdef, all ones, single bits and values with zero upper bytes. docs/experiments.md documents the new capability and its measurements.

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:6578899396 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1374). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T09:40:08Z. --- ## Change `tools/cpu_switch_aot.py` now lifts `BSWAP` of a 32- or 64-bit register. The bytes are reversed in place, a 32-bit result zero-extends into the 64-bit register like any 32-bit write, and no flag changes, so a `SETcc`, `Jcc` or `CMOVcc` after it reads the flags of the instruction before it. The 16-bit form (operand-size prefix) is refused because the Intel SDM leaves its result undefined, as are `BSWAP RSP` (a dynamic frame), an unwritten register and a repeat prefix. `switch_bit_counts` used `BSWAP` as a rejection control; it now uses `XLATB`, which the translator still refuses. No other behavior changes. AI-assisted. No store or atomic is involved, so there is no memory-window question. ## Verification Windows (not run by me): `ctest --preset dev -R '^cpu_aot_switch_(bswap|bit_counts)$'` and the same with `--preset release`. This test is outside `--preset pr`. Linux x86-64 (what ran), with `llvm-ml-19`, `lld-link-19` and `clang++-19` at `-O0` and `-O2`. The original PE was mapped into memory and called through the Windows x64 convention together with the register probe, since Wine has no 32-bit support on this machine: - `switch_bswap_check.py`: 33 exports x 829 inputs = 27,357 comparisons of the original, the translation and a Python reference (`int.to_bytes` / `int.from_bytes`), 4 rejection controls, 0 mismatches at both optimization levels. Register probe sentinels (RBX, RSI) match. - Fails on `origin/main`: the same fixture is refused with `unsupported flag producer` because `BSWAP` is not lifted. - Five faults run as references each disagree with the original in their own words only: swap of 32 bits for a 64-bit register, upper half kept for a 32-bit register, 16-bit words swapped instead of bytes, register left unswapped (words 0 and 4); flags taken from the result (words 2 and 3). - The same five faults planted in the generated C++ (32-bit-only swap for r64, no zero extension for r32, 16-bit word order, a wrong destination register in `bswap64_rdx`, flags overwritten after `BSWAP`) each fail at the first input that exposes them. - `switch_bit_counts_check.check_rejections`: 8 of 8 controls pass. - `py_compile` of the three Python files, `tests/docs_links_check.py`, `tests/bidi_controls_check.py`, `git diff --check`, `x2w_pipe.py preflight`: pass. Not run: MSVC, ML64, Wine, Intel hardware, the other `cpu_aot_*` Windows tests. ## Evidence and follow-up The fixture is original assembly written for this change (MIT, no external input). It covers 15 registers (RAX to R15 without RSP) at 32 and 64 bits, each between two `CMP`s whose sixteen conditions are stored, plus a 64-then-32-bit sequence and `JB` and `CMOVL` after `BSWAP`. Inputs include zero, one, 0x80, 0x0123456789abcdef, all ones, single bits and values with zero upper bytes. `docs/experiments.md` documents the new capability and its measurements. ## 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-24 10:02:35 +00:00
tedin7 commented 2026-11-24 10:02:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T09:44:19Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit 384ee4acec19ed0a62f76b5633326f316424dcdc:

  • Debug (--preset dev):
    • Build (cpu_switch_bswap*, cpu_switch_bit_counts*): 3.57s (configure 1.53s)
    • Test (ctest --preset dev -R '^(cpu_aot_switch_bswap|cpu_aot_switch_bit_counts)$' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (cpu_aot_switch_bit_counts 0.52s, cpu_aot_switch_bswap 0.91s)
  • Release /O2 (--preset release):
    • Build (cpu_switch_bswap*, cpu_switch_bit_counts*): 0.95s (configure 1.64s)
    • Test (ctest --preset release -R '^(cpu_aot_switch_bswap|cpu_aot_switch_bit_counts)$' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (cpu_aot_switch_bit_counts 0.49s, cpu_aot_switch_bswap 0.85s)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127821012 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1374#issuecomment-7127821012). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T09:44:19Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit `384ee4acec19ed0a62f76b5633326f316424dcdc`: - **Debug (`--preset dev`)**: - Build (`cpu_switch_bswap*`, `cpu_switch_bit_counts*`): `3.57s` (configure `1.53s`) - Test (`ctest --preset dev -R '^(cpu_aot_switch_bswap|cpu_aot_switch_bit_counts)$' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`cpu_aot_switch_bit_counts` `0.52s`, `cpu_aot_switch_bswap` `0.91s`) - **Release `/O2` (`--preset release`)**: - Build (`cpu_switch_bswap*`, `cpu_switch_bit_counts*`): `0.95s` (configure `1.64s`) - Test (`ctest --preset release -R '^(cpu_aot_switch_bswap|cpu_aot_switch_bit_counts)$' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`cpu_aot_switch_bit_counts` `0.49s`, `cpu_aot_switch_bswap` `0.85s`)
chatgpt-codex-connector[bot commented 2026-11-24 10:02:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T09:44:31Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T09:48:13.361600Z 384ee4a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7127825411 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1374#issuecomment-7127825411). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T09:44:31Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"384ee4acec19ed0a62f76b5633326f316424dcdc","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-24T09:48:13.361600Z">2026-11-24T09:48:13.361600Z</relative-time> | `384ee4a` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
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!1367
No description provided.