Skip to content

Named handlers support #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@
("resume", "makeResume()"),
("resume_throw", "makeResumeThrow()"),
("switch", "makeStackSwitch()"),
("suspend_to", "makeSuspendTo()"),
("resume_with", "makeResumeWith()"),
# GC
("ref.i31", "makeRefI31(Unshared)"),
("ref.i31_shared", "makeRefI31(Shared)"),
Expand Down
3 changes: 3 additions & 0 deletions scripts/test/fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
'stack_switching_resume.wast',
'stack_switching_resume_throw.wast',
'stack_switching_switch.wast',
'stack_switching_named.wast',
'stack_switching_suspend_to.wast',
'stack_switching_resume_with.wast',
# TODO: fuzzer support for exact references
'exact-references.wast',
]
Expand Down
4 changes: 4 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
case HeapType::struct_:
case HeapType::array:
case HeapType::exn:
case HeapType::handler:
WASM_UNREACHABLE("invalid type");
case HeapType::string:
WASM_UNREACHABLE("TODO: string literals");
Expand All @@ -98,6 +99,7 @@ BinaryenLiteral toBinaryenLiteral(Literal x) {
case HeapType::nofunc:
case HeapType::nocont:
case HeapType::noexn:
case HeapType::nohandler:
// Null.
return ret;
}
Expand Down Expand Up @@ -144,6 +146,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
case HeapType::struct_:
case HeapType::array:
case HeapType::exn:
case HeapType::handler:
WASM_UNREACHABLE("invalid type");
case HeapType::string:
WASM_UNREACHABLE("TODO: string literals");
Expand All @@ -152,6 +155,7 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) {
case HeapType::nofunc:
case HeapType::nocont:
case HeapType::noexn:
case HeapType::nohandler:
assert(type.isNullable());
return Literal::makeNull(heapType);
}
Expand Down
42 changes: 32 additions & 10 deletions src/gen-s-parser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4815,12 +4815,23 @@ switch (buf[0]) {
return Ok{};
}
goto parse_error;
case '_':
if (op == "resume_throw"sv) {
CHECK_ERR(makeResumeThrow(ctx, pos, annotations));
return Ok{};
case '_': {
switch (buf[7]) {
case 't':
if (op == "resume_throw"sv) {
CHECK_ERR(makeResumeThrow(ctx, pos, annotations));
return Ok{};
}
goto parse_error;
case 'w':
if (op == "resume_with"sv) {
CHECK_ERR(makeResumeWith(ctx, pos, annotations));
return Ok{};
}
goto parse_error;
default: goto parse_error;
}
goto parse_error;
}
default: goto parse_error;
}
}
Expand Down Expand Up @@ -5177,12 +5188,23 @@ switch (buf[0]) {
default: goto parse_error;
}
}
case 'u':
if (op == "suspend"sv) {
CHECK_ERR(makeSuspend(ctx, pos, annotations));
return Ok{};
case 'u': {
switch (buf[7]) {
case '\0':
if (op == "suspend"sv) {
CHECK_ERR(makeSuspend(ctx, pos, annotations));
return Ok{};
}
goto parse_error;
case '_':
if (op == "suspend_to"sv) {
CHECK_ERR(makeSuspendTo(ctx, pos, annotations));
return Ok{};
}
goto parse_error;
default: goto parse_error;
}
goto parse_error;
}
case 'w':
if (op == "switch"sv) {
CHECK_ERR(makeStackSwitch(ctx, pos, annotations));
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
Flow visitResume(Resume* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitResumeThrow(ResumeThrow* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitStackSwitch(StackSwitch* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitResumeWith(ResumeWith* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitSuspendTo(SuspendTo* curr) { WASM_UNREACHABLE("TODO"); }
};

} // anonymous namespace
Expand Down
4 changes: 4 additions & 0 deletions src/ir/ReFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ void ReFinalize::visitSuspend(Suspend* curr) { curr->finalize(getModule()); }
void ReFinalize::visitResume(Resume* curr) { curr->finalize(); }
void ReFinalize::visitResumeThrow(ResumeThrow* curr) { curr->finalize(); }
void ReFinalize::visitStackSwitch(StackSwitch* curr) { curr->finalize(); }
void ReFinalize::visitResumeWith(ResumeWith* curr) { curr->finalize(); }
void ReFinalize::visitSuspendTo(SuspendTo* curr) {
curr->finalize(getModule());
}

void ReFinalize::visitExport(Export* curr) { WASM_UNREACHABLE("unimp"); }
void ReFinalize::visitGlobal(Global* curr) { WASM_UNREACHABLE("unimp"); }
Expand Down
7 changes: 7 additions & 0 deletions src/ir/branch-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void operateOnScopeNameUsesAndSentTypes(Expression* expr, T func) {
func(name, r->sentTypes[i]);
}
}
} else if (auto* r = expr->dynCast<ResumeWith>()) {
for (Index i = 0; i < r->handlerTags.size(); i++) {
auto dest = r->handlerTags[i];
if (!dest.isNull() && dest == name) {
func(name, r->sentTypes[i]);
}
}
} else {
assert(expr->is<Try>() || expr->is<Rethrow>()); // delegate or rethrow
}
Expand Down
29 changes: 29 additions & 0 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,35 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}
note(&curr->cont, Type(*ct, Nullable));
}

