Fully support multiple returns in Wasmtime (#2806)
* Fully support multiple returns in Wasmtime For quite some time now Wasmtime has "supported" multiple return values, but only in the mose bare bones ways. Up until recently you couldn't get a typed version of functions with multiple return values, and never have you been able to use `Func::wrap` with functions that return multiple values. Even recently where `Func::typed` can call functions that return multiple values it uses a double-indirection by calling a trampoline which calls the real function. The underlying reason for this lack of support is that cranelift's ABI for returning multiple values is not possible to write in Rust. For example if a wasm function returns two `i32` values there is no Rust (or C!) function you can write to correspond to that. This commit, however fixes that. This commit adds two new ABIs to Cranelift: `WasmtimeSystemV` and `WasmtimeFastcall`. The intention is that these Wasmtime-specific ABIs match their corresponding ABI (e.g. `SystemV` or `WindowsFastcall`) for everything *except* how multiple values are returned. For multiple return values we simply define our own version of the ABI which Wasmtime implements, which is that for N return values the first is returned as if the function only returned that and the latter N-1 return values are returned via an out-ptr that's the last parameter to the function. These custom ABIs provides the ability for Wasmtime to bind these in Rust meaning that `Func::wrap` can now wrap functions that return multiple values and `Func::typed` no longer uses trampolines when calling functions that return multiple values. Although there's lots of internal changes there's no actual changes in the API surface area of Wasmtime, just a few more impls of more public traits which means that more types are supported in more places! Another change made with this PR is a consolidation of how the ABI of each function in a wasm module is selected. The native `SystemV` ABI, for example, is more efficient at returning multiple values than the wasmtime version of the ABI (since more things are in more registers). To continue to take advantage of this Wasmtime will now classify some functions in a wasm module with the "fast" ABI. Only functions that are not reachable externally from the module are classified with the fast ABI (e.g. those not exported, used in tables, or used with `ref.func`). This should enable purely internal functions of modules to have a faster calling convention than those which might be exposed to Wasmtime itself. Closes #1178 * Tweak some names and add docs * "fix" lightbeam compile * Fix TODO with dummy environ * Unwind info is a property of the target, not the ABI * Remove lightbeam unused imports * Attempt to fix arm64 * Document new ABIs aren't stable * Fix filetests to use the right target * Don't always do 64-bit stores with cranelift This was overwriting upper bits when 32-bit registers were being stored into return values, so fix the code inline to do a sized store instead of one-size-fits-all store. * At least get tests passing on the old backend * Fix a typo * Add some filetests with mixed abi calls * Get `multi` example working * Fix doctests on old x86 backend * Add a mixture of wasmtime/system_v tests
This commit is contained in:
@@ -197,18 +197,26 @@ impl ABIMachineSpec for AArch64MachineDeps {
|
||||
next_stack = 16;
|
||||
}
|
||||
|
||||
// Note on return values: on the regular non-baldrdash ABI, we may return values in 8
|
||||
// registers for V128 and I64 registers independently of the number of register values
|
||||
// returned in the other class. That is, we can return values in up to 8 integer and 8
|
||||
// vector registers at once.
|
||||
// In Baldrdash, we can only use one register for return value for all the register
|
||||
// classes. That is, we can't return values in both one integer and one vector register;
|
||||
// only one return value may be in a register.
|
||||
let (max_per_class_reg_vals, mut remaining_reg_vals) = match args_or_rets {
|
||||
ArgsOrRets::Args => (8, 16), // x0-x7 and v0-v7
|
||||
|
||||
let (max_per_class_reg_vals, mut remaining_reg_vals) = match (args_or_rets, is_baldrdash) {
|
||||
(ArgsOrRets::Args, _) => (8, 16), // x0-x7 and v0-v7
|
||||
(ArgsOrRets::Rets, false) => (8, 16), // x0-x7 and v0-v7
|
||||
(ArgsOrRets::Rets, true) => (1, 1), // x0 or v0, but not both
|
||||
// Note on return values: on the regular ABI, we may return values
|
||||
// in 8 registers for V128 and I64 registers independently of the
|
||||
// number of register values returned in the other class. That is,
|
||||
// we can return values in up to 8 integer and
|
||||
// 8 vector registers at once.
|
||||
//
|
||||
// In Baldrdash and Wasmtime, we can only use one register for
|
||||
// return value for all the register classes. That is, we can't
|
||||
// return values in both one integer and one vector register; only
|
||||
// one return value may be in a register.
|
||||
ArgsOrRets::Rets => {
|
||||
if is_baldrdash || call_conv.extends_wasmtime() {
|
||||
(1, 1) // x0 or v0, but not both
|
||||
} else {
|
||||
(8, 16) // x0-x7 and v0-v7
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for i in 0..params.len() {
|
||||
@@ -282,15 +290,18 @@ impl ABIMachineSpec for AArch64MachineDeps {
|
||||
// Compute the stack slot's size.
|
||||
let size = (ty_bits(param.value_type) / 8) as u64;
|
||||
|
||||
let size = if call_conv != isa::CallConv::AppleAarch64 {
|
||||
let size = if call_conv == isa::CallConv::AppleAarch64
|
||||
|| (call_conv.extends_wasmtime() && args_or_rets == ArgsOrRets::Rets)
|
||||
{
|
||||
// MacOS aarch64 and Wasmtime allow stack slots with
|
||||
// sizes less than 8 bytes. They still need to be
|
||||
// properly aligned on their natural data alignment,
|
||||
// though.
|
||||
size
|
||||
} else {
|
||||
// Every arg takes a minimum slot of 8 bytes. (16-byte stack
|
||||
// alignment happens separately after all args.)
|
||||
std::cmp::max(size, 8)
|
||||
} else {
|
||||
// MacOS aarch64 allows stack slots with sizes less than 8
|
||||
// bytes. They still need to be properly aligned on their
|
||||
// natural data alignment, though.
|
||||
size
|
||||
};
|
||||
|
||||
// Align the stack slot.
|
||||
|
||||
Reference in New Issue
Block a user