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:
@@ -237,10 +237,20 @@ impl ABIMachineSpec for X64ABIMachineSpec {
|
||||
extension: param.extension,
|
||||
});
|
||||
} else {
|
||||
// Compute size. Every arg takes a minimum slot of 8 bytes. (16-byte
|
||||
// stack alignment happens separately after all args.)
|
||||
// Compute size. For the wasmtime ABI it differs from native
|
||||
// ABIs in how multiple values are returned, so we take a
|
||||
// leaf out of arm64's book by not rounding everything up to
|
||||
// 8 bytes. For all ABI arguments, and other ABI returns,
|
||||
// though, each slot takes a minimum of 8 bytes.
|
||||
//
|
||||
// Note that in all cases 16-byte stack alignment happens
|
||||
// separately after all args.
|
||||
let size = (reg_ty.bits() / 8) as u64;
|
||||
let size = std::cmp::max(size, 8);
|
||||
let size = if args_or_rets == ArgsOrRets::Rets && call_conv.extends_wasmtime() {
|
||||
size
|
||||
} else {
|
||||
std::cmp::max(size, 8)
|
||||
};
|
||||
// Align.
|
||||
debug_assert!(size.is_power_of_two());
|
||||
next_stack = align_to(next_stack, size);
|
||||
@@ -824,15 +834,7 @@ impl From<StackAMode> for SyntheticAmode {
|
||||
}
|
||||
|
||||
fn get_intreg_for_arg(call_conv: &CallConv, idx: usize, arg_idx: usize) -> Option<Reg> {
|
||||
let is_fastcall = match call_conv {
|
||||
CallConv::Fast
|
||||
| CallConv::Cold
|
||||
| CallConv::SystemV
|
||||
| CallConv::BaldrdashSystemV
|
||||
| CallConv::Baldrdash2020 => false,
|
||||
CallConv::WindowsFastcall => true,
|
||||
_ => panic!("int args only supported for SysV or Fastcall calling convention"),
|
||||
};
|
||||
let is_fastcall = call_conv.extends_windows_fastcall();
|
||||
|
||||
// Fastcall counts by absolute argument number; SysV counts by argument of
|
||||
// this (integer) class.
|
||||
@@ -853,15 +855,7 @@ fn get_intreg_for_arg(call_conv: &CallConv, idx: usize, arg_idx: usize) -> Optio
|
||||
}
|
||||
|
||||
fn get_fltreg_for_arg(call_conv: &CallConv, idx: usize, arg_idx: usize) -> Option<Reg> {
|
||||
let is_fastcall = match call_conv {
|
||||
CallConv::Fast
|
||||
| CallConv::Cold
|
||||
| CallConv::SystemV
|
||||
| CallConv::BaldrdashSystemV
|
||||
| CallConv::Baldrdash2020 => false,
|
||||
CallConv::WindowsFastcall => true,
|
||||
_ => panic!("float args only supported for SysV or Fastcall calling convention"),
|
||||
};
|
||||
let is_fastcall = call_conv.extends_windows_fastcall();
|
||||
|
||||
// Fastcall counts by absolute argument number; SysV counts by argument of
|
||||
// this (floating-point) class.
|
||||
@@ -894,7 +888,10 @@ fn get_intreg_for_retval(
|
||||
1 => Some(regs::rdx()),
|
||||
_ => None,
|
||||
},
|
||||
CallConv::BaldrdashSystemV | CallConv::Baldrdash2020 => {
|
||||
CallConv::BaldrdashSystemV
|
||||
| CallConv::Baldrdash2020
|
||||
| CallConv::WasmtimeSystemV
|
||||
| CallConv::WasmtimeFastcall => {
|
||||
if intreg_idx == 0 && retval_idx == 0 {
|
||||
Some(regs::rax())
|
||||
} else {
|
||||
@@ -922,7 +919,10 @@ fn get_fltreg_for_retval(
|
||||
1 => Some(regs::xmm1()),
|
||||
_ => None,
|
||||
},
|
||||
CallConv::BaldrdashSystemV | CallConv::Baldrdash2020 => {
|
||||
CallConv::BaldrdashSystemV
|
||||
| CallConv::Baldrdash2020
|
||||
| CallConv::WasmtimeFastcall
|
||||
| CallConv::WasmtimeSystemV => {
|
||||
if fltreg_idx == 0 && retval_idx == 0 {
|
||||
Some(regs::xmm0())
|
||||
} else {
|
||||
@@ -992,12 +992,12 @@ fn get_callee_saves(call_conv: &CallConv, regs: &Set<Writable<RealReg>>) -> Vec<
|
||||
CallConv::BaldrdashWindows => {
|
||||
todo!("baldrdash windows");
|
||||
}
|
||||
CallConv::Fast | CallConv::Cold | CallConv::SystemV => regs
|
||||
CallConv::Fast | CallConv::Cold | CallConv::SystemV | CallConv::WasmtimeSystemV => regs
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|r| is_callee_save_systemv(r.to_reg()))
|
||||
.collect(),
|
||||
CallConv::WindowsFastcall => regs
|
||||
CallConv::WindowsFastcall | CallConv::WasmtimeFastcall => regs
|
||||
.iter()
|
||||
.cloned()
|
||||
.filter(|r| is_callee_save_fastcall(r.to_reg()))
|
||||
|
||||
Reference in New Issue
Block a user