Skip to content

Commit d869e31

Browse files
alexcrichtondicej
authored andcommitted
Update for new async ABI changes
This commit updates to account for WebAssembly/component-model#520. This is a large-ish change to the semantics from a runtime perspective and needed a number of changes: * The `sync_prepare_call` and `async_prepare_call` libcalls were merged together. The previous async version statically took two arguments but now it's taking a variable number of arguments which looked quite a lot like `sync_prepare_call` so they're now merged into one. * Lots of little updates were made to `fact/signatures.rs` to account for ABI changes. * Tests with handwritten signatures were all updated to the new ABI. * The `CallerInfo::Async` structure which "buffers" a call was updated to have a `Vec<ValRaw>` for the incoming parameters. This is a particularly inefficient way to store parameters but it's in theory workable for now. * The `Storage` abstraction in host calls was refactored and updated to account for async and how lifting parameters could be either flat or indirect. Similar updates were made to the dynamic path as well
1 parent 26b9a59 commit d869e31

File tree

28 files changed

+723
-654
lines changed

28 files changed

+723
-654
lines changed

Cargo.lock

Lines changed: 17 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -594,21 +594,21 @@ lto = true
594594

595595
# TODO: remove this once we've switched to a wasm-tools/wit-bindgen release:
596596
[patch.crates-io]
597-
# wasmparser = { git = "https://github.com/bytecodealliance/wasm-tools" }
598-
# wat = { git = "https://github.com/bytecodealliance/wasm-tools" }
599-
# wast = { git = "https://github.com/bytecodealliance/wasm-tools" }
600-
# wasmprinter = { git = "https://github.com/bytecodealliance/wasm-tools" }
601-
# wasm-encoder = { git = "https://github.com/bytecodealliance/wasm-tools" }
602-
# wasm-smith = { git = "https://github.com/bytecodealliance/wasm-tools" }
603-
# wasm-mutate = { git = "https://github.com/bytecodealliance/wasm-tools" }
604-
# wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools" }
605-
# wit-component = { git = "https://github.com/bytecodealliance/wasm-tools" }
606-
# wasm-wave = { git = "https://github.com/bytecodealliance/wasm-tools" }
607-
# wasm-compose = { git = "https://github.com/bytecodealliance/wasm-tools" }
608-
# wasm-metadata = { git = "https://github.com/bytecodealliance/wasm-tools" }
609-
# wit-bindgen = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'fix-a-tpyo' }
610-
# wit-bindgen-rt = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'fix-a-tpyo' }
611-
# wit-bindgen-rust-macro = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'fix-a-tpyo' }
597+
wasmparser = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
598+
wat = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
599+
wast = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
600+
wasmprinter = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
601+
wasm-encoder = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
602+
wasm-smith = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
603+
wasm-mutate = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
604+
wit-parser = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
605+
wit-component = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
606+
wasm-wave = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
607+
wasm-compose = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
608+
wasm-metadata = { git = "https://github.com/alexcrichton/wasm-tools", branch = 'new-async-abi' }
609+
wit-bindgen = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'new-async-abi' }
610+
wit-bindgen-rt = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'new-async-abi' }
611+
wit-bindgen-rust-macro = { git = "https://github.com/alexcrichton/wit-bindgen", branch = 'new-async-abi' }
612612

613613
# wasmparser = { path = '../wasm-tools/crates/wasmparser' }
614614
# wat = { path = '../wasm-tools/crates/wat' }

