Skip to content

Commit 242e6a8

Browse files
committed
[EquivalenceClasses] Use DenseMap instead of std::set. (NFC)
1 parent ee4e819 commit 242e6a8

File tree

4 files changed

+20
-71
lines changed

4 files changed

+20
-71
lines changed

llvm/include/llvm/ADT/EquivalenceClasses.h

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
#ifndef LLVM_ADT_EQUIVALENCECLASSES_H
1616
#define LLVM_ADT_EQUIVALENCECLASSES_H
1717

18+
#include "llvm/ADT/DenseMap.h"
1819
#include "llvm/ADT/SmallVector.h"
20+
#include "llvm/Support/Allocator.h"
1921
#include <cassert>
2022
#include <cstddef>
2123
#include <cstdint>
2224
#include <iterator>
23-
#include <set>
2425

2526
namespace llvm {
2627

@@ -57,8 +58,7 @@ namespace llvm {
5758
/// 4
5859
/// 5 1 2
5960
///
60-
template <class ElemTy, class Compare = std::less<ElemTy>>
61-
class EquivalenceClasses {
61+
template <class ElemTy> class EquivalenceClasses {
6262
/// ECValue - The EquivalenceClasses data structure is just a set of these.
6363
/// Each of these represents a relation for a value. First it stores the
6464
/// value itself, which provides the ordering that the set queries. Next, it
@@ -112,36 +112,15 @@ class EquivalenceClasses {
112112
}
113113
};
114114

115-
/// A wrapper of the comparator, to be passed to the set.
116-
struct ECValueComparator {
117-
using is_transparent = void;
118-
119-
ECValueComparator() : compare(Compare()) {}
120-
121-
bool operator()(const ECValue &lhs, const ECValue &rhs) const {
122-
return compare(lhs.Data, rhs.Data);
123-
}
124-
125-
template <typename T>
126-
bool operator()(const T &lhs, const ECValue &rhs) const {
127-
return compare(lhs, rhs.Data);
128-
}
129-
130-
template <typename T>
131-
bool operator()(const ECValue &lhs, const T &rhs) const {
132-
return compare(lhs.Data, rhs);
133-
}
134-
135-
const Compare compare;
136-
};
137-
138115
/// TheMapping - This implicitly provides a mapping from ElemTy values to the
139116
/// ECValues, it just keeps the key as part of the value.
140-
std::set<ECValue, ECValueComparator> TheMapping;
117+
DenseMap<ElemTy, ECValue *> TheMapping;
141118

142119
/// List of all members, used to provide a determinstic iteration order.
143120
SmallVector<const ECValue *> Members;
144121

122+
mutable BumpPtrAllocator ECValueAllocator;
123+
145124
public:
146125
EquivalenceClasses() = default;
147126
EquivalenceClasses(const EquivalenceClasses &RHS) {
@@ -223,10 +202,14 @@ class EquivalenceClasses {
223202
/// insert - Insert a new value into the union/find set, ignoring the request
224203
/// if the value already exists.
225204
const ECValue &insert(const ElemTy &Data) {
226-
auto I = TheMapping.insert(ECValue(Data));
227-
if (I.second)
228-
Members.push_back(&*I.first);
229-
return *I.first;
205+
auto I = TheMapping.insert({Data, nullptr});
206+
if (!I.second)
207+
return *I.first->second;
208+
209+
auto *ECV = new (ECValueAllocator) ECValue(Data);
210+
I.first->second = ECV;
211+
Members.push_back(ECV);
212+
return *ECV;
230213
}
231214

232215
/// findLeader - Given a value in the set, return a member iterator for the
@@ -237,7 +220,7 @@ class EquivalenceClasses {
237220
auto I = TheMapping.find(V);
238221
if (I == TheMapping.end())
239222
return member_iterator(nullptr);
240-
return findLeader(*I);
223+
return findLeader(*I->second);
241224
}
242225
member_iterator findLeader(const ECValue &ECV) const {
243226
return member_iterator(ECV.getLeader());

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Transforms/Utils/LoopUtils.h"
3434
#include <numeric>
3535
#include <queue>
36+
#include <set>
3637

3738
#define DEBUG_TYPE "vector-combine"
3839
#include "llvm/Transforms/Utils/InstructionWorklist.h"

llvm/unittests/ADT/EquivalenceClassesTest.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,4 @@ TYPED_TEST_P(ParameterizedTest, MultipleSets) {
9696
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
9797
}
9898

99-
namespace {
100-
// A dummy struct for testing EquivalenceClasses with a comparator.
101-
struct TestStruct {
102-
TestStruct(int value) : value(value) {}
103-
104-
bool operator==(const TestStruct &other) const {
105-
return value == other.value;
106-
}
107-
108-
int value;
109-
};
110-
// Comparator to be used in test case.
111-
struct TestStructComparator {
112-
bool operator()(const TestStruct &lhs, const TestStruct &rhs) const {
113-
return lhs.value < rhs.value;
114-
}
115-
};
116-
} // namespace
117-
118-
REGISTER_TYPED_TEST_SUITE_P(ParameterizedTest, MultipleSets);
119-
using ParamTypes =
120-
testing::Types<EquivalenceClasses<int>,
121-
EquivalenceClasses<TestStruct, TestStructComparator>>;
122-
INSTANTIATE_TYPED_TEST_SUITE_P(EquivalenceClassesTest, ParameterizedTest,
123-
ParamTypes, );
124-
12599
} // llvm

mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,8 @@ class OneShotAnalysisState : public AnalysisState {
224224
}
225225

226226
private:
227-
/// llvm::EquivalenceClasses wants comparable elements. This comparator uses
228-
/// pointer comparison on the defining op. This is a poor man's comparison
229-
/// but it's not like UnionFind needs ordering anyway.
230-
struct ValueComparator {
231-
bool operator()(const Value &lhs, const Value &rhs) const {
232-
return lhs.getImpl() < rhs.getImpl();
233-
}
234-
};
235-
236-
using EquivalenceClassRangeType = llvm::iterator_range<
237-
llvm::EquivalenceClasses<Value, ValueComparator>::member_iterator>;
227+
using EquivalenceClassRangeType =
228+
llvm::iterator_range<llvm::EquivalenceClasses<Value>::member_iterator>;
238229
/// Check that aliasInfo for `v` exists and return a reference to it.
239230
EquivalenceClassRangeType getAliases(Value v) const;
240231

@@ -249,15 +240,15 @@ class OneShotAnalysisState : public AnalysisState {
249240
/// value may alias with one of multiple other values. The concrete aliasing
250241
/// value may not even be known at compile time. All such values are
251242
/// considered to be aliases.
252-
llvm::EquivalenceClasses<Value, ValueComparator> aliasInfo;
243+
llvm::EquivalenceClasses<Value> aliasInfo;
253244

254245
/// Auxiliary structure to store all the equivalent buffer classes. Equivalent
255246
/// buffer information is "must be" conservative: Only if two values are
256247
/// guaranteed to be equivalent at runtime, they said to be equivalent. It is
257248
/// possible that, in the presence of branches, it cannot be determined
258249
/// statically if two values are equivalent. In that case, the values are
259250
/// considered to be not equivalent.
260-
llvm::EquivalenceClasses<Value, ValueComparator> equivalentInfo;
251+
llvm::EquivalenceClasses<Value> equivalentInfo;
261252

262253
// Bufferization statistics.
263254
int64_t statNumTensorOutOfPlace = 0;

0 commit comments

Comments
 (0)