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

Optimize wasm calls ever-so-slightly #7084

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/runtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }
}
Expand Down
4 changes: 3 additions & 1 deletion crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
39 changes: 38 additions & 1 deletion crates/wasmtime/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ fn enter_wasm<T>(store: &mut StoreContextMut<'_, T>) -> Option<usize> {
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.
Expand All @@ -1435,6 +1435,43 @@ fn enter_wasm<T>(store: &mut StoreContextMut<'_, T>) -> Option<usize> {
Some(prev_stack)
}

#[inline]
fn stack_pointer() -> usize {
let stack_pointer: usize;
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")] {
unsafe {
std::arch::asm!(
"mov {}, sp",
out(reg) stack_pointer,
options(nostack,nomem),
);
}
} 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");
}
}
stack_pointer
}

fn exit_wasm<T>(store: &mut StoreContextMut<'_, T>, prev_stack: Option<usize>) {
// If we don't have a previous stack pointer to restore, then there's no
// cleanup we need to perform here.
Expand Down