Skip to content

Update exact reference semantics #7499

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 17 commits into from
Apr 16, 2025
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
6 changes: 0 additions & 6 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,6 @@ class Builder {
ret->finalize(Type(type.getBottom(), Nullable));
return ret;
}
RefNull* makeRefNull(Type type) {
assert(type.isNullable() && type.isNull() && type.isExact());
auto* ret = wasm.allocator.alloc<RefNull>();
ret->finalize(type);
return ret;
}
RefIsNull* makeRefIsNull(Expression* value) {
auto* ret = wasm.allocator.alloc<RefIsNull>();
ret->value = value;
Expand Down
1 change: 1 addition & 0 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class Type {
: Type(heapType.getID() | (nullable == Nullable ? NullMask : 0) |
(exact == Exact ? ExactMask : 0)) {
assert(!(heapType.getID() & (TupleMask | NullMask | ExactMask)));
assert(!heapType.isBasic() || exact == Inexact);
}

// Predicates
Expand Down
28 changes: 16 additions & 12 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,14 @@ Type Type::getLeastUpperBound(Type a, Type b) {
if (auto heapType = HeapType::getLeastUpperBound(heapTypeA, heapTypeB)) {
auto nullability =
(a.isNullable() || b.isNullable()) ? Nullable : NonNullable;
auto exactness = (a.isInexact() || b.isInexact()) ? Inexact : Exact;
// The LUB can only be exact if the heap types are the same or one of them
// is bottom.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment still seems useful, perhaps modified?

if (heapTypeA != heapTypeB && !heapTypeA.isBottom() &&
!heapTypeB.isBottom()) {
exactness = Inexact;
}
// If one heap type is bottom, then the exactness comes from the other
// heap type. Otherwise, the LUB can only be exact if both of the inputs
// are the same exact heap type.
auto exactness = heapTypeA.isBottom() ? b.getExactness()
: heapTypeB.isBottom() ? a.getExactness()
: (a.isInexact() || b.isInexact()) ? Inexact
: (heapTypeA != heapTypeB) ? Inexact
: Exact;
return Type(*heapType, nullability, exactness);
}
}
Expand Down Expand Up @@ -850,6 +851,7 @@ Type Type::getGreatestLowerBound(Type a, Type b) {
if ((a.isExact() && heapType != heapA) ||
(b.isExact() && heapType != heapB)) {
heapType = heapA.getBottom();
exactness = Inexact;
}
return Type(heapType, nullability, exactness);
}
Expand Down Expand Up @@ -1515,13 +1517,15 @@ bool SubTyper::isSubType(Type a, Type b) {
if (a.isNullable() && !b.isNullable()) {
return false;
}
if (a.isInexact() && !b.isInexact()) {
return false;
}
auto heapTypeA = a.getHeapType();
auto heapTypeB = b.getHeapType();
if (b.isExact() && !heapTypeA.isBottom()) {
return heapTypeA == heapTypeB;
if (b.isExact()) {
if (a.isExact()) {
return heapTypeA == heapTypeB;
}
if (!heapTypeA.isBottom()) {
return false;
}
}
return isSubType(heapTypeA, heapTypeB);
}
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ void MemoryGrow::finalize() {

void RefNull::finalize(HeapType heapType) {
assert(heapType.isBottom());
type = Type(heapType, Nullable, Exact);
type = Type(heapType, Nullable);
}

void RefNull::finalize(Type type_) { type = type_; }
Expand Down
270 changes: 128 additions & 142 deletions test/gtest/type-builder.cpp

Large diffs are not rendered by default.

Loading
Loading