cpu: lower SETP, SETNP, CMOVP and CMOVNP where the reaching PF definition is modeled #1193

Manually merged
github merged 1 commit from gh-pr/1193/head into gh-pr/1193/base 2026-11-26 22:47:47 +00:00
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-24 03:20 UTC · original
legacy-code-archaeologist:cpu/lower-parity → main · +489 −16 · 10 files · 1 commits · merged 2026-11-24 03:38 UTC

Refs #1153

What

The shared lowering (tools/ffx_version_query.py) now lowers SETP, SETNP, CMOVP and CMOVNP, next to JP and JNP, when the reaching PF definition is modeled on every path from the entry (check_parity):

  • PF producers: VCOMISS, VUCOMISS, VCOMISD, VUCOMISD (PF = unordered); PTEST, VPTEST, VTESTPS, VTESTPD and PCMPxSTRx (PF cleared); a register ADD, SUB or CMP that a LAHF reads (lahf_sources).
  • Refused: PF after any other writer (for example ADD without LAHF), after an instruction that leaves PF undefined (IMUL), after a call or a host binding, at a join where only one path defines PF, and a reader that no path from the entry reaches.
  • Inside the x87 profile, x87_lower.check_flags keeps deciding. GATED now includes the new readers, and assigns_parity counts the four VEX compares.
  • CMOVP and CMOVNP read a memory source whatever PF says and zero-extend a 32-bit destination either way, like the other CMOVcc.
  • Functions without a PF reader keep their generated code; all new branches are guarded by parity.

Existing tests changed (please check)

Three refusal cases pinned "SETP/CMOVP unsupported", which this change lowers on purpose. Each now inserts a PF writer the lowering does not model, so it still tests a refusal, now with the PF message:

  • tests/cpu/x87_compare.py: fucomi then setp/cmovp → fucomi and inc eax, then setp/cmovp, needle "reads PF, which the integer lowering models only after".
  • tests/cpu/x87_parity.py: "SETP after FUCOMI" → "SETP after FUCOMI and INC", PF needle.
  • tests/cpu/integer_flags_check.py: out_of_scope_setp drops the LAHF, so SETP reads PF of an ADD that no LAHF reads; needle "reads PF".

These adjusted controls found a bug: before GATED was extended, x87 functions skipped check_flags for SETP and CMOVP.

Evidence

cpu_aot_parity (parity.asm, parity.py) runs SETNP, SETP, SETE, SETB, CMOVP (register), CMOVNP (memory) and JP after each VEX compare and after FUCOMIP (x87 profile), for twelve operand pairs: ordered, equal, ±0, infinities, and QNaN and SNaN in either operand. MXCSR is 0x1F80, set and restored by the harness. Native and translated must both match the fixed flags.

The test also checks:

  • the setnp; sete; and equality idiom;
  • SETP after a LAHF-read ADD against the parity of the sum's low byte, for seven pairs;
  • that CMOVP and CMOVNP memory sources on a PAGE_NOACCESS page fault for ordered and unordered pairs;
  • five refusals.

Two controls must break: stale drops the compare's PF assignment, and reversed swaps every PF condition. An /O2 build must pass.

Under masked exceptions VCOMISS and VUCOMISS set the same flags, so this fixture does not tell them apart. FLD quiets a signaling NaN, so the FUCOMIP case compares it as a quiet NaN.

Windows 11 Pro 26100, Intel i7-6820HQ (AVX), MSVC 19.52, commit 416df060 on 645b9697:

ctest --preset dev --no-tests=error -R '^cpu_aot_parity$' -V

passes: five refusals; native and lifted match for all exports; stale fails on pf_ucomiss(qnan, 1) and reversed on pf_ucomiss(1, 2); /O2 keeps every rule. The release preset passes cpu_aot_parity, cpu_aot_integer_flags, cpu_aot_x87_compare and cpu_aot_x87_parity.

