Skip to content

Commit

Permalink
Merge #2264
Browse files Browse the repository at this point in the history
2264: Middleware refactor r=syrusakbary a=syrusakbary

<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description
In order to be able to refactor the LLVM experimental compiler, receiving a callback instead of a serialized module metadata, we need to make compilers act on non-mutable input.
This is something that compilers should do, so this PR moves towards the scenario of pure compilation.


# Review

- [ ] Add a short description of the change to the CHANGELOG.md file


Co-authored-by: Syrus Akbary <[email protected]>
  • Loading branch information
bors[bot] and syrusakbary authored Apr 27, 2021
2 parents d470083 + 47be919 commit ea82c68
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 104 deletions.
8 changes: 4 additions & 4 deletions lib/compiler-cranelift/src/address_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

use cranelift_codegen::machinst::buffer::MachSrcLoc;
use cranelift_codegen::{isa, Context};
use wasmer_compiler::{FunctionAddressMap, FunctionBodyData, InstructionAddressMap, SourceLoc};
use wasmer_compiler::{wasmparser::Range, FunctionAddressMap, InstructionAddressMap, SourceLoc};

pub fn get_function_address_map<'data>(
context: &Context,
data: &FunctionBodyData<'data>,
range: Range,
body_len: usize,
isa: &dyn isa::TargetIsa,
) -> FunctionAddressMap {
Expand Down Expand Up @@ -44,8 +44,8 @@ pub fn get_function_address_map<'data>(
// Generate artificial srcloc for function start/end to identify boundary
// within module. Similar to FuncTranslator::cur_srcloc(): it will wrap around
// if byte code is larger than 4 GB.
let start_srcloc = SourceLoc::new(data.module_offset as u32);
let end_srcloc = SourceLoc::new((data.module_offset + data.data.len()) as u32);
let start_srcloc = SourceLoc::new(range.start as u32);
let end_srcloc = SourceLoc::new(range.end as u32);

FunctionAddressMap {
instructions,
Expand Down
29 changes: 19 additions & 10 deletions lib/compiler-cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ 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, FunctionBinaryReader, FunctionBody,
FunctionBodyData, MiddlewareBinaryReader, ModuleMiddleware, ModuleMiddlewareChain,
SectionIndex,
};
use wasmer_types::entity::{EntityRef, PrimaryMap};
use wasmer_types::{FunctionIndex, LocalFunctionIndex, SignatureIndex};
Expand All @@ -51,22 +52,24 @@ impl CraneliftCompiler {
}

impl Compiler for CraneliftCompiler {
/// Get the middlewares for this compiler
fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.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 Expand Up @@ -118,15 +121,20 @@ impl Compiler for CraneliftCompiler {
// if generate_debug_info {
// context.func.collect_debug_info();
// }
let mut reader =
MiddlewareBinaryReader::new_with_offset(input.data, input.module_offset);
reader.set_middleware_chain(
self.config
.middlewares
.generate_function_middleware_chain(*i),
);

func_translator.translate(
module_translation_state,
input.data,
input.module_offset,
&mut reader,
&mut context.func,
&mut func_env,
*i,
&self.config,
)?;

let mut code_buf: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -172,7 +180,8 @@ impl Compiler for CraneliftCompiler {
other => other.maybe_into_to_windows_unwind(),
};

let address_map = get_function_address_map(&context, input, code_buf.len(), &*isa);
let range = reader.range();
let address_map = get_function_address_map(&context, range, code_buf.len(), &*isa);

// We transform the Cranelift JumpTable's into compiler JumpTables
let func_jt_offsets = transform_jump_table(context.func.jt_offsets);
Expand Down
30 changes: 10 additions & 20 deletions lib/compiler-cranelift/src/translator/func_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ use super::code_translator::{bitcast_arguments, translate_operator, wasm_param_t
use super::func_environ::{FuncEnvironment, ReturnMode};
use super::func_state::FuncTranslationState;
use super::translation_utils::get_vmctx_value_label;
use crate::config::Cranelift;
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::{self, Block, InstBuilder, ValueLabel};
use cranelift_codegen::timing;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
use tracing::info;
use wasmer_compiler::wasmparser;
use wasmer_compiler::{
wasm_unsupported, wptype_to_type, MiddlewareBinaryReader, ModuleMiddlewareChain,
ModuleTranslationState, WasmResult,
wasm_unsupported, wptype_to_type, FunctionBinaryReader, ModuleTranslationState, WasmResult,
};
use wasmer_types::LocalFunctionIndex;

Expand Down Expand Up @@ -64,28 +62,20 @@ impl FuncTranslator {
pub fn translate<FE: FuncEnvironment + ?Sized>(
&mut self,
module_translation_state: &ModuleTranslationState,
code: &[u8],
code_offset: usize,
reader: &mut dyn FunctionBinaryReader,
func: &mut ir::Function,
environ: &mut FE,
local_function_index: LocalFunctionIndex,
config: &Cranelift,
) -> WasmResult<()> {
let mut reader = MiddlewareBinaryReader::new_with_offset(code, code_offset);
reader.set_middleware_chain(
config
.middlewares
.generate_function_middleware_chain(local_function_index),
);
environ.push_params_on_stack(local_function_index);
self.translate_from_reader(module_translation_state, reader, func, environ)
}

/// Translate a binary WebAssembly function from a `MiddlewareBinaryReader`.
/// Translate a binary WebAssembly function from a `FunctionBinaryReader`.
pub fn translate_from_reader<FE: FuncEnvironment + ?Sized>(
&mut self,
module_translation_state: &ModuleTranslationState,
mut reader: MiddlewareBinaryReader,
reader: &mut dyn FunctionBinaryReader,
func: &mut ir::Function,
environ: &mut FE,
) -> WasmResult<()> {
Expand All @@ -101,7 +91,7 @@ impl FuncTranslator {

// This clears the `FunctionBuilderContext`.
let mut builder = FunctionBuilder::new(func, &mut self.func_ctx);
builder.set_srcloc(cur_srcloc(&reader));
builder.set_srcloc(cur_srcloc(reader));
let entry_block = builder.create_block();
builder.append_block_params_for_function_params(entry_block);
builder.switch_to_block(entry_block); // This also creates values for the arguments.
Expand All @@ -119,7 +109,7 @@ impl FuncTranslator {
builder.append_block_params_for_function_returns(exit_block);
self.state.initialize(&builder.func.signature, exit_block);

parse_local_decls(&mut reader, &mut builder, num_params, environ)?;
parse_local_decls(reader, &mut builder, num_params, environ)?;
parse_function_body(
module_translation_state,
reader,
Expand Down Expand Up @@ -169,7 +159,7 @@ fn declare_wasm_parameters<FE: FuncEnvironment + ?Sized>(
///
/// Declare local variables, starting from `num_params`.
fn parse_local_decls<FE: FuncEnvironment + ?Sized>(
reader: &mut MiddlewareBinaryReader,
reader: &mut dyn FunctionBinaryReader,
builder: &mut FunctionBuilder,
num_params: usize,
environ: &mut FE,
Expand Down Expand Up @@ -231,7 +221,7 @@ fn declare_locals<FE: FuncEnvironment + ?Sized>(
/// arguments and locals are declared in the builder.
fn parse_function_body<FE: FuncEnvironment + ?Sized>(
module_translation_state: &ModuleTranslationState,
mut reader: MiddlewareBinaryReader,
reader: &mut dyn FunctionBinaryReader,
builder: &mut FunctionBuilder,
state: &mut FuncTranslationState,
environ: &mut FE,
Expand All @@ -241,7 +231,7 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(

// Keep going until the final `End` operator which pops the outermost block.
while !state.control_stack.is_empty() {
builder.set_srcloc(cur_srcloc(&reader));
builder.set_srcloc(cur_srcloc(reader));
let op = reader.read_operator()?;
environ.before_translate_operator(&op, builder, state)?;
translate_operator(module_translation_state, &op, builder, state, environ)?;
Expand Down Expand Up @@ -299,7 +289,7 @@ fn parse_function_body<FE: FuncEnvironment + ?Sized>(
}

/// Get the current source location from a reader.
fn cur_srcloc(reader: &MiddlewareBinaryReader) -> ir::SourceLoc {
fn cur_srcloc(reader: &dyn FunctionBinaryReader) -> ir::SourceLoc {
// We record source locations as byte code offsets relative to the beginning of the file.
// This will wrap around if byte code is larger than 4 GB.
ir::SourceLoc::new(reader.original_position() as u32)
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) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.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
4 changes: 2 additions & 2 deletions lib/compiler-llvm/src/translator/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use crate::config::{CompiledKind, LLVM};
use crate::object_file::{load_object_file, CompiledFunction};
use wasmer_compiler::wasmparser::{MemoryImmediate, Operator};
use wasmer_compiler::{
wptype_to_type, CompileError, FunctionBodyData, MiddlewareBinaryReader, ModuleMiddlewareChain,
ModuleTranslationState, RelocationTarget, Symbol, SymbolRegistry,
wptype_to_type, CompileError, FunctionBinaryReader, FunctionBodyData, MiddlewareBinaryReader,
ModuleMiddlewareChain, ModuleTranslationState, RelocationTarget, Symbol, SymbolRegistry,
};
use wasmer_types::entity::PrimaryMap;
use wasmer_types::{
Expand Down
14 changes: 8 additions & 6 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::TrapInformation;
use wasmer_compiler::{
Architecture, CompileModuleInfo, CompilerConfig, MiddlewareBinaryReader, ModuleMiddlewareChain,
ModuleTranslationState, OperatingSystem, Target,
Architecture, CompileModuleInfo, CompilerConfig, FunctionBinaryReader, MiddlewareBinaryReader,
ModuleMiddleware, ModuleMiddlewareChain, ModuleTranslationState, OperatingSystem, Target,
};
use wasmer_compiler::{Compilation, CompileError, CompiledFunction, Compiler, SectionIndex};
use wasmer_compiler::{FunctionBody, FunctionBodyData};
Expand Down Expand Up @@ -42,12 +42,17 @@ impl SinglepassCompiler {
}

impl Compiler for SinglepassCompiler {
/// Get the middlewares for this compiler
fn get_middlewares(&self) -> &[Arc<dyn ModuleMiddleware>] {
&self.config.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 @@ -64,9 +69,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
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
7 changes: 5 additions & 2 deletions lib/compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,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 @@ -107,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 @@ -117,6 +117,9 @@ pub trait Compiler: Send + MemoryUsage {
) -> Option<Result<Vec<u8>, CompileError>> {
None
}

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

/// The kinds of wasmer_types objects that might be found in a native object file.
Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ pub use crate::target::{
};
#[cfg(feature = "translator")]
pub use crate::translator::{
translate_module, wptype_to_type, FunctionBodyData, FunctionMiddleware, MiddlewareBinaryReader,
MiddlewareReaderState, ModuleEnvironment, ModuleInfoTranslation, ModuleMiddleware,
ModuleMiddlewareChain, ModuleTranslationState,
translate_module, wptype_to_type, FunctionBinaryReader, FunctionBodyData, FunctionMiddleware,
MiddlewareBinaryReader, MiddlewareReaderState, ModuleEnvironment, ModuleInfoTranslation,
ModuleMiddleware, ModuleMiddlewareChain, ModuleTranslationState,
};
pub use crate::trap::TrapInformation;
pub use crate::unwind::CompiledFunctionUnwindInfo;
Expand Down
Loading

0 comments on commit ea82c68

Please sign in to comment.