crates/cranelift/src/compiler/component.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cranelift_codegen::ir::condcodes::IntCC;
66
use cranelift_codegen::ir::{self, InstBuilder, MemFlags, Value};
77
use cranelift_codegen::isa::{CallConv, TargetIsa};
88
use cranelift_frontend::FunctionBuilder;
9-
use wasmtime_environ::fact::SYNC_PREPARE_FIXED_PARAMS;
9+
use wasmtime_environ::fact::PREPARE_CALL_FIXED_PARAMS;
1010
use wasmtime_environ::{CompiledFunctionBody, component::*};
1111
use wasmtime_environ::{
1212
HostCall, ModuleInternedTypeIndex, PtrSize, TrapSentinel, Tunables, WasmFuncType, WasmValType,
@@ -255,9 +255,8 @@ impl<'a> TrampolineCompiler<'a> {
255255
me.raise_if_host_trapped(rets.pop().unwrap());
256256
})
257257
}
258-
Trampoline::SyncPrepareCall { memory } => self.translate_sync_prepare(*memory),
258+
Trampoline::PrepareCall { memory } => self.translate_prepare(*memory),
259259
Trampoline::SyncStartCall { callback } => self.translate_sync_start(*callback),
260-
Trampoline::AsyncPrepareCall { memory } => self.translate_async_prepare(*memory),
261260
Trampoline::AsyncStartCall {
262261
callback,
263262
post_return,
@@ -471,7 +470,7 @@ impl<'a> TrampolineCompiler<'a> {
471470
);
472471
}
473472

