perf(lift): SSE fast paths for the float forms the translator does lane by lane (A64_VECFP) #1782
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
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1782
Loading…
Reference in a new issue
No description provided.
Delete branch "snaildev99/Longshot:perf/vecfp"
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 game's physics, cloth and animation (Havok, skeletal animation) are full of four-lane multiply-add, multiply by an element, min / max and float / double conversions. The translated code still runs those instructions one lane at a time through the scalar exact-float helper or in software, at 40 to 60 CPU cycles each;
docs/cpu-profile.mdlists this as candidate 6 (6.3 % of a core). This PR gives 27 of those instruction forms a fast path: one SSE operation, whose result is kept only where it is provably the Switch's bit for bit; otherwise the instruction's own C runs as before.The goal is fewer host instructions per game instruction for the same results, every result and flag bit for bit as before. Frame rate is the check that nothing got slower, and where the processor sets the frame it rises: Castle Town's Market ran 3.5 % faster standing still and 5.1 % faster turning the camera on main
a433db32(three interleaved runs a side), and +3.9 % / +3.5 % as the mean of eight on/off pairs in a 48-run matrix, a gain in every pair. In a Havok bounding-box routine (f_81746440, lifted and timed in a harness) one call goes from 6,900 to 1,520 TSC ticks. The paths are on by default;A64_VECFP=0lifts exactly as main.How:
nx2/vecfp.pywraps the translator's text for these forms inif (!a64_vfpx_<form>(c, ...)) { <its own C> }. The helper (appended toa64_fpx.h) does the operation on SSE and returns 1 only when every lane's result is provably Arm's with no FPSR bit to add; else it writes nothing and returns 0. Inside a fast float region (nx2/fastfp.py,FF_VX) the multiply forms become one SSE operation of the region. The forms: FMUL / FMLA / FMLS by element, vector FMLA / FMLS, FMUL 2S, FDIV, FMAX / FMIN and their NM forms, FCVT between single and double. The keep rules, per form: multiplies, multiply-adds and divides by FPX1's (RECOMP_FPX_OPEN, KEEP32 / KEEPDIV32, under FZ no subnormal operand or addend), the fused forms only with FMA3 (else they decline); FMAX / FMIN(NM) when no lane is a NaN (under FZ none subnormal), equal values AND / OR-ed so max(+0, -0) is +0; FCVT S to D every number but NaN (under FZ a subnormal declines); FCVT D to S FPX1's gate and a big result or a zero input. Beside main's own vector paths:vecfp.main_handlesasks the lift's own passes whether they already make a word one vector operation (today exactly FMUL 4S by element, #1576) and leaves that word to them. No}at the start of a line, sincenx2/split_giants.pyandnx2/oot_patches.pyend a function's body at the first one;tests/test_function_ends.pychecks it.Read first:
nx2/vecfp.py(fast_call,wrap,HEADER,main_handles,install); theFF_VXtwins innx2/fastfp.py;tests/test_vecfp.py.See #505 (the hot list, which this works beside).
Testing
Tested: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X. In the game: this change at its pre-rebase head in a test tree on main
a433db32(test/perf-prs@ be5623e0 on my fork, built from scratch twice,A64_VECFP=0and on, 0 failed, both camera edits found by both builds): 15 hands-free Market runs, 6 with the paths on; in a v3.0.0 test tree, 33 Market runs with the paths on; with the first version on main544229a2, the native maths units of another unmerged branch (themselves checked exact against the game's code in an AArch64 emulator) compared with the lifted code of their functions, which hold six of these forms: about 67 million calls, 0 mismatches. NoFATALand no frame of 50 ms or more in any log. The rebase onto main068fb8eb(#1802) changed no code:docs/development.md, which #1802 removed, lost the switch's row, which is now indocs/dev/reference/build.mdbesideA64_FASTFP. Game-free at this head (008b63bf):tests/test_vecfp.py(28 words: both lane counts, by-element indices, an aliased FMLA, S and D forms; the lifted, hot and region copies against the same word with FPX off, 60,000 cases each: normal values and mixes with zeros, subnormals, extremes, infinities, quiet and signalling NaNs, underflow edges, +0 / -0; FZ off and on; FPSR IXC, all, none; x86-64-v3 and v2: 0 mismatches, the fast path taken on the normal cases; FMUL 4S by element runs main's path; withA64_VECFP=0every word's text is the translator's),tests/test_function_ends.pyandtests/test_doc_anchors.pypass;tests/test_lift_cache.pypasses every check of this PR (A64_VECFPchanges the lift key,nx2/vecfp.pyis a lift source) and fails one that fails the same way on main068fb8eb("...and the others still by their code"). Earlier,A64_VECFP=0against main: a made-up image of 4,600 functions holding every form, lifted by main and by this branch with the switch at 0, in one process and in three Windows-style emit processes: every output file identical.tests/run_all.pyon Windows at this head: 705 passed, 51 failed, 116 skipped; main068fb8ebon this PC, run beside it: 704 passed, 50 failed, 116 skipped. The same failures, apart from timing tests that failed in one of those runs only and pass when run alone, on main and here (test_invalidate_vcore,test_compile_stops_on_exception,test_longshot_ctl,test_mesh_args_cleanup);test_vbytestimes out at 600 s in both. Main's failures on this PC:test_appimage_player_build,test_auto_handheld,test_bug_report_defaults,test_capture_read_once,test_crash_dialog,test_crt_options,test_damage_debug_menu,test_dev_ab,test_dev_build,test_dev_migrate,test_dev_packs,test_display_startup_failure,test_dlss_feature_size,test_failed_compile_keeps_objects,test_frame_overlap,test_frame_wait_bounds,test_game_menu_mouse_buttons,test_guest_imported,test_high_fps_fixes,test_host_guest_writes,test_hud_layout_row,test_job_queue,test_libc_hooks,test_lift_cache,test_linux_preflight,test_linux_pressure,test_make_game_ngx,test_mise_build,test_missing_nup,test_mods_list,test_ngx_sdk,test_pads_after_menu,test_pending_guest_import,test_pipeline_play_cache,test_portable_builder_home,test_portable_builder_startup,test_prepare_cache_save,test_present_thread,test_profiling_mode,test_psychov35_fsr3_fg,test_readback_timeout_source,test_sdiv_overflow,test_seed_reader,test_slow_frames,test_song_note_cue,test_upscaler_rows,test_vcore_escape,test_walk_key.Still to test:
python -I -B tests/test_vecfp.py, then main against this PR in the Market for a few minutes; the game's CPU seconds with the frame rate capped at 60 on a PC that holds 60 there, and the frame times uncapped, expecting 0 mismatches for every word, the same frame rate or better with less CPU time, and the game looking and playing as on main (@Beanstalk linux).A64_VECFP=0and unset, the FPS counter in the Market, expecting the frame rate with the paths no lower than without (by hand: a tester with an Intel CPU; MXCSR costs differ there).Measurements
Verdict: fewer host instructions for the same results: 27 vector float forms take one SSE operation where they took a loop over lanes at 40 to 60 cycles, every result bit for bit the same. Frame rate is the check that nothing got slower, and where the processor sets the frame it rises: the Market +3.5 % standing and +5.1 % turning on main
a433db32. Nothing where the renderer sets the frame (Hyrule Field by day). The cost is build time: about 4 % more compiler time on a default build and no longer a compile on this PC; +28 % compile time with every lifted file at -O2.Baseline: two builds of one tree,
test/perf-prs@ be5623e0 on my fork (maina433db32with this PR and its siblings #1780 and #1781 merged, the default hot list), each from scratch:A64_VECFP=0(Before: the lifted C anda64_fpx.hare main's to the byte) and unset (After). Both with every run-time switch unset. Main has since moved four commits to068fb8eb(#1802 docs, tools and the PR template; #1774 and #1807 the Insert menu's rows; #1792 dev packs): outside comments, docs and the dev tools they change the Insert menu's code (#1774, #1807) and the version string and some log texts (#1802): nothing in the lift (its files changed only in comments), and nothing the Market runs each frame but the FPS counter's overlay, which runs alike on both sides. So the numbers hold.Setup: Windows 11 (build 22631), GeForce RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X (12 cores), 128 GB. Child Link at the Market's save point by day; 3840x2160 full screen, frame rate cap Uncapped, no mods; the GPU wait 0.0 to 0.1 ms in every run (the processor sets the frame). Hands-free through
scripts/longshot_ctl.py: the save loads, 10 s to settle, 15 s standing (the FPS counter's value once a second), then 40 s with the right stick held to one side. Three runs a side, interleaved, the median.A64_VECFP=0: test/perf-prs @ be5623e0longshot.exeWith its two siblings on as well, the same runs: 76.0 standing (+10.5 %) and 68.1 turning (+8.1 %). No frame of 50 ms or more and no
FATALin any of the 15 logs. Which hot piece is slowest moved, by how the pieces are cut, so the default build paid no wall time here.The 48-run matrix
The change was measured beside every combination of #1780, #1781 and main's
RX_VK_CAPTURE_NT, in one test treetest/perf-matrix@ 2c30c326 on my fork (v3.0.009cbdd97with the three changes, every lifted file at -O2 so that the hot list is not a variable), built twice:A64_VECFP=0(Before) and unset (After). Sixteen variants, three interleaved runs each, 48 runs in 74 minutes with nobody at the PC.A64_VECFP=0: standing / turning fpsRX_VK_CAPTURE_NT=1HOS_NVN_REC_HANDOFF=1HOS_OBJ_CACHE=16384RX_VK_CAPTURE_NT=1,HOS_OBJ_CACHE=16384HOS_NVN_REC_HANDOFF=1,HOS_OBJ_CACHE=16384A gain in every pair (+1.5 to +3.6 fps standing, +1.2 to +2.7 turning). Peak working set 9,944 to 10,030 MB either way. Build cost with every lifted file at -O2: compile 483 s to 616 s (+28 %), the lift 87 s to 86 s. Against the shipped hot list in the v3.0.0 tree (an earlier run with the PC in use, three runs a side): 67.3 / 61.0 fps without the paths, 69.8 / 63.7 with them (+4 %); -O2 on every file does not make the paths redundant (67.1 / 61.2 without them). The run file is
docs/tested/2026-11-19-vecfp-snaildev99-matrix-v.md.Earlier, with the first version on main
544229a2: Hyrule Field by day no change; on a hand-played route, one run each, the game's time a frame -5 % on Death Mountain Trail and -6 % in Kakariko. Offline, whole routines lifted both ways (ticks a call, flush-to-zero off / on): the Havok box routinef_817464406,892 / 7,602 to 1,520 / 1,667; the box transformf_80ba6d80in its region 91 to 76.The switch
A64_VECFP(new, lift time; its row indocs/dev/reference/build.md): unset = on (elem,vec,minmax,fcvt);0off; or a list of the groups;true/yesas on; any other value stops the lift with a message. It ships on, as the measurements chose. It is in the lift's key.Risks
What could break: a keep rule that is wrong would give a float result the Switch would not, in physics, cloth or animation. Checked as above; a path declines whatever it cannot prove (a NaN, a subnormal under FZ, a closed FPX1 gate, no FMA3 for the fused forms).
Open for review: #1560 (dyst, ready for review) rewrites
emit_functionand the fast float regions' copy and wrappers into one function with hook points. A trial merge with its head3f318305had two conflict blocks, both innx2/lift.py(an import line and an install line: both sides kept); the two were not run together. Whichever goes in second is fitted to the other; I will do that on this branch if #1560 lands first.Turning it off:
A64_VECFP=0(lift time: the build translates the game again), or a list of the groups to keep.After updating: the lift's key changes, so the first build after updating translates the game again and compiles all lifted code (as any translator change). Lifted objects about 1 % larger. Saves,
longshot_options.iniand the shader caches: nothing changes.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def. (A lift-time switch: on, as measured; its row is indocs/dev/reference/build.mdbesideA64_FASTFP, as the lift's switches' are.)tests/that reads changed source is updated, not weakened. (tests/test_lift_cache.pynamesA64_VECFPandnx2/vecfp.py;tests/test_vecfp.pyandtests/test_function_ends.pyare new.)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
The translator writes FMUL/FMLA/FMLS by element, vector FMLA/FMLS, FMUL 2S and FDIV as a loop of the scalar FPX helper per lane, and FMAX/FMIN(NM) and FCVT between S and D with no fast path at all (integer bodies, software rounding). Outside a fast float region a 4-lane FMUL by element cost about 48 cycles, FMLA by element 52, FMAX 4S 48 and an FCVT 25 to 40 (Ryzen 9 5900X, the instruction lifted and compiled as the build does). nx2/vecfp.py wraps a64emit.translate: each such word's C becomes `if (!a64_vfpx_<form>(c, ...)) { its C }`. The helper does the op on SSE and keeps the result only where FPX1's rules prove it Arm's with no FPSR bit to add (a64_suyu.h: the gate, FPSR.IXC set and FPCR at its default but FZ, and per lane the KEEP32 / KEEPDIV32 tests and, under FZ, no subnormal operand or addend); FMAX/FMIN when no lane is a NaN (or, under FZ, subnormal), with the AND / OR of equal values (+0 for max(+0, -0)); FCVT S->D for every number, D->S when the result is big or the input zero. Else it writes nothing and the instruction's own C runs as before. The helpers' names begin a64_vfpx_, so these instructions still join fast float regions. A64_VECFP (lift time, in the lift key as an A64_ variable): on by default; 0 off; or a list of elem, vec, minmax, fcvt. docs/development.md has the row. tests/test_vecfp.py: 28 hand-written words (both lane counts, by-element indices, an aliased FMLA, S and D) translated as the lift does, their hot copy and a region of each, against the same word translated with FPX off, on 60,000 cases each (normal, and mixed with zeros, subnormals, extremes, infinities, quiet and signalling NaNs, underflow edges, equal operands), FZ off and on, FPSR with IXC, all flags or none; the fast path must be taken on the normal cases; x86-64-v3 and v2 (no FMA3: the fused forms decline). Five hand-made faults each fail it. The same words were checked against Unicorn's AArch64 outside the repository: 0 differences in register or FPSR. Measured offline (whole game functions lifted and compiled as the build does, synthetic inputs, Ryzen 9 5900X; not a frame rate): Havok's AABB-of-vertices routine 6,900 -> 1,520 TSC ticks a call, a box-by-matrix transform 91 -> 76 in its region and 328 -> 105 outside one; outputs byte for byte the same. Co-Authored-By: Claude Opus 5.5 <[email protected]>In the lifted C a "}" at the start of a line closes a function: the lifter writes every instruction's own blocks so, and the tools that read the lifted C end a function's body at the first one. nx2/split_giants.py's split_text does (each definition's body runs to the first "}" at the start of a line), and so do nx2/oot_patches.py's edits, which look for their place before it. nx2/vecfp.py wrote the brace that closes its `if (!a64_vfpx_...) {` there. With A64_VECFP on: - two edits were not made: the lift said "the free camera was found 0 times" and "the disable camera centering was found 0 times". f_804e48c0 (the view, 210 KB of C) has an FCVT Sd, Dn at +39 KB and the free camera's place at +96 KB; f_80644c80 (the player camera, 642 KB) an FMAX at +277 KB and the camera centering's place at +321 KB, so both places were looked for in the part of the function before the wrapped instruction; - split_text took every function with one of these forms to end at it, so the function was left whole ("unbalanced body", or below MIN_KB by its cut-short size): 221 of the game's 368 giant functions (192 MB of 257 MB of C) were compiled whole. A build with the paths on compiled in 13 min where the same lift without them took 5 (2026-11-17, Ryzen 9 5900X, 23 jobs; the hot pieces 2.7 times and the chunks with such giants 1.8 times their time, the chunks without 1.02 times). One file at a time, as the build compiles it: code_021_6.c 243 s -> 71 s, code_022_5.c 169 s -> 71 s, a -O2 hot piece 235 s -> 164 s. The closing brace is indented now. A fast float region's check copy (A64_FASTFP=check) had the same fault in its else's brace and is indented too. With A64_VECFP=0 and A64_FASTFP not set to check the lift is as before. Checked with two lifts of the game (vecfp on, before and after): after, every oot_patches edit is found (the same lines as before plus the two), and split_giants cuts 366 of the 368 giant functions (the other two are too small to cut). tests/test_function_ends.py (game-free, no compiler): the blocks vecfp.wrap and ff_region (both modes) write put no "}" at the start of a line; split_text cuts a giant with wrapped instructions; each oot_patches edit that looks for a place inside one function (free camera, camera centering, heart protection, flight, flight rotation, movement speed, whole frames) finds it after a wrapped instruction, a region of them, and a region's check copy, and not in the next function. It fails 30 ways on the wrapping before this change. Co-Authored-By: Claude Opus 5.5 <[email protected]>A value the switch did not know ("true", "yes", a misspelt group) gave an empty set of groups, so every fast path was off without a word while the lift key still changed. Now 1, on, all, true and yes turn every group on, 0, off, none, false and no turn them off, a comma list names groups, and anything else stops the lift with a message naming the switch and the groups it takes. tests/test_vecfp.py checks the new words and three bad values. The module's docstring says where its cycle counts come from (offline, on lifted game routines) in place of a private folder's name. Co-Authored-By: Claude Opus 5.5 <[email protected]>Tested in game: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X: two builds of one test tree (v3.0.0 with these commits, test/perf-matrix @ 2c30c326 on my fork; every lifted file at -O2), A64_VECFP=0 and the paths on; 24 hands-free runs of each in the child Market (15 s standing, then 40 s turning the camera). With the paths on: no FATAL and no frame of 50 ms or more in any log, both camera edits found by the build, +1.5 to +3.6 fps standing and +1.2 to +2.7 turning in all eight on/off pairs. Not built or played at this head on current main yet: that is why this is WIP.
Possibly related, for context:
Built and played at this head. The test tree is
test/perf-prs@ be5623e0 on my fork: maina433db32with #1780, #1781 and #1782 merged, the default hot list, built from scratch twice (A64_VECFP=0, where the lifted code is main's, and the float paths on). Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X, 3840x2160 uncapped, the child Market: 15 s standing, then 40 s turning the camera; five variants, three interleaved runs each, nobody at the PC, medians.HOS_NVN_REC_HANDOFF=1(#1780)HOS_OBJ_CACHE=16384(#1781)No frame of 50 ms or more, no
FATALand nonot a live recordingin any of the 15 logs; the object cache missed 0.69 % of 22 million lookups.Build cost of the float paths on a default build (both from scratch, 23 jobs): the lifted files' compiler time 7,374 s to 7,646 s (+3.7 %), the compile's wall time 458 s to 422 s (the slowest hot piece sets it, and it is not slower), lifted objects 679 to 687 MB.
The description has these numbers; the full 16-variant matrix on v3.0.0 is still its first table.
WIP: perf(lift): SSE fast paths for the float forms the translator does lane by lane (A64_VECFP)to perf(lift): SSE fast paths for the float forms the translator does lane by lane (A64_VECFP)@Beanstalk linux: run python -I -B tests/test_vecfp.py, then build this branch and play a few minutes in the Market, expecting 0 mismatches for every word and the game to look and play as on main.
@snaildev99 only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forf433669, and the PR has changed since (008b63b), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.f4336699ae008b63bf15Rebased onto main
068fb8eb(008b63bf). #1802 removeddocs/development.md, which held theA64_VECFProw; the row is now indocs/dev/reference/build.mdbesideA64_FASTFP(one docs commit), and no code changed. The description now uses the new template, and says plainly what this PR is for: fewer host instructions per game instruction for bit-identical results, with frame rate as the check that nothing got slower.Why the numbers still hold on
068fb8eb: they were measured on maina433db32, and the four commits since (#1802 docs, tools and the PR template; #1774 and #1807 the Insert menu's rows; #1792 dev packs): outside comments, docs and the dev tools they change the Insert menu's code (#1774, #1807) and the version string and some log texts (#1802): nothing in the lift (its files changed only in comments), and nothing the Market runs each frame but the FPS counter's overlay, which runs alike on both sides. So the numbers hold.The farm request is reworded below, to measure the game's CPU time at a frame cap besides the frame times. A ~4 % change is close to what two runs a side can resolve, and the point of the change is less CPU time for the same frames.
@Beanstalk linux: run python -I -B tests/test_vecfp.py, then main against this PR in the Market for a few minutes. Measure the game's CPU seconds with the frame rate capped at 60 (on a PC that holds 60 there), and the frame times uncapped. Expect 0 mismatches for every word, the same frame rate or better with less CPU time, and the game looking and playing as on main.
@snaildev99 only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.