Skip to content

Implement type imports and exports #7330

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Fix hashing and comparison
  • Loading branch information
vouillon committed Feb 27, 2025
commit df600790728f55c646bce3fb57c8a651dec57aa7
14 changes: 12 additions & 2 deletions src/passes/TypeMerging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct TypeMerging : public Pass {
bool shapeEq(HeapType a, HeapType b);
bool shapeEq(const Struct& a, const Struct& b);
bool shapeEq(Array a, Array b);
bool shapeEq(Import a, Import b);
bool shapeEq(Signature a, Signature b);
bool shapeEq(Field a, Field b);
bool shapeEq(Type a, Type b);
Expand Down Expand Up @@ -574,7 +575,7 @@ bool shapeEq(HeapType a, HeapType b) {
case HeapTypeKind::Cont:
WASM_UNREACHABLE("TODO: cont");
case HeapTypeKind::Import:
return false;
return shapeEq(a.getImport(), b.getImport());
case HeapTypeKind::Basic:
WASM_UNREACHABLE("unexpected kind");
}
Expand Down Expand Up @@ -641,7 +642,16 @@ size_t shapeHash(Signature a) {
return digest;
}

size_t shapeHash(Import a) { return shapeHash(a.bound); }
bool shapeEq(Import a, Import b) {
return a.module == b.module && a.base == b.base && shapeEq(a.bound, b.bound);
}

size_t shapeHash(Import a) {
size_t digest = shapeHash(a.bound);
hash_combine(digest, wasm::hash(a.module));
hash_combine(digest, wasm::hash(a.base));
return digest;
}

bool shapeEq(Field a, Field b) {
return a.packedType == b.packedType && a.mutable_ == b.mutable_ &&
Expand Down
19 changes: 16 additions & 3 deletions src/wasm/wasm-type-shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ template<typename CompareTypes> struct RecGroupComparator {
return compare(a.type, b.type);
}

Comparison compare(Import a, Import b) { return compare(a.bound, b.bound); }
Comparison compare(Import a, Import b) {
if (a.module != b.module) {
return a.module < b.module ? LT : GT;
}
if (a.base != b.base) {
return a.base < b.base ? LT : GT;
}
return compare(a.bound, b.bound);
}

Comparison compare(Field a, Field b) {
if (a.mutable_ != b.mutable_) {
Expand Down Expand Up @@ -247,7 +255,7 @@ struct RecGroupHasher {
hash_combine(digest, hash(type.getContinuation()));
return digest;
case HeapTypeKind::Import:
assert(type.isContinuation());
assert(type.isImport());
wasm::rehash(digest, 1077427175);
hash_combine(digest, hash(type.getImport()));
return digest;
Expand Down Expand Up @@ -275,7 +283,12 @@ struct RecGroupHasher {

size_t hash(Continuation cont) { return hash(cont.type); }

size_t hash(Import import) { return hash(import.bound); }
size_t hash(Import import) {
size_t digest = hash(import.bound);
hash_combine(digest, wasm::hash(import.module));
hash_combine(digest, wasm::hash(import.base));
return digest;
}

size_t hash(Field field) {
size_t digest = wasm::hash(field.mutable_);
Expand Down