Lift SETcc and CMOVcc in the jump-table translator #173

Manually merged
github merged 2 commits from gh-pr/173/head into gh-pr/173/base 2026-11-26 21:32:01 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 17:56 UTC · original
Vonsek:cpu-switch-setcc-cmov → main · +290 −20 · 7 files · 2 commits · merged 2026-11-21 20:26 UTC

Change

tools/cpu_switch_aot.py rejected every SETcc and CMOVcc, and modelled flags only for a conditional jump placed directly after its producer. Compilers emit both instructions for comparisons, selects, min/max and clamps, often with a MOV between the compare and the consumer.

  • Each flag consumer (Jcc, SETcc, CMOVcc) now looks back for its producer through instructions that leave the flags unchanged (MOV, MOVZX, MOVSX, MOVSXD, LEA, NOP, NOT, SETcc, CMOVcc), up to 16 instructions, inside one straight-line run that no edge enters. The supported producers are unchanged: CMP, TEST, ADD, SUB, AND, OR, XOR, INC, DEC. Any other flag writer in between (for example NEG) is rejected, as are the overflow and parity conditions.
  • SETcc writes a memory byte or a low byte register and keeps bits 8 to 63 of that register. AH-DH are still rejected.
  • CMOVcc with 32- or 64-bit operands. A 32-bit CMOVcc clears bits 32 to 63 of the destination even when the condition is false. A memory source is loaded on both outcomes, as on hardware.
  • tests/cpu/switch.asm gains flag_select, an eight-entry LLVM-layout table to SETL, SETBE, SETG into memory, SETNE after a 16-bit compare, 32-bit CMOVA, 64-bit CMOVNS after TEST, CMOVGE from memory with a MOV between compare and consumer, and CMOVE after TEST with an immediate. It compares EDX with bits 32 to 63 of RDX and returns RAX whole, so the upper-half behaviour of both instructions is observable.
  • tests/cpu/switch_check.py runs 4,000 inputs at the signed, unsigned and 16-bit boundaries through the original, the translation and a Python reference. The SWAP_TABLE build exchanges the SETL and CMOVA entries and must follow the swapped reference. Four new rejection controls patch the linked DLL: SETO, SETL AH, a NEG between CMP and CMOVGE, and a branch into SETBE (flags across a join).
  • README CPU row and docs/experiments.md (switch dispatch section) describe the new coverage.

The test stays cpu_aot_jump_tables; flag_select is added to its export list in CMakeLists.txt.

