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

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/1193 into main 2026-11-24 03:38:26 +00:00
legacy-code-archaeologist commented 2026-11-24 03:20:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-24T03:20:12Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6573860301 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1193). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-24T03:20:12Z. --- 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)
upstream-sync closed this pull request 2026-11-24 06:46:49 +00:00
tedin7 commented 2026-11-24 06:46:50 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033223068 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1193#pullrequestreview-6033223068). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T03:27:42Z. **Review:** COMMENTED --- 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.
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!1186
No description provided.