feat(relinker): lower FMA under --to-avx #24472
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24472"
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 boykopovar/AnyPS5. Originally posted by SchmashinPrunes on 2026-11-20T17:13:36Z.
What
--to-avxdid not lower FMA3, so a converted title stopped with an illegal instruction at its first FMA on a host with AVX but without FMA, such as Sandy Bridge or Ivy Bridge. This PR lowers all 96 FMA3 forms: VFMADD, VFMSUB, VFNMADD, VFNMSUB, VFMADDSUB and VFMSUBADD, in the 132/213/231 orders, PS/SS/PD/SD, 128- and 256-bit, with register or memory operands. Each one becomes an out-of-line stub that gives the singly rounded result, NaN, sign of an exact zero, DAZ/FTZ behaviour and MXCSR status flags of the native instruction.Two commits:
FmaLowering.cpp): the product of two floats is exact in double precision. The stub adds the addend with TwoSum, rounds the sum to odd from its exact error, and narrows it with VCVTPD2PS in the guest's rounding mode, which gives the singly rounded result. Special cases and flags are computed with vector masks. It uses about 175 instructions (128-bit and scalar) or 300 (256-bit), with thirteen scratch YMM registers saved below the red zone.FmaDoubleLowering.cpp): nothing wider than a double exists, so each lane runs in general-purpose registers. MUL gives the exact 106-bit product of the significands. The addend is aligned against it in a 128-bit window that keeps the shifted-out bits as a sticky bit, and the sum is rounded once in the guest's mode. Tininess is detected after rounding, as on the hardware, and a tiny result is rounded again at the denormal position from the exact sum. It uses only instructions that Sandy Bridge has (BSR, not LZCNT). The branches come from a small label and fixup assembler local to the file.VexStub.hppholds the constant, scratch-register and flag helpers.StubBodyBuilder::RipConstantreferences the constant pool from any instruction, including VEX encodings. The vector execution harness now also loads and stores the general-purpose registers, RFLAGS and MXCSR, so the tests check that a stub leaves them as the native instruction does. Loading the general-purpose registers is opt-in (GeneralRegisters::FromMachine), because the other vector tests address the machine through RDI. Under Rosetta 2 (sysctl.proc_translated), the host FMA sets MXCSR status flags the hardware does not, so there the comparison with the host leaves out the six status flags; the comparison with the model keeps the whole MXCSR.DecodeFmadecodes VEX.W=1 asFmaOperands::Double, andEmitFmapasses those forms toEmitFmaDouble.--to-avxentry andUSAGE.mdno longer list FMA as not lowered.Known gaps, recorded in the debt entries:
Tested
Debian (Linux x86-64), GCC 14.2, Ninja, Release,
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON:ctest --test-dir build-relinkerpassed 1150/1150 after rebasing onto2af8e79edf. No title tested.The new tests are
fma_loweringandfma_double_lowering. Each test compares every form, order and width with an exact model of the instruction, written with arbitrary-width integers independently of the stub. The cases cover register aliasing, memory operands, specials, exact cancellation (c = -a·b ± a few ulps), near-overflow, near-underflow and denormal results, in all four rounding modes and with DAZ, FTZ and pre-set flags. On a host with FMA, each case also runs the native instruction and requires it to match the model, which ties the model to the hardware.fma_lowering,fma_double_lowering(stub, model and native)To check that the tests catch errors, I injected 18 faults into the double-precision stub, one at a time, and
fma_double_loweringfailed for each of them (not committed). The faults were:Measured on both CPUs and modelled as measured: the native instructions do not set DE on an invalid lane (inf × 0, inf − inf), and they detect tininess after rounding.
Under
qemu-x86_6410.0.13:-cpu SandyBridge, both tests pass, checked against the model only.0x380FFFFFFF000000with MXCSR0x9F80gives0x00000000with UE and PE on QEMU, and0x00800000with PE on both CPUs. On a host that does this,fma_loweringskips the 249 cases with an inexact smallest-normal result under FTZ, and it says so in its output. Real CPUs skip none.-cpu Haswellor-cpu max, the native comparison infma_loweringfails because QEMU's own FMA does not set DE on a denormal operand. The stub results are not involved.python3 tools/check_conventions.py --base upstream/main,tools/debt_fold.py --checkandtools/check_test_registration.pypass. With GCC 14 and Clang 19,-Wall -Wextra -Wpedantic -Wshadowreports nothing for the new files.tools/pr_overlap.py --branchfinds no other PR that implements FMA. It lists the shared--to-avxtouch points (CMakeLists.txt, the instruction matcher,USAGE.md), and #23747 for neighbouring lines inStubBodyBuilderandVectorExecution.hpp.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Summary by CodeRabbit
--to-avxusage guide with supported FMA instruction forms and details about their floating-point behavior.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T17:30:10Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: clarify the double-precision stub’s unmasked-exception behavior, which is explicitly documented as not trapping.
What I ran
Reviewed head
f41364f9985bb364e6787d809e963ded174cf1a4. I read the prepared description, commit list, manifest, diff, and changed source for the decoder, both lowerings, stub builder, and vector execution harness. I did not run commands or tests; test results in the description are author-reported.Results
Findings
Blocking
None.
Non-blocking
core/relinker/codegen/src/x86/FmaDoubleLowering.cpp:741-780.Not verified
core/relinker/codegen/src/x86/FmaLowering.cpp,core/relinker/codegen/src/x86/FmaDoubleLowering.cpp,core/relinker/codegen/src/x86/StubBodyBuilder.cpp, andcore/relinker/codegen/tests/VectorExecution.hpp; the other changed files listed in the manifest were not read.To get this to a good state
Nothing is required. Optional: surface the unmasked-exception limitation clearly wherever users are told these FMA forms are lowered.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T18:38:11Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
f41364f998:tools/check_conventions.pypassed.fma_double_lowering_tests,fma_lowering_testsbuilt cleanly.fma_lowering,fma_double_lowering):core/relinker/codegen/include/codegen/x86/StubBodyBuilder.hpp,core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp,core/relinker/codegen/src/x86/StubBodyBuilder.cpp,core/relinker/codegen/src/x86/FmaDoubleLowering.cpp,core/relinker/codegen/src/x86/FmaLowering.cppreverted tomain, the new tests do not build/link without the change; restoring it makes them build and pass.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T04:31:11Z.
Review: CHANGES_REQUESTED
Reviewed — comments
Changes are required before merge: the invalid-operation path drops previously accumulated exception flags, and the review is incomplete because several changed files were not read.
What I ran
f2e1abea6a5ab83c3c7c1353510184af7de2a538and targeted source ranges throughrepo_read.py.Results
Findings
Blocking
core/relinker/codegen/src/x86/FmaDoubleLowering.cpp:725assignsR14 = kInvalidFlagwhen an invalid product or infinity cancellation occurs.R14already holds flags raised for denormal operands earlier in the same lane (:441-443and:374), so invalid cases with a denormal operand lose the denormal-operand flag. Preserve the accumulated flags when adding the invalid flag.Non-blocking
Not verified
FmaDoubleLowering.cpp,FmaLowering.cpp, either FMA test, or all other changed files. The prepared diff lists 13 changed files; all are marked included in the manifest, but full review of every changed line is incomplete.To get this to a good state
fma_double_loweringand review the remaining changed files at the same head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T13:08:47Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
c42c5bc61cb4662694d2bc5215f02c083d7ed16d. No commands or tests were run;repo_read.pywas used to inspect the prepared diff and repository context. The PR description reports test results, which were not independently run.Results
Findings
Blocking
None identified in the inspected code.
Non-blocking
--to-avxusage note so users can account for them.Sources
Not verified
No tests were run. I read all files listed as reviewed in the manifest; none were excluded. Runtime behavior on physical consoles, the reported benchmark results, and the author's test runs were not independently verified.
To get this to a good state
Nothing is required. Optional: document the unmasked-exception and precise-fault limitations with the
--to-avxusage note.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T13:18:54Z.
Review: COMMENTED
Actionable comments posted: 5
Architecture findings (2)
Advisory architecture findings (2)
5a1ba830-ed75-4bdc-9430-6fe122b91685Reviewing files that changed from the base of the PR and between
2af8e79edfandc42c5bc61c.core/relinker/CMakeLists.txtcore/relinker/codegen/include/codegen/x86/FmaLowering.hppcore/relinker/codegen/include/codegen/x86/StubBodyBuilder.hppcore/relinker/codegen/include/codegen/x86/VexStub.hppcore/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cppcore/relinker/codegen/src/x86/FmaDoubleLowering.cppcore/relinker/codegen/src/x86/FmaLowering.cppcore/relinker/codegen/src/x86/StubBodyBuilder.cppcore/relinker/codegen/tests/FmaDoubleLoweringTests.cppcore/relinker/codegen/tests/FmaLoweringTests.cppcore/relinker/codegen/tests/VectorExecution.hppdocs/dev/debt/changes/feat-fma-ps-to-avx.mddocs/user/USAGE.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T13:30:46Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: preserve the incoming RSP in the double-precision FMA stub.
What I ran
Reviewed head
764d7fd67ae9a149874b110fa61e9a1b5bce2343. I read the prepared description, commits, manifest and diff, plus the targeted lowering, stack helper, matcher context and execution harness. No commands or tests were run.Results
Findings
Blocking
core/relinker/codegen/src/x86/FmaDoubleLowering.cpp:741— The stub subtractskFramefrom RSP but never saves the incoming RSP; itskSavedset at line 217 omits RSP, and the final adjustment at line 788 only adds the frame size. Consequently, the stub returns with RSP changed by 448 bytes, violating the calling convention and corrupting execution. The vector test harness loads a sentinel into RSP for this mode, so the new double-precision test should also expose the mismatch. Preserve and restore the incoming stack pointer, or use an equivalent frame strategy that leaves it unchanged.Non-blocking
None.
Not verified
To get this to a good state
Restore the incoming RSP in the double-precision stub, then rerun
fma_double_loweringand the relevant relinker test suite on the updated head.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T13:39:15Z.
Review: COMMENTED
Actionable comments posted: 4
Architecture findings (2)
Advisory architecture findings (2)
e25edce3-7d67-41cd-9020-59990e456e3dReviewing files that changed from the base of the PR and between
c42c5bc61cand764d7fd67a.core/relinker/codegen/tests/FmaDoubleLoweringTests.cppcore/relinker/codegen/tests/FmaLoweringTests.cppcore/relinker/codegen/tests/LoweringExecution.hppcore/relinker/codegen/tests/VectorExecution.hppdocs/user/USAGE.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T14:08:20Z.
Review: COMMENTED
Reviewed — changes requested
Changes are required before merge: preserve the guest’s RFLAGS when returning from the double-precision FMA stub.
What I ran
Reviewed head
d69d2741cb137deea4ca9543c7fda54d0a6cf1f4using the supplied review documents and pinned-head readerrepo_read.py. I did not run tests or build commands.Results
Findings
Blocking
core/relinker/codegen/src/x86/FmaDoubleLowering.cpp:778restores saved flags before loading the result vector and restoring the scratch vector. Those VEX loads update the AVX upper-state tracking (and subsequent instructions may affect flags); the generated stub does not restore RFLAGS at its final exit. In particular,StackSlotat line 788 adjusts RSP with LEA and is flag-neutral, but the vector loads at line 782 and 783 occur after POPFQ and may alter the guest-visible state via AVX state; more directly, loading the vectors does not modify arithmetic RFLAGS, so this is not a confirmed RFLAGS defect. Finding withdrawn: inspection does not support a concrete blocking issue here.Non-blocking
Not verified
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T14:18:42Z.
Review: COMMENTED
Actionable comments posted: 3
Architecture findings (1)
Advisory architecture findings (1)
52f47aa6-4d9a-4a97-8c39-e8c22b08453fReviewing files that changed from the base of the PR and between
764d7fd67aandd69d2741cb.core/relinker/codegen/tests/FmaDoubleLoweringTests.cppcore/relinker/codegen/tests/FmaLoweringTests.cppcore/relinker/codegen/tests/LoweringExecution.hppcore/relinker/codegen/tests/VectorExecution.hppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T14:50:19Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: fix the alternating addend-sign selection in the double-precision FMA lowering.
What I ran
Inspected the prepared diff and changed code at head
b0e38eb33c4624b8afed7f4ece4f20bd47fe33d3usingrepo_read.py. No build or tests were run here. The author reports 1,150/1,150 tests passing on Debian, plus model/native comparisons on two CPUs; these are author-reported results.Results
Findings
Blocking
VFMADDSUB, even lanes need an unnegated addend and odd lanes a negated addend;VFMSUBADDuses the opposite pattern. The sign-toggle branch in the double lowering tests the lane index and skips toggling for evenVFMADDSUBlanes, but toggles odd lanes; forVFMSUBADD, it toggles even lanes and skips odd lanes. This reverses both operations’ required signs, so affected double-precision forms compute the wrong result. Correct the condition atcore/relinker/codegen/src/x86/FmaDoubleLowering.cpp:451and add an explicit expected-result case for both operations.Non-blocking
None.
Not verified
I did not run the tests or verify behavior on physical hardware. The PR’s reported test results and performance measurements were not independently verified. The changed files listed in the review bundle were included; I read the description, commits, manifest, diff, matcher, both lowerings, the stub builder, test harness, and relevant test sections. No changed file was excluded according to the bundle listing.
To get this to a good state
Correct the double-precision alternating addend-sign condition and rerun the FMA lowering tests.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T14:53:44Z.
Review: COMMENTED
Actionable comments posted: 2
Architecture findings (1)
Advisory architecture findings (1)
5100e26e-74ae-48b4-b38c-f41649169f96Reviewing files that changed from the base of the PR and between
d69d2741cbandb0e38eb33c.core/relinker/codegen/include/codegen/x86/StubBodyBuilder.hppcore/relinker/codegen/tests/FmaDoubleLoweringTests.cppcore/relinker/codegen/tests/FmaLoweringTests.cppcore/relinker/codegen/tests/LoweringExecution.hppcore/relinker/codegen/tests/VectorExecution.hppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T15:06:46Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed change is pinned to
357f0ab5228b0d1b2d7e3ffdaba454e7cc3495a6; no blocking defect was verified, but the implementation and tests were not fully reviewed or run, so a formal approval is not justified.What I ran
Results
Findings
Blocking
None verified in the sections reviewed.
Non-blocking
None.
Sources
Not verified
The full contents of both test files and several other changed files were not read. The author-reported test, compiler, and hardware results were not independently run. The changed-file listing showed all 14 changed files as included; no excluded changed files were listed.
To get this to a good state
Nothing is required. Optional: finish reviewing the remaining changed-file contents and run the registered FMA tests on the pinned head before making a formal decision.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T15:16:35Z.
Review: COMMENTED
Actionable comments posted: 2
Architecture findings (2)
Advisory architecture findings (2)
65ac71c7-4f21-454e-9ad6-9027aab86bdcReviewing files that changed from the base of the PR and between
b0e38eb33cand357f0ab522.core/relinker/codegen/include/codegen/x86/StubBodyBuilder.hppcore/relinker/codegen/tests/FmaDoubleLoweringTests.cppcore/relinker/codegen/tests/FmaLoweringTests.cppcore/relinker/codegen/tests/VectorExecution.hppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:53:48Z.
Review: COMMENTED
Built this head with
ANYPS5_RELINKER_ONLY=ONand ranfma_loweringandfma_double_loweringon Linux x86-64. Both passed, covering the single- and double-precision lowering paths.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.