Skip to content

Commit 0843618

Browse files
authored
[threads] Simplify and generalize reftype writing without GC (#6766)
Similar to #6765, but for types instead of heap types. Generalize the logic for transforming written reference types to types that are supported without GC so that it will automatically handle shared types and other new types correctly.
1 parent 6b93a84 commit 0843618

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

scripts/fuzz_opt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def is_git_repo():
361361
'shared-absheaptype.wast',
362362
'type-ssa-shared.wast',
363363
'shared-ref_eq.wast',
364-
'shared-null-no-gc.wast',
364+
'shared-types-no-gc.wast',
365365
]
366366

367367

src/wasm/wasm-binary.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,23 +1520,15 @@ void WasmBinaryWriter::writeType(Type type) {
15201520
// internally use more refined versions of those types, but we cannot emit
15211521
// those more refined types.
15221522
if (!wasm->features.hasGC()) {
1523-
if (Type::isSubType(type, Type(HeapType::func, Nullable))) {
1524-
o << S32LEB(BinaryConsts::EncodedType::funcref);
1525-
return;
1526-
}
1527-
if (Type::isSubType(type, Type(HeapType::ext, Nullable))) {
1528-
o << S32LEB(BinaryConsts::EncodedType::externref);
1529-
return;
1530-
}
1531-
if (Type::isSubType(type, Type(HeapType::exn, Nullable))) {
1532-
o << S32LEB(BinaryConsts::EncodedType::exnref);
1533-
return;
1534-
}
1535-
if (Type::isSubType(type, Type(HeapType::string, Nullable))) {
1536-
o << S32LEB(BinaryConsts::EncodedType::stringref);
1537-
return;
1523+
auto ht = type.getHeapType();
1524+
if (ht.isBasic() && ht.getBasic(Unshared) == HeapType::string) {
1525+
// Do not overgeneralize stringref to anyref. We have tests that when a
1526+
// stringref is expected, we actually get a stringref. If we see a
1527+
// string, the stringref feature must be enabled.
1528+
type = Type(HeapTypes::string.getBasic(ht.getShared()), Nullable);
1529+
} else {
1530+
type = Type(type.getHeapType().getTop(), Nullable);
15381531
}
1539-
WASM_UNREACHABLE("bad type without GC");
15401532
}
15411533
auto heapType = type.getHeapType();
15421534
if (type.isNullable() && heapType.isBasic() && !heapType.isShared()) {
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
22

3-
;; Test that we can write a binary without crashing when using a shared bottom
4-
;; type without GC enabled.
3+
;; Test that we can write a binary without crashing when using shared reference
4+
;; types without GC enabled.
55

66
;; RUN: wasm-opt %s --enable-reference-types --enable-shared-everything --roundtrip -S -o - | filecheck %s
77

88
(module
9-
;; CHECK: (func $test
9+
;; CHECK: (func $null
1010
;; CHECK-NEXT: (drop
1111
;; CHECK-NEXT: (ref.null (shared nofunc))
1212
;; CHECK-NEXT: )
1313
;; CHECK-NEXT: )
14-
(func $test
14+
(func $null
1515
(drop
1616
(ref.null (shared func))
1717
)
1818
)
19+
20+
;; CHECK: (func $signature (result (ref null (shared func)))
21+
;; CHECK-NEXT: (unreachable)
22+
;; CHECK-NEXT: )
23+
(func $signature (result (ref null (shared func)))
24+
(unreachable)
25+
)
1926
)

0 commit comments

Comments
 (0)