summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2023-09-27 10:57:42 -0700
committerGitHub <noreply@github.com>2023-09-27 17:57:42 +0000
commit7fe2773010a5be824ac2b75cf8ee5dac90368aaf (patch)
tree5fc1ed31e8b6a1d04e35a1c11558bba4a96e29e2 /src
parentb2af87928a8c39e9c6ebedc748412c15c95c56ab (diff)
downloadbinaryen-7fe2773010a5be824ac2b75cf8ee5dac90368aaf.tar.gz
binaryen-7fe2773010a5be824ac2b75cf8ee5dac90368aaf.tar.bz2
binaryen-7fe2773010a5be824ac2b75cf8ee5dac90368aaf.zip
[NFC] Refactor SupertypesFirst utility (#5979)
Move the topological sort from the constructor to a separate method. This CRTP utility calls into its subclass to query supertype relationships, but doing so in the base class constructor means that the subclass has not been fully initialized yet, which can cause problems.
Diffstat (limited to 'src')
-rw-r--r--src/ir/type-updating.cpp7
-rw-r--r--src/passes/TypeMerging.cpp12
-rw-r--r--src/wasm-type-ordering.h9
3 files changed, 15 insertions, 13 deletions
diff --git a/src/ir/type-updating.cpp b/src/ir/type-updating.cpp
index 59b77cca1..642992d7e 100644
--- a/src/ir/type-updating.cpp
+++ b/src/ir/type-updating.cpp
@@ -45,15 +45,14 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes() {
: HeapTypeOrdering::SupertypesFirstBase<SupertypesFirst> {
GlobalTypeRewriter& parent;
- SupertypesFirst(GlobalTypeRewriter& parent,
- const std::vector<HeapType>& types)
- : SupertypesFirstBase(types), parent(parent) {}
+ SupertypesFirst(GlobalTypeRewriter& parent) : parent(parent) {}
std::optional<HeapType> getSuperType(HeapType type) {
return parent.getSuperType(type);
}
};
- for (auto type : SupertypesFirst(*this, privateTypes)) {
+ SupertypesFirst sortedTypes(*this);
+ for (auto type : sortedTypes.sort(privateTypes)) {
typeIndices[type] = i++;
}
diff --git a/src/passes/TypeMerging.cpp b/src/passes/TypeMerging.cpp
index 3d7a032b5..7bd4ad7fe 100644
--- a/src/passes/TypeMerging.cpp
+++ b/src/passes/TypeMerging.cpp
@@ -167,9 +167,7 @@ struct MergeableSupertypesFirst
: HeapTypeOrdering::SupertypesFirstBase<MergeableSupertypesFirst> {
TypeMerging& merging;
- template<typename T>
- MergeableSupertypesFirst(TypeMerging& merging, const T& types)
- : SupertypesFirstBase(types), merging(merging) {}
+ MergeableSupertypesFirst(TypeMerging& merging) : merging(merging) {}
std::optional<HeapType> getSuperType(HeapType type) {
if (auto super = type.getSuperType()) {
@@ -307,7 +305,8 @@ bool TypeMerging::merge(MergeKind kind) {
// For each type, either create a new partition or add to its supertype's
// partition.
- for (auto type : MergeableSupertypesFirst(*this, mergeable)) {
+ MergeableSupertypesFirst sortedTypes(*this);
+ for (auto type : sortedTypes.sort(mergeable)) {
// We need partitions for any public children of this type since those
// children will participate in the DFA we're creating.
for (auto child : getPublicChildren(type)) {
@@ -415,7 +414,7 @@ bool TypeMerging::merge(MergeKind kind) {
std::vector<HeapType> newMergeable;
bool merged = false;
for (const auto& partition : refinedPartitions) {
- auto target = *MergeableSupertypesFirst(*this, partition).begin();
+ auto target = *MergeableSupertypesFirst(*this).sort(partition).begin();
newMergeable.push_back(target);
for (auto type : partition) {
if (type != target) {
@@ -453,7 +452,8 @@ TypeMerging::splitSupertypePartition(const std::vector<HeapType>& types) {
std::unordered_set<HeapType> includedTypes(types.begin(), types.end());
std::vector<std::vector<HeapType>> partitions;
std::unordered_map<HeapType, Index> partitionIndices;
- for (auto type : MergeableSupertypesFirst(*this, types)) {
+ MergeableSupertypesFirst sortedTypes(*this);
+ for (auto type : sortedTypes.sort(types)) {
auto super = type.getSuperType();
if (super && includedTypes.count(*super)) {
// We must already have a partition for the supertype we can add to.
diff --git a/src/wasm-type-ordering.h b/src/wasm-type-ordering.h
index af1995de4..400ac014a 100644
--- a/src/wasm-type-ordering.h
+++ b/src/wasm-type-ordering.h
@@ -35,13 +35,15 @@ struct SupertypesFirstBase
// track membership in the input collection.
InsertOrderedMap<HeapType, bool> typeSet;
- template<typename T> SupertypesFirstBase(const T& types) {
+ SupertypeProvider& self() { return *static_cast<SupertypeProvider*>(this); }
+
+ template<typename T> SupertypeProvider& sort(const T& types) {
for (auto type : types) {
typeSet[type] = false;
}
// Find the supertypes that are in the collection.
for (auto [type, _] : typeSet) {
- if (auto super = type.getSuperType()) {
+ if (auto super = self().getSuperType(type)) {
if (auto it = typeSet.find(*super); it != typeSet.end()) {
it->second = true;
}
@@ -53,11 +55,12 @@ struct SupertypesFirstBase
this->push(type);
}
}
+ return self();
}
void pushPredecessors(HeapType type) {
// Do not visit types that weren't in the input collection.
- if (auto super = static_cast<SupertypeProvider*>(this)->getSuperType(type);
+ if (auto super = self().getSuperType(type);
super && typeSet.count(*super)) {
this->push(*super);
}