Skip to content

Commit 2c26019

Browse files
committed
[CGData] Outlined Hash Tree
This defines the OutlinedHashTree class. It contains sequences of stable hash values of instructions that have been outlined. This OutlinedHashTree can be used to track the outlined instruction sequences across modules. A trie structure is used in its implementation, allowing for a compact sharing of common prefixes.
1 parent 75c515f commit 2c26019

10 files changed

+702
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===- OutlinedHashTree.h --------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
//
9+
// This defines the OutlinedHashTree class. It contains sequences of stable
10+
// hash values of instructions that have been outlined. This OutlinedHashTree
11+
// can be used to track the outlined instruction sequences across modules.
12+
//
13+
//===---------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_CODEGENDATA_OUTLINEDHASHTREE_H
16+
#define LLVM_CODEGENDATA_OUTLINEDHASHTREE_H
17+
18+
#include "llvm/ADT/StableHashing.h"
19+
#include "llvm/ObjectYAML/YAML.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
22+
#include <unordered_map>
23+
#include <vector>
24+
25+
namespace llvm {
26+
27+
/// A HashNode is an entry in an OutlinedHashTree, holding a hash value
28+
/// and a collection of Successors (other HashNodes). If a HashNode has
29+
/// a positive terminal value (Terminals > 0), it signifies the end of
30+
/// a hash sequence with that occurrence count.
31+
struct HashNode {
32+
/// The hash value of the node.
33+
stable_hash Hash;
34+
/// The number of terminals in the sequence ending at this node.
35+
unsigned Terminals;
36+
/// The successors of this node.
37+
std::unordered_map<stable_hash, std::unique_ptr<HashNode>> Successors;
38+
};
39+
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+
48+
class OutlinedHashTree {
49+
50+
using EdgeCallbackFn =
51+
std::function<void(const HashNode *, const HashNode *)>;
52+
using NodeCallbackFn = std::function<void(const HashNode *)>;
53+
54+
using HashSequence = std::vector<stable_hash>;
55+
using HashSequencePair = std::pair<std::vector<stable_hash>, unsigned>;
56+
57+
public:
58+
/// Walks every edge and node in the OutlinedHashTree and calls CallbackEdge
59+
/// for the edges and CallbackNode for the nodes with the stable_hash for
60+
/// the source and the stable_hash of the sink for an edge. These generic
61+
/// callbacks can be used to traverse a OutlinedHashTree for the purpose of
62+
/// print debugging or serializing it.
63+
void walkGraph(NodeCallbackFn CallbackNode,
64+
EdgeCallbackFn CallbackEdge = nullptr,
65+
bool SortedWalk = false) const;
66+
67+
/// Release all hash nodes except the root hash node.
68+
void clear() {
69+
assert(getRoot()->Hash == 0 && getRoot()->Terminals == 0);
70+
getRoot()->Successors.clear();
71+
}
72+
73+
/// \returns true if the hash tree has only the root node.
74+
bool empty() { return size() == 1; }
75+
76+
/// \returns the size of a OutlinedHashTree by traversing it. If
77+
/// \p GetTerminalCountOnly is true, it only counts the terminal nodes
78+
/// (meaning it returns the the number of hash sequences in the
79+
/// OutlinedHashTree).
80+
size_t size(bool GetTerminalCountOnly = false) const;
81+
82+
/// \returns the depth of a OutlinedHashTree by traversing it.
83+
size_t depth() const;
84+
85+
/// \returns the root hash node of a OutlinedHashTree.
86+
const HashNode *getRoot() const { return Root.get(); }
87+
HashNode *getRoot() { return Root.get(); }
88+
89+
/// Inserts a \p Sequence into the this tree. The last node in the sequence
90+
/// will increase Terminals.
91+
void insert(const HashSequencePair &SequencePair);
92+
93+
/// Merge a \p OtherTree into this Tree.
94+
void merge(const OutlinedHashTree *OtherTree);
95+
96+
/// \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>(); }
100+
101+
private:
102+
std::unique_ptr<HashNode> Root;
103+
};
104+
105+
} // namespace llvm
106+
107+
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===- OutlinedHashTreeRecord.h --------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
//
9+
// This defines the OutlinedHashTreeRecord class. This class holds the outlined
10+
// hash tree for both serialization and deserialization processes. It utilizes
11+
// two data formats for serialization: raw binary data and YAML.
12+
// These two formats can be used interchangeably.
13+
//
14+
//===---------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
17+
#define LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H
18+
19+
#include "llvm/CodeGenData/OutlinedHashTree.h"
20+
21+
namespace llvm {
22+
23+
using IdHashNodeStableMapTy = std::map<unsigned, HashNodeStable>;
24+
using IdHashNodeMapTy = std::map<unsigned, HashNode *>;
25+
using HashNodeIdMapTy = std::unordered_map<const HashNode *, unsigned>;
26+
27+
struct OutlinedHashTreeRecord {
28+
std::unique_ptr<OutlinedHashTree> HashTree;
29+
30+
OutlinedHashTreeRecord() { HashTree = std::make_unique<OutlinedHashTree>(); }
31+
OutlinedHashTreeRecord(std::unique_ptr<OutlinedHashTree> HashTree)
32+
: HashTree(std::move(HashTree)){};
33+
34+
/// Serialize the outlined hash tree to a raw_ostream.
35+
void serialize(raw_ostream &OS) const;
36+
/// Deserialize the outlined hash tree from a raw_ostream.
37+
void deserialize(const unsigned char *&Ptr);
38+
/// Serialize the outlined hash tree to a YAML stream.
39+
void serializeYAML(yaml::Output &YOS) const;
40+
/// Deserialize the outlined hash tree from a YAML stream.
41+
void deserializeYAML(yaml::Input &YIS);
42+
43+
/// Merge the other outlined hash tree into this one.
44+
void merge(const OutlinedHashTreeRecord &Other) {
45+
HashTree->merge(Other.HashTree.get());
46+
}
47+
48+
/// \returns true if the outlined hash tree is empty.
49+
bool empty() const { return HashTree->empty(); }
50+
51+
/// Print the outlined hash tree in a YAML format.
52+
void print(raw_ostream &OS = llvm::errs()) const {
53+
yaml::Output YOS(OS);
54+
serializeYAML(YOS);
55+
}
56+
57+
private:
58+
/// Convert the outlined hash tree to stable data.
59+
void convertToStableData(IdHashNodeStableMapTy &IdNodeStableMap) const;
60+
61+
/// Convert the stable data back to the outlined hash tree.
62+
void convertFromStableData(const IdHashNodeStableMapTy &IdNodeStableMap);
63+
};
64+
65+
} // end namespace llvm
66+
67+
#endif // LLVM_CODEGENDATA_OUTLINEDHASHTREERECORD_H

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_subdirectory(InterfaceStub)
1010
add_subdirectory(IRPrinter)
1111
add_subdirectory(IRReader)
1212
add_subdirectory(CodeGen)
13+
add_subdirectory(CodeGenData)
1314
add_subdirectory(CodeGenTypes)
1415
add_subdirectory(BinaryFormat)
1516
add_subdirectory(Bitcode)

