From f83420bbf78837aabf1d617b2b71d4ca8bfc21dd Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Thu, 1 Dec 2022 10:02:42 -0800 Subject: [PATCH] Parallelize with rayon --- src/backend/mod.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index f7fd76e..0936cfc 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -6,6 +6,7 @@ use crate::ir::{ExportKind, FuncDecl, FunctionBody, ImportKind, Module, Type, Va use crate::passes::rpo::RPO; use crate::Operator; use anyhow::Result; +use rayon::prelude::*; use std::borrow::Cow; pub mod stackify; @@ -689,10 +690,19 @@ pub fn compile(module: &Module<'_>) -> anyhow::Result> { into_mod.section(&elem); let mut code = wasm_encoder::CodeSection::new(); - for (func, func_decl) in module.funcs().skip(num_func_imports) { - let body = func_decl.body().unwrap(); - log::debug!("Compiling {}", func); - let body = WasmFuncBackend::new(body)?.compile()?; + let bodies = module + .funcs() + .skip(num_func_imports) + .collect::>() + .par_iter() + .map(|(func, func_decl)| -> Result { + let body = func_decl.body().unwrap(); + log::debug!("Compiling {}", func); + WasmFuncBackend::new(body)?.compile() + }) + .collect::>>()?; + + for body in bodies { code.function(&body); } into_mod.section(&code);