Remove the local field of Module (#2091)

This was added long ago at this point to assist with caching, but
caching has moved to a different level such that this wonky second level
of a `Module` isn't necessary. This commit removes the `ModuleLocal`
type to simplify accessors and generally make it easier to work with.
This commit is contained in:
Alex Crichton
2020-08-04 12:29:16 -05:00
committed by GitHub
parent c21fe0eb73
commit 3d2e0e55f2
26 changed files with 131 additions and 164 deletions

View File

@@ -302,17 +302,17 @@ impl Compiler for Cranelift {
input: &FunctionBodyData<'_>,
isa: &dyn isa::TargetIsa,
) -> Result<CompiledFunction, CompileError> {
let local = &translation.module.local;
let module = &translation.module;
let tunables = &translation.tunables;
let func_index = local.func_index(func_index);
let func_index = module.func_index(func_index);
let mut context = Context::new();
context.func.name = get_func_name(func_index);
context.func.signature = local.native_func_signature(func_index).clone();
context.func.signature = module.native_func_signature(func_index).clone();
if tunables.debug_info {
context.func.collect_debug_info();
}
let mut func_env = FuncEnvironment::new(isa.frontend_config(), local, tunables);
let mut func_env = FuncEnvironment::new(isa.frontend_config(), module, tunables);
// We use these as constant offsets below in
// `stack_limit_from_arguments`, so assert their values here. This

View File

@@ -1,6 +1,6 @@
use crate::module::{MemoryPlan, MemoryStyle, ModuleLocal, TableStyle};
use crate::module::{MemoryPlan, MemoryStyle, TableStyle};
use crate::vmoffsets::VMOffsets;
use crate::{Tunables, INTERRUPTED, WASM_PAGE_SIZE};
use crate::{Module, Tunables, INTERRUPTED, WASM_PAGE_SIZE};
use cranelift_codegen::cursor::FuncCursor;
use cranelift_codegen::ir;
use cranelift_codegen::ir::condcodes::*;
@@ -203,7 +203,7 @@ pub struct FuncEnvironment<'module_environment> {
target_config: TargetFrontendConfig,
/// The module-level environment which this function-level environment belongs to.
module: &'module_environment ModuleLocal,
module: &'module_environment Module,
/// The Cranelift global holding the vmctx address.
vmctx: Option<ir::GlobalValue>,
@@ -220,7 +220,7 @@ pub struct FuncEnvironment<'module_environment> {
impl<'module_environment> FuncEnvironment<'module_environment> {
pub fn new(
target_config: TargetFrontendConfig,
module: &'module_environment ModuleLocal,
module: &'module_environment Module,
tunables: &'module_environment Tunables,
) -> Self {
let builtin_function_signatures = BuiltinFunctionSignatures::new(

View File

@@ -49,7 +49,7 @@ pub use crate::func_environ::BuiltinFunctionIndex;
#[cfg(feature = "lightbeam")]
pub use crate::lightbeam::Lightbeam;
pub use crate::module::{
EntityIndex, MemoryPlan, MemoryStyle, Module, ModuleLocal, TableElements, TablePlan, TableStyle,
EntityIndex, MemoryPlan, MemoryStyle, Module, TableElements, TablePlan, TableStyle,
};
pub use crate::module_environ::*;
pub use crate::tunables::Tunables;

View File

@@ -22,11 +22,11 @@ impl Compiler for Lightbeam {
if translation.tunables.debug_info {
return Err(CompileError::DebugInfoNotSupported);
}
let func_index = translation.module.local.func_index(i);
let func_index = translation.module.func_index(i);
let env = FuncEnvironment::new(
isa.frontend_config(),
&translation.module.local,
&translation.module,
&translation.tunables,
);
let mut codegen_session: CodeGenSession<_> = CodeGenSession::new(

View File

@@ -146,12 +146,6 @@ pub struct Module {
/// The name of this wasm module, often found in the wasm file.
pub name: Option<String>,
/// Local information about a module which is the bare minimum necessary to
/// translate a function body. This is derived as `Hash` whereas this module
/// isn't, since it contains too much information needed to translate a
/// function.
pub local: ModuleLocal,
/// All import records, in the order they are declared in the module.
pub imports: Vec<(String, String, EntityIndex)>,
@@ -173,16 +167,7 @@ pub struct Module {
/// WebAssembly table initializers.
pub func_names: HashMap<FuncIndex, String>,
}
/// Local information known about a wasm module, the bare minimum necessary to
/// translate function bodies.
///
/// This is stored within a `Module` and it implements `Hash`, unlike `Module`,
/// and is used as part of the cache key when we load compiled modules from the
/// global cache.
#[derive(Debug, Clone, Hash, Serialize, Deserialize)]
pub struct ModuleLocal {
/// Unprocessed signatures exactly as provided by `declare_signature()`.
pub signatures: PrimaryMap<SignatureIndex, (WasmFuncType, ir::Signature)>,
@@ -224,17 +209,15 @@ impl Module {
passive_elements: HashMap::new(),
passive_data: HashMap::new(),
func_names: HashMap::new(),
local: ModuleLocal {
num_imported_funcs: 0,
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
signatures: PrimaryMap::new(),
functions: PrimaryMap::new(),
table_plans: PrimaryMap::new(),
memory_plans: PrimaryMap::new(),
globals: PrimaryMap::new(),
},
num_imported_funcs: 0,
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
signatures: PrimaryMap::new(),
functions: PrimaryMap::new(),
table_plans: PrimaryMap::new(),
memory_plans: PrimaryMap::new(),
globals: PrimaryMap::new(),
}
}
@@ -247,9 +230,7 @@ impl Module {
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
NEXT_ID.fetch_add(1, SeqCst)
}
}
impl ModuleLocal {
/// Convert a `DefinedFuncIndex` into a `FuncIndex`.
pub fn func_index(&self, defined_func: DefinedFuncIndex) -> FuncIndex {
FuncIndex::new(self.num_imported_funcs + defined_func.index())

View File

@@ -195,7 +195,6 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
fn reserve_signatures(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.local
.signatures
.reserve_exact(usize::try_from(num).unwrap());
Ok(())
@@ -204,7 +203,7 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
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.local.signatures.push((wasm, sig));
self.result.module.signatures.push((wasm, sig));
Ok(())
}
@@ -223,17 +222,17 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
field: &str,
) -> WasmResult<()> {
debug_assert_eq!(
self.result.module.local.functions.len(),
self.result.module.local.num_imported_funcs,
self.result.module.functions.len(),
self.result.module.num_imported_funcs,
"Imported functions must be declared first"
);
let func_index = self.result.module.local.functions.push(sig_index);
let func_index = self.result.module.functions.push(sig_index);
self.result.module.imports.push((
module.to_owned(),
field.to_owned(),
EntityIndex::Function(func_index),
));
self.result.module.local.num_imported_funcs += 1;
self.result.module.num_imported_funcs += 1;
if let Some(info) = &mut self.result.debuginfo {
info.wasm_file.imported_func_count += 1;
}
@@ -242,18 +241,18 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
fn declare_table_import(&mut self, table: Table, module: &str, field: &str) -> WasmResult<()> {
debug_assert_eq!(
self.result.module.local.table_plans.len(),
self.result.module.local.num_imported_tables,
self.result.module.table_plans.len(),
self.result.module.num_imported_tables,
"Imported tables must be declared first"
);
let plan = TablePlan::for_table(table, &self.result.tunables);
let table_index = self.result.module.local.table_plans.push(plan);
let table_index = self.result.module.table_plans.push(plan);
self.result.module.imports.push((
module.to_owned(),
field.to_owned(),
EntityIndex::Table(table_index),
));
self.result.module.local.num_imported_tables += 1;
self.result.module.num_imported_tables += 1;
Ok(())
}
@@ -264,21 +263,21 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
field: &str,
) -> WasmResult<()> {
debug_assert_eq!(
self.result.module.local.memory_plans.len(),
self.result.module.local.num_imported_memories,
self.result.module.memory_plans.len(),
self.result.module.num_imported_memories,
"Imported memories must be declared first"
);
if memory.shared {
return Err(WasmError::Unsupported("shared memories".to_owned()));
}
let plan = MemoryPlan::for_memory(memory, &self.result.tunables);
let memory_index = self.result.module.local.memory_plans.push(plan);
let memory_index = self.result.module.memory_plans.push(plan);
self.result.module.imports.push((
module.to_owned(),
field.to_owned(),
EntityIndex::Memory(memory_index),
));
self.result.module.local.num_imported_memories += 1;
self.result.module.num_imported_memories += 1;
Ok(())
}
@@ -289,24 +288,23 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
field: &str,
) -> WasmResult<()> {
debug_assert_eq!(
self.result.module.local.globals.len(),
self.result.module.local.num_imported_globals,
self.result.module.globals.len(),
self.result.module.num_imported_globals,
"Imported globals must be declared first"
);
let global_index = self.result.module.local.globals.push(global);
let global_index = self.result.module.globals.push(global);
self.result.module.imports.push((
module.to_owned(),
field.to_owned(),
EntityIndex::Global(global_index),
));
self.result.module.local.num_imported_globals += 1;
self.result.module.num_imported_globals += 1;
Ok(())
}
fn reserve_func_types(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.local
.functions
.reserve_exact(usize::try_from(num).unwrap());
self.result
@@ -316,14 +314,13 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
}
fn declare_func_type(&mut self, sig_index: SignatureIndex) -> WasmResult<()> {
self.result.module.local.functions.push(sig_index);
self.result.module.functions.push(sig_index);
Ok(())
}
fn reserve_tables(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.local
.table_plans
.reserve_exact(usize::try_from(num).unwrap());
Ok(())
@@ -331,14 +328,13 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
fn declare_table(&mut self, table: Table) -> WasmResult<()> {
let plan = TablePlan::for_table(table, &self.result.tunables);
self.result.module.local.table_plans.push(plan);
self.result.module.table_plans.push(plan);
Ok(())
}
fn reserve_memories(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.local
.memory_plans
.reserve_exact(usize::try_from(num).unwrap());
Ok(())
@@ -349,21 +345,20 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
return Err(WasmError::Unsupported("shared memories".to_owned()));
}
let plan = MemoryPlan::for_memory(memory, &self.result.tunables);
self.result.module.local.memory_plans.push(plan);
self.result.module.memory_plans.push(plan);
Ok(())
}
fn reserve_globals(&mut self, num: u32) -> WasmResult<()> {
self.result
.module
.local
.globals
.reserve_exact(usize::try_from(num).unwrap());
Ok(())
}
fn declare_global(&mut self, global: Global) -> WasmResult<()> {
self.result.module.local.globals.push(global);
self.result.module.globals.push(global);
Ok(())
}
@@ -456,10 +451,10 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
module_offset: body_offset,
});
if let Some(info) = &mut self.result.debuginfo {
let func_index = self.code_index + self.result.module.local.num_imported_funcs as u32;
let func_index = self.code_index + self.result.module.num_imported_funcs as u32;
let func_index = FuncIndex::from_u32(func_index);
let sig_index = self.result.module.local.functions[func_index];
let sig = &self.result.module.local.signatures[sig_index];
let sig_index = self.result.module.functions[func_index];
let sig = &self.result.module.signatures[sig_index];
let mut locals = Vec::new();
let body = wasmparser::FunctionBody::new(body_offset, body_bytes);
for pair in body.get_locals_reader()? {

View File

@@ -19,7 +19,7 @@
// builtins: VMBuiltinFunctionsArray,
// }
use crate::module::ModuleLocal;
use crate::module::Module;
use crate::BuiltinFunctionIndex;
use cranelift_codegen::ir;
use cranelift_wasm::{
@@ -75,7 +75,7 @@ pub struct VMOffsets {
impl VMOffsets {
/// Return a new `VMOffsets` instance, for a given pointer size.
pub fn new(pointer_size: u8, module: &ModuleLocal) -> Self {
pub fn new(pointer_size: u8, module: &Module) -> Self {
Self {
pointer_size,
num_signature_ids: cast_to_u32(module.signatures.len()),