Merge branch 'master' into middleware-refactor

This commit is contained in:
Syrus Akbary
2021-04-27 09:44:25 -07:00
2 changed files with 29 additions and 10 deletions

View File

@ -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::{
@ -73,7 +74,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]])
})
@ -83,12 +84,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);
@ -109,7 +110,7 @@ impl Compiler for SinglepassCompiler {
&vmoffsets,
&memory_styles,
&table_styles,
*i,
i,
&locals,
)
.map_err(to_compile_error)?;
@ -130,8 +131,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()
@ -140,7 +140,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()
@ -170,6 +170,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::*;