Skip to content

Commit

Permalink
add component-model-async/lower.wast test (#10103)
Browse files Browse the repository at this point in the history
This is another piece of #9582 which I'm splitting out to make review easier.

This test includes a minimal component which lowers an import with the `async`
option.

The rest of the changes fill in some TODOs to make the test pass.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Jan 24, 2025
1 parent 3ba13d1 commit 38a47d2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
12 changes: 8 additions & 4 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,6 @@ impl<'a> TrampolineCompiler<'a> {
async_,
} = *options;

if async_ {
todo!()
}

// vmctx: *mut VMComponentContext
host_sig.params.push(ir::AbiParam::new(pointer_type));
callee_args.push(vmctx);
Expand Down Expand Up @@ -350,6 +346,14 @@ impl<'a> TrampolineCompiler<'a> {
.iconst(ir::types::I8, i64::from(string_encoding as u8)),
);

// async_: bool
host_sig.params.push(ir::AbiParam::new(ir::types::I8));
callee_args.push(
self.builder
.ins()
.iconst(ir::types::I8, if async_ { 1 } else { 0 }),
);

// storage: *mut ValRaw
host_sig.params.push(ir::AbiParam::new(pointer_type));
callee_args.push(values_vec_ptr);
Expand Down
14 changes: 14 additions & 0 deletions crates/wasmtime/src/runtime/component/func/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl HostFunc {
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
async_: u8,
storage: NonNull<MaybeUninit<ValRaw>>,
storage_len: usize,
) -> bool
Expand All @@ -66,6 +67,7 @@ impl HostFunc {
memory,
realloc,
StringEncoding::from_u8(string_encoding).unwrap(),
async_ != 0,
NonNull::slice_from_raw_parts(storage, storage_len).as_mut(),
|store, args| (*data)(store, args),
)
Expand Down Expand Up @@ -142,6 +144,7 @@ unsafe fn call_host<T, Params, Return, F>(
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: StringEncoding,
async_: bool,
storage: &mut [MaybeUninit<ValRaw>],
closure: F,
) -> Result<()>
Expand All @@ -150,6 +153,10 @@ where
Return: Lower,
F: FnOnce(StoreContextMut<'_, T>, Params) -> Result<Return>,
{
if async_ {
todo!()
}

/// Representation of arguments to this function when a return pointer is in
/// use, namely the argument list is followed by a single value which is the
/// return pointer.
Expand Down Expand Up @@ -320,12 +327,17 @@ unsafe fn call_host_dynamic<T, F>(
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: StringEncoding,
async_: bool,
storage: &mut [MaybeUninit<ValRaw>],
closure: F,
) -> Result<()>
where
F: FnOnce(StoreContextMut<'_, T>, &[Val], &mut [Val]) -> Result<()>,
{
if async_ {
todo!()
}

let options = Options::new(
store.0.id(),
NonNull::new(memory),
Expand Down Expand Up @@ -429,6 +441,7 @@ extern "C" fn dynamic_entrypoint<T, F>(
memory: *mut VMMemoryDefinition,
realloc: *mut VMFuncRef,
string_encoding: u8,
async_: u8,
storage: NonNull<MaybeUninit<ValRaw>>,
storage_len: usize,
) -> bool
Expand All @@ -447,6 +460,7 @@ where
memory,
realloc,
StringEncoding::from_u8(string_encoding).unwrap(),
async_ != 0,
NonNull::slice_from_raw_parts(storage, storage_len).as_mut(),
|store, params, results| (*data)(store, params, results),
)
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmtime/src/runtime/vm/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub struct ComponentInstance {
/// option for the canonical ABI options.
/// * `string_encoding` - this is the configured string encoding for the
/// canonical ABI this lowering corresponds to.
/// * `async_` - whether the caller is using the async ABI.
/// * `args_and_results` - pointer to stack-allocated space in the caller where
/// all the arguments are stored as well as where the results will be written
/// to. The size and initialized bytes of this depends on the core wasm type
Expand All @@ -117,6 +118,7 @@ pub type VMLoweringCallee = extern "C" fn(
opt_memory: *mut VMMemoryDefinition,
opt_realloc: *mut VMFuncRef,
string_encoding: u8,
async_: u8,
args_and_results: NonNull<mem::MaybeUninit<ValRaw>>,
nargs_and_results: usize,
) -> bool;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ impl InterpreterRef<'_> {
use wasmtime_environ::component::ComponentBuiltinFunctionIndex;

if id == const { HostCall::ComponentLowerImport.index() } {
call!(@host VMLoweringCallee(nonnull, nonnull, u32, nonnull, ptr, ptr, u8, nonnull, size) -> bool);
call!(@host VMLoweringCallee(nonnull, nonnull, u32, nonnull, ptr, ptr, u8, u8, nonnull, size) -> bool);
}

macro_rules! component {
Expand Down
3 changes: 3 additions & 0 deletions crates/wast/src/spectest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<(
use wasmtime::component::{Resource, ResourceType};

let engine = linker.engine().clone();
linker
.root()
.func_wrap("host-echo-u32", |_, v: (u32,)| Ok(v))?;
linker
.root()
.func_wrap("host-return-two", |_, _: ()| Ok((2u32,)))?;
Expand Down
13 changes: 13 additions & 0 deletions tests/misc_testsuite/component-model-async/lower.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;;! component_model_async = true

;; async lower
(component
(import "host-echo-u32" (func $foo (param "p1" u32) (result u32)))
(core module $libc (memory (export "memory") 1))
(core instance $libc (instantiate $libc))
(core func $foo (canon lower (func $foo) async (memory $libc "memory")))
(core module $m
(func (import "" "foo") (param i32 i32) (result i32))
)
(core instance $i (instantiate $m (with "" (instance (export "foo" (func $foo))))))
)

0 comments on commit 38a47d2

Please sign in to comment.