llvm/lib/CodeGenData/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_llvm_component_library(LLVMCodeGenData
2+
OutlinedHashTree.cpp
3+
OutlinedHashTreeRecord.cpp
4+
5+
ADDITIONAL_HEADER_DIRS
6+
${LLVM_MAIN_INCLUDE_DIR}/llvm/CodeGenData
7+
8+
DEPENDS
9+
intrinsics_gen
10+
11+
LINK_COMPONENTS
12+
Core
13+
Support
14+
)
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//===-- OutlinedHashTree.cpp ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// An OutlinedHashTree is a Trie that contains sequences of stable hash values
10+
// of instructions that have been outlined. This OutlinedHashTree can be used
11+
// to understand the outlined instruction sequences collected across modules.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm/CodeGenData/OutlinedHashTree.h"
16+
17+
#include <stack>
18+
#include <tuple>
19+
20+
#define DEBUG_TYPE "outlined-hash-tree"
21+
22+
using namespace llvm;
23+
24+
void OutlinedHashTree::walkGraph(NodeCallbackFn CallbackNode,
25+
EdgeCallbackFn CallbackEdge,
26+
bool SortedWalk) const {
27+
std::stack<const HashNode *> Stack;
28+
Stack.push(getRoot());
29+
30+
while (!Stack.empty()) {
31+
const auto *Current = Stack.top();
32+
Stack.pop();
33+
if (CallbackNode)
34+
CallbackNode(Current);
35+
36+
auto HandleNext = [&](const HashNode *Next) {
37+
if (CallbackEdge)
38+
CallbackEdge(Current, Next);
39+
Stack.push(Next);
40+
};
41+
if (SortedWalk) {
42+
std::map<stable_hash, const HashNode *> SortedSuccessors;
43+
for (const auto &P : Current->Successors)
44+
SortedSuccessors[P.first] = P.second.get();
45+
for (const auto &P : SortedSuccessors)
46+
HandleNext(P.second);
47+
} else {
48+
for (const auto &P : Current->Successors)
49+
HandleNext(P.second.get());
50+
}
51+
}
52+
}
53+
54+
size_t OutlinedHashTree::size(bool GetTerminalCountOnly) const {
55+
size_t Size = 0;
56+
walkGraph([&Size, GetTerminalCountOnly](const HashNode *N) {
57+
Size += (N && (!GetTerminalCountOnly || N->Terminals));
58+
});
59+
return Size;
60+
}
61+
62+
size_t OutlinedHashTree::depth() const {
63+
size_t Size = 0;
64+
std::unordered_map<const HashNode *, size_t> DepthMap;
65+
walkGraph([&Size, &DepthMap](
66+
const HashNode *N) { Size = std::max(Size, DepthMap[N]); },
67+
[&DepthMap](const HashNode *Src, const HashNode *Dst) {
68+
size_t Depth = DepthMap[Src];
69+
DepthMap[Dst] = Depth + 1;
70+
});
71+
return Size;
72+
}
73+
74+
void OutlinedHashTree::insert(const HashSequencePair &SequencePair) {
75+
const auto &Sequence = SequencePair.first;
76+
unsigned Count = SequencePair.second;
77+
HashNode *Current = getRoot();
78+
79+
for (stable_hash StableHash : Sequence) {
80+
auto I = Current->Successors.find(StableHash);
81+
if (I == Current->Successors.end()) {
82+
std::unique_ptr<HashNode> Next = std::make_unique<HashNode>();
83+
HashNode *NextPtr = Next.get();
84+
NextPtr->Hash = StableHash;
85+
Current->Successors.emplace(StableHash, std::move(Next));
86+
Current = NextPtr;
87+
} else
88+
Current = I->second.get();
89+
}
90+
Current->Terminals += Count;
91+
}
92+
93+
void OutlinedHashTree::merge(const OutlinedHashTree *Tree) {
94+
HashNode *Dst = getRoot();
95+
const HashNode *Src = Tree->getRoot();
96+
std::stack<std::pair<HashNode *, const HashNode *>> Stack;
97+
Stack.push({Dst, Src});
98+
99+
while (!Stack.empty()) {
100+
auto [DstNode, SrcNode] = Stack.top();
101+
Stack.pop();
102+
if (!SrcNode)
103+
continue;
104+
DstNode->Terminals += SrcNode->Terminals;
105+
106+
for (auto &[Hash, NextSrcNode] : SrcNode->Successors) {
107+
HashNode *NextDstNode;
108+
auto I = DstNode->Successors.find(Hash);
109+
if (I == DstNode->Successors.end()) {
110+
auto NextDst = std::make_unique<HashNode>();
111+
NextDstNode = NextDst.get();
112+
NextDstNode->Hash = Hash;
113+
DstNode->Successors.emplace(Hash, std::move(NextDst));
114+
} else
115+
NextDstNode = I->second.get();
116+
117+
Stack.push({NextDstNode, NextSrcNode.get()});
118+
}
119+
}
120+
}
121+
122+
unsigned OutlinedHashTree::find(const HashSequence &Sequence) const {
123+
const HashNode *Current = getRoot();
124+
for (stable_hash StableHash : Sequence) {
125+
const auto I = Current->Successors.find(StableHash);
126+
if (I == Current->Successors.end())
127+
return 0;
128+
Current = I->second.get();
129+
}
130+
return Current->Terminals;
131+
}

0 commit comments

Comments
 (0)