-
Notifications
You must be signed in to change notification settings - Fork 696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stacktop #360
Comments
From looking at Emscripten code converted to WASM, there's an awful lot of code like this:
I think an alloca operation that is implicitly popped on return is all you need:
|
Alloca would be perfect. That's all I want. |
The current thinking from #154 and related conversations is that when we add threads, we'll add thread-local unaliased globals, which are expected to be well suited for these tasks. In the brief time before we add threads, storing STACKTOP in linear memory is not very problematic since there's only one thread. And, this saves us from having to add new features to the language. Without threads yet, it's difficult to make sure that features like this that we add now will interact well with threads when we do add them. |
User-space STACKTOP is dangerous because it's a fundamental ABI primitive. Whatever becomes convention in the MVP era, we'll be stuck with forever. |
We're not planning to have a stable ABI until we add dynamc linking. |
... stable library ABI, that is. wasm itself will be a stable format, but an MVP-era wasm (static) library may not be able to be linked with a dynamic-linking-era wasm libary. |
Links to discussions of ABIs: for C and C++ and dynamic linking in general. |
Are we really okay with every function body containing duplicated logic to set/restore a custom I don't think there's any argument to be made that |
I recommend reading the following:
The research has done very similar things to what we're discussing: shadow stack (wasm's locals), on-heap stack for address-taken things (wasm's stack top). They have numbers on how much of each exists in real-world code, and numbers on what that costs at runtime. |
@sunfishcode Would it cause any problems to add TLS to the MVP for this use? MVP is single-threaded so the polyfill could implement these as asm.js globals? This might save reworking code generators in the interim. |
(@kg and I discussed this in person) I don't think it's worth having It may be a nice size win, but I think we want data before we spec it in. |
@JSStats That was originally my preference as well, however there's concern that if wasm has thread-local variables before it has threads, they may not be designed right, and it could cause problems when threads are added. It seems like the risk of trouble ought to be low, but on the other hand, managing STACKTOP is believed to be only a minor inconvenience for code generators. |
I concur that just putting STACKTOP in the instance memory is good enough for the MVP.
Yes, I was thinking something the same as LLVM's or C's alloca. If the function uses There are two components to the
The argument for letting the runtime control the stack pointer is that it wouldn't require any modifications to extend to support threading, dynamic linking, exceptions, or a cross-language ABI. The argument against is that it should eventually be possible to reproduce the functionality with a thread-local, imported global variable. I don't have a clear argument for |
The reason that we're seeing the load of globals into locals in the Emscripten-generated code is because global loads in general (when not eliminated by GVN) require real loads from memory; storing the global in a local makes it easy for even a simple compiler to store Agreed that where we want to go is unaliased TLS variables that can literally be stored in the engine's TLS array and, for dynamic linking, this would be solved by an explicit named sharing. In the meantime, where all we support is static-linking/single-threading, I agree with above comments that a constant heap address if Good Enough. |
Now that global vars are gone we need a simple way to track things like the current top of the stack. I think we should do this with a special opcodes or host imports, i.e.
We probably will want to have a similar register for the current thread ID once we have threads (this will make it easier for applications to do TLS, etc).
Solving this in user-space is a bad idea, I think.
The text was updated successfully, but these errors were encountered: