Pre-generate trampoline functions (#957)
* Refactor wasmtime_runtime::Export Instead of an enumeration with variants that have data fields have an enumeration where each variant has a struct, and each struct has the data fields. This allows us to store the structs in the `wasmtime` API and avoid lots of `panic!` calls and various extraneous matches. * Pre-generate trampoline functions The `wasmtime` crate supports calling arbitrary function signatures in wasm code, and to do this it generates "trampoline functions" which have a known ABI that then internally convert to a particular signature's ABI and call it. These trampoline functions are currently generated on-the-fly and are cached in the global `Store` structure. This, however, is suboptimal for a few reasons: * Due to how code memory is managed each trampoline resides in its own 64kb allocation of memory. This means if you have N trampolines you're using N * 64kb of memory, which is quite a lot of overhead! * Trampolines are never free'd, even if the referencing module goes away. This is similar to #925. * Trampolines are a source of shared state which prevents `Store` from being easily thread safe. This commit refactors how trampolines are managed inside of the `wasmtime` crate and jit/runtime internals. All trampolines are now allocated in the same pass of `CodeMemory` that the main module is allocated into. A trampoline is generated per-signature in a module as well, instead of per-function. This cache of trampolines is stored directly inside of an `Instance`. Trampolines are stored based on `VMSharedSignatureIndex` so they can be looked up from the internals of the `ExportFunction` value. The `Func` API has been updated with various bits and pieces to ensure the right trampolines are registered in the right places. Overall this should ensure that all trampolines necessary are generated up-front rather than lazily. This allows us to remove the trampoline cache from the `Compiler` type, and move one step closer to making `Compiler` threadsafe for usage across multiple threads. Note that as one small caveat the `Func::wrap*` family of functions don't need to generate a trampoline at runtime, they actually generate the trampoline at compile time which gets passed in. Also in addition to shuffling a lot of code around this fixes one minor bug found in `code_memory.rs`, where `self.position` was loaded before allocation, but the allocation may push a new chunk which would cause `self.position` to be zero instead. * Pass the `SignatureRegistry` as an argument to where it's needed. This avoids the need for storing it in an `Arc`. * Ignore tramoplines for functions with lots of arguments Co-authored-by: Dan Gohman <sunfish@mozilla.com>
This commit is contained in:
@@ -12,16 +12,16 @@ use crate::traphandlers::{catch_traps, Trap};
|
||||
use crate::vmcontext::{
|
||||
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionBody, VMFunctionImport,
|
||||
VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex,
|
||||
VMTableDefinition, VMTableImport,
|
||||
VMTableDefinition, VMTableImport, VMTrampoline,
|
||||
};
|
||||
use crate::TrapRegistration;
|
||||
use crate::{ExportFunction, ExportGlobal, ExportMemory, ExportTable};
|
||||
use memoffset::offset_of;
|
||||
use more_asserts::assert_lt;
|
||||
use std::alloc::{self, Layout};
|
||||
use std::any::Any;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
@@ -99,6 +99,9 @@ pub(crate) struct Instance {
|
||||
/// Pointers to functions in executable memory.
|
||||
finished_functions: BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>,
|
||||
|
||||
/// Pointers to trampoline functions used to enter particular signatures
|
||||
trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
|
||||
|
||||
/// Hosts can store arbitrary per-instance information here.
|
||||
host_state: Box<dyn Any>,
|
||||
|
||||
@@ -301,8 +304,7 @@ impl Instance {
|
||||
pub fn lookup_by_declaration(&self, export: &wasmtime_environ::Export) -> Export {
|
||||
match export {
|
||||
wasmtime_environ::Export::Function(index) => {
|
||||
let signature =
|
||||
self.module.local.signatures[self.module.local.functions[*index]].clone();
|
||||
let signature = self.signature_id(self.module.local.functions[*index]);
|
||||
let (address, vmctx) =
|
||||
if let Some(def_index) = self.module.local.defined_func_index(*index) {
|
||||
(
|
||||
@@ -313,11 +315,12 @@ impl Instance {
|
||||
let import = self.imported_function(*index);
|
||||
(import.body, import.vmctx)
|
||||
};
|
||||
Export::Function {
|
||||
ExportFunction {
|
||||
address,
|
||||
signature,
|
||||
vmctx,
|
||||
}
|
||||
.into()
|
||||
}
|
||||
wasmtime_environ::Export::Table(index) => {
|
||||
let (definition, vmctx) =
|
||||
@@ -327,11 +330,12 @@ impl Instance {
|
||||
let import = self.imported_table(*index);
|
||||
(import.from, import.vmctx)
|
||||
};
|
||||
Export::Table {
|
||||
ExportTable {
|
||||
definition,
|
||||
vmctx,
|
||||
table: self.module.local.table_plans[*index].clone(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
wasmtime_environ::Export::Memory(index) => {
|
||||
let (definition, vmctx) =
|
||||
@@ -341,13 +345,14 @@ impl Instance {
|
||||
let import = self.imported_memory(*index);
|
||||
(import.from, import.vmctx)
|
||||
};
|
||||
Export::Memory {
|
||||
ExportMemory {
|
||||
definition,
|
||||
vmctx,
|
||||
memory: self.module.local.memory_plans[*index].clone(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
wasmtime_environ::Export::Global(index) => Export::Global {
|
||||
wasmtime_environ::Export::Global(index) => ExportGlobal {
|
||||
definition: if let Some(def_index) = self.module.local.defined_global_index(*index)
|
||||
{
|
||||
self.global_ptr(def_index)
|
||||
@@ -356,7 +361,8 @@ impl Instance {
|
||||
},
|
||||
vmctx: self.vmctx_ptr(),
|
||||
global: self.module.local.globals[*index],
|
||||
},
|
||||
}
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -853,6 +859,7 @@ impl InstanceHandle {
|
||||
module: Arc<Module>,
|
||||
trap_registration: TrapRegistration,
|
||||
finished_functions: BoxedSlice<DefinedFuncIndex, *mut [VMFunctionBody]>,
|
||||
trampolines: HashMap<VMSharedSignatureIndex, VMTrampoline>,
|
||||
imports: Imports,
|
||||
data_initializers: &[DataInitializer<'_>],
|
||||
vmshared_signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
|
||||
@@ -892,6 +899,7 @@ impl InstanceHandle {
|
||||
passive_elements: Default::default(),
|
||||
passive_data,
|
||||
finished_functions,
|
||||
trampolines,
|
||||
dbg_jit_registration,
|
||||
host_state,
|
||||
signal_handler: Cell::new(None),
|
||||
@@ -1093,6 +1101,11 @@ impl InstanceHandle {
|
||||
self.instance().get_defined_table(index)
|
||||
}
|
||||
|
||||
/// Gets the trampoline pre-registered for a particular signature
|
||||
pub fn trampoline(&self, sig: VMSharedSignatureIndex) -> Option<VMTrampoline> {
|
||||
self.instance().trampolines.get(&sig).cloned()
|
||||
}
|
||||
|
||||
/// Return a reference to the contained `Instance`.
|
||||
pub(crate) fn instance(&self) -> &Instance {
|
||||
unsafe { &*(self.instance as *const Instance) }
|
||||
|
||||
Reference in New Issue
Block a user