Truly independent web browser
LibWasm: Run WebAssembly on x86-64 CPUs without AVX2 and BMI
Andreas Klingcaf2cbaf1b81
diff --git a/Libraries/LibWasm/Engine/build.rs b/Libraries/LibWasm/Engine/build.rsindex 5e434eb8af1..fa13b5abfff 100644--- a/Libraries/LibWasm/Engine/build.rs+++ b/Libraries/LibWasm/Engine/build.rs@@ -62,6 +62,13 @@ fn main() {let layout_path = output_directory.join("layout.conf");write_if_changed(&layout_path, &layout);+ // The interpreter counts zeros with LZCNT and TZCNT where the build targets CPUs that have them (see+ // `check_cpu_features()`), and with bit scans everywhere else.+ let target_features = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();+ let has_target_feature = |feature: &str| target_features.split(',').any(|candidate| candidate == feature);+ let has_lzcnt = architecture == flapc::Architecture::X86_64 && has_target_feature("lzcnt");+ let has_bmi1 = architecture == flapc::Architecture::X86_64 && has_target_feature("bmi1");+let source = fs::read_to_string(&interpreter_source).expect("read interpreter.flap");let source_name = interpreter_source.display().to_string();let compiler = flapc::Compiler::new(flapc::CompileOptions {@@ -70,8 +77,8 @@ fn main() {object_format,},has_jscvt: false,- has_lzcnt: false,- has_bmi1: false,+ has_lzcnt,+ has_bmi1,enable_assertions: true,profiling: false,profile: flapc::Profile::WebAssembly,diff --git a/Libraries/LibWasm/Engine/src/runtime/interpreter.rs b/Libraries/LibWasm/Engine/src/runtime/interpreter.rsindex f8b53d4533d..a06b4d82420 100644--- a/Libraries/LibWasm/Engine/src/runtime/interpreter.rs+++ b/Libraries/LibWasm/Engine/src/runtime/interpreter.rs@@ -71,11 +71,11 @@ fn memory_fault_handler() -> usize {dispatch_table_entry(crate::abi::OPCODE_MEMORY_FAULT)}-/// The CPU features the interpreter's code uses and that the target's baseline guarantees: x86-64-v3 on x86-64, and-/// ARMv8.2-A on AArch64. A CPU without them would not fault but compute wrong results (lzcnt runs as bsr on older-/// CPUs), so the engine refuses to start instead.+/// The CPU features the interpreter's code uses: SSSE3, SSE4.1, SSE4.2 and POPCNT, and LZCNT and BMI1 where the build+/// targets them (see build.rs), on x86-64, and NEON, LSE and RDM on AArch64. A CPU without them would not fault but compute wrong results (lzcnt+/// runs as bsr on older CPUs), so the engine refuses to start instead.pub fn check_cpu_features() {- let missing = missing_cpu_features();+ let missing = missing_interpreter_cpu_features();assert!(missing.is_empty(),"The WebAssembly engine needs CPU features this machine lacks: {}",@@ -83,38 +83,65 @@ pub fn check_cpu_features() {);}+/// Whether the CPU has the features the JIT's code uses: those of x86-64-v3 on x86-64, and those of the interpreter on+/// AArch64. Without them, everything runs in the interpreter.+pub fn cpu_supports_jit() -> bool {+ missing_jit_cpu_features().is_empty()+}+#[cfg(target_arch = "x86_64")]-fn missing_cpu_features() -> Vec<&'static str> {- let mut missing = Vec::new();- macro_rules! require {- ($($feature:tt),+) => {- $(if !std::arch::is_x86_feature_detected!($feature) {- missing.push($feature);- })+- };+use std::arch::is_x86_feature_detected as is_cpu_feature_detected;++#[cfg(target_arch = "aarch64")]+use std::arch::is_aarch64_feature_detected as is_cpu_feature_detected;++#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]+macro_rules! missing_cpu_features {+ ($($feature:tt),+) => {{+ let mut missing = Vec::new();+ $(if !is_cpu_feature_detected!($feature) {+ missing.push($feature);+ })++ missing+ }};+}++#[cfg(target_arch = "x86_64")]+fn missing_interpreter_cpu_features() -> Vec<&'static str> {+ let mut missing = missing_cpu_features!("ssse3", "sse4.1", "sse4.2", "popcnt");+ if cfg!(target_feature = "lzcnt") {+ missing.extend(missing_cpu_features!("lzcnt"));+ }+ if cfg!(target_feature = "bmi1") {+ missing.extend(missing_cpu_features!("bmi1"));}- require!(- "sse4.1", "sse4.2", "popcnt", "lzcnt", "bmi1", "bmi2", "avx2", "fma", "movbe"- );missing}+#[cfg(target_arch = "x86_64")]+fn missing_jit_cpu_features() -> Vec<&'static str> {+ missing_cpu_features!(+ "sse4.1", "sse4.2", "popcnt", "lzcnt", "bmi1", "bmi2", "avx", "avx2", "fma", "movbe"+ )+}+#[cfg(target_arch = "aarch64")]-fn missing_cpu_features() -> Vec<&'static str> {- let mut missing = Vec::new();- macro_rules! require {- ($($feature:tt),+) => {- $(if !std::arch::is_aarch64_feature_detected!($feature) {- missing.push($feature);- })+- };- }- require!("neon", "lse", "rdm");- missing+fn missing_interpreter_cpu_features() -> Vec<&'static str> {+ missing_cpu_features!("neon", "lse", "rdm")+}++#[cfg(target_arch = "aarch64")]+fn missing_jit_cpu_features() -> Vec<&'static str> {+ missing_interpreter_cpu_features()+}++#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]+fn missing_interpreter_cpu_features() -> Vec<&'static str> {+ Vec::new()}#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]-fn missing_cpu_features() -> Vec<&'static str> {+fn missing_jit_cpu_features() -> Vec<&'static str> {Vec::new()}diff --git a/Libraries/LibWasm/Engine/src/runtime/jit.rs b/Libraries/LibWasm/Engine/src/runtime/jit.rsindex 85eb9a6465f..7a8611b1bb0 100644--- a/Libraries/LibWasm/Engine/src/runtime/jit.rs+++ b/Libraries/LibWasm/Engine/src/runtime/jit.rs@@ -123,6 +123,7 @@ pub fn options() -> Options {all(any(target_os = "linux", target_os = "macos"), target_arch = "x86_64"),all(any(target_os = "linux", target_os = "macos"), target_arch = "aarch64")));+ options.enabled &= super::interpreter::cpu_supports_jit();options})}@@ -269,8 +270,8 @@ pub fn initial_native_entry() -> u64 {native::stubs().adapter}-/// Whether the JIT runs: if `LIBWASM_JIT` wants it and the process has executable memory for its stubs, which a-/// hardened runtime without the JIT entitlement does not give it.+/// Whether the JIT runs: if `LIBWASM_JIT` wants it, the CPU has the features its code uses, and the process has+/// executable memory for its stubs, which a hardened runtime without the JIT entitlement does not give it.pub(crate) fn enabled() -> bool {options().enabled && native::try_stubs().is_some()}