cpu: lower FNSTCW, FSTCW and WAIT in the x87 profile #507
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/513"
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-22T08:40:00Z.
Change
Refs #406. This follows #499 (the integer-operand arithmetic). It adds the control-word store and
waitto the opt-in x87 profile inlower(..., x87=True):fnstcw m16,fstcw m16andwait. This is the fixed-control-word part of the next step discussed in #406;fldcwand the dynamic control word come separately.Semantics.
fnstcw m16: stores the profile's control word,0x027F, in two bytes. It does not touch the register stack.wait: does nothing. Every exception is masked, so none can be pending.fstcw m16: iced decodes it aswaitfollowed byfnstcw, and the profile lowers the two in turn.The native runs confirm each of these rules.
Out of scope:
fldcwstays rejected, also afterfstcw, along with every status-word form (fnstsw,fstsw,fnclex) andfninit.Lowering (
tools/x87_lower.py).fnstcwbecomesstore<uint16_t>(a, 0x027f);, andwaitbecomes an empty statement. Neither changes the stack depth. No helper is added totools/x87_soft.h.Fixture
tests/cpu/x87_fnstcw.asmand.defare generated bytests/cpu/x87_fnstcw.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names, including thewaitandfnstcwthatfstcwdecodes into. The.asmregisters its test on its first line (; coverage: source cpu_aot_x87_fnstcw). The 6 exports cover:fnstcwalone andfstcwalone;fnstcwandfstcwbetween two m80 loads, anfdivpand the final store;waitbefore, between and after loads, anfdivpand the final store;fstcwbeside the values of a full stack of eight, all stored afterwards.Model. The model writes
0x027Ffor each control-word store and divides with theFractionmodel ofcpu_aot_x87_div. It shares no code withtools/x87_soft.h. The harness, input bytes, guards and live-value check are those of the earlier x87 PRs. The live-value check enters each export with a caller value and control word0x0F7Floaded, and the harness switches to0x027Faround the call, sofnstcwmust store the profile's word, not the caller's.Cases. 1776 cases:
Negative controls. Four mutated translations must each mismatch:
fninit0x037F, the wordfninitloadswideskiplowbyte1068 is every case of the five exports that store the word. The
waitexport has no store, so no control changes it. Awidestore at the end of the output block counts as a mismatch because it writes into the guard.Rejections. Translation rejects 5 sequences with the expected diagnostic:
fldcw m16,fldcwafterfstcw,fnstsw ax,fnstsw m16andfnclex.Changes to existing tests.
cpu_aot_x87_addusedwaitas a rejection case. It becomesffree st(1), which stays outside the profile, so the test keeps 17 rejections. Its case and control counts are unchanged. The profile sentence indocs/experiments.mdnow namesfldcwandffreeamong the rejected instructions instead ofwait.Verification
Tested at
1ba6f04onmainat14e5456. The machine runs 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).1ba6f04(ctest --preset dev --no-tests=error -R '^cpu_aot_x87_'), includingcpu_aot_x87_addwith its replaced rejection case.ctest --preset dev --no-tests=error -j 8 --output-junit test-results.xmlgives 231 passed, 2 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 235 at1ba6f04. 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.mainat14e5456gives the same two failures and skips in the same session (230 passed out of 234). This change touches no GPU code.tests/form_coverage_check.py,tests/docs_links_check.pyandtests/bidi_controls_check.pypass at1ba6f04locally and in the Linux job of the fork CI run below./O2build-and-test, Linux Python and gfx1013 LLVM jobs passed at1ba6f04in the fork'sWindows checksrun 45003191737. Both Windows jobs ran and passed all fifteen x87 tests (192 passed and 2 capability skips,bvh_wave64_warpandwave_shuffle_warp, out of 194 in each).Coverage.
python tools/form_coverage.py out/build/dev/test-results.xmlgives x64 373 of 948 forms at1ba6f04, and 370 onmainat14e5456in the same session. The added forms arefnstcw,fstcwandwait.Not measured:
wait, which the fixed control word cannot produce.Overlap
I trial-merged this branch with all 72 open PRs. Each one conflicts in the same files against
mainand againstmainwith this branch merged: the 9 that merge cleanly intomainalso merge cleanly with it, and this branch adds no conflict to the others. The changes stay insidetools/x87_lower.py, the new fixture, one rejection case oftests/cpu/x87_add.py, a CMake block after thecpu_aot_x87_iarithtest, and the x87 section ofdocs/experiments.md.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the three forms, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).