Skip to content

Make i31ref and dataref nullable #4843

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 5 commits into from
Aug 26, 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
8 changes: 1 addition & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ Current Trunk
- Change constant values of some reference types in the C and JS APIs. This is
only observable if you hardcode specific values instead of calling the
relevant methods (like `BinaryenTypeDataref()`). (#4755)
- `BinaryenModulePrintStackIR`, `BinaryenModuleWriteStackIR` and
`BinaryenModuleAllocateAndWriteStackIR` now have an extra boolean
argument `optimize`. (#4832)
- Remove support for the `let` instruction that has been removed from the typed
function references spec.
- HeapType::ext has been restored but is no longer a subtype of HeapType::any to
match the latest updates in the GC spec. (#4898)
- `i31ref` and `dataref` are now nullable to match the latest GC spec. (#4843)

v109
----
Expand Down
82 changes: 32 additions & 50 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,38 @@ static bool maybePrintRefShorthand(std::ostream& o, Type type) {
return false;
}
auto heapType = type.getHeapType();
if (heapType.isBasic()) {
if (type.isNullable()) {
switch (heapType.getBasic()) {
case HeapType::ext:
o << "externref";
return true;
case HeapType::func:
o << "funcref";
return true;
case HeapType::any:
o << "anyref";
return true;
case HeapType::eq:
o << "eqref";
return true;
case HeapType::i31:
case HeapType::data:
break;
case HeapType::string:
o << "stringref";
return true;
case HeapType::stringview_wtf8:
o << "stringview_wtf8";
return true;
case HeapType::stringview_wtf16:
o << "stringview_wtf16";
return true;
case HeapType::stringview_iter:
o << "stringview_iter";
return true;
}
} else {
switch (heapType.getBasic()) {
case HeapType::ext:
case HeapType::func:
case HeapType::any:
case HeapType::eq:
break;
case HeapType::i31:
o << "i31ref";
return true;
case HeapType::data:
o << "dataref";
return true;
case HeapType::string:
case HeapType::stringview_wtf8:
case HeapType::stringview_wtf16:
case HeapType::stringview_iter:
break;
}
if (heapType.isBasic() && type.isNullable()) {
switch (heapType.getBasic()) {
case HeapType::ext:
o << "externref";
return true;
case HeapType::func:
o << "funcref";
return true;
case HeapType::any:
o << "anyref";
return true;
case HeapType::eq:
o << "eqref";
return true;
case HeapType::i31:
o << "i31ref";
return true;
case HeapType::data:
o << "dataref";
return true;
case HeapType::string:
o << "stringref";
return true;
case HeapType::stringview_wtf8:
o << "stringview_wtf8";
return true;
case HeapType::stringview_wtf16:
o << "stringview_wtf16";
return true;
case HeapType::stringview_iter:
o << "stringview_iter";
return true;
}
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ class Type {
// │ funcref ║ x │ │ x │ x │ f n │ ┐ Ref
// │ anyref ║ x │ │ x │ x │ f? n │ │ f_unc
// │ eqref ║ x │ │ x │ x │ n │ │ n_ullable
// │ i31ref ║ x │ │ x │ x │ │ │
// │ dataref ║ x │ │ x │ x │ │ │
// │ i31ref ║ x │ │ x │ x │ n │ │
// │ dataref ║ x │ │ x │ x │ n │ │
// ├─ Compound ──╫───┼───┼───┼───┤───────┤ │
// │ Ref ║ │ x │ x │ x │ f? n? │◄┘
// │ Tuple ║ │ x │ │ x │ │
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,10 +1825,10 @@ bool WasmBinaryBuilder::getBasicType(int32_t code, Type& out) {
out = Type(HeapType::eq, Nullable);
return true;
case BinaryConsts::EncodedType::i31ref:
out = Type(HeapType::i31, NonNullable);
out = Type(HeapType::i31, Nullable);
return true;
case BinaryConsts::EncodedType::dataref:
out = Type(HeapType::data, NonNullable);
out = Type(HeapType::data, Nullable);
return true;
case BinaryConsts::EncodedType::stringref:
out = Type(HeapType::string, Nullable);
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wasm-s-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,10 @@ Type SExpressionWasmBuilder::stringToType(const char* str,
return Type(HeapType::eq, Nullable);
}
if (strncmp(str, "i31ref", 6) == 0 && (prefix || str[6] == 0)) {
return Type(HeapType::i31, NonNullable);
return Type(HeapType::i31, Nullable);
}
if (strncmp(str, "dataref", 7) == 0 && (prefix || str[7] == 0)) {
return Type(HeapType::data, NonNullable);
return Type(HeapType::data, Nullable);
}
if (strncmp(str, "stringref", 9) == 0 && (prefix || str[9] == 0)) {
return Type(HeapType::string, Nullable);
Expand Down
4 changes: 2 additions & 2 deletions src/wasm/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,10 @@ MaybeResult<typename Ctx::TypeT> reftype(Ctx& ctx, ParseInput& in) {
return ctx.makeRefType(ctx.makeEq(), Nullable);
}
if (in.takeKeyword("i31ref"sv)) {
return ctx.makeRefType(ctx.makeI31(), NonNullable);
return ctx.makeRefType(ctx.makeI31(), Nullable);
}
if (in.takeKeyword("dataref"sv)) {
return ctx.makeRefType(ctx.makeData(), NonNullable);
return ctx.makeRefType(ctx.makeData(), Nullable);
}
if (in.takeKeyword("arrayref"sv)) {
return in.err("arrayref not yet supported");
Expand Down
8 changes: 4 additions & 4 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2161,10 +2161,10 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(pop eqref)
)
(drop
(pop i31ref)
(pop (ref i31))
)
(drop
(pop dataref)
(pop (ref data))
)
(drop
(pop stringref)
Expand Down Expand Up @@ -4265,10 +4265,10 @@ getExpressionInfo(tuple[3])={"id":14,"type":5,"value":3.7}
(pop eqref)
)
(drop
(pop i31ref)
(pop (ref i31))
)
(drop
(pop dataref)
(pop (ref data))
)
(drop
(pop stringref)
Expand Down
4 changes: 2 additions & 2 deletions test/gc.wast.from-wast
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
(global $global_anyref (mut anyref) (ref.null any))
(global $global_eqref (mut eqref) (ref.null eq))
(global $global_i31ref (mut i31ref) (i31.new
Expand Down Expand Up @@ -148,7 +148,7 @@
)
)
)
(func $test-variants (param $local_i31refnull (ref null i31)) (param $local_i31refnonnull i31ref) (param $local_datarefnull (ref null data)) (param $local_datarefnonnull dataref)
(func $test-variants (param $local_i31refnull i31ref) (param $local_i31refnonnull (ref i31)) (param $local_datarefnull dataref) (param $local_datarefnonnull (ref data))
(nop)
)
)
4 changes: 2 additions & 2 deletions test/gc.wast.fromBinary
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
(global $global_anyref (mut anyref) (ref.null any))
(global $global_eqref (mut eqref) (ref.null eq))
(global $global_i31ref (mut i31ref) (i31.new
Expand Down Expand Up @@ -148,7 +148,7 @@
)
)
)
(func $test-variants (param $local_i31refnull (ref null i31)) (param $local_i31refnonnull i31ref) (param $local_datarefnull (ref null data)) (param $local_datarefnonnull dataref)
(func $test-variants (param $local_i31refnull i31ref) (param $local_i31refnonnull (ref i31)) (param $local_datarefnull dataref) (param $local_datarefnonnull (ref data))
(nop)
)
)
Expand Down
4 changes: 2 additions & 2 deletions test/gc.wast.fromBinary.noDebugInfo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(type $i31ref_dataref_=>_none (func (param i31ref dataref)))
(type $ref?|i31|_i31ref_ref?|data|_dataref_=>_none (func (param (ref null i31) i31ref (ref null data) dataref)))
(type $i31ref_ref|i31|_dataref_ref|data|_=>_none (func (param i31ref (ref i31) dataref (ref data))))
(global $global$0 (mut anyref) (ref.null any))
(global $global$1 (mut eqref) (ref.null eq))
(global $global$2 (mut i31ref) (i31.new
Expand Down Expand Up @@ -148,7 +148,7 @@
)
)
)
(func $1 (param $0 (ref null i31)) (param $1 i31ref) (param $2 (ref null data)) (param $3 dataref)
(func $1 (param $0 i31ref) (param $1 (ref i31)) (param $2 dataref) (param $3 (ref data))
(nop)
)
)
Expand Down
8 changes: 4 additions & 4 deletions test/heap-types.wast.from-wast
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@
(local $y anyref)
(local $z anyref)
(local $temp-func funcref)
(local $temp-data (ref null data))
(local $temp-i31 (ref null i31))
(local $temp-data dataref)
(local $temp-i31 i31ref)
(block $null
(local.set $z
(br_on_null $null
Expand All @@ -241,7 +241,7 @@
)
)
(drop
(block $data (result (ref null data))
(block $data (result dataref)
(local.set $y
(br_on_data $data
(local.get $x)
Expand All @@ -251,7 +251,7 @@
)
)
(drop
(block $i31 (result (ref null i31))
(block $i31 (result i31ref)
(local.set $y
(br_on_i31 $i31
(local.get $x)
Expand Down
8 changes: 4 additions & 4 deletions test/heap-types.wast.fromBinary
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@
(local $y anyref)
(local $z anyref)
(local $temp-func funcref)
(local $temp-data (ref null data))
(local $temp-i31 (ref null i31))
(local $temp-data dataref)
(local $temp-i31 i31ref)
(block $label$1
(local.set $z
(br_on_null $label$1
Expand All @@ -239,7 +239,7 @@
)
)
(drop
(block $label$3 (result (ref null data))
(block $label$3 (result dataref)
(local.set $y
(br_on_data $label$3
(local.get $x)
Expand All @@ -249,7 +249,7 @@
)
)
(drop
(block $label$4 (result (ref null i31))
(block $label$4 (result i31ref)
(local.set $y
(br_on_i31 $label$4
(local.get $x)
Expand Down
8 changes: 4 additions & 4 deletions test/heap-types.wast.fromBinary.noDebugInfo
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@
(local $1 anyref)
(local $2 anyref)
(local $3 funcref)
(local $4 (ref null data))
(local $5 (ref null i31))
(local $4 dataref)
(local $5 i31ref)
(block $label$1
(local.set $2
(br_on_null $label$1
Expand All @@ -239,7 +239,7 @@
)
)
(drop
(block $label$3 (result (ref null data))
(block $label$3 (result dataref)
(local.set $1
(br_on_data $label$3
(local.get $0)
Expand All @@ -249,7 +249,7 @@
)
)
(drop
(block $label$4 (result (ref null i31))
(block $label$4 (result i31ref)
(local.set $1
(br_on_i31 $label$4
(local.get $0)
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/coalesce-locals-gc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
;; CHECK: (global $global (ref null $array) (ref.null $array))
(global $global (ref null $array) (ref.null $array))

;; CHECK: (func $test-dead-get-non-nullable (param $0 dataref)
;; CHECK: (func $test-dead-get-non-nullable (param $0 (ref data))
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result dataref)
;; CHECK-NEXT: (block (result (ref data))
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down
16 changes: 6 additions & 10 deletions test/lit/passes/dae-gc-refine-params.wast
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@
)

;; CHECK: (func $unused-and-refinable
;; CHECK-NEXT: (local $0 (ref null data))
;; CHECK-NEXT: (local $0 dataref)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
;; NOMNL: (func $unused-and-refinable (type $none_=>_none)
;; NOMNL-NEXT: (local $0 (ref null data))
;; NOMNL-NEXT: (local $0 dataref)
;; NOMNL-NEXT: (nop)
;; NOMNL-NEXT: )
(func $unused-and-refinable (param $0 dataref)
Expand Down Expand Up @@ -498,25 +498,21 @@
)

;; CHECK: (func $non-nullable-fixup (param $0 (ref ${}))
;; CHECK-NEXT: (local $1 (ref null data))
;; CHECK-NEXT: (local $1 dataref)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (ref.as_non_null
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; NOMNL: (func $non-nullable-fixup (type $ref|${}|_=>_none) (param $0 (ref ${}))
;; NOMNL-NEXT: (local $1 (ref null data))
;; NOMNL-NEXT: (local $1 dataref)
;; NOMNL-NEXT: (local.set $1
;; NOMNL-NEXT: (local.get $0)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (local.set $1
;; NOMNL-NEXT: (ref.as_non_null
;; NOMNL-NEXT: (local.get $1)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: (local.get $1)
;; NOMNL-NEXT: )
;; NOMNL-NEXT: )
(func $non-nullable-fixup (param $0 dataref)
Expand Down
Loading