Generate trampolines based on signatures (#947)
* Generate trampolines based on signatures Instead of generating a trampoline-per-function generate a trampoline-per-signature. This should hopefully greatly increase the cache hit rate on trampolines within a module and avoid generating a function-per-function. * Update crates/runtime/src/traphandlers.rs Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com> Co-authored-by: Sergei Pepyakin <s.pepyakin@gmail.com>
This commit is contained in:
@@ -347,10 +347,14 @@ impl Instance {
|
||||
&*self.host_state
|
||||
}
|
||||
|
||||
fn invoke_function(&self, index: FuncIndex) -> Result<(), InstantiationError> {
|
||||
// TODO: Check that the callee's calling convention matches what we expect.
|
||||
/// Invoke the WebAssembly start function of the instance, if one is present.
|
||||
fn invoke_start_function(&self) -> Result<(), InstantiationError> {
|
||||
let start_index = match self.module.start_func {
|
||||
Some(idx) => idx,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
let (callee_address, callee_vmctx) = match self.module.defined_func_index(index) {
|
||||
let (callee_address, callee_vmctx) = match self.module.defined_func_index(start_index) {
|
||||
Some(defined_index) => {
|
||||
let body = *self
|
||||
.finished_functions
|
||||
@@ -359,8 +363,8 @@ impl Instance {
|
||||
(body as *const _, self.vmctx_ptr())
|
||||
}
|
||||
None => {
|
||||
assert_lt!(index.index(), self.module.imported_funcs.len());
|
||||
let import = self.imported_function(index);
|
||||
assert_lt!(start_index.index(), self.module.imported_funcs.len());
|
||||
let import = self.imported_function(start_index);
|
||||
(import.body, import.vmctx)
|
||||
}
|
||||
};
|
||||
@@ -370,15 +374,6 @@ impl Instance {
|
||||
.map_err(InstantiationError::StartTrap)
|
||||
}
|
||||
|
||||
/// Invoke the WebAssembly start function of the instance, if one is present.
|
||||
fn invoke_start_function(&self) -> Result<(), InstantiationError> {
|
||||
if let Some(start_index) = self.module.start_func {
|
||||
self.invoke_function(start_index)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the offset from the vmctx pointer to its containing Instance.
|
||||
pub(crate) fn vmctx_offset() -> isize {
|
||||
offset_of!(Self, vmctx) as isize
|
||||
|
||||
@@ -17,6 +17,7 @@ extern "C" {
|
||||
jmp_buf: *mut *const u8,
|
||||
vmctx: *mut u8,
|
||||
caller_vmctx: *mut u8,
|
||||
trampoline: *const VMFunctionBody,
|
||||
callee: *const VMFunctionBody,
|
||||
values_vec: *mut u8,
|
||||
) -> i32;
|
||||
@@ -133,13 +134,23 @@ impl fmt::Display for Trap {
|
||||
|
||||
impl std::error::Error for Trap {}
|
||||
|
||||
/// Call the wasm function pointed to by `callee`. `values_vec` points to
|
||||
/// a buffer which holds the incoming arguments, and to which the outgoing
|
||||
/// return values will be written.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_call_trampoline(
|
||||
/// Call the wasm function pointed to by `callee`.
|
||||
///
|
||||
/// * `vmctx` - the callee vmctx argument
|
||||
/// * `caller_vmctx` - the caller vmctx argument
|
||||
/// * `trampoline` - the jit-generated trampoline whose ABI takes 4 values, the
|
||||
/// callee vmctx, the caller vmctx, the `callee` argument below, and then the
|
||||
/// `values_vec` argument.
|
||||
/// * `callee` - the third argument to the `trampoline` function
|
||||
/// * `values_vec` - points to a buffer which holds the incoming arguments, and to
|
||||
/// which the outgoing return values will be written.
|
||||
///
|
||||
/// Wildly unsafe because it calls raw function pointers and reads/writes raw
|
||||
/// function pointers.
|
||||
pub unsafe fn wasmtime_call_trampoline(
|
||||
vmctx: *mut VMContext,
|
||||
caller_vmctx: *mut VMContext,
|
||||
trampoline: *const VMFunctionBody,
|
||||
callee: *const VMFunctionBody,
|
||||
values_vec: *mut u8,
|
||||
) -> Result<(), Trap> {
|
||||
@@ -148,6 +159,7 @@ pub unsafe extern "C" fn wasmtime_call_trampoline(
|
||||
cx.jmp_buf.as_ptr(),
|
||||
vmctx as *mut u8,
|
||||
caller_vmctx as *mut u8,
|
||||
trampoline,
|
||||
callee,
|
||||
values_vec,
|
||||
)
|
||||
@@ -156,8 +168,7 @@ pub unsafe extern "C" fn wasmtime_call_trampoline(
|
||||
|
||||
/// Call the wasm function pointed to by `callee`, which has no arguments or
|
||||
/// return values.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasmtime_call(
|
||||
pub unsafe fn wasmtime_call(
|
||||
vmctx: *mut VMContext,
|
||||
caller_vmctx: *mut VMContext,
|
||||
callee: *const VMFunctionBody,
|
||||
|
||||
Reference in New Issue
Block a user