Closed Bug 495403 Opened 17 years ago Closed 17 years ago

TM: Add an ABI flag to nanojit to restore the register state after calls

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 514374

People

(Reporter: gal, Assigned: n.nethercote)

References

Details

Suggested name: ABI_NOSPILL. The idea is to restore the scratch registers that were evicted for the call immediately after the call. This minimizes the impact on the register allocator at merge points. This is useful for calling builtins in a known slow-path on trace in a cold branch path (i.e. unbox int, and very rarely handle the 32-bit cases that reads a double and then converts to int). Lobbing this over Nick to see what he thinks about it.
Blocks: 495402
I'm happy to take a look. Some questions: - There are no comments explaining the existing call ABI alternatives. Are they documented anywhere? - Is the aim of this bug just to implement it in Nanojit? How will NOSPILL calls be created? It'll be hard to test if none are created. - Are there any benchmarks that are likely to improve with this change?
1: The code is the best documentation we have. Its basically FASTCALL, CDECL and THISCALL. I think those are pretty standard and well defined. 2: The idea is to use them as soon we have them. You could just make all calls NOSPILL and then test with that whether it works (ignore performance loss). 3: We will inline unbox/box with this and see how much it helps. I don't think we can guestimate this well without actually implementing it. But the pile of patches should be short, and each patch should be simple enough to take the risk.
Re calling conventions, based on (http://en.wikipedia.org/wiki/X86_calling_conventions) and reading the code I have the following for x86: - all of them: - return value in eax (for integers) - eax/ecx/edx available for use in the function - ABI_FASTCALL: - 1st arg in ecx, 2nd in edx, rest on stack (pushed right-to-left) - callee cleans up the stack. - ABI_THISCALL: [Not used in TM] - 'this' in ecx, args on stack (pushed right-to-left) - callee cleans up the stack. - ABI_STDCALL: [Not used in TM or TR] - args on stack (pushed right-to-left) - callee cleans up the stack. - ABI_CDECL: - args on stack, pushed right-to-left - caller cleans up the stack. With ABI_NOSPILL added there are 5 ABIs, which requires 3 bits to hold, but we don't have any spare bits in the 2nd word of struct CallInfo. But it looks like we can remove ABI_STDCALL so maybe this isn't a problem. I'm not yet sure how ABI_NOSPILL fits in. Sounds like it's similar to ABI_CDECL but you want to immediately restore the scratch regs (eax/ecx/edx on x86, I think, plus FpRegs?) so that the register state is exactly the same as it was before the call occurred. (Or is it the same as ABI_FASTCALL but with the restoration of scratch registers?) But the restoration of scratch regs sounds like something beyond the calling convention, more a description of what you should do once the call is done. Or maybe I'm just being pedantic. W.r.t. "inline unbox/box", you mean js_UnboxInt32(), js_UnboxDouble(), js_BoxInt32(), js_BoxDouble()? If so, I don't understand how C functions can be inlined in TM code. I'm obviously misunderstanding something. And I'm still very vague on the scenario where this helps. I.e. I don't understand the "This is useful..." sentence in comment 0. Any example code that shows bad spilling behaviour that would be affected by this would be very helpful. Sorry, I still have lots to learn about how the tracing works.
NOSPILL is not a new ABI. Its a flag, that indicates that the call should be performed by explicitly saving non-volatile registers instead of spilling them. The goal is to avoid spilling, since spilling effects both sides of a merge point. Consider the following pseudo LIR: if IS_INT(val) else goto y; x = val >> 1 goto x; y: x = call(UnboxDoubleAsInt32); x: Here we generate efficient code for the first (fast) path. It only uses one register and no spill. Unfortunately, the call does a bunch of spills, and we have to reconcile those in y and the branch point (if), so we end up with a bunch of register and spill movements. What I want is being able to call such slow path code (UnboxDoubleAsInt32) without affecting the code quality of the fast path. Whether the call is there or not, I want all surrounding code to unaffected. If the NOSPILL flag is set, the call uses PUSH instead of spilling to save volatile registers and immediately after the call it does a bunch of POPs. This way there is no compensation for register states due to call spilling. As for inlining unbox, we simply take what BoxInt32 does, and translate it to lir. I call that inlining. Right now we will do it by hand.
Thanks for the explanation, it's making a lot more sense now. To summarise: it's a way of doing a call that effectively doesn't affect the register allocation state. And this is worth doing on slow paths in order to avoid hurting fast paths. And it's a flag that applies to call sites, rather than particular functions, right? (And independent of the calling convention used by the called function?) Which means it should be stored in LInsC... that's fine, there's plenty of free space there (24 bits currently). IIUC a working patch won't be that big -- it'll mostly be some modifications to Assembler::asm_call(). (Hmm, merge points don't normally occur in traces... the merge point in this case is because we're creating the LIR by "inlining" the built-in.)
Bug 514374 should make this a lot easier.
Depends on: 514374
514374 actually solved this issue. I am already using it in some experimental code. Its a lot nicer than explicit PUSH/POP spilling, too :)
Status: NEW → RESOLVED
Closed: 17 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.