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

@@ -2,7 +2,6 @@
use crate::tunables::Tunables;
use crate::WASM_MAX_PAGES;
use cranelift_codegen::ir;
use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::{
DataIndex, DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex,
@@ -169,7 +168,7 @@ pub struct Module {
pub func_names: HashMap<FuncIndex, String>,
/// Unprocessed signatures exactly as provided by `declare_signature()`.
pub signatures: PrimaryMap<SignatureIndex, (WasmFuncType, ir::Signature)>,
pub signatures: PrimaryMap<SignatureIndex, WasmFuncType>,
/// Number of imported functions in the module.
pub num_imported_funcs: usize,
@@ -319,16 +318,10 @@ impl Module {
index.index() < self.num_imported_globals
}
/// Convenience method for looking up the native signature of a compiled
/// Wasm function.
pub fn native_func_signature(&self, func_index: FuncIndex) -> &ir::Signature {
&self.signatures[self.functions[func_index]].1
}
/// Convenience method for looking up the original Wasm signature of a
/// function.
pub fn wasm_func_type(&self, func_index: FuncIndex) -> &WasmFuncType {
&self.signatures[self.functions[func_index]].0
&self.signatures[self.functions[func_index]]
}
}

View File

@@ -35,6 +35,9 @@ pub struct ModuleTranslation<'data> {
/// Module information.
pub module: Module,
/// Map of native signatures
pub native_signatures: PrimaryMap<SignatureIndex, ir::Signature>,
/// References to the function bodies.
pub function_body_inputs: PrimaryMap<DefinedFuncIndex, FunctionBodyData<'data>>,
@@ -111,6 +114,7 @@ impl<'data> ModuleEnvironment<'data> {
result: ModuleTranslation {
target_config,
module: Module::new(),
native_signatures: PrimaryMap::new(),
function_body_inputs: PrimaryMap::new(),
data_initializers: Vec::new(),
tunables: tunables.clone(),
@@ -198,17 +202,17 @@ impl<'data> TargetEnvironment for ModuleEnvironment<'data> {
/// environment-dependent wasm instructions. These functions should not be called by the user.
impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data> {
fn reserve_signatures(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.signatures
.reserve_exact(usize::try_from(num).unwrap());
let num = usize::try_from(num).unwrap();
self.result.module.signatures.reserve_exact(num);
self.result.native_signatures.reserve_exact(num);
Ok(())
}
fn declare_signature(&mut self, wasm: WasmFuncType, sig: ir::Signature) -> WasmResult<()> {
let sig = translate_signature(sig, self.pointer_type());
// TODO: Deduplicate signatures.
self.result.module.signatures.push((wasm, sig));
self.result.module.signatures.push(wasm);
self.result.native_signatures.push(sig);
Ok(())
}
@@ -461,7 +465,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
}
info.wasm_file.funcs.push(FunctionMetadata {
locals: locals.into_boxed_slice(),
params: sig.0.params.iter().cloned().map(|i| i.into()).collect(),
params: sig.params.iter().cloned().map(|i| i.into()).collect(),
});
}
self.result