Skip to content

Commit

Permalink
Implement caching for parser refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed May 6, 2019
1 parent 4770277 commit 0926a50
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 39 deletions.
29 changes: 23 additions & 6 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use inkwell::{
use smallvec::SmallVec;
use std::sync::Arc;
use wasmer_runtime_core::{
backend::Backend,
backend::{Backend, CacheGen, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
memory::MemoryType,
module::ModuleInfo,
module::{ModuleInfo, ModuleInner},
structures::{Map, SliceMap, TypedIndex},
types::{
FuncIndex, FuncSig, GlobalIndex, LocalFuncIndex, LocalOrImport, MemoryIndex, SigIndex,
Expand All @@ -25,7 +26,7 @@ use wasmparser::{
Type as WpType,
};

use crate::backend::LLVMBackend;
use crate::backend::{LLVMBackend, LLVMCache};
use crate::intrinsics::{CtxType, GlobalCache, Intrinsics, MemoryCache};
use crate::read_info::type_to_type;
use crate::state::{ControlFrame, IfElseState, State};
Expand Down Expand Up @@ -4640,7 +4641,10 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
Ok(self.functions.last_mut().unwrap())
}

fn finalize(self, module_info: &ModuleInfo) -> Result<LLVMBackend, CodegenError> {
fn finalize(
self,
module_info: &ModuleInfo,
) -> Result<(LLVMBackend, Box<dyn CacheGen>), CodegenError> {
// self.module.print_to_stderr();

generate_trampolines(
Expand Down Expand Up @@ -4668,8 +4672,8 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>

// self.module.print_to_stderr();

let (backend, _cache_gen) = LLVMBackend::new(self.module, self.intrinsics);
Ok(backend)
let (backend, cache_gen) = LLVMBackend::new(self.module, self.intrinsics);
Ok((backend, Box::new(cache_gen)))
}

fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), CodegenError> {
Expand All @@ -4693,4 +4697,17 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
self.func_import_count += 1;
Ok(())
}

unsafe fn from_cache(artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
let (info, _, memory) = artifact.consume();
let (backend, cache_gen) =
LLVMBackend::from_buffer(memory).map_err(CacheError::DeserializeError)?;

Ok(ModuleInner {
runnable_module: Box::new(backend),
cache_gen: Box::new(cache_gen),

info,
})
}
}
37 changes: 16 additions & 21 deletions lib/runtime-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,

/// Creates a new function and returns the function-scope code generator for it.
fn next_function(&mut self) -> Result<&mut FCG, E>;
fn finalize(self, module_info: &ModuleInfo) -> Result<RM, E>;
fn finalize(self, module_info: &ModuleInfo) -> Result<(RM, Box<dyn CacheGen>), E>;
fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), E>;

/// Sets function signatures.
fn feed_function_signatures(&mut self, assoc: Map<FuncIndex, SigIndex>) -> Result<(), E>;

/// Adds an import function.
fn feed_import_function(&mut self) -> Result<(), E>;

unsafe fn from_cache(cache: Artifact, _: Token) -> Result<ModuleInner, CacheError>;
}

pub struct StreamingCompiler<
Expand Down Expand Up @@ -131,15 +133,6 @@ impl<
compiler_config: CompilerConfig,
_: Token,
) -> CompileResult<ModuleInner> {
struct Placeholder;
impl CacheGen for Placeholder {
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), CacheError> {
Err(CacheError::Unknown(
"the streaming compiler API doesn't support caching yet".to_string(),
))
}
}

let mut mcg = MCG::new();
let mut chain = (self.middleware_chain_generator)();
let info = crate::parse::read_module(
Expand All @@ -149,22 +142,24 @@ impl<
&mut chain,
&compiler_config,
)?;
let exec_context = mcg
.finalize(&info)
.map_err(|x| CompileError::InternalError {
msg: format!("{:?}", x),
})?;
let (exec_context, cache_gen) =
mcg.finalize(&info)
.map_err(|x| CompileError::InternalError {
msg: format!("{:?}", x),
})?;
Ok(ModuleInner {
cache_gen: Box::new(Placeholder),
cache_gen,
runnable_module: Box::new(exec_context),
info: info,
info,
})
}

unsafe fn from_cache(&self, _artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
Err(CacheError::Unknown(
"the streaming compiler API doesn't support caching yet".to_string(),
))
unsafe fn from_cache(
&self,
artifact: Artifact,
token: Token,
) -> Result<ModuleInner, CacheError> {
MCG::from_cache(artifact, token)
}
}

Expand Down
46 changes: 34 additions & 12 deletions lib/singlepass-backend/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use smallvec::SmallVec;
use std::ptr::NonNull;
use std::{any::Any, collections::HashMap, sync::Arc};
use wasmer_runtime_core::{
backend::{Backend, RunnableModule},
backend::{sys::Memory, Backend, CacheGen, RunnableModule, Token},
cache::{Artifact, Error as CacheError},
codegen::*,
memory::MemoryType,
module::ModuleInfo,
module::{ModuleInfo, ModuleInner},
structures::{Map, TypedIndex},
typed_func::Wasm,
types::{
Expand Down Expand Up @@ -349,7 +350,10 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
Ok(self.functions.last_mut().unwrap())
}

fn finalize(mut self, _: &ModuleInfo) -> Result<X64ExecutionContext, CodegenError> {
fn finalize(
mut self,
_: &ModuleInfo,
) -> Result<(X64ExecutionContext, Box<dyn CacheGen>), CodegenError> {
let (assembler, mut br_table_data, breakpoints) = match self.functions.last_mut() {
Some(x) => (
x.assembler.take().unwrap(),
Expand Down Expand Up @@ -404,15 +408,27 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
.collect(),
);

Ok(X64ExecutionContext {
code: output,
functions: self.functions,
signatures: self.signatures.as_ref().unwrap().clone(),
_br_table_data: br_table_data,
breakpoints: breakpoints,
func_import_count: self.func_import_count,
function_pointers: out_labels,
})
struct Placeholder;
impl CacheGen for Placeholder {
fn generate_cache(&self) -> Result<(Box<[u8]>, Memory), CacheError> {
Err(CacheError::Unknown(
"the singlepass backend doesn't support caching yet".to_string(),
))
}
}

Ok((
X64ExecutionContext {
code: output,
functions: self.functions,
signatures: self.signatures.as_ref().unwrap().clone(),
_br_table_data: br_table_data,
breakpoints: breakpoints,
func_import_count: self.func_import_count,
function_pointers: out_labels,
},
Box::new(Placeholder),
))
}

fn feed_signatures(&mut self, signatures: Map<SigIndex, FuncSig>) -> Result<(), CodegenError> {
Expand Down Expand Up @@ -461,6 +477,12 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>

Ok(())
}

unsafe fn from_cache(artifact: Artifact, _: Token) -> Result<ModuleInner, CacheError> {
Err(CacheError::Unknown(
"the singlepass compiler API doesn't support caching yet".to_string(),
))
}
}

impl X64FunctionCode {
Expand Down

0 comments on commit 0926a50

Please sign in to comment.