Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/plugin): Disable SIMD #6163

Merged
merged 10 commits into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Expand Up @@ -4,6 +4,9 @@
rustdocflags = ["--cfg", "docsrs"]
rustflags = []

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-feature=+sse2"]

[target.aarch64-apple-darwin]
rustflags = []

Expand Down
55 changes: 46 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions crates/swc_plugin_runner/Cargo.toml
Expand Up @@ -38,6 +38,7 @@ rkyv-impl = [

[dependencies]
anyhow = "1.0.42"
enumset = "1.0.12"
once_cell = "1.10.0"
parking_lot = "0.12.0"
serde = { version = "1.0.126", features = ["derive"] }
Expand All @@ -48,9 +49,11 @@ swc_common = { version = "0.29.8", path = "../swc_common", features = [
swc_ecma_ast = { version = "0.94.11", path = "../swc_ecma_ast" }
swc_plugin_proxy = { version = "0.22.11", path = "../swc_plugin_proxy" }

tracing = "0.1.32"
wasmer = { version = "2.3.0", default-features = false }
wasmer-wasi = { version = "2.3.0", default-features = false }
tracing = "0.1.32"
wasmer = { version = "2.3.0", default-features = false }
wasmer-compiler-cranelift = "2.3.0"
wasmer-engine-universal = "2.3.0"
wasmer-wasi = { version = "2.3.0", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wasmer-cache = { version = "2.3.0", optional = true }
Expand Down
26 changes: 22 additions & 4 deletions crates/swc_plugin_runner/src/cache.rs
Expand Up @@ -4,12 +4,13 @@ use std::{
};

use anyhow::{Context, Error};
use enumset::EnumSet;
use parking_lot::Mutex;
use swc_common::{
collections::AHashMap,
sync::{Lazy, OnceCell},
};
use wasmer::{Module, Store};
use wasmer::{BaseTunables, CpuFeature, Engine, Module, Store, Target, Triple};
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash};

Expand All @@ -30,7 +31,7 @@ compile_error!(
/// however it is not gauranteed to be compatible across wasmer's
/// internal changes.
/// https://github.com/wasmerio/wasmer/issues/2781
const MODULE_SERIALIZATION_VERSION: &str = "v3";
const MODULE_SERIALIZATION_VERSION: &str = "v4";

/// A shared instance to plugin's module bytecode cache.
pub static PLUGIN_MODULE_CACHE: Lazy<PluginModuleCache> = Lazy::new(Default::default);
Expand Down Expand Up @@ -149,7 +150,7 @@ impl PluginModuleCache {
std::fs::read(&binary_path).context("Cannot read plugin from specified path")?;
let module_bytes_hash = Hash::generate(&module_bytes);

let wasmer_store = Store::default();
let wasmer_store = new_store();

let load_cold_wasm_bytes = || {
let span = tracing::span!(
Expand Down Expand Up @@ -205,7 +206,7 @@ impl PluginModuleCache {
//TODO: In native runtime we have to reconstruct module using raw bytes in
// memory cache. requires https://github.com/wasmerio/wasmer/pull/2821

let wasmer_store = Store::default();
let wasmer_store = new_store();
let module = Module::new(&wasmer_store, in_memory_module_bytes)?;

Ok(module)
Expand Down Expand Up @@ -234,3 +235,20 @@ impl PluginModuleCache {
}
}
}

/// Creates an instnace of [Store].
///
/// This function exists because we need to disable simd.
fn new_store() -> Store {
// Use empty enumset to disable simd.
let mut set = EnumSet::new();
set.insert(CpuFeature::SSE2);
let target = Target::new(Triple::host(), set);

let config = wasmer_compiler_cranelift::Cranelift::default();
let engine = wasmer_engine_universal::Universal::new(config)
.target(target)
.engine();
let tunables = BaseTunables::for_target(engine.target());
Store::new_with_tunables(&engine, tunables)
}