Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 0016cd2

Browse files
committed
add plumbing for task.cancel intrinsic
Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent de4ac25 commit 0016cd2

10 files changed

Lines changed: 95 additions & 24 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,18 +595,18 @@ lto = true
595595

596596
# TODO: remove this once we've switched to a wasm-tools/wit-bindgen release:
597597
[patch.crates-io]
598-
wasmparser = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
599-
wat = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
600-
wast = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
601-
wasmprinter = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
602-
wasm-encoder = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
603-
wasm-smith = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
604-
wasm-mutate = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
605-
wit-parser = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
606-
wit-component = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
607-
wasm-wave = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
608-
wasm-compose = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
609-
wasm-metadata = { git = "https://github.com/dicej/wasm-tools", branch = "subtask-cancel" }
598+
wasmparser = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
599+
wat = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
600+
wast = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
601+
wasmprinter = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
602+
wasm-encoder = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
603+
wasm-smith = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
604+
wasm-mutate = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
605+
wit-parser = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
606+
wit-component = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
607+
wasm-wave = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
608+
wasm-compose = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
609+
wasm-metadata = { git = "https://github.com/dicej/wasm-tools", branch = "task-cancel" }
610610
wit-bindgen = { git = "https://github.com/bytecodealliance/witx-bindgen" }
611611
wit-bindgen-rt = { git = "https://github.com/bytecodealliance/witx-bindgen" }
612612
wit-bindgen-rust-macro = { git = "https://github.com/bytecodealliance/witx-bindgen" }

