cpu: lower the ZF and CF forms of FCMOVcc in the x87 profile #470
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/476"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T05:55:30Z.
Change
Refs #406. This follows #471 (
fscale). It adds the six conditional moves that read only ZF and CF to the opt-in x87 profile inlower(..., x87=True):fcmovb,fcmove,fcmovbe,fcmovnb,fcmovneandfcmovnbewithst, st(i).Semantics. Each form copies
st(i)intost(0)when its condition holds and leaves the stack depth unchanged.fcmovbfcmovefcmovbefcmovnbfcmovnefcmovnbecarryandzero, so they come from integer instructions or fromfcomi(#456).lower()starts both flags as false and has no input for the caller's flags, so a function that could observe them is rejected (see below).fld st(i)does. A signaling NaN stays signaling, and unsupported encodings are copied unchanged. The native runs confirm this (controlquietbelow).Out of scope:
fcmovuandfcmovnuread PF, which the integer lowering does not model, so they stay rejected.Lowering (
tools/x87_lower.py).if (carry || zero) x87_slot[top] = x87_slot[top - k];with the form's condition. Anst(i)at or beyond the stack depth is an underflow. No helper is added totools/x87_soft.h.Defined flags.
defined_flags()is a forward pass over the function that computes which of the six status flags are defined before each instruction. In a function with a move, translation rejects every instruction that reads a flag that is not defined there. The check covers every reader, not only the moves, becauseadc,sbb,rcl,rcrandcmcpass an undefined CF on into the flags and registers they write. A later writer of ZF, such astest, could then carry the caller's CF to a move. The pass tracks all six status flags, so a reader of OF or another flag that the integer lowering may model later is covered too.DFis not tracked, because the Windows x64 calling convention clears it.imul, for example).repcompare may change nothing, so they keep only the flags already defined.shl,shrorsarby the operand width or more leaves CF undefined, and a 16-bitshldorshrdby more than 16 leaves both undefined.The analysis runs only for functions that contain a conditional move. No other lowering changes, so the integer readers of functions without a move keep their current behavior.
Fixture
tests/cpu/x87_fcmov.asmand.defare generated bytests/cpu/x87_fcmov.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names. The.asmregisters its test on its first line (; coverage: source cpu_aot_x87_fcmov). The 16 exports cover:mov eax, [rcx+48]; cmp eax, [rcx+52], movingst(1);fcmovbmovingst(2)whilest(1)must stay unchanged;fcomi; fcmovb) and minimum (fcomi; fcmovnbe) of two values, as compilers emit them;fcmovne st(1), thenfcmove st(2));fcmove st, st(0);fcmovnbmovingst(7)at depth 8, with all eight values stored as m64;cmpbefore the move:mov eax, 0FFFFFFFFh; adc eax, 0; fcmoveandmov eax, 0; sbb eax, 0; fcmove(ZF from that CF),cmc; fcmovb, andmov eax, 0; rcl eax, 1; test eax, eax; fcmovne(that CF carried through EAX).Every export stores every value, so a translation that moves the wrong register or changes another slot mismatches.
Seven more exports are native only. Each moves on the caller's flags:
fcmovbandfcmoveright after two loads, with no flag writer;mov eax, 0FFFFFFFFh; adc eax, 0; fcmoveandmov eax, 0; sbb eax, 0; fcmove, whose ZF comes from the caller's CF;adc eax, 0orrcl eax, 1from EAX = 0, thentest eax, eax; fcmovne, which carry the caller's CF through EAX to a defined ZF;cmc; fcmovb, which inverts the caller's CF.They run through
x87_call_flags, a new entry oftests/cpu/x87_harness.asm. It isx87_callwith the caller's CF and ZF loaded bysahfjust before the call, so the native code sees the flags its caller set. For 40 value pairs and all four (ZF, CF) pairs, each export followed the caller's flag in all 1120 runs. Translation must reject each export at its first reader of the undefined flag: the move, or theadc,sbb,rclorcmc. Before this rule, translation accepted the five indirect exports.Model. The model sets ZF and CF from the dwords or from the comparison model of
cpu_aot_x87_compare, follows EAX, ZF and CF throughmov,adc,sbb,rcl,testandcmc, evaluates each condition itself and copies raw encodings. It shares no code withtools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs.Cases. 13290 cases:
cmpexports, including the four readers of CF, every seventh pair of the 34 special encodings with dword pairs that compare below, equal and above, and random values with dword pairs that include the signed and unsigned edges;fcmovb_st2,fcmovne_fcmove), 450 random triples of the special encodings, each with one of the below, equal and above dword pairs;fcomiexports, every pair of the special encodings and random values;In 5807 of the cases a move changes the output block.
Negative controls. Eight mutated translations must each mismatch:
invertfcmovbmoves when CF is clearbeonlyfcmovbereads only CFnbeorfcmovnbemoves when either flag is clearnevernefcmovnenever movesalwaysquietcmcdropcmcis droppednocarryinadcandsbbignore the incoming CFRejections. Translation rejects 14 sequences with the expected diagnostic:
fcmovuandfcmovnu;fcmovb st, st(1)with one value,fcmove st, st(2)with two values, andfcmovnbeon an empty stack;jrcxzaround thecmp),fcmovbeafterstc(ZF undefined),fcmovnbafter a call,fcmoveafterimul(ZF undefined),fcmovbaftershl eax, clwith no earlier writer,fcmovbaftershl ax, 16, and acmcor asetobefore thecmpthat defines the move's flags. The integer lowering would also rejectseto, but the flag rule comes first.Eight sequences whose flags are written on every path must still translate. Four are moves: after
clc, after a join where both paths write ZF, afterxor eax, eax, and afterfcomi. The other four are readers of CF after an explicit producer:clcbeforeadc,stcbeforesbb,cmpbeforecmc, andclcbeforerclandtest.Changes to existing tests: the shared native harness
tests/cpu/x87_harness.asmgains thex87_call_flagsentry. The other entries are unchanged.Verification
Tested at
5c0151e, the merge ofmainat933cbad(after #484) into4d7bc7c. The merge brought in #287's BMI2 lowering intools/ffx_version_query.pywithout conflicts. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).5c0151e(ctest --preset dev --no-tests=error -R '^cpu_aot_x87_').ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlat5c0151egives 205 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 208. The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, which fails the same way onmain. This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass at5c0151e./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed at5c0151ein the fork'sWindows checksrun 44997062998. Both Windows jobs ran and passed all thirteen x87 tests (173 passed and 2 capability skips,bvh_wave64_warpandwave_shuffle_warp, out of 175 in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 180 of 948 forms, and 173 withoutcpu_aot_x87_fcmov. The added forms are the six conditional moves andsbb, which no other fixture covers and whichfcmove_sbbtranslates. These totals are at5c0151eand include every fixture onmainat933cbad.Not measured:
fcmovuandfcmovnu, which wait for PF in the integer lowering.Overlap
I trial-merged this branch with the 84 other open PRs. Each one conflicts in the same files against
mainand against this branch: the 11 that merge cleanly intomainalso merge cleanly here, and this branch adds no conflict to the others. The changes stay insidetools/x87_lower.py, the new fixture, the newx87_call_flagsentry of the native harness, a CMake block after thecpu_aot_x87_scaletest, and the x87 section ofdocs/experiments.md. If #335 adds OF to the integer lowering, the moves are unaffected because none of them reads it, and the flag rule already covers a reader of OF in a function with a move.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the six forms, the signaling-NaN measurement, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:06:50Z.
The six predicates and full-width copies check out for the CMP/FCOMI cases. There is a missing precondition: lower() initializes carry and zero to false, so two FLDs followed by FCMOVB before any flag writer silently consume that initializer. On native x64 the move can observe either incoming CF. Please reject an FCMOV whose required flags are not defined on every incoming path (including a join with one undefined predecessor), or explicitly bind those flags as inputs if separately agreed. A caller setting CF with STC versus CLC before the same leaf distinguishes the cases; keep the existing defined-producer controls.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:21:15Z.
The new entry and join checks address the direct cases. One path at
b1d485bstill accepts incoming CF: two FLDs, CMC, then FCMOVB. iced reports CF in both rflags_read and rflags_written for CMC (verified with byte F5). Consequently _flags_after() marks CF defined even though CMC only inverts the unbound incoming bit. STC versus CLC in the caller still changes the native result. Please carry that dependency through CMC and the supported carry-consuming writers such as ADC/SBB and rotates through carry. Add a rejection for entry-CMC-FCMOVB and retain a positive case with CLC or STC before CMC.Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T06:36:01Z.
Thanks, fixed in
b1d485b.defined_flags()intools/x87_lower.pyis a forward pass over ZF and CF. It takes writes from iced's flag information, minus the flags iced reports undefined, and intersects at joins. Calls leave both flags undefined. CL-count shifts andrepcompares define no new flag, and 8- and 16-bit shifts by the operand width or more leave CF undefined. The diagnostic names the flag:fcmovb st,st(1) at 0x1004 reads CF, which no instruction defines on every path from the entry. No flag is bound as an input.x87_call_flags, that loads the caller's CF and ZF withsahfjust before the call. Two native-only exports runfcmovbandfcmoveright after two loads. For 40 value pairs under all four (ZF, CF) pairs, each move followed the caller's flag in all 320 runs, and translation rejects both exports.jrcxzaround thecmp);fcmovbeafterstc(ZF undefined);fcmovnbafter a call;fcmoveafterimul(ZF undefined);fcmovbaftershl eax, clwith no earlier writer;fcmovbaftershl ax, 16.clc, after a join where both paths write ZF, afterxor eax, eax, and afterfcomi.The body has the updated output. Fork CI run 44993139598 passed at
b1d485b.The integer flag readers (
jcc,setcc,cmovcc,adc/sbb,rcl/rcr) consume the same initializer:jeas a function's first instruction lowers toif (zero) goto .... That is outside this PR. The same pass could gate them in a separate one if you want it.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:57:35Z.
At
b1d485bthe direct reads and joins are covered. One case still lets the new FCMOV consume a caller flag indirectly:mov eax, 0FFFFFFFFh; adc eax, 0; fcmove st, st(1)after two distinct FLDs. Incoming CF=0 gives EAX=FFFFFFFFh and ZF=0; CF=1 gives EAX=0 and ZF=1._flags_aftermarks ADC's ZF as defined solely because iced lists it as written. Please exercise the two native caller-CF values with your harness and reject this FCMOV when the producer consumes undefined carry. The same dependency applies to SBB and carry rotates for flags they produce. An explicit CLC/STC before ADC should still pass. This can stay scoped to the x87 profile.Imported from R0Y00/Xbox2Windows. Originally posted by FZNull on 2026-11-22T07:05:38Z.
Independent verification on a second machine. Trial-merged this PR onto main
5a2c7822(merge commitaec5bc8, clean, no conflicts) and ran the x87 suite.Environment: Windows 11 build 26300.9974, AMD Ryzen 5 5500 (
AMD64 Family 25 Model 80 Stepping 0, AuthenticAMD— Zen 3, a different part from the Zen 4 used above), MSVC 19.50.35726, Windows SDK 10.0.26100.0, CMake 4.5.1 + Ninja 1.13.2, DXC 1.9.2609.5, Python 3.13.12.Build: full build 899/899 targets, exit 0.
Tests:
13/13 x87 tests pass, 72.69 s total.
cpu_aot_x87_fcmovreports:All six negative controls and the rejection counts match the PR body exactly. Control run
cpu_aot_wide_arithmeticalso passes (2929 inputs, fault and rejection controls OK). No failures, no skips. The PR also still merges cleanly onto the later main933cbadpergit merge-tree(not built).Verdict: verified on an AMD Zen 3 machine; every measured figure matches the PR. Reviewed at merge commit
aec5bc8(PR headb1d485bon main5a2c7822).AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-22T07:17:56Z.
Thanks, fixed in
4d7bc7c. The branch also mergesmain(5c0151e). In a function that contains a conditional move, translation now rejects every instruction that reads a status flag that is not defined on every path from the entry, not only the move.Why every reader.
adc,sbb,rcl,rcrandcmcpass an undefined CF on into the flags they write, as in your example. They also pass it into registers:mov eax, 0; adc eax, 0; test eax, eax; fcmovnesees a ZF thattestdefines, but the value comes from the caller's CF. Rejecting the first reader of the undefined flag covers both paths. The pass now tracks all six status flags rather than only ZF and CF, so the rule still holds if the integer lowering later reads OF (#335) or another flag.DFis not tracked, because the Windows x64 calling convention clears it.Native runs. Five new native-only exports run through
x87_call_flagsunder all four (ZF, CF) pairs. Together with the two earlier ones, each export followed the caller's flag in all 1120 runs over 40 value pairs. Translation rejects each export at its first reader of the undefined flag. The previous head accepted all five new exports.adc_entryadc(CF)sbb_entrymov eax, 0; sbb eax, 0; fcmovesbb(CF)adc_test_entrymov eax, 0; adc eax, 0; test eax, eax; fcmovneadc(CF)rcl_test_entrymov eax, 0; rcl eax, 1; test eax, eax; fcmovnercl(CF)cmc_entrycmc; fcmovbcmc(CF)Translated readers. Four new exports read the CF of the fixture's
cmpbefore the move. They are theadcandsbbsequences above,cmc; fcmovb, and thercl/testone. Each is compared with native on 798 cases. Two new mutations must mismatch: a droppedcmc(696 mismatches) andadc/sbbwithout the incoming carry (510). The test now has 13290 cases over 16 exports. The six existing controls still mismatch at their new counts, which are in the updated body.Static cases.
cmcbefore the definingcmp, and asetobefore the definingcmp. The integer lowering would also rejectseto, but the flag rule comes first.clcbeforeadc,stcbeforesbb,cmpbeforecmc, andclcbeforerclandtest.The scope stays inside the x87 profile: functions without a conditional move are unchanged.
The body has the updated output, controls and verification. The full suite at
5c0151egives 205 passed, the #369 probe failing as onmain, and 2 capability skips out of 208. Fork CI run 44997062998 passed.AI-assisted (Claude Code).