Skip to content

Commit 1b4f01f

Browse files
committed
implement {future,stream}.forward
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
1 parent 74d66b1 commit 1b4f01f

11 files changed

Lines changed: 2206 additions & 22 deletions

File tree

crates/cranelift/src/compiler/component.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ impl<'a> TrampolineCompiler<'a> {
401401
);
402402
}
403403
}
404+
Trampoline::StreamForward { instance, ty } => {
405+
self.translate_libcall(
406+
host::stream_forward,
407+
TrapSentinel::Falsy,
408+
WasmArgs::InRegisters,
409+
|me, params| {
410+
params.push(me.index_value(*instance));
411+
params.push(me.index_value(*ty));
412+
},
413+
);
414+
}
404415
Trampoline::StreamCancelRead {
405416
instance,
406417
ty,
@@ -498,6 +509,17 @@ impl<'a> TrampolineCompiler<'a> {
498509
},
499510
);
500511
}
512+
Trampoline::FutureForward { instance, ty } => {
513+
self.translate_libcall(
514+
host::future_forward,
515+
TrapSentinel::Falsy,
516+
WasmArgs::InRegisters,
517+
|me, params| {
518+
params.push(me.index_value(*instance));
519+
params.push(me.index_value(*ty));
520+
},
521+
);
522+
}
501523
Trampoline::FutureCancelRead {
502524
instance,
503525
ty,
@@ -1551,13 +1573,15 @@ impl<'a> TrampolineCompiler<'a> {
15511573
| Trampoline::StreamNew { instance, .. }
15521574
| Trampoline::StreamRead { instance, .. }
15531575
| Trampoline::StreamWrite { instance, .. }
1576+
| Trampoline::StreamForward { instance, .. }
15541577
| Trampoline::StreamCancelRead { instance, .. }
15551578
| Trampoline::StreamCancelWrite { instance, .. }
15561579
| Trampoline::StreamDropReadable { instance, .. }
15571580
| Trampoline::StreamDropWritable { instance, .. }
15581581
| Trampoline::FutureNew { instance, .. }
15591582
| Trampoline::FutureRead { instance, .. }
15601583
| Trampoline::FutureWrite { instance, .. }
1584+
| Trampoline::FutureForward { instance, .. }
15611585
| Trampoline::FutureCancelRead { instance, .. }
15621586
| Trampoline::FutureCancelWrite { instance, .. }
15631587
| Trampoline::FutureDropReadable { instance, .. }

crates/environ/src/component.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ macro_rules! foreach_builtin_component_function {
146146
#[cfg(feature = "component-model-async")]
147147
future_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, future: u32, address: u32) -> u64;
148148
#[cfg(feature = "component-model-async")]
149+
future_forward(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32, writer: u32) -> bool;
150+
#[cfg(feature = "component-model-async")]
149151
future_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;
150152
#[cfg(feature = "component-model-async")]
151153
future_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;
@@ -160,6 +162,8 @@ macro_rules! foreach_builtin_component_function {
160162
#[cfg(feature = "component-model-async")]
161163
stream_read(vmctx: vmctx, caller_instance: u32, ty: u32, options: u32, stream: u32, address: u32, count: u32) -> u64;
162164
#[cfg(feature = "component-model-async")]
165+
stream_forward(vmctx: vmctx, caller_instance: u32, ty: u32, reader: u32, writer: u32) -> bool;
166+
#[cfg(feature = "component-model-async")]
163167
stream_cancel_write(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, writer: u32) -> u64;
164168
#[cfg(feature = "component-model-async")]
165169
stream_cancel_read(vmctx: vmctx, caller_instance: u32, ty: u32, async_: u8, reader: u32) -> u64;

crates/environ/src/component/dfg.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ pub enum Trampoline {
390390
ty: TypeStreamTableIndex,
391391
options: OptionsId,
392392
},
393+
StreamForward {
394+
instance: RuntimeComponentInstanceIndex,
395+
ty: TypeStreamTableIndex,
396+
},
393397
StreamCancelRead {
394398
instance: RuntimeComponentInstanceIndex,
395399
ty: TypeStreamTableIndex,
@@ -422,6 +426,10 @@ pub enum Trampoline {
422426
ty: TypeFutureTableIndex,
423427
options: OptionsId,
424428
},
429+
FutureForward {
430+
instance: RuntimeComponentInstanceIndex,
431+
ty: TypeFutureTableIndex,
432+
},
425433
FutureCancelRead {
426434
instance: RuntimeComponentInstanceIndex,
427435
ty: TypeFutureTableIndex,
@@ -1033,6 +1041,10 @@ impl LinearizeDfg<'_> {
10331041
ty: *ty,
10341042
options: self.options(*options),
10351043
},
1044+
Trampoline::StreamForward { instance, ty } => info::Trampoline::StreamForward {
1045+
instance: *instance,
1046+
ty: *ty,
1047+
},
10361048
Trampoline::StreamCancelRead {
10371049
instance,
10381050
ty,
@@ -1085,6 +1097,10 @@ impl LinearizeDfg<'_> {
10851097
ty: *ty,
10861098
options: self.options(*options),
10871099
},
1100+
Trampoline::FutureForward { instance, ty } => info::Trampoline::FutureForward {
1101+
instance: *instance,
1102+
ty: *ty,
1103+
},
10881104
Trampoline::FutureCancelRead {
10891105
instance,
10901106
ty,

crates/environ/src/component/info.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,16 @@ pub enum Trampoline {
887887
options: OptionsIndex,
888888
},
889889

890+
/// A `stream.forward` intrinsic to forward all remaining elements from the
891+
/// readable end of one `stream` into the writable end of another `stream`
892+
/// of the specified type.
893+
StreamForward {
894+
/// The specific component instance which is calling the intrinsic.
895+
instance: RuntimeComponentInstanceIndex,
896+
/// The table index for the specific `stream` type and caller instance.
897+
ty: TypeStreamTableIndex,
898+
},
899+
890900
/// A `stream.cancel-read` intrinsic to cancel an in-progress read from a
891901
/// `stream` of the specified type.
892902
StreamCancelRead {
@@ -960,6 +970,16 @@ pub enum Trampoline {
960970
options: OptionsIndex,
961971
},
962972

973+
/// A `future.forward` intrinsic to forward the value of the `future` with
974+
/// the specified readable end into the `future` with the specified
975+
/// writable end.
976+
FutureForward {
977+
/// The specific component instance which is calling the intrinsic.
978+
instance: RuntimeComponentInstanceIndex,
979+
/// The table index for the specific `future` type and caller instance.
980+
ty: TypeFutureTableIndex,
981+
},
982+
963983
/// A `future.cancel-read` intrinsic to cancel an in-progress read from a
964984
/// `future` of the specified type.
965985
FutureCancelRead {
@@ -1225,13 +1245,15 @@ impl Trampoline {
12251245
StreamNew { .. } => format!("stream-new"),
12261246
StreamRead { .. } => format!("stream-read"),
12271247
StreamWrite { .. } => format!("stream-write"),
1248+
StreamForward { .. } => format!("stream-forward"),
12281249
StreamCancelRead { .. } => format!("stream-cancel-read"),
12291250
StreamCancelWrite { .. } => format!("stream-cancel-write"),
12301251
StreamDropReadable { .. } => format!("stream-drop-readable"),
12311252
StreamDropWritable { .. } => format!("stream-drop-writable"),
12321253
FutureNew { .. } => format!("future-new"),
12331254
FutureRead { .. } => format!("future-read"),
12341255
FutureWrite { .. } => format!("future-write"),
1256+
FutureForward { .. } => format!("future-forward"),
12351257
FutureCancelRead { .. } => format!("future-cancel-read"),
12361258
FutureCancelWrite { .. } => format!("future-cancel-write"),
12371259
FutureDropReadable { .. } => format!("future-drop-readable"),

crates/environ/src/component/translate.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ enum LocalInitializer<'data> {
242242
ty: ComponentDefinedTypeId,
243243
options: LocalCanonicalOptions,
244244
},
245+
StreamForward {
246+
ty: ComponentDefinedTypeId,
247+
func: ModuleInternedTypeIndex,
248+
},
245249
StreamCancelRead {
246250
ty: ComponentDefinedTypeId,
247251
func: ModuleInternedTypeIndex,
@@ -272,6 +276,10 @@ enum LocalInitializer<'data> {
272276
ty: ComponentDefinedTypeId,
273277
options: LocalCanonicalOptions,
274278
},
279+
FutureForward {
280+
ty: ComponentDefinedTypeId,
281+
func: ModuleInternedTypeIndex,
282+
},
275283
FutureCancelRead {
276284
ty: ComponentDefinedTypeId,
277285
func: ModuleInternedTypeIndex,
@@ -1145,6 +1153,16 @@ impl<'a, 'data> Translator<'a, 'data> {
11451153
core_func_index += 1;
11461154
LocalInitializer::StreamWrite { ty, options }
11471155
}
1156+
wasmparser::CanonicalFunction::StreamForward { ty } => {
1157+
let ty = self
1158+
.validator
1159+
.types(0)
1160+
.unwrap()
1161+
.component_defined_type_at(ty);
1162+
let func = self.core_func_signature(core_func_index)?;
1163+
core_func_index += 1;
1164+
LocalInitializer::StreamForward { ty, func }
1165+
}
11481166
wasmparser::CanonicalFunction::StreamCancelRead { ty, async_ } => {
11491167
let ty = self
11501168
.validator
@@ -1215,6 +1233,16 @@ impl<'a, 'data> Translator<'a, 'data> {
12151233
core_func_index += 1;
12161234
LocalInitializer::FutureWrite { ty, options }
12171235
}
1236+
wasmparser::CanonicalFunction::FutureForward { ty } => {
1237+
let ty = self
1238+
.validator
1239+
.types(0)
1240+
.unwrap()
1241+
.component_defined_type_at(ty);
1242+
let func = self.core_func_signature(core_func_index)?;
1243+
core_func_index += 1;
1244+
LocalInitializer::FutureForward { ty, func }
1245+
}
12181246
wasmparser::CanonicalFunction::FutureCancelRead { ty, async_ } => {
12191247
let ty = self
12201248
.validator

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,21 @@ impl<'a> Inliner<'a> {
849849
));
850850
frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
851851
}
852+
StreamForward { ty, func } => {
853+
let InterfaceType::Stream(ty) =
854+
types.defined_type(frame.translation.types_ref(), *ty)?
855+
else {
856+
unreachable!()
857+
};
858+
let index = self.result.trampolines.push((
859+
*func,
860+
dfg::Trampoline::StreamForward {
861+
instance: frame.instance,
862+
ty,
863+
},
864+
));
865+
frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
866+
}
852867
StreamCancelRead { ty, func, async_ } => {
853868
let InterfaceType::Stream(ty) =
854869
types.defined_type(frame.translation.types_ref(), *ty)?
@@ -966,6 +981,21 @@ impl<'a> Inliner<'a> {
966981
));
967982
frame.funcs.push((func, dfg::CoreDef::Trampoline(index)));
968983
}
984+
FutureForward { ty, func } => {
985+
let InterfaceType::Future(ty) =
986+
types.defined_type(frame.translation.types_ref(), *ty)?
987+
else {
988+
unreachable!()
989+
};
990+
let index = self.result.trampolines.push((
991+
*func,
992+
dfg::Trampoline::FutureForward {
993+
instance: frame.instance,
994+
ty,
995+
},
996+
));
997+
frame.funcs.push((*func, dfg::CoreDef::Trampoline(index)));
998+
}
969999
FutureCancelRead { ty, func, async_ } => {
9701000
let InterfaceType::Future(ty) =
9711001
types.defined_type(frame.translation.types_ref(), *ty)?

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4205,6 +4205,15 @@ pub trait VMComponentAsyncStore {
42054205
address: u32,
42064206
) -> Result<u32>;
42074207

4208+
/// The `future.forward` intrinsic.
4209+
fn future_forward(
4210+
&mut self,
4211+
instance: Instance,
4212+
ty: TypeFutureTableIndex,
4213+
reader: u32,
4214+
writer: u32,
4215+
) -> Result<()>;
4216+
42084217
/// The `future.drop-writable` intrinsic.
42094218
fn future_drop_writable(
42104219
&mut self,
@@ -4267,6 +4276,15 @@ pub trait VMComponentAsyncStore {
42674276
count: u32,
42684277
) -> Result<u32>;
42694278

4279+
/// The `stream.forward` intrinsic.
4280+
fn stream_forward(
4281+
&mut self,
4282+
instance: Instance,
4283+
ty: TypeStreamTableIndex,
4284+
reader: u32,
4285+
writer: u32,
4286+
) -> Result<()>;
4287+
42704288
/// The `stream.drop-writable` intrinsic.
42714289
fn stream_drop_writable(
42724290
&mut self,
@@ -4494,6 +4512,21 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
44944512
.map(|result| result.encode())
44954513
}
44964514

4515+
fn future_forward(
4516+
&mut self,
4517+
instance: Instance,
4518+
ty: TypeFutureTableIndex,
4519+
reader: u32,
4520+
writer: u32,
4521+
) -> Result<()> {
4522+
instance.guest_forward(
4523+
StoreContextMut(self),
4524+
TransmitIndex::Future(ty),
4525+
reader,
4526+
writer,
4527+
)
4528+
}
4529+
44974530
fn future_drop_writable(
44984531
&mut self,
44994532
instance: Instance,
@@ -4561,6 +4594,21 @@ impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
45614594
.map(|result| result.encode())
45624595
}
45634596

4597+
fn stream_forward(
4598+
&mut self,
4599+
instance: Instance,
4600+
ty: TypeStreamTableIndex,
4601+
reader: u32,
4602+
writer: u32,
4603+
) -> Result<()> {
4604+
instance.guest_forward(
4605+
StoreContextMut(self),
4606+
TransmitIndex::Stream(ty),
4607+
reader,
4608+
writer,
4609+
)
4610+
}
4611+
45644612
fn stream_drop_writable(
45654613
&mut self,
45664614
instance: Instance,

0 commit comments

Comments
 (0)