Verification

  • Windows: ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V at the rebased head (main with #86; the earlier run at 173c476 gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207, ML64 14.44.35230), Windows SDK 10.0.26100: passed. ML64 assembles flag_select in both the original and SWAP_TABLE builds without changes. Output: 4000 SETcc/CMOVcc inputs match and the swapped table changed 762, 13 rejection controls and the leaf-without-unwind translation passed, 4 flag-consumer rejection controls passed, and the existing select, run_ops and shift_narrow comparisons still pass. The full ctest --preset dev --no-tests=error passed 86/86, with bvh_wave64_hardware, bvh_wave64_warp and wave_shuffle_warp skipped.
  • Linux stand-in: switch.asm converted to GNU syntax, assembled with llvm-mc -triple x86_64-pc-windows-msvc, linked with lld-link /dll /noentry /def:tests/cpu/switch.def, original and swapped. The translated C++ was built with clang as a shared library. All 4,000 flag_select inputs match the original and the reference, and the swapped table changed 762 results; the existing select, run_ops and shift_narrow checks still pass. switch_check.py rejections passed all 13 existing controls and the 4 new ones.
  • Fail before: with tools/cpu_switch_aot.py from main, translation stops with unsupported instruction at RVA 0x11c1: setl al.
  • Mutation checks: writing the full register for a byte SETcc fails at flag_select(0x6, 0x100000000); keeping the 64-bit destination for a false 32-bit CMOVcc fails at flag_select(0x3, 0x0).
  • Compiler output: five small C functions built by clang for x86_64-pc-windows-msvc at -O1 and -O2 contain 14 SETcc/CMOVcc instructions, including SETE R8B and both operand widths. Their translations matched the original code on 25,000 calls per level.

Evidence and follow-up

  • Not covered: SETcc/CMOVcc after NEG, shifts, IMUL or BT producers; 16-bit CMOVcc; ADC/SBB; flags carried across a conditional-jump fallthrough.
**@⁠Vonsek** on GitHub · 2026-11-21 17:56 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173) `Vonsek:cpu-switch-setcc-cmov` → `main` · +290 −20 · 7 files · 2 commits · merged 2026-11-21 20:26 UTC ## Change `tools/cpu_switch_aot.py` rejected every `SETcc` and `CMOVcc`, and modelled flags only for a conditional jump placed directly after its producer. Compilers emit both instructions for comparisons, selects, min/max and clamps, often with a `MOV` between the compare and the consumer. - Each flag consumer (`Jcc`, `SETcc`, `CMOVcc`) now looks back for its producer through instructions that leave the flags unchanged (`MOV`, `MOVZX`, `MOVSX`, `MOVSXD`, `LEA`, `NOP`, `NOT`, `SETcc`, `CMOVcc`), up to 16 instructions, inside one straight-line run that no edge enters. The supported producers are unchanged: `CMP`, `TEST`, `ADD`, `SUB`, `AND`, `OR`, `XOR`, `INC`, `DEC`. Any other flag writer in between (for example `NEG`) is rejected, as are the overflow and parity conditions. - `SETcc` writes a memory byte or a low byte register and keeps bits 8 to 63 of that register. `AH`-`DH` are still rejected. - `CMOVcc` with 32- or 64-bit operands. A 32-bit `CMOVcc` clears bits 32 to 63 of the destination even when the condition is false. A memory source is loaded on both outcomes, as on hardware. - `tests/cpu/switch.asm` gains `flag_select`, an eight-entry LLVM-layout table to `SETL`, `SETBE`, `SETG` into memory, `SETNE` after a 16-bit compare, 32-bit `CMOVA`, 64-bit `CMOVNS` after `TEST`, `CMOVGE` from memory with a `MOV` between compare and consumer, and `CMOVE` after `TEST` with an immediate. It compares EDX with bits 32 to 63 of RDX and returns RAX whole, so the upper-half behaviour of both instructions is observable. - `tests/cpu/switch_check.py` runs 4,000 inputs at the signed, unsigned and 16-bit boundaries through the original, the translation and a Python reference. The `SWAP_TABLE` build exchanges the `SETL` and `CMOVA` entries and must follow the swapped reference. Four new rejection controls patch the linked DLL: `SETO`, `SETL AH`, a `NEG` between `CMP` and `CMOVGE`, and a branch into `SETBE` (flags across a join). - README CPU row and `docs/experiments.md` (switch dispatch section) describe the new coverage. The test stays `cpu_aot_jump_tables`; `flag_select` is added to its export list in `CMakeLists.txt`. ## Verification - Windows: `ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V` at the rebased head (main with #86; the earlier run at 173c476 gave the same result) on Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207, ML64 14.44.35230), Windows SDK 10.0.26100: passed. ML64 assembles `flag_select` in both the original and `SWAP_TABLE` builds without changes. Output: `4000 SETcc/CMOVcc inputs match and the swapped table changed 762`, `13 rejection controls and the leaf-without-unwind translation passed`, `4 flag-consumer rejection controls passed`, and the existing select, `run_ops` and `shift_narrow` comparisons still pass. The full `ctest --preset dev --no-tests=error` passed 86/86, with `bvh_wave64_hardware`, `bvh_wave64_warp` and `wave_shuffle_warp` skipped. - Linux stand-in: `switch.asm` converted to GNU syntax, assembled with `llvm-mc -triple x86_64-pc-windows-msvc`, linked with `lld-link /dll /noentry /def:tests/cpu/switch.def`, original and swapped. The translated C++ was built with clang as a shared library. All 4,000 `flag_select` inputs match the original and the reference, and the swapped table changed 762 results; the existing select, run_ops and shift_narrow checks still pass. `switch_check.py rejections` passed all 13 existing controls and the 4 new ones. - Fail before: with `tools/cpu_switch_aot.py` from `main`, translation stops with `unsupported instruction at RVA 0x11c1: setl al`. - Mutation checks: writing the full register for a byte `SETcc` fails at `flag_select(0x6, 0x100000000)`; keeping the 64-bit destination for a false 32-bit `CMOVcc` fails at `flag_select(0x3, 0x0)`. - Compiler output: five small C functions built by clang for `x86_64-pc-windows-msvc` at `-O1` and `-O2` contain 14 `SETcc`/`CMOVcc` instructions, including `SETE R8B` and both operand widths. Their translations matched the original code on 25,000 calls per level. ## Evidence and follow-up - Not covered: `SETcc`/`CMOVcc` after `NEG`, shifts, `IMUL` or `BT` producers; 16-bit `CMOVcc`; `ADC`/`SBB`; flags carried across a conditional-jump fallthrough.
Flags now reach every consumer (Jcc, SETcc, CMOVcc) in the straight-line
run after their producer, across instructions that leave the flags
unchanged. A byte SETcc keeps bits 8..63 of its register; a 32-bit CMOVcc
zero-extends its destination on both outcomes and reads a memory source
either way.

