Skip to content

Commit

Permalink
Plumb parallel_compilation config to compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Aug 9, 2021
1 parent e8b5282 commit 48a6be8
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 39 deletions.
3 changes: 2 additions & 1 deletion crates/cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ impl<'config> ModuleCacheEntry<'config> {
}

/// Gets cached data if state matches, otherwise calls the `compute`.
pub fn get_data<T, U, E>(&self, state: T, compute: fn(T) -> Result<U, E>) -> Result<U, E>
pub fn get_data<T, U, E, F>(&self, state: T, compute: F) -> Result<U, E>
where
T: Hash,
U: Serialize + for<'a> Deserialize<'a>,
F: FnOnce(T) -> Result<U, E>,
{
let inner = match &self.0 {
Some(inner) => inner,
Expand Down
40 changes: 20 additions & 20 deletions crates/cache/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ fn test_write_read_cache() {
let entry1 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler1, &cache_config));
let entry2 = ModuleCacheEntry::from_inner(ModuleCacheEntryInner::new(compiler2, &cache_config));

entry1.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();

entry1.get_data::<_, i32, i32>(2, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(2, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(2, |_| panic!()).unwrap();

entry1.get_data::<_, i32, i32>(3, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(3, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(3, |_| panic!()).unwrap();

entry1.get_data::<_, i32, i32>(4, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(4, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(3, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(4, |_| panic!()).unwrap();

entry2.get_data::<_, i32, i32>(1, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(3, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32>(4, |_| panic!()).unwrap();
entry2.get_data::<_, i32, i32>(1, |_| panic!()).unwrap();
entry2.get_data::<_, i32, i32, _>(1, |_| Ok(100)).unwrap();
entry1.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(2, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(3, |_| panic!()).unwrap();
entry1.get_data::<_, i32, i32, _>(4, |_| panic!()).unwrap();
entry2.get_data::<_, i32, i32, _>(1, |_| panic!()).unwrap();
}
8 changes: 5 additions & 3 deletions crates/jit/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ impl Compiler {
&self,
translation: &mut ModuleTranslation,
types: &TypeTables,
#[cfg(feature = "parallel-compilation")] parallel_compilation: bool,
) -> Result<Compilation, SetupError> {
let functions = mem::take(&mut translation.function_body_inputs);
let functions = functions.into_iter().collect::<Vec<_>>();
let funcs = maybe_parallel!(functions.(into_iter | into_par_iter))
.map(|(index, func)| {
let funcs = maybe_parallel!(parallel_compilation, functions.(into_iter | into_par_iter), iter => {
iter.map(|(index, func)| {
self.compiler.compile_function(
translation,
index,
Expand All @@ -150,7 +151,8 @@ impl Compiler {
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.collect::<CompiledFunctions>();
.collect::<CompiledFunctions>()
});

let dwarf_sections = if self.tunables.generate_native_debuginfo && !funcs.is_empty() {
transform_dwarf_data(
Expand Down
26 changes: 19 additions & 7 deletions crates/jit/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,15 @@ impl CompilationArtifacts {
/// The `use_paged_init` argument controls whether or not an attempt is made to
/// organize linear memory initialization data as entire pages or to leave
/// the memory initialization data as individual segments.
///
/// The `prefer_parallel_compilation` is used to control whether compilation should be performed
/// using multiple threads or not. This has only an effect if the feature `parallel-compilation`
/// is enabled.
pub fn build(
compiler: &Compiler,
data: &[u8],
use_paged_mem_init: bool,
#[cfg(feature = "parallel-compilation")] parallel_compilation: bool,
) -> Result<(usize, Vec<CompilationArtifacts>, TypeTables), SetupError> {
let (main_module, translations, types) = ModuleEnvironment::new(
compiler.frontend_config(),
Expand All @@ -112,13 +117,18 @@ impl CompilationArtifacts {
.translate(data)
.map_err(|error| SetupError::Compile(CompileError::Wasm(error)))?;

let list = maybe_parallel!(translations.(into_iter | into_par_iter))
.map(|mut translation| {
let list = maybe_parallel!(parallel_compilation, translations.(into_iter | into_par_iter), iter => {
iter.map(|mut translation| {
let Compilation {
obj,
unwind_info,
funcs,
} = compiler.compile(&mut translation, &types)?;
} = compiler.compile(
&mut translation,
&types,
#[cfg(feature = "parallel-compilation")]
parallel_compilation,
)?;

let ModuleTranslation {
mut module,
Expand Down Expand Up @@ -160,7 +170,8 @@ impl CompilationArtifacts {
has_unparsed_debuginfo,
})
})
.collect::<Result<Vec<_>, SetupError>>()?;
.collect::<Result<Vec<_>, SetupError>>()?
});
Ok((
main_module,
list,
Expand Down Expand Up @@ -226,10 +237,11 @@ impl CompiledModule {
artifacts: Vec<CompilationArtifacts>,
isa: &dyn TargetIsa,
profiler: &dyn ProfilingAgent,
#[cfg(feature = "parallel-compilation")] parallel_compilation: bool,
) -> Result<Vec<Arc<Self>>, SetupError> {
maybe_parallel!(artifacts.(into_iter | into_par_iter))
.map(|a| CompiledModule::from_artifacts(a, isa, profiler))
.collect()
maybe_parallel!(parallel_compilation, artifacts.(into_iter | into_par_iter), iter => {
iter.map(|a| CompiledModule::from_artifacts(a, isa, profiler)).collect()
})
}

/// Creates `CompiledModule` directly from `CompilationArtifacts`.
Expand Down
19 changes: 13 additions & 6 deletions crates/jit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@

#[cfg(feature = "parallel-compilation")]
macro_rules! maybe_parallel {
($e:ident.($serial:ident | $parallel:ident)) => {
$e.$parallel()
};
($condition:ident, $e:ident.($serial:ident | $parallel:ident), $iter_name:ident => { $body:expr }) => {{
if $condition {
let $iter_name = $e.$parallel();
$body
} else {
let $iter_name = $e.$serial();
$body
}
}};
}

#[cfg(not(feature = "parallel-compilation"))]
macro_rules! maybe_parallel {
($e:ident.($serial:ident | $parallel:ident)) => {
$e.$serial()
};
($condition:ident, $e:ident.($serial:ident | $parallel:ident), $iter_name:ident => { $body:expr }) => {{
let $iter_name = $e.$serial();
$body
}};
}

mod code_memory;
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ impl Engine {
&self.inner.compiler,
&bytes,
USE_PAGED_MEM_INIT,
#[cfg(feature = "parallel-compilation")]
self.inner.config.parallel_compilation,
)?;

crate::module::SerializedModule::from_artifacts(&self.inner.compiler, &artifacts, &types)
Expand Down
18 changes: 16 additions & 2 deletions crates/wasmtime/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,32 @@ impl Module {
engine.cache_config(),
)
.get_data((engine.compiler(), binary), |(compiler, binary)| {
CompilationArtifacts::build(compiler, binary, USE_PAGED_MEM_INIT)
CompilationArtifacts::build(
compiler,
binary,
USE_PAGED_MEM_INIT,
#[cfg(feature = "parallel-compilation")]
engine.config().parallel_compilation,
)
})?;
} else {
let (main_module, artifacts, types) =
CompilationArtifacts::build(engine.compiler(), binary, USE_PAGED_MEM_INIT)?;
CompilationArtifacts::build(
engine.compiler(),
binary,
USE_PAGED_MEM_INIT,
#[cfg(feature = "parallel-compilation")]
engine.config().parallel_compilation,
)?;
}
};

let modules = CompiledModule::from_artifacts_list(
artifacts,
engine.compiler().isa(),
&*engine.config().profiler,
#[cfg(feature = "parallel-compilation")]
engine.config().parallel_compilation,
)?;

Self::from_parts(engine, modules, main_module, Arc::new(types), &[])
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmtime/src/module/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ impl<'a> SerializedModule<'a> {
.collect(),
engine.compiler().isa(),
&*engine.config().profiler,
#[cfg(feature = "parallel-compilation")]
engine.config().parallel_compilation,
)?;

assert!(!modules.is_empty());
Expand Down

0 comments on commit 48a6be8

Please sign in to comment.