Skip to content

cpu: lower PF consumers after VEX scalar compares - #1188

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
Batushn:cpu/vex-pf-consumers
Nov 24, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
Batushn:cpu/vex-pf-consumers

Conversation

@Batushn

@Batushn Batushn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Change

Fixes #1153.

Allow PF consumers (SETP, SETNP, CMOVP, CMOVNP alongside JP, JNP, FCMOVU, FCMOVNU) after modeled PF producers, including the four scalar VEX compares (VCOMISS, VUCOMISS, VCOMISD, VUCOMISD), whenever x87_lower.check_flags proves a reaching modeled PF definition at every use.

  • tools/x87_lower.py: Added SETP, SETNP, CMOVP, CMOVNP to GATED; added VEX_SCALAR_COMPARES (VCOMISS, VUCOMISS, VCOMISD, VUCOMISD) and already-lowered PF_CLEARED instructions (RDRAND, RDSEED, PTEST, VPTEST, VTESTPS, VTESTPD, PCMPESTRI, PCMPESTRM, PCMPISTRI, PCMPISTRM) to assigns_parity(i); threaded skip_lahf and host_ops into _after, states, and check_flags; preserved existing error prefixes (reads PF, which the integer lowering models only after ...).
  • tools/ffx_version_query.py: Gated SETP/SETNP/CMOVP/CMOVNP on x87_lower.check_flags (rejecting indirect branches and wrapping non-x87 ValueError in "unsupported query operation: ..." so integer_flags_check stays intact) and lowered CMOVP/CMOVNP and SETP/SETNP using existing write / guest eager-read & 32-bit zero-extension paths. Functions without PF consumers keep unchanged parity-free C++.
  • tests/cpu/vex_parity.asm, tests/cpu/vex_parity.def, tests/cpu/vex_parity.py, CMakeLists.txt: Added cpu_aot_vex_parity differential fixture comparing native, translated, and /O2 DLLs across 14 exports and 1618 cases.
  • tests/cpu/x87_compare.py, tests/cpu/x87_parity.py, docs/experiments.md: Updated setp/cmovp expectations after fucomi and documented cpu_aot_vex_parity.

Verification

Commit e132e45 (based on main 063df2c) on AMD64 Family 25 Model 116 Stepping 1, AuthenticAMD (Windows, VS 2022 BuildTools cl 19.44.35229.0):

  • ctest --preset dev --no-tests=error -R "^cpu_aot_(vex_parity|x87_parity|x87_compare|x87_status|x87_fcmov|integer_flags|avx_scalar_fp|cmov_setcc|lower_dead_reads)$" --output-on-failure: 9/9 passed (cpu_aot_vex_parity + all related flag/parity/AVX/CMOV/fault tests).
  • ctest --preset release --no-tests=error -R "^cpu_aot_vex_parity$" -V: 1/1 passed:
    • rejected 13 negative PF cases, accepted 4 positive sequences, and preserved parity-free lowering
    • control stalepf: caught 3 deliberate PF mismatches
    • control swap: caught 5 deliberate PF mismatches
    • 1618 VEX scalar compare / PF consumer cases (masked MXCSR 0x1f80, qNaN/sNaN in both operands, 32/64/16-bit CMOVP/CMOVNP, fault checks, CFG joins) matched native, translated, and /O2 DLLs
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: passed.
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py: Checked 15 Markdown files and 255 links.
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py: Checked 951 tracked source, script and build/CI files: no bidirectional controls.

Evidence and follow-up

  • tests/cpu/vex_parity.asm and tests/cpu/vex_parity.py are original synthetic tests under repository conventions (; coverage: source cpu_aot_vex_parity).
  • Register and memory RHS forms for vcomiss, vucomiss, vcomisd, vucomisd are checked under documented masked MXCSR = 0x1F80 (saved/restored via host_exchange_mxcsr), with quiet and signaling NaNs in each operand, +/-0.0, +/-inf, subnormals, and ordered pairs (<, ==, >). Floating-point exception behavior outside vex_parity is unchanged.
  • cmovp/cmovnp are verified for both PF outcomes at 32-bit (upper 32 bits cleared even when condition is false), 64-bit, and 16-bit widths, plus unconditional memory reads (0xC0000005 on PAGE_NOACCESS and NULL via cpu_guarded_call) for both PF=0 and PF=1 in Debug, Release, and /O2.
  • Mutated C++ controls (stalepf dropping parity = unordered;, swap reversing PF consumer conditions) both fail as required, and 13 negative lowering cases (entry/no writer, add, cmp, inc, 32-bit test, shl, call, ambiguous/uninitialized joins, indirect branch, host_ops) remain rejected.

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.

AI-assisted: yes

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Audited diff (e132e457) and ran platform-independent checks on Linux (no Windows/MSVC build or iced_x86 environment locally, so cpu_aot_vex_parity and form_coverage_check.py were not executed).

Linux checks run on e132e457:

  • python3 -B tests/docs_links_check.py: passed (Checked 15 Markdown files and 255 links.)
  • python3 -B tests/bidi_controls_check.py: passed (Checked 951 tracked source, script and build/CI files: no bidirectional controls.)
  • python3 -B tests/rdna2_tools_check.py: passed (Ran 109 tests in 2.398s, OK (skipped=10))

Code audit:

  • tools/x87_lower.py: SETP, SETNP, CMOVP, CMOVNP are gated, VEX_SCALAR_COMPARES (VCOMISS, VUCOMISS, VCOMISD, VUCOMISD) and PF_CLEARED (RDRAND, RDSEED, PTEST, VPTEST, VTESTPS, VTESTPD, PCMPESTRI, PCMPESTRM, PCMPISTRI, PCMPISTRM) are recognized by assigns_parity, and host_ops invalidates tracked flags/PF in _after.
  • tools/ffx_version_query.py: Confirmed that all PF_CLEARED instructions already emit parity = false; when parity is enabled, VCOMISS/VUCOMISS/VCOMISD/VUCOMISD compute unordered from operand bits and assign zero, carry, parity accordingly, SETP/SETNP preserve upper bits via write, and CMOVP/CMOVNP eagerly read memory operands via guest() and zero-extend 32-bit register destinations via write_register even when not taken.
  • AGENTS.md compliance: title-neutral wording, docs/experiments.md updated, and negative controls (stalepf, swap, and 13 rejected lowering cases) are present.

Windows cpu_aot_vex_parity and form_coverage_check.py results remain unverified locally.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks for this work. The shared PF lowering landed in #1193 along with the native and optimized comparisons. Your distinct source-built cases are welcome as a focused follow-up against current main. Please keep the existing shared proof in place and point out any accepted or refused case the landed fixture does not exercise.

@R0Y00

R0Y00 commented Nov 24, 2026

Copy link
Copy Markdown
Owner

At 64d451e this PR builds on the #1193 lowering already in main and retains the independent MASM/native/optimized test, adjusted to exercise current PF refusals. Windows Release matched all 1,618 native and lifted cases and detected the stale-PF and swapped-operand controls.

@R0Y00
R0Y00 merged commit a3633d4 into R0Y00:main Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cpu: PF after the VEX scalar compares in the shared lowering (SETP/SETNP/CMOVP/CMOVNP/JP/JNP)

3 participants