Skip to content

Commit

Permalink
Make compilers to take non-mutable inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Apr 27, 2021
1 parent ae20af8 commit 2df5854
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 46 deletions.
14 changes: 8 additions & 6 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use wasmer_compiler::CompileError;
use wasmer_compiler::{CallingConvention, ModuleTranslationState, Target};
use wasmer_compiler::{
Compilation, CompileModuleInfo, CompiledFunction, CompiledFunctionFrameInfo,
CompiledFunctionUnwindInfo, Compiler, Dwarf, FunctionBody, FunctionBodyData,
ModuleMiddlewareChain, SectionIndex,
CompiledFunctionUnwindInfo, Compiler, Dwarf, FunctionBody, FunctionBodyData, ModuleMiddleware,
SectionIndex,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
Expand All @@ -51,22 +51,24 @@ impl CraneliftCompiler {
}

impl Compiler for CraneliftCompiler {
/// Get the middlewares for this compiler
fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.config.get_middlewares()
}

/// Compile the module using Cranelift, producing a compilation result with
/// associated relocations.
fn compile_module(
&self,
target: &Target,
compile_info: &mut CompileModuleInfo,
compile_info: &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 Down
9 changes: 5 additions & 4 deletions lib/compiler-cranelift/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ impl Cranelift {

settings::Flags::new(flags)
}

/// Get Middlewares
pub fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl CompilerConfig for Cranelift {
Expand All @@ -206,10 +211,6 @@ impl CompilerConfig for Cranelift {
fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>) {
self.middlewares.push(middleware);
}

fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl Default for Cranelift {
Expand Down
18 changes: 8 additions & 10 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIter
use std::sync::Arc;
use wasmer_compiler::{
Compilation, CompileError, CompileModuleInfo, Compiler, CustomSection, CustomSectionProtection,
Dwarf, FunctionBodyData, ModuleMiddlewareChain, ModuleTranslationState, RelocationTarget,
Dwarf, FunctionBodyData, ModuleMiddleware, ModuleTranslationState, RelocationTarget,
SectionBody, SectionIndex, Symbol, SymbolRegistry, Target,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
Expand Down Expand Up @@ -188,21 +188,22 @@ impl LLVMCompiler {
}

impl Compiler for LLVMCompiler {
/// Get the middlewares for this compiler
fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.config.get_middlewares()
}

fn experimental_native_compile_module<'data, 'module>(
&self,
target: &Target,
compile_info: &'module mut CompileModuleInfo,
compile_info: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: &PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
symbol_registry: &dyn SymbolRegistry,
// The metadata to inject into the wasmer_metadata section of the object file.
wasmer_metadata: &[u8],
) -> Option<Result<Vec<u8>, CompileError>> {
let mut module = (*compile_info.module).clone();
self.config.middlewares.apply_on_module_info(&mut module);
compile_info.module = Arc::new(module);

Some(self.compile_native_object(
target,
compile_info,
Expand All @@ -218,17 +219,14 @@ impl Compiler for LLVMCompiler {
fn compile_module<'data, 'module>(
&self,
target: &Target,
compile_info: &'module mut CompileModuleInfo,
compile_info: &'module 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 Down
9 changes: 5 additions & 4 deletions lib/compiler-llvm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ impl LLVM {
)
.unwrap()
}

/// Get Middlewares
pub fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl CompilerConfig for LLVM {
Expand All @@ -218,10 +223,6 @@ impl CompilerConfig for LLVM {
fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>) {
self.middlewares.push(middleware);
}

fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl Default for LLVM {
Expand Down
10 changes: 6 additions & 4 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ impl SinglepassCompiler {
}

impl Compiler for SinglepassCompiler {
/// Get the middlewares for this compiler
fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.config.get_middlewares()
}

/// Compile the module using Singlepass, producing a compilation result with
/// associated relocations.
fn compile_module(
&self,
target: &Target,
compile_info: &mut CompileModuleInfo,
compile_info: &CompileModuleInfo,
_module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'_>>,
) -> Result<Compilation, CompileError> {
Expand All @@ -63,9 +68,6 @@ impl Compiler for SinglepassCompiler {
}
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 vmoffsets = VMOffsets::new(8, &compile_info.module);
let module = &compile_info.module;
let import_trampolines: PrimaryMap<SectionIndex, _> = (0..module.num_imported_functions)
Expand Down
9 changes: 5 additions & 4 deletions lib/compiler-singlepass/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl Singlepass {
self.enable_nan_canonicalization = enable;
self
}

/// Get Middlewares
pub fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl CompilerConfig for Singlepass {
Expand All @@ -70,10 +75,6 @@ impl CompilerConfig for Singlepass {
fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>) {
self.middlewares.push(middleware);
}

fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>> {
self.middlewares.clone()
}
}

impl Default for Singlepass {
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 mut CompileModuleInfo,
compile_info: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
Expand Down
10 changes: 5 additions & 5 deletions lib/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ pub trait CompilerConfig {

/// Pushes a middleware onto the back of the middleware chain.
fn push_middleware(&mut self, middleware: Arc<dyn ModuleMiddleware>);

/// Get the middlewares for this compiler
fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>>;
}

impl<T> From<T> for Box<dyn CompilerConfig + 'static>
Expand Down Expand Up @@ -98,7 +95,7 @@ pub trait Compiler: Send + MemoryUsage {
fn compile_module<'data, 'module>(
&self,
target: &Target,
module: &'module mut CompileModuleInfo,
module: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState,
// The list of function bodies
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
Expand All @@ -110,7 +107,7 @@ pub trait Compiler: Send + MemoryUsage {
fn experimental_native_compile_module<'data, 'module>(
&self,
_target: &Target,
_module: &'module mut CompileModuleInfo,
_module: &'module CompileModuleInfo,
_module_translation: &ModuleTranslationState,
// The list of function bodies
_function_body_inputs: &PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
Expand All @@ -120,6 +117,9 @@ pub trait Compiler: Send + MemoryUsage {
) -> Option<Result<Vec<u8>, CompileError>> {
None
}

/// Get the middlewares for this compiler
fn get_middlewares(&self) -> Vec<Arc<dyn ModuleMiddleware>>;
}

/// The kinds of wasmer_types objects that might be found in a native object file.
Expand Down
9 changes: 7 additions & 2 deletions lib/engine-jit/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use loupe::MemoryUsage;
use std::sync::{Arc, Mutex};
use wasmer_compiler::{CompileError, Features, Triple};
#[cfg(feature = "compiler")]
use wasmer_compiler::{CompileModuleInfo, ModuleEnvironment};
use wasmer_compiler::{CompileModuleInfo, ModuleEnvironment, ModuleMiddlewareChain};
use wasmer_engine::{
register_frame_info, Artifact, DeserializeError, FunctionExtent, GlobalFrameInfoRegistration,
SerializeError,
Expand Down Expand Up @@ -84,10 +84,15 @@ impl JITArtifact {

let compiler = inner_jit.compiler()?;

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

// Compile the Module
let compilation = compiler.compile_module(
&jit.target(),
&mut compile_info,
&compile_info,
// SAFETY: Calling `unwrap` is correct since
// `environ.translate()` above will write some data into
// `module_translation_state`.
Expand Down
13 changes: 10 additions & 3 deletions lib/engine-native/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use tracing::trace;
use wasmer_compiler::{CompileError, Features, OperatingSystem, Symbol, SymbolRegistry, Triple};
#[cfg(feature = "compiler")]
use wasmer_compiler::{
CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleTranslationState,
CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleMiddlewareChain,
ModuleTranslationState,
};
use wasmer_engine::{Artifact, DeserializeError, InstantiationError, SerializeError};
#[cfg(feature = "compiler")]
Expand Down Expand Up @@ -196,9 +197,15 @@ impl NativeArtifact {
metadata_binary.extend(serialized_data);

let (mut compile_info, symbol_registry) = metadata.split();

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

let maybe_obj_bytes = compiler.experimental_native_compile_module(
&target,
&mut compile_info,
&compile_info,
module_translation.as_ref().unwrap(),
&function_body_inputs,
&symbol_registry,
Expand All @@ -222,7 +229,7 @@ impl NativeArtifact {
None => {
let compilation = compiler.compile_module(
&target,
&mut compile_info,
&compile_info,
module_translation.as_ref().unwrap(),
function_body_inputs,
)?;
Expand Down
13 changes: 10 additions & 3 deletions lib/engine-object-file/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use std::sync::Arc;
use wasmer_compiler::{CompileError, Features, OperatingSystem, SymbolRegistry, Triple};
#[cfg(feature = "compiler")]
use wasmer_compiler::{
CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleTranslationState,
CompileModuleInfo, FunctionBodyData, ModuleEnvironment, ModuleMiddlewareChain,
ModuleTranslationState,
};
use wasmer_engine::{Artifact, DeserializeError, InstantiationError, SerializeError};
#[cfg(feature = "compiler")]
Expand Down Expand Up @@ -203,9 +204,15 @@ impl ObjectFileArtifact {
let metadata_length = metadata_binary.len();

let (compile_info, symbol_registry) = metadata.split();

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

let maybe_obj_bytes = compiler.experimental_native_compile_module(
&target,
compile_info,
&compile_info,
module_translation.as_ref().unwrap(),
&function_body_inputs,
&symbol_registry,
Expand All @@ -217,7 +224,7 @@ impl ObjectFileArtifact {
} else {
let compilation = compiler.compile_module(
&target,
&mut metadata.compile_info,
&metadata.compile_info,
module_translation.as_ref().unwrap(),
function_body_inputs,
)?;
Expand Down

0 comments on commit 2df5854

Please sign in to comment.