Skip to content

Commit d00f1c1

Browse files
authored
[CGData] Outlined Hash Tree (#89792)
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. This is a patch for https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
1 parent 75bc20f commit d00f1c1

File tree

10 files changed

+701
-0
lines changed

10 files changed

+701
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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/DenseMap.h"
19+
#include "llvm/ADT/StableHashing.h"
20+
#include "llvm/ObjectYAML/YAML.h"
21+
#include "llvm/Support/raw_ostream.h"
22+
23+
#include <unordered_map>
24+
#include <vector>
25+
26+
namespace llvm {
27+
28+
/// A HashNode is an entry in an OutlinedHashTree, holding a hash value
29+
/// and a collection of Successors (other HashNodes). If a HashNode has
30+
/// a positive terminal value (Terminals > 0), it signifies the end of
31+
/// a hash sequence with that occurrence count.
32+
struct HashNode {
33+
/// The hash value of the node.
34+
stable_hash Hash = 0;
35+
/// The number of terminals in the sequence ending at this node.
36+
std::optional<unsigned> Terminals;
37+
/// The successors of this node.
38+
/// We don't use DenseMap as a stable_hash value can be tombstone.
39+
std::unordered_map<stable_hash, std::unique_ptr<HashNode>> Successors;
40+
};
41+
42+
class OutlinedHashTree {
43+
44+
using EdgeCallbackFn =
45+
std::function<void(const HashNode *, const HashNode *)>;
46+
using NodeCallbackFn = std::function<void(const HashNode *)>;
47+
48+
using HashSequence = SmallVector<stable_hash>;
49+
using HashSequencePair = std::pair<HashSequence, unsigned>;
50+
51+
public:
52+
/// Walks every edge and node in the OutlinedHashTree and calls CallbackEdge
53+
/// for the edges and CallbackNode for the nodes with the stable_hash for
54+
/// the source and the stable_hash of the sink for an edge. These generic
55+
/// callbacks can be used to traverse a OutlinedHashTree for the purpose of
56+
/// print debugging or serializing it.
57+
void walkGraph(NodeCallbackFn CallbackNode,
58+
EdgeCallbackFn CallbackEdge = nullptr,
59+
bool SortedWalk = false) const;
60+
61+
/// Release all hash nodes except the root hash node.
62+
void clear() {
63+
assert(getRoot()->Hash == 0 && !getRoot()->Terminals);
64+
getRoot()->Successors.clear();
65+
}
66+
67+
/// \returns true if the hash tree has only the root node.
68+
bool empty() { return size() == 1; }
69+
70+
/// \returns the size of a OutlinedHashTree by traversing it. If
71+
/// \p GetTerminalCountOnly is true, it only counts the terminal nodes
72+
/// (meaning it returns the the number of hash sequences in the
73+
/// OutlinedHashTree).
74+
size_t size(bool GetTerminalCountOnly = false) const;
75+
76+
/// \returns the depth of a OutlinedHashTree by traversing it.
77+
size_t depth() const;
78+
79+
/// \returns the root hash node of a OutlinedHashTree.
80+
const HashNode *getRoot() const { return &Root; }
81+
HashNode *getRoot() { return &Root; }
82+
83+
/// Inserts a \p Sequence into the this tree. The last node in the sequence
84+
/// will increase Terminals.
85+
void insert(const HashSequencePair &SequencePair);
86+
87+
/// Merge a \p OtherTree into this Tree.
88+
void merge(const OutlinedHashTree *OtherTree);
89+
90+
/// \returns the matching count if \p Sequence exists in the OutlinedHashTree.
91+
std::optional<unsigned> find(const HashSequence &Sequence) const;
92+
93+
private:
94+
HashNode Root;
95+
};
96+
97+
} // namespace llvm
98+
99+
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
/// HashNodeStable is the serialized, stable, and compact representation
24+
/// of a HashNode.
25+
struct HashNodeStable {
26+
llvm::yaml::Hex64 Hash;
27+
unsigned Terminals;
28+
std::vector<unsigned> SuccessorIds;
29+
};
30+
31+
using IdHashNodeStableMapTy = std::map<unsigned, HashNodeStable>;
32+
using IdHashNodeMapTy = DenseMap<unsigned, HashNode *>;
33+
using HashNodeIdMapTy = DenseMap<const HashNode *, unsigned>;
34+
35+
struct OutlinedHashTreeRecord {
36+
std::unique_ptr<OutlinedHashTree> HashTree;
37+
38+
OutlinedHashTreeRecord() { HashTree = std::make_unique<OutlinedHashTree>(); }
39+
OutlinedHashTreeRecord(std::unique_ptr<OutlinedHashTree> HashTree)
40+
: HashTree(std::move(HashTree)) {};
41+
42+
/// Serialize the outlined hash tree to a raw_ostream.
43+
void serialize(raw_ostream &OS) const;
44+
/// Deserialize the outlined hash tree from a raw_ostream.
45+
void deserialize(const unsigned char *&Ptr);
46+
/// Serialize the outlined hash tree to a YAML stream.
47+
void serializeYAML(yaml::Output &YOS) const;
48+
/// Deserialize the outlined hash tree from a YAML stream.
49+
void deserializeYAML(yaml::Input &YIS);
50+
51+
/// Merge the other outlined hash tree into this one.
52+
void merge(const OutlinedHashTreeRecord &Other) {
53+
HashTree->merge(Other.HashTree.get());
54+
}
55+
56+
/// \returns true if the outlined hash tree is empty.
57+
bool empty() const { return HashTree->empty(); }
58+
59+
/// Print the outlined hash tree in a YAML format.
60+
void print(raw_ostream &OS = llvm::errs()) const {
61+
yaml::Output YOS(OS);
62+
serializeYAML(YOS);
63+
}
64+
65+
private:
66+
/// Convert the outlined hash tree to stable data.
67+
void convertToStableData(IdHashNodeStableMapTy &IdNodeStableMap) const;
68+
69+
/// Convert the stable data back to the outlined hash tree.
70+
void convertFromStableData(const IdHashNodeStableMapTy &IdNodeStableMap);
71+
};
72+
73+
} // end namespace llvm
74+
75+
#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: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
#define DEBUG_TYPE "outlined-hash-tree"
18+
19+
using namespace llvm;
20+
21+
void OutlinedHashTree::walkGraph(NodeCallbackFn CallbackNode,
22+
EdgeCallbackFn CallbackEdge,
23+
bool SortedWalk) const {
24+
SmallVector<const HashNode *> Stack;
25+
Stack.emplace_back(getRoot());
26+
27+
while (!Stack.empty()) {
28+
const auto *Current = Stack.pop_back_val();
29+
if (CallbackNode)
30+
CallbackNode(Current);
31+
32+
auto HandleNext = [&](const HashNode *Next) {
33+
if (CallbackEdge)
34+
CallbackEdge(Current, Next);
35+
Stack.emplace_back(Next);
36+
};
37+
if (SortedWalk) {
38+
SmallVector<std::pair<stable_hash, const HashNode *>> SortedSuccessors;
39+
for (const auto &[Hash, Successor] : Current->Successors)
40+
SortedSuccessors.emplace_back(Hash, Successor.get());
41+
llvm::sort(SortedSuccessors);
42+
for (const auto &P : SortedSuccessors)
43+
HandleNext(P.second);
44+
} else {
45+
for (const auto &P : Current->Successors)
46+
HandleNext(P.second.get());
47+
}
48+
}
49+
}
50+
51+
size_t OutlinedHashTree::size(bool GetTerminalCountOnly) const {
52+
size_t Size = 0;
53+
walkGraph([&Size, GetTerminalCountOnly](const HashNode *N) {
54+
Size += (N && (!GetTerminalCountOnly || N->Terminals));
55+
});
56+
return Size;
57+
}
58+
59+
size_t OutlinedHashTree::depth() const {
60+
size_t Size = 0;
61+
DenseMap<const HashNode *, size_t> DepthMap;
62+
walkGraph([&Size, &DepthMap](
63+
const HashNode *N) { Size = std::max(Size, DepthMap[N]); },
64+
[&DepthMap](const HashNode *Src, const HashNode *Dst) {
65+
size_t Depth = DepthMap[Src];
66+
DepthMap[Dst] = Depth + 1;
67+
});
68+
return Size;
69+
}
70+
71+
void OutlinedHashTree::insert(const HashSequencePair &SequencePair) {
72+
auto &[Sequence, Count] = SequencePair;
73+
HashNode *Current = getRoot();
74+
75+
for (stable_hash StableHash : Sequence) {
76+
auto I = Current->Successors.find(StableHash);
77+
if (I == Current->Successors.end()) {
78+
std::unique_ptr<HashNode> Next = std::make_unique<HashNode>();
79+
HashNode *NextPtr = Next.get();
80+
NextPtr->Hash = StableHash;
81+
Current->Successors.emplace(StableHash, std::move(Next));
82+
Current = NextPtr;
83+
} else
84+
Current = I->second.get();
85+
}
86+
if (Count)
87+
Current->Terminals = (Current->Terminals ? *Current->Terminals : 0) + Count;
88+
}
89+
90+
void OutlinedHashTree::merge(const OutlinedHashTree *Tree) {
91+
HashNode *Dst = getRoot();
92+
const HashNode *Src = Tree->getRoot();
93+
SmallVector<std::pair<HashNode *, const HashNode *>> Stack;
94+
Stack.emplace_back(Dst, Src);
95+
96+
while (!Stack.empty()) {
97+
auto [DstNode, SrcNode] = Stack.pop_back_val();
98+
if (!SrcNode)
99+
continue;
100+
if (SrcNode->Terminals)
101+
DstNode->Terminals =
102+
(DstNode->Terminals ? *DstNode->Terminals : 0) + *SrcNode->Terminals;
103+
for (auto &[Hash, NextSrcNode] : SrcNode->Successors) {
104+
HashNode *NextDstNode;
105+
auto I = DstNode->Successors.find(Hash);
106+
if (I == DstNode->Successors.end()) {
107+
auto NextDst = std::make_unique<HashNode>();
108+
NextDstNode = NextDst.get();
109+
NextDstNode->Hash = Hash;
110+
DstNode->Successors.emplace(Hash, std::move(NextDst));
111+
} else
112+
NextDstNode = I->second.get();
113+
114+
Stack.emplace_back(NextDstNode, NextSrcNode.get());
115+
}
116+
}
117+
}
118+
119+
std::optional<unsigned>
120+
OutlinedHashTree::find(const HashSequence &Sequence) const {
121+
const HashNode *Current = getRoot();
122+
for (stable_hash StableHash : Sequence) {
123+
const auto I = Current->Successors.find(StableHash);
124+
if (I == Current->Successors.end())
125+
return 0;
126+
Current = I->second.get();
127+
}
128+
return Current->Terminals;
129+
}

0 commit comments

Comments
 (0)