Skip to content

Emit call_ref with a type annotation #5079

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

Merged
merged 2 commits into from
Sep 23, 2022
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Current Trunk
-------------

- Add extra `memory64` argument for `BinaryenSetMemory` and new `BinaryenMemoryIs64` C-API method to determine 64-bit memory. (#4963)
- `TypeBuilderSetSubType` now takes a super type as the second argument.
- `TypeBuilderSetSubType` now takes a supertype as the second argument.
- `call_ref` can now take a signature type immediate in the text format. The
type immediate will become mandatory in the future.

v110
----
Expand Down
24 changes: 7 additions & 17 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2060,23 +2060,17 @@ struct PrintExpressionContents
}

void visitCallRef(CallRef* curr) {
if (curr->isReturn) {
if (printUnreachableReplacement(curr->target)) {
return;
}
printMedium(o, "return_call_ref ");
assert(curr->target->type != Type::unreachable);
// TODO: Workaround if target has bottom type.
printHeapType(o, curr->target->type.getHeapType(), wasm);
} else {
printMedium(o, "call_ref");
// TODO: Workaround if target has bottom type.
if (printUnreachableReplacement(curr->target)) {
return;
}
printMedium(o, curr->isReturn ? "return_call_ref " : "call_ref ");
printHeapType(o, curr->target->type.getHeapType(), wasm);
}
void visitRefTest(RefTest* curr) {
printMedium(o, "ref.test_static ");
printHeapType(o, curr->intendedType, wasm);
}

void visitRefCast(RefCast* curr) {
if (curr->safety == RefCast::Unsafe) {
printMedium(o, "ref.cast_nop_static ");
Expand All @@ -2085,6 +2079,7 @@ struct PrintExpressionContents
}
printHeapType(o, curr->intendedType, wasm);
}

void visitBrOn(BrOn* curr) {
switch (curr->op) {
case BrOnNull:
Expand Down Expand Up @@ -2139,7 +2134,6 @@ struct PrintExpressionContents
o << ' ';
TypeNamePrinter(o, wasm).print(curr->type.getHeapType());
}

void printFieldName(HeapType type, Index index) {
processFieldName(wasm, type, index, [&](Name name) {
if (name.is()) {
Expand Down Expand Up @@ -2755,11 +2749,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
decIndent();
}
void visitCallRef(CallRef* curr) {
if (curr->isReturn) {
maybePrintUnreachableReplacement(curr, curr->target->type);
} else {
visitExpression(curr);
}
maybePrintUnreachableReplacement(curr, curr->target->type);
}
void visitStructNew(StructNew* curr) {
maybePrintUnreachableReplacement(curr, curr->type);
Expand Down
3 changes: 2 additions & 1 deletion src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,8 @@ enum ASTNodes {

// typed function references opcodes

CallRef = 0x14,
CallRefUnannotated = 0x14,
CallRef = 0x17,
RetCallRef = 0x15,

// gc opcodes
Expand Down
11 changes: 4 additions & 7 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3776,12 +3776,13 @@ BinaryConsts::ASTNodes WasmBinaryBuilder::readExpression(Expression*& curr) {
visitMemoryGrow(grow);
break;
}
case BinaryConsts::CallRef:
case BinaryConsts::CallRefUnannotated:
visitCallRef((curr = allocator.alloc<CallRef>())->cast<CallRef>());
break;
case BinaryConsts::CallRef:
case BinaryConsts::RetCallRef: {
auto call = allocator.alloc<CallRef>();
call->isReturn = true;
call->isReturn = code == BinaryConsts::RetCallRef;
curr = call;
visitCallRef(call, getTypeByIndex(getU32LEB()));
break;
Expand Down Expand Up @@ -6810,11 +6811,7 @@ void WasmBinaryBuilder::visitCallRef(CallRef* curr,
for (size_t i = 0; i < num; i++) {
curr->operands[num - i - 1] = popNonVoidExpression();
}
if (maybeType) {
curr->finalize();
} else {
curr->finalize(sig.results);
}
curr->finalize(sig.results);
}

bool WasmBinaryBuilder::maybeVisitI31New(Expression*& out, uint32_t code) {
Expand Down
21 changes: 15 additions & 6 deletions src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2831,22 +2831,31 @@ Expression* SExpressionWasmBuilder::makeTupleExtract(Element& s) {

Expression* SExpressionWasmBuilder::makeCallRef(Element& s, bool isReturn) {
Index operandsStart = 1;
HeapType sigType;
if (isReturn) {
std::optional<HeapType> sigType;
try {
sigType = parseHeapType(*s[1]);
operandsStart = 2;
} catch (ParseException& p) {
// The type annotation is required for return_call_ref but temporarily
// optional for call_ref.
if (isReturn) {
throw;
}
}
std::vector<Expression*> operands;
parseOperands(s, operandsStart, s.size() - 1, operands);
auto* target = parseExpression(s[s.size() - 1]);

if (isReturn) {
if (!sigType.isSignature()) {
if (sigType) {
if (!sigType->isSignature()) {
throw ParseException(
"return_call_ref type annotation should be a signature", s.line, s.col);
std::string(isReturn ? "return_call_ref" : "call_ref") +
" type annotation should be a signature",
s.line,
s.col);
}
return Builder(wasm).makeCallRef(
target, operands, sigType.getSignature().results, isReturn);
target, operands, sigType->getSignature().results, isReturn);
}
return ValidatingBuilder(wasm, s.line, s.col)
.validateAndMakeCallRef(target, operands, isReturn);
Expand Down
13 changes: 5 additions & 8 deletions src/wasm/wasm-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2013,14 +2013,11 @@ void BinaryInstWriter::visitI31Get(I31Get* curr) {
}

void BinaryInstWriter::visitCallRef(CallRef* curr) {
if (curr->isReturn) {
assert(curr->target->type != Type::unreachable);
// TODO: `emitUnreachable` if target has bottom type.
o << int8_t(BinaryConsts::RetCallRef);
parent.writeIndexedHeapType(curr->target->type.getHeapType());
return;
}
o << int8_t(BinaryConsts::CallRef);
assert(curr->target->type != Type::unreachable);
// TODO: `emitUnreachable` if target has bottom type.
o << int8_t(curr->isReturn ? BinaryConsts::RetCallRef
: BinaryConsts::CallRef);
parent.writeIndexedHeapType(curr->target->type.getHeapType());
}

void BinaryInstWriter::visitRefTest(RefTest* curr) {
Expand Down
2 changes: 1 addition & 1 deletion test/lit/passes/dae_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@
(unreachable)
)
;; CHECK: (func $1
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i64
;; CHECK-NEXT: (i64.const 0)
;; CHECK-NEXT: (global.get $global$0)
;; CHECK-NEXT: )
Expand Down
10 changes: 5 additions & 5 deletions test/lit/passes/gufa-refs.wast
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $two-params
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (struct.new_default $struct)
;; CHECK-NEXT: (ref.func $func-2params-a)
Expand Down Expand Up @@ -3744,19 +3744,19 @@
)

;; CHECK: (func $do-calls (type $none_=>_none)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i1
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: (ref.func $reffed1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i1
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: (ref.func $reffed1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i2
;; CHECK-NEXT: (i32.const 1337)
;; CHECK-NEXT: (ref.func $reffed2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i2
;; CHECK-NEXT: (i32.const 99999)
;; CHECK-NEXT: (ref.func $reffed2)
;; CHECK-NEXT: )
Expand Down
6 changes: 4 additions & 2 deletions test/lit/passes/inlining-optimizing.wast
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
)
;; CHECK: (func $1
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (block ;; (replaces something unreachable we can't emit)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/inlining_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
;; CHECK: (func $1
;; CHECK-NEXT: (block $__inlined_func$0
;; CHECK-NEXT: (block
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $none_=>_none
;; CHECK-NEXT: (ref.null $none_=>_none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $__inlined_func$0)
Expand All @@ -153,7 +153,7 @@
;; NOMNL: (func $1 (type $none_=>_none)
;; NOMNL-NEXT: (block $__inlined_func$0
;; NOMNL-NEXT: (block
;; NOMNL-NEXT: (call_ref
;; NOMNL-NEXT: (call_ref $none_=>_none
;; NOMNL-NEXT: (ref.null $none_=>_none)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (br $__inlined_func$0)
Expand Down
2 changes: 1 addition & 1 deletion test/lit/passes/intrinsic-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $none
;; CHECK-NEXT: (ref.null $none)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/local-cse_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

;; CHECK: (func $calls (param $x i32) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i32_=>_i32
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: (ref.func $calls)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i32_=>_i32
;; CHECK-NEXT: (i32.const 10)
;; CHECK-NEXT: (ref.func $calls)
;; CHECK-NEXT: )
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/merge-similar-functions.wast
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $none_=>_i32
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand All @@ -352,7 +352,7 @@
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i32_=>_i32
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
Expand Down
6 changes: 3 additions & 3 deletions test/lit/passes/merge-similar-functions_types.wast
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $type$0
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
Expand Down Expand Up @@ -287,7 +287,7 @@
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $type$0
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
Expand All @@ -309,7 +309,7 @@
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: (call_ref
;; NOMNL-NEXT: (call_ref $type$1
;; NOMNL-NEXT: (local.get $0)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (nop)
Expand Down
6 changes: 3 additions & 3 deletions test/lit/passes/optimize-instructions-call_ref.wast
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
)

;; CHECK: (func $fallthrough-bad-type (result i32)
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $none_=>_i32
;; CHECK-NEXT: (block (result (ref $none_=>_i32))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.func $return-nothing)
Expand Down Expand Up @@ -188,7 +188,7 @@
(func $return-nothing)

;; CHECK: (func $fallthrough-unreachable
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i32_i32_=>_none
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (block (result (ref $i32_i32_=>_none))
Expand Down Expand Up @@ -258,7 +258,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $i32_i32_=>_none
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (select (result (ref $i32_i32_=>_none))
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/precompute-gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call_ref
;; CHECK-NEXT: (call_ref $func-return-i32
;; CHECK-NEXT: (local.get $temp)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand All @@ -1169,7 +1169,7 @@
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (drop
;; NOMNL-NEXT: (call_ref
;; NOMNL-NEXT: (call_ref $func-return-i32
;; NOMNL-NEXT: (local.get $temp)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
Expand Down
Loading