Skip to content

Commit 43d635c

Browse files
authored
Subtyping and LUBs for exact heap types (#7412)
Also fix `with(Inexact)` to no longer strip sharedness from basic heap types. Add exact heap types, defined subtypes, and exact subtypes to the gtest for heap type relations.
1 parent 3875529 commit 43d635c

File tree

3 files changed

+183
-9
lines changed

3 files changed

+183
-9
lines changed

src/wasm-type.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ class HeapType {
234234
constexpr HeapType with(Exactness exactness) const {
235235
assert((!isBasic() || exactness == Inexact) &&
236236
"abstract types cannot be exact");
237-
return HeapType(exactness == Exact ? (id | ExactMask) : (id & ~ExactMask));
237+
return isBasic() ? *this
238+
: HeapType(exactness == Exact ? (id | ExactMask)
239+
: (id & ~ExactMask));
238240
}
239241

240242
// The ID is the numeric representation of the heap type and can be used in

src/wasm/wasm-type.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ Type Type::getLeastUpperBound(Type a, Type b) {
798798
}
799799
}
800800
return Type::none;
801-
WASM_UNREACHABLE("unexpected type");
802801
}
803802

804803
Type Type::getGreatestLowerBound(Type a, Type b) {
@@ -1195,6 +1194,9 @@ std::optional<HeapType> HeapType::getLeastUpperBound(HeapType a, HeapType b) {
11951194
return getBasicHeapTypeLUB(getBasicHeapSupertype(a),
11961195
getBasicHeapSupertype(b));
11971196
}
1197+
if (a.with(Inexact) == b.with(Inexact)) {
1198+
return a.with(Inexact);
1199+
}
11981200

11991201
auto* infoA = getHeapTypeInfo(a);
12001202
auto* infoB = getHeapTypeInfo(b);
@@ -1505,7 +1507,7 @@ bool SubTyper::isSubType(HeapType a, HeapType b) {
15051507
// See:
15061508
// https://github.com/WebAssembly/function-references/blob/master/proposals/function-references/Overview.md#subtyping
15071509
// https://github.com/WebAssembly/gc/blob/master/proposals/gc/MVP.md#defined-types
1508-
if (a == b) {
1510+
if (a == b || a.with(Inexact) == b) {
15091511
return true;
15101512
}
15111513
if (a.isShared() != b.isShared()) {
@@ -1550,6 +1552,11 @@ bool SubTyper::isSubType(HeapType a, HeapType b) {
15501552
// bottom types.
15511553
return a == b.getBottom();
15521554
}
1555+
if (b.isExact()) {
1556+
// The only subtypes of an exact type are itself and bottom, both of which
1557+
// we have ruled out.
1558+
return false;
1559+
}
15531560
// Subtyping must be declared rather than derived from structure, so we will
15541561
// not recurse. TODO: optimize this search with some form of caching.
15551562
HeapTypeInfo* curr = getHeapTypeInfo(a);

0 commit comments

Comments
 (0)