Skip to content

Commit 40eaed6

Browse files
kyulee-comKyungwoo Lee
authored and
Kyungwoo Lee
committed
Address comments from Ellis
1 parent 5204a70 commit 40eaed6

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

llvm/include/llvm/CodeGenData/OutlinedHashTree.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,22 @@ namespace llvm {
3030
/// a hash sequence with that occurrence count.
3131
struct HashNode {
3232
/// The hash value of the node.
33-
stable_hash Hash;
33+
stable_hash Hash = 0;
3434
/// The number of terminals in the sequence ending at this node.
35-
unsigned Terminals;
35+
std::optional<unsigned> Terminals;
3636
/// The successors of this node.
37+
/// We don't use DenseMap as a stable_hash value can be tombstone.
3738
std::unordered_map<stable_hash, std::unique_ptr<HashNode>> Successors;
3839
};
3940

40-
/// HashNodeStable is the serialized, stable, and compact representation
41-
/// of a HashNode.
42-
struct HashNodeStable {
43-
llvm::yaml::Hex64 Hash;
44-
unsigned Terminals;
45-
std::vector<unsigned> SuccessorIds;
46-
};
47-
4841
class OutlinedHashTree {
4942

5043
using EdgeCallbackFn =
5144
std::function<void(const HashNode *, const HashNode *)>;
5245
using NodeCallbackFn = std::function<void(const HashNode *)>;
5346

54-
using HashSequence = std::vector<stable_hash>;
55-
using HashSequencePair = std::pair<std::vector<stable_hash>, unsigned>;
47+
using HashSequence = SmallVector<stable_hash>;
48+
using HashSequencePair = std::pair<HashSequence, unsigned>;
5649

5750
public:
5851
/// Walks every edge and node in the OutlinedHashTree and calls CallbackEdge
@@ -66,7 +59,7 @@ class OutlinedHashTree {
6659

6760
/// Release all hash nodes except the root hash node.
6861
void clear() {
69-
assert(getRoot()->Hash == 0 && getRoot()->Terminals == 0);
62+
assert(getRoot()->Hash == 0 && !getRoot()->Terminals);
7063
getRoot()->Successors.clear();
7164
}
7265

@@ -83,8 +76,8 @@ class OutlinedHashTree {
8376
size_t depth() const;
8477

8578
/// \returns the root hash node of a OutlinedHashTree.
86-
const HashNode *getRoot() const { return Root.get(); }
87-
HashNode *getRoot() { return Root.get(); }
79+
const HashNode *getRoot() const { return &Root; }
80+
HashNode *getRoot() { return &Root; }
8881

8982
/// Inserts a \p Sequence into the this tree. The last node in the sequence
9083
/// will increase Terminals.
@@ -94,12 +87,10 @@ class OutlinedHashTree {
9487
void merge(const OutlinedHashTree *OtherTree);
9588

9689
/// \returns the matching count if \p Sequence exists in the OutlinedHashTree.
97-
unsigned find(const HashSequence &Sequence) const;
98-
99-
OutlinedHashTree() { Root = std::make_unique<HashNode>(); }
90+
std::optional<unsigned> find(const HashSequence &Sequence) const;
10091

10192
private:
102-
std::unique_ptr<HashNode> Root;
93+
HashNode Root;
10394
};
10495

10596
} // namespace llvm

llvm/include/llvm/CodeGenData/OutlinedHashTreeRecord.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@
1616
#ifndef LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
1717
#define LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
1818

19+
#include "llvm/ADT/DenseMap.h"
1920
#include "llvm/CodeGenData/OutlinedHashTree.h"
2021

2122
namespace llvm {
2223

24+
/// HashNodeStable is the serialized, stable, and compact representation
25+
/// of a HashNode.
26+
struct HashNodeStable {
27+
llvm::yaml::Hex64 Hash;
28+
unsigned Terminals;
29+
std::vector<unsigned> SuccessorIds;
30+
};
31+
2332
using IdHashNodeStableMapTy = std::map<unsigned, HashNodeStable>;
24-
using IdHashNodeMapTy = std::map<unsigned, HashNode *>;
25-
using HashNodeIdMapTy = std::unordered_map<const HashNode *, unsigned>;
33+
using IdHashNodeMapTy = DenseMap<unsigned, HashNode *>;
34+
using HashNodeIdMapTy = DenseMap<const HashNode *, unsigned>;
2635

2736
struct OutlinedHashTreeRecord {
2837
std::unique_ptr<OutlinedHashTree> HashTree;

llvm/lib/CodeGenData/OutlinedHashTree.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ using namespace llvm;
2424
void OutlinedHashTree::walkGraph(NodeCallbackFn CallbackNode,
2525
EdgeCallbackFn CallbackEdge,
2626
bool SortedWalk) const {
27-
std::stack<const HashNode *> Stack;
28-
Stack.push(getRoot());
27+
SmallVector<const HashNode *> Stack;
28+
Stack.emplace_back(getRoot());
2929

3030
while (!Stack.empty()) {
31-
const auto *Current = Stack.top();
32-
Stack.pop();
31+
const auto *Current = Stack.pop_back_val();
3332
if (CallbackNode)
3433
CallbackNode(Current);
3534

3635
auto HandleNext = [&](const HashNode *Next) {
3736
if (CallbackEdge)
3837
CallbackEdge(Current, Next);
39-
Stack.push(Next);
38+
Stack.emplace_back(Next);
4039
};
4140
if (SortedWalk) {
4241
std::map<stable_hash, const HashNode *> SortedSuccessors;
@@ -72,8 +71,7 @@ size_t OutlinedHashTree::depth() const {
7271
}
7372

7473
void OutlinedHashTree::insert(const HashSequencePair &SequencePair) {
75-
const auto &Sequence = SequencePair.first;
76-
unsigned Count = SequencePair.second;
74+
auto &[Sequence, Count] = SequencePair;
7775
HashNode *Current = getRoot();
7876

7977
for (stable_hash StableHash : Sequence) {
@@ -87,22 +85,23 @@ void OutlinedHashTree::insert(const HashSequencePair &SequencePair) {
8785
} else
8886
Current = I->second.get();
8987
}
90-
Current->Terminals += Count;
88+
if (Count)
89+
Current->Terminals = (Current->Terminals ? *Current->Terminals : 0) + Count;
9190
}
9291

9392
void OutlinedHashTree::merge(const OutlinedHashTree *Tree) {
9493
HashNode *Dst = getRoot();
9594
const HashNode *Src = Tree->getRoot();
96-
std::stack<std::pair<HashNode *, const HashNode *>> Stack;
97-
Stack.push({Dst, Src});
95+
SmallVector<std::pair<HashNode *, const HashNode *>> Stack;
96+
Stack.emplace_back(Dst, Src);
9897

9998
while (!Stack.empty()) {
100-
auto [DstNode, SrcNode] = Stack.top();
101-
Stack.pop();
99+
auto [DstNode, SrcNode] = Stack.pop_back_val();
102100
if (!SrcNode)
103101
continue;
104-
DstNode->Terminals += SrcNode->Terminals;
105-
102+
if (SrcNode->Terminals)
103+
DstNode->Terminals =
104+
(DstNode->Terminals ? *DstNode->Terminals : 0) + *SrcNode->Terminals;
106105
for (auto &[Hash, NextSrcNode] : SrcNode->Successors) {
107106
HashNode *NextDstNode;
108107
auto I = DstNode->Successors.find(Hash);
@@ -114,12 +113,13 @@ void OutlinedHashTree::merge(const OutlinedHashTree *Tree) {
114113
} else
115114
NextDstNode = I->second.get();
116115

117-
Stack.push({NextDstNode, NextSrcNode.get()});
116+
Stack.emplace_back(NextDstNode, NextSrcNode.get());
118117
}
119118
}
120119
}
121120

122-
unsigned OutlinedHashTree::find(const HashSequence &Sequence) const {
121+
std::optional<unsigned>
122+
OutlinedHashTree::find(const HashSequence &Sequence) const {
123123
const HashNode *Current = getRoot();
124124
for (stable_hash StableHash : Sequence) {
125125
const auto I = Current->Successors.find(StableHash);

llvm/lib/CodeGenData/OutlinedHashTreeRecord.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ void OutlinedHashTreeRecord::convertToStableData(
131131
auto Id = P.second;
132132
HashNodeStable NodeStable;
133133
NodeStable.Hash = Node->Hash;
134-
NodeStable.Terminals = Node->Terminals;
134+
NodeStable.Terminals = Node->Terminals ? *Node->Terminals : 0;
135135
for (auto &P : Node->Successors)
136136
NodeStable.SuccessorIds.push_back(NodeIdMap[P.second.get()]);
137137
IdNodeStableMap[Id] = NodeStable;
138138
}
139139

140140
// Sort the Successors so that they come out in the same order as in the map.
141141
for (auto &P : IdNodeStableMap)
142-
std::sort(P.second.SuccessorIds.begin(), P.second.SuccessorIds.end());
142+
llvm::sort(P.second.SuccessorIds);
143143
}
144144

145145
void OutlinedHashTreeRecord::convertFromStableData(
@@ -155,7 +155,8 @@ void OutlinedHashTreeRecord::convertFromStableData(
155155
assert(IdNodeMap.count(Id));
156156
HashNode *Curr = IdNodeMap[Id];
157157
Curr->Hash = NodeStable.Hash;
158-
Curr->Terminals = NodeStable.Terminals;
158+
if (NodeStable.Terminals)
159+
Curr->Terminals = NodeStable.Terminals;
159160
auto &Successors = Curr->Successors;
160161
assert(Successors.empty());
161162
for (auto SuccessorId : NodeStable.SuccessorIds) {

llvm/unittests/CodeGenData/OutlinedHashTreeTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ TEST(OutlinedHashTreeTest, Find) {
5050
// The node count does not change as the same sequences are added.
5151
ASSERT_TRUE(HashTree.size() == 4);
5252
// The terminal counts are accumulated from two same sequences.
53-
ASSERT_TRUE(HashTree.find({1, 2, 3}) == 3);
54-
ASSERT_TRUE(HashTree.find({1, 2}) == 0);
53+
ASSERT_TRUE(HashTree.find({1, 2, 3}));
54+
ASSERT_TRUE(HashTree.find({1, 2, 3}).value() == 3);
55+
ASSERT_FALSE(HashTree.find({1, 2}));
5556
}
5657

5758
TEST(OutlinedHashTreeTest, Merge) {

0 commit comments

Comments
 (0)