diff --git a/Cargo.lock b/Cargo.lock index 0b062168f..1b7b19bc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1220,6 +1220,7 @@ dependencies = [ "hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", "serde-bench 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde_bytes 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/lib/clif-backend/Cargo.toml b/lib/clif-backend/Cargo.toml index 701f821fc..06fd397dc 100644 --- a/lib/clif-backend/Cargo.toml +++ b/lib/clif-backend/Cargo.toml @@ -19,6 +19,7 @@ wasmparser = "0.23.0" byteorder = "1" nix = "0.13.0" libc = "0.2.48" +rayon = "1.0" # Dependencies for caching. [dependencies.serde] diff --git a/lib/clif-backend/src/lib.rs b/lib/clif-backend/src/lib.rs index df2e4e208..c4abf644f 100644 --- a/lib/clif-backend/src/lib.rs +++ b/lib/clif-backend/src/lib.rs @@ -24,6 +24,7 @@ use wasmer_runtime_core::{ #[macro_use] extern crate serde_derive; +extern crate rayon; extern crate serde; use wasmparser::{self, WasmDecoder}; diff --git a/lib/clif-backend/src/resolver.rs b/lib/clif-backend/src/resolver.rs index 604778f01..53b3a789a 100644 --- a/lib/clif-backend/src/resolver.rs +++ b/lib/clif-backend/src/resolver.rs @@ -7,6 +7,7 @@ use crate::{ }, signal::HandlerData, }; +use rayon::prelude::*; use byteorder::{ByteOrder, LittleEndian}; use cranelift_codegen::{ir, isa, Context}; @@ -92,25 +93,45 @@ impl FuncResolverBuilder { function_bodies: Map, info: &ModuleInfo, ) -> CompileResult<(Self, HandlerData)> { - let mut compiled_functions: Vec> = Vec::with_capacity(function_bodies.len()); - let mut local_relocs = Map::with_capacity(function_bodies.len()); - let mut external_relocs = Map::new(); + let num_func_bodies = function_bodies.len(); + let mut compiled_functions: Vec<(Vec, RelocSink)> = Vec::with_capacity(num_func_bodies); + let mut local_relocs = Map::with_capacity(num_func_bodies); + let mut external_relocs = Map::with_capacity(num_func_bodies); let mut trap_sink = TrapSink::new(); - let mut local_trap_sink = LocalTrapSink::new(); - let mut ctx = Context::new(); + let compiled_functions: Result, (RelocSink, LocalTrapSink))>, CompileError> = + function_bodies + .into_vec() + .par_iter() + .map_init( + || Context::new(), + |ctx, func| { + let mut code_buf = Vec::new(); + ctx.func = func.to_owned(); + let mut reloc_sink = RelocSink::new(); + let mut local_trap_sink = LocalTrapSink::new(); + + ctx.compile_and_emit( + isa, + &mut code_buf, + &mut reloc_sink, + &mut local_trap_sink, + ) + .map_err(|e| CompileError::InternalError { msg: e.to_string() })?; + ctx.clear(); + Ok((code_buf, (reloc_sink, local_trap_sink))) + }, + ) + .collect(); + + let compiled_functions = compiled_functions?; let mut total_size = 0; - - for (_, func) in function_bodies { - ctx.func = func; - let mut code_buf = Vec::new(); - let mut reloc_sink = RelocSink::new(); - - ctx.compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut local_trap_sink) - .map_err(|e| CompileError::InternalError { msg: e.to_string() })?; - ctx.clear(); - + // We separate into two iterators, one iterable and one into iterable + let (code_bufs, sinks): (Vec>, Vec<(RelocSink, LocalTrapSink)>) = + compiled_functions.into_iter().unzip(); + for (code_buf, (reloc_sink, mut local_trap_sink)) in code_bufs.iter().zip(sinks.into_iter()) + { // Clear the local trap sink and consolidate all trap info // into a single location. trap_sink.drain_local(total_size, &mut local_trap_sink); @@ -118,7 +139,6 @@ impl FuncResolverBuilder { // Round up each function's size to pointer alignment. total_size += round_up(code_buf.len(), mem::size_of::()); - compiled_functions.push(code_buf); local_relocs.push(reloc_sink.local_relocs.into_boxed_slice()); external_relocs.push(reloc_sink.external_relocs.into_boxed_slice()); } @@ -145,10 +165,10 @@ impl FuncResolverBuilder { *i = 0xCC; } - let mut map = Map::with_capacity(compiled_functions.len()); + let mut map = Map::with_capacity(num_func_bodies); let mut previous_end = 0; - for compiled in compiled_functions.iter() { + for compiled in code_bufs.iter() { let new_end = previous_end + round_up(compiled.len(), mem::size_of::()); unsafe { memory.as_slice_mut()[previous_end..previous_end + compiled.len()] diff --git a/lib/runtime-core/src/structures/map.rs b/lib/runtime-core/src/structures/map.rs index d7177c427..add5b0257 100644 --- a/lib/runtime-core/src/structures/map.rs +++ b/lib/runtime-core/src/structures/map.rs @@ -56,6 +56,10 @@ where pub fn into_boxed_map(self) -> BoxedMap { BoxedMap::new(self.elems.into_boxed_slice()) } + + pub fn into_vec(self) -> Vec { + self.elems + } } impl Map