Skip to content
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
125 changes: 70 additions & 55 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,9 @@ namespace wasm {
// two non-reference children of `select` must have the same type because that
// would require inspecting the types of those children.
//
// The skipUnreachable() hook function determines the behavior on code that
// we cannot process due to unreachability. For example, an unreachable
// StructNew has no struct type defined, so we cannot apply its heap type. By
// default we do not skip such unreachable code, and error in such such cases if
// the type is not provided (by passing the heap type as mentioned above, as a
// parameter to the visit* method). Optimization passes can often skip
// unreachable code (leaving it for DCE), while other operations might not.
// The noteUnknown() hook is called whenever there is insufficient type
// information to generate constraints for all the children. Some users may wish
// to ignore this situation and others may want to assert that it never occurs.
template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
Module& wasm;
Function* func;
Expand Down Expand Up @@ -105,8 +101,6 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {

Type getLabelType(Name label) { return self().getLabelType(label); }

bool skipUnreachable() { return false; }

void visitNop(Nop* curr) {}

void visitBlock(Block* curr) {
Expand Down Expand Up @@ -195,7 +189,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitAtomicRMW(AtomicRMW* curr) {
if (self().skipUnreachable() && curr->type == Type::unreachable) {
if (curr->type == Type::unreachable) {
self().noteUnknown();
return;
}
assert(curr->type == Type::i32 || curr->type == Type::i64);
Expand Down Expand Up @@ -828,7 +823,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitTupleExtract(TupleExtract* curr,
std::optional<size_t> arity = std::nullopt) {
if (!arity) {
if (self().skipUnreachable() && !curr->tuple->type.isTuple()) {
if (!curr->tuple->type.isTuple()) {
self().noteUnknown();
return;
}
assert(curr->tuple->type.isTuple());
Expand All @@ -845,7 +841,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {

void visitCallRef(CallRef* curr, std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->target->type.isRef()) {
if (!curr->target->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->target->type.getHeapType().getSignature();
Expand All @@ -859,36 +856,32 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitRefTest(RefTest* curr) {
if (self().skipUnreachable() && !curr->castType.isRef()) {
return;
}
auto top = curr->castType.getHeapType().getTop();
note(&curr->ref, Type(top, Nullable));
}

void visitRefCast(RefCast* curr, std::optional<Type> target = std::nullopt) {
if (self().skipUnreachable() && !curr->type.isRef()) {
return;
if (!target) {
if (!curr->type.isRef()) {
self().noteUnknown();
return;
}
target = curr->type;
}
auto top = curr->type.getHeapType().getTop();
auto top = target->getHeapType().getTop();
note(&curr->ref, Type(top, Nullable));
if (curr->desc) {
if (!target) {
target = curr->type;
}
if (self().skipUnreachable() && !target->isRef()) {
return;
}
auto desc = target->getHeapType().getDescriptorType();
assert(desc);
note(&curr->desc, Type(*desc, Nullable, curr->type.getExactness()));
note(&curr->desc, Type(*desc, Nullable, target->getExactness()));
}
}

void visitRefGetDesc(RefGetDesc* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -900,18 +893,19 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
switch (curr->op) {
case BrOnNull:
case BrOnNonNull:
// br_on(_non)_null is polymorphic over reference types and does not
// take a type immediate.
assert(!target);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure why this is so. Perhaps add a comment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

noteAnyReference(&curr->ref);
return;
case BrOnCast:
case BrOnCastFail:
case BrOnCastDesc:
case BrOnCastDescFail: {
if (!target) {
assert(curr->castType.isRef());
target = curr->castType;
}
if (self().skipUnreachable() && !target->isRef()) {
return;
}
auto top = target->getHeapType().getTop();
note(&curr->ref, Type(top, Nullable));
if (curr->op == BrOnCastDesc || curr->op == BrOnCastDescFail) {
Expand All @@ -926,7 +920,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitStructNew(StructNew* curr) {
if (self().skipUnreachable() && !curr->type.isRef()) {
if (!curr->type.isRef()) {
self().noteUnknown();
return;
}
if (!curr->isWithDefault()) {
Expand All @@ -945,7 +940,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitStructGet(StructGet* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -956,7 +952,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitStructSet(StructSet* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -970,7 +967,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitStructRMW(StructRMW* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -984,7 +982,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitStructCmpxchg(StructCmpxchg* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -999,7 +998,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {

void visitArrayNew(ArrayNew* curr) {
if (!curr->isWithDefault()) {
if (self().skipUnreachable() && !curr->type.isRef()) {
if (!curr->type.isRef()) {
self().noteUnknown();
return;
}
note(&curr->init, curr->type.getHeapType().getArray().element.type);
Expand All @@ -1018,7 +1018,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitArrayNewFixed(ArrayNewFixed* curr) {
if (self().skipUnreachable() && !curr->type.isRef()) {
if (!curr->type.isRef()) {
self().noteUnknown();
return;
}
auto type = curr->type.getHeapType().getArray().element.type;
Expand All @@ -1030,7 +1031,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayGet(ArrayGet* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1042,7 +1044,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArraySet(ArraySet* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1061,13 +1064,15 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
std::optional<HeapType> dest = std::nullopt,
std::optional<HeapType> src = std::nullopt) {
if (!dest) {
if (self().skipUnreachable() && !curr->destRef->type.isRef()) {
if (!curr->destRef->type.isRef()) {
self().noteUnknown();
return;
}
dest = curr->destRef->type.getHeapType();
}
if (!src) {
if (self().skipUnreachable() && !curr->srcRef->type.isRef()) {
if (!curr->srcRef->type.isRef()) {
self().noteUnknown();
return;
}
src = curr->srcRef->type.getHeapType();
Expand All @@ -1082,7 +1087,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayFill(ArrayFill* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1097,7 +1103,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayInitData(ArrayInitData* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1111,7 +1118,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayInitElem(ArrayInitElem* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1125,7 +1133,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayRMW(ArrayRMW* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand All @@ -1139,7 +1148,8 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitArrayCmpxchg(ArrayCmpxchg* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
if (!curr->ref->type.isRef()) {
self().noteUnknown();
return;
}
ht = curr->ref->type.getHeapType();
Expand Down Expand Up @@ -1241,14 +1251,16 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
void visitContBind(ContBind* curr,
std::optional<HeapType> src = std::nullopt,
std::optional<HeapType> dest = std::nullopt) {
if (!src.has_value()) {
if (self().skipUnreachable() && !curr->cont->type.isRef()) {
if (!src) {
if (!curr->cont->type.isRef()) {
self().noteUnknown();
return;
}
src = curr->cont->type.getHeapType();
}
if (!dest.has_value()) {
if (self().skipUnreachable() && !curr->type.isRef()) {
if (!dest) {
if (!curr->type.isRef()) {
self().noteUnknown();
return;
}
dest = curr->type.getHeapType();
Expand All @@ -1273,8 +1285,9 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}

void visitResume(Resume* curr, std::optional<HeapType> ct = std::nullopt) {
if (!ct.has_value()) {
if (self().skipUnreachable() && !curr->cont->type.isRef()) {
if (!ct) {
if (!curr->cont->type.isRef()) {
self().noteUnknown();
return;
}
ct = curr->cont->type.getHeapType();
Expand All @@ -1290,8 +1303,9 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {

void visitResumeThrow(ResumeThrow* curr,
std::optional<HeapType> ct = std::nullopt) {
if (!ct.has_value()) {
if (self().skipUnreachable() && !curr->cont->type.isRef()) {
if (!ct) {
if (!curr->cont->type.isRef()) {
self().noteUnknown();
return;
}
ct = curr->cont->type.getHeapType();
Expand All @@ -1307,8 +1321,9 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {

void visitStackSwitch(StackSwitch* curr,
std::optional<HeapType> ct = std::nullopt) {
if (!ct.has_value()) {
if (self().skipUnreachable() && !curr->cont->type.isRef()) {
if (!ct) {
if (!curr->cont->type.isRef()) {
self().noteUnknown();
return;
}
ct = curr->cont->type.getHeapType();
Expand Down
5 changes: 2 additions & 3 deletions src/passes/TypeSSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,8 @@ struct Analyzer

Type getLabelType(Name label) { WASM_UNREACHABLE("unexpected branch"); }

// Skip unreachable code. If we cannot compute a constraint due to
// unreachability, we can ignore it.
bool skipUnreachable() { return true; }
// We don't mind if we cannot compute a constraint due to unreachability.
void noteUnknown() {}
} typer(*this);
typer.visit(curr);
}
Expand Down
5 changes: 5 additions & 0 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ struct IRBuilder::ChildPopper
// condition.
children.push_back({&curr->condition, {Subtype{Type::i32}}});
}

// It is a bug if we ever have insufficient type information.
void noteUnknown() {
WASM_UNREACHABLE("unexpected insufficient type information");
}
};

IRBuilder& builder;
Expand Down
Loading