474-
fn translate_sync_prepare(&mut self, memory: Option<RuntimeMemoryIndex>) {
473+
fn translate_prepare(&mut self, memory: Option<RuntimeMemoryIndex>) {
475474
match self.abi {
476475
Abi::Wasm => {}
477476

@@ -489,8 +488,8 @@ impl<'a> TrampolineCompiler<'a> {
489488
let pointer_type = self.isa.pointer_type();
490489
let wasm_func_ty = &self.types[self.signature].unwrap_func();
491490

492-
let param_offset = SYNC_PREPARE_FIXED_PARAMS.len();
493-
let spill_offset = param_offset + 2;
491+
let param_offset = PREPARE_CALL_FIXED_PARAMS.len();
492+
let spill_offset = param_offset + 2; // skip caller/callee vmctx
494493

495494
let (values_vec_ptr, len) = self.compiler.allocate_stack_array_and_spill_args(
496495
&WasmFuncType::new(
@@ -517,7 +516,7 @@ impl<'a> TrampolineCompiler<'a> {
517516

518517
self.translate_intrinsic_libcall(
519518
vmctx,
520-
host::sync_prepare,
519+
host::prepare_call,
521520
&callee_args,
522521
TrapSentinel::Falsy,
523522
);
@@ -572,34 +571,6 @@ impl<'a> TrampolineCompiler<'a> {
572571
self.builder.ins().return_(&results);
573572
}
574573

575-
fn translate_async_prepare(&mut self, memory: Option<RuntimeMemoryIndex>) {
576-
match self.abi {
577-
Abi::Wasm => {}
578-
579-
Abi::Array => {
580-
// This code can only be called from (FACT-generated) Wasm, so
581-
// we don't need to support the array ABI.
582-
self.builder.ins().trap(TRAP_INTERNAL_ASSERT);
583-
return;
584-
}
585-
}
586-
587-
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
588-
let vmctx = args[0];
589-
590-
let mut callee_args = vec![vmctx, self.load_optional_memory(vmctx, memory)];
591-
592-
// remaining parameters
593-
callee_args.extend(args[2..].iter().copied());
594-
595-
self.translate_intrinsic_libcall(
596-
vmctx,
597-
host::async_prepare,
598-
&callee_args,
599-
TrapSentinel::Falsy,
600-
);
601-
}
602-
603574
fn translate_async_start(
604575
&mut self,
605576
callback: Option<RuntimeCallbackIndex>,

crates/environ/src/component.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,26 @@
3232
/// for transferring parameters.
3333
pub const MAX_FLAT_PARAMS: usize = 16;
3434

35+
/// Similar to `MAX_FLAT_PARAMS`, but used for async-lowered imports instead of
36+
/// sync ones.
37+
pub const MAX_FLAT_ASYNC_PARAMS: usize = 4;
38+
3539
/// Canonical ABI-defined constant for the maximum number of "flat" results.
3640
/// This number of results are returned directly from wasm and otherwise results
3741
/// are transferred through memory.
3842
pub const MAX_FLAT_RESULTS: usize = 1;
3943

44+
/// Sentinel value in `result_count_or_max_if_async` as part of the
45+
/// `prepare_call` libcall which indicates that preparation is being done for an
46+
/// async function that produces no result, aka there is no return pointer.
47+
pub const PREPARE_ASYNC_NO_RESULT: u32 = u32::MAX;
48+
49+
/// Sentinel value in `result_count_or_max_if_async` as part of the
50+
/// `prepare_call` libcall which indicates that preparation is being done for an
51+
/// async function that produces at least one result, aka there is a return
52+
/// pointer.
53+
pub const PREPARE_ASYNC_WITH_RESULT: u32 = u32::MAX - 1;
54+
4055
mod artifacts;
4156
mod info;
4257
mod names;
@@ -106,12 +121,22 @@ macro_rules! foreach_builtin_component_function {
106121
#[cfg(feature = "component-model-async")]
107122
subtask_cancel(vmctx: vmctx, caller_instance: u32, async_: u8, task_id: u32) -> u64;
108123
#[cfg(feature = "component-model-async")]
109-
sync_prepare(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, callee_instance: u32, task_return_type: u32, string_encoding: u32, result_count: u32, storage: ptr_u8, storage_len: size) -> bool;
124+
prepare_call(
125+
vmctx: vmctx,
126+
memory: ptr_u8,
127+
start: ptr_u8,
128+
return_: ptr_u8,
129+
caller_instance: u32,
130+
callee_instance: u32,
131+
task_return_type: u32,
132+
string_encoding: u32,
133+
result_count_or_max_if_async: u32,
134+
storage: ptr_u8,
135+
torage_len: size
136+
) -> bool;
110137
#[cfg(feature = "component-model-async")]
111138
sync_start(vmctx: vmctx, callback: ptr_u8, callee: ptr_u8, param_count: u32, storage: ptr_u8, storage_len: size) -> bool;
112139
#[cfg(feature = "component-model-async")]
113-
async_prepare(vmctx: vmctx, memory: ptr_u8, start: ptr_u8, return_: ptr_u8, caller_instance: u32, callee_instance: u32, task_return_type: u32, string_encoding: u32, params: u32, results: u32) -> bool;
114-
#[cfg(feature = "component-model-async")]
115140
async_start(vmctx: vmctx, callback: ptr_u8, post_return: ptr_u8, callee: ptr_u8, param_count: u32, result_count: u32, flags: u32) -> u64;
116141
#[cfg(feature = "component-model-async")]
117142
future_new(vmctx: vmctx, ty: u32) -> u64;

crates/environ/src/component/dfg.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,12 @@ pub enum Trampoline {
383383
ResourceTransferBorrow,
384384
ResourceEnterCall,
385385
ResourceExitCall,
386-
SyncPrepareCall {
386+
PrepareCall {
387387
memory: Option<MemoryId>,
388388
},
389389
SyncStartCall {
390390
callback: Option<CallbackId>,
391391
},
392-
AsyncPrepareCall {
393-
memory: Option<MemoryId>,
394-
},
395392
AsyncStartCall {
396393
callback: Option<CallbackId>,
397394
post_return: Option<PostReturnId>,
@@ -895,15 +892,12 @@ impl LinearizeDfg<'_> {
895892
Trampoline::ResourceTransferBorrow => info::Trampoline::ResourceTransferBorrow,
896893
Trampoline::ResourceEnterCall => info::Trampoline::ResourceEnterCall,
897894
Trampoline::ResourceExitCall => info::Trampoline::ResourceExitCall,
898-
Trampoline::SyncPrepareCall { memory } => info::Trampoline::SyncPrepareCall {
895+
Trampoline::PrepareCall { memory } => info::Trampoline::PrepareCall {
899896
memory: memory.map(|v| self.runtime_memory(v)),
900897
},
901898
Trampoline::SyncStartCall { callback } => info::Trampoline::SyncStartCall {
902899
callback: callback.map(|v| self.runtime_callback(v)),
903900
},
904-
Trampoline::AsyncPrepareCall { memory } => info::Trampoline::AsyncPrepareCall {
905-
memory: memory.map(|v| self.runtime_memory(v)),
906-
},
907901
Trampoline::AsyncStartCall {
908902
callback,
909903
post_return,

0 commit comments

Comments
 (0)