flag_select in the switch fixture covers eight SETcc/CMOVcc forms on 4,000
inputs, with a swapped-table control and four rejection controls.
CMOVcc reads its memory source whether or not it moves, so an inaccessible source faults. The translation loaded it with an ordinary memcpy, which an optimizing compiler drops when the condition folds to false. The source is now read through a volatile load.

cmov_read (CMP ECX, ECX; CMOVNE EAX, [R8]) must return the zero-extended RDX low dword and fault with a null R8, in the original, the translation and the swapped-table build. In an MSVC Release build without the volatile load, the translation returns instead of faulting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:31:59 +00:00
github manually merged commit 8642b84480 into gh-pr/173/base 2026-11-26 21:32:01 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 18:16 UTC · original

GitHub reports a conflict on 173c4765. Please reconcile the conditional flag-producer logic and jump-table fixture with the current translator, keeping the existing jump-table controls. Run the focused jump-table CTest on the reconciled head before marking ready.

**@⁠R0Y00** on GitHub · 2026-11-21 18:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7067129619) GitHub reports a conflict on `173c4765`. Please reconcile the conditional flag-producer logic and jump-table fixture with the current translator, keeping the existing jump-table controls. Run the focused jump-table CTest on the reconciled head before marking ready.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 18:24 UTC · original · edited 2026-11-21 18:33 UTC

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-21T18:33:54.372027Z 3311b0b 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.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 18:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7067230031) · edited 2026-11-21 18:33 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"3311b0b6a397e8f42c9b0633d8b27d33246125f3","has_any_findings":true,"has_unresolved_findings":true,"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-21T18:33:54.372027Z">2026-11-21T18:33:54.372027Z</relative-time> | `3311b0b` | 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>
@ -478,0 +514,4 @@
# A volatile read: when the condition folds to false the compiler may not drop the load,
# which faults on hardware for an inaccessible source.
source = f"load_always<{unsigned(width)}>({self.address(ins)})"
return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 18:33 UTC · original

P2 Badge Make the unconditional CMOV source read non-elidable

