Repository navigation
cpu: lower an opt-in x87 leaf profile with FADDP - #422
Conversation
lower(..., x87=True) accepts FLD and FSTP with m32, m64 and m80 operands and FADDP ST(i), ST(0) at stack depths fixed at translation time, from control word 0x027F and an empty stack to an empty stack at return. Any other x87 instruction, stack overflow or underflow, join mismatch, unreachable x87 instruction or live x87 state at a call, return, indirect branch or external edge is rejected. Arithmetic is integer-only in x87_soft.h: 53-bit rounding with the extended exponent range, exact m80 copies, SDM Table 4-7 NaN selection. cpu_aot_x87_add compares 16 exports over 8085 cases through a native harness that fixes the entry state and reports the exit state, with input and output guards and six mutated controls. Refs R0Y00#406 AI-assisted (Claude Code).
|
One harness issue at |
x87_call kept only the caller's environment (FNSTENV/FLDENV), so a live caller value in the register a fixture reuses came back overwritten. It now uses FNSAVE/FRSTOR. x87_seeded enters each export with a live ST(0) value and control word 0x0F7F and checks both on return; the old environment-only harness is kept as a control that must lose the value. AI-assisted (Claude Code).
|
@R0Y00 Fixed in New wrapper check.
Negative control. The previous environment-only harness is kept as The rest of the output is unchanged. AI-assisted (Claude Code). |
Change
Refs #406. This is the first PR of the plan agreed there: an opt-in x87 leaf-function profile in the shared
lower()with one register-add form. Multiplication, division, conversions, constants, comparisons (with PF) and stack faults are left for later PRs, each with its own evidence.Interface.
lower(..., x87=False)is the default and still rejects every x87 instruction, so existing callers are unchanged. Withx87=Truethe profile accepts:fldandfstpwith an m32, m64 or m80 memory operand;faddp st(i), stforifrom 1 to 7.Stack model (
tools/x87_lower.py).0x027F(53-bit precision, round to nearest-even, all exceptions masked), TOP 0, eight empty registers and a clear status word. The function must return with the stack empty.x87_slot[n], so no TOP exists at run time.Rejected at translation:
fldcw,fnstsw,fninit,fnstenvandwait, plus register forms offld,fadd st, st(i)andfaddp st(0), st;faddpwithout enough values;Arithmetic (
tools/x87_soft.h). The helpers use integers only, with no host floating point.Accepted encodings. Every 80-bit encoding is accepted as an operand, so there is no restriction to canonical finite values; the special-value semantics above are implemented and checked. The status word, exceptions and the other rounding modes are not modelled. This is safe only because the profile rejects every instruction that could observe or change them.
Measured, not taken from the SDM: when two NaNs of the same kind have equal significands, the positive one wins in either operand order. My first model returned the destination operand, and 7 of 8061 native cases disagreed, all of them
-NaN(x) + +NaN(x).Fixture
tests/cpu/x87_add.asmand.defare generated bytests/cpu/x87_add.py asmfrom theSEQUENCEStable. The 16 exports are:faddp st(2), stand thenfaddp st(1), st.Harness.
tests/cpu/x87_harness.asmis native-only and never translated;CPU_FIXTURESmaps it toNone. For each call it:fnsave, environment and all eight register values, which also leaves TOP 0 and an empty stack;0x027F;frstor.Harness check.
x87_seededenters each of the 16 exports with a live caller value in ST(0): pi with all 64 significand bits, held in the physical register each export reuses. It also loads control word0x0F7F(64-bit precision, round toward zero). After the harness returns, it requires:A control harness,
x87_call_envonly, keeps only the environment (fnstenv/fninit/fldenv). It must lose the value around every export, and it does: the register comes back holding the fixture's last value.The native and translated exports both run through it with identical input bytes.
Check.
tests/cpu/x87_add.py checkcompares each 64-byte output block, plus 64 guard bytes on each side of the input and the output, against an independentfractions.Fractionmodel. It requires every export to return with control word0x027F, TOP 0 and all tags empty. The 8085 cases include:Negative controls. Six mutated translations of the same image must each mismatch:
pc64doublem80doublenostickynanordersignedzeroRejections. The check also confirms that the default
lower()rejectsfld. It then confirms thatx87=Truerejects 16 sequences outside the profile, each with its expected diagnostic.Verification
Tested at
cace6d2one5e0079(it merges cleanly withac48a50). 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).ctest --preset dev --no-tests=error -R '^cpu_' -j 8gives 44 of 44 passed.ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xmlgives 145 passed, 1 failed and 2 capability skips (bvh_wave64_warp,wave_shuffle_warp) out of 148. 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.pyandtests/docs_links_check.pypass.cace6d2in the fork'sWindows checksrun 44971715289.cpu_aot_x87_addran and passed there (test 70 of 120). CI runs without-V, so the runner's CPU vendor is not in the log.Coverage.
python tools/form_coverage.py out/test-results.xmlgives x64 58 of 948 forms, and 55 withoutcpu_aot_x87_add. The added forms arefld,fstpandfaddp. The harness is native-only, so itsfnstenv,fninit,fldcwandfldenvare not counted.Not measured:
faddp st(3)tost(7), st. Onlyst(1)andst(2)are exercised; the others use the same slot arithmetic.Overlap
The shared interfaces are
lower()intools/ffx_version_query.pyandCPU_FIXTURESintools/form_coverage.py. I trial-merged this branch with every opencpu:PR.mainis also clean against this branch, including cpu: lower 32-bit and 64-bit BMI2 instructions #287, cpu: lower 32-bit and 64-bit integer bit-scan, bit-count, byte-swap, and bit-test instructions #289, cpu: lower 32-bit and 64-bit ROL, ROR, SHLD, and SHRD instructions #291, cpu: lower CBW, CWDE, CDQE, CQO, MOVSX, and ADC instructions #326, cpu: lower unsigned and equality CMOVcc and SETcc forms #328, cpu: lower 32-bit and 64-bit MUL, DIV, IDIV, XADD, and CMPXCHG instructions #331, cpu: lower 32-bit and 64-bit ADX instructions (ADCX, ADOX) and SETO #335, cpu: lower SSSE3/SSE4.1/SSE4.2 insert/extract, ptest, misc integer, and crc32 #377, cpu: lower RCL, RCR, CLC, STC and CMC in the shared lifter #379, cpu: lower XCHG in the shared lifter, atomically for memory #386, cpu: lower AVX broadcast, permute, shuffle, blend, dot, round, and vzeroall instructions #390, cpu: lower LOOP, LOOPE, LOOPNE, JRCXZ and their ECX forms #403, cpu: lower VEX AVX packed shifts, packs, unpacks, shuffles, blends, and insert/extract #404, cpu: lower MOVBE in the shared lifter #412 and cpu: lower XLATB in the shared lifter #415. To keep cpu: lower AVX broadcast, permute, shuffle, blend, dot, round, and vzeroall instructions #390 and cpu: lower VEX AVX packed shifts, packs, unpacks, shuffles, blends, and insert/extract #404 clean, the depth check sits right after theallowedset, away from their vector setup. To keep cpu: add AVX packed-integer and bit-move leaf AOT translators #373 clean, the docs section sits before## Mapping an image without LoadLibrary.string_op()directly abovedef lower, so the keyword-onlyx87=Falseon the signature line conflicts textually. I'll rebase whichever of the two lands second.mainalready, so they were not compared.No open PR touches x87.
Evidence
The fixture is synthetic and original to this repository (MIT).
docs/experiments.mddocuments the profile, its accepted encodings, its rejection boundaries, the controls and the reproduction command.Checklist
AI-assisted (Claude Code).