Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WASM's V128 as Rust u128 in Func::wrap() ? #7004

Closed
cylewitruk opened this issue Sep 12, 2023 · 5 comments
Closed

Support WASM's V128 as Rust u128 in Func::wrap() ? #7004

cylewitruk opened this issue Sep 12, 2023 · 5 comments

Comments

@cylewitruk
Copy link

cylewitruk commented Sep 12, 2023

Thanks for filing a feature request! Please fill out the TODOs below.

Feature

Wondering why V128 isn't supported in Func::wrap()? Both Walrus and Wasmer seem to allow V128's to be used in host functions mapped as u128's. I know this wasn't the "intended" use for V128's, but it would sure be nice for host functions, at least in Rust :)

Benefit

Would allow passing of 128-bit numbers to/from host-functions without the need to split them into two parameters including lower and upper bits.

Implementation

Haven't looked into the Wasmtime internals, so no comment.

Alternatives

  • Passing two i64's low+high bits and bitwising them apart/together (fast, but boilerplate needed).
  • Using ExternRef's to pass u/i128's (slow)
  • Open to other suggestions...?
@alexcrichton
Copy link
Member

I've been meaning to dust off this branch for some time now!

@cylewitruk
Copy link
Author

Well look at that! I searched issues and PR's before opening this and didn't find anything, but looks like it was hiding in a branch :) That would be really awesome, actually. We're currently evaluating Wasmtime as the runtime for stacks.co VM runtime for its Clarity language. As all numbers are currently treated as 128-bits, this would definitely save us some headaches! :D

@alexcrichton
Copy link
Member

One thing to keep in mind perhaps is that v128 is generally intended for vector/simd operations right now so it may not be totally suitable for 128-bit numbers since there's nothing like v128.add which performs a 128-bit addition. You may get more mileage out of pairs of u64 depending on your use case.

Additionally most engines may not be that optimal at this time passing around v128 as parameters and results. The v128 type is generally thought of "you use this in your hot inner loop and that's it". One example is that Cranelift/Wasmtime follow System-V which means that on RISC-V for example all arguments/results are passed through memory rather than in registers.

Not to say that this isn't a good use case, just figured it might be worth giving a heads up! I personally think it'd be reasonable to improve these sorts of issues if they arise. If you're interested in running on other engines though it might be worth testing there too

@cylewitruk
Copy link
Author

cylewitruk commented Sep 15, 2023

Very informed input, and good information for anyone else who might be wondering the same thing!

In our case we're aware of the limitations and that v128 was meant primarily for simd operations - we're currently passing low/high u64 pairs back-and-forth, but this does incur a slight overhead in host functions instead of simply being able to, for example, u128::from_le_bytes(...).

@alexcrichton
Copy link
Member

Ok sounds good! I forgot to close this earlier but this was solved by #7010.

If you notice any performance issues though in this area (or others) please let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants