WIP: fix(lift): with local registers, a function with code below its entry loads them before its entry #1587
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
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1587
Loading…
Reference in a new issue
No description provided.
Delete branch "dyst/Longshot:fix/lift-entry-local-regs"
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?
Type: fix · Risk: low · Tested on: Windows 11, RTX 3080, Ryzen 9 3900X (game-free; the default lift compared byte for byte) · Needs: none for the default build; a game run with local registers on, any PC
TL;DR
With the translator's local registers turned on, the 26 game functions whose code starts below their entry point began by jumping over the lines that load their registers, so they ran on garbage values. It never happened in a normal build, where local registers are off, but pull request #1469 turns them on by default. This makes those functions load their registers first. A normal build's translated game is byte for byte the same; a new game-free test fails on main and passes here.
Breakdown
A64_LOCAL_REGSorA64_LOCAL_FLAGSset, and #1469, which turns them on by default.tests/test_lift_entry_local_regs.pyfails on main and passes here, and fails again with the fix's line taken out; the default lift compared byte for byte with main's.Why
nx2/lift_fixes.pystarts a function whose code lies below its entry withgoto L_<entry>;right after the head (its fix 4; 26 functions in a lift of OoT). WithA64_LOCAL_REGSorA64_LOCAL_FLAGSthe kit'slocalize_regsdeclares the function's register locals right after the head too, each loaded from the context (uint64_t x0_ = c->x[0], ...;). The goto came first, so it jumped over those declarations: C allows that, but the locals are never loaded. With #1469's defaults each of those 26 functions ran on whatever the stack held.Stacked on #1560, whose
nx2/emit.pyholds the head lines this moves.How to reproduce
Seen on: any PC, game-free
Settings and switches:
A64_LOCAL_REGS=6at lift timeSteps:
python -I -B tests/test_lift_entry_local_regs.pyon main; or lift withA64_LOCAL_REGS=6and read any functionnx2/lift_fixes.pygives an entry gotoExpected: the locals' declarations, then the goto
Actual: the goto, then the declarations it jumps over
What changed
The head lines (entry hooks' call, the entry goto) now go into the function's lines before the kit's
localize_regsruns, instead of being spliced into its finished text after it;localize_regsthen declares the locals right after the head, ahead of them. A PLT stub, which the method writes and returns before that point, still gets them in its text.nx2/emit.py:emithands the head lines toemit_function, which puts them after the head before its last passes (two tagged lines); a PLT stub keeps the text splice.tests/test_lift_entry_local_regs.py(new): a made-up function with code below its entry, emitted with local registers, local flags and both: every declaration that loads a local comes before the goto; without them the goto still follows the head.Measurements
Verdict: nothing to measure for a default build: its translated game is byte-identical to main's. With local registers on (this fix and #1469's change, registers only), the game plays, and the frame rate is within noise of main's on this PC: +0.2% in Hyrule Field and +1.6% in the Market, over 3 interleaved pairs.
310e7c9d(the base's lift)sa/gen,sa/image), byte for byteFlags and settings
None.
Testing
Tested:
tests/test_lift_entry_local_regs.pyfails on main and on #1560 (the goto on line 1, the declarations on line 2) and passes here; with the fix's line taken out it fails again.The default lift of this branch and the base's lift of main at
310e7c9d: byte-identical.tests/test_lift_emit.py,test_lift_entry_below.py,test_spawn_emit.py,test_split_giants.py,test_mark_groups.py,test_fastfp_regions.py,test_parallel_emit.pypass on Windows 11.tests/test_libc_hooks.pyfails here and on main alike (it looks forL.discover(innx2/lift.py).In the game (Windows 11, RTX 3080, Ryzen 9 3900X, NVIDIA's driver): a pack over
310e7c9dwith this fix and #1469's change (A64_LOCAL_REGS=6, flags left in the context), 4 runs of the A/B kit's route from a Hyrule Field save (a walk and a look round, then the Market): no crash or hang, and every game patch applied.Not tested:
tests/run_all.pyin full; Linux.Must be tested: None beyond Linux, any PC, with
A64_LOCAL_REGS=6(Test Farm: linux, a few minutes of play).Risks
What could break: in a default build, nothing (byte-identical). With local registers, where a function's head lines go.
Turning it off: revert the commit.
Compatibility: a player's next build translates the game again once (the lift key hashes
nx2/); nothing recompiles.Checklist
Everyone
type(scope): description) of the status line's type.can't be run by hand is in Testing's Not testable by hand, with why. Default build: byte-identical; the run with local registers on is pending.
in Risks.
main. Stacked on #1560, on310e7c9d.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named). Not run in full yet.third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.mainand passing here.🤖 Generated with Claude Code
Tested in game: Windows 11, RTX 3080 (NVIDIA driver), Ryzen 9 3900X: a pack over
310e7c9dwith this fix and #1469's change (A64_LOCAL_REGS=6, flags left in the context), 4 runs of a Hyrule Field walk and look round then the Market from a save: no crash or hang, every game patch applied; frame rate within noise of main (+0.2% Hyrule Field, +1.6% Market over 3 interleaved pairs). A default build's translated C is byte-identical to main's.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.