Repository navigation
Description
Split off from #139: I was thinking that perhaps globals should be removed from the MVP and instead go in with the dynamic linking feature. The reasoning is that, until we have to worry about dynamic linking, we can simply place globals in the heap and use compiler-chosen static offsets; indeed this is what we'll need to do anyway for any global whose address is taken (and what Emscripten does for most globals anyway, to avoid JS engine number-of-closure-variable limitations). Technically, globals have the advantage that they can't be aliased by loads/stores, but I don't expect this will win much in practice (esp. assuming a C++ compiler has already optimized the code).
Thinking forward to dynamic linking: since we have to deal with aliased globals anyway, it seems simpler and more orthogonal (not requiring a separate set of LoadGlobal/StoreGlobal ops) to have globals declare immutable pointers (i.e., integers) that would then be used as arguments to plain LoadHeap/StoreHeap ops (so LoadGlobal(a) => LoadHeap(Global(a)) where Global(a) is a const-expr of int32/int64 type). Even trivial backends should have no problem eliminating bounds checks.
With this strategy, the question is of course where the memory pointed to by the global pointers comes from: the engine has no knowledge of how the memory in the [0, sbrk-max) range is being used by malloc et al. There is a lot of room for design here, but I think roughly what we need to do is let the application allocate the data and give it to the engine (either directly or by registering a global data allocator with the runtime). The important thing is that we keep allocation (and addresses) deterministic and under application control so that applications can do smart things with their address space (shadow stacks, emulating MAP_FIXED, etc).
Lastly, I think this same strategy applies to TLS variables: TLS variables would be pointers into the heap and we'd need a way for the user application to allocate the memory pointed to by these TLS variables (when new threads are created or for each thread when new modules are loaded with TLS variables; that's why I feel like these two issues are related and can have symmetric solutions).
Activity
[-]Postponing globals until dynamic linking[/-][+]Postpone adding globals until dynamic linking[/+]jfbastien commentedon Jun 12, 2015
"globals declare immutable pointers (i.e., integers)" sounds an awful lot like a relocation!
Putting my TLS usecases link here.
This proposal sgtm.
kripken commentedon Jun 12, 2015
How would this work in the polyfill?
lukewagner commentedon Jun 12, 2015
Since this would only come with dynamic linking:
kripken commentedon Jun 12, 2015
Sorry, I meant, how would existing asm.js code (no linking) be polyfillable, without globals (which are in asm.js)?
lukewagner commentedon Jun 12, 2015
Well, asm.js could still use asm.js. Unless you're asking about asm.js->wasm->asm.js; that seems harder (I can imagine hacky solutions, though, especially if Emscripten were to participate), but that seems ok.
kripken commentedon Jun 12, 2015
Was that not the point of your polyfill prototype, asm.js=>wasm=>asm.js? I guess I am surprised to hear that is being given up on? This is the first feature that seems to break that.
lukewagner commentedon Jun 12, 2015
Yeah, that was useful for experimentation purposes, but I don't think it should constrain the overall design.
kripken commentedon Jun 12, 2015
It will also be a limitation on using emscripten to emit wasm - as we were intending to go through asm.js in some parts of our pipeline (I discussed details of that at length with @sunfishcode). This would appear to require a complete rethinking of that strategy. I'll think more about that, but in general I think this aspect - breaking asm=>wasm - is something we need to discuss and consider carefully. I was operating under that assumption, removing it would be a big change.
lukewagner commentedon Jun 12, 2015
For Emscripten's case can't you just control codegen not to emit globals (to use heap instead, as is already supported)?
kripken commentedon Jun 12, 2015
That is what I am thinking about, sure, but it isn't quite so simple. Not all the globals come from compiled code.
lukewagner commentedon Jun 12, 2015
In the limit, the polyfill can always transform reads/writes to globals to FFI calls that use outside-asm.js storage. I don't think this corner case should influence the much-longer-lived standard, though.
kripken commentedon Jun 12, 2015
In general I agree the polyfill should not influence long-term plans, and good point that ffis can be a non-performant polyfill, if we need that.
Anyhow, getting back to the topic here: I think globals are a very convenient feature for human beings. Just like having the
!=operation. Still matters even if generally compilers emit wasm code; humans still do View Source it, debug it, etc. To be concrete, a very important global is the C stack pointer, and what is better of the following:The stack pointer is an extremely common thing, appearing at the beginning and end of most functions (and also sometimes in the middle). Without globals, our View Source suffers significantly, and in particular regresses significantly compared to asm.js (which is a shame since we win in so many other ways!). And with decreases in source readability come downsides in debuggability, understanding performance profiles, etc.
lukewagner commentedon Jun 12, 2015
With the above proposal,
spis just replaced withload(sp)andstore(sp, s)(assuming we have symbol data, which is the assumption of your snippets above) which seems just as readable and is more honest about what's happening.kripken commentedon Jun 12, 2015
I didn't realize that about the proposal, but
spis still significantly better thanload(sp)imo.27 remaining items
lukewagner commentedon Sep 10, 2015
Nope: I think the main module (which is all that MVP has) should always be able to absolutely position its data segments (just like on native); it's only dylibs (or whatever we're calling modules that can be dynamically linked) that require dynamically positioned data segments. Concretely, I think this will mean that dylibs have a different type of segment stored in their memory section which doesn't specify a start address and instead declares a global immutable pointer.
AndrewScheidecker commentedon Sep 10, 2015
That works given a distinction between main modules and library modules. But I'm not sure why the distinction of main module is needed as anything other than "module which happened to be loaded into this instance first". A typical use case for WebAssembly seems like the main module will actually be a library of functions that are called by JavaScript, rather than something like an executable with a single entry point. Once there's dynamic linking, should such a module only be usable as the main module of an instance?
IMO this is a legacy of native that doesn't benefit WebAssembly. Once you give guarantees about where data segments will be loaded, you can never take that back.
lukewagner commentedon Sep 10, 2015
@AndrewScheidecker I think the distinction is pretty fundamental as soon as you start thinking about how libc and other shared state would work. It's important to distinguish the use cases for dynamic linking vs. plain MVP modules and imports. In the case of modules and imports, the intention is, like you said, for each module to stand on its own as a collection of functions that can be called from dependent JS or wasm modules; every module in the MVP is a "main" module and thus enables the type of reuse you're talking about. But for dynamic linking, dylibs must carefully coordinate wrt linear memory, ABI (including the shared
sp), initialization, etc, to play well with other modules in the same instance; w/o careful coordinate, modules will naturally clobber each other.So either:
So I don't see any downside to starting with absolutely-positioned data segments.
AndrewScheidecker commentedon Sep 10, 2015
To be clear, I'm not saying that MVP modules should eventually work flawlessly as dynamically linked modules, just that the MVP should avoid designs that we know will need to change to support threading and dynamic linking.
In the MVP, there isn't a main module distinction because every module is a main module. But allowing those modules to give the data segment a static location in memory means that post-MVP that capability must either be removed, or we must distinguish such modules as not dynamically linkable.
We know that we eventually need relocatable data segments. It's arguable whether static data segments are useful, and they imply this main module distinction that isn't otherwise implied by anything else in the MVP design.
lukewagner commentedon Sep 10, 2015
Modules that are not specifically compiled to be dynamically linkable are not going to be magically dynamically linkable, no matter what we do with global data sections. If two modules that are dynamically linked into the same instance do not explicitly cooperate on sharing libc (think about the
mallocheap) and key variables likesp, they're going to clobber each other.I think we'll find multiple reasons to make distinctions between main and dylibs when we really dig in (I won't go into it here, but other examples have come up). Regardless, if we did want to avoid any distinction, we could allow all modules to use either static or dynamic data segments. Load-time-linked dylibs could know a static address just as well as a main module (load-time linking is just like a single main module that has been broken up for caching benefits).
jfbastien commentedon Sep 11, 2015
I agree with @AndrewScheidecker that we want main modules to be able to position its data segment more freely, but that discussion should go in #302 :-)
@lukewagner makes a point about "old modules" that aren't dynamically linkable, and their non-interaction with "new modules" which are. We can indeed have two worlds that have different capabilities, but I do think that it would be nice to get them to be the same by design!
jfbastien commentedon Sep 11, 2015
I've been thinking recently: one thing I like about globals is that they don't require being contiguous in virtual memory. They stand to significantly reduce virtual memory fragmentation by being easier to move because they're much smaller. That payoff is limited to C/C++ (address-not-taken globals) but may be bigger in other languages (Fortran!). This is pretty important when considering that wasm coexists in the same process as other web things.
AndrewScheidecker commentedon Sep 11, 2015
I think we're on the same page: I don't think MVP modules need to be forward compatible with dynamic linking. I was trying to say that non-relocatable data segments imply a distinction between modules that are dynamically linkable and those that are not, and that nothing else in the MVP seems to require that distinction.
I'm not saying that relocatable data segments are the feature needed to make dynamic linking work, just that they're just an obvious requirement, and it affects functionality in the MVP. Making sure all the modules in an instance link to the same libc (or whatever language runtime module) is certainly a problem, but I don't think is dependent on functionality in the MVP.
The bearing on this issue is that if MVP data segments are relocatable they need some module-scoped namespace for immutable values to bind their base address in (similar to what's described in the initial issue). That's a component of global variables, but doesn't require actual variables.
Regardless, we can put aliased global variables in data segments for the MVP. Post-MVP we can give data segments a thread-local qualifier to say the runtime should instance them for each thread in TLS. With dynamic linking, we can import/export data segments as untyped variables.
Do we need unaliased global variables for the MVP, or after, or never? TBH the stack pointer is the only thing I see that benefits from unaliased global variables, and that may be better implemented as a more specialized state of the runtime.
lukewagner commentedon Sep 11, 2015
@jfbastien Agreed with @JSStats that the savings would be relatively minor in C/C++. Maybe FORTRAN would find a use (esp if we allow array types for globals, as we've discussed already), but I think this line of reasoning puts "
aliasedunaliased global variables (with array types)" solidly in "future features".@AndrewScheidecker Even after we have full dynamic linking support, some modules will want to absolutely position their global data segments, so starting with only providing absolute offsets makes sense not just in the MVP, but after. Furthermore, I expect absolute addresses will be slightly more efficient when a wasm engine generates relocatable code (using a GOT instead of baking in the address, motivated by machine-code caching/sharing), so apps would be motivated to use absolute offsets when they could (when they were the main module or a load-time-linked module). I don't think we can force modules to be fully relocatable, so we shouldn't try in this one instance, especially when it increases the risks I described earlier that what we specify in MVP will end up being slightly wrong when we do dynamic linking for real.
jfbastien commentedon Sep 11, 2015
@lukewagner said:
Agreed.
lukewagner commentedon Sep 11, 2015
(D'oh, and of course, I meant "unaliased global variables", but I think you understood it as that anyway :)
lukewagner commentedon Sep 14, 2015
This issue will be resolved by #344 (still open for feedback).