Parallelize the compilation loop with Rayon (#58)

This commit is contained in:
Max McDonnell
2019-03-04 20:27:57 -05:00
committed by Dan Gohman
parent 04d4b20df4
commit 6eb09d9edd
2 changed files with 34 additions and 22 deletions

View File

@@ -19,6 +19,7 @@ cast = { version = "0.2.2", default-features = false }
failure = { version = "0.1.3", default-features = false } failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false } failure_derive = { version = "0.1.3", default-features = false }
indexmap = "1.0.2" indexmap = "1.0.2"
rayon = "1.0"
[features] [features]
default = ["std"] default = ["std"]

View File

@@ -13,6 +13,7 @@ use cranelift_codegen::isa;
use cranelift_codegen::Context; use cranelift_codegen::Context;
use cranelift_entity::PrimaryMap; use cranelift_entity::PrimaryMap;
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator}; use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::vec::Vec; use std::vec::Vec;
/// Implementation of a relocation sink that just saves all the information for later /// Implementation of a relocation sink that just saves all the information for later
@@ -89,30 +90,40 @@ pub fn compile_module<'data, 'module>(
) -> Result<(Compilation, Relocations), CompileError> { ) -> Result<(Compilation, Relocations), CompileError> {
let mut functions = PrimaryMap::with_capacity(function_body_inputs.len()); let mut functions = PrimaryMap::with_capacity(function_body_inputs.len());
let mut relocations = PrimaryMap::with_capacity(function_body_inputs.len()); let mut relocations = PrimaryMap::with_capacity(function_body_inputs.len());
for (i, input) in function_body_inputs.into_iter() {
let func_index = module.func_index(i);
let mut context = Context::new();
context.func.name = get_func_name(func_index);
context.func.signature = module.signatures[module.functions[func_index]].clone();
let mut trans = FuncTranslator::new(); function_body_inputs
trans .into_iter()
.translate( .collect::<Vec<(DefinedFuncIndex, &&'data [u8])>>()
input, .par_iter()
&mut context.func, .map(|(i, input)| {
&mut FuncEnvironment::new(isa.frontend_config(), module), let func_index = module.func_index(*i);
) let mut context = Context::new();
.map_err(CompileError::Wasm)?; context.func.name = get_func_name(func_index);
context.func.signature = module.signatures[module.functions[func_index]].clone();
let mut code_buf: Vec<u8> = Vec::new(); let mut trans = FuncTranslator::new();
let mut reloc_sink = RelocSink::new(); trans
let mut trap_sink = binemit::NullTrapSink {}; .translate(
context input,
.compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut trap_sink) &mut context.func,
.map_err(CompileError::Codegen)?; &mut FuncEnvironment::new(isa.frontend_config(), module),
functions.push(code_buf); )
relocations.push(reloc_sink.func_relocs); .map_err(CompileError::Wasm)?;
}
let mut code_buf: Vec<u8> = Vec::new();
let mut reloc_sink = RelocSink::new();
let mut trap_sink = binemit::NullTrapSink {};
context
.compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut trap_sink)
.map_err(CompileError::Codegen)?;
Ok((code_buf, reloc_sink.func_relocs))
})
.collect::<Result<Vec<_>, CompileError>>()?
.into_iter()
.for_each(|(function, relocs)| {
functions.push(function);
relocations.push(relocs);
});
// TODO: Reorganize where we create the Vec for the resolved imports. // TODO: Reorganize where we create the Vec for the resolved imports.
Ok((Compilation::new(functions), relocations)) Ok((Compilation::new(functions), relocations))