* 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
218 lines
6.9 KiB
Rust
218 lines
6.9 KiB
Rust
#![allow(missing_docs)]
|
|
|
|
use crate::code_memory::CodeMemory;
|
|
use crate::instantiate::SetupError;
|
|
use cranelift_codegen::ir::InstBuilder;
|
|
use cranelift_codegen::isa::TargetIsa;
|
|
use wasmtime_environ::{CompileError, CompiledFunction, Relocation, RelocationTarget};
|
|
use wasmtime_runtime::{InstantiationError, VMFunctionBody, VMTrampoline};
|
|
|
|
pub mod ir {
|
|
pub(super) use cranelift_codegen::ir::{
|
|
AbiParam, ConstantOffset, JumpTable, Signature, SourceLoc,
|
|
};
|
|
pub use cranelift_codegen::ir::{
|
|
ExternalName, Function, InstBuilder, MemFlags, StackSlotData, StackSlotKind,
|
|
};
|
|
}
|
|
pub use cranelift_codegen::print_errors::pretty_error;
|
|
pub use cranelift_codegen::Context;
|
|
pub use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
|
|
|
pub mod binemit {
|
|
pub use cranelift_codegen::binemit::NullTrapSink;
|
|
pub(super) use cranelift_codegen::binemit::{Addend, Reloc, RelocSink};
|
|
pub use cranelift_codegen::binemit::{CodeOffset, NullStackMapSink, TrapSink};
|
|
}
|
|
|
|
/// Create a trampoline for invoking a function.
|
|
pub fn make_trampoline(
|
|
isa: &dyn TargetIsa,
|
|
code_memory: &mut CodeMemory,
|
|
fn_builder_ctx: &mut FunctionBuilderContext,
|
|
signature: &ir::Signature,
|
|
value_size: usize,
|
|
) -> Result<VMTrampoline, SetupError> {
|
|
let compiled_function = build_trampoline(isa, fn_builder_ctx, signature, value_size)?;
|
|
|
|
assert!(compiled_function.relocations.is_empty());
|
|
let ptr = code_memory
|
|
.allocate_for_function(&compiled_function)
|
|
.map_err(|message| {
|
|
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(message)))
|
|
})?
|
|
.as_ptr();
|
|
Ok(unsafe { std::mem::transmute::<*const VMFunctionBody, VMTrampoline>(ptr) })
|
|
}
|
|
|
|
pub(crate) fn build_trampoline(
|
|
isa: &dyn TargetIsa,
|
|
fn_builder_ctx: &mut FunctionBuilderContext,
|
|
signature: &ir::Signature,
|
|
value_size: usize,
|
|
) -> Result<CompiledFunction, SetupError> {
|
|
let pointer_type = isa.pointer_type();
|
|
let mut wrapper_sig =
|
|
wasmtime_cranelift::blank_sig(isa, wasmtime_cranelift::wasmtime_call_conv(isa));
|
|
|
|
// Add the `callee_address` parameter.
|
|
wrapper_sig.params.push(ir::AbiParam::new(pointer_type));
|
|
|
|
// Add the `values_vec` parameter.
|
|
wrapper_sig.params.push(ir::AbiParam::new(pointer_type));
|
|
|
|
let mut context = Context::new();
|
|
context.func = ir::Function::with_name_signature(ir::ExternalName::user(0, 0), wrapper_sig);
|
|
|
|
{
|
|
let mut builder = FunctionBuilder::new(&mut context.func, fn_builder_ctx);
|
|
let block0 = builder.create_block();
|
|
|
|
builder.append_block_params_for_function_params(block0);
|
|
builder.switch_to_block(block0);
|
|
builder.seal_block(block0);
|
|
|
|
let (vmctx_ptr_val, caller_vmctx_ptr_val, callee_value, values_vec_ptr_val) = {
|
|
let params = builder.func.dfg.block_params(block0);
|
|
(params[0], params[1], params[2], params[3])
|
|
};
|
|
|
|
// Load the argument values out of `values_vec`.
|
|
let mflags = ir::MemFlags::trusted();
|
|
let callee_args = signature
|
|
.params
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, r)| {
|
|
match i {
|
|
0 => vmctx_ptr_val,
|
|
1 => caller_vmctx_ptr_val,
|
|
_ =>
|
|
// i - 2 because vmctx and caller vmctx aren't passed through `values_vec`.
|
|
{
|
|
builder.ins().load(
|
|
r.value_type,
|
|
mflags,
|
|
values_vec_ptr_val,
|
|
((i - 2) * value_size) as i32,
|
|
)
|
|
}
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
|
|
let new_sig = builder.import_signature(signature.clone());
|
|
|
|
let call = builder
|
|
.ins()
|
|
.call_indirect(new_sig, callee_value, &callee_args);
|
|
|
|
let results = builder.func.dfg.inst_results(call).to_vec();
|
|
|
|
// Store the return values into `values_vec`.
|
|
let mflags = ir::MemFlags::trusted();
|
|
for (i, r) in results.iter().enumerate() {
|
|
builder
|
|
.ins()
|
|
.store(mflags, *r, values_vec_ptr_val, (i * value_size) as i32);
|
|
}
|
|
|
|
builder.ins().return_(&[]);
|
|
builder.finalize()
|
|
}
|
|
|
|
let mut code_buf = Vec::new();
|
|
let mut reloc_sink = TrampolineRelocSink::default();
|
|
let mut trap_sink = binemit::NullTrapSink {};
|
|
let mut stack_map_sink = binemit::NullStackMapSink {};
|
|
context
|
|
.compile_and_emit(
|
|
isa,
|
|
&mut code_buf,
|
|
&mut reloc_sink,
|
|
&mut trap_sink,
|
|
&mut stack_map_sink,
|
|
)
|
|
.map_err(|error| {
|
|
SetupError::Compile(CompileError::Codegen(pretty_error(
|
|
&context.func,
|
|
Some(isa),
|
|
error,
|
|
)))
|
|
})?;
|
|
|
|
let unwind_info = context.create_unwind_info(isa).map_err(|error| {
|
|
SetupError::Compile(CompileError::Codegen(pretty_error(
|
|
&context.func,
|
|
Some(isa),
|
|
error,
|
|
)))
|
|
})?;
|
|
|
|
Ok(CompiledFunction {
|
|
body: code_buf,
|
|
jt_offsets: context.func.jt_offsets,
|
|
unwind_info,
|
|
relocations: reloc_sink.relocs,
|
|
stack_maps: Default::default(),
|
|
stack_slots: Default::default(),
|
|
traps: Default::default(),
|
|
value_labels_ranges: Default::default(),
|
|
address_map: Default::default(),
|
|
})
|
|
}
|
|
|
|
/// We don't expect trampoline compilation to produce many relocations, so
|
|
/// this `RelocSink` just asserts that it doesn't recieve most of them, but
|
|
/// handles libcall ones.
|
|
#[derive(Default)]
|
|
pub struct TrampolineRelocSink {
|
|
relocs: Vec<Relocation>,
|
|
}
|
|
|
|
impl TrampolineRelocSink {
|
|
/// Returns collected relocations.
|
|
pub fn relocs(&self) -> &[Relocation] {
|
|
&self.relocs
|
|
}
|
|
}
|
|
|
|
impl binemit::RelocSink for TrampolineRelocSink {
|
|
fn reloc_external(
|
|
&mut self,
|
|
offset: binemit::CodeOffset,
|
|
_srcloc: ir::SourceLoc,
|
|
reloc: binemit::Reloc,
|
|
name: &ir::ExternalName,
|
|
addend: binemit::Addend,
|
|
) {
|
|
let reloc_target = if let ir::ExternalName::LibCall(libcall) = *name {
|
|
RelocationTarget::LibCall(libcall)
|
|
} else {
|
|
panic!("unrecognized external name")
|
|
};
|
|
self.relocs.push(Relocation {
|
|
reloc,
|
|
reloc_target,
|
|
offset,
|
|
addend,
|
|
});
|
|
}
|
|
fn reloc_constant(
|
|
&mut self,
|
|
_code_offset: binemit::CodeOffset,
|
|
_reloc: binemit::Reloc,
|
|
_constant_offset: ir::ConstantOffset,
|
|
) {
|
|
panic!("trampoline compilation should not produce constant relocs");
|
|
}
|
|
fn reloc_jt(
|
|
&mut self,
|
|
_offset: binemit::CodeOffset,
|
|
_reloc: binemit::Reloc,
|
|
_jt: ir::JumpTable,
|
|
) {
|
|
panic!("trampoline compilation should not produce jump table relocs");
|
|
}
|
|
}
|