Skip to content

Commit

Permalink
Merge pull request wasmerio#2262 from matklad/||
Browse files Browse the repository at this point in the history
Make parallelism optional for singlepass backend
  • Loading branch information
syrusakbary authored Apr 26, 2021
2 parents 2b106d0 + 60348fc commit a00f3ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/compiler-singlepass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"
wasmer-compiler = { path = "../compiler", version = "1.0.2", features = ["translator"], default-features = false }
wasmer-vm = { path = "../vm", version = "1.0.2" }
wasmer-types = { path = "../types", version = "1.0.2", default-features = false, features = ["std"] }
rayon = "1.5"
rayon = { version = "1.5", optional = true }
hashbrown = { version = "0.9", optional = true }
serde = { version = "1.0", features = ["derive"] }
more-asserts = "0.2"
Expand All @@ -33,7 +33,7 @@ target-lexicon = { version = "0.11", default-features = false }
maintenance = { status = "actively-developed" }

[features]
default = ["std", "enable-serde"]
default = ["std", "enable-serde", "rayon"]
enable-serde = ["wasmer-compiler/enable-serde", "wasmer-types/enable-serde"]
std = ["wasmer-compiler/std", "wasmer-types/std"]
core = ["hashbrown"]
35 changes: 27 additions & 8 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::codegen_x64::{
};
use crate::config::Singlepass;
use loupe::MemoryUsage;
use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
#[cfg(feature = "rayon")]
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::sync::Arc;
use wasmer_compiler::TrapInformation;
use wasmer_compiler::{
Expand Down Expand Up @@ -71,7 +72,7 @@ impl Compiler for SinglepassCompiler {
let import_trampolines: PrimaryMap<SectionIndex, _> = (0..module.num_imported_functions)
.map(FunctionIndex::new)
.collect::<Vec<_>>()
.into_par_iter()
.into_par_iter_if_rayon()
.map(|i| {
gen_import_call_trampoline(&vmoffsets, i, &module.signatures[module.functions[i]])
})
Expand All @@ -81,12 +82,12 @@ impl Compiler for SinglepassCompiler {
let functions = function_body_inputs
.iter()
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
.par_iter()
.into_par_iter_if_rayon()
.map(|(i, input)| {
let middleware_chain = self
.config
.middlewares
.generate_function_middleware_chain(*i);
.generate_function_middleware_chain(i);
let mut reader =
MiddlewareBinaryReader::new_with_offset(input.data, input.module_offset);
reader.set_middleware_chain(middleware_chain);
Expand All @@ -107,7 +108,7 @@ impl Compiler for SinglepassCompiler {
&vmoffsets,
&memory_styles,
&table_styles,
*i,
i,
&locals,
)
.map_err(to_compile_error)?;
Expand All @@ -128,8 +129,7 @@ impl Compiler for SinglepassCompiler {
.signatures
.values()
.collect::<Vec<_>>()
.par_iter()
.cloned()
.into_par_iter_if_rayon()
.map(gen_std_trampoline)
.collect::<Vec<_>>()
.into_iter()
Expand All @@ -138,7 +138,7 @@ impl Compiler for SinglepassCompiler {
let dynamic_function_trampolines = module
.imported_function_types()
.collect::<Vec<_>>()
.par_iter()
.into_par_iter_if_rayon()
.map(|func_type| gen_std_dynamic_import_trampoline(&vmoffsets, &func_type))
.collect::<Vec<_>>()
.into_iter()
Expand Down Expand Up @@ -168,6 +168,25 @@ fn to_compile_error<T: ToCompileError>(x: T) -> CompileError {
x.to_compile_error()
}

trait IntoParIterIfRayon {
type Output;
fn into_par_iter_if_rayon(self) -> Self::Output;
}

impl<T: Send> IntoParIterIfRayon for Vec<T> {
#[cfg(not(feature = "rayon"))]
type Output = std::vec::IntoIter<T>;
#[cfg(feature = "rayon")]
type Output = rayon::vec::IntoIter<T>;

fn into_par_iter_if_rayon(self) -> Self::Output {
#[cfg(not(feature = "rayon"))]
return self.into_iter();
#[cfg(feature = "rayon")]
return self.into_par_iter();
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit a00f3ee

Please sign in to comment.