Skip to content

series: cut FPCR writes at ARM64EC transitions, keep AFP on (0013, 0014) - #12

Open
utkarshdalal wants to merge 2 commits into
gamenativefrom
exitfunctionec-skip-fpcr-write
Open

utkarshdalal wants to merge 2 commits into
gamenativefrom
exitfunctionec-skip-fpcr-write

Conversation

@utkarshdalal

@utkarshdalal utkarshdalal commented Nov 15, 2026 •

Copy link
Copy Markdown

What

Two patches that cut the FPCR writes FEX makes on every switch between emulated x86 code and native ARM64EC code (Wine, DXVK). AFP stays on by default where the CPU supports it, and floating-point results don't change. SERIES_VERSION goes to 8.

  • 0013: ExitFunctionEC (Source/Windows/ARM64EC/Module.S) cleared FPCR.NEP and AH on every exit to native code with an unconditional msr. It now checks first and skips the write when both bits are already clear.
  • 0014: the ARM64EC entry from native code no longer sets NEP and AH. The JIT sets them at the head of any block that has an op whose host code depends on them. So a call out to native code that returns into FP-free guest code (calls, integer logic, memcpy, locks) costs no FPCR write in either direction.

Why

On an 8 Elite, an FPCR write that changes a bit costs about 8 ns (2 per round trip). Reading FPCR and branching costs nothing measurable. On the 8 Gen 2 the same writes cost under 2 ns, and NEP/AH are not implemented there.

In A Hat in Time on an AYN Odin 3 (8 Elite), the entry write and the exit write took about 26% of the main thread's cycles.

How 0014 stays correct

  • Which ops count. The IR generator gives every op a UsesHostFPMode property, built from IR.json and failing closed:
    • Counted: everything in VectorScalar, F64 and F80, plus any unknown class. Also every VF* vector op, FP conversions, FP compares (FCmp, Select, Float_ToGPR_*), and the rounding-mode ops, which save and restore the whole FPCR.
    • Treated as FP-free: integer ALU, memory, atomics, branches, moves, integer SIMD, crypto, and the GPR↔vector moves.
    • An op can override its class with a UsesHostFPMode field in IR.json.
    • This was checked on the 8 Elite by toggling NEP around each instruction. NEP changes fadd, fabs, fcmeq, scvtf/ucvtf and fcvt. It does not change fmov (any form), ins, loads, integer scalar SIMD (add, cmeq, shl, abs, sqadd, sqxtn, addp), fcsel, addv/umaxv, pmull or sha1h.
  • Where the guard goes. It sits right after the block's Target label, which every entry passes: dispatcher lookups, block links, call-ret returns (they land on the return block's entry point, before Target), and jumps inside the multiblock.
    • The guard is mrs, tbnz on NEP, and only if clear orr + msr. It doesn't touch NZCV.
    • Calls out of JIT code in the middle of a block still restore the bits when they come back (FillStaticRegs, unchanged).
  • Spill. SpillStaticRegs skips its FPCR write when NEP, AH and FIZ are already clear. It checks with and + cbz, so NZCV is untouched before it gets saved.
  • Caches. Blocks carry their guards in both modes, so compiled and cached code is valid whether the entry is lazy or eager.
    • The code cache already rejects caches from another FEX commit.
    • The disk cache format version goes 42 → 43 because codegen changed.
  • Opt-out. FEX_EXP_EAGERAFP=1 makes the entry set the bits again, as before.

Results (AYN Odin 3, Snapdragon 8 Elite)

Correctness. An x86-64 test program calls a native import (QueryPerformanceCounter, GetLastError, ...) between FP operations:

  • It covers scalar add/sub/mul/div, sqrt/rcp/rsqrt, minss/maxss/minps/maxps with NaN and ±0, denormals, conversions, comparisons, double and packed ops, and switches MXCSR between FTZ-only and FTZ+DAZ.
  • It hashes every result bit, including the upper lanes, and ran in a GameNative custom-game container with Proton 10.
Build Result hash Native call, integer work around it Native call, one FP op around it
2611-gn-6 (current) 7556e233fe8b6de3 63 ns 66 ns
This series, lazy (default) 7556e233fe8b6de3 22 ns 50–54 ns
This series, FEX_EXP_EAGERAFP=1 7556e233fe8b6de3 76 ns 82 ns
2611-gn-6, FEX_HOSTFEATURES=disableafp 0ced81a8a5c63803 27 ns 26 ns

The lazy build matches the current results bit for bit. Disabling AFP changes them, which is why AFP stays on.

A Hat in Time, title screen. 60 fps cap, Proton 11.0-2 arm64ec-6, same FEX 2611-gn-6 base with only the DLL swapped, simpleperf over 15 s:

Main thread cycles Whole process cycles Top FPCR write sites, main thread
2611-gn-6 37.2 G 71.7 G entry msr 15.3%, exit msr 11.1%
This series 32.7 G (−12%) 62.9 G (−12%) exit msr 2.4%, then block guards at ≤1.5% each

The writes that remain are the necessary ones: FP code on both sides of a native call. The title screen rendered the same in both runs. These are single runs on a warm device (about 90 °C), and cycles are less frequency-sensitive than time, but this is not a gameplay benchmark.

Checks

  • The full series (0001 to 0014) applies with apply.sh on a clean FEX-2611, and the result is identical to the tree built and tested above.
  • The ARM64EC DLL builds with llvm-mingw 20251007 (Release, the same flags as CI).
  • Not yet run: the WoW64 build (only the CI matrix builds it; nothing ARM64EC-specific changed there besides the guard, which stays a no-op where the entry is eager), an 8 Gen 2 device (no AFP, so the guards are not emitted), and gameplay beyond the title screen. Adb input didn't get past "Press any button".

@utkarshdalal utkarshdalal changed the title series: skip the FPCR write in ExitFunctionEC when NEP and AH are already clear (0013) series: cut FPCR writes at ARM64EC transitions, keep AFP on (0013, 0014) Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant