Skip to content

Rework context saving logic#48

Merged
OFFTKP merged 15 commits into
masterfrom
enterexit
Apr 21, 2025
Merged

Rework context saving logic#48
OFFTKP merged 15 commits into
masterfrom
enterexit

Conversation

@OFFTKP

@OFFTKP OFFTKP commented Apr 20, 2025

Copy link
Copy Markdown
Owner

Instead of loading and storing only the registers a basic block uses, load all the registers when entering JIT code, and store them all when exiting JIT code.

There's two benefits with this approach:

  • With block linking this should result in fewer total loads/stores that should boost performance a tiny bit and reduce code size per block, and maybe even more when call/ret and indirect linking is established fully

  • Asynchronous signals can happen at any point in JIT code. Our previous solution was to walk the block instructions to find all the modified registers, and pull them from ucontext. This is a hacky solution and it also shuts down future opportunities for multiple block compilation -- we can't walk recompiled code that contains control flow instructions. With this rewrite we are guaranteed the fact that while we are in JIT code the statically allocated GPRs will contain the correct values. We also have a flag state_is_correct in ThreadState, when we writeback the registers we set it to 0 so that we can modify the registers willy nilly as the ThreadState has the correct values and we don't need to pull from ucontext. This happens for example when we want to call a function (like felix86_syscall) and we writeback the state and overwrite a0 which is otherwise an allocated register

There's also a downside:

  • Blocks that have non-jitted instructions (x87 with 80-bit operands, pcmpxstrx, non-inlineable syscalls, ...) suffer a bit more under this build. Hopefully the difference is not noticeable.

Problems to resolve before merging:

  • Celeste has some seams in the snow -- too many hours wasted chasing this weirdest bug I've ever seen, it's a feature! (until I can make sure it's not a GPU problem)
  • Balatro is flaky Always has been?
  • apt segfault? doesn't happen before this pr
    • happens after apt install supertux2 (package doesnt exist, irrelevant)

felix86 0.1.0.217986d
VLEN: 256
cpu: Spacemit X60 (8) @ 1.600GHz
gpu: AMD ATI Radeon HD 7350/8350 / R5 220
model: Milk-V Jupiter
distro: Bianbu 2.1.1 riscv64
de: GNOME 46.0
wm: Mutter
kernel: 6.6.63
memory: 979MiB / 7836MiB

  • apt
  • SuperTux
  • Baba is you
  • Hotline Miami 2
  • The Binding of Isaac Rebirth
  • Don't Starve
  • World of Goo
  • Celeste
  • openarena
  • SuperTuxKart
  • VVVVVV
  • Balatro

@OFFTKP OFFTKP force-pushed the enterexit branch 4 times, most recently from 0fdaa3a to 217986d Compare April 21, 2025 14:13
@OFFTKP OFFTKP mentioned this pull request Apr 21, 2025
@OFFTKP OFFTKP merged commit 589fdb7 into master Apr 21, 2025
@OFFTKP OFFTKP deleted the enterexit branch April 21, 2025 23:48
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