Repository navigation
Description
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
titzer commentedon May 26, 2015
On Tue, May 26, 2015 at 6:03 PM, Dan Gohman [email protected]
wrote:
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.
lukewagner commentedon May 26, 2015
I had been sortof assuming a hybrid strategy, though I'd certainly like to discuss further:
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.
pizlonator commentedon May 26, 2015
+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
jfbastien commentedon May 26, 2015
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.
sunfishcode commentedon May 26, 2015
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.
jfbastien commentedon May 26, 2015
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.
sunfishcode commentedon May 26, 2015
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.
pizlonator commentedon May 26, 2015
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:
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
jfbastien commentedon May 26, 2015
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.
MikeHolman commentedon May 26, 2015
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.
titzer commentedon May 26, 2015
On Tue, May 26, 2015 at 7:14 PM, Dan Gohman [email protected]
wrote:
titzer commentedon May 26, 2015
On Tue, May 26, 2015 at 7:17 PM, JF Bastien [email protected]
wrote:
I vote for full 64-bit integers. Anything less would be short changing
WebAssembly producers, I think.
titzer commentedon May 26, 2015
On Tue, May 26, 2015 at 7:27 PM, JF Bastien [email protected]
wrote:
jfbastien commentedon Jun 9, 2015
I'm refactoring FutureFeatures.md, and deleting the following list on 64-bit heaps which should instead be in the bug:
size is also baked into the code in a hundred other ways (consider
offsetof).uintptr_t type was only used for local variable/expression types).
subtle bugs if the pointer was storing a real int64-width value.
still want a separate 32-bit build.
(conditionally loaded after a feature test)?
Refactor future features
Move 64-bit integer arithmetic to MVP.
jfbastien commentedon Jun 10, 2015
@lukewagner / @sunfishcode / @pizlonator / @MikeHolman / @titzer : I opened #135 to move 64-bit integer arithmetic (not heaps) to MVP, based on the discussion here.
sunfishcode commentedon Jun 10, 2015
Looks like discussion in this issue is done; #135 has all the action now.