Skip to content

How should 64-bit integers look? #81

Closed

Description

@sunfishcode

64-bit integers are an "essential post-v1 feature", so we don't need to figure them out right now, but there are some areas where it would help to anticipate them. While thinking about one of @jfbastien's comments in #79, I thought of two major categories of possibilities.

Option A: Explicit 64-bit versus 32-bit WebAssembly

Make WebAssembly code either explicitly 64-bit or 32-bit. In 64-bit wasm, you'd only get 64-bit integers. In 32-bit wasm, you'd only get 32-bit integers. This would simplify a bunch of things.

It's unfortunate to lose "just one WebAssembly", but things were going to get a little weird with 32-bit indices into >4GiB heaps anyway. And C/C++ code is always going to be incompatible between 32-bit and 64-bit. So perhaps this loss isn't as big as it might seem.

32-bit still wants access to 64-bit integers, but an int32-pair approach with special intrinsics to perform add, mul, etc. would be simple and would recover some, though not all, of the benefit.

On x86 at least, 32-bit operations are sometimes smaller and sometimes faster than their 64-bit counterparts, so simple execution engines would loose those optimizations. Execution engines willing to construct SSA form and run a pass could probably recover these optimizations in many cases though. Or optionally, we could introduce explicitly 32-bit operations that operate on 64-bit registers much like x64 and AArch64 have, and then we wouldn't need any cleverness.

This option would also make it harder, though not impossible, to support 64-bit code on 32-bit platforms.