void visitSuspendTo(SuspendTo* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht.has_value()) {
ht = curr->handler->type.getHeapType();
}
assert(ht->isHandler());
auto params = wasm.getTag(curr->tag)->params();
assert(params.size() == curr->operands.size());
for (size_t i = 0; i < params.size(); ++i) {
note(&curr->operands[i], params[i]);
}
note(&curr->handler, Type(*ht, Nullable));
}

void visitResumeWith(ResumeWith* curr,
std::optional<HeapType> ct = std::nullopt) {
if (!ct.has_value()) {
ct = curr->cont->type.getHeapType();
}
assert(ct->isContinuation());
auto params = ct->getContinuation().type.getSignature().params;
assert(params.size() >= 1 &&
((params.size() - 1) == curr->operands.size()));
for (size_t i = 0; i < params.size() - 1; ++i) {
note(&curr->operands[i], params[i]);
}
note(&curr->cont, Type(*ct, Nullable));
}
};

} // namespace wasm
Expand Down
15 changes: 15 additions & 0 deletions src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,21 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
}
return ret;
}
CostType visitResumeWith(ResumeWith* curr) {
// Inspired by indirect calls, but twice the cost.
CostType ret = 12 + visit(curr->cont);
for (auto* arg : curr->operands) {
ret += visit(arg);
}
return ret;
}
CostType visitSuspendTo(SuspendTo* curr) {
CostType ret = 12 + visit(curr->handler);
for (auto* arg : curr->operands) {
ret += visit(arg);
}
return ret;
}

private:
CostType nullCheckCost(Expression* ref) {
Expand Down
23 changes: 23 additions & 0 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,29 @@ class EffectAnalyzer {
parent.throws_ = true;
}
}
void visitResumeWith(ResumeWith* curr) {
// This acts as a kitchen sink effect.
parent.calls = true;

// resume instructions accept nullable continuation references and trap
// on null.
parent.implicitTrap = true;

if (parent.features.hasExceptionHandling() && parent.tryDepth == 0) {
parent.throws_ = true;
}
}
void visitSuspendTo(SuspendTo* curr) {
// Similar to resume/call: Suspending means that we execute arbitrary
// other code before we may resume here.
parent.calls = true;
if (parent.features.hasExceptionHandling() && parent.tryDepth == 0) {
parent.throws_ = true;
}

// A suspend may go unhandled and therefore trap.
parent.implicitTrap = true;
}
};

public:
Expand Down
1 change: 1 addition & 0 deletions src/ir/gc-type-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ inline std::optional<Field> getField(HeapType type, Index index = 0) {
return type.getArray().element;
case HeapTypeKind::Func:
case HeapTypeKind::Cont:
case HeapTypeKind::Handler:
case HeapTypeKind::Basic:
break;
}
Expand Down
8 changes: 8 additions & 0 deletions src/ir/possible-contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,14 @@ struct InfoCollector
// TODO: optimize when possible
addRoot(curr);
}
void visitResumeWith(ResumeWith* curr) {
// TODO: optimize when possible
addRoot(curr);
}
void visitSuspendTo(SuspendTo* curr) {
// TODO: optimize when possible
addRoot(curr);
}

void visitFunction(Function* func) {
// Functions with a result can flow a value out from their body.
Expand Down
4 changes: 4 additions & 0 deletions src/ir/subtype-exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
void visitStackSwitch(StackSwitch* curr) {
WASM_UNREACHABLE("not implemented");
}
void visitResumeWith(ResumeWith* curr) {
WASM_UNREACHABLE("not implemented");
}
void visitSuspendTo(SuspendTo* curr) { WASM_UNREACHABLE("not implemented"); }
};

} // namespace wasm
Expand Down
2 changes: 2 additions & 0 deletions src/ir/subtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct SubTypes {
break;
case HeapTypeKind::Cont:
WASM_UNREACHABLE("TODO: cont");
case HeapTypeKind::Handler:
WASM_UNREACHABLE("TODO: handler");
case HeapTypeKind::Basic:
WASM_UNREACHABLE("unexpected kind");
}
Expand Down
2 changes: 2 additions & 0 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes(
}
case HeapTypeKind::Cont:
WASM_UNREACHABLE("TODO: cont");
case HeapTypeKind::Handler:
WASM_UNREACHABLE("TODO: handler");
case HeapTypeKind::Basic:
WASM_UNREACHABLE("unexpected kind");
}
Expand Down
Loading
Loading