Move native signatures out of Module (#2362)

After compilation there's actually no need to hold onto the native
signature for a wasm function type, so this commit moves out the
`ir::Signature` value from a `Module` into a separate field that's
deallocated when compilation is finished. This simplifies the
`SignatureRegistry` because it only needs to track wasm functino types
and it also means less work is done for `Func::wrap`.
This commit is contained in:
Alex Crichton
2020-11-04 14:22:37 -06:00
committed by GitHub
parent dd9bfcefaa
commit 6b137c2a3d
13 changed files with 65 additions and 66 deletions

View File

@@ -541,7 +541,7 @@ impl Func {
// Signatures should always be registered in the store's registry of
// shared signatures, so we should be able to unwrap safely here.
let signatures = self.instance.store.signatures().borrow();
let (wft, _, _) = signatures
let (wft, _) = signatures
.lookup_shared(self.sig_index())
.expect("signature should be registered");
@@ -554,7 +554,7 @@ impl Func {
/// Returns the number of parameters that this function takes.
pub fn param_arity(&self) -> usize {
let signatures = self.instance.store.signatures().borrow();
let (sig, _, _) = signatures
let (sig, _) = signatures
.lookup_shared(self.sig_index())
.expect("signature should be registered");
sig.params.len()
@@ -563,7 +563,7 @@ impl Func {
/// Returns the number of results this function produces.
pub fn result_arity(&self) -> usize {
let signatures = self.instance.store.signatures().borrow();
let (sig, _, _) = signatures
let (sig, _) = signatures
.lookup_shared(self.sig_index())
.expect("signature should be registered");
sig.returns.len()
@@ -657,7 +657,7 @@ impl Func {
.borrow()
.lookup_shared(unsafe { export.anyfunc.as_ref().type_index })
.expect("failed to retrieve trampoline from module")
.2;
.1;
Func {
instance,

View File

@@ -293,7 +293,7 @@ fn with_imports<R>(
let ty = store
.signatures()
.borrow()
.lookup(&m.signatures[m.functions[i]].0)
.lookup(&m.signatures[m.functions[i]])
.ok_or_else(|| anyhow!("function types incompatible"))?;
if !func.matches_expected(ty) {
bail!("function types incompatible");

View File

@@ -915,10 +915,9 @@ impl Store {
module: &'a wasmtime_environ::Module,
) -> impl Fn(wasm::SignatureIndex) -> VMSharedSignatureIndex + 'a {
move |index| {
let (wasm, _native) = &module.signatures[index];
self.signatures()
.borrow()
.lookup(wasm)
.lookup(&module.signatures[index])
.expect("signature not previously registered")
}
}
@@ -993,8 +992,8 @@ impl Store {
let trampolines = module.compiled_module().trampolines();
let module = module.compiled_module().module();
let mut signatures = self.signatures().borrow_mut();
for (index, (wasm, native)) in module.signatures.iter() {
signatures.register(wasm, native, trampolines[index]);
for (index, wasm) in module.signatures.iter() {
signatures.register(wasm, trampolines[index]);
}
}

View File

@@ -3,7 +3,7 @@
use std::collections::{hash_map, HashMap};
use std::convert::TryFrom;
use wasmtime_environ::{ir, wasm::WasmFuncType};
use wasmtime_environ::wasm::WasmFuncType;
use wasmtime_runtime::{VMSharedSignatureIndex, VMTrampoline};
/// WebAssembly requires that the caller and callee signatures in an indirect
@@ -25,8 +25,6 @@ pub struct SignatureRegistry {
struct Entry {
// The WebAssembly type signature, using wasm types.
wasm: WasmFuncType,
// The native signature we're using for this wasm type signature.
native: ir::Signature,
// The native trampoline used to invoke this type signature from `Func`.
// Note that the code memory for this trampoline is not owned by this
// type, but instead it's expected to be owned by the store that this
@@ -39,7 +37,6 @@ impl SignatureRegistry {
pub fn register(
&mut self,
wasm: &WasmFuncType,
native: &ir::Signature,
trampoline: VMTrampoline,
) -> VMSharedSignatureIndex {
let len = self.wasm2index.len();
@@ -57,7 +54,6 @@ impl SignatureRegistry {
let index = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
self.index_map.push(Entry {
wasm: wasm.clone(),
native: native.clone(),
trampoline,
});
entry.insert(index);
@@ -78,9 +74,9 @@ impl SignatureRegistry {
pub fn lookup_shared(
&self,
idx: VMSharedSignatureIndex,
) -> Option<(&WasmFuncType, &ir::Signature, VMTrampoline)> {
) -> Option<(&WasmFuncType, VMTrampoline)> {
self.index_map
.get(idx.bits() as usize)
.map(|e| (&e.wasm, &e.native, e.trampoline))
.map(|e| (&e.wasm, e.trampoline))
}
}

View File

@@ -223,7 +223,7 @@ pub fn create_handle_with_function(
// First up we manufacture a trampoline which has the ABI specified by `ft`
// and calls into `stub_fn`...
let sig_id = module.signatures.push((wft.clone(), sig.clone()));
let sig_id = module.signatures.push(wft.clone());
let func_id = module.functions.push(sig_id);
module
.exports
@@ -241,10 +241,7 @@ pub fn create_handle_with_function(
&sig,
mem::size_of::<u128>(),
)?;
store
.signatures()
.borrow_mut()
.register(&wft, &sig, trampoline);
store.signatures().borrow_mut().register(&wft, trampoline);
// Next up we wrap everything up into an `InstanceHandle` by publishing our
// code memory (makes it executable) and ensuring all our various bits of
@@ -268,23 +265,18 @@ pub unsafe fn create_handle_with_raw_function(
store: &Store,
state: Box<dyn Any>,
) -> Result<StoreInstanceHandle> {
let pointer_type = store.engine().compiler().isa().pointer_type();
let sig = ft.get_wasmtime_signature(pointer_type);
let wft = ft.to_wasm_func_type();
let mut module = Module::new();
let mut finished_functions = PrimaryMap::new();
let sig_id = module.signatures.push((wft.clone(), sig.clone()));
let sig_id = module.signatures.push(wft.clone());
let func_id = module.functions.push(sig_id);
module
.exports
.insert(String::new(), EntityIndex::Function(func_id));
finished_functions.push(func);
store
.signatures()
.borrow_mut()
.register(&wft, &sig, trampoline);
store.signatures().borrow_mut().register(&wft, trampoline);
create_handle(module, store, finished_functions, state, &[])
}

View File

@@ -37,10 +37,10 @@ pub fn create_global(store: &Store, gt: &GlobalType, val: Val) -> Result<StoreIn
// our global with a `ref.func` to grab that imported function.
let signatures = store.signatures().borrow();
let shared_sig_index = f.sig_index();
let (wasm, native, _) = signatures
let (wasm, _) = signatures
.lookup_shared(shared_sig_index)
.expect("signature not registered");
let local_sig_index = module.signatures.push((wasm.clone(), native.clone()));
let local_sig_index = module.signatures.push(wasm.clone());
let func_index = module.functions.push(local_sig_index);
module.num_imported_funcs = 1;
module