Skip to content

Commit

Permalink
[Parser] Parse br_on_cast{_fail} input annotations (WebAssembly#6198)
Browse files Browse the repository at this point in the history
And validate in IRBuilder both that the input annotation is valid and that the
input matches it.
  • Loading branch information
tlively authored and radekdoulik committed Jul 12, 2024
1 parent 19f80df commit be6b82f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ struct NullInstrParserCtx {

Result<> makeBrOn(Index, LabelIdxT, BrOnOp) { return Ok{}; }

template<typename TypeT> Result<> makeBrOn(Index, LabelIdxT, BrOnOp, TypeT) {
template<typename TypeT>
Result<> makeBrOn(Index, LabelIdxT, BrOnOp, TypeT, TypeT) {
return Ok{};
}

Expand Down Expand Up @@ -1690,9 +1691,12 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return withLoc(pos, irBuilder.makeRefCast(type));
}

Result<>
makeBrOn(Index pos, Index label, BrOnOp op, Type castType = Type::none) {
return withLoc(pos, irBuilder.makeBrOn(label, op, castType));
Result<> makeBrOn(Index pos,
Index label,
BrOnOp op,
Type in = Type::none,
Type out = Type::none) {
return withLoc(pos, irBuilder.makeBrOn(label, op, in, out));
}

Result<> makeStructNew(Index pos, HeapType type) {
Expand Down
8 changes: 5 additions & 3 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1594,9 +1594,11 @@ template<typename Ctx> Result<> makeBrOnNull(Ctx& ctx, Index pos, bool onFail) {
template<typename Ctx> Result<> makeBrOnCast(Ctx& ctx, Index pos, bool onFail) {
auto label = labelidx(ctx);
CHECK_ERR(label);
auto type = reftype(ctx);
CHECK_ERR(type);
return ctx.makeBrOn(pos, *label, onFail ? BrOnCastFail : BrOnCast, *type);
auto in = reftype(ctx);
CHECK_ERR(in);
auto out = reftype(ctx);
CHECK_ERR(out);
return ctx.makeBrOn(pos, *label, onFail ? BrOnCastFail : BrOnCast, *in, *out);
}

template<typename Ctx>
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
[[nodiscard]] Result<> makeRefTest(Type type);
[[nodiscard]] Result<> makeRefCast(Type type);
[[nodiscard]] Result<>
makeBrOn(Index label, BrOnOp op, Type castType = Type::none);
makeBrOn(Index label, BrOnOp op, Type in = Type::none, Type out = Type::none);
[[nodiscard]] Result<> makeStructNew(HeapType type);
[[nodiscard]] Result<> makeStructNewDefault(HeapType type);
[[nodiscard]] Result<>
Expand Down
12 changes: 10 additions & 2 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1420,12 +1420,20 @@ Result<> IRBuilder::makeRefCast(Type type) {
return Ok{};
}

Result<> IRBuilder::makeBrOn(Index label, BrOnOp op, Type castType) {
Result<> IRBuilder::makeBrOn(Index label, BrOnOp op, Type in, Type out) {
BrOn curr;
CHECK_ERR(visitBrOn(&curr));
if (out != Type::none) {
if (!Type::isSubType(out, in)) {
return Err{"output type is not a subtype of the input type"};
}
if (!Type::isSubType(curr.ref->type, in)) {
return Err{"expected input to match input type annotation"};
}
}
auto name = getLabelName(label);
CHECK_ERR(name);
push(builder.makeBrOn(op, *name, curr.ref, castType));
push(builder.makeBrOn(op, *name, curr.ref, out));
return Ok{};
}

Expand Down
4 changes: 2 additions & 2 deletions test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -3549,7 +3549,7 @@
block (result i31ref)
block (result (ref any))
local.get 0
br_on_cast 1 i31ref
br_on_cast 1 anyref i31ref
end
unreachable
end
Expand All @@ -3574,7 +3574,7 @@
block (result (ref any))
block (result i31ref)
local.get 0
br_on_cast_fail 1 i31ref
br_on_cast_fail 1 anyref i31ref
end
unreachable
end
Expand Down

0 comments on commit be6b82f

Please sign in to comment.