WIP: perf(lift): the lifted code's floats as host floats, without FPSR bookkeeping the game never reads (RX_FP_NATIVE) #2235
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
6 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2235
Loading…
Reference in a new issue
No description provided.
Delete branch "dyst/Longshot:perf/fp-native"
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?
Summary
The lifter keeps every float result of the game bit for bit with Arm including FPSR's cumulative exception flags
(IOC, DZC, OFC, UFC, IXC, IDC). Each float op computes the native result and then proves it raised no flag FPSR lacks,
else runs a software body. The fast float regions (
nx2/fastfp.py) run their ops asasm volatileinside an MXCSRwindow, with a register snapshot and an exact copy of the whole region to fall back to. The vector forms (
nx2/vecfp.py)keep a lane only where it is provably Arm's. That is about 730,000 sites in the lifted code. It comes from the a64-kit and
suyu translator's standard, "registers + NZCV + V regs + FPSR + memory: 0 differences from Unicorn", which a general
translator needs because it cannot know whether a program reads FPSR.
This game doesn't read it during play:
memcpy(f_8575d1d4,f_8575d1e4,f_8575d1fc,f_8575d214,f_857f71f0) and two others (f_851109b8,f_85110e58). None of them is innx2/ran_funcs.txt, the coverage of a 100-minute play session (77,725 functions).host/hos_thread.c).0x9fin play, every flag set (#2036 found the same), so the bookkeeping keeps re-proving flags that stay set.FPCR is different and is kept: the game sets flush-to-zero itself (
f_8169d350, a setFlushToZero that play runs).This PR compiles the lifted code against copies of the lift's two float headers (
nx2/fp_native.py, written bynx2/build.pyand put ahead of the lift's on the include path) in which:RECOMP_FPX_INHIBITtoken each such write ends with) and at a fiber's start, which inherits its creator's FPCR (
fiber_main).Conversions, min/max, compares and the FRECPE/FRSQRTE estimates keep their exact code. The translation itself doesn't
change (compile only, not in the lift key).
RX_FP_NATIVE=0builds the headers as before.The result: Market +12% fps and Lake Hylia +7% on a Ryzen 9 3900X; the game's own loop is 10-22% shorter, and at the
lake the render chain then becomes the limit. The executable is 61 MB (10%) smaller.
Read first:
nx2/fp_native.py, the hunks innx2/build.py,fiber_maininhost/hos_thread.c.Related open work, which keeps FPSR exact and which this would change:
FPSR handling (lanes kept for their flags, MXCSR read-backs, flag declines) and their gains against main would change.
No PR, issue or review found gives a reason FPSR must stay exact. Prior work searched: FPSR, fastfp, fpx, MXCSR,
"native float", vecfp, fpxkeep, "exact float".
Testing
Tested: Windows 11, Ryzen 9 3900X, RTX 3080 10 GB (NVIDIA 621.14), 3440x1440 window at 67% render resolution,
V-Sync off, uncapped. A/B by
dev.py abagainst the base of v3.1.1 (a2dc5385): the Market and Lake Hylia, 2 runs a sideeach, interleaved (at
659c7ebe; the head only adds the fiber's MXCSR at its start), plus a 30 s smoke pair at the Market on the head,388fa3cb(with the fiber change). Every run reached its scene and played its 60 s; no crash,nothing wrong seen in the window. Played by hand at the Market for a few minutes, uncapped.
Still to test:
sessions (all ages, cutscenes, minigames, saves and loads), expecting no line (by hand, then the Test Farm).
@Beanstalkthe Market and Lake Hylia A/Bagainst main, expecting a gain of the same order and no crash.
nothing different from main (by hand).
mise run test(by hand).Measurements
Verdict: +12% fps at the Market and +7% at Lake Hylia; every run of this PR beats every run of main in both scenes.
Two runs a side: the base drifts a few fps between sessions, so more runs are owed (Still to test).
Setup: Windows 11, Ryzen 9 3900X, RTX 3080 (NVIDIA 621.14), 3440x1440 window, render resolution 67%, V-Sync off,
uncapped (
HOS_FPS_MAX=1000),dev.py ab, 60 s measured after the warm-up, A1 B1 A2 B2.game_msis the frametrace's game loop.
a2dc5385(v3.1.1)659c7ebe(smoke:388fa3cb)game_ms(median)game_ms(median)longshot.exe388fa3cb)The Market's 60.0 is not a cap: 17% of this PR's frames there are under 15 ms. At Lake Hylia the game's loop is 22%
shorter but the frame only 7%: the wait for the GPU's previous frame grows, so the render chain holds the frame there.
Build: this PR's pack compiled all of the game code in 664 s (11:28 with the rest); a clean build of each side is
still to measure.
Risks
0xFFC00000(Arm0x7FC00000), and which operand's NaN an oppasses on can differ. Code that stores a NaN and then tests its sign bit would see a difference. Not seen.
RX_FP_NATIVE=0at build time builds the exact headers again. A player notices nothing after updating but thespeed (the game is built again, as for any change to the game code).
Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). Not run yet.main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def.RX_FP_NATIVEis a build switch, read bynx2/build.py: its row is indocs/dev/reference/build.md(asA64_FASTFP's), notswitches.def. On by default from the first push.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: Windows 11, Ryzen 9 3900X, RTX 3080 (NVIDIA 621.14), 3440x1440 window at 67%, V-Sync off, uncapped. dev.py ab against the v3.1.1 base (
a2dc5385): Market 52.7/54.7 -> 60.0/60.0 fps and Lake Hylia 50.8/52.6 -> 55.3/55.5 fps (2 runs a side, at659c7ebe), and a Market smoke pair on the head388fa3cb: 52.4 -> 60.0 fps. Every run reached its scene and played through; no crash, nothing wrong seen in the window.Possibly related, for context:
The switch defaults on, but the FPCR side mirrors only FZ and the evidence that the game never depends on the rest is one 100-minute coverage file with no proof build.
RECOMP_FPX_OPENused to requireFPSR.IXCset andFPCRclear of everything inRECOMP_FPX_FPCR_MASK— rounding mode, DN, AHP, FZ16 and the trap bits — before an op took the fast path.nx2/fp_native.pyforces that gate, and theKEEP/FIN/BIG/FZ*tests, to true. It mirrors onlyFPCR.FZinto MXCSR (FTZ/DAZ); it does not mirror FPCR.RMode or DN. The six functions that read FPSR (f_8575d1d4,f_8575d1e4,f_8575d1fc,f_8575d214,f_857f71f0,f_851109b8,f_85110e58) are the libc fenv routines — exactly the ones that can callfesetround/feholdexcept/fesetenv. Their absence fromnx2/ran_funcs.txtis one 100-minute session, not a proof: a cutscene, minigame or save path that calls one of them, or a lifted block that reads FPSR, now sees the host's flags and rounding, not Arm's. The PR's own Still-to-test says the proof build is owed; until it runs, "the game never reads FPSR" is a coverage claim, not a demonstrated one.NaN/denormal: the native path keeps x86 NaN encodings (default
0xFFC00000vs Arm0x7FC00000, and a different operand's NaN), and under FZ the x86 after-rounding tininess differs from Arm's before-rounding at exactly 2^-126; both are acknowledged and untested. The global FTZ/DAZ also reaches host code a guest thread runs, not just the float regions.Test: there is no test for
nx2/fp_native.pyat all — not the header rewrites, notRX_FP_NATIVE=0, not a byte-identity check, not an FPSR-reader trap, not a NaN/denormal case;mise run testhas not run. The regex patcher does fail if a header line moves, which is a guard, but it is not a test of the resulting semantics.Off path:
RX_FP_NATIVE=0skips the copies and the host#ifdef; the generated headers should be the lift's own, but nothing checks them, and the build is not literally byte-identical to main because the changedhost/hos_thread.c/nx2/build.pymove the source fingerprint and version string. "Same headers" is the right claim; "byte-identical" is not demonstrated.Measurements: the A/B is at
659c7ebe, not at this head388fa3cb; the head only adds the fiber's MXCSR and has a single 30 s Market smoke pair. The before is current maina2dc5385(post-#2174), which is right; the Lake Hylia A/B is not at the head.No approval, no fix claim. The core idea may be sound for this game, but the default-on switch rests on a coverage snapshot and no game-free test of the semantics it removes.
Correction on the FPSR evidence: the description says the 8 FPSR reads are in "6 functions" but names seven:
f_8575d1d4,f_8575d1e4,f_8575d1fc,f_8575d214,f_857f71f0,f_851109b8,f_85110e58. The proof build should name every one; the exact list matters to the claim that none runs in play.@review
Recommendation: Request changes. The default-on rewrite can return incorrect vector min/max results and breaks builds using supported lift switches. The performance results are promising on the tested Windows PC, but the final head and other affected systems need further runs.
The change removes much of the lifted code’s floating-point bookkeeping and reports a substantial speedup, at the cost of weaker Arm floating-point semantics and a new build switch. The rewrite also removes a guard needed for correct vector min/max results.
Blocking (2): must change before merging.
nx2/fp_native.py:60: Forcing a64_vx_none to succeed also disables the correctness guard for vector min/max, although those operations are meant to remain exact. (more on the line) Do: Keep the original bad-lane check for min/max, and restrict any unconditional acceptance to the arithmetic helpers whose behavior is intentionally changed. Add a game-free behavioral test covering NaNs and FZ subnormals.nx2/fp_native.py:60: The patcher requires helpers that supported lift configurations deliberately omit. (more on the line) Do: Make rewrites conditional on the corresponding generated helper being present, while still rejecting unexpected partial or changed helper definitions; cover both supported off modes in a game-free test.Testing before merging (2) (runs on the systems it can change; it doesn't hold the recommendation)
@fixdoes its part first: Add the proposed one-time reader instrumentation so the run can identify an observed read.)@fixcan do L1, L2 and L4.Checks: Failing on this head, which is behind its base, where they pass:
Guide screenshots / Guide images and local UI source review (pull_request),CI / Website build and checks (pull_request).@rebase(or@fix) brings it up to date and runs them again; not held against it.More: does it help, other systems, switches, what was read
Does it help: Unproven on the final head across the affected systems. On Windows with a Ryzen 9 3900X and RTX 3080, the PR reports two interleaved runs per side at
659c7ebe: Market 52.7/54.7 to 60.0/60.0 fps and Lake Hylia 50.8/52.6 to 55.3/55.5 fps. The final head has only one 30-second Market smoke pair, 52.4 to 60.0 fps.Other systems: The rewritten float operations run throughout the lifted game on Windows and Linux, across CPU vendors and GPUs. The reported runs cover Windows on one AMD CPU and NVIDIA GPU; Linux, Steam Deck, other CPUs and longer gameplay remain untested. The MXCSR change also affects host code called on guest threads.
Switches: RX_FP_NATIVE is added as a build-time switch, on by default, with an off setting documented in docs/dev/reference/build.md. Its default has promising measurements on one PC but needs broader validation; it also currently breaks builds combined with A64_FASTFP=0 or A64_VECFP=0.
Reviewed:
388fa3cb02againsta2dc538590: Read the supplied PR description, comments and review, repository rules and PR template, the complete diff and commit messages, nx2/fp_native.py, and relevant portions of the build, float generation, vector helpers, thread and fiber code. Skimmed docs/dev/reference/build.md and docs/dev/standards.md. The full nx2/build.py and host/hos_thread.c were not read; no recorded runs, translations or generated files were changed.; nothing run (a code review).Checked and fine:
Testing: No code or tests were run, as requested. The PR reports Windows gameplay and A/B results; its own test suite and Linux build remain outstanding.
Overlaps and merge order: The description identifies related open work: #2152 and #2067 optimize bookkeeping this change removes; #2068 remains relevant, while #2036, #1910 and #1904 would need their FPSR handling and gains reassessed.
Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
@ -0,0 +57,4 @@"_mm_sub_ps((a), (b)) : (ins)[0] == 'm' ? _mm_mul_ps((a), (b)) : _mm_div_ps((a), (b)))")FN = r"static inline __attribute__\(\(always_inline\)\) "FPX = [(r"^(A64_VX int a64_vx_none\(__m128i bad, int k\)) \{.*\}$", r"\1 { (void)bad; (void)k; return 1; }", 1),L1 (blocking): Forcing a64_vx_none to succeed also disables the correctness guard for vector min/max, although those operations are meant to remain exact.
When: For a vector FMAX with a NaN in the first operand and a finite second operand, a64_vfpx_minmax32v now accepts the lane and selects the finite second operand instead of falling back to Arm's NaN handling. With FZ enabled, subnormal operands can likewise bypass the guard and produce the wrong selected bits.
Do: Keep the original bad-lane check for min/max, and restrict any unconditional acceptance to the arithmetic helpers whose behavior is intentionally changed. Add a game-free behavioral test covering NaNs and FZ subnormals.
L2 (blocking): The patcher requires helpers that supported lift configurations deliberately omit.
When: With A64_VECFP=0, vecfp.py does not append a64_vx_none, so the first FPX substitution matches zero times and the default RX_FP_NATIVE build exits. With A64_FASTFP=0, fastfp.py omits FASTFP_HEADER and the required fast-region substitutions similarly abort the build.
Do: Make rewrites conditional on the corresponding generated helper being present, while still rejecting unexpected partial or changed helper definitions; cover both supported off modes in a game-free test.
🤖 A note on overlap with #2355 (WIP).
#2355 shortens one path inside today's lifted float handling: the lane routes (
a64_vfpx_*32_lanes) for a quiet-NaN lane beside normal lanes. That's the Havok cloth's case, worth 83.14 → 90.24 fps in Lost Woods (child), 3 pairs, 5700X3D, main0e32bc10. It's behind a lift-time switch,A64_FPXKEEP_QUICK, off by default.If this PR replaces that handling, #2355 becomes unnecessary. #2355 changes only
nx2/fpxkeep.py; a build with both should be checked if both move forward.🤖 Generated with Claude Code
Steam Deck numbers for this change, from a player. The same bench is described in full on #2308.
Steam Deck LCD, SteamOS 3.9.5, RADV. Local builds of
a2dc5385+ this PR forRX_MARCH=znver2, withRX_FP_NATIVEon (fpn) vsRX_FP_NATIVE=0(base). Market entrance, standing still, headless, 3 interleaved runs a side, 60 s windows:game_msmedianOn the Deck this is the bigger half of the #2235 + #2308 stack: the play profile on top added only -0.07 ms of
game_msthere.fps did not move because this bench ran headless at 2560x1440 (see the correction below), where render_ms (~30-34 ms) bounds the frame. I'm repeating it at the Deck's 1280x800.
Nothing looked wrong in play. I have not done a pixel check of
fpnagainstbaseyet; that is part of the re-run.Correction: the bench did not render at 1920x1080. That is only the size of its offscreen present image. With no screen, the game's output resolution falls back to 2560x1440 (
oe: output resolution 2560x1440 (none chosen: the game's)in every run's log), 3.6x the pixels of the Deck's own 1280x800. The bench now setsHOS_OUTPUT_RES=1280x800; the re-run follows.Assisted-by: Claude
StebanPi referenced this pull request2026-11-24 17:17:07 +00:00
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.