Skip to content

Commit

Permalink
Rename task.yield to yield (#2066)
Browse files Browse the repository at this point in the history
Catching up to WebAssembly/component-model#438 a bit
  • Loading branch information
alexcrichton authored Feb 24, 2025
1 parent 1dae3da commit 53d1e22
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 72 deletions.
4 changes: 2 additions & 2 deletions crates/wasm-encoder/src/component/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ impl ComponentBuilder {
}

/// Declares a new `task.yield` intrinsic.
pub fn task_yield(&mut self, async_: bool) -> u32 {
self.canonical_functions().task_yield(async_);
pub fn yield_(&mut self, async_: bool) -> u32 {
self.canonical_functions().yield_(async_);
inc(&mut self.core_funcs)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-encoder/src/component/canonicals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl CanonicalFunctionSection {
/// are able to make progress, if any.
///
/// If `async_` is true, the caller instance may be reentered.
pub fn task_yield(&mut self, async_: bool) -> &mut Self {
pub fn yield_(&mut self, async_: bool) -> &mut Self {
self.bytes.push(0x0c);
self.bytes.push(if async_ { 1 } else { 0 });
self.num_added += 1;
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-encoder/src/reencode/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,8 @@ pub mod component_utils {
options.iter().map(|o| reencoder.canonical_option(*o)),
);
}
wasmparser::CanonicalFunction::TaskYield { async_ } => {
section.task_yield(async_);
wasmparser::CanonicalFunction::Yield { async_ } => {
section.yield_(async_);
}
wasmparser::CanonicalFunction::SubtaskDrop => {
section.subtask_drop();
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmparser/src/readers/component/canonicals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub enum CanonicalFunction {
},
/// A function which yields control to the host so that other tasks are able
/// to make progress, if any.
TaskYield {
Yield {
/// If `true`, indicates the caller instance maybe reentered.
async_: bool,
},
Expand Down Expand Up @@ -282,7 +282,7 @@ impl<'a> FromReader<'a> for CanonicalFunction {
result: crate::read_resultlist(reader)?,
options: read_opts(reader)?,
},
0x0c => CanonicalFunction::TaskYield {
0x0c => CanonicalFunction::Yield {
async_: reader.read()?,
},
0x0d => CanonicalFunction::SubtaskDrop,
Expand Down
11 changes: 3 additions & 8 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,7 @@ impl ComponentState {
CanonicalFunction::TaskReturn { result, options } => {
self.task_return(&result, &options, types, offset, features)
}
CanonicalFunction::TaskYield { async_ } => {
self.task_yield(async_, types, offset, features)
}
CanonicalFunction::Yield { async_ } => self.yield_(async_, types, offset, features),
CanonicalFunction::SubtaskDrop => self.subtask_drop(types, offset, features),
CanonicalFunction::StreamNew { ty } => self.stream_new(ty, types, offset, features),
CanonicalFunction::StreamRead { ty, options } => {
Expand Down Expand Up @@ -1249,18 +1247,15 @@ impl ComponentState {
Ok(())
}

fn task_yield(
fn yield_(
&mut self,
_async_: bool,
types: &mut TypeAlloc,
offset: usize,
features: &WasmFeatures,
) -> Result<()> {
if !features.component_model_async() {
bail!(
offset,
"`task.yield` requires the component model async feature"
)
bail!(offset, "`yield` requires the component model async feature")
}

self.core_funcs
Expand Down
16 changes: 8 additions & 8 deletions crates/wasmprinter/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl Printer<'_, '_> {
self.print_idx(&state.core.func_names, *idx)?;
self.end_group()?;
}
CanonicalOption::Async => self.result.write_str("async")?,
CanonicalOption::Async => self.print_type_keyword("async")?,
CanonicalOption::Callback(idx) => {
self.start_group("callback ")?;
self.print_idx(&state.core.func_names, *idx)?;
Expand Down Expand Up @@ -928,10 +928,10 @@ impl Printer<'_, '_> {
Ok(())
})?;
}
CanonicalFunction::TaskYield { async_ } => {
self.print_intrinsic(state, "canon task.yield", &|me, _| {
CanonicalFunction::Yield { async_ } => {
self.print_intrinsic(state, "canon yield", &|me, _| {
if async_ {
me.result.write_str(" async")?;
me.print_type_keyword(" async")?;
}
Ok(())
})?;
Expand Down Expand Up @@ -960,7 +960,7 @@ impl Printer<'_, '_> {
self.print_intrinsic(state, "canon stream.cancel-read ", &|me, state| {
me.print_idx(&state.component.type_names, ty)?;
if async_ {
me.result.write_str(" async")?;
me.print_type_keyword(" async")?;
}
Ok(())
})?;
Expand All @@ -969,7 +969,7 @@ impl Printer<'_, '_> {
self.print_intrinsic(state, "canon stream.cancel-write ", &|me, state| {
me.print_idx(&state.component.type_names, ty)?;
if async_ {
me.result.write_str(" async")?;
me.print_type_keyword(" async")?;
}
Ok(())
})?;
Expand Down Expand Up @@ -1005,7 +1005,7 @@ impl Printer<'_, '_> {
self.print_intrinsic(state, "canon future.cancel-read ", &|me, state| {
me.print_idx(&state.component.type_names, ty)?;
if async_ {
me.result.write_str(" async")?;
me.print_type_keyword(" async")?;
}
Ok(())
})?;
Expand All @@ -1014,7 +1014,7 @@ impl Printer<'_, '_> {
self.print_intrinsic(state, "canon future.cancel-write ", &|me, state| {
me.print_idx(&state.component.type_names, ty)?;
if async_ {
me.result.write_str(" async")?;
me.print_type_keyword(" async")?;
}
Ok(())
})?;
Expand Down
4 changes: 2 additions & 2 deletions crates/wast/src/component/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ impl<'a> Encoder<'a> {
info.opts.iter().map(Into::into),
);
}
CanonicalFuncKind::TaskYield(info) => {
CanonicalFuncKind::Yield(info) => {
self.core_func_names.push(name);
self.funcs.task_yield(info.async_);
self.funcs.yield_(info.async_);
}
CanonicalFuncKind::SubtaskDrop => {
self.core_func_names.push(name);
Expand Down
4 changes: 2 additions & 2 deletions crates/wast/src/component/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ impl<'a> Expander<'a> {
name: func.name,
kind: CanonicalFuncKind::TaskReturn(info),
}),
CoreFuncKind::TaskYield(info) => ComponentField::CanonicalFunc(CanonicalFunc {
CoreFuncKind::Yield(info) => ComponentField::CanonicalFunc(CanonicalFunc {
span: func.span,
id: func.id,
name: func.name,
kind: CanonicalFuncKind::TaskYield(info),
kind: CanonicalFuncKind::Yield(info),
}),
CoreFuncKind::SubtaskDrop => ComponentField::CanonicalFunc(CanonicalFunc {
span: func.span,
Expand Down
14 changes: 7 additions & 7 deletions crates/wast/src/component/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub enum CoreFuncKind<'a> {
ThreadAvailableParallelism(CanonThreadAvailableParallelism),
BackpressureSet,
TaskReturn(CanonTaskReturn<'a>),
TaskYield(CanonTaskYield),
Yield(CanonYield),
SubtaskDrop,
StreamNew(CanonStreamNew<'a>),
StreamRead(CanonStreamRead<'a>),
Expand Down Expand Up @@ -110,8 +110,8 @@ impl<'a> Parse<'a> for CoreFuncKind<'a> {
Ok(CoreFuncKind::BackpressureSet)
} else if l.peek::<kw::task_return>()? {
Ok(CoreFuncKind::TaskReturn(parser.parse()?))
} else if l.peek::<kw::task_yield>()? {
Ok(CoreFuncKind::TaskYield(parser.parse()?))
} else if l.peek::<kw::yield_>()? {
Ok(CoreFuncKind::Yield(parser.parse()?))
} else if l.peek::<kw::subtask_drop>()? {
parser.parse::<kw::subtask_drop>()?;
Ok(CoreFuncKind::SubtaskDrop)
Expand Down Expand Up @@ -357,7 +357,7 @@ pub enum CanonicalFuncKind<'a> {

BackpressureSet,
TaskReturn(CanonTaskReturn<'a>),
TaskYield(CanonTaskYield),
Yield(CanonYield),
SubtaskDrop,
StreamNew(CanonStreamNew<'a>),
StreamRead(CanonStreamRead<'a>),
Expand Down Expand Up @@ -611,15 +611,15 @@ impl<'a> Parse<'a> for CanonWaitableSetPoll<'a> {

/// Information relating to the `task.yield` intrinsic.
#[derive(Debug)]
pub struct CanonTaskYield {
pub struct CanonYield {
/// If true, the component instance may be reentered during a call to this
/// intrinsic.
pub async_: bool,
}

impl<'a> Parse<'a> for CanonTaskYield {
impl<'a> Parse<'a> for CanonYield {
fn parse(parser: Parser<'a>) -> Result<Self> {
parser.parse::<kw::task_yield>()?;
parser.parse::<kw::yield_>()?;
let async_ = parser.parse::<Option<kw::r#async>>()?.is_some();

Ok(Self { async_ })
Expand Down
4 changes: 2 additions & 2 deletions crates/wast/src/component/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<'a> Resolver<'a> {
}
CanonicalFuncKind::ThreadAvailableParallelism(_)
| CanonicalFuncKind::BackpressureSet
| CanonicalFuncKind::TaskYield(_)
| CanonicalFuncKind::Yield(_)
| CanonicalFuncKind::SubtaskDrop
| CanonicalFuncKind::ErrorContextDrop => {}
CanonicalFuncKind::TaskReturn(info) => {
Expand Down Expand Up @@ -971,7 +971,7 @@ impl<'a> ComponentState<'a> {
| CanonicalFuncKind::ThreadAvailableParallelism(_)
| CanonicalFuncKind::BackpressureSet
| CanonicalFuncKind::TaskReturn(_)
| CanonicalFuncKind::TaskYield(_)
| CanonicalFuncKind::Yield(_)
| CanonicalFuncKind::SubtaskDrop
| CanonicalFuncKind::StreamNew(_)
| CanonicalFuncKind::StreamRead(_)
Expand Down
2 changes: 1 addition & 1 deletion crates/wast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ pub mod kw {
custom_keyword!(thread_available_parallelism = "thread.available_parallelism");
custom_keyword!(backpressure_set = "backpressure.set");
custom_keyword!(task_return = "task.return");
custom_keyword!(task_yield = "task.yield");
custom_keyword!(yield_ = "yield");
custom_keyword!(subtask_drop = "subtask.drop");
custom_keyword!(stream_new = "stream.new");
custom_keyword!(stream_read = "stream.read");
Expand Down
6 changes: 3 additions & 3 deletions crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,8 +1652,8 @@ impl<'a> EncodingState<'a> {
Ok(self
.materialize_shim_import(shims, &ShimKind::WaitableSetPoll { async_: *async_ }))
}
Import::TaskYield { async_ } => {
let index = self.component.task_yield(*async_);
Import::Yield { async_ } => {
let index = self.component.yield_(*async_);
Ok((ExportKind::Func, index))
}
Import::SubtaskDrop => {
Expand Down Expand Up @@ -2144,7 +2144,7 @@ impl<'a> Shims<'a> {
| Import::ExportedResourceNew(..)
| Import::ErrorContextDrop
| Import::BackpressureSet
| Import::TaskYield { .. }
| Import::Yield { .. }
| Import::SubtaskDrop
| Import::FutureNew(..)
| Import::StreamNew(..)
Expand Down
17 changes: 9 additions & 8 deletions crates/wit-component/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ pub enum Import {
/// A `waitable.join` intrinsic.
WaitableJoin,

/// A `canon task.wait` intrinsic.
/// A `canon yield` intrinsic.
///
/// This allows the guest to yield (e.g. during an computationally-intensive
/// operation) and allow other subtasks to make progress.
TaskYield { async_: bool },
Yield { async_: bool },

/// A `canon subtask.drop` intrinsic.
///
Expand Down Expand Up @@ -610,10 +610,11 @@ impl ImportMap {
return Ok(Import::WaitableJoin);
}

if Some(name) == names.task_yield() {
if Some(name) == names.yield_() {
validate_not_async()?;
let expected = FuncType::new([], []);
validate_func_sig(name, &expected, ty)?;
return Ok(Import::TaskYield {
return Ok(Import::Yield {
async_: abi == AbiVariant::GuestImportAsync,
});
}
Expand Down Expand Up @@ -1413,7 +1414,7 @@ trait NameMangling {
fn waitable_set_poll(&self) -> Option<&str>;
fn waitable_set_drop(&self) -> Option<&str>;
fn waitable_join(&self) -> Option<&str>;
fn task_yield(&self) -> Option<&str>;
fn yield_(&self) -> Option<&str>;
fn subtask_drop(&self) -> Option<&str>;
fn callback_name<'a>(&self, s: &'a str) -> Option<&'a str>;
fn async_name<'a>(&self, s: &'a str) -> Option<&'a str>;
Expand Down Expand Up @@ -1500,7 +1501,7 @@ impl NameMangling for Standard {
fn waitable_join(&self) -> Option<&str> {
None
}
fn task_yield(&self) -> Option<&str> {
fn yield_(&self) -> Option<&str> {
None
}
fn subtask_drop(&self) -> Option<&str> {
Expand Down Expand Up @@ -1685,8 +1686,8 @@ impl NameMangling for Legacy {
fn waitable_join(&self) -> Option<&str> {
Some("[waitable-join]")
}
fn task_yield(&self) -> Option<&str> {
Some("[task-yield]")
fn yield_(&self) -> Option<&str> {
Some("[yield]")
}
fn subtask_drop(&self) -> Option<&str> {
Some("[subtask-drop]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(import "$root" "[waitable-set-poll]" (func (;5;) (type 3)))
(import "$root" "[waitable-set-drop]" (func (;6;) (type 0)))
(import "$root" "[waitable-join]" (func (;7;) (type 1)))
(import "$root" "[task-yield]" (func (;8;) (type 4)))
(import "$root" "[yield]" (func (;8;) (type 4)))
(import "$root" "[subtask-drop]" (func (;9;) (type 0)))
(import "$root" "[error-context-new;encoding=utf8]" (func (;10;) (type 3)))
(import "$root" "[error-context-debug-message;encoding=utf8;realloc=cabi_realloc]" (func (;11;) (type 1)))
Expand Down Expand Up @@ -105,7 +105,7 @@
(alias core export 0 "1" (core func (;3;)))
(core func (;4;) (canon waitable-set.drop))
(core func (;5;) (canon waitable.join))
(core func (;6;) (canon task.yield))
(core func (;6;) (canon yield))
(core func (;7;) (canon subtask.drop))
(alias core export 0 "2" (core func (;8;)))
(alias core export 0 "3" (core func (;9;)))
Expand All @@ -117,7 +117,7 @@
(export "[waitable-set-poll]" (func 3))
(export "[waitable-set-drop]" (func 4))
(export "[waitable-join]" (func 5))
(export "[task-yield]" (func 6))
(export "[yield]" (func 6))
(export "[subtask-drop]" (func 7))
(export "[error-context-new;encoding=utf8]" (func 8))
(export "[error-context-debug-message;encoding=utf8;realloc=cabi_realloc]" (func 9))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(import "$root" "[waitable-set-poll]" (func (param i32 i32) (result i32)))
(import "$root" "[waitable-set-drop]" (func (param i32)))
(import "$root" "[waitable-join]" (func (param i32 i32)))
(import "$root" "[task-yield]" (func))
(import "$root" "[yield]" (func))
(import "$root" "[subtask-drop]" (func (param i32)))
(import "$root" "[error-context-new;encoding=utf8]" (func (param i32 i32) (result i32)))
(import "$root" "[error-context-debug-message;encoding=utf8;realloc=cabi_realloc]" (func (param i32 i32)))
Expand Down
2 changes: 1 addition & 1 deletion src/bin/wasm-tools/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl<'a> Dump<'a> {
| CanonicalFunction::WaitableSetPoll { .. }
| CanonicalFunction::WaitableSetDrop
| CanonicalFunction::WaitableJoin
| CanonicalFunction::TaskYield { .. }
| CanonicalFunction::Yield { .. }
| CanonicalFunction::SubtaskDrop
| CanonicalFunction::StreamNew { .. }
| CanonicalFunction::StreamRead { .. }
Expand Down
18 changes: 9 additions & 9 deletions tests/local/component-model-async/task-builtins.wast
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,25 @@
"type mismatch for export `waitable.join` of module instantiation argument ``"
)

;; task.yield
;; yield
(component
(core module $m
(import "" "task.yield" (func $task-yield))
(import "" "yield" (func $yield))
)
(core func $task-yield (canon task.yield async))
(core instance $i (instantiate $m (with "" (instance (export "task.yield" (func $task-yield))))))
(core func $yield (canon yield async))
(core instance $i (instantiate $m (with "" (instance (export "yield" (func $yield))))))
)

;; task.yield; incorrect type
;; yield; incorrect type
(assert_invalid
(component
(core module $m
(import "" "task.yield" (func $task-yield (param i32) (result i32)))
(import "" "yield" (func $yield (param i32) (result i32)))
)
(core func $task-yield (canon task.yield async))
(core instance $i (instantiate $m (with "" (instance (export "task.yield" (func $task-yield))))))
(core func $yield (canon yield async))
(core instance $i (instantiate $m (with "" (instance (export "yield" (func $yield))))))
)
"type mismatch for export `task.yield` of module instantiation argument ``"
"type mismatch for export `yield` of module instantiation argument ``"
)

;; subtask.drop
Expand Down
Loading

0 comments on commit 53d1e22

Please sign in to comment.