From bc72bcf5d5b5290efa9408dfd3424f4a2b6549e3 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Fri, 24 Jan 2025 09:36:19 -0700 Subject: [PATCH] add component-model-async/lower.wast test 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 --- crates/cranelift/src/compiler/component.rs | 12 ++++++++---- crates/wasmtime/src/runtime/component/func/host.rs | 14 ++++++++++++++ crates/wasmtime/src/runtime/vm/component.rs | 2 ++ crates/wasmtime/src/runtime/vm/interpreter.rs | 2 +- crates/wast/src/spectest.rs | 3 +++ .../component-model-async/lower.wast | 13 +++++++++++++ 6 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/misc_testsuite/component-model-async/lower.wast diff --git a/crates/cranelift/src/compiler/component.rs b/crates/cranelift/src/compiler/component.rs index b2da3f3aed62..796b029280a2 100644 --- a/crates/cranelift/src/compiler/component.rs +++ b/crates/cranelift/src/compiler/component.rs @@ -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); @@ -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); diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rs index c5369cec7d83..a8c593286bde 100644 --- a/crates/wasmtime/src/runtime/component/func/host.rs +++ b/crates/wasmtime/src/runtime/component/func/host.rs @@ -46,6 +46,7 @@ impl HostFunc { memory: *mut VMMemoryDefinition, realloc: *mut VMFuncRef, string_encoding: u8, + async_: u8, storage: NonNull>, storage_len: usize, ) -> bool @@ -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), ) @@ -142,6 +144,7 @@ unsafe fn call_host( memory: *mut VMMemoryDefinition, realloc: *mut VMFuncRef, string_encoding: StringEncoding, + async_: bool, storage: &mut [MaybeUninit], closure: F, ) -> Result<()> @@ -150,6 +153,10 @@ where Return: Lower, F: FnOnce(StoreContextMut<'_, T>, Params) -> Result, { + 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. @@ -320,12 +327,17 @@ unsafe fn call_host_dynamic( memory: *mut VMMemoryDefinition, realloc: *mut VMFuncRef, string_encoding: StringEncoding, + async_: bool, storage: &mut [MaybeUninit], 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), @@ -429,6 +441,7 @@ extern "C" fn dynamic_entrypoint( memory: *mut VMMemoryDefinition, realloc: *mut VMFuncRef, string_encoding: u8, + async_: u8, storage: NonNull>, storage_len: usize, ) -> bool @@ -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), ) diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rs index 766959c46804..06e941b041f7 100644 --- a/crates/wasmtime/src/runtime/vm/component.rs +++ b/crates/wasmtime/src/runtime/vm/component.rs @@ -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 @@ -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>, nargs_and_results: usize, ) -> bool; diff --git a/crates/wasmtime/src/runtime/vm/interpreter.rs b/crates/wasmtime/src/runtime/vm/interpreter.rs index bd0973fdbd87..538889cf1add 100644 --- a/crates/wasmtime/src/runtime/vm/interpreter.rs +++ b/crates/wasmtime/src/runtime/vm/interpreter.rs @@ -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 { diff --git a/crates/wast/src/spectest.rs b/crates/wast/src/spectest.rs index 924bf66d1c40..e9b1ec7575ae 100644 --- a/crates/wast/src/spectest.rs +++ b/crates/wast/src/spectest.rs @@ -94,6 +94,9 @@ pub fn link_component_spectest(linker: &mut component::Linker) -> 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,)))?; diff --git a/tests/misc_testsuite/component-model-async/lower.wast b/tests/misc_testsuite/component-model-async/lower.wast new file mode 100644 index 000000000000..bcb4862fc8b4 --- /dev/null +++ b/tests/misc_testsuite/component-model-async/lower.wast @@ -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)))))) +)