Skip to content

Postpone adding globals until dynamic linking #154

Closed

Description

@lukewagner

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

  1. changed the title [-]Postponing globals until dynamic linking[/-] [+]Postpone adding globals until dynamic linking[/+] on Jun 12, 2015
  2. jfbastien commented on Jun 12, 2015

    @jfbastien
    Member

    "globals declare immutable pointers (i.e., integers)" sounds an awful lot like a relocation!
    Putting my TLS usecases link here.

    This proposal sgtm.

  3. kripken commented on Jun 12, 2015

    @kripken
    Member

    How would this work in the polyfill?

  4. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    Since this would only come with dynamic linking:

    1. for load-time dynamic linking, we can convert global variables into literal integers (assuming the polyfill slurps all the dynamically-linked modules into one asm.js module)
    2. for runtime-dynamic linking, I don't think we have an efficient polyfill option anyway; but anyhow global variables can turn into integer global variables in asm.js.
  5. kripken commented on Jun 12, 2015

    @kripken
    Member

    Sorry, I meant, how would existing asm.js code (no linking) be polyfillable, without globals (which are in asm.js)?

  6. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    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.

  7. kripken commented on Jun 12, 2015

    @kripken
    Member

    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.

  8. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    Yeah, that was useful for experimentation purposes, but I don't think it should constrain the overall design.

  9. kripken commented on Jun 12, 2015

    @kripken
    Member

    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.

  10. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    For Emscripten's case can't you just control codegen not to emit globals (to use heap instead, as is already supported)?

  11. kripken commented on Jun 12, 2015

    @kripken
    Member

    That is what I am thinking about, sure, but it isn't quite so simple. Not all the globals come from compiled code.

  12. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    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.

  13. kripken commented on Jun 12, 2015

    @kripken
    Member

    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:

    void withGlobals() {
      i32 s = sp;
      i32 buffer = s;
      i32 more = s + 8;
      sp = s + 16;
      // ..
      operate(buffer, more);
      // ..
      sp = s;
    }
    
    void withoutGlobals() {
      i32 s = load32(1024);
      i32 buffer = s;
      i32 more = s + 8;
      store32(1024, s + 16);
      // ..
      operate(buffer, more);
      // ..
      store32(1024, s);
    }
    

    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.

  14. lukewagner commented on Jun 12, 2015

    @lukewagner
    MemberAuthor

    With the above proposal, sp is just replaced with load(sp) and store(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.

  15. kripken commented on Jun 12, 2015

    @kripken
    Member

    I didn't realize that about the proposal, but sp is still significantly better than load(sp) imo.

  16. 27 remaining items

  17. lukewagner commented on Sep 10, 2015

    @lukewagner
    MemberAuthor

    ...because I agree that we should try not to put stuff in the MVP that will have to be changed
    post-MVP, and this will need to be changed sooner or later to support dynamic linking.
    That it will need to change is uncontroversial, right?

    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.

  18. AndrewScheidecker commented on Sep 10, 2015

    @AndrewScheidecker

    I think the main module (which is all that MVP has) should always be able to absolutely position its data segments

    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?

    (just like on native)

    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.

  19. lukewagner commented on Sep 10, 2015

    @lukewagner
    MemberAuthor

    @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:

    1. we'll have two separate kinds of modules and it will make sense for main modules to be able to absolutely position their data segments; or
    2. we'll have just one kind of module, but some modules will effectively only work when loaded as the main module and thus will be able to absolutely position their data segments.

    So I don't see any downside to starting with absolutely-positioned data segments.

  20. AndrewScheidecker commented on Sep 10, 2015

    @AndrewScheidecker

    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.

  21. lukewagner commented on Sep 10, 2015

    @lukewagner
    MemberAuthor

    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.

    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 malloc heap) and key variables like sp, they're going to clobber each other.

    and they imply this main module distinction that isn't otherwise implied by anything else in the MVP
    design.

    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).

  22. jfbastien commented on Sep 11, 2015

    @jfbastien
    Member

    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!

  23. jfbastien commented on Sep 11, 2015

    @jfbastien
    Member

    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.

  24. AndrewScheidecker commented on Sep 11, 2015

    @AndrewScheidecker

    Modules that are not specifically compiled to be dynamically linkable are not going to be magically dynamically linkable

    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.

    If two modules that are dynamically linked into the same instance do not explicitly cooperate on sharing libc (think about the malloc heap) and key variables like sp, they're going to clobber each other.

    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.

    but that discussion should go in #302

    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.

  25. lukewagner commented on Sep 11, 2015

    @lukewagner
    MemberAuthor

    @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.

  26. jfbastien commented on Sep 11, 2015

    @jfbastien
    Member

    @lukewagner said:

    @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 "aliased global variables (with array types)" solidly in "future features".

    Agreed.

  27. lukewagner commented on Sep 11, 2015

    @lukewagner
    MemberAuthor

    (D'oh, and of course, I meant "unaliased global variables", but I think you understood it as that anyway :)

  28. lukewagner commented on Sep 14, 2015

    @lukewagner
    MemberAuthor

    This issue will be resolved by #344 (still open for feedback).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions