wasmtime: add build-time option for parallel compilation (#1903)
When running in embedded environments, threads creation is sometimes undesirable. This adds a feature to toggle wasmtime's internal thread creation for parallel compilation.
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2417,6 +2417,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.12.1",
|
||||
"bincode",
|
||||
"cfg-if",
|
||||
"cranelift-codegen",
|
||||
"cranelift-entity",
|
||||
"cranelift-frontend",
|
||||
|
||||
@@ -72,7 +72,7 @@ members = [
|
||||
]
|
||||
|
||||
[features]
|
||||
default = ["jitdump", "wasmtime/wat"]
|
||||
default = ["jitdump", "wasmtime/wat", "wasmtime/parallel-compilation"]
|
||||
lightbeam = [
|
||||
"wasmtime-environ/lightbeam",
|
||||
"wasmtime-jit/lightbeam",
|
||||
|
||||
@@ -20,7 +20,7 @@ cranelift-wasm = { path = "../../cranelift/wasm", version = "0.65.0", features =
|
||||
wasmparser = "0.58.0"
|
||||
lightbeam = { path = "../lightbeam", optional = true, version = "0.18.0" }
|
||||
indexmap = "1.0.2"
|
||||
rayon = "1.2.1"
|
||||
rayon = { version = "1.2.1", optional = true }
|
||||
thiserror = "1.0.4"
|
||||
directories = "2.0.1"
|
||||
sha2 = "0.8.0"
|
||||
@@ -32,6 +32,7 @@ zstd = "0.5"
|
||||
toml = "0.5.5"
|
||||
file-per-thread-logger = "0.1.1"
|
||||
more-asserts = "0.2.1"
|
||||
cfg-if = "0.1.9"
|
||||
|
||||
[target.'cfg(target_os = "windows")'.dependencies]
|
||||
winapi = "0.3.7"
|
||||
@@ -46,5 +47,8 @@ pretty_env_logger = "0.4.0"
|
||||
filetime = "0.2.7"
|
||||
lazy_static = "1.3.0"
|
||||
|
||||
[features]
|
||||
parallel-compilation = ["rayon"]
|
||||
|
||||
[badges]
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
@@ -99,6 +99,7 @@ use cranelift_codegen::print_errors::pretty_error;
|
||||
use cranelift_codegen::{binemit, isa, Context};
|
||||
use cranelift_entity::PrimaryMap;
|
||||
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator, ModuleTranslationState};
|
||||
#[cfg(feature = "parallel-compilation")]
|
||||
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
|
||||
use std::convert::TryFrom;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@@ -318,11 +319,10 @@ fn compile(env: CompileEnv<'_>) -> Result<ModuleCacheDataTupleType, CompileError
|
||||
let mut traps = PrimaryMap::with_capacity(env.function_body_inputs.len());
|
||||
let mut stack_maps = PrimaryMap::with_capacity(env.function_body_inputs.len());
|
||||
|
||||
env.function_body_inputs
|
||||
.into_iter()
|
||||
.collect::<Vec<(DefinedFuncIndex, &FunctionBodyData<'_>)>>()
|
||||
.par_iter()
|
||||
.map_init(FuncTranslator::new, |func_translator, (i, input)| {
|
||||
type FunctionBodyInput<'a> = (DefinedFuncIndex, &'a FunctionBodyData<'a>);
|
||||
|
||||
let compile_function = |func_translator: &mut FuncTranslator,
|
||||
(i, input): &FunctionBodyInput| {
|
||||
let func_index = env.local.func_index(*i);
|
||||
let mut context = Context::new();
|
||||
context.func.name = get_func_name(func_index);
|
||||
@@ -416,10 +416,28 @@ fn compile(env: CompileEnv<'_>) -> Result<ModuleCacheDataTupleType, CompileError
|
||||
unwind_info,
|
||||
stack_map_sink.finish(),
|
||||
))
|
||||
})
|
||||
.collect::<Result<Vec<_>, CompileError>>()?
|
||||
.into_iter()
|
||||
.for_each(
|
||||
};
|
||||
|
||||
let inputs: Vec<FunctionBodyInput> = env.function_body_inputs.into_iter().collect();
|
||||
|
||||
let results: Result<Vec<_>, CompileError> = {
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "parallel-compilation")] {
|
||||
inputs
|
||||
.par_iter()
|
||||
.map_init(FuncTranslator::new, compile_function)
|
||||
.collect()
|
||||
} else {
|
||||
let mut func_translator = FuncTranslator::new();
|
||||
inputs
|
||||
.iter()
|
||||
.map(|input| compile_function(&mut func_translator, input))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
results?.into_iter().for_each(
|
||||
|(
|
||||
function,
|
||||
func_jt_offsets,
|
||||
|
||||
@@ -38,7 +38,7 @@ wasmtime-wasi = { path = "../wasi" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[features]
|
||||
default = ['wat', 'jitdump']
|
||||
default = ['wat', 'jitdump', 'parallel-compilation']
|
||||
|
||||
# Enables experimental support for the lightbeam codegen backend, an alternative
|
||||
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
|
||||
@@ -50,3 +50,6 @@ jitdump = ["wasmtime-jit/jitdump"]
|
||||
|
||||
# Enables support for the `VTune` profiler
|
||||
vtune = ["wasmtime-jit/vtune"]
|
||||
|
||||
# Enables parallel compilation of WebAssembly code
|
||||
parallel-compilation = ["wasmtime-environ/parallel-compilation"]
|
||||
|
||||
@@ -11,7 +11,7 @@ repository = "https://github.com/bytecodealliance/wasmtime"
|
||||
include = ["src/**/*", "LICENSE"]
|
||||
|
||||
[dependencies]
|
||||
wasmtime = { path = "../../wasmtime", version = "0.18.0" }
|
||||
wasmtime = { path = "../../wasmtime", version = "0.18.0", default-features = false }
|
||||
wasmtime-wiggle-macro = { path = "./macro", version = "0.18.0" }
|
||||
witx = { path = "../../wasi-common/WASI/tools/witx", version = "0.8.5", optional = true }
|
||||
wiggle = { path = "..", version = "0.18.0" }
|
||||
|
||||
Reference in New Issue
Block a user