Bug 146846 - jsc-tailcall: JavaScript functions should restore the stack pointer after a call
Summary: jsc-tailcall: JavaScript functions should restore the stack pointer after a call
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Basile Clement
URL:
Keywords:
Depends on:
Blocks: 146484 146847
  Show dependency treegraph
 
Reported: 2015-07-10 12:23 PDT by Basile Clement
Modified: 2015-07-14 14:20 PDT (History)
4 users (show)

See Also:


Attachments
Patch using StackMap size (5.13 KB, patch)
2015-07-13 11:53 PDT, Basile Clement
msaboff: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Basile Clement 2015-07-10 12:23:26 PDT
The LLint and baseline JIT are already doing this, we statically know the stack size in the DFG, and we can get it from the LLVM stackmap in FTL code - so this should be an easy change.

Once https://bugs.webkit.org/show_bug.cgi?id=146845 is taken care of as well, this will allow us to:

 1) Get rid of the arity fixup return thunk (since the caller now knows where the stack pointer should be as an offset of its base pointer)

 2) Actually implement tail calls since they can shrink or extend the stack in unpredictable ways.
Comment 1 Basile Clement 2015-07-13 11:53:48 PDT
Created attachment 256709 [details]
Patch using StackMap size

This uses the stackmap size from LLVM to restore the stack pointer to where it should be in the patchpoint. I am interested in hearing opinions on the following two points:

 - We may want to adapt the default patchpoint size to accommodate the additional lea opcode.

 - LLVM provides a stackmap/stackrestore intrinsic that we should be able to use. I am unsure what guarantees we would have with this; in particular, I fear LLVM spilling values onto the stack before the call but after saving the stack pointer, performing the call, then trying to restore those values using the stack pointer as it is supposed to be callee-save. I did not see this happen even when testing that approach with high register pressure (LLVM ends up using bp-based offsets for the spilling), but again, I am unsure how much we can rely on this. I believe messing up with the stack pointer in the patchpoint is better because it makes it completely transparent to LLVM.
Comment 2 Basile Clement 2015-07-13 12:52:58 PDT
(In reply to comment #1)
> Created attachment 256709 [details]
> Patch using StackMap size
> 
> This uses the stackmap size from LLVM to restore the stack pointer to where
> it should be in the patchpoint. I am interested in hearing opinions on the
> following two points:
> 
>  - We may want to adapt the default patchpoint size to accommodate the
> additional lea opcode.
> 
>  - LLVM provides a stackmap/stackrestore intrinsic that we should be able to
> use. I am unsure what guarantees we would have with this; in particular, I
> fear LLVM spilling values onto the stack before the call but after saving
> the stack pointer, performing the call, then trying to restore those values
> using the stack pointer as it is supposed to be callee-save. I did not see
> this happen even when testing that approach with high register pressure
> (LLVM ends up using bp-based offsets for the spilling), but again, I am
> unsure how much we can rely on this. I believe messing up with the stack
> pointer in the patchpoint is better because it makes it completely
> transparent to LLVM.

After talking offline with fpizlo, the stackmap size approach looks like the best one.
Comment 3 Michael Saboff 2015-07-14 14:03:51 PDT
Comment on attachment 256709 [details]
Patch using StackMap size

View in context: https://bugs.webkit.org/attachment.cgi?id=256709&action=review

r=me with suggested name change to a function.

> Source/JavaScriptCore/ftl/FTLJSCall.cpp:54
> +void JSCall::emit(CCallHelpers& jit, unsigned stackSize)

Instead of calling this "emit", how about "emitRestoreStackPointer"?
Comment 4 Basile Clement 2015-07-14 14:09:17 PDT
(In reply to comment #3)
> Comment on attachment 256709 [details]
> Patch using StackMap size
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=256709&action=review
> 
> r=me with suggested name change to a function.
> 
> > Source/JavaScriptCore/ftl/FTLJSCall.cpp:54
> > +void JSCall::emit(CCallHelpers& jit, unsigned stackSize)
> 
> Instead of calling this "emit", how about "emitRestoreStackPointer"?

FTR, after discussion offline, we'll keep "emit" since this is doing more than just restoring the stack pointer.
Comment 5 Basile Clement 2015-07-14 14:20:20 PDT
(In reply to comment #4)
> (In reply to comment #3)
> > Comment on attachment 256709 [details]
> > Patch using StackMap size
> > 
> > View in context:
> > https://bugs.webkit.org/attachment.cgi?id=256709&action=review
> > 
> > r=me with suggested name change to a function.
> > 
> > > Source/JavaScriptCore/ftl/FTLJSCall.cpp:54
> > > +void JSCall::emit(CCallHelpers& jit, unsigned stackSize)
> > 
> > Instead of calling this "emit", how about "emitRestoreStackPointer"?
> 
> FTR, after discussion offline, we'll keep "emit" since this is doing more
> than just restoring the stack pointer.

Committed r186815 <http://trac.webkit.org/changeset/186815>.