tweag / asterius

DEPRECATED in favor of ghc wasm backend, see https://www.tweag.io/blog/2022-11-22-wasm-backend-merged-in-ghc
1.98k stars 57 forks source link

Transparent & uniform handling of i64 FFI #33

Open TerrorJack opened 6 years ago

TerrorJack commented 6 years ago

Currently, WebAssembly as in spec/V8 doesn't support passing i64 across WebAssembly/JavaScript boundary, and the i64 via BigInt proposal isn't landing anytime soon. Before we properly generate 32-bit code (which is going to be a standalone issue later), we still need to polyfill this feature.

Previously, there are multiple different workarounds co-existing in asterius:

For more reliable code generation, i64 FFI should be handled in a transparent & uniform manner:

TerrorJack commented 6 years ago

The i64 to i32 lowering hacks which either passes two parameters or uses a global for higher 32-bits are overly complicated for our use cases. Simpler solution: pass raw bit patterns in f64 instead, so we won't lose higher 32 bits, nor do we need to rely on any global state for function calls.

TerrorJack commented 6 years ago

We now pass i64 as f64 for FFI. Closing this issue for now, since fully transparent handling based on a Module -> Module rewriting is another pile of work and should be scheduled later.

TerrorJack commented 5 years ago

https://bugs.chromium.org/p/v8/issues/detail?id=7741#c8

Finally, V8 guys have made the move!

We won't remove the current hacks immediately since it takes some time for V8's changes to propagate to node.js/chrome, but still worth celebrating.

TerrorJack commented 4 years ago

Revisiting this issue after one year:

--experimental-wasm-bigint landed in V8 but is still off by default. For SpiderMonkey it seems to be a work in progress. So other than "debug mode" we still need to assume the absence of wasm bigint integration for now.

We need this asap for #358. The i64 hash values aren't safe to be assumed to stay within Number.MAX_SAFE_INTEGER range. While we can risk a reinterpret cast or use a global buffer for casting, the real solution IMO is fixing this old issue, and use vanilla i64 parameters & return values in our imported & exported functions.

Reopening this issue since it's relevant again today.

TerrorJack commented 4 years ago

The C/C++ and Rust folks should face a similar challenge. The existing solution seems to be:

I have yet to find some clang compiled wasi target executables for further investigation. Anyway we should make our solution close to the existing ABIs, to prepare for future introduction of the clang toolchain and cbits support.