mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-22 21:21:33 +00:00
Pass module info to FCG as Arc
This commit is contained in:
@ -37,6 +37,7 @@ use wasmer_runtime_core::{
|
||||
},
|
||||
vm,
|
||||
};
|
||||
use wasmparser::Operator;
|
||||
use wasmparser::Type as WpType;
|
||||
|
||||
pub struct CraneliftModuleCodeGenerator {
|
||||
@ -75,7 +76,7 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
|
||||
fn next_function(
|
||||
&mut self,
|
||||
module_info: &ModuleInfo,
|
||||
module_info: Arc<ModuleInfo>,
|
||||
) -> Result<&mut CraneliftFunctionCodeGenerator, CodegenError> {
|
||||
// define_function_body(
|
||||
|
||||
@ -108,6 +109,8 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
|
||||
func_translator,
|
||||
next_local: 0,
|
||||
clif_signatures: self.clif_signatures.clone(),
|
||||
module_info: Arc::clone(&module_info),
|
||||
target_config: self.isa.frontend_config().clone(),
|
||||
};
|
||||
let builder = FunctionBuilder::new(
|
||||
&mut func_env.func_body,
|
||||
@ -370,12 +373,14 @@ pub struct CraneliftFunctionCodeGenerator {
|
||||
func_translator: FuncTranslator,
|
||||
next_local: usize,
|
||||
pub clif_signatures: Map<SigIndex, ir::Signature>,
|
||||
module_info: Arc<ModuleInfo>,
|
||||
target_config: isa::TargetFrontendConfig,
|
||||
}
|
||||
|
||||
impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
/// Gets configuration information needed for compiling functions
|
||||
fn target_config(&self) -> isa::TargetFrontendConfig {
|
||||
self.env.target_config()
|
||||
self.target_config
|
||||
}
|
||||
|
||||
/// Gets native pointers types.
|
||||
@ -405,7 +410,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let local_global_addr = match global_index.local_or_import(&self.env.module.info) {
|
||||
let local_global_addr = match global_index.local_or_import(&self.module_info) {
|
||||
LocalOrImport::Local(local_global_index) => {
|
||||
let globals_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
@ -457,7 +462,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
Ok(cranelift_wasm::GlobalVariable::Memory {
|
||||
gv: local_global_addr,
|
||||
offset: (vm::LocalGlobal::offset_data() as i32).into(),
|
||||
ty: self.env.get_global(clif_global_index).ty,
|
||||
ty: ptr_type,
|
||||
})
|
||||
}
|
||||
|
||||
@ -475,8 +480,8 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let (local_memory_ptr_ptr, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
let (local_memory_ptr_ptr, description) = match mem_index.local_or_import(&self.module_info)
|
||||
{
|
||||
LocalOrImport::Local(local_mem_index) => {
|
||||
let memories_base_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
@ -494,7 +499,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
offset: (local_memory_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
}),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
self.module_info.memories[local_mem_index],
|
||||
)
|
||||
}
|
||||
LocalOrImport::Import(import_mem_index) => {
|
||||
@ -514,7 +519,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
offset: (local_memory_ptr_offset as i64).into(),
|
||||
global_type: ptr_type,
|
||||
}),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
self.module_info.imported_memories[import_mem_index].1,
|
||||
)
|
||||
}
|
||||
};
|
||||
@ -585,7 +590,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
let (table_struct_ptr_ptr, description) = match table_index
|
||||
.local_or_import(&self.env.module.info)
|
||||
.local_or_import(&self.module_info)
|
||||
{
|
||||
LocalOrImport::Local(local_table_index) => {
|
||||
let tables_base = func.create_global_value(ir::GlobalValueData::Load {
|
||||
@ -606,7 +611,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
|
||||
(
|
||||
table_struct_ptr_ptr,
|
||||
self.env.module.info.tables[local_table_index],
|
||||
self.module_info.tables[local_table_index],
|
||||
)
|
||||
}
|
||||
LocalOrImport::Import(import_table_index) => {
|
||||
@ -628,7 +633,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
|
||||
(
|
||||
table_struct_ptr_ptr,
|
||||
self.env.module.info.imported_tables[import_table_index].1,
|
||||
self.module_info.imported_tables[import_table_index].1,
|
||||
)
|
||||
}
|
||||
};
|
||||
@ -688,7 +693,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
func_index: cranelift_wasm::FuncIndex,
|
||||
) -> cranelift_wasm::WasmResult<ir::FuncRef> {
|
||||
// Get signature of function.
|
||||
let signature_index = self.env.get_func_type(func_index);
|
||||
let signature_index = self.get_func_type(func_index);
|
||||
|
||||
// Create a signature reference from specified signature (with VMContext param added).
|
||||
let signature = func.import_signature(self.generate_signature(signature_index));
|
||||
@ -815,7 +820,7 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
let callee_index: FuncIndex = Converter(clif_callee_index).into();
|
||||
let ptr_type = self.pointer_type();
|
||||
|
||||
match callee_index.local_or_import(&self.env.module.info) {
|
||||
match callee_index.local_or_import(&self.module_info) {
|
||||
LocalOrImport::Local(local_function_index) => {
|
||||
// this is an internal function
|
||||
let vmctx = pos
|
||||
@ -925,17 +930,17 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
|
||||
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
|
||||
|
||||
let (namespace, mem_index, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info)
|
||||
{
|
||||
LocalOrImport::Local(local_mem_index) => (
|
||||
call_names::LOCAL_NAMESPACE,
|
||||
local_mem_index.index(),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
self.module_info.memories[local_mem_index],
|
||||
),
|
||||
LocalOrImport::Import(import_mem_index) => (
|
||||
call_names::IMPORT_NAMESPACE,
|
||||
import_mem_index.index(),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
self.module_info.imported_memories[import_mem_index].1,
|
||||
),
|
||||
};
|
||||
|
||||
@ -989,17 +994,17 @@ impl FuncEnvironment for CraneliftFunctionCodeGenerator {
|
||||
|
||||
let mem_index: MemoryIndex = Converter(clif_mem_index).into();
|
||||
|
||||
let (namespace, mem_index, description) =
|
||||
match mem_index.local_or_import(&self.env.module.info) {
|
||||
let (namespace, mem_index, description) = match mem_index.local_or_import(&self.module_info)
|
||||
{
|
||||
LocalOrImport::Local(local_mem_index) => (
|
||||
call_names::LOCAL_NAMESPACE,
|
||||
local_mem_index.index(),
|
||||
self.env.module.info.memories[local_mem_index],
|
||||
self.module_info.memories[local_mem_index],
|
||||
),
|
||||
LocalOrImport::Import(import_mem_index) => (
|
||||
call_names::IMPORT_NAMESPACE,
|
||||
import_mem_index.index(),
|
||||
self.env.module.info.imported_memories[import_mem_index].1,
|
||||
self.module_info.imported_memories[import_mem_index].1,
|
||||
),
|
||||
};
|
||||
|
||||
@ -1083,7 +1088,8 @@ impl FunctionCodeGenerator<CodegenError> for CraneliftFunctionCodeGenerator {
|
||||
let builder = self.builder.as_mut().unwrap();
|
||||
//let func_environment = FuncEnv::new();
|
||||
//let state = TranslationState::new();
|
||||
translate_operator(*op, builder, &mut self.func_translator.state, self);
|
||||
let opp = Operator::Unreachable; // Placeholder
|
||||
translate_operator(opp, builder, &mut self.func_translator.state, self);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1137,6 +1143,14 @@ impl CraneliftFunctionCodeGenerator {
|
||||
pub fn return_mode(&self) -> ReturnMode {
|
||||
ReturnMode::NormalReturns
|
||||
}
|
||||
|
||||
pub fn get_func_type(
|
||||
&self,
|
||||
func_index: cranelift_wasm::FuncIndex,
|
||||
) -> cranelift_wasm::SignatureIndex {
|
||||
let sig_index: SigIndex = self.module_info.func_assoc[Converter(func_index).into()];
|
||||
Converter(sig_index).into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a signature with VMContext as the last param
|
||||
|
@ -2475,7 +2475,7 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
|
||||
|
||||
fn next_function(
|
||||
&mut self,
|
||||
_module_info: &ModuleInfo,
|
||||
_module_info: Arc<ModuleInfo>,
|
||||
) -> Result<&mut LLVMFunctionCodeGenerator, CodegenError> {
|
||||
// Creates a new function and returns the function-scope code generator for it.
|
||||
let (context, builder, intrinsics) = match self.functions.last_mut() {
|
||||
|
@ -11,6 +11,7 @@ use smallvec::SmallVec;
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use wasmparser::{Operator, Type as WpType};
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -47,7 +48,7 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
|
||||
fn check_precondition(&mut self, module_info: &ModuleInfo) -> Result<(), E>;
|
||||
|
||||
/// Creates a new function and returns the function-scope code generator for it.
|
||||
fn next_function(&mut self, module_info: &ModuleInfo) -> Result<&mut FCG, E>;
|
||||
fn next_function(&mut self, module_info: Arc<ModuleInfo>) -> Result<&mut FCG, E>;
|
||||
fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>;
|
||||
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>;
|
||||
|
||||
|
@ -27,7 +27,7 @@ pub struct ModuleInner {
|
||||
pub info: ModuleInfo,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct ModuleInfo {
|
||||
// This are strictly local and the typsystem ensures that.
|
||||
pub memories: Map<LocalMemoryIndex, MemoryDescriptor>,
|
||||
|
@ -16,6 +16,7 @@ use crate::{
|
||||
};
|
||||
use hashbrown::HashMap;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
use wasmparser::{
|
||||
BinaryReaderError, ExternalKind, FuncType, ImportSectionEntryType, Operator, Type as WpType,
|
||||
WasmDecoder,
|
||||
@ -53,7 +54,7 @@ pub fn read_module<
|
||||
middlewares: &mut MiddlewareChain,
|
||||
compiler_config: &CompilerConfig,
|
||||
) -> Result<ModuleInfo, LoadError> {
|
||||
let mut info = ModuleInfo {
|
||||
let mut info = Arc::new(ModuleInfo {
|
||||
memories: Map::new(),
|
||||
globals: Map::new(),
|
||||
tables: Map::new(),
|
||||
@ -80,7 +81,7 @@ pub fn read_module<
|
||||
em_symbol_map: compiler_config.symbol_map.clone(),
|
||||
|
||||
custom_sections: HashMap::new(),
|
||||
};
|
||||
});
|
||||
|
||||
let mut parser = wasmparser::ValidatingParser::new(
|
||||
wasm,
|
||||
@ -103,10 +104,13 @@ pub fn read_module<
|
||||
use wasmparser::ParserState;
|
||||
let state = parser.read();
|
||||
match *state {
|
||||
ParserState::EndWasm => break Ok(info),
|
||||
ParserState::EndWasm => break Ok(Arc::try_unwrap(info).unwrap()),
|
||||
ParserState::Error(err) => Err(LoadError::Parse(err))?,
|
||||
ParserState::TypeSectionEntry(ref ty) => {
|
||||
info.signatures.push(func_type_to_func_sig(ty)?);
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.signatures
|
||||
.push(func_type_to_func_sig(ty)?);
|
||||
}
|
||||
ParserState::ImportSectionEntry { module, field, ty } => {
|
||||
let namespace_index = namespace_builder.as_mut().unwrap().register(module);
|
||||
@ -119,8 +123,11 @@ pub fn read_module<
|
||||
match ty {
|
||||
ImportSectionEntryType::Function(sigindex) => {
|
||||
let sigindex = SigIndex::new(sigindex as usize);
|
||||
info.imported_functions.push(import_name);
|
||||
info.func_assoc.push(sigindex);
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.imported_functions
|
||||
.push(import_name);
|
||||
Arc::get_mut(&mut info).unwrap().func_assoc.push(sigindex);
|
||||
mcg.feed_import_function()
|
||||
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||
}
|
||||
@ -132,7 +139,10 @@ pub fn read_module<
|
||||
maximum: table_ty.limits.maximum,
|
||||
};
|
||||
|
||||
info.imported_tables.push((import_name, table_desc));
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.imported_tables
|
||||
.push((import_name, table_desc));
|
||||
}
|
||||
ImportSectionEntryType::Memory(memory_ty) => {
|
||||
let mem_desc = MemoryDescriptor {
|
||||
@ -140,20 +150,26 @@ pub fn read_module<
|
||||
maximum: memory_ty.limits.maximum.map(|max| Pages(max)),
|
||||
shared: memory_ty.shared,
|
||||
};
|
||||
info.imported_memories.push((import_name, mem_desc));
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.imported_memories
|
||||
.push((import_name, mem_desc));
|
||||
}
|
||||
ImportSectionEntryType::Global(global_ty) => {
|
||||
let global_desc = GlobalDescriptor {
|
||||
mutable: global_ty.mutable,
|
||||
ty: wp_type_to_type(global_ty.content_type)?,
|
||||
};
|
||||
info.imported_globals.push((import_name, global_desc));
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.imported_globals
|
||||
.push((import_name, global_desc));
|
||||
}
|
||||
}
|
||||
}
|
||||
ParserState::FunctionSectionEntry(sigindex) => {
|
||||
let sigindex = SigIndex::new(sigindex as usize);
|
||||
info.func_assoc.push(sigindex);
|
||||
Arc::get_mut(&mut info).unwrap().func_assoc.push(sigindex);
|
||||
}
|
||||
ParserState::TableSectionEntry(table_ty) => {
|
||||
let table_desc = TableDescriptor {
|
||||
@ -162,7 +178,7 @@ pub fn read_module<
|
||||
maximum: table_ty.limits.maximum,
|
||||
};
|
||||
|
||||
info.tables.push(table_desc);
|
||||
Arc::get_mut(&mut info).unwrap().tables.push(table_desc);
|
||||
}
|
||||
ParserState::MemorySectionEntry(memory_ty) => {
|
||||
let mem_desc = MemoryDescriptor {
|
||||
@ -171,7 +187,7 @@ pub fn read_module<
|
||||
shared: memory_ty.shared,
|
||||
};
|
||||
|
||||
info.memories.push(mem_desc);
|
||||
Arc::get_mut(&mut info).unwrap().memories.push(mem_desc);
|
||||
}
|
||||
ParserState::ExportSectionEntry { field, kind, index } => {
|
||||
let export_index = match kind {
|
||||
@ -181,17 +197,23 @@ pub fn read_module<
|
||||
ExternalKind::Global => ExportIndex::Global(GlobalIndex::new(index as usize)),
|
||||
};
|
||||
|
||||
info.exports.insert(field.to_string(), export_index);
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.exports
|
||||
.insert(field.to_string(), export_index);
|
||||
}
|
||||
ParserState::StartSectionEntry(start_index) => {
|
||||
info.start_func = Some(FuncIndex::new(start_index as usize));
|
||||
Arc::get_mut(&mut info).unwrap().start_func =
|
||||
Some(FuncIndex::new(start_index as usize));
|
||||
}
|
||||
ParserState::BeginFunctionBody { .. } => {
|
||||
let id = func_count.wrapping_add(1);
|
||||
func_count = id;
|
||||
if func_count == 0 {
|
||||
info.namespace_table = namespace_builder.take().unwrap().finish();
|
||||
info.name_table = name_builder.take().unwrap().finish();
|
||||
Arc::get_mut(&mut info).unwrap().namespace_table =
|
||||
namespace_builder.take().unwrap().finish();
|
||||
Arc::get_mut(&mut info).unwrap().name_table =
|
||||
name_builder.take().unwrap().finish();
|
||||
mcg.feed_signatures(info.signatures.clone())
|
||||
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||
mcg.feed_function_signatures(info.func_assoc.clone())
|
||||
@ -201,7 +223,7 @@ pub fn read_module<
|
||||
}
|
||||
|
||||
let fcg = mcg
|
||||
.next_function(&info)
|
||||
.next_function(Arc::clone(&info))
|
||||
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||
middlewares
|
||||
.run(
|
||||
@ -233,15 +255,15 @@ pub fn read_module<
|
||||
|
||||
loop {
|
||||
let state = parser.read();
|
||||
match *state {
|
||||
ParserState::Error(err) => return Err(LoadError::Parse(err)),
|
||||
match state {
|
||||
ParserState::Error(err) => return Err(LoadError::Parse(*err)),
|
||||
ParserState::FunctionBodyLocals { ref locals } => {
|
||||
for &(count, ty) in locals.iter() {
|
||||
fcg.feed_local(ty, count as usize)
|
||||
.map_err(|x| LoadError::Codegen(format!("{:?}", x)))?;
|
||||
}
|
||||
}
|
||||
ParserState::CodeOperator(ref op) => {
|
||||
ParserState::CodeOperator(op) => {
|
||||
if !body_begun {
|
||||
body_begun = true;
|
||||
fcg.begin_body(&info)
|
||||
@ -299,7 +321,10 @@ pub fn read_module<
|
||||
elements: elements.unwrap(),
|
||||
};
|
||||
|
||||
info.elem_initializers.push(table_init);
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.elem_initializers
|
||||
.push(table_init);
|
||||
}
|
||||
ParserState::BeginActiveDataSectionEntry(memory_index) => {
|
||||
let memory_index = MemoryIndex::new(memory_index as usize);
|
||||
@ -330,7 +355,10 @@ pub fn read_module<
|
||||
base: base.unwrap(),
|
||||
data,
|
||||
};
|
||||
info.data_initializers.push(data_init);
|
||||
Arc::get_mut(&mut info)
|
||||
.unwrap()
|
||||
.data_initializers
|
||||
.push(data_init);
|
||||
}
|
||||
ParserState::BeginGlobalSectionEntry(ty) => {
|
||||
let init = loop {
|
||||
@ -351,7 +379,7 @@ pub fn read_module<
|
||||
|
||||
let global_init = GlobalInit { desc, init };
|
||||
|
||||
info.globals.push(global_init);
|
||||
Arc::get_mut(&mut info).unwrap().globals.push(global_init);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
|
@ -302,7 +302,7 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
|
||||
|
||||
fn next_function(
|
||||
&mut self,
|
||||
_module_info: &ModuleInfo,
|
||||
_module_info: Arc<ModuleInfo>,
|
||||
) -> Result<&mut X64FunctionCode, CodegenError> {
|
||||
let (mut assembler, mut function_labels, br_table_data, breakpoints) =
|
||||
match self.functions.last_mut() {
|
||||
|
Reference in New Issue
Block a user