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

Upstream wasm-wave instances in wasmtime #8872

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
66 changes: 62 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ wasm-smith = "0.211.1"
wasm-mutate = "0.211.1"
wit-parser = "0.211.1"
wit-component = "0.211.1"
wasm-wave = "0.211.1"

# Non-Bytecode Alliance maintained dependencies:
# --------------------------
Expand Down
4 changes: 4 additions & 0 deletions crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ wasmtime-wmemcheck = { workspace = true, optional = true }
target-lexicon = { workspace = true }
wasmparser = { workspace = true }
wasm-encoder = { workspace = true, optional = true }
wasm-wave = { workspace = true, optional = true }
anyhow = { workspace = true }
libc = { workspace = true }
cfg-if = { workspace = true }
Expand Down Expand Up @@ -269,3 +270,6 @@ call-hook = []
# Enables support for "memory protection keys" which can be used in conjunction
# with the pooling allocator on x64 to compact linear memory allocations.
memory-protection-keys = ["pooling-allocator"]

# Wave
wave = ["dep:wasm-wave"]
3 changes: 3 additions & 0 deletions crates/wasmtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ mod coredump;
#[cfg(feature = "coredump")]
pub use coredump::*;

#[cfg(feature = "wave")]
mod wave;

fn _assertions_runtime() {
use crate::_assert_send_and_sync;

Expand Down
33 changes: 12 additions & 21 deletions crates/wasmtime/src/runtime/component/func.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use crate::component::instance::{Instance, InstanceData};
use crate::component::storage::storage_as_slice;
use crate::component::types::Type;
use crate::component::types::{ComponentFunc, Type};
use crate::component::values::Val;
use crate::prelude::*;
use crate::runtime::vm::component::ResourceTables;
use crate::runtime::vm::{Export, ExportFunction};
use crate::store::{StoreOpaque, Stored};
use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
use alloc::sync::Arc;
use core::mem::{self, MaybeUninit};
use core::ptr::NonNull;
use wasmtime_environ::component::{
CanonicalOptions, ComponentTypes, CoreDef, InterfaceType, RuntimeComponentInstanceIndex,
TypeFuncIndex, TypeTuple, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS,
CanonicalOptions, CoreDef, InterfaceType, RuntimeComponentInstanceIndex, TypeFuncIndex,
TypeTuple, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS,
};

mod host;
Expand Down Expand Up @@ -42,7 +41,6 @@ pub struct Func(Stored<FuncData>);
pub struct FuncData {
export: ExportFunction,
ty: TypeFuncIndex,
types: Arc<ComponentTypes>,
options: Options,
instance: Instance,
component_instance: RuntimeComponentInstanceIndex,
Expand Down Expand Up @@ -77,7 +75,6 @@ impl Func {
export,
options,
ty,
types: data.component_types().clone(),
instance: *instance,
component_instance,
post_return,
Expand Down Expand Up @@ -208,28 +205,22 @@ impl Func {
Ok(())
}

/// Get the parameter types for this function.
pub fn params(&self, store: impl AsContext) -> Box<[Type]> {
/// Get the `ComponentFunc`, which describes the type of this function.
pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
let store = store.as_context();
let data = &store[self.0];
let instance = store[data.instance.0].as_ref().unwrap();
data.types[data.types[data.ty].params]
.types
.iter()
.map(|ty| Type::from(ty, &instance.ty()))
.collect()
ComponentFunc::from(data.ty, &instance.ty())
}

/// Get the parameter types for this function.
pub fn params(&self, store: impl AsContext) -> Box<[Type]> {
self.ty(&store).params().collect()
}

/// Get the result types for this function.
pub fn results(&self, store: impl AsContext) -> Box<[Type]> {
let store = store.as_context();
let data = &store[self.0];
let instance = store[data.instance.0].as_ref().unwrap();
data.types[data.types[data.ty].results]
.types
.iter()
.map(|ty| Type::from(ty, &instance.ty()))
.collect()
self.ty(&store).results().collect()
}

/// Invokes this function with the `params` given and returns the result.
Expand Down
42 changes: 42 additions & 0 deletions crates/wasmtime/src/runtime/wave.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Integation with wasm-wave: string representations of values and types

#[cfg(feature = "component-model")]
mod component;
mod core;

macro_rules! unwrap_val {
($val:expr, $case:path, $name:expr) => {
match $val {
$case(v) => v,
_ => panic!("called unwrap_{name} on non-{name} value", name = $name),
}
};
}
macro_rules! unwrap_2val {
($val:expr, $case:path, $name:expr) => {
match $val {
$case(n, v) => (n, v),
_ => panic!("called unwrap_{name} on non-{name} value", name = $name),
}
};
}
pub(crate) use unwrap_2val;
pub(crate) use unwrap_val;

#[inline]
pub(crate) fn canonicalize_nan32(val: f32) -> f32 {
if val.is_nan() {
f32::NAN
} else {
val
}
}

#[inline]
pub(crate) fn canonicalize_nan64(val: f64) -> f64 {
if val.is_nan() {
f64::NAN
} else {
val
}
}
Loading
Loading