diff --git a/scripts/gen-s-parser.py b/scripts/gen-s-parser.py index 56345480d57..9253030e126 100755 --- a/scripts/gen-s-parser.py +++ b/scripts/gen-s-parser.py @@ -206,6 +206,7 @@ ("i64.extend32_s", "makeUnary(UnaryOp::ExtendS32Int64)"), # atomic instructions ("memory.atomic.notify", "makeAtomicNotify()"), + ("struct.wait", "makeStructWait()"), ("memory.atomic.wait32", "makeAtomicWait(Type::i32)"), ("memory.atomic.wait64", "makeAtomicWait(Type::i64)"), ("atomic.fence", "makeAtomicFence()"), diff --git a/src/gen-s-parser.inc b/src/gen-s-parser.inc index 0472d5a3dea..f7f75dfb396 100644 --- a/src/gen-s-parser.inc +++ b/src/gen-s-parser.inc @@ -5400,6 +5400,12 @@ switch (buf[0]) { return Ok{}; } goto parse_error; + case 'w': + if (op == "struct.wait"sv) { + CHECK_ERR(makeStructWait(ctx, pos, annotations)); + return Ok{}; + } + goto parse_error; default: goto parse_error; } } diff --git a/src/interpreter/interpreter.cpp b/src/interpreter/interpreter.cpp index 6c97c82f729..6aec10abbb1 100644 --- a/src/interpreter/interpreter.cpp +++ b/src/interpreter/interpreter.cpp @@ -283,6 +283,7 @@ struct ExpressionInterpreter : OverriddenVisitor { Flow visitResume(Resume* curr) { WASM_UNREACHABLE("TODO"); } Flow visitResumeThrow(ResumeThrow* curr) { WASM_UNREACHABLE("TODO"); } Flow visitStackSwitch(StackSwitch* curr) { WASM_UNREACHABLE("TODO"); } + Flow visitStructWait(StructWait* curr) { WASM_UNREACHABLE("TODO"); } }; } // anonymous namespace diff --git a/src/ir/ReFinalize.cpp b/src/ir/ReFinalize.cpp index 06d6aab2c97..fb58d21e152 100644 --- a/src/ir/ReFinalize.cpp +++ b/src/ir/ReFinalize.cpp @@ -202,6 +202,7 @@ void ReFinalize::visitResumeThrow(ResumeThrow* curr) { } } void ReFinalize::visitStackSwitch(StackSwitch* curr) { curr->finalize(); } +void ReFinalize::visitStructWait(StructWait* curr) { curr->finalize(); } void ReFinalize::visitExport(Export* curr) { WASM_UNREACHABLE("unimp"); } void ReFinalize::visitGlobal(Global* curr) { WASM_UNREACHABLE("unimp"); } diff --git a/src/ir/child-typer.h b/src/ir/child-typer.h index a6f87543814..030045582fa 100644 --- a/src/ir/child-typer.h +++ b/src/ir/child-typer.h @@ -1357,6 +1357,16 @@ template struct ChildTyper : OverriddenVisitor { } note(&curr->cont, Type(*ct, Nullable)); } + + void visitStructWait(StructWait* curr, + std::optional ht = std::nullopt) { + if (!ht) { + ht = curr->structType; + } + note(&curr->ref, Type(*ht, Nullable)); + note(&curr->expected, Type(Type::BasicType::i32)); + note(&curr->timeout, Type(Type::BasicType::i64)); + } }; } // namespace wasm diff --git a/src/ir/cost.h b/src/ir/cost.h index 853040a3f19..74ee94d9fae 100644 --- a/src/ir/cost.h +++ b/src/ir/cost.h @@ -118,6 +118,10 @@ struct CostAnalyzer : public OverriddenVisitor { return AtomicCost + visit(curr->ptr) + visit(curr->expected) + visit(curr->timeout); } + CostType visitStructWait(StructWait* curr) { + return AtomicCost + nullCheckCost(curr->ref) + visit(curr->ref) + + visit(curr->expected) + visit(curr->timeout); + } CostType visitAtomicNotify(AtomicNotify* curr) { return AtomicCost + visit(curr->ptr) + visit(curr->notifyCount); } diff --git a/src/ir/effects.h b/src/ir/effects.h index 1a7f4af616b..6342cb839f3 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -1159,6 +1159,22 @@ class EffectAnalyzer { parent.throws_ = true; } } + + void visitStructWait(StructWait* curr) { + parent.isAtomic = true; + parent.mayNotReturn = true; + parent.implicitTrap = true; + + // field must exist and be a packed waitqueue if this is valid Wasm. + if (curr->type.isStruct() && + curr->index < curr->type.getHeapType().getStruct().fields.size() && + curr->type.getHeapType() + .getStruct() + .fields.at(curr->index) + .mutable_ == Mutable) { + parent.readsMutableStruct = true; + } + } }; public: diff --git a/src/ir/module-utils.cpp b/src/ir/module-utils.cpp index cc13101b717..2af77f309fe 100644 --- a/src/ir/module-utils.cpp +++ b/src/ir/module-utils.cpp @@ -446,6 +446,12 @@ struct CodeScanner : PostWalker { info.note(curr->cont->type); info.note(curr->type); } + void visitStructWait(StructWait* curr) { + info.note(curr->structType); + if (curr->ref && curr->ref->type != Type::unreachable) { + info.note(curr->ref->type); + } + } void visitBlock(Block* curr) { info.noteControlFlow(Signature(Type::none, curr->type)); } diff --git a/src/ir/possible-contents.cpp b/src/ir/possible-contents.cpp index 06825343d1f..606024080f3 100644 --- a/src/ir/possible-contents.cpp +++ b/src/ir/possible-contents.cpp @@ -1401,6 +1401,7 @@ struct InfoCollector // TODO: optimize when possible addRoot(curr); } + void visitStructWait(StructWait* curr) { addRoot(curr); } void visitFunction(Function* func) { // Functions with a result can flow a value out from their body. diff --git a/src/ir/subtype-exprs.h b/src/ir/subtype-exprs.h index 5e8bac40a1f..ffb318c4c03 100644 --- a/src/ir/subtype-exprs.h +++ b/src/ir/subtype-exprs.h @@ -598,6 +598,9 @@ struct SubtypingDiscoverer : public OverriddenVisitor { .type.getSignature(); self()->noteSubtype(currResult, retSig.results); } + void visitStructWait(StructWait* curr) { + // todo? + } }; } // namespace wasm diff --git a/src/parser/contexts.h b/src/parser/contexts.h index c16ac92268e..19ae170fa14 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -956,6 +956,12 @@ struct NullInstrParserCtx { makeStackSwitch(Index, const std::vector&, HeapTypeT, TagIdxT) { return Ok{}; } + + template + Result<> + makeStructWait(Index, const std::vector&, HeapTypeT, FieldIdxT) { + return Ok{}; + } }; struct NullCtx : NullTypeParserCtx, NullInstrParserCtx { @@ -2947,6 +2953,13 @@ struct ParseDefsCtx : TypeParserCtx, AnnotationParserCtx { Name tag) { return withLoc(pos, irBuilder.makeStackSwitch(type, tag)); } + + Result<> makeStructWait(Index pos, + const std::vector& annotations, + HeapType type, + Index field) { + return withLoc(pos, irBuilder.makeStructWait(type, field)); + } }; } // namespace wasm::WATParser diff --git a/src/parser/parsers.h b/src/parser/parsers.h index 2a3a5ec5a4e..e2c6a6e4c32 100644 --- a/src/parser/parsers.h +++ b/src/parser/parsers.h @@ -2840,6 +2840,17 @@ Result<> makeStackSwitch(Ctx& ctx, return ctx.makeStackSwitch(pos, annotations, *type, *tag); } +template +Result<> makeStructWait(Ctx& ctx, + Index pos, + const std::vector& annotations) { + auto type = typeidx(ctx); + CHECK_ERR(type); + auto field = fieldidx(ctx, *type); + CHECK_ERR(field); + return ctx.makeStructWait(pos, annotations, *type, *field); +} + // ======= // Modules // ======= diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 6f2f558e1ec..e38d11275b6 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -376,6 +376,11 @@ struct PrintSExpression : public UnifiedExpressionVisitor { visitExpression(curr); } } + void visitStructWait(StructWait* curr) { + if (!maybePrintUnreachableReplacement(curr, curr->type)) { + visitExpression(curr); + } + } // Module-level visitors void handleSignature(Function* curr, bool printImplicitNames = false); @@ -2658,6 +2663,14 @@ struct PrintExpressionContents o << ' '; curr->tag.print(o); } + + void visitStructWait(StructWait* curr) { + printMedium(o, "struct.wait"); + o << ' '; + printHeapTypeName(curr->structType); + o << ' '; + o << curr->index; + } }; void PrintSExpression::setModule(Module* module) { diff --git a/src/passes/TypeGeneralizing.cpp b/src/passes/TypeGeneralizing.cpp index c0607ef51be..ccfcb38afa5 100644 --- a/src/passes/TypeGeneralizing.cpp +++ b/src/passes/TypeGeneralizing.cpp @@ -899,6 +899,7 @@ struct TransferFn : OverriddenVisitor { void visitResume(Resume* curr) { WASM_UNREACHABLE("TODO"); } void visitResumeThrow(ResumeThrow* curr) { WASM_UNREACHABLE("TODO"); } void visitStackSwitch(StackSwitch* curr) { WASM_UNREACHABLE("TODO"); } + void visitStructWait(StructWait* curr) { WASM_UNREACHABLE("TODO"); } }; struct TypeGeneralizing : WalkerPass> { diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 52a6ad9dfeb..76a9ca6c4fd 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -702,6 +702,7 @@ enum ASTNodes { I64AtomicWait = 0x02, AtomicFence = 0x03, Pause = 0x04, + StructWait = 0x05, I32AtomicLoad = 0x10, I64AtomicLoad = 0x11, @@ -1252,9 +1253,10 @@ enum ASTNodes { Resume = 0xe3, ResumeThrow = 0xe4, ResumeThrowRef = 0xe5, - Switch = 0xe6, // NOTE(dhil): the internal class is known as - // StackSwitch to avoid conflict with the existing - // 'switch table'. + Switch = 0xe6, // NOTE(dhil): the internal class is known as + // StackSwitch to avoid conflict with the existing + // 'switch table'. + OnLabel = 0x00, // (on $tag $label) OnSwitch = 0x01 // (on $tag switch) }; diff --git a/src/wasm-builder.h b/src/wasm-builder.h index cd73babb6ca..be6b82c8076 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -1363,6 +1363,21 @@ class Builder { return ret; } + StructWait* makeStructWait(HeapType structType, + Index index, + Expression* ref, + Expression* expected, + Expression* timeout) { + auto* ret = wasm.allocator.alloc(); + ret->structType = structType; + ret->index = index; + ret->ref = ref; + ret->expected = expected; + ret->timeout = timeout; + ret->finalize(); + return ret; + } + // Additional helpers Drop* makeDrop(Expression* value) { diff --git a/src/wasm-delegations-fields.def b/src/wasm-delegations-fields.def index 895fa8b9276..9d5f55d9ff5 100644 --- a/src/wasm-delegations-fields.def +++ b/src/wasm-delegations-fields.def @@ -887,6 +887,14 @@ DELEGATE_FIELD_CHILD_VECTOR(StackSwitch, operands) DELEGATE_FIELD_NAME_KIND(StackSwitch, tag, ModuleItemKind::Tag) DELEGATE_FIELD_CASE_END(StackSwitch) +DELEGATE_FIELD_CASE_START(StructWait) +DELEGATE_FIELD_CHILD(StructWait, timeout) +DELEGATE_FIELD_CHILD(StructWait, expected) +DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(StructWait, ref) +DELEGATE_FIELD_INT(StructWait, index) +DELEGATE_FIELD_HEAPTYPE(StructWait, structType) +DELEGATE_FIELD_CASE_END(StructWait) + DELEGATE_FIELD_MAIN_END diff --git a/src/wasm-delegations.def b/src/wasm-delegations.def index dcec0e6e938..f4ff97de322 100644 --- a/src/wasm-delegations.def +++ b/src/wasm-delegations.def @@ -115,5 +115,6 @@ DELEGATE(Suspend); DELEGATE(Resume); DELEGATE(ResumeThrow); DELEGATE(StackSwitch); +DELEGATE(StructWait); #undef DELEGATE diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 5ae570437ed..13176a9d34c 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -2932,6 +2932,7 @@ class ConstantExpressionRunner : public ExpressionRunner { Flow visitResume(Resume* curr) { return Flow(NONCONSTANT_FLOW); } Flow visitResumeThrow(ResumeThrow* curr) { return Flow(NONCONSTANT_FLOW); } Flow visitStackSwitch(StackSwitch* curr) { return Flow(NONCONSTANT_FLOW); } + Flow visitStructWait(StructWait* curr) { return Flow(NONCONSTANT_FLOW); } void trap(std::string_view why) override { throw NonconstantException(); } @@ -4918,6 +4919,10 @@ class ModuleRunnerBase : public ExpressionRunner { Flow visitResume(Resume* curr) { return doResume(curr); } Flow visitResumeThrow(ResumeThrow* curr) { return doResume(curr); } Flow visitStackSwitch(StackSwitch* curr) { return Flow(NONCONSTANT_FLOW); } + Flow visitStructWait(StructWait* curr) { + WASM_UNREACHABLE("struct.wait not implemented"); + return Flow(); + } void trap(std::string_view why) override { // Traps break all current continuations - they will never be resumable. diff --git a/src/wasm-ir-builder.h b/src/wasm-ir-builder.h index 82b7fc68450..dcaaf4fe59e 100644 --- a/src/wasm-ir-builder.h +++ b/src/wasm-ir-builder.h @@ -285,6 +285,7 @@ class IRBuilder : public UnifiedExpressionVisitor> { return makeResumeThrow(ct, Name(), tags, labels); } Result<> makeStackSwitch(HeapType ct, Name tag); + Result<> makeStructWait(HeapType type, Index index); // Private functions that must be public for technical reasons. Result<> visitExpression(Expression*); diff --git a/src/wasm.h b/src/wasm.h index c35b1ea2531..59d7e851314 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -760,6 +760,7 @@ class Expression { ResumeThrowId, // Id for the stack switching `switch` StackSwitchId, + StructWaitId, NumExpressionIds }; Id _id; @@ -2155,6 +2156,20 @@ class StackSwitch : public SpecificExpression { void finalize(); }; +class StructWait : public SpecificExpression { +public: + StructWait() = default; + StructWait(MixedArena& allocator) : StructWait() {} + + Expression* ref = nullptr; + Expression* expected = nullptr; + Expression* timeout = nullptr; + HeapType structType; + Index index; + + void finalize(); +}; + // Globals struct Named { diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 6783b1eafbb..0564c961bb4 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -3913,6 +3913,11 @@ Result<> WasmBinaryReader::readInst() { auto type = getIndexedHeapType(); return builder.makeArrayCmpxchg(type, order); } + case BinaryConsts::StructWait: { + auto structType = getIndexedHeapType(); + auto index = getU32LEB(); + return builder.makeStructWait(structType, index); + } } return Err{"unknown atomic operation " + std::to_string(op)}; } diff --git a/src/wasm/wasm-ir-builder.cpp b/src/wasm/wasm-ir-builder.cpp index 23ff8764971..9139f4c3725 100644 --- a/src/wasm/wasm-ir-builder.cpp +++ b/src/wasm/wasm-ir-builder.cpp @@ -662,6 +662,12 @@ struct IRBuilder::ChildPopper ConstraintCollector{builder, children}.visitStackSwitch(curr, ct); return popConstrainedChildren(children); } + + Result<> visitStructWait(StructWait* curr) { + std::vector children; + ConstraintCollector{builder, children}.visitStructWait(curr); + return popConstrainedChildren(children); + } }; Result<> IRBuilder::visit(Expression* curr) { @@ -2655,6 +2661,16 @@ Result<> IRBuilder::makeStackSwitch(HeapType ct, Name tag) { return Ok{}; } +Result<> IRBuilder::makeStructWait(HeapType type, Index index) { + StructWait curr(wasm.allocator); + curr.structType = type; + curr.index = index; + CHECK_ERR(ChildPopper{*this}.visitStructWait(&curr)); + push(builder.makeStructWait( + curr.structType, curr.index, curr.ref, curr.expected, curr.timeout)); + return Ok{}; +} + void IRBuilder::applyAnnotations(Expression* expr, const CodeAnnotation& annotation) { if (annotation.branchLikely) { diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 0199e217c6a..2550934b819 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -2911,6 +2911,13 @@ void BinaryInstWriter::visitStackSwitch(StackSwitch* curr) { o << U32LEB(parent.getTagIndex(curr->tag)); } +void BinaryInstWriter::visitStructWait(StructWait* curr) { + o << static_cast(BinaryConsts::AtomicPrefix) + << U32LEB(BinaryConsts::StructWait); + parent.writeIndexedHeapType(curr->structType); + o << U32LEB(curr->index); +} + void BinaryInstWriter::emitScopeEnd(Expression* curr) { assert(!breakStack.empty()); breakStack.pop_back(); diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 48156d008ba..594bde7c02f 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -569,6 +569,7 @@ struct FunctionValidator : public WalkerPass> { void visitResume(Resume* curr); void visitResumeThrow(ResumeThrow* curr); void visitStackSwitch(StackSwitch* curr); + void visitStructWait(StructWait* curr); void visitFunction(Function* curr); @@ -4260,6 +4261,44 @@ void FunctionValidator::visitStackSwitch(StackSwitch* curr) { "switch must be annotated with a continuation type"); } +void FunctionValidator::visitStructWait(StructWait* curr) { + shouldBeTrue( + !getModule() || getModule()->features.hasSharedEverything(), + curr, + "struct.wait requires shared-everything [--enable-shared-everything]"); + if (curr->ref->type == Type::unreachable) { + return; + } + shouldBeTrue( + curr->ref->type.isRef(), curr, "struct.wait ref must be a reference"); + shouldBeSubType( + curr->ref->type, + Type(curr->structType, Nullable), + curr, + "struct.wait ref must be a subtype of the specified struct type"); + shouldBeTrue(curr->structType.isStruct(), + curr, + "struct.wait must be parameterized by a struct type"); + if (curr->structType.isStruct() && + curr->index < curr->structType.getStruct().fields.size()) { + shouldBeTrue(curr->structType.getStruct().fields[curr->index].packedType == + Field::WaitQueue, + curr, + "struct.wait struct field must be a waitqueue"); + } else if (curr->structType.isStruct()) { + shouldBeTrue(false, curr, "struct.wait struct field index out of bounds"); + } + + shouldBeEqual(curr->expected->type, + Type(Type::BasicType::i32), + curr, + "struct.wait expected must be an i32"); + shouldBeEqual(curr->timeout->type, + Type(Type::BasicType::i64), + curr, + "struct.wait timeout must be an i64"); +} + void FunctionValidator::visitFunction(Function* curr) { FeatureSet features; // Check for things like having a rec group with GC enabled. The type we're diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 959b6cd4bfe..ac55d71710c 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -2017,4 +2017,6 @@ void Module::clearDebugInfo() { debugInfoSymbolNames.clear(); } +void StructWait::finalize() { type = Type::i32; } + } // namespace wasm diff --git a/src/wasm2js.h b/src/wasm2js.h index b92a7f851e5..82ad801d1d8 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -2464,6 +2464,10 @@ Ref Wasm2JSBuilder::processExpression(Expression* curr, unimplemented(curr); WASM_UNREACHABLE("unimp"); } + Ref visitStructWait(StructWait* curr) { + unimplemented(curr); + WASM_UNREACHABLE("unimp"); + } private: Ref makePointer(Expression* ptr, Address offset) { diff --git a/test/lit/waitqueue.wast b/test/lit/waitqueue.wast index 1a3e9726674..0171f4840c3 100644 --- a/test/lit/waitqueue.wast +++ b/test/lit/waitqueue.wast @@ -4,6 +4,19 @@ ;; RTRIP: (type $t (struct (field waitqueue))) (type $t (struct (field waitqueue))) - ;; use $t so roundtripping doesn't drop the definition - (global (ref null $t) (ref.null $t)) + ;; RTRIP: (global $g (ref $t) (struct.new $t + ;; RTRIP-NEXT: (i32.const 0) + ;; RTRIP-NEXT: )) + (global $g (ref $t) (struct.new $t (i32.const 0))) + + ;; RTRIP: (func $f (type $1) (result i32) + ;; RTRIP-NEXT: (struct.wait $t 0 + ;; RTRIP-NEXT: (global.get $g) + ;; RTRIP-NEXT: (i32.const 0) + ;; RTRIP-NEXT: (i64.const 0) + ;; RTRIP-NEXT: ) + ;; RTRIP-NEXT: ) + (func $f (result i32) + (struct.wait $t 0 (global.get $g) (i32.const 0) (i64.const 0)) + ) ) diff --git a/test/spec/waitqueue.wast b/test/spec/waitqueue.wast new file mode 100644 index 00000000000..05c2478fab3 --- /dev/null +++ b/test/spec/waitqueue.wast @@ -0,0 +1,38 @@ +(assert_invalid + (module + (type $t (struct (field i32))) + (func (param $expected i32) (param $timeout i64) (result i32) + (struct.wait $t 0 (ref.null $t) (local.get $expected) (local.get $timeout)) + ) + ) "struct.wait struct field must be a waitqueue" +) + +(assert_invalid + (module + (type $t (struct (field waitqueue))) + (global $g (ref $t) (struct.new $t (i32.const 0))) + (func (param $expected i32) (param $timeout i64) (result i32) + (struct.wait $t 0 (global.get $g) (i64.const 0) (local.get $timeout)) + ) + ) "struct.wait expected must be an i32" +) + +(assert_invalid + (module + (type $t (struct (field waitqueue))) + (global $g (ref $t) (struct.new $t (i32.const 0))) + (func (param $expected i32) (param $timeout i64) (result i32) + (struct.wait $t 0 (global.get $g) (local.get $expected) (i32.const 0)) + ) + ) "struct.wait timeout must be an i64" +) + +(module + (type $t (struct (field waitqueue))) + + (global $g (ref $t) (struct.new $t (i32.const 0))) + + (func (export "waitqueue.wait") (param $expected i32) (param $timeout i64) (result i32) + (struct.wait $t 0 (global.get $g) (local.get $expected) (local.get $timeout)) + ) +)