cpu: lower FCOM, FUCOM and FNSTSW AX with a status-word consumer check #529
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/535"
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-22T10:03:48Z.
Change
Refs #406. This is the first status-word PR. It follows option (b) of the decision on #406:
fnstsw axis accepted when static analysis can prove that every reachable consumer observes only defined condition bits. It follows #530 (the checked entry).Accepted forms.
fcomandfcompwithst(i)or an m32 or m64 operand,fcompp,fucomandfucompwithst(i), andfucompp.fnstsw ax.sahf,jpandjnp.Semantics. C3, C2 and C0 are
0 0 1for less,1 0 0for equal,0 0 0for greater and1 1 1for unordered. The order and NaN rules are those ofx87_compare, whichfcomialready uses. TOP is(8 - depth) mod 8, because a function enters with TOP 0 and the depth before every instruction is fixed.Static analysis (
tools/x87_lower.py,check_flags). A forward fixpoint over the function computes five things before each reachable instruction:At a join, a bit or flag stays known only if it is known on every incoming path. Following the SDM:
fcomi,fcomip,fucomiandfucomipleave them unchanged ("not affected").wait,fnstcwandfnstswitself.After
fnstsw ax, AX has only those bits known. Consumers follow these rules:sahf: sets CF from C0, PF from C2, ZF from C3 and SF from the busy bit. Each flag is defined only when its bit is known. AF (bit 1 of TOP) is left undefined.test ah, imm8andand ah, imm8: define ZF, PF and SF only when the immediate selects known bits.andmakes the bits it clears known.The existing flag rule (from #476) then applies to every function with
fnstsw,sahf,jporjnp. A reader of a flag that an unknown bit left undefined is rejected with... depends on a status-word bit that is not known on every path from the entry.PF. The integer lowering now sets PF only after
sahfand the 8-bit forms oftestandand. It does so only in a function with ajporjnpthat the x87 profile has checked.jpandjnpare rejected when any other instruction may have written PF last, for examplefucomi,cmportest eax, eax. The PF offcomiis left for a follow-up, together withfcmovuandfcmovnu.wait. I kept the SDM's "undefined" forwait, although iced lists it as leaving the condition bits alone. As a result,fstsw ax(wait; fnstsw ax) after a comparison is rejected. This choice is easy to relax if you prefer iced's reading.Generated code. The status variable and
parityare declared only in functions that use them. I compared every generated source against the parent commit, built in the same build directory: all 222 earlier generated.cppfiles are byte-identical.Fixture
tests/cpu/x87_status.asmand.defare generated bytests/cpu/x87_status.py asm, andemitchecks every decoded x87 instruction against its sequence. After eachfnstsw ax, a record reads AH through every accepted consumer, with only flag-neutralmovs in between:sahf, thenje,jne,jb,jae,ja,jbe,jpandjnp;test ahwith 01h, 04h and 40h (C0, C2 and C3) and each TOP bit, thenjne;test ahwith 45h, 44h, 05h and 41h, thenjp;and ah, 45h, stored, thenjp, thencmp ahwith 40h, 01h and 00h, each followed byje.The record then clears EAX. The model compares with the
Fractionmodel ofcpu_aot_x87_compareand shares no code withtools/x87_soft.h.The 22 exports cover:
st(1),fcompp,fucompp,fcom st(2),fucomp st(2), a value against itself, and the four memory forms;fucom st(7)at depth 8, where TOP is 0 again;fcomfollowed byfcomi, andfucomfollowed byfucomip, so C0, C2 and C3 must come from the earlier comparison;fnstsw;fcom st(1)andfucom st(2), chosen by an input dword;mov, andjpaftertest al, al,test byte ptr [m], 0A5handand al, 3Ch.Cases. 17843 cases:
At
fnstsw, 4856 cases compared less, 2265 equal, 4914 greater and 5296 unordered, at TOP 0, 5, 6 and 7. As the decision asked, every record tells ordered inputs from unordered ones through PF withsahf; jp(PF is C2). It also runs the compiler idiomstest ah, 44h; jp(not taken only for equal) andtest ah, 05h; jpandtest ah, 41h; jp.Native-only exports. Two exports read bits that the profile does not know:
status_wordstores the whole word afterfcom. In 1759 runs its known bits matched the model. The other bits took0x0000,0x0001(IE) and0x0002(DE).split_stickyis the branch export withtest al, 1(IE) on the C3 successor, afterfucom. An unorderedfucomleft IE clear for a quiet NaN and set it for a signaling NaN, so that byte does not follow from C0, C2 and C3.Translation rejects both, at the store of AX and at
test al, 1.Negative controls. Seven mutated translations must each mismatch:
c2dropc0c3top0fnstswstores TOP as 0sahfpfsahfloads PF from bit 4oddparitytestandandset PF for odd parityandparityandstatements set PF for odd parityfcomiclearfcomiandfucomipclear C0, C2 and C3andparitymismatches in every case that reads PF, because each record has ajpstraight after itsand. Thesplitcases have no record and do not read PF.Rejections. Translation rejects 32 sequences with the expected diagnostic:
fnstsw axwithout a comparison;fnstsw axafterfadd, afterfldandfstp, afterfnstcw, afterwait(fstsw ax), as a secondfnstsw ax, or after a call;splitexport whose other successor tests C1.test al, 1andtest ax, 4500h;and ah, 47hwith a store of AH;fldcw;jpafterfucomi,cmportest eax, eax, or before any flag writer;sahfin a function without x87 instructions;lahf.fnstswwith an m16 operand;fcom), DC D9 and DE D1 (fcomp);ftst,fxamandficom;Eight twins must still translate:
sahf; jbafterfcom;fcomibetweenfcomandfnstsw;test al, al; jp;and ah, 45hwith a store of AH;xor eax, eax;sahfon AH frommov;fnstsw axin the checked entry afterfldcw.Changes to existing tests. Rejection cases that the profile now accepts are replaced by instructions that stay outside it, and each list keeps its length:
cpu_aot_x87_addfcom st(1),fnstsw axftst,fnstsw m16cpu_aot_x87_subfcom st(1)fxamcpu_aot_x87_mulfcomp st(1)fprem1cpu_aot_x87_comparesahf;fcom st(1),fucom st(1),fucompp;fnstsw axjo;ficomp m32,fcomas DC D1,fucomppwith one value;fnstsw m16cpu_aot_x87_fnstcwfnstsw axfninitcpu_aot_x87_fldcwfnstsw axin the checked entryfnstsw m16in the checked entryjpandjnpafterfucomiincpu_aot_x87_compareare still rejected, now by the PF rule. Their needle names that rule.fnstsw m16incpu_aot_x87_fnstcwnow reportsunsupported x87 operand. Case and control counts of these tests are unchanged. The matching sentences indocs/experiments.mdare updated.Verification
Tested at
4a80919onmainat119bdd6. The machine runs Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0 and Python 3.13 on an AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).4a80919(ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'), including the six with replaced rejection cases.ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 242 passed, 2 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 246 at4a80919. The run includes the GPU tests on an AMD Radeon RX 9070 XT. The two failures aregpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in #369, anddxr_opacity_hardware, the RDNA 4 result recorded in #216.mainat119bdd6gives the same two failures and skips in the same session (241 passed out of 245). This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass at4a80919, locally and in the Linux job of the fork CI run below./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed at4a80919in the fork'sWindows checksrun 45009545338. Both Windows jobs ran and passed all seventeen x87 tests (202 passed and 2 capability skips,bvh_wave64_warpandwave_shuffle_warp, out of 204 in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 432 of 948 forms at4a80919, and 424 atmainin the same session. The added forms arefcom,fcomp,fcompp,fucom,fucomp,fucompp,fnstswandsahf;jpandjnpwere already covered bycpu_aot_flag_conditions.Not measured:
fninitandfclex,ftst,fxamandficom. These can follow with their own semantics and native cases, as the decision on #406 suggests.Overlap
I trial-merged this branch with the 54 open PRs other than #534. Each one conflicts in the same files against
mainand againstmainwith this branch merged: the 5 that merge cleanly intomainalso merge cleanly with it, and this branch adds no conflict to the others. It conflicts with #534 (OF afterfcomi) intools/x87_lower.pyand thecpu_aot_x87_compareparagraph ofdocs/experiments.md; I will rebase whichever of the two lands second. The changes stay insidetools/x87_lower.py, the x87 branch oftools/ffx_version_query.py, the new fixture, the replaced rejection cases of six x87 tests, a CMake block after thecpu_aot_x87_fldcwtest, and the x87 section ofdocs/experiments.md.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the forms, the analysis, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).