Repository navigation
Allocate space for user stack at link time - #179
Conversation
5c6a4b8 to
6ccf91a
Compare
|
OK, addressed the comments. please take another look at the code. I'm still looking at how to update the tests. Are the test cases in dot_s autogenerated, or manual? |
|
Manual. But you can update their outputs automatically with |
| prepare(); | ||
| // Place the stack pointer at the bottom of the linear memory, to keep its | ||
| // address small (and thus with a small encoding). | ||
| place_stack_pointer(stackSize); |
There was a problem hiding this comment.
placeStackPointer for consistency, please
|
I had some more general thoughts about this. First, since the stack comes after the static allocations, all the size does for us is potentially increase memory. But the program will need more memory anyhow for sbrk/malloc. And probably the memory size should be arriving from the LLVM backend anyhow? @sunfishcode? Overall, the program's runtime/startup code might do the following:
This seems more flexible, does less work in s2wasm, and is consistent with Emscripten. Thoughts? |
|
Mostly I want this to be able to test LLVM independently of all the emscripten machinery, it doesn't have to be the only or final mechanism. But, here are some other thoughts too: First, of course the stack increases memory. Most programs will still need it. And there's no good way to resize it without mmap (or having it grow upward into a space that can be increased by increasing the entire linear memory). So it will have to be large enough, according to the program's needs. The program will also need heap memory; it would be the job of the allocator to allocate that, and how much it would want initially would be a design decision; presumably it would need to balance the cost of having a large initial size against the (probably large) cost of having to call Also what do you mean by startup code? The LLVM backend doesn't really have a good idea how much stack would be needed (other than whether it's needed at all), but it is possible to have all the stack info come from the .s file. Previously I was testing locally by appending (or prepending) the following to the .s file: If we made s2wasm support commons then it would even support linking multiple files. |
|
Oh, are you just looking for something temporary to replace that appended code? I'm fine with something for that. But I'm a little worried this will help here but then need to be replaced later with something better, and tests will break during that change, etc. In any case, I think we should be able to support both a simple fixed stack if that's useful for you, and the flexible runtime approach that Emscripten uses. Perhaps the option here could do nothing (except return the location of the stack pointer in metadata) when not set? Then the flexible approach would do that.
Sure. I was saying that I think it makes sense to be consistent with what Emscripten does here, both because the consistency is nice for itself, and also because what Emscripten does is the flexible approach of letting the runtime set this, which should be easy for other things to use as well.
I meant the outside JS code. The code that loads the wasm module, connects it to the rest of the world, etc. |
|
I think it would be good to have a temporary thing for now, maybe with a command-line parameter to s2wasm allowing us to specify a non-default stack size. In the long term have the toolchain-provided |
As spec'd in: WebAssembly/design#495 And discussed in: WebAssembly/spec#231 This will make it simpler and more uniform to add a start entry point. s2wasm is the right place to add start because it'll eventually need to do other basic setup, e.g. put code in start to setup the stack, as dschuff is doing in: #179 Or rather, the linker is the right place and s2wasm happens to act as our linker right now.
|
Right, in other words this isn't a toolchain design proposal, it's just supposed to be part of the minimal test harness that we're already using for the torture tests, where we don't need a libc or multi-file linking or startup code. More broadly, I do think we want a more flexible mechanism than this; something that works for now, and when we get mmap, and when we get dynamic linking. I think in general I would prefer the stack allocation to be handled inside the wasm module if possible but I'm open to discussion on that, and this probably isn't the place. Anyway I'll make the option more optional. |
af039ca to
08c208b
Compare
|
Updated, rebased, and squashed. |
As a followup, I think we'll also want a "set memory sizes" option to set the initial and max memory sizes, if they aren't arriving from the LLVM backend. I can do that. |
|
Yeah, I was just thinking about that. I'm fine with keeping that as-is for now. In general though I actually think maybe we don't need to create the stack pointer if there are no references to it in the file. Since we are assuming that this is an already-linked module, if there are no references to the stack pointer, then there need be no stack pointer and no stack.
For case 3, there are a couple of options. |
|
Updated the branch to always generate the pointer as before. |
|
Hmm, I don't see much harm in always creating the stack pointer itself. Since it's tiny. And trying to optimize it out means the code will never have a stack, but we might need one in linked code (JS today, or other wasm some day). |
| [](Options *o, const std::string &argument) { | ||
| o->extra["global-base"] = argument; | ||
| }) | ||
| .add("--stack-size", "-s", "Size of the user stack in linear memory", |
There was a problem hiding this comment.
as mentioned earlier, the name seems wrong to me, if (1) we create the stack pointer anyhow, and (2) the stack might be created by a runtime later. Instead, how about --create-stack or --create-stack-segment? The last seems most precise, but maybe long.
There was a problem hiding this comment.
Sorry I missed the comment about the name. How about --allocate-stack?
There was a problem hiding this comment.
Heh, I think we just wrote it at the same time ;) Sounds great!
|
WRT creating the stack pointer at all: That's why the dynamic linker would set up the stack in the dynamic linking case. Otherwise if there are no references and we are statically linked, then there can be nothing that uses the stack pointer. (This includes JS, which shouldn't be touching the user stack anyway). |
|
I see your point, yeah. |
|
OK, --allocate-stack it is. |
| size_t globalBase = options.extra.find("global-base") != options.extra.end() | ||
| ? std::stoull(options.extra["global-base"]) | ||
| : 1; | ||
| size_t stackSize = |
|
lgtm with that last variable renamed, and squashed. |
Currently s2wasm pre-allocates space for the special __stack_pointer variable but not for the actual stack, and __stack_pointer is null. On real systems the stack is allocated by the dynamic linker, thread runtime, or other toolchain/system-provided startup code. Since we don't have any of that in wasm yet, just add an option to allocate the stack in the linker for now, so we can continue to run single-file libc-free tests.
65b7beb to
c081e08
Compare
Allocate space for user stack at link time
Currently s2wasm pre-allocates space for the special __stack_pointer
variable but not for the actual stack, and __stack_pointer is null. On
real systems the stack is allocated by the dynamic linker, thread
runtime, or other toolchain/system-provided startup code. Since we don't
have any of that in wasm yet, just allocate the stack in the linker for now.