The full dev suite without _hardware passed 482/482 before the rebase; after the rebase, ctest --preset dev -R '^cpu_' -E '_hardware$' passes 210/210. Run one at a time, cpu_*_hardware and compute_store_aot*_hardware pass, except cpu_shim_shadow_hardware, which already fails on main (reported on #799). tools/form_coverage.py --gaps x64_zen2 no longer lists setnp, cmovp or cmovnp.

🤖 Generated with Claude Code

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-24 03:20 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1193) `legacy-code-archaeologist:cpu/lower-parity` → `main` · +489 −16 · 10 files · 1 commits · merged 2026-11-24 03:38 UTC Refs #1153 ## What The shared lowering (`tools/ffx_version_query.py`) now lowers SETP, SETNP, CMOVP and CMOVNP, next to JP and JNP, when the reaching PF definition is modeled on every path from the entry (`check_parity`): - PF producers: VCOMISS, VUCOMISS, VCOMISD, VUCOMISD (PF = unordered); PTEST, VPTEST, VTESTPS, VTESTPD and PCMPxSTRx (PF cleared); a register ADD, SUB or CMP that a LAHF reads (`lahf_sources`). - Refused: PF after any other writer (for example ADD without LAHF), after an instruction that leaves PF undefined (IMUL), after a call or a host binding, at a join where only one path defines PF, and a reader that no path from the entry reaches. - Inside the x87 profile, `x87_lower.check_flags` keeps deciding. `GATED` now includes the new readers, and `assigns_parity` counts the four VEX compares. - CMOVP and CMOVNP read a memory source whatever PF says and zero-extend a 32-bit destination either way, like the other CMOVcc. - Functions without a PF reader keep their generated code; all new branches are guarded by `parity`. ## Existing tests changed (please check) Three refusal cases pinned "SETP/CMOVP unsupported", which this change lowers on purpose. Each now inserts a PF writer the lowering does not model, so it still tests a refusal, now with the PF message: - `tests/cpu/x87_compare.py`: `fucomi then setp/cmovp` → `fucomi and inc eax, then setp/cmovp`, needle "reads PF, which the integer lowering models only after". - `tests/cpu/x87_parity.py`: "SETP after FUCOMI" → "SETP after FUCOMI and INC", PF needle. - `tests/cpu/integer_flags_check.py`: `out_of_scope_setp` drops the LAHF, so SETP reads PF of an ADD that no LAHF reads; needle "reads PF". These adjusted controls found a bug: before `GATED` was extended, x87 functions skipped `check_flags` for SETP and CMOVP. ## Evidence `cpu_aot_parity` ([parity.asm](tests/cpu/parity.asm), [parity.py](tests/cpu/parity.py)) runs SETNP, SETP, SETE, SETB, CMOVP (register), CMOVNP (memory) and JP after each VEX compare and after FUCOMIP (x87 profile), for twelve operand pairs: ordered, equal, ±0, infinities, and QNaN and SNaN in either operand. MXCSR is 0x1F80, set and restored by the harness. Native and translated must both match the fixed flags. The test also checks: - the `setnp; sete; and` equality idiom; - SETP after a LAHF-read ADD against the parity of the sum's low byte, for seven pairs; - that CMOVP and CMOVNP memory sources on a PAGE_NOACCESS page fault for ordered and unordered pairs; - five refusals. Two controls must break: `stale` drops the compare's PF assignment, and `reversed` swaps every PF condition. An `/O2` build must pass. Under masked exceptions VCOMISS and VUCOMISS set the same flags, so this fixture does not tell them apart. FLD quiets a signaling NaN, so the FUCOMIP case compares it as a quiet NaN. Windows 11 Pro 26100, Intel i7-6820HQ (AVX), MSVC 19.52, commit 416df060 on 645b9697: ``` ctest --preset dev --no-tests=error -R '^cpu_aot_parity$' -V ``` passes: five refusals; native and lifted match for all exports; `stale` fails on `pf_ucomiss(qnan, 1)` and `reversed` on `pf_ucomiss(1, 2)`; `/O2` keeps every rule. The release preset passes `cpu_aot_parity`, `cpu_aot_integer_flags`, `cpu_aot_x87_compare` and `cpu_aot_x87_parity`. The full dev suite without `_hardware` passed 482/482 before the rebase; after the rebase, `ctest --preset dev -R '^cpu_' -E '_hardware$'` passes 210/210. Run one at a time, `cpu_*_hardware` and `compute_store_aot*_hardware` pass, except `cpu_shim_shadow_hardware`, which already fails on main (reported on #799). `tools/form_coverage.py --gaps x64_zen2` no longer lists setnp, cmovp or cmovnp. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The shared lowering now models PF for JP, JNP, SETP, SETNP, CMOVP and CMOVNP
outside the x87 profile when every path from the entry reaches the reader
through an instruction whose lowering assigns PF: the VEX scalar compares
(PF = unordered), PTEST, VPTEST, VTESTPx and PCMPxSTRx (PF cleared), or a
register ADD, SUB or CMP that a LAHF reads. A call, a host binding, an
undefined PF, any other PF writer, a disagreeing join or an unreached reader
is refused (check_parity). Inside the x87 profile check_flags keeps deciding;
it now also gates the new readers and counts the VEX compares as producers.