When an optimized translation's condition folds to false, this ordinary memcpy-backed load can disappear despite being outside the ternary. For example, the emitted fragment for mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8], compiled with Clang 17 at -O2 on Linux, contains no memory read: it returns normally for r8 == 0, whereas the original instruction faults. This violates the documented unconditional-memory-source behavior. Make the access non-elidable and add a constant-false, inaccessible-source control.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 18:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#discussion_r4654315687) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Make the unconditional CMOV source read non-elidable** When an optimized translation's condition folds to false, this ordinary `memcpy`-backed load can disappear despite being outside the ternary. For example, the emitted fragment for `mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]`, compiled with Clang 17 at `-O2` on Linux, contains no memory read: it returns normally for `r8 == 0`, whereas the original instruction faults. This violates the documented unconditional-memory-source behavior. Make the access non-elidable and add a constant-false, inaccessible-source control. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
@ -478,0 +514,4 @@
# A volatile read: when the condition folds to false the compiler may not drop the load,
# which faults on hardware for an inaccessible source.
source = f"load_always<{unsigned(width)}>({self.address(ins)})"
return f"{{ const {unsigned(width)} source = {source}; {line} }}", reads, writes
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 19:43 UTC · original

Fixed in 1e9969a. A CMOVcc memory source is now read through a volatile load (load_always), so it is kept when the condition folds to false. The new export cmov_read (mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]) is the constant-false, inaccessible-source control: with a valid R8 it must return the zero-extended low dword of RDX, and with a null R8 it must fault, in the original, the translation and the swapped-table build. In an MSVC Release build of the translation without the volatile load the null call returns (translation cmov_read with a null source did not fault); with it, the test passes in Debug and Release.

**@⁠Vonsek** on GitHub · 2026-11-21 19:43 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#discussion_r4654637828) Fixed in 1e9969a. A `CMOVcc` memory source is now read through a `volatile` load (`load_always`), so it is kept when the condition folds to false. The new export `cmov_read` (`mov rax, rdx; cmp ecx, ecx; cmovne eax, [r8]`) is the constant-false, inaccessible-source control: with a valid R8 it must return the zero-extended low dword of RDX, and with a null R8 it must fault, in the original, the translation and the swapped-table build. In an MSVC Release build of the translation without the volatile load the null call returns (`translation cmov_read with a null source did not fault`); with it, the test passes in Debug and Release.
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 19:43 UTC · original

Rebased on main 7233cec (b5ef0f9, plus 1e9969a for the review finding). The only conflict was the README "CPU translation" row; the current row is kept with the SETcc/CMOVcc sentence added. The flag-producer logic and the jump-table fixture rebased cleanly onto the current translator, and the existing jump-table controls are unchanged.

On Windows 11 26200.9974 (MSVC 14.44.35207, ML64 14.44.35230, SDK 10.0.26100): ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V passed (select tables, run_ops, shift_narrow, 4,000 flag_select inputs, cmov_read, 13 rejection controls and 4 flag-consumer rejection controls), in Debug and in a Release build. The full suite passed 121/121.

**@⁠Vonsek** on GitHub · 2026-11-21 19:43 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7068231739) Rebased on main 7233cec (b5ef0f9, plus 1e9969a for the review finding). The only conflict was the README "CPU translation" row; the current row is kept with the SETcc/CMOVcc sentence added. The flag-producer logic and the jump-table fixture rebased cleanly onto the current translator, and the existing jump-table controls are unchanged. On Windows 11 26200.9974 (MSVC 14.44.35207, ML64 14.44.35230, SDK 10.0.26100): `ctest --preset dev --no-tests=error -R '^cpu_aot_jump_tables$' -V` passed (select tables, `run_ops`, `shift_narrow`, 4,000 `flag_select` inputs, `cmov_read`, 13 rejection controls and 4 flag-consumer rejection controls), in Debug and in a Release build. The full suite passed 121/121.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 19:59 UTC · original

At 1e9969a, only docs/experiments.md conflicts. The translator merges automatically. Please retain the addr32 and conditional-move results in the combined document; the open inline review still needs resolution.

**@⁠R0Y00** on GitHub · 2026-11-21 19:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#issuecomment-7068423856) At `1e9969a`, only `docs/experiments.md` conflicts. The translator merges automatically. Please retain the addr32 and conditional-move results in the combined document; the open inline review still needs resolution.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 18:33 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3311b0b6a3

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 18:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/173#pullrequestreview-6007292387) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `3311b0b6a3` <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". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
This discussion has been locked. Commenting is limited to contributors.
No description provided.