Support Func imports with zero shims (#839)

* Move `Func` to its own file

* Support `Func` imports with zero shims

This commit extends the `Func` type in the `wasmtime` crate with static
`wrap*` constructors. The goal of these constructors is to create a
`Func` type which has zero shims associated with it, creating as small
of a layer as possible between wasm code and calling imported Rust code.

This is achieved by creating an `extern "C"` shim function which matches
the ABI of what Cranelift will generate, and then the host function is
passed directly into an `InstanceHandle` to get called later. This also
enables enough inlining opportunities that LLVM will be able to see all
functions and inline everything to the point where your function is
called immediately from wasm, no questions asked.
This commit is contained in:
Alex Crichton
2020-02-04 14:32:35 -06:00
committed by GitHub
parent e09231e33f
commit 1bfca842b0
11 changed files with 631 additions and 190 deletions

View File

@@ -4,6 +4,7 @@ use super::create_handle::create_handle;
use super::trap::TrapSink;
use crate::{Callable, FuncType, Store, Trap, Val};
use anyhow::{bail, Result};
use std::any::Any;
use std::cmp;
use std::convert::TryFrom;
use std::panic::{self, AssertUnwindSafe};
@@ -325,3 +326,35 @@ pub fn create_handle_with_function(
Box::new(trampoline_state),
)
}
pub unsafe fn create_handle_with_raw_function(
ft: &FuncType,
func: *const VMFunctionBody,
store: &Store,
state: Box<dyn Any>,
) -> Result<InstanceHandle> {
let isa = {
let isa_builder = native::builder();
let flag_builder = settings::builder();
isa_builder.finish(settings::Flags::new(flag_builder))
};
let pointer_type = isa.pointer_type();
let sig = match ft.get_wasmtime_signature(pointer_type) {
Some(sig) => sig.clone(),
None => bail!("not a supported core wasm signature {:?}", ft),
};
let mut module = Module::new();
let mut finished_functions: PrimaryMap<DefinedFuncIndex, *const VMFunctionBody> =
PrimaryMap::new();
let sig_id = module.signatures.push(sig.clone());
let func_id = module.functions.push(sig_id);
module
.exports
.insert("trampoline".to_string(), Export::Function(func_id));
finished_functions.push(func);
create_handle(module, Some(store), finished_functions, state)
}