Skip to content

Commit 7791dc9

Browse files
committed
Update component async bits for new intrinsics
1 parent 20b2a14 commit 7791dc9

File tree

11 files changed

+332
-99
lines changed

11 files changed

+332
-99
lines changed

crates/cranelift/src/compiler/component.rs

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,36 @@ impl<'a> TrampolineCompiler<'a> {
103103
Trampoline::ResourceNew(ty) => self.translate_resource_new(*ty),
104104
Trampoline::ResourceRep(ty) => self.translate_resource_rep(*ty),
105105
Trampoline::ResourceDrop(ty) => self.translate_resource_drop(*ty),
106-
Trampoline::TaskBackpressure { instance } => {
107-
self.translate_task_backpressure_call(*instance)
106+
Trampoline::BackpressureSet { instance } => {
107+
self.translate_backpressure_set_call(*instance)
108108
}
109-
Trampoline::TaskReturn { results } => self.translate_task_return_call(*results),
110-
Trampoline::TaskWait {
109+
Trampoline::TaskReturn { results, options } => {
110+
self.translate_task_return_call(*results, options)
111+
}
112+
Trampoline::WaitableSetNew { instance } => self.translate_waitable_set_new(*instance),
113+
Trampoline::WaitableSetWait {
111114
instance,
112115
async_,
113116
memory,
114-
} => {
115-
self.translate_task_wait_or_poll_call(*instance, *async_, *memory, host::task_wait)
116-
}
117+
} => self.translate_task_wait_or_poll_call(
118+
*instance,
119+
*async_,
120+
*memory,
121+
host::waitable_set_wait,
122+
),
117123
Trampoline::WaitableSetPoll {
118124
instance,
119125
async_,
120126
memory,
121-
} => {
122-
self.translate_task_wait_or_poll_call(*instance, *async_, *memory, host::task_poll)
123-
}
124-
Trampoline::TaskYield { async_ } => self.translate_task_yield_call(*async_),
127+
} => self.translate_task_wait_or_poll_call(
128+
*instance,
129+
*async_,
130+
*memory,
131+
host::waitable_set_poll,
132+
),
133+
Trampoline::WaitableSetDrop { instance } => self.translate_waitable_set_drop(*instance),
134+
Trampoline::WaitableJoin { instance } => self.translate_waitable_join(*instance),
135+
Trampoline::Yield { async_ } => self.translate_yield_call(*async_),
125136
Trampoline::SubtaskDrop { instance } => self.translate_subtask_drop_call(*instance),
126137
Trampoline::StreamNew { ty } => self.translate_future_or_stream_call(
127138
&[ty.as_u32()],
@@ -375,7 +386,7 @@ impl<'a> TrampolineCompiler<'a> {
375386
}
376387
}
377388

378-
fn translate_task_return_call(&mut self, results: TypeTupleIndex) {
389+
fn translate_task_return_call(&mut self, results: TypeTupleIndex, options: &CanonicalOptions) {
379390
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
380391
let vmctx = args[0];
381392

@@ -394,6 +405,60 @@ impl<'a> TrampolineCompiler<'a> {
394405
);
395406
}
396407

408+
fn translate_waitable_set_new(&mut self, instance: RuntimeComponentInstanceIndex) {
409+
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
410+
let vmctx = args[0];
411+
412+
let instance = self
413+
.builder
414+
.ins()
415+
.iconst(ir::types::I32, i64::from(instance.as_u32()));
416+
417+
self.translate_intrinsic_libcall(
418+
vmctx,
419+
host::waitable_set_new,
420+
&[vmctx, instance],
421+
TrapSentinel::NegativeOne,
422+
);
423+
}
424+
425+
fn translate_waitable_set_drop(&mut self, instance: RuntimeComponentInstanceIndex) {
426+
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
427+
let vmctx = args[0];
428+
let set = args[2];
429+
430+
let instance = self
431+
.builder
432+
.ins()
433+
.iconst(ir::types::I32, i64::from(instance.as_u32()));
434+
435+
self.translate_intrinsic_libcall(
436+
vmctx,
437+
host::waitable_set_drop,
438+
&[vmctx, instance, set],
439+
TrapSentinel::Falsy,
440+
);
441+
}
442+
443+
fn translate_waitable_join(&mut self, instance: RuntimeComponentInstanceIndex) {
444+
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
445+
let vmctx = args[0];
446+
let set = args[2];
447+
let waitable = args[3];
448+
449+
let instance = self
450+
.builder
451+
.ins()
452+
.iconst(ir::types::I32, i64::from(instance.as_u32()));
453+
454+
self.translate_intrinsic_libcall(
455+
vmctx,
456+
host::waitable_join,
457+
&[vmctx, instance, set, waitable],
458+
TrapSentinel::Falsy,
459+
);
460+
}
461+
397462
fn translate_sync_enter(&mut self) {
398463
match self.abi {
399464
Abi::Wasm => {}
@@ -534,7 +599,7 @@ impl<'a> TrampolineCompiler<'a> {
534599
self.translate_intrinsic_libcall(vmctx, get_libcall, &callee_args, sentinel);
535600
}
536601

537-
fn translate_task_backpressure_call(&mut self, caller_instance: RuntimeComponentInstanceIndex) {
602+
fn translate_backpressure_set_call(&mut self, caller_instance: RuntimeComponentInstanceIndex) {
538603
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
539604
let vmctx = args[0];
540605

@@ -549,7 +614,7 @@ impl<'a> TrampolineCompiler<'a> {
549614

550615
self.translate_intrinsic_libcall(
551616
vmctx,
552-
host::task_backpressure,
617+
host::backpressure_set,
553618
&callee_args,
554619
TrapSentinel::Falsy,
555620
);
@@ -586,7 +651,7 @@ impl<'a> TrampolineCompiler<'a> {
586651
);
587652
}
588653

589-
fn translate_task_yield_call(&mut self, async_: bool) {
654+
fn translate_yield_call(&mut self, async_: bool) {
590655
let args = self.builder.func.dfg.block_params(self.block0).to_vec();
591656
let vmctx = args[0];
592657

@@ -597,12 +662,7 @@ impl<'a> TrampolineCompiler<'a> {
597662
.iconst(ir::types::I8, if async_ { 1 } else { 0 }),
598663
];
599664

600-
self.translate_intrinsic_libcall(
601-
vmctx,
602-
host::task_yield,
603-
&callee_args,
604-
TrapSentinel::Falsy,
605-
);
665+
self.translate_intrinsic_libcall(vmctx, host::yield_, &callee_args, TrapSentinel::Falsy);
606666
}
607667

608668
fn translate_subtask_drop_call(&mut self, caller_instance: RuntimeComponentInstanceIndex) {

crates/environ/src/component.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,21 @@ macro_rules! foreach_builtin_component_function {
8484
resource_exit_call(vmctx: vmctx) -> bool;
8585

8686
#[cfg(feature = "component-model-async")]
87-
task_backpressure(vmctx: vmctx, caller_instance: u32, enabled: u32) -> bool;
87+
backpressure_set(vmctx: vmctx, caller_instance: u32, enabled: u32) -> bool;
8888
#[cfg(feature = "component-model-async")]
8989
task_return(vmctx: vmctx, ty: u32, storage: ptr_u8, storage_len: size) -> bool;
9090
#[cfg(feature = "component-model-async")]
91-
task_wait(vmctx: vmctx, caller_instance: u32, async_: u8, memory: ptr_u8, payload: u32) -> u64;
91+
waitable_set_new(vmctx: vmctx, caller_instance: u32) -> u64;
9292
#[cfg(feature = "component-model-async")]
93-
task_poll(vmctx: vmctx, caller_instance: u32, async_: u8, memory: ptr_u8, payload: u32) -> u64;
93+
waitable_set_wait(vmctx: vmctx, caller_instance: u32, set: u32, async_: u8, memory: ptr_u8, payload: u32) -> u64;
9494
#[cfg(feature = "component-model-async")]
95-
task_yield(vmctx: vmctx, async_: u8) -> bool;
95+
waitable_set_poll(vmctx: vmctx, caller_instance: u32, set: u32, async_: u8, memory: ptr_u8, payload: u32) -> u64;
96+
#[cfg(feature = "component-model-async")]
97+
waitable_set_drop(vmctx: vmctx, caller_instance: u32, set: u32) -> bool;
98+
#[cfg(feature = "component-model-async")]
99+
waitable_join(vmctx: vmctx, caller_instance: u32, set: u32, waitable: u32) -> bool;
100+
#[cfg(feature = "component-model-async")]
101+
yield_(vmctx: vmctx, async_: u8) -> bool;
96102
#[cfg(feature = "component-model-async")]
97103
subtask_drop(vmctx: vmctx, caller_instance: u32, task_id: u32) -> bool;
98104
#[cfg(feature = "component-model-async")]
@@ -116,7 +122,7 @@ macro_rules! foreach_builtin_component_function {
116122
#[cfg(feature = "component-model-async")]
117123
future_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
118124
#[cfg(feature = "component-model-async")]
119-
future_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
125+
future_close_readable(vmctx: vmctx, ty: u32, reader: u32, error: u32) -> bool;
120126
#[cfg(feature = "component-model-async")]
121127
stream_new(vmctx: vmctx, ty: u32) -> u64;
122128
#[cfg(feature = "component-model-async")]
@@ -130,7 +136,7 @@ macro_rules! foreach_builtin_component_function {
130136
#[cfg(feature = "component-model-async")]
131137
stream_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
132138
#[cfg(feature = "component-model-async")]
133-
stream_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
139+
stream_close_readable(vmctx: vmctx, ty: u32, reader: u32, error: u32) -> bool;
134140
#[cfg(feature = "component-model-async")]
135141
flat_stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
136142
#[cfg(feature = "component-model-async")]

crates/environ/src/component/dfg.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,12 @@ pub enum Trampoline {
288288
},
289289
TaskReturn {
290290
results: TypeTupleIndex,
291+
options: CanonicalOptions,
292+
},
293+
WaitableSetNew {
294+
instance: RuntimeComponentInstanceIndex,
291295
},
292-
TaskWait {
296+
WaitableSetWait {
293297
instance: RuntimeComponentInstanceIndex,
294298
async_: bool,
295299
memory: MemoryId,
@@ -781,10 +785,14 @@ impl LinearizeDfg<'_> {
781785
Trampoline::BackpressureSet { instance } => info::Trampoline::BackpressureSet {
782786
instance: *instance,
783787
},
784-
Trampoline::TaskReturn { results } => {
785-
info::Trampoline::TaskReturn { results: *results }
786-
}
787-
Trampoline::TaskWait {
788+
Trampoline::TaskReturn { results, options } => info::Trampoline::TaskReturn {
789+
results: *results,
790+
options: self.options(options),
791+
},
792+
Trampoline::WaitableSetNew { instance } => info::Trampoline::WaitableSetNew {
793+
instance: *instance,
794+
},
795+
Trampoline::WaitableSetWait {
788796
instance,
789797
async_,
790798
memory,

crates/environ/src/component/info.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,9 @@ pub enum Trampoline {
692692
TaskReturn {
693693
/// Tuple representing the result types this intrinsic accepts.
694694
results: TypeTupleIndex,
695+
696+
/// The canonical ABI options specified for this intrinsic.
697+
options: CanonicalOptions,
695698
},
696699

697700
/// A `waitable-set.new` intrinsic.
@@ -1002,11 +1005,14 @@ impl Trampoline {
10021005
ResourceNew(i) => format!("component-resource-new[{}]", i.as_u32()),
10031006
ResourceRep(i) => format!("component-resource-rep[{}]", i.as_u32()),
10041007
ResourceDrop(i) => format!("component-resource-drop[{}]", i.as_u32()),
1005-
TaskBackpressure { .. } => format!("task-backpressure"),
1008+
BackpressureSet { .. } => format!("backpressure-set"),
10061009
TaskReturn { .. } => format!("task-return"),
1007-
TaskWait { .. } => format!("task-wait"),
1008-
TaskPoll { .. } => format!("task-poll"),
1009-
TaskYield { .. } => format!("task-yield"),
1010+
WaitableSetNew { .. } => format!("waitable-set-new"),
1011+
WaitableSetWait { .. } => format!("waitable-set-wait"),
1012+
WaitableSetPoll { .. } => format!("waitable-set-poll"),
1013+
WaitableSetDrop { .. } => format!("waitable-set-drop"),
1014+
WaitableJoin { .. } => format!("waitable-join"),
1015+
Yield { .. } => format!("yield"),
10101016
SubtaskDrop { .. } => format!("subtask-drop"),
10111017
StreamNew { .. } => format!("stream-new"),
10121018
StreamRead { .. } => format!("stream-read"),

crates/environ/src/component/translate.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ enum LocalInitializer<'data> {
194194
TaskReturn {
195195
func: ModuleInternedTypeIndex,
196196
result: Option<ComponentValType>,
197+
options: LocalCanonicalOptions,
197198
},
198199
WaitableSetNew {
199200
func: ModuleInternedTypeIndex,
@@ -646,7 +647,7 @@ impl<'a, 'data> Translator<'a, 'data> {
646647
core_func_index += 1;
647648
LocalInitializer::BackpressureSet { func: core_type }
648649
}
649-
wasmparser::CanonicalFunction::TaskReturn { result } => {
650+
wasmparser::CanonicalFunction::TaskReturn { result, options } => {
650651
let result = result.map(|ty| match ty {
651652
wasmparser::ComponentValType::Primitive(ty) => {
652653
ComponentValType::Primitive(ty)
@@ -655,9 +656,14 @@ impl<'a, 'data> Translator<'a, 'data> {
655656
ComponentValType::Type(types.component_defined_type_at(ty))
656657
}
657658
});
659+
let options = self.canonical_options(&options);
658660
let func = self.core_func_signature(core_func_index)?;
659661
core_func_index += 1;
660-
LocalInitializer::TaskReturn { func, result }
662+
LocalInitializer::TaskReturn {
663+
func,
664+
result,
665+
options,
666+
}
661667
}
662668
wasmparser::CanonicalFunction::WaitableSetNew => {
663669
let func = self.core_func_signature(core_func_index)?;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,16 +679,22 @@ impl<'a> Inliner<'a> {
679679
));
680680
frame.funcs.push(dfg::CoreDef::Trampoline(index));
681681
}
682-
TaskReturn { func, result } => {
682+
TaskReturn {
683+
func,
684+
result,
685+
options,
686+
} => {
683687
let results = result
684688
.iter()
685689
.map(|ty| types.valtype(frame.translation.types_ref(), ty))
686690
.collect::<Result<_>>()?;
687691
let results = types.new_tuple_type(results);
692+
let options = self.adapter_options(frame, types, options);
693+
let options = self.canonical_options(options);
688694
let index = self
689695
.result
690696
.trampolines
691-
.push((*func, dfg::Trampoline::TaskReturn { results }));
697+
.push((*func, dfg::Trampoline::TaskReturn { results, options }));
692698
frame.funcs.push(dfg::CoreDef::Trampoline(index));
693699
}
694700
WaitableSetNew { func } => {

0 commit comments

Comments
 (0)