Skip to content

Commit

Permalink
Make threading optional in single pass backend
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Apr 26, 2021
1 parent 7ad7543 commit 60348fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 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"]
30 changes: 25 additions & 5 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,7 +82,7 @@ impl Compiler for SinglepassCompiler {
let functions = function_body_inputs
.iter()
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
.into_par_iter()
.into_par_iter_if_rayon()
.map(|(i, input)| {
let middleware_chain = self
.config
Expand Down Expand Up @@ -128,7 +129,7 @@ impl Compiler for SinglepassCompiler {
.signatures
.values()
.collect::<Vec<_>>()
.into_par_iter()
.into_par_iter_if_rayon()
.map(gen_std_trampoline)
.collect::<Vec<_>>()
.into_iter()
Expand All @@ -137,7 +138,7 @@ impl Compiler for SinglepassCompiler {
let dynamic_function_trampolines = module
.imported_function_types()
.collect::<Vec<_>>()
.into_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 @@ -167,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 60348fc

Please sign in to comment.