wasmtime: Option to disable parallel compilation (#3169)
* Introduce parallel-compilation configuration switch * Plumb parallel_compilation config to compilation * Adjust obj.rs * Address review * Fix compilation fail in `cache` crate * Fix obj.rs Also remove the now unneeded feature in /Cargo.toml * fmt
This commit is contained in:
@@ -46,6 +46,7 @@ pub struct Compiler {
|
||||
strategy: CompilationStrategy,
|
||||
tunables: Tunables,
|
||||
features: WasmFeatures,
|
||||
parallel_compilation: bool,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
@@ -55,6 +56,7 @@ impl Compiler {
|
||||
strategy: CompilationStrategy,
|
||||
tunables: Tunables,
|
||||
features: WasmFeatures,
|
||||
parallel_compilation: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
isa,
|
||||
@@ -68,6 +70,7 @@ impl Compiler {
|
||||
},
|
||||
tunables,
|
||||
features,
|
||||
parallel_compilation,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,8 +140,9 @@ impl Compiler {
|
||||
) -> 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 = self
|
||||
.run_maybe_parallel(functions, |(index, func)| {
|
||||
self.compiler.compile_function(
|
||||
translation,
|
||||
index,
|
||||
@@ -147,8 +151,7 @@ impl Compiler {
|
||||
&self.tunables,
|
||||
types,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
})?
|
||||
.into_iter()
|
||||
.collect::<CompiledFunctions>();
|
||||
|
||||
@@ -172,6 +175,33 @@ impl Compiler {
|
||||
funcs,
|
||||
})
|
||||
}
|
||||
|
||||
/// Run the given closure in parallel if the compiler is configured to do so.
|
||||
pub(crate) fn run_maybe_parallel<
|
||||
A: Send,
|
||||
B: Send,
|
||||
E: Send,
|
||||
F: Fn(A) -> Result<B, E> + Send + Sync,
|
||||
>(
|
||||
&self,
|
||||
input: Vec<A>,
|
||||
f: F,
|
||||
) -> Result<Vec<B>, E> {
|
||||
if self.parallel_compilation {
|
||||
#[cfg(feature = "parallel-compilation")]
|
||||
return input
|
||||
.into_par_iter()
|
||||
.map(|a| f(a))
|
||||
.collect::<Result<Vec<B>, E>>();
|
||||
}
|
||||
|
||||
// In case the parallel-compilation feature is disabled or the parallel_compilation config
|
||||
// was turned off dynamically fallback to the non-parallel version.
|
||||
input
|
||||
.into_iter()
|
||||
.map(|a| f(a))
|
||||
.collect::<Result<Vec<B>, E>>()
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Compiler {
|
||||
@@ -182,6 +212,7 @@ impl Hash for Compiler {
|
||||
isa,
|
||||
tunables,
|
||||
features,
|
||||
parallel_compilation: _,
|
||||
} = self;
|
||||
|
||||
// Hash compiler's flags: compilation strategy, isa, frontend config,
|
||||
|
||||
Reference in New Issue
Block a user