crates/cranelift/src/compiler/component.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl<'a> TrampolineCompiler<'a> {
108108
Trampoline::TaskReturn { results, options } => {
109109
self.translate_task_return_call(*results, options)
110110
}
111+
Trampoline::TaskCancel { instance } => self.translate_task_cancel_call(*instance),
111112
Trampoline::WaitableSetNew { instance } => self.translate_waitable_set_new(*instance),
112113
Trampoline::WaitableSetWait {
113114
instance,
@@ -653,6 +654,25 @@ impl<'a> TrampolineCompiler<'a> {
653654
);
654655
}
655656

657+
fn translate_task_cancel_call(&mut self, caller_instance: RuntimeComponentInstanceIndex) {
658+
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
659+
let vmctx = args[0];
660+
661+
let callee_args = vec![
662+
vmctx,
663+
self.builder
664+
.ins()
665+
.iconst(ir::types::I32, i64::from(caller_instance.as_u32())),
666+
];
667+
668+
self.translate_intrinsic_libcall(
669+
vmctx,
670+
host::task_cancel,
671+
&callee_args,
672+
TrapSentinel::Falsy,
673+
);
674+
}
675+
656676
fn translate_task_wait_or_poll_call(
657677
&mut self,
658678
caller_instance: RuntimeComponentInstanceIndex,

crates/environ/src/component.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ macro_rules! foreach_builtin_component_function {
8888
#[cfg(feature = "component-model-async")]
8989
task_return(vmctx: vmctx, ty: u32, memory: ptr_u8, string_encoding: u8, storage: ptr_u8, storage_len: size) -> bool;
9090
#[cfg(feature = "component-model-async")]
91+
task_cancel(vmctx: vmctx, caller_instance: u32) -> bool;
92+
#[cfg(feature = "component-model-async")]
9193
waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;
9294
#[cfg(feature = "component-model-async")]
9395
waitable_set_wait(vmctx: vmctx, caller_instance: u32, async_: u8, memory: ptr_u8, set: u32, payload: u32) -> u64;

crates/environ/src/component/dfg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ pub enum Trampoline {
291291
results: TypeTupleIndex,
292292
options: CanonicalOptions,
293293
},
294+
TaskCancel {
295+
instance: RuntimeComponentInstanceIndex,
296+
},
294297
WaitableSetNew {
295298
instance: RuntimeComponentInstanceIndex,
296299
},
@@ -799,6 +802,9 @@ impl LinearizeDfg<'_> {
799802
results: *results,
800803
options: self.options(options),
801804
},
805+
Trampoline::TaskCancel { instance } => info::Trampoline::TaskCancel {
806+
instance: *instance,
807+
},
802808
Trampoline::WaitableSetNew { instance } => info::Trampoline::WaitableSetNew {
803809
instance: *instance,
804810
},

crates/environ/src/component/info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,14 @@ pub enum Trampoline {
720720
options: CanonicalOptions,
721721
},
722722

723+
/// A `task.cancel` intrinsic, which acknowledges a `CANCELLED` event
724+
/// delivered to a guest task previously created by a call to an async
725+
/// export.
726+
TaskCancel {
727+
/// The specific component instance which is calling the intrinsic.
728+
instance: RuntimeComponentInstanceIndex,
729+
},
730+
723731
/// A `waitable-set.new` intrinsic.
724732
WaitableSetNew {
725733
/// The specific component instance which is calling the intrinsic.
@@ -1051,6 +1059,7 @@ impl Trampoline {
10511059
ResourceDrop(i) => format!("component-resource-drop[{}]", i.as_u32()),
10521060
BackpressureSet { .. } => format!("backpressure-set"),
10531061
TaskReturn { .. } => format!("task-return"),
1062+
TaskCancel { .. } => format!("task-cancel"),
10541063
WaitableSetNew { .. } => format!("waitable-set-new"),
10551064
WaitableSetWait { .. } => format!("waitable-set-wait"),
10561065
WaitableSetPoll { .. } => format!("waitable-set-poll"),

crates/environ/src/component/translate.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ enum LocalInitializer<'data> {
196196
result: Option<ComponentValType>,
197197
options: LocalCanonicalOptions,
198198
},
199+
TaskCancel {
200+
func: ModuleInternedTypeIndex,
201+
},
199202
WaitableSetNew {
200203
func: ModuleInternedTypeIndex,
201204
},
@@ -679,6 +682,11 @@ impl<'a, 'data> Translator<'a, 'data> {
679682
options,
680683
}
681684
}
685+
wasmparser::CanonicalFunction::TaskCancel => {
686+
let func = self.core_func_signature(core_func_index)?;
687+
core_func_index += 1;
688+
LocalInitializer::TaskCancel { func }
689+
}
682690
wasmparser::CanonicalFunction::WaitableSetNew => {
683691
let func = self.core_func_signature(core_func_index)?;
684692
core_func_index += 1;

crates/environ/src/component/translate/inline.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,15 @@ impl<'a> Inliner<'a> {
698698
.push((*func, dfg::Trampoline::TaskReturn { results, options }));
699699
frame.funcs.push(dfg::CoreDef::Trampoline(index));
700700
}
701+
TaskCancel { func } => {
702+
let index = self.result.trampolines.push((
703+
*func,
704+
dfg::Trampoline::TaskCancel {
705+
instance: frame.instance,
706+
},
707+
));
708+
frame.funcs.push(dfg::CoreDef::Trampoline(index));
709+
}
701710
WaitableSetNew { func } => {
702711
let index = self.result.trampolines.push((
703712
*func,

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,14 @@ impl ComponentInstance {
19341934
Ok(())
19351935
}
19361936

1937+
pub(crate) fn task_cancel(
1938+
&mut self,
1939+
caller_instance: RuntimeComponentInstanceIndex,
1940+
) -> Result<()> {
1941+
_ = caller_instance;
1942+
todo!()
1943+
}
1944+
19371945
pub(crate) fn backpressure_set(
19381946
&mut self,
19391947
caller_instance: RuntimeComponentInstanceIndex,

crates/wasmtime/src/runtime/vm/component/libcalls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,15 @@ unsafe fn task_return(
615615
})
616616
}
617617

618+
#[cfg(feature = "component-model-async")]
619+
unsafe fn task_cancel(vmctx: NonNull<VMComponentContext>, caller_instance: u32) -> Result<()> {
620+
ComponentInstance::from_vmctx(vmctx, |instance| {
621+
instance.task_cancel(
622+
wasmtime_environ::component::RuntimeComponentInstanceIndex::from_u32(caller_instance),
623+
)
624+
})
625+
}
626+
618627
#[cfg(feature = "component-model-async")]
619628
unsafe fn waitable_set_new(
620629
vmctx: NonNull<VMComponentContext>,

0 commit comments

Comments
 (0)