Repository navigation
cpu: lower x87 register moves, FCHS and FABS in the x87 profile - #443
Merged
Merged
Conversation
The opt-in x87 leaf profile now accepts FLD, FST and FSTP with an ST(i) operand, FST with an m32 or m64 operand, FXCH ST(i), FCHS and FABS. Register copies keep the full 80-bit encoding; FCHS and FABS change only the sign bit. The undocumented FSTP and FXCH alias encodings stay rejected. cpu_aot_x87_moves compares 26 native and translated exports against an independent raw-encoding model, with seven negative controls. AI-assisted (Claude Code).
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Change
Refs #406. This follows #422 (add), #431 (multiply), #433 (divide) and #440 (subtract). It adds register data movement to the opt-in x87 profile in
lower(..., x87=True). The profile now also accepts:fld st(i): pushes a copy ofst(i);fst st(i): copiesst(0)intost(i);fstp st(i): the same copy, then a pop;fxch st(i): exchangesst(0)andst(i);fchsandfabs;fstwith an m32 or m64 operand: storesst(0)without popping.Out of scope: constants (
fld1,fldz, ...), integer conversions (fild,fist, ...), comparisons,ffreeandfincstp/fdecstp. They remain rejected, and the check confirms it for several of them. The entry/exit contract and the static stack depths are unchanged.Lowering (
tools/x87_lower.py). Each new form is a static slot operation, since the stack depth is known at translation time:fld st(i):x87_slot[d] = x87_slot[d-1-i];fst st(i)andfstp st(i):x87_slot[top-i] = x87_slot[top], andfstpthen lowers the depth;fxch st(i): a swap of two slots.Rejections (
tools/x87_lower.py).st(i)operand at or beyond the current depth is rejected as an underflow, andfld st(i)at depth 8 as an overflow.fstp st(i)(DF D0+i,DF D8+i) and offxch st(i)(DD C8+i,DF C8+i). Assemblers do not emit them and the fixture does not measure them, so translation rejects them by encoding (ALIASES).Helpers (
tools/x87_soft.h).x87_chsandx87_abs: these change only the sign bit, of every encoding. Zeros, NaNs (signaling stays signaling), unnormals and pseudo-denormals keep their other bits.fst: register copies move the whole 80-bit value.fstm32/m64 reuses the existingx87_to_f32/x87_to_f64rounding offstp.Fixture
tests/cpu/x87_moves.asmand.defare generated bytests/cpu/x87_moves.py asm, andemitchecks that every decoded x87 instruction is the form its sequence names. The 26 exports cover:fld st(0),st(1)andst(2), andfld st(6)at depth 7;fst st(1)andst(2);fstp st(0),st(1)andst(2);fxch st(0),st(1)andst(2), andfxch st(7)at depth 8;fchsandfabs, each stored to m80, m64 and m32, andfchs; fabs; fchs;fstm32 and m64, andfst m64; fst m32, each followed byfstpm80 of the register;a + (-b),|a| * |b|, andfsubpafterfxch.The depth-7 and depth-8 exports load eight m64 values and store all eight.
Harness and checks. As in the earlier x87 PRs:
fnsave/frstorharness with control word0x027F;The model in
tests/cpu/x87_moves.pymoves raw encodings and shares no code withtools/x87_soft.h. For the three arithmetic exports it reuses theFractionmodels of the add, multiply and subtract tests.Cases. 20224 cases:
cpu_aot_x87_addand 200 random values through the one-value exports;Negative controls. Seven mutated translations must each mismatch:
chssubfchscomputes0 - xabsnanfabsleaves -0 and negative NaNs unchangedfxchnonefxchdoes nothingfldtopfld st(i)always copiesst(0)fstpdropfstp st(i)only popsfstwritefstm32/m64 also writes the rounded value back tost(0)copyquietRejections. Translation rejects 16 sequences with the expected diagnostic:
fld st(1)with one value, andfld st(7)with seven values;fld st(0)at depth 8;fst st(2)with two values;fstp st(0)on an empty stack;fxch st(1)with one value;fchsandfabson an empty stack;DF D1,DF D9,DD C9andDF C9;fist m32,fld1,ffree st(1)andfdecstp.Changes to existing tests. Three rejection cases of the earlier x87 tests used forms that this PR accepts:
cpu_aot_x87_add'sfld st(1)becomesfild m16;cpu_aot_x87_sub'sfchsandfabsbecomefrndintandfscale.Each replacement is still outside the profile, so every test keeps its number of rejections. Their case and control counts are unchanged.
Verification
Tested at
2236668onfb7547c. 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).x87_add,x87_mul,x87_div,x87_sub,x87_moves).ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 165 passed, 1 failed and 2 capability skips out of 168. The run includes the GPU tests. The failure isgpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #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.2236668in the fork'sWindows checksrun 44981173792. All five x87 tests ran and passed there (tests 83 to 87 of 140).Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 131 of 948 forms, and 127 withoutcpu_aot_x87_moves. The added forms arefst,fxch,fchsandfabs.fldandfstpwere already covered.Not measured:
st(3)tost(7)operands, apart fromfld st(6)andfxch st(7).fxch,fchsandfabs, as in cpu: lower an opt-in x87 leaf profile with FADDP #422.Overlap
I trial-merged this branch with all 46 open
cpu:PRs. Every merge that is clean againstmainis also clean against this branch. The changes stay inside the x87 files, oneCPU_FIXTURESline after"x87_sub", a CMake block after thecpu_aot_x87_subtest, and the x87 section ofdocs/experiments.md. No open PR touches x87.Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the data-movement forms, the alias rejection, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).