cpu_aot_parity compares native and translated readers after VUCOMISS,
VCOMISS, VUCOMISD, VCOMISD and FUCOMIP for twelve operand pairs under MXCSR
0x1F80, the equality idiom, SETP after a LAHF source, and faults of CMOVP and
CMOVNP memory sources; stale and reversed controls must break and an /O2
build must pass.

Three existing refusal cases that pinned SETP or CMOVP as unsupported now
insert a PF writer the lowering does not model and expect the PF refusal.

Refs #1153

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:47:45 +00:00
github manually merged commit 416df06052 into gh-pr/1193/base 2026-11-26 22:47:47 +00:00
Author
Owner

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

Reviewed 416df060 (diff + Linux doc/bidi checks; Windows build/MASM/MSVC tests and Python checks requiring pefile/iced_x86 were not run locally).

Findings

  1. Overlap with open PR #1188: #1188 (cpu: lower PF consumers after VEX scalar compares) also targets #1153 and modifies the same core files (tools/ffx_version_query.py, tools/x87_lower.py, tests/cpu/x87_compare.py, tests/cpu/x87_parity.py, CMakeLists.txt, docs/experiments.md). Merging either one will require coordinating or rebasing the other.
  2. Code & semantics:
    • tools/ffx_version_query.py (check_parity): sound forward dataflow over x87_lower._successors(i) starting with known[entry] = False, intersecting at joins (known[target] and after), resetting to False on host_ops, calls, undefined PF, or unmodeled PF writers, and rejecting both unmodeled and unreachable PF readers.
    • CMOVP/CMOVNP and SETP/SETNP match the existing CMOVcc/SETcc lowering (including eager evaluation of memory operands and 32-bit zero-extension).
    • tools/x87_lower.py: extends GATED with SETP, SETNP, CMOVP, CMOVNP and assigns_parity with the four VEX scalar compares so x87-profile functions still go through check_flags.
    • tests/cpu/parity.{asm,def,py} and docs/experiments.md: title-neutral, include negative controls (stale, reversed) and 5 refusal cases.

Checks run on Linux

  • python3 -B tests/docs_links_check.py: passed (15 Markdown files, 266 links).
  • python3 -B tests/bidi_controls_check.py: passed (974 tracked files).
  • tests/cpu/integer_flags_check.py, tests/cpu/x87_compare.py, tests/cpu/x87_parity.py: not runnable here because pefile / iced_x86 are not installed in this Linux Python environment.
  • Windows build/test (cpu_aot_parity, cl /O2) was not run locally.
**@⁠tedin7** on GitHub · 2026-11-24 03:27 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/1193#pullrequestreview-6033223068) · **reviewed** Reviewed `416df060` (diff + Linux doc/bidi checks; Windows build/MASM/MSVC tests and Python checks requiring `pefile`/`iced_x86` were not run locally). ### Findings 1. **Overlap with open PR #1188**: `#1188` (`cpu: lower PF consumers after VEX scalar compares`) also targets `#1153` and modifies the same core files (`tools/ffx_version_query.py`, `tools/x87_lower.py`, `tests/cpu/x87_compare.py`, `tests/cpu/x87_parity.py`, `CMakeLists.txt`, `docs/experiments.md`). Merging either one will require coordinating or rebasing the other. 2. **Code & semantics**: - `tools/ffx_version_query.py` (`check_parity`): sound forward dataflow over `x87_lower._successors(i)` starting with `known[entry] = False`, intersecting at joins (`known[target] and after`), resetting to `False` on `host_ops`, calls, undefined `PF`, or unmodeled `PF` writers, and rejecting both unmodeled and unreachable `PF` readers. - `CMOVP`/`CMOVNP` and `SETP`/`SETNP` match the existing `CMOVcc`/`SETcc` lowering (including eager evaluation of memory operands and 32-bit zero-extension). - `tools/x87_lower.py`: extends `GATED` with `SETP`, `SETNP`, `CMOVP`, `CMOVNP` and `assigns_parity` with the four VEX scalar compares so x87-profile functions still go through `check_flags`. - `tests/cpu/parity.{asm,def,py}` and `docs/experiments.md`: title-neutral, include negative controls (`stale`, `reversed`) and 5 refusal cases. ### Checks run on Linux - `python3 -B tests/docs_links_check.py`: passed (15 Markdown files, 266 links). - `python3 -B tests/bidi_controls_check.py`: passed (974 tracked files). - `tests/cpu/integer_flags_check.py`, `tests/cpu/x87_compare.py`, `tests/cpu/x87_parity.py`: not runnable here because `pefile` / `iced_x86` are not installed in this Linux Python environment. - Windows build/test (`cpu_aot_parity`, `cl /O2`) was not run locally.
This discussion has been locked. Commenting is limited to contributors.
No description provided.