Skip to content

resize_memory, memory_size, page_size: opcodes or intrinsic functions? #362

Closed

Description

@AndrewScheidecker

Is there a reason for these to being special opcodes vs just being functions imported from some implementation-provided intrinsic module? I think there are three things to trade off here:

  • It's simpler in my implementation, and I imagine most others to add an intrinsic function.
  • A dedicated opcode is more efficient for encoding frequently used operations.
  • It's simpler to dispatch dedicated opcodes if you want to generate machine code that's not just a call.

I think all three of these opcodes (resize_memory, memory_size, and page_size) could all be intrinsic functions with very little effect on size or speed, so I don't think it's worth having dedicated opcodes for these.

Activity

  1. jfbastien commented on Sep 19, 2015

    @jfbastien
    Member

    ASTSemantics isn't very clear on whether they're opcodes or intrinsics! I'd been assuming that they were intrinsics as that seems more natural.

  2. AndrewScheidecker commented on Sep 19, 2015

    @AndrewScheidecker
    Author

    ASTSemantics says "builtin operation", which is a little ambiguous, but to
    me means a special type of AST node with a dedicated opcode.

  3. sunfishcode commented on Sep 20, 2015

    @sunfishcode
    Member

    Presently, wasm has no intrinsic functions.

    2 of the 3 bullet points in the original post here favor dedicated opcodes, and for the third, it's not clear why intrinsic functions would be easier to implement. And, it keeps the overall system simpler if we don't have multiple kinds of things (operators and intrinsics) that serve the same purpose.

  4. AndrewScheidecker commented on Sep 20, 2015

    @AndrewScheidecker
    Author

    wasm does have intrinsic functions, they're just not called that. stdio.print in the spec interpreter is an example of what I mean by "intrinsic function". It's just a function that's imported from a module defined by the implementation. So a resize_memory intrinsic would look something like this:

    (import $resize_memory "wasm_intrinsics" "resize_memory" (param i32))
    ...
    (call_import $resize_memory (const.i32 10000))
    

    It's easier to implement such a function because most consumers of the AST don't need to know anything about it.

  5. sunfishcode commented on Sep 21, 2015

    @sunfishcode
    Member

    stdio.print is a host function, rather than being part of the main wasm spec, and we can have fairly clear criteria for what's a host function and what's not: host functions just do eg. I/O and don't need to interact with core semantics in any special way. resize_memory affects sandboxing, so it is something that many consumers will need to be aware of.

  6. titzer commented on Sep 21, 2015

    @titzer

    Operations resizing and otherwise manipulating the linear memory are
    fundamental to the engine's operation, so I think they make the most sense
    as opcodes.

    On Mon, Sep 21, 2015 at 6:11 AM, Dan Gohman [email protected]
    wrote:

    stdio.print is a host function, rather than being part of the main wasm
    spec, and we can have fairly clear criteria for what's a host function and
    what's not: host functions just do eg. I/O and don't need to interact with
    core semantics in any special way. resize_memory affects sandboxing, so it
    is something that many consumers will need to be aware of.

    —
    Reply to this email directly or view it on GitHub
    #362 (comment).

  7. AndrewScheidecker commented on Sep 21, 2015

    @AndrewScheidecker
    Author

    Why does "fundamental to the engine's operation" mean it needs to be an opcode? We'll eventually have things like dlopen, mmap, and create_thread that are "fundamental to the engine's operation", but they don't need to be opcodes any more than resize_memory does. It can work either way, but I think defining another 20 opcodes for such things would be very unwieldy.

    I made a branch of the spec interpreter with these implemented as intrinsic functions instead of opcodes: AndrewScheidecker/WebAssembly-spec@74c1cfc

    It removes their implementation from the lexer, the parser, the type checker, and the evaluator. Instead, the import matching is extended to call Intrinsics.match_import if the import module is "wasm_intrinsics", and those functions are defined in the spec/intrinsics module. That module is distinct from the host/builtins module to make it clear that they are an integral part of the spec and not something host-specific.

    I hope you can see that in this branch adding e.g. a get_external_time function to the Intrinsics module would be pretty simple compared to adding an opcode for it.

  8. titzer commented on Sep 21, 2015

    @titzer

    On Mon, Sep 21, 2015 at 4:11 PM, Andrew Scheidecker <
    [email protected]> wrote:

    Why does "fundamental to the engine's operation" mean it needs to be an
    opcode? We'll eventually have things like dlopen, mmap, and create_thread
    that are "fundamental to the engine's operation", but they don't need to be
    opcodes any more than resize_memory does. It can work either way, but I
    think defining another 20 opcodes for such things would be very unwieldy.

    Why exactly would that be unwieldy? Why define a different kind of animal
    (i.e. an intrinsic function) that is somehow shaped differently than an
    opcode, but is just as mandatory, has a well-defined signature and
    well-defined semantics, etc?

    I made a branch of the spec interpreter with these implemented as
    intrinsic functions instead of opcodes:
    AndrewScheidecker/WebAssembly-spec@74c1cfc
    AndrewScheidecker/WebAssembly-spec@74c1cfc

    It removes their implementation from the lexer, the parser, the type
    checker, and the evaluator. Instead, the import matching is extended to
    call Intrinsics.match_import if the import module is "wasm_intrinsics",
    and those functions are defined in the spec/intrinsics module. That module
    is distinct from the host/builtins module to make it clear that they are
    an integral part of the spec and not something host-specific.

    I hope you can see that in this branch adding e.g. a get_external_time
    function to the Intrinsics module would be pretty simple compared to
    adding an opcode for it.

    I don't see timekeeping and clocks as fundamental to the operation of the
    engine in the same was as memory and threads are, but maybe that's just
    perspective. In some sense, I'd see clocks and time as optional foreign
    functions that don't need to be spec'd. They can be provided by an embedder
    of the engine, like printing and IO, and don't really fundamentally alter
    the semantics of the state stored in the engine, namely the memory and the
    threads running.

    I'd propose that operations that cannot be reasonably implemented by the
    embedder outside the engine should be considered for opcode status. Along
    those lines, altering the size of memory and creating new threads that run
    inside the engine seem to fall into consideration for opcode status.

    —
    Reply to this email directly or view it on GitHub
    #362 (comment).

  9. AndrewScheidecker commented on Sep 21, 2015

    @AndrewScheidecker
    Author

    Why exactly would that be unwieldy? Why define a different kind of animal
    (i.e. an intrinsic function) that is somehow shaped differently than an
    opcode, but is just as mandatory, has a well-defined signature and
    well-defined semantics, etc?

    The advantage is that it uses the machinery already there to declare and call functions defined outside of the WebAssembly module with a verified signature. You don't have to define its syntax or its encoding, just a signature and semantics.

    I don't see timekeeping and clocks as fundamental to the operation of the
    engine in the same was as memory and threads are, but maybe that's just
    perspective. In some sense, I'd see clocks and time as optional foreign
    functions that don't need to be spec'd.

    It's true that clocks aren't coupled to the operation of the engine, but they're a pretty fundamental component of the execution environment. It would be tragic if WebAssembly didn't standardize them.

    I can understand not including I/O in the base standard, but even in that case I think it would be better to define a standard write_debug_output function similar to Windows OutputDebugString or JavaScript console.log.

  10. titzer commented on Sep 21, 2015

    @titzer

    On Mon, Sep 21, 2015 at 5:29 PM, Andrew Scheidecker <
    [email protected]> wrote:

    Why exactly would that be unwieldy? Why define a different kind of animal
    (i.e. an intrinsic function) that is somehow shaped differently than an
    opcode, but is just as mandatory, has a well-defined signature and
    well-defined semantics, etc?

    The advantage is that it uses the machinery already there to declare and
    call functions defined outside of the WebAssembly module with a verified
    signature. You don't have to define its syntax or its encoding, just a
    signature and semantics.

    Pick almost any data-only opcode and you could argue the same thing. They
    all have a signature in terms of local types. E.g. we discussed for a long
    time whether some floating point opcodes like f64_sqrt should be opcodes or
    not. There's no reason why would couldn't, e.g. make all the floating point
    opcodes into intrinsics.

    So what guiding principle do we use to select what's an opcode and what's
    an intrinsic function?

    I don't see timekeeping and clocks as fundamental to the operation of the
    engine in the same was as memory and threads are, but maybe that's just
    perspective. In some sense, I'd see clocks and time as optional foreign
    functions that don't need to be spec'd.

    It's true that clocks aren't coupled to the operation of the engine, but
    they're a pretty fundamental component of the execution environment. It
    would be tragic if WebAssembly didn't standardize them.

    I can understand not including I/O in the base standard, but even in that
    case I think it would be better to define a standard write_debug_output
    function similar to Windows OutputDebugString or JavaScript console.log.

    —
    Reply to this email directly or view it on GitHub
    #362 (comment).

  11. AndrewScheidecker commented on Sep 21, 2015

    @AndrewScheidecker
    Author

    Pick almost any data-only opcode and you could argue the same thing.

    It's possible to turn almost anything into calls, but there's a tradeoff:

    So what guiding principle do we use to select what's an opcode and what's
    an intrinsic function?

    I touched on this in my first post, but it's a tradeoff between encoding size and complexity. Opcodes will be more efficient in terms of encoded size and encode/decode speed (to the extent there aren't too many opcodes), and are simpler to match if you're doing something special for them in your code generator or some other AST transform. But I don't think that tradeoff is worthwhile for resize_memory, and maybe not for memory_size and page_size, though that's more questionable.

  12. jfbastien commented on Sep 21, 2015

    @jfbastien
    Member

    I tend to prefer representing basic capabilities as functions, or rather syscalls, and not as opcodes. I do think we want to have some nice namespacing, and it would be nice if it all looked like it came from the embedder. For reference, NaCl has a few syscalls.

    It's easy to express this in a header file, as a function with usual type signatures. I'm not sure how opcodes would be used from developer's code (inline asm?).

  13. sunfishcode commented on Sep 21, 2015

    @sunfishcode
    Member

    The criteria for what's a host function and what's not is can be pretty clear.

    What's missing here are criteria for what would distinguish an "intrinsic" from a builtin operation. The binary encoding has an opcode translation table, so there should be no encoding size or encode/decode speed advantage to making things intrinsics instead of opcodes.

    It's pretty easy to expose opcodes to developers, even without inline asm.

  14. kg commented on Sep 21, 2015

    @kg
    Contributor

    I don't think it makes sense for things like mmap or sqrt or resize_memory to be opcodes. It eats up valuable opcode index space and requires decoders, disassemblers, etc to implement them individually. If we make them functions we're eliminating most of those problems.

    What's the advantage to making them opcodes?

  15. sunfishcode commented on Sep 21, 2015

    @sunfishcode
    Member

    Because of the opcode translation table, these opcodes don't actually take up any opcode index space. Consumers will have to know their type, but this would be true of intrinsics too, since we'd want to verify that recognized intrinsics match their expected type signatures. Concerning specific functions named above:

    mmap: probably won't be an opcode, because the current thinking is leaning toward having finer-grained opcodes rather than doing everything through a single interface.

    sqrt: is an opcode, because it is a single fast instruction on most CPUs. Despite the fact that many programming language syntaxes use call syntax for it instead of giving it its own character, in practice it often serves as a basic floating point operation next to +, -, *, and /.

    resize_memory: is an opcode because it changes the semantics of subsequent load and store operations, which is pretty fundamental to wasm implementations.

  16. pjuftring commented on Sep 21, 2015

    @pjuftring
    Contributor

    I think, that every instruction, that can directly be converted to machine-code should be opcode and every instruction, that is OS/Engine-dependent should be an intrinsic-function.
    If I understand correctly how (jit-)compilers for wasm are supposed to work, then this seperation will eventually be made if you reuse the same wasm-compiler for different OS/engines of the same architecture: You only have to change the host-specific implementations and leave the architecture-specific stuff alone. If you make this seperation in the language you could have a nice symmetry there: The opcodes are implemented for each architecture; the intrinsics are implemented for each engine.

  17. kg commented on Sep 21, 2015

    @kg
    Contributor

    Because of the opcode translation table, these opcodes don't actually take up any opcode index space.

    This isn't true. If a module uses mmap or resize_memory once, those opcodes consume index space in the entire module. An smart encoder (as described in my proposals from a while back) can shift them to the back of the index space to minimize the impact, but this still matters if we end up with dozens of special opcodes.

    Consumers will have to know their type, but this would be true of intrinsics too, since we'd want to verify that recognized intrinsics match their expected type signatures.

    This is already part of the contract for imports, so I'm not sure what your point is here. My argument was that the opcode itself will have a specific layout, preventing generic decoding, while function calls and imports can be decoded generically. If we introduce a new opcode for every one of these operations, it will break decoding in older wasm implementations.

    Also: Making operations like mmap or resize_memory a function instead of an opcode opens up some opportunities for instrumentation and debugging tools, because it's possible to shim those by importing from a new module that forwards to the underlying implementation.

    mmap: probably won't be an opcode, because the current thinking is leaning toward having finer-grained opcodes rather than doing everything through a single interface.

    This seems to route around my question. Are you saying that it would be an opcode in the sense that we will have a dozen opcodes to do various page-related things?

    resize_memory: is an opcode because it changes the semantics of subsequent load and store operations, which is pretty fundamental to wasm implementations.

    A callee could invoke the resize_memory opcode though, so I don't see how you avoid any hard work by making it an opcode instead of an intrinsic function.

  18. sunfishcode commented on Sep 21, 2015

    @sunfishcode
    Member

    @pjuftring

    I think, that every instruction, that can directly be converted to machine-code should be opcode and every instruction, that is OS/Engine-dependent should be an intrinsic-function.

    It turns out that sandboxing performance is important enough that JITs can be expected to do architecture-specific things to implement it. OdinMonkey, for example, does different things on each hardware architecture, and the memory-resizing operation needs to be aware of this. Consequently, this distinction isn't as clear as it initially seems.

    @kg

    This isn't true. If a module uses mmap or resize_memory once, those opcodes consume index space in the entire module. An smart encoder (as described in my proposals from a while back) can shift them to the back of the index space to minimize the impact, but this still matters if we end up with dozens of special opcodes.

    As you say, lesser-used opcodes can be sorted to the back; this takes them out of the valuable low range. Consequently, even if we do have dozens of lesser-used opcodes, a modest producer can sort them to the back and the more-frequently-used opcodes will be unencumbered.

    This is already part of the contract for imports, so I'm not sure what your point is here. My argument was that the opcode itself will have a specific layout, preventing generic decoding, while function calls and imports can be decoded generically. If we introduce a new opcode for every one of these operations, it will break decoding in older wasm implementations.

    Some strategies for this are discussed here; there's more design work to be done here, but ultimately: we're surely going to want to add some new opcodes in the future, so we'll need a good solution for this regardless.

    Also: Making operations like mmap or resize_memory a function instead of an opcode opens up some opportunities for instrumentation and debugging tools, because it's possible to shim those by importing from a new module that forwards to the underlying implementation.

    map_file and resize_memory aren't the only things one can imagine wanting to shim. One might want to instrument loads, stores, or calls too. I am hoping we'll see ideas like these and these become well-supported ways to hook into WebAssembly for debugging, instrumentation, and numerous other purposes.

    This seems to route around my question. Are you saying that it would be an opcode in the sense that we will have a dozen opcodes to do various page-related things?

    This is FutureFeatures territory and not fully explored yet, but that is the current direction. I pointed it out to emphasize that we're aiming for the builtin operations to be simple primitives that each do one thing, rather than mmap which is fairly complex.

    A callee could invoke the resize_memory opcode though, so I don't see how you avoid any hard work by making it an opcode instead of an intrinsic function.

    Some embedders may restrict access to the linear memory so that code outside wasm can't access it directly ("If allowed by the module [...]"). In this case, the only way a memory resize can happen is through a resize_memory operator.

  19. jcbeyler commented on Sep 21, 2015

    @jcbeyler

    I was going to say that I thought putting sqrt into opcodes was a bad idea but then remember my GCC days and it was there and worked well there.

    I think it really depends, from what I understand it really will depend on what you want to do in the BE of the compiler. Both systems won't change that much but perhaps we could do testing at some point of binary sizes and performance of both systems.

    I feel that the intrinsics will lengthen a bit compile time but the opcodes will augment the binary size (though to be honest, you need to have the call for the intrinsic in place and that encoding might actually outweigh the opcode system).

    On the other hand, adding the opcodes means adding more code to the compilers with probably little gains compared to intrinsics if we are going to have that anyway. By writing these lines, I admit I'd lean more towards in putting them in intrinsics :)

  20. titzer commented on Sep 22, 2015

    @titzer

    Intrinsics vs opcodes ends up being a mess in almost every VM/language that
    I've seen, especially when intrinsics are introduced to work around a lack
    of extensibility in a binary format, e.g. Java bytecode. Then JITs get into
    the game of recognizing and swapping out certain library functions for
    inline code. In that situation it's really hard to see what is the "surface
    area" of direct support in the engine versus what is "user" or "library"
    code. I think we need to keep the line between what is in the engine and
    outside the engine very clear.

    Having two different mechanisms for specifying an engine-supplied operation
    is a burden on both producers and consumers, so an easy way to avoid that
    is that the engine has no intrinsic functions--i.e. all engine-supplied
    operations are bytecodes. Standard libraries, APIs, and embedders add
    functions, but the engine itself doesn't need to recognize any special ones.

    We'll need to have a solid story for extensible bytecode anyway, since we
    already envision SIMD, threads, and a GC'd heap as extensions beyond 1.0. I
    don't think that anyone is suggesting here that those extensions be
    expressed purely in terms of new intrinsic functions--some probably
    couldn't anyway.

    On Tue, Sep 22, 2015 at 12:19 AM, jcbeyler [email protected] wrote:

    I was going to say that I thought putting sqrt into opcodes was a bad idea
    but then remember my GCC days and it was there and worked well there.

    I think it really depends, from what I understand it really will depend on
    what you want to do in the BE of the compiler. Both systems won't change
    that much but perhaps we could do testing at some point of binary sizes and
    performance of both systems.

    I feel that the intrinsics will lengthen a bit compile time but the
    opcodes will augment the binary size (though to be honest, you need to have
    the call for the intrinsic in place and that encoding might actually
    outweigh the opcode system).

    On the other hand, adding the opcodes means adding more code to the
    compilers with probably little gains compared to intrinsics if we are going
    to have that anyway. By writing these lines, I admit I'd lean more towards
    in putting them in intrinsics :)

    —
    Reply to this email directly or view it on GitHub
    #362 (comment).

  21. titzer commented on Sep 22, 2015

    @titzer

    On Mon, Sep 21, 2015 at 10:23 PM, Patrick Uftring [email protected]
    wrote:

    I think, that every instruction, that can directly be converted to
    machine-code should be opcode and every instruction, that is
    OS/Engine-dependent should be an intrinsic-function.
    If I understand correctly how (jit-)compilers for wasm are supposed to
    work, then this seperation will eventually be made if you reuse the same
    wasm-compiler for different OS/engines of the same architecture: You only
    have to change the host-specific implementations and leave the
    architecture-specific stuff alone. If you make this seperation in the
    language you could have a nice symmetry there: The opcodes are implemented
    for each architecture; the intrinsics are implemented for each engine.

    Bounds checking loads and stores will likely be implemented quite
    differently on different engines, so I'm not sure this distinction makes
    sense. We wouldn't want loads and stores to be calls to intrinsic
    functions, because it'd be too slow. JITs can be considered a key part of a
    wasm engine and I'm not sure an engine-independent JIT would be the right
    boundary for which to design an interface.

    —
    Reply to this email directly or view it on GitHub
    #362 (comment).

  22. lukewagner commented on Sep 22, 2015

    @lukewagner
    Member

    I also don't see any advantage to segregating between "opcodes" and "builtin functions"; module-local index tables should entirely avoid any index space problems. So +1 for only having 1 thing, not 2.

  23. sunfishcode commented on Oct 9, 2015

    @sunfishcode
    Member

    I believe there's a sufficiently strong consensus here to avoid making a distinction between opcodes and intrinsic functions.

  24. AndrewScheidecker commented on Oct 12, 2015

    @AndrewScheidecker
    Author

    FYI there are some references to sbrk and dlopen being intrinsic functions in NonWeb you might want to change.

  25. sunfishcode commented on Oct 13, 2015

    @sunfishcode
    Member

    I created #405 to update those references. Thanks!

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions