|
| 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 | + /// Walks every edge and node in the OutlinedHashTree and calls CallbackEdge |
| 58 | + /// for the edges and CallbackNode for the nodes with the stable_hash for |
| 59 | + /// the source and the stable_hash of the sink for an edge. These generic |
| 60 | + /// callbacks can be used to traverse a OutlinedHashTree for the purpose of |
| 61 | + /// print debugging or serializing it. |
| 62 | + void walkGraph(EdgeCallbackFn CallbackEdge, |
| 63 | + NodeCallbackFn CallbackNode) const; |
| 64 | + |
| 65 | +public: |
| 66 | + /// Walks the nodes of a OutlinedHashTree using walkGraph. |
| 67 | + void walkVertices(NodeCallbackFn Callback) const { |
| 68 | + walkGraph([](const HashNode *A, const HashNode *B) {}, Callback); |
| 69 | + } |
| 70 | + |
| 71 | + /// Release all hash nodes except the root hash node. |
| 72 | + void clear() { |
| 73 | + assert(getRoot()->Hash == 0 && getRoot()->Terminals == 0); |
| 74 | + getRoot()->Successors.clear(); |
| 75 | + } |
| 76 | + |
| 77 | + /// \returns true if the hash tree has only the root node. |
| 78 | + bool empty() { return size() == 1; } |
| 79 | + |
| 80 | + /// \returns the size of a OutlinedHashTree by traversing it. If |
| 81 | + /// \p GetTerminalCountOnly is true, it only counts the terminal nodes |
| 82 | + /// (meaning it returns the size of the number of hash sequences in a |
| 83 | + /// OutlinedHashTree). |
| 84 | + size_t size(bool GetTerminalCountOnly = false) const { |
| 85 | + size_t Size = 0; |
| 86 | + walkVertices([&Size, GetTerminalCountOnly](const HashNode *N) { |
| 87 | + Size += (N && (!GetTerminalCountOnly || N->Terminals)); |
| 88 | + }); |
| 89 | + return Size; |
| 90 | + } |
| 91 | + |
| 92 | + /// \returns the depth of a OutlinedHashTree by traversing it. |
| 93 | + size_t depth() const { |
| 94 | + size_t Size = 0; |
| 95 | + std::unordered_map<const HashNode *, size_t> DepthMap; |
| 96 | + |
| 97 | + walkGraph( |
| 98 | + [&DepthMap](const HashNode *Src, const HashNode *Dst) { |
| 99 | + size_t Depth = DepthMap[Src]; |
| 100 | + DepthMap[Dst] = Depth + 1; |
| 101 | + }, |
| 102 | + [&Size, &DepthMap](const HashNode *N) { |
| 103 | + Size = std::max(Size, DepthMap[N]); |
| 104 | + }); |
| 105 | + |
| 106 | + return Size; |
| 107 | + } |
| 108 | + |
| 109 | + /// \returns the root hash node of a OutlinedHashTree. |
| 110 | + const HashNode *getRoot() const { return Root.get(); } |
| 111 | + HashNode *getRoot() { return Root.get(); } |
| 112 | + |
| 113 | + /// Inserts a \p Sequence into the this tree. The last node in the sequence |
| 114 | + /// will increase Terminals. |
| 115 | + void insert(const HashSequencePair &SequencePair); |
| 116 | + |
| 117 | + /// Merge a \p OtherTree into this Tree. |
| 118 | + void merge(const OutlinedHashTree *OtherTree); |
| 119 | + |
| 120 | + /// \returns the matching count if \p Sequence exists in a OutlinedHashTree. |
| 121 | + unsigned find(const HashSequence &Sequence) const; |
| 122 | + |
| 123 | + OutlinedHashTree() { Root = std::make_unique<HashNode>(); } |
| 124 | + |
| 125 | +private: |
| 126 | + std::unique_ptr<HashNode> Root; |
| 127 | +}; |
| 128 | + |
| 129 | +} // namespace llvm |
| 130 | + |
| 131 | +#endif |
0 commit comments