From 84cc9fa123e58c5ff236145a24157c098daede64 Mon Sep 17 00:00:00 2001 From: Frank Emrich Date: Tue, 19 Mar 2024 17:53:08 +0000 Subject: Typed continuations: suspend instructions (#6393) This PR is part of a series that adds basic support for the [typed continuations/wasmfx proposal](https://github.com/wasmfx/specfx). This particular PR adds support for the `suspend` instruction for suspending with a given tag, documented [here](https://github.com/wasmfx/specfx/blob/main/proposals/continuations/Overview.md#instructions). These instructions are of the form `(suspend $tag)`. Assuming that `$tag` is defined with _n_ `param` types `t_1` to `t_n`, the instruction consumes _n_ arguments of types `t_1` to `t_n`. Its result type is the same as the `result` type of the tag. Thus, the folded textual representation looks like `(suspend $tag arg1 ... argn)`. Support for the instruction is implemented in both the old and the new wat parser. Note that this PR does not implement validation of the new instruction. This PR also fixes finalization of `cont.new`, `cont.bind` and `resume` nodes in those cases where any of their children are unreachable. --- src/wasm/wasm-ir-builder.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/wasm/wasm-ir-builder.cpp') diff --git a/src/wasm/wasm-ir-builder.cpp b/src/wasm/wasm-ir-builder.cpp index 4bc80d25c..99462e9a8 100644 --- a/src/wasm/wasm-ir-builder.cpp +++ b/src/wasm/wasm-ir-builder.cpp @@ -658,6 +658,19 @@ Result<> IRBuilder::visitResume(Resume* curr) { return Ok{}; } +Result<> IRBuilder::visitSuspend(Suspend* curr) { + auto tag = wasm.getTag(curr->tag); + auto sig = tag->sig; + auto size = sig.params.size(); + curr->operands.resize(size); + for (size_t i = 0; i < size; ++i) { + auto val = pop(); + CHECK_ERR(val); + curr->operands[size - i - 1] = *val; + } + return Ok{}; +} + Result<> IRBuilder::visitTupleMake(TupleMake* curr) { assert(curr->operands.size() >= 2); for (size_t i = 0, size = curr->operands.size(); i < size; ++i) { @@ -1930,4 +1943,14 @@ Result<> IRBuilder::makeResume(HeapType ct, return Ok{}; } +Result<> IRBuilder::makeSuspend(Name tag) { + Suspend curr(wasm.allocator); + curr.tag = tag; + CHECK_ERR(visitSuspend(&curr)); + + std::vector operands(curr.operands.begin(), curr.operands.end()); + push(builder.makeSuspend(tag, operands)); + return Ok{}; +} + } // namespace wasm -- cgit v1.2.3