From 0c63911b7d3595e4b11d920f6f15203e3f77df17 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Sep 2023 10:41:37 -0700 Subject: [PATCH 1/4] Optimize wasm calls ever-so-slightly --- crates/runtime/src/instance.rs | 1 + crates/wasmtime/Cargo.toml | 4 +++- crates/wasmtime/src/func.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/runtime/src/instance.rs b/crates/runtime/src/instance.rs index 29c91450501e..5c8df90223d3 100644 --- a/crates/runtime/src/instance.rs +++ b/crates/runtime/src/instance.rs @@ -404,6 +404,7 @@ impl Instance { } /// Return a pointer to the interrupts structure + #[inline] pub fn runtime_limits(&mut self) -> *mut *const VMRuntimeLimits { unsafe { self.vmctx_plus_offset_mut(self.offsets().vmctx_runtime_limits()) } } diff --git a/crates/wasmtime/Cargo.toml b/crates/wasmtime/Cargo.toml index 6aa56e8f54f1..d735b74b6feb 100644 --- a/crates/wasmtime/Cargo.toml +++ b/crates/wasmtime/Cargo.toml @@ -41,7 +41,6 @@ serde_json = { workspace = true } bincode = "1.2.1" indexmap = { workspace = true } paste = "1.0.3" -psm = "0.1.11" once_cell = { workspace = true } rayon = { version = "1.0", optional = true } object = { workspace = true } @@ -56,6 +55,9 @@ features = [ "Win32_System_Diagnostics_Debug", ] +[target.'cfg(target_arch = "s390x")'.dependencies] +psm = "0.1.11" + [dev-dependencies] tempfile = "3.0" wasmtime-wasi = { path = "../wasi", default-features = true } diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index 60e2a5047903..d86a7f5a9bf2 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -1408,7 +1408,7 @@ fn enter_wasm(store: &mut StoreContextMut<'_, T>) -> Option { return None; } - let stack_pointer = psm::stack_pointer() as usize; + let stack_pointer = stack_pointer(); // Determine the stack pointer where, after which, any wasm code will // immediately trap. This is checked on the entry to all wasm functions. @@ -1435,6 +1435,36 @@ fn enter_wasm(store: &mut StoreContextMut<'_, T>) -> Option { Some(prev_stack) } +#[inline] +fn stack_pointer() -> usize { + // psm::stack_pointer() as usize + let stack_pointer: usize; + unsafe { + cfg_if::cfg_if! { + if #[cfg(target_arch = "x86_64")] { + std::arch::asm!( + "mov {}, rsp", + out(reg) stack_pointer, + options(nostack,nomem), + ); + } else if #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] { + // aarch64 and riscv64 happen to have the same assembler syntax + // for `mov` and `sp`, so they both use this. + std::arch::asm!( + "mov {}, sp", + out(reg) stack_pointer, + options(nostack,nomem), + ); + } else if #[cfg(target_arch = "s390x")] { + stack_pointer = psm::stack_pointer() as usize; + } else { + compile_error!("unsupported platform"); + } + } + } + stack_pointer +} + fn exit_wasm(store: &mut StoreContextMut<'_, T>, prev_stack: Option) { // If we don't have a previous stack pointer to restore, then there's no // cleanup we need to perform here. From 8e9b8edf61730e529c643f6e8f880e99ff0c99a3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Sep 2023 10:48:30 -0700 Subject: [PATCH 2/4] Fix riscv64 --- crates/wasmtime/src/func.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index d86a7f5a9bf2..b24d7c6e339f 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -1447,14 +1447,18 @@ fn stack_pointer() -> usize { out(reg) stack_pointer, options(nostack,nomem), ); - } else if #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] { - // aarch64 and riscv64 happen to have the same assembler syntax - // for `mov` and `sp`, so they both use this. + } else if #[cfg(target_arch = "aarch64")] { std::arch::asm!( "mov {}, sp", out(reg) stack_pointer, options(nostack,nomem), ); + } else if #[cfg(target_arch = "riscv64")] { + std::arch::asm!( + "mv {}, sp", + out(reg) stack_pointer, + options(nostack,nomem), + ); } else if #[cfg(target_arch = "s390x")] { stack_pointer = psm::stack_pointer() as usize; } else { From 44eeb5bbe28be7641819a116e3d40d2648b8a57e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Sep 2023 10:48:45 -0700 Subject: [PATCH 3/4] Remove stray comment --- crates/wasmtime/src/func.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index b24d7c6e339f..5decacbf5f87 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -1437,7 +1437,6 @@ fn enter_wasm(store: &mut StoreContextMut<'_, T>) -> Option { #[inline] fn stack_pointer() -> usize { - // psm::stack_pointer() as usize let stack_pointer: usize; unsafe { cfg_if::cfg_if! { From b6f3ffb21920677f9f2dc1cfda3fd356af6ed58f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 25 Sep 2023 13:59:16 -0700 Subject: [PATCH 4/4] Shuffle around where `unsafe` lies --- crates/wasmtime/src/func.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/crates/wasmtime/src/func.rs b/crates/wasmtime/src/func.rs index 5decacbf5f87..3ec071c72980 100644 --- a/crates/wasmtime/src/func.rs +++ b/crates/wasmtime/src/func.rs @@ -1438,31 +1438,35 @@ fn enter_wasm(store: &mut StoreContextMut<'_, T>) -> Option { #[inline] fn stack_pointer() -> usize { let stack_pointer: usize; - unsafe { - cfg_if::cfg_if! { - if #[cfg(target_arch = "x86_64")] { + cfg_if::cfg_if! { + if #[cfg(target_arch = "x86_64")] { + unsafe { std::arch::asm!( "mov {}, rsp", out(reg) stack_pointer, options(nostack,nomem), ); - } else if #[cfg(target_arch = "aarch64")] { + } + } else if #[cfg(target_arch = "aarch64")] { + unsafe { std::arch::asm!( "mov {}, sp", out(reg) stack_pointer, options(nostack,nomem), ); - } else if #[cfg(target_arch = "riscv64")] { + } + } else if #[cfg(target_arch = "riscv64")] { + unsafe { std::arch::asm!( "mv {}, sp", out(reg) stack_pointer, options(nostack,nomem), ); - } else if #[cfg(target_arch = "s390x")] { - stack_pointer = psm::stack_pointer() as usize; - } else { - compile_error!("unsupported platform"); } + } else if #[cfg(target_arch = "s390x")] { + stack_pointer = psm::stack_pointer() as usize; + } else { + compile_error!("unsupported platform"); } } stack_pointer