If we end up using ELF containers (#74), this would fit pretty naturally into the division between 32-bit ELF and 64-bit ELF.

Option B: New local type(s)

Introduce i64 as a new local type. We need instructions to convert between integer types, we need load and store to accept either 32-bit or 64-bit indices (when we have >4GiB heaps), load and store would need to be able to extend to and truncate from either 32-bit or 64-bit, execution engines need to support all the arithmetic operations on both 32-bit and 64-bit, and so on. And, it means we can't do register coloring between i32 variables and i64 variables at the wasm level.

If we do this, then some of the reasons for not doing i8 and i16 local types would be diminished as well. Those types are still not highly valuable for arithmetic, so we still may not want them, but if we're going to have multiple integer types, having a few more isn't as much of a burden. And if we added them, it'd likely mean we could get rid of extending loads and truncating stores, because we could just use type conversions instead (at the cost of making execution engines pattern-match these, since extending loads and truncating stores are usually single-instruction operations).

The loss of coloring between i8, i16, and i32 variables would probably be more significant than between i32 and i64 though.

Activity

  1. titzer commented on May 26, 2015

    @titzer

    On Tue, May 26, 2015 at 6:03 PM, Dan Gohman [email protected]
    wrote:

    64-bit integers are an "essential post-v1 feature", so we don't need to
    figure them out right now, but there are some areas where it would help to
    anticipate them. While thinking about one of @jfbastien
    https://github.com/jfbastien's comments in #79
    WebAssembly/spec#79, I thought of two major
    categories of possibilities.
    Option A: Explicit 64-bit versus 32-bit WebAssembly

    Make WebAssembly code either explicitly 64-bit or 32-bit. In 64-bit wasm,
    you'd only get 64-bit integers. In 32-bit wasm, you'd only get 32-bit
    integers. This would simplify a bunch of things.

    It's unfortunate to lose "just one WebAssembly", but things were going to
    get a little weird with 32-bit indices into >4GiB heaps anyway. And C/C++
    code is always going to be incompatible between 32-bit and 64-bit. So
    perhaps this loss isn't as big as it might seem.

    32-bit still wants access to 64-bit integers, but an int32-pair approach
    with special intrinsics to perform add, mul, etc. would be simple and would
    recover some, though not all, of the benefit.

    On x86 at least, 32-bit operations are sometimes smaller and sometimes
    faster than their 64-bit counterparts, so simple execution engines would
    loose those optimizations. Execution engines willing to construct SSA form
    and run a pass could probably recover these optimizations in many cases
    though. Or optionally, we could introduce explicitly 32-bit operations that
    operate on 64-bit registers much like x64 and AArch64 have, and then we
    wouldn't need any cleverness.

    This option would also make it harder, though not impossible, to support
    64-bit code on 32-bit platforms.

    If we end up using ELF containers (#74
    WebAssembly/spec#74), this would fit pretty
    naturally into the division between 32-bit ELF and 64-bit ELF.
    Option B: New local type(s)

    Introduce i64 as a new local type. We need instructions to convert between
    integer types, we need load and store to accept either 32-bit or 64-bit
    indices (when we have >4GiB heaps), load and store would need to be able to
    extend to and truncate from either 32-bit or 64-bit, execution engines need
    to support all the arithmetic operations on both 32-bit and 64-bit, and so
    on. And, it means we can't do register coloring between i32 variables and
    i64 variables at the wasm level.

    If we do this, then some of the reasons for not doing i8 and i16 local
    types would be diminished as well. Those types are still not highly
    valuable for arithmetic, so we still may not want them, but if we're going
    to have multiple integer types, having a few more isn't as much of a
    burden. And if we added them, it'd likely mean we could get rid of
    extending loads and truncating stores, because we could just use type
    conversions instead (at the cost of making execution engines pattern-match
    these, since extending loads and truncating stores are usually
    single-instruction operations).

    The loss of coloring between i8, i16, and i32 variables would probably be
    more significant than between i32 and i64 though.

    There seems to be real pressure to have 64-bit integers and arithmetic even
    if the heap is 32 bits. E.g. Unity in particular mentioned that emulation
    of 64-bit arithmetic in asm.js is really really slow. Compiling other
    languages that have 64-bit integers will face similar problems. Emulating
    64-bit arithmetic with 32-bit arithmetic totally sucks and we shouldn't
    burden WebAssembly producers with it; the engine should handle and optimize
    it.

    I personally find Option B the only viable solution. I think that's
    orthogonal to whether i8 and i16 are available as local types. Leaving them
    out of local types means we only have to deal with them at memory accesses
    as now. Otherwise we could add 8 and 16 bit arithmetic.

    There is a third option C, which would be to make all the integer
    arithmetic operations work on integers of all sizes (8, 16, 32, 64) and of
    both signedness and then define appropriate implicit conversions that are
    associated with arithmetic on different integer sizes. I went really far
    down this road in other work on Virgil, even allowing arbitrary sub-word
    sizes such as i17, etc. It can totally work, but there be dragons here,
    especially when mixing signs; making this fully general comes with the
    burden of some tricky rules and some weird implicit sign and zero
    extensions.

    —
    Reply to this email directly or view it on GitHub
    WebAssembly/spec#81.

  2. lukewagner commented on May 26, 2015

    @lukewagner
    Member

    I had been sortof assuming a hybrid strategy, though I'd certainly like to discuss further:

    • int64 is added as a new type along with corresponding int64 arithmetic which are always available
    • a wasm module has a global 32/64 mode, but it only affects heap ops
      • in 32-bit mode, load/store pointer operands take int32, on 64-bit mode, they take int64; the mode is determined by the module's declaration, not hardware, so this is not polymorphic
      • additionally, imports/linking would specify whether the loaded module was assumed to be 32/64 to catch ABI-mismatch errors early.
      • 64-bit hardware could efficiently run 32/64-mode, but (and this is debatable, but simplifies things), 64-bit mode modules would throw if loaded on 32-bit (we'd make this feature-detectable)

    My vague assumption is that only allowing loads/stores to operate on word-sized integers will simplify the implementation from having to deal with, e.g., two different cases when reasoning about wrap-around.

  3. pizlonator commented on May 26, 2015

    @pizlonator
    Contributor

    +1 for option B.

    It doesn't bother me that we don't also create var types for 8 bit and 16 bit. Doing 32 bit math to emulate 8 bit math is a lot less crappy then doing 32 bit math to emulate 64 bit math.

    -Fil

    On May 26, 2015, at 9:33 AM, titzer [email protected] wrote:

    On Tue, May 26, 2015 at 6:03 PM, Dan Gohman [email protected]
    wrote:

    64-bit integers are an "essential post-v1 feature", so we don't need to
    figure them out right now, but there are some areas where it would help to
    anticipate them. While thinking about one of @jfbastien
    https://github.com/jfbastien's comments in #79
    WebAssembly/spec#79, I thought of two major
    categories of possibilities.
    Option A: Explicit 64-bit versus 32-bit WebAssembly

    Make WebAssembly code either explicitly 64-bit or 32-bit. In 64-bit wasm,
    you'd only get 64-bit integers. In 32-bit wasm, you'd only get 32-bit
    integers. This would simplify a bunch of things.

    It's unfortunate to lose "just one WebAssembly", but things were going to
    get a little weird with 32-bit indices into >4GiB heaps anyway. And C/C++
    code is always going to be incompatible between 32-bit and 64-bit. So
    perhaps this loss isn't as big as it might seem.

    32-bit still wants access to 64-bit integers, but an int32-pair approach
    with special intrinsics to perform add, mul, etc. would be simple and would
    recover some, though not all, of the benefit.

    On x86 at least, 32-bit operations are sometimes smaller and sometimes
    faster than their 64-bit counterparts, so simple execution engines would
    loose those optimizations. Execution engines willing to construct SSA form
    and run a pass could probably recover these optimizations in many cases
    though. Or optionally, we could introduce explicitly 32-bit operations that
    operate on 64-bit registers much like x64 and AArch64 have, and then we
    wouldn't need any cleverness.

    This option would also make it harder, though not impossible, to support
    64-bit code on 32-bit platforms.

    If we end up using ELF containers (#74
    WebAssembly/spec#74), this would fit pretty
    naturally into the division between 32-bit ELF and 64-bit ELF.
    Option B: New local type(s)

    Introduce i64 as a new local type. We need instructions to convert between
    integer types, we need load and store to accept either 32-bit or 64-bit
    indices (when we have >4GiB heaps), load and store would need to be able to
    extend to and truncate from either 32-bit or 64-bit, execution engines need
    to support all the arithmetic operations on both 32-bit and 64-bit, and so
    on. And, it means we can't do register coloring between i32 variables and
    i64 variables at the wasm level.

    If we do this, then some of the reasons for not doing i8 and i16 local
    types would be diminished as well. Those types are still not highly
    valuable for arithmetic, so we still may not want them, but if we're going
    to have multiple integer types, having a few more isn't as much of a
    burden. And if we added them, it'd likely mean we could get rid of
    extending loads and truncating stores, because we could just use type
    conversions instead (at the cost of making execution engines pattern-match
    these, since extending loads and truncating stores are usually
    single-instruction operations).

    The loss of coloring between i8, i16, and i32 variables would probably be
    more significant than between i32 and i64 though.

    There seems to be real pressure to have 64-bit integers and arithmetic even
    if the heap is 32 bits. E.g. Unity in particular mentioned that emulation
    of 64-bit arithmetic in asm.js is really really slow. Compiling other
    languages that have 64-bit integers will face similar problems. Emulating
    64-bit arithmetic with 32-bit arithmetic totally sucks and we shouldn't
    burden WebAssembly producers with it; the engine should handle and optimize
    it.

    I personally find Option B the only viable solution. I think that's
    orthogonal to whether i8 and i16 are available as local types. Leaving them
    out of local types means we only have to deal with them at memory accesses
    as now. Otherwise we could add 8 and 16 bit arithmetic.

    There is a third option C, which would be to make all the integer
    arithmetic operations work on integers of all sizes (8, 16, 32, 64) and of
    both signedness and then define appropriate implicit conversions that are
    associated with arithmetic on different integer sizes. I went really far
    down this road in other work on Virgil, even allowing arbitrary sub-word
    sizes such as i17, etc. It can totally work, but there be dragons here,
    especially when mixing signs; making this fully general comes with the
    burden of some tricky rules and some weird implicit sign and zero
    extensions.

    ―
    Reply to this email directly or view it on GitHub
    WebAssembly/spec#81.

    ―
    Reply to this email directly or view it on GitHub.

  4. jfbastien commented on May 26, 2015

    @jfbastien
    Member

    I have a strong preference for what @lukewagner outlined. It's what we've been planning for PNaCl: support all integer arithmetic on 8/16/32/64, and explicit 32 or 64 heap.

    So far we haven't found a compelling enough reason to warrant implementing a 64-bit sandbox on the web, but applications such as photo/video editors or games are obvious fits for large heaps.

  5. sunfishcode commented on May 26, 2015

    @sunfishcode
    MemberAuthor

    Emulating 64-bit integers would be a lot less painful if we just added add-with-carry and umulh/smulh intrinsics, or perhaps even full-on (lo, hi) = add(lhs_lo, lhs_hi, rhs_lo, rhs_hi) and friends. I'm bringing this up because having only a single integer type at a time would simplify a bunch of things, though I'm not attached to it.

  6. jfbastien commented on May 26, 2015

    @jfbastien
    Member

    I'd rather have native 64-bit arithmetic support rather than emulating with (lo, hi) = add(lhs_lo, lhs_hi, rhs_lo, rhs_hi). IMO emulation means that JS polyfill details are bleeding into the implementation of wasm, which I think we should avoid.

    I'm not opposed to add-with-carry and umulh/smulh, especially if they provide a nice evolutionary path toward native 64-bit arithmetic support without compromising wasm's long-term feature set.

  7. sunfishcode commented on May 26, 2015

    @sunfishcode
    MemberAuthor

    I wasn't even thinking about the JS polyfill here. A platform with a single integer type has a remarkable simplicity that's interesting to think about, and potentially even worth making some sacrifices for.

    i8/i16 arithmetic doesn't really justify itself. I was just thinking that once we have i32 versus i64, we have to do a lot of the things that i8 and i16 would need anyway, and then it might just be nice to eliminate inconsistency between i8 and i16 on one hand and i32 and i64 on the other.

  8. pizlonator commented on May 26, 2015

    @pizlonator
    Contributor

    That’s a good point.

    The issue I have with this is pattern matching 64-bit math from either an emulated 64-bit math using add-with-carry and friends, or pattern matching it from (lo,hi)=math(lo,hi,lo,hi). This has issues:

    • It sucks at the ABI boundaries. If you pass two 32-bit ints, or return two 32-bit ints, then it’s not clear that you could always turn this into a single 64-bit int that you pass through an argument register or return in a return register.
    • Add-with-carry and friends feel fragile for pattern matching.
    • Having operations that return two values makes the IR more complex. (I don’t know if we’ve considered this specific question yet. Seems like an AST IR needs to have types for any value that an AST node can return, so returning a (lo,hi) pair requires at least as much effort from the spec and implementer as returning a 64-bit integer.)

    So, I prefer just adding native 64-bit integers, because it’s no more complex than add-with-carry/lo,hi and it has a better chance of actually achievin the goal of fast 64-bit math on all targets.

    That being said, I think that add-with-carry and friends are obviously great candidates for addition in future versions.

    -Filip

    On May 26, 2015, at 10:14 AM, Dan Gohman [email protected] wrote:

    Emulating 64-bit integers would be a lot less painful if we just added add-with-carry and umulh/smulh intrinsics, or perhaps even full-on (lo, hi) = add(lhs_lo, lhs_hi, rhs_lo, rhs_hi) and friends. I'm bringing this up because having only a single integer type at a time would simplify a bunch of things, though I'm not attached to it.

    —
    Reply to this email directly or view it on GitHub WebAssembly/spec#81 (comment).

  9. jfbastien commented on May 26, 2015

    @jfbastien
    Member

    To be explicit: maybe we want to have 64-bit arithmetic in V1. IIUC @pizlonator's point is that breaking up 64-bit arithmetic to 32-bit is way easier than doing the reverse in an ad-hoc way, and removes implementation warts we'd be stuck with for a long time.

  10. MikeHolman commented on May 26, 2015

    @MikeHolman
    Member

    I had also been thinking along the lines of what @lukewagner suggested. I assumed int64 was post-V1 due to difficulty in having a polyfill (and I think for V1 the polyfill should be an important consideration as long as it doesn't take us down some less than optimal path of no return).

    If the polyfill isn't a blocker for 64-bit arithmetic, then I am fine with having it in V1. Though I still want to leave 64-bit heaps for post-V1.

  11. titzer commented on May 26, 2015

    @titzer

    On Tue, May 26, 2015 at 7:14 PM, Dan Gohman [email protected]
    wrote:

    Emulating 64-bit integers would be a lot less painful if we just added
    add-with-carry and umulh/smulh intrinsics, or perhaps even full-on (lo, hi)
    = add(lhs_lo, lhs_hi, rhs_lo, rhs_hi) and friends. I'm bringing this up
    because having only a single integer type at a time would simplify a bunch
    of things, though I'm not attached to it.

    Add-with-carry and other status flags are really painful on architectures
    without, e.g. MIPS. While there is something to be said for some neat
    tricks on architectures that have them, I'm not sure that's the right level
    of abstraction and whether anything other than LLVM's complex instruction
    selector could make use of them.

    —
    Reply to this email directly or view it on GitHub
    WebAssembly/spec#81 (comment).

  12. titzer commented on May 26, 2015

    @titzer

    On Tue, May 26, 2015 at 7:17 PM, JF Bastien [email protected]
    wrote:

    I'd rather have native 64-bit arithmetic support rather than emulating
    with (lo, hi) = add(lhs_lo, lhs_hi, rhs_lo, rhs_hi). IMO emulation means
    that JS polyfill details are bleeding into the implementation of wasm,
    which I think we should avoid.

    Yeah, especially that emulating a 64-bit divide with 32-bit divisions is
    about half a page of code, and there's little hope of pattern matching
    that. So it'd have to be an intrinsic. And even easy cases such as 64/32 is
    way more efficient on 32-bits, but only adding that would be weird.

    I vote for full 64-bit integers. Anything less would be short changing
    WebAssembly producers, I think.

    I'm not opposed to add-with-carry and umulh/smulh, especially if they
    provide a nice evolutionary path toward native 64-bit arithmetic support
    without compromising wasm's long-term feature set.

    —
    Reply to this email directly or view it on GitHub
    WebAssembly/spec#81 (comment).

  13. titzer commented on May 26, 2015

    @titzer

    On Tue, May 26, 2015 at 7:27 PM, JF Bastien [email protected]
    wrote:

    To be explicit: maybe we want to have 64-bit arithmetic in V1. IIUC
    @pizlonator https://github.com/pizlonator's point is that breaking up
    64-bit arithmetic to 32-bit is way easier than doing the reverse in an
    ad-hoc way, and removes implementation warts we'd be stuck with for a long
    time.

    • 1

    —
    Reply to this email directly or view it on GitHub
    WebAssembly/spec#81 (comment).

  14. jfbastien commented on Jun 9, 2015

    @jfbastien
    Member

    I'm refactoring FutureFeatures.md, and deleting the following list on 64-bit heaps which should instead be in the bug:

    • Should we provide a uintptr_t (only 64-bit on 64-bit systems)?
      • This feature alone would not allow a C++ compiler to write size-polymorphic code since the word
        size is also baked into the code in a hundred other ways (consider offsetof).
      • The compiler could inflate all pointer types that are used in heap storage to 64-bit (so the
        uintptr_t type was only used for local variable/expression types).
        • This would imply an implicit truncation of any load of a pointer from the heap which could cause
          subtle bugs if the pointer was storing a real int64-width value.
        • This would still unnecessarily increase heap size on 32-bit; applications sensitive to OOM would
          still want a separate 32-bit build.
        • Now there are three compile targets: all-32, all-64, and this uintptr_t hybrid.
      • More discussion and experimentation needed.
        • Would the hybrid mostly Just Work?
        • Are there users who would want to ship a hybrid build instead of two 32- and 64-bit builds
          (conditionally loaded after a feature test)?
  15. jfbastien commented on Jun 10, 2015

    @jfbastien
    Member

    @lukewagner / @sunfishcode / @pizlonator / @MikeHolman / @titzer : I opened #135 to move 64-bit integer arithmetic (not heaps) to MVP, based on the discussion here.

  16. sunfishcode commented on Jun 10, 2015

    @sunfishcode
    MemberAuthor

    Looks like discussion in this issue is done; #135 has all the action now.

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