diff options
author | Alon Zakai <azakai@google.com> | 2024-09-16 15:37:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-16 15:37:43 -0700 |
commit | ed19e3f699ddb72d59f227a9f20846c9ce79e2c6 (patch) | |
tree | c88e7be8f2672e6569f74c118dff12b201345c8e /src/wasm | |
parent | 106f84b4f2dc373b540ace29139f850576f22b8a (diff) | |
download | binaryen-ed19e3f699ddb72d59f227a9f20846c9ce79e2c6.tar.gz binaryen-ed19e3f699ddb72d59f227a9f20846c9ce79e2c6.tar.bz2 binaryen-ed19e3f699ddb72d59f227a9f20846c9ce79e2c6.zip |
[NFC] Move enough of wasm-type.cpp into wasm-type.h to inline core is*() methods (#6936)
This just moves code around. As a result, isRef() vanishes entirely from the
profiling traces in #6931, since now the core isRef/Tuple/etc. methods are
all inlineable.
This also required some reordering of wasm-type.h, namely to move HeapType
up front. No changes to that class otherwise.
TypeInfo is now in the header. getTypeInfo is now a static method on Type.
This has the downside of moving internal details into the header, and it may
increase compile time a little. The upside is making the --precompute benchmark
from #6931 significantly faster, 33%, and it will also help the many
Type::isNonNullable() etc. calls we have scattered around the codebase in
other passes too.
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/wasm-type.cpp | 124 |
1 files changed, 12 insertions, 112 deletions
diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 151d45a1d..dad8bb669 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -41,43 +41,6 @@ namespace wasm { namespace { -struct TypeInfo { - using type_t = Type; - // Used in assertions to ensure that temporary types don't leak into the - // global store. - bool isTemp = false; - enum Kind { - TupleKind, - RefKind, - } kind; - struct Ref { - HeapType heapType; - Nullability nullability; - }; - union { - Tuple tuple; - Ref ref; - }; - - TypeInfo(const Tuple& tuple) : kind(TupleKind), tuple(tuple) {} - TypeInfo(Tuple&& tuple) : kind(TupleKind), tuple(std::move(tuple)) {} - TypeInfo(HeapType heapType, Nullability nullable) - : kind(RefKind), ref{heapType, nullable} {} - TypeInfo(const TypeInfo& other); - ~TypeInfo(); - - constexpr bool isTuple() const { return kind == TupleKind; } - constexpr bool isRef() const { return kind == RefKind; } - - // If this TypeInfo represents a Type that can be represented more simply, - // return that simpler Type. For example, this handles eliminating singleton - // tuple types. - std::optional<Type> getCanonical() const; - - bool operator==(const TypeInfo& other) const; - bool operator!=(const TypeInfo& other) const { return !(*this == other); } -}; - using RecGroupInfo = std::vector<HeapType>; struct HeapTypeInfo { @@ -403,11 +366,6 @@ public: namespace wasm { namespace { -TypeInfo* getTypeInfo(Type type) { - assert(!type.isBasic()); - return (TypeInfo*)type.getID(); -} - HeapTypeInfo* getHeapTypeInfo(HeapType ht) { assert(!ht.isBasic()); return (HeapTypeInfo*)ht.getID(); @@ -419,12 +377,14 @@ HeapType asHeapType(std::unique_ptr<HeapTypeInfo>& info) { Type markTemp(Type type) { if (!type.isBasic()) { - getTypeInfo(type)->isTemp = true; + Type::getTypeInfo(type)->isTemp = true; } return type; } -bool isTemp(Type type) { return !type.isBasic() && getTypeInfo(type)->isTemp; } +bool isTemp(Type type) { + return !type.isBasic() && Type::getTypeInfo(type)->isTemp; +} bool isTemp(HeapType type) { return !type.isBasic() && getHeapTypeInfo(type)->isTemp; @@ -517,6 +477,8 @@ std::optional<HeapType> getBasicHeapTypeLUB(HeapType::BasicHeapType a, return {lubUnshared.getBasic(share)}; } +} // anonymous namespace + TypeInfo::TypeInfo(const TypeInfo& other) { kind = other.kind; switch (kind) { @@ -588,6 +550,8 @@ HeapTypeInfo::~HeapTypeInfo() { WASM_UNREACHABLE("unexpected kind"); } +namespace { + struct TypeStore { std::recursive_mutex mutex; @@ -756,60 +720,6 @@ Type::Type(HeapType heapType, Nullability nullable) { new (this) Type(globalTypeStore.insert(TypeInfo(heapType, nullable))); } -bool Type::isTuple() const { - if (isBasic()) { - return false; - } else { - return getTypeInfo(*this)->isTuple(); - } -} - -bool Type::isRef() const { - if (isBasic()) { - return false; - } else { - return getTypeInfo(*this)->isRef(); - } -} - -bool Type::isFunction() const { - if (isBasic()) { - return false; - } else { - auto* info = getTypeInfo(*this); - return info->isRef() && info->ref.heapType.isFunction(); - } -} - -bool Type::isData() const { - if (isBasic()) { - return false; - } else { - auto* info = getTypeInfo(*this); - return info->isRef() && info->ref.heapType.isData(); - } -} - -bool Type::isNullable() const { - if (isRef()) { - return getTypeInfo(*this)->ref.nullability == Nullable; - } else { - return false; - } -} - -bool Type::isNonNullable() const { - if (isRef()) { - return getTypeInfo(*this)->ref.nullability == NonNullable; - } else { - return false; - } -} - -bool Type::isSignature() const { - return isRef() && getHeapType().isSignature(); -} - bool Type::isStruct() const { return isRef() && getHeapType().isStruct(); } bool Type::isArray() const { return isRef() && getHeapType().isArray(); } @@ -919,16 +829,6 @@ FeatureSet Type::getFeatures() const { return getSingleFeatures(*this); } -const Tuple& Type::getTuple() const { - assert(isTuple()); - return getTypeInfo(*this)->tuple; -} - -HeapType Type::getHeapType() const { - assert(isRef()); - return getTypeInfo(*this)->ref.heapType; -} - Type Type::get(unsigned byteSize, bool float_) { if (byteSize < 4) { return Type::i32; @@ -2111,7 +2011,7 @@ size_t RecGroupHasher::hash(Type type) const { if (type.isBasic()) { wasm::rehash(digest, type.getID()); } else { - hash_combine(digest, hash(*getTypeInfo(type))); + hash_combine(digest, hash(*Type::getTypeInfo(type))); } return digest; } @@ -2243,7 +2143,7 @@ bool RecGroupEquator::eq(Type a, Type b) const { if (a.isBasic() || b.isBasic()) { return a == b; } - return eq(*getTypeInfo(a), *getTypeInfo(b)); + return eq(*Type::getTypeInfo(a), *Type::getTypeInfo(b)); } bool RecGroupEquator::eq(HeapType a, HeapType b) const { @@ -2393,7 +2293,7 @@ template<typename Self> void TypeGraphWalkerBase<Self>::scanType(Type* type) { if (type->isBasic()) { return; } - auto* info = getTypeInfo(*type); + auto* info = Type::getTypeInfo(*type); switch (info->kind) { case TypeInfo::TupleKind: { auto& types = info->tuple; @@ -2799,7 +2699,7 @@ buildRecGroup(std::unique_ptr<RecGroupInfo>&& groupInfo, auto canonicalizeTypes = [&](bool tuples) { for (auto& [original, uses] : locations.types) { if (original.isTuple() == tuples) { - Type canonical = globalTypeStore.insert(*getTypeInfo(original)); + Type canonical = globalTypeStore.insert(*Type::getTypeInfo(original)); for (Type* use : uses) { *use = canonical; } |