WIP: perf(lift): a subtraction's V flag as N xor (a < b), so the game's signed compares are one host compare (A64_SUB_V) #1871
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!1871
Loading…
Reference in a new issue
No description provided.
Delete branch "dyst/Longshot:perf/lift-sub-v"
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 signed compares (
CMP x1, x2thenB.LT,B.GE,B.GT,B.LE, or aCSEL/CSETon them: loop bounds, ranges, counters) cost seven to nine host instructions each in the lifted code, where an x86 CPU does them in two (cmpandjl). The translator writes a subtraction's V flag as overflow bits,((a ^ b) & (a ^ r)) >> 63, and the signed conditions read it against N (N != V); clang folds neither at-O1nor at-O2. In the lift of the game, subtractions set flags at about 168,000 places and signed conditions read N against V at about 170,000.For a subtraction, N xor V is exactly "a < b" as signed integers of the operation's width (that is what Arm's LT means after a CMP). So this writes V as the result's sign bit xor that signed compare: the same bit for every a and b, and a reader's
N != Vbecomes(N ^ N) ^ (a < b), which clang folds to one compare.CMP x1, w2, sxtw; B.LTgoes from nine instructions to three,CMP w1, #29; CSELfrom seven to three. Every flag keeps its value bit for bit; only V's C text changes, and only SetFlags' subtraction form (SUBS/CMP immediate, shifted and extended register, CCMP when its condition holds). ADDS/CMN (about 3,900 places) and ADCS/SBCS write V in other forms and are untouched.The idea is from Rekarina, another recompiler of the game, which compares a SUBS's operands directly where only that kind of flag-setter reaches a reader (https://git.rekarina.dev/rekarina/rekarina,
recomp/flags.pyandruntime/cpu.hfc_sub). The identity here needs no reaching analysis and is exact for every reader. No code was copied.Result: fewer host instructions for the same flags (
longshot.exe's code 1.1 MB smaller), but no frame-rate gain this PC can show: over 15 interleaved runs a side the Market and Lake Hylia are within the spread (the game's time a frame 1-2 % lower). A first series of 5 showed +7 to +9 %; it did not reproduce.Read first:
nx2/cmpflags.py(the docstring has the proof),tests/test_cmpflags.py.Testing
Tested: Windows 11 Pro (22631), RTX 3080 10 GB (NVIDIA 621.14), Ryzen 9 3900X: 10 hands-free runs (5 a side) through the Market from its entrance to the fountain and at Lake Hylia, child Link from the kokiri-start save; no crash and no FATAL in any log, the shots at the fountain and the lake as main's (the water's phase aside).
docs/tested/2026-11-20-sub-v-dyst.mdhas the record. The lift log's patches all found their places, as on main (only the chunk numbers they land in moved: the text is shorter).Checks:
tests/test_cmpflags.py(new): the rewrite in Python;A64_SUB_V=0leaves the translator as it was; 17 guest words (11 subtraction forms at 32 and 64 bits, CCMP register and immediate; ADDS, CMN, ADCS, SBCS, ANDS, CCMN must keep their text) translated both ways and run from the same context, the whole context compared after: every pair of 40 edge values under each of the 16 NZCV values, then 2,000,000 random states a word, at-O1and-O2: 0 mismatches in 68.9 million checks. It has teeth: with the compare made unsigned it reports 403,641 mismatches.test_dead_flags,test_function_ends,test_doc_anchorspass;test_lift_cachefails only its "...and the others still by their code" check, as on main89c3d21e(its checks that every module the lift loads is in the key pass withnx2/cmpflags.py).tests/run_all.pywith the Builder's llvm-mingw: 704 passed, 55 failed, 123 skipped; the same 55 fail on main89c3d21e, rerun beside it (none fails here only).Still to test:
python -I -B tests/test_cmpflags.py, then the Market and Lake Hylia against main, expecting 0 mismatches and the same frame rate or better (@Beanstalk)Measurements
Verdict: within the spread. Over 15 interleaved runs a side (four series on 2026-11-20), the Market walk 37.5 -> 38.3 FPS, at the fountain 43.9 -> 43.8, Lake Hylia 43.5 -> 44.9 (medians; every interquartile range overlaps), the game's time a frame 1-2 % lower. The first series (5 a side) showed +7.4 % at the fountain and +8.8 % at the lake; the two series after it in steadier conditions showed none, and the base itself ran 3-4 FPS faster in them. Not a measured gain.
Setup: Windows 11 Pro (22631), GeForce RTX 3080 10 GB (NVIDIA 621.14), Ryzen 9 3900X (12 cores, 24 threads), 64 GB. Default options (the game's 2560x1440 output, V-Sync and frame generation off), cap off (
HOS_FPS_MAX=1000), a 1720x720 window. Child Link from the kokiri-start save, hands-free through the control bridge: the Debug teleport to the Market, a warm-up until no frame waits for a pipeline, 15 s with the left stick held forward (the entrance to the fountain), 30 s standing at the fountain; the teleport to Lake Hylia, the warm-up, 30 s standing. Each run a timing job of the PC's queue, interleaved (ABBAAB, BAAB, BAABBAABBA, ABBAABBAAB). FPS = 1000 / the mean frame time over the stretch, from the frame trace; the game's time a frame from itsgame_ms. The GPU waited 0.05 to 0.07 ms a frame: the processor sets the frame here.Both sides are one shared base, main @
3f2c9b3c, the change as a pack over it (measure/sub-v-3f2c@ 572bd93e on my fork: this PR's commit cherry-picked, without #1782's import line beside it): every object is the base's but the lifted game's.3f2c9b3c3f2c9b3c)longshot.exe.textTried beside it and dropped: Z written as a == b as well (so HI, LS, GT and LE fold too) was slower, 5 runs a side: the walk 38.3 -> 35.1, the fountain 44.0 -> 39.6, the game's time a frame 21.7 -> 24.0 ms (likely the counter loops,
subs x0, x0, #1; b.ne, where x86 takes Z from the subtraction itself and a == b needs a compare of its own).Risks
What could break: nothing should, as every flag keeps its value; a mistake in the identity would show as a wrong branch in game logic anywhere, which
tests/test_cmpflags.pychecks on edge and random operands of every form at both widths. The rewrite matches the translator's exact text, so a change to SetFlags' text in the kit leaves the lift as main's (no rewrite) rather than wrong. Turning it off:A64_SUB_V=0at lift time (the build translates the game again; the lift is then main's to the byte). After updating, the lift key changes, so the first build translates the game again and compiles all lifted code, as any translator change does; saves,longshot_options.iniand the shader caches are unchanged.Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (tests/run_all.py: the 55 failures all fail on main89c3d21etoo.)main, or Measurements says why there is nothing to measure. (Measured: within the spread on this PC; no gain shown.)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_VECFP.)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 Pro 22631, RTX 3080 (NVIDIA 621.14), Ryzen 9 3900X: 5 hands-free runs against 5 of main through the Market (entrance to the fountain, then standing) and at Lake Hylia, child Link; no crash or FATAL, pictures as main's; fountain +7.4 %, Lake Hylia +8.8 %, walk within the spread.
Correction to the numbers this opened with: the +7.4 % (fountain) and +8.8 % (Lake Hylia) came from the first series of 5 runs a side, and three more interleaved series on the same PC and base did not reproduce them. Over 15 runs a side the Market walk, the fountain and Lake Hylia are all within the spread (the game's time a frame 1-2 % lower; the description has the table). The base itself ran 3-4 FPS faster in the later series, which points at the PC's state during the first, not at this change. Also tried and dropped: Z written as a == b as well, which was clearly slower (fountain 44.0 -> 39.6 FPS), likely because the subs/b.ne counter loops lose x86's free ZF. This stays WIP: the instruction count is lower for the same flags, but no frame-rate gain is shown here; a weaker CPU (the Steam Deck) may show one, or it can be closed.
@Beanstalk
@HumanBean approval of
af09e83recorded. This diff touches sensitive paths, so a second member ofBigBeansmust approve it too.Beanstalk test farm
Commit
af09e83· approved by HumanBean (untrusted) · dashboardThis diff touches sensitive paths, so it needs two members of
BigBeans:nx2/cmpflags.py,nx2/lift.pyA member of
BigBeanscan review this commit and comment@Beanstalkto test it.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.