Skip to content

Commit

Permalink
Parse non-nullable tuple elements without special handling (WebAssemb…
Browse files Browse the repository at this point in the history
…ly#5910)

In the binary parser, when creating a scratch local to hold multivalue results
as tuples, we previously ensured that the scratch local did not contain any
non-nullable by modifying its type and inserting ref.as_non_null as necessary.
Now that we properly support non-nullable elements in tuple locals, however,
this parser behavior is no longer necessary. Remove it.
  • Loading branch information
tlively authored Aug 30, 2023
1 parent e8adbdd commit 5bfb192
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
28 changes: 5 additions & 23 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3006,32 +3006,14 @@ void WasmBinaryReader::skipUnreachableCode() {
void WasmBinaryReader::pushExpression(Expression* curr) {
auto type = curr->type;
if (type.isTuple()) {
// Store tuple to local and push individual extracted values
// Store tuple to local and push individual extracted values.
Builder builder(wasm);
// Non-nullable types require special handling as they cannot be stored to
// a local, so we may need to use a different local type than the original.
auto localType = type;
if (!wasm.features.hasGCNNLocals()) {
std::vector<Type> finalTypes;
for (auto t : type) {
if (t.isNonNullable()) {
t = Type(t.getHeapType(), Nullable);
}
finalTypes.push_back(t);
}
localType = Type(Tuple(finalTypes));
}
requireFunctionContext("pushExpression-tuple");
Index tuple = builder.addVar(currFunction, localType);
Index tuple = builder.addVar(currFunction, type);
expressionStack.push_back(builder.makeLocalSet(tuple, curr));
for (Index i = 0; i < localType.size(); ++i) {
Expression* value =
builder.makeTupleExtract(builder.makeLocalGet(tuple, localType), i);
if (localType[i] != type[i]) {
// We modified this to be nullable; undo that.
value = builder.makeRefAs(RefAsNonNull, value);
}
expressionStack.push_back(value);
for (Index i = 0; i < type.size(); ++i) {
expressionStack.push_back(
builder.makeTupleExtract(builder.makeLocalGet(tuple, type), i));
}
} else {
expressionStack.push_back(curr);
Expand Down
8 changes: 3 additions & 5 deletions test/lit/passes/roundtrip.wast
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; CHECK: (type $none (func))
(type $none (func))
;; CHECK: (func $foo (type $none)
;; CHECK-NEXT: (local $0 (funcref (ref null $none)))
;; CHECK-NEXT: (local $0 (funcref (ref $none)))
;; CHECK-NEXT: (local $1 funcref)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (block $label$1 (result funcref (ref $none))
Expand All @@ -23,10 +23,8 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (tuple.extract 1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (tuple.extract 1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $1)
Expand Down

0 comments on commit 5bfb192

Please sign in to comment.