Shopify / yjit

Optimizing JIT compiler built inside CRuby
636 stars 21 forks source link

Change fixed register mapping to avoid clash with C arg regs #153

Closed maximecb closed 2 years ago

maximecb commented 2 years ago

At the moment, the fixed mapping or registers that we use for REG_EC, REG_SP, REG_CFP clashes with the C argument registers. That makes it tricky to make C function calls without messing up, we have to be really careful. It's somewhat of a pain point at the moment.

Currently (see yjit_core.h):

#define REG_CFP RSI
#define REG_EC RDI
#define REG_SP RDX

C argument registers (see yjit_asm.h):

 #define C_ARG_REGS ( (x86opnd_t[]){ RDI, RSI, RDX, RCX, R8, R9 } )

Callee-save registers: rbx, rbp, r12, r13, r14, and r15 Caller-save (scratch) registers: rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 See System V ABI for reference: https://wiki.osdev.org/System_V_ABI#x86-64

If instead we used callee-save registers for CFP, EC, SP, then we might be able to get away without saving them before making calls, and not have to worry about C argument clashes. This comes with the slight inconvenience that we have to save the callee-save registers that we use when entering YJIT code and save them when leaving YJIT code. I think we can be forward looking here and say we're aiming to get to a point where we stay in YJIT code for as long as possible, so this may pay for itself, since we would be doing less saving and reloading of EC, SP, CFP registers around C calls.

Alternatively, some of the caller-save registers (R10, R11) do not overlap with the C argument registers, and so these could be good options as well, because we don't have to pay a cost to save/restore them when entering/leaving YJIT code (but, callees will also not save them for us).

@XrXr, @jhawthorn , I was wondering if either of you might be interested & willing to take on this issue.

jhawthorn commented 2 years ago

I was wondering if either of you might be interested & willing to take on this issue

👋 Definitely interested in trying out callee-saved registers 😁

maximecb commented 2 years ago

Awesome! :)

If you get it working, are you also able to run yjit-bench before/after, on a Linux machine?