Skip to content

Commit

Permalink
Update module info in-place.
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair authored and nlewycky committed Nov 23, 2020
1 parent 13a979a commit 9d8f315
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 24 deletions.
13 changes: 9 additions & 4 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ use cranelift_codegen::{binemit, Context};
#[cfg(feature = "unwind")]
use gimli::write::{Address, EhFrame, FrameTable};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::CompileError;
use wasmer_compiler::{CallingConvention, ModuleTranslationState, Target};
use wasmer_compiler::{
Compilation, CompileModuleInfo, CompiledFunction, CompiledFunctionFrameInfo,
CompiledFunctionUnwindInfo, Compiler, Dwarf, FunctionBody, FunctionBodyData, SectionIndex,
CompiledFunctionUnwindInfo, Compiler, Dwarf, FunctionBody, FunctionBodyData,
ModuleMiddlewareChain, SectionIndex,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
Expand Down Expand Up @@ -54,14 +56,17 @@ impl Compiler for CraneliftCompiler {
fn compile_module(
&self,
target: &Target,
compile_info: &CompileModuleInfo,
compile_info: &mut CompileModuleInfo,
module_translation_state: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
) -> Result<Compilation, CompileError> {
let isa = self.config().isa(target);
let frontend_config = isa.frontend_config();
let memory_styles = &compile_info.memory_styles;
let table_styles = &compile_info.table_styles;
let mut module = (*compile_info.module).clone();
self.config.middlewares.apply_on_module_info(&mut module);
compile_info.module = Arc::new(module);
let module = &compile_info.module;
let signatures = module
.signatures
Expand All @@ -77,7 +82,7 @@ impl Compiler for CraneliftCompiler {
// FDEs will cause some issues in Linux.
None
} else {
use std::sync::{Arc, Mutex};
use std::sync::Mutex;
match target.triple().default_calling_convention() {
Ok(CallingConvention::SystemV) => {
match isa.create_systemv_cie() {
Expand Down Expand Up @@ -125,7 +130,7 @@ impl Compiler for CraneliftCompiler {
)?;

let mut code_buf: Vec<u8> = Vec::new();
let mut reloc_sink = RelocSink::new(module, func_index);
let mut reloc_sink = RelocSink::new(&module, func_index);
let mut trap_sink = TrapSink::new();
let mut stackmap_sink = binemit::NullStackMapSink {};
context
Expand Down
15 changes: 10 additions & 5 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ use inkwell::module::{Linkage, Module};
use inkwell::targets::FileType;
use inkwell::DLLStorageClass;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::{
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
Dwarf, FunctionBodyData, ModuleTranslationState, RelocationTarget, SectionBody, SectionIndex,
Symbol, SymbolRegistry, Target,
Dwarf, FunctionBodyData, ModuleMiddlewareChain, ModuleTranslationState, RelocationTarget,
SectionBody, SectionIndex, Symbol, SymbolRegistry, Target,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};

//use std::sync::{Arc, Mutex};
//use std::sync::Mutex;

/// A compiler that compiles a WebAssembly module with LLVM, translating the Wasm to LLVM IR,
/// optimizing it and then translating to assembly.
Expand Down Expand Up @@ -233,13 +234,17 @@ impl Compiler for LLVMCompiler {
fn compile_module<'data, 'module>(
&self,
target: &Target,
compile_info: &'module CompileModuleInfo,
compile_info: &'module mut CompileModuleInfo,
module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
) -> Result<Compilation, CompileError> {
//let data = Arc::new(Mutex::new(0));
let memory_styles = &compile_info.memory_styles;
let table_styles = &compile_info.table_styles;

let mut module = (*compile_info.module).clone();
self.config.middlewares.apply_on_module_info(&mut module);
compile_info.module = Arc::new(module);
let module = &compile_info.module;

// TODO: merge constants in sections.
Expand All @@ -260,7 +265,7 @@ impl Compiler for LLVMCompiler {
// TODO: remove (to serialize)
//let _data = data.lock().unwrap();
func_translator.translate(
&module,
module,
module_translation,
i,
input,
Expand Down
5 changes: 4 additions & 1 deletion lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Compiler for SinglepassCompiler {
fn compile_module(
&self,
_target: &Target,
compile_info: &CompileModuleInfo,
compile_info: &mut CompileModuleInfo,
_module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
) -> Result<Compilation, CompileError> {
Expand All @@ -57,6 +57,9 @@ impl Compiler for SinglepassCompiler {
let vmoffsets = VMOffsets::new(8, &compile_info.module);
let memory_styles = &compile_info.memory_styles;
let table_styles = &compile_info.table_styles;
let mut module = (*compile_info.module).clone();
self.config.middlewares.apply_on_module_info(&mut module);
compile_info.module = Arc::new(module);
let module = &compile_info.module;
let import_trampolines: PrimaryMap<SectionIndex, _> = (0..module.num_imported_functions)
.map(FunctionIndex::new)
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait Compiler {
fn compile_module<'data, 'module>(
&self,
target: &Target,
compile_info: &'module CompileModuleInfo,
compile_info: &'module mut CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub trait Compiler {
fn compile_module<'data, 'module>(
&self,
target: &Target,
module: &'module CompileModuleInfo,
module: &'module mut CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
Expand Down
4 changes: 2 additions & 2 deletions lib/engine-jit/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl JITArtifact {
.map(|table_type| tunables.table_style(table_type))
.collect();

let compile_info = CompileModuleInfo {
let mut compile_info = CompileModuleInfo {
module: Arc::new(translation.module),
features: features.clone(),
memory_styles,
Expand All @@ -82,7 +82,7 @@ impl JITArtifact {
// Compile the Module
let compilation = compiler.compile_module(
&jit.target(),
&compile_info,
&mut compile_info,
// SAFETY: Calling `unwrap` is correct since
// `environ.translate()` above will write some data into
// `module_translation_state`.
Expand Down
9 changes: 5 additions & 4 deletions lib/engine-native/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl NativeArtifact {
.map(|_function_body| 0u64)
.collect::<PrimaryMap<LocalFunctionIndex, u64>>();

let metadata = ModuleMetadata {
let mut metadata = ModuleMetadata {
compile_info,
prefix: engine_inner.get_prefix(&data),
data_initializers,
Expand All @@ -190,12 +190,13 @@ impl NativeArtifact {
.expect("Should write number");
metadata_binary.extend(serialized_data);

let (compile_info, symbol_registry) = metadata.split();
let maybe_obj_bytes = compiler.experimental_native_compile_module(
&target,
&metadata.compile_info,
compile_info,
module_translation.as_ref().unwrap(),
&function_body_inputs,
&metadata,
symbol_registry,
&metadata_binary,
);

Expand All @@ -216,7 +217,7 @@ impl NativeArtifact {
None => {
let compilation = compiler.compile_module(
&target,
&metadata.compile_info,
&mut metadata.compile_info,
module_translation.as_ref().unwrap(),
function_body_inputs,
)?;
Expand Down
10 changes: 10 additions & 0 deletions lib/engine-native/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ pub struct ModuleMetadata {
pub function_body_lengths: PrimaryMap<LocalFunctionIndex, u64>,
}

impl ModuleMetadata {
pub fn split(&mut self) -> (&mut CompileModuleInfo, &dyn SymbolRegistry) {
let compile_info = &self.compile_info;
let symbol_registry = self as &dyn SymbolRegistry;
#[allow(mutable_transmutes)]
let compile_info = unsafe { std::mem::transmute::<&_, &mut _>(compile_info) };
(compile_info, symbol_registry)
}
}

impl SymbolRegistry for ModuleMetadata {
fn symbol_to_name(&self, symbol: Symbol) -> String {
match symbol {
Expand Down
9 changes: 5 additions & 4 deletions lib/engine-object-file/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl ObjectFileArtifact {
.map(|_function_body| 0u64)
.collect::<PrimaryMap<LocalFunctionIndex, u64>>();

let metadata = ModuleMetadata {
let mut metadata = ModuleMetadata {
compile_info,
prefix: engine_inner.get_prefix(&data),
data_initializers,
Expand Down Expand Up @@ -194,12 +194,13 @@ impl ObjectFileArtifact {
metadata_binary.extend(serialized_data);
let metadata_length = metadata_binary.len();

let (compile_info, symbol_registry) = metadata.split();
let maybe_obj_bytes = compiler.experimental_native_compile_module(
&target,
&metadata.compile_info,
compile_info,
module_translation.as_ref().unwrap(),
&function_body_inputs,
&metadata,
symbol_registry,
&metadata_binary,
);

Expand All @@ -208,7 +209,7 @@ impl ObjectFileArtifact {
} else {
let compilation = compiler.compile_module(
&target,
&metadata.compile_info,
&mut metadata.compile_info,
module_translation.as_ref().unwrap(),
function_body_inputs,
)?;
Expand Down
10 changes: 10 additions & 0 deletions lib/engine-object-file/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ pub struct ModuleMetadata {
pub function_body_lengths: PrimaryMap<LocalFunctionIndex, u64>,
}

impl ModuleMetadata {
pub fn split(&mut self) -> (&mut CompileModuleInfo, &dyn SymbolRegistry) {
let compile_info = &self.compile_info;
let symbol_registry = self as &dyn SymbolRegistry;
#[allow(mutable_transmutes)]
let compile_info = unsafe { std::mem::transmute::<&_, &mut _>(compile_info) };
(compile_info, symbol_registry)
}
}

impl SymbolRegistry for ModuleMetadata {
fn symbol_to_name(&self, symbol: Symbol) -> String {
match symbol {
Expand Down
4 changes: 2 additions & 2 deletions lib/vm/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use wasmer_types::{
TableIndex, TableInitializer, TableType,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ModuleId {
id: usize,
}
Expand All @@ -41,7 +41,7 @@ impl Default for ModuleId {

/// A translated WebAssembly module, excluding the function bodies and
/// memory initializers.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleInfo {
/// A unique identifier (within this process) for this module.
///
Expand Down

0 comments on commit 9d8f315

Please sign in to comment.