Skip to content

Commit 5c13864

Browse files
committed
Use Map
1 parent b0c2ac6 commit 5c13864

File tree

4 files changed

+20
-154
lines changed

4 files changed

+20
-154
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 & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -13,113 +13,4 @@ using namespace llvm;
1313

1414
namespace llvm {
1515

16-
TEST(EquivalenceClassesTest, CopyAssignemnt) {
17-
EquivalenceClasses<int> EC, Copy;
18-
EC.insert(1);
19-
EC.insert(4);
20-
EquivalenceClasses<int> &Ref = Copy = EC;
21-
EXPECT_EQ(Copy.getNumClasses(), 2u);
22-
EXPECT_EQ(&Ref, &Copy);
23-
}
24-
25-
TEST(EquivalenceClassesTest, NoMerges) {
26-
EquivalenceClasses<int> EqClasses;
27-
// Until we merged any sets, check that every element is only equivalent to
28-
// itself.
29-
for (int i = 0; i < 3; i++)
30-
for (int j = 0; j < 3; j++)
31-
if (i == j)
32-
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
33-
else
34-
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
35-
}
36-
37-
TEST(EquivalenceClassesTest, SimpleMerge1) {
38-
EquivalenceClasses<int> EqClasses;
39-
// Check that once we merge (A, B), (B, C), (C, D), then all elements belong
40-
// to one set.
41-
EqClasses.unionSets(0, 1);
42-
EqClasses.unionSets(1, 2);
43-
EqClasses.unionSets(2, 3);
44-
for (int i = 0; i < 4; ++i)
45-
for (int j = 0; j < 4; ++j)
46-
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
47-
}
48-
49-
TEST(EquivalenceClassesTest, SimpleMerge2) {
50-
EquivalenceClasses<int> EqClasses;
51-
// Check that once we merge (A, B), (C, D), (A, C), then all elements belong
52-
// to one set.
53-
EqClasses.unionSets(0, 1);
54-
EqClasses.unionSets(2, 3);
55-
EqClasses.unionSets(0, 2);
56-
for (int i = 0; i < 4; ++i)
57-
for (int j = 0; j < 4; ++j)
58-
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
59-
}
60-
61-
TEST(EquivalenceClassesTest, TwoSets) {
62-
EquivalenceClasses<int> EqClasses;
63-
// Form sets of odd and even numbers, check that we split them into these
64-
// two sets correcrly.
65-
for (int i = 0; i < 30; i += 2)
66-
EqClasses.unionSets(0, i);
67-
for (int i = 1; i < 30; i += 2)
68-
EqClasses.unionSets(1, i);
69-
70-
for (int i = 0; i < 30; i++)
71-
for (int j = 0; j < 30; j++)
72-
if (i % 2 == j % 2)
73-
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
74-
else
75-
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
76-
}
77-
78-
// Type-parameterized tests: Run the same test cases with different element
79-
// types.
80-
template <typename T> class ParameterizedTest : public testing::Test {};
81-
82-
TYPED_TEST_SUITE_P(ParameterizedTest);
83-
84-
TYPED_TEST_P(ParameterizedTest, MultipleSets) {
85-
TypeParam EqClasses;
86-
// Split numbers from [0, 100) into sets so that values in the same set have
87-
// equal remainders (mod 17).
88-
for (int i = 0; i < 100; i++)
89-
EqClasses.unionSets(i % 17, i);
90-
91-
for (int i = 0; i < 100; i++)
92-
for (int j = 0; j < 100; j++)
93-
if (i % 17 == j % 17)
94-
EXPECT_TRUE(EqClasses.isEquivalent(i, j));
95-
else
96-
EXPECT_FALSE(EqClasses.isEquivalent(i, j));
97-
}
98-
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-
12516
} // 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)