From 7ad7543f8936e16bb49446e449adabc31f87791d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 26 Apr 2021 18:03:22 +0300 Subject: [PATCH 1/2] Prepare to abstract rayon away by minimizing the API we use --- lib/compiler-singlepass/src/compiler.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/compiler-singlepass/src/compiler.rs b/lib/compiler-singlepass/src/compiler.rs index edc1d93a4a4..e69c0ad2e9b 100644 --- a/lib/compiler-singlepass/src/compiler.rs +++ b/lib/compiler-singlepass/src/compiler.rs @@ -81,12 +81,12 @@ impl Compiler for SinglepassCompiler { let functions = function_body_inputs .iter() .collect::)>>() - .par_iter() + .into_par_iter() .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); @@ -107,7 +107,7 @@ impl Compiler for SinglepassCompiler { &vmoffsets, &memory_styles, &table_styles, - *i, + i, &locals, ) .map_err(to_compile_error)?; @@ -128,8 +128,7 @@ impl Compiler for SinglepassCompiler { .signatures .values() .collect::>() - .par_iter() - .cloned() + .into_par_iter() .map(gen_std_trampoline) .collect::>() .into_iter() @@ -138,7 +137,7 @@ impl Compiler for SinglepassCompiler { let dynamic_function_trampolines = module .imported_function_types() .collect::>() - .par_iter() + .into_par_iter() .map(|func_type| gen_std_dynamic_import_trampoline(&vmoffsets, &func_type)) .collect::>() .into_iter() From 60348fc5e3d53e651e1e652603a40cc3eae79061 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 26 Apr 2021 18:12:13 +0300 Subject: [PATCH 2/2] Make threading optional in single pass backend --- lib/compiler-singlepass/Cargo.toml | 4 ++-- lib/compiler-singlepass/src/compiler.rs | 30 ++++++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/compiler-singlepass/Cargo.toml b/lib/compiler-singlepass/Cargo.toml index 1cb57fefc47..1f2c53e2600 100644 --- a/lib/compiler-singlepass/Cargo.toml +++ b/lib/compiler-singlepass/Cargo.toml @@ -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" @@ -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"] diff --git a/lib/compiler-singlepass/src/compiler.rs b/lib/compiler-singlepass/src/compiler.rs index e69c0ad2e9b..4269f54e3a8 100644 --- a/lib/compiler-singlepass/src/compiler.rs +++ b/lib/compiler-singlepass/src/compiler.rs @@ -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::{ @@ -71,7 +72,7 @@ impl Compiler for SinglepassCompiler { let import_trampolines: PrimaryMap = (0..module.num_imported_functions) .map(FunctionIndex::new) .collect::>() - .into_par_iter() + .into_par_iter_if_rayon() .map(|i| { gen_import_call_trampoline(&vmoffsets, i, &module.signatures[module.functions[i]]) }) @@ -81,7 +82,7 @@ impl Compiler for SinglepassCompiler { let functions = function_body_inputs .iter() .collect::)>>() - .into_par_iter() + .into_par_iter_if_rayon() .map(|(i, input)| { let middleware_chain = self .config @@ -128,7 +129,7 @@ impl Compiler for SinglepassCompiler { .signatures .values() .collect::>() - .into_par_iter() + .into_par_iter_if_rayon() .map(gen_std_trampoline) .collect::>() .into_iter() @@ -137,7 +138,7 @@ impl Compiler for SinglepassCompiler { let dynamic_function_trampolines = module .imported_function_types() .collect::>() - .into_par_iter() + .into_par_iter_if_rayon() .map(|func_type| gen_std_dynamic_import_trampoline(&vmoffsets, &func_type)) .collect::>() .into_iter() @@ -167,6 +168,25 @@ fn to_compile_error(x: T) -> CompileError { x.to_compile_error() } +trait IntoParIterIfRayon { + type Output; + fn into_par_iter_if_rayon(self) -> Self::Output; +} + +impl IntoParIterIfRayon for Vec { + #[cfg(not(feature = "rayon"))] + type Output = std::vec::IntoIter; + #[cfg(feature = "rayon")] + type Output = rayon::vec::IntoIter; + + 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::*;