|
| 1 | +//===- StableFunctionMap.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 | +// TODO |
| 10 | +// |
| 11 | +//===---------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_CGDATA_STABLEFUNCTIONMAP_H |
| 14 | +#define LLVM_CGDATA_STABLEFUNCTIONMAP_H |
| 15 | + |
| 16 | +#include "llvm/ADT/DenseMap.h" |
| 17 | +#include "llvm/ADT/StableHashing.h" |
| 18 | +#include "llvm/ADT/StringMap.h" |
| 19 | +#include "llvm/IR/StructuralHash.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 | +using IndexPairHash = std::pair<IndexPair, stable_hash>; |
| 29 | +using IndexOperandHashVecType = SmallVector<IndexPairHash>; |
| 30 | + |
| 31 | +/// A stable function is a function with a stable hash while tracking the |
| 32 | +/// locations of ignored operands and their hashes. |
| 33 | +struct StableFunction { |
| 34 | + /// The combined stable hash of the function. |
| 35 | + stable_hash Hash; |
| 36 | + /// The name of the function. |
| 37 | + std::string FunctionName; |
| 38 | + /// The name of the module the function is in. |
| 39 | + std::string ModuleName; |
| 40 | + /// The number of instructions. |
| 41 | + unsigned InstCount; |
| 42 | + /// A vector of pairs of IndexPair and operand hash which was skipped. |
| 43 | + IndexOperandHashVecType IndexOperandHashes; |
| 44 | + |
| 45 | + StableFunction(stable_hash Hash, const std::string FunctionName, |
| 46 | + const std::string ModuleName, unsigned InstCount, |
| 47 | + IndexOperandHashVecType &&IndexOperandHashes) |
| 48 | + : Hash(Hash), FunctionName(FunctionName), ModuleName(ModuleName), |
| 49 | + InstCount(InstCount), |
| 50 | + IndexOperandHashes(std::move(IndexOperandHashes)) {} |
| 51 | + StableFunction() = default; |
| 52 | +}; |
| 53 | + |
| 54 | +/// An efficient form of StableFunction for fast look-up |
| 55 | +struct StableFunctionEntry { |
| 56 | + /// The combined stable hash of the function. |
| 57 | + stable_hash Hash; |
| 58 | + /// Id of the function name. |
| 59 | + unsigned FunctionNameId; |
| 60 | + /// Id of the module name. |
| 61 | + unsigned ModuleNameId; |
| 62 | + /// The number of instructions. |
| 63 | + unsigned InstCount; |
| 64 | + /// A map from an IndexPair to a stable_hash which was skipped. |
| 65 | + std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap; |
| 66 | + |
| 67 | + StableFunctionEntry( |
| 68 | + stable_hash Hash, unsigned FunctionNameId, unsigned ModuleNameId, |
| 69 | + unsigned InstCount, |
| 70 | + std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap) |
| 71 | + : Hash(Hash), FunctionNameId(FunctionNameId), ModuleNameId(ModuleNameId), |
| 72 | + InstCount(InstCount), |
| 73 | + IndexOperandHashMap(std::move(IndexOperandHashMap)) {} |
| 74 | +}; |
| 75 | + |
| 76 | +using HashFuncsMapType = |
| 77 | + DenseMap<stable_hash, SmallVector<std::unique_ptr<StableFunctionEntry>>>; |
| 78 | + |
| 79 | +class StableFunctionMap { |
| 80 | + /// A map from a stable_hash to a vector of functions with that hash. |
| 81 | + HashFuncsMapType HashToFuncs; |
| 82 | + /// A vector of strings to hold names. |
| 83 | + SmallVector<std::string> IdToName; |
| 84 | + /// A map from StringRef (name) to an ID. |
| 85 | + StringMap<unsigned> NameToId; |
| 86 | + /// True if the function map is finalized with minimal content. |
| 87 | + bool Finalized = false; |
| 88 | + |
| 89 | +public: |
| 90 | + /// Get the HashToFuncs map for serialization. |
| 91 | + const HashFuncsMapType &getFunctionMap() const { return HashToFuncs; } |
| 92 | + |
| 93 | + /// Get the NameToId vector for serialization. |
| 94 | + const SmallVector<std::string> getNames() { return IdToName; } |
| 95 | + |
| 96 | + /// Get an existing ID associated with the given name or create a new ID if it |
| 97 | + /// doesn't exist. |
| 98 | + unsigned getIdOrCreateForName(StringRef Name); |
| 99 | + |
| 100 | + /// Get the name associated with a given ID |
| 101 | + std::optional<std::string> getNameForId(unsigned Id) const; |
| 102 | + |
| 103 | + /// Insert a `StableFunction` object into the function map. This method |
| 104 | + /// handles the uniquing of string names and create a `StableFunctionEntry` |
| 105 | + /// for insertion. |
| 106 | + void insert(const StableFunction &Func); |
| 107 | + |
| 108 | + /// Insert a `StableFunctionEntry` into the function map directly. This |
| 109 | + /// method assumes that string names have already been uniqued and the |
| 110 | + /// `StableFunctionEntry` is ready for insertion. |
| 111 | + void insert(std::unique_ptr<StableFunctionEntry> FuncEntry) { |
| 112 | + assert(!Finalized && "Cannot insert after finalization"); |
| 113 | + HashToFuncs[FuncEntry->Hash].emplace_back(std::move(FuncEntry)); |
| 114 | + } |
| 115 | + |
| 116 | + /// Merge a \p OtherMap into this function map. |
| 117 | + void merge(const StableFunctionMap &OtherMap); |
| 118 | + |
| 119 | + /// \returns true if there is no stable function entry. |
| 120 | + bool empty() { return size() == 0; } |
| 121 | + |
| 122 | + enum SizeType { |
| 123 | + UniqueHashCount, // The number of unique hashes in HashToFuncs. |
| 124 | + TotalFunctionCount, // The number of total functions in HashToFuncs. |
| 125 | + MergeableFunctionCount, // The number of functions that can be merged based |
| 126 | + // on their hash. |
| 127 | + }; |
| 128 | + |
| 129 | + /// \returns the size of StableFunctionMap. |
| 130 | + /// \p Type is the type of size to return. |
| 131 | + size_t size(SizeType Type = UniqueHashCount) const; |
| 132 | + |
| 133 | + /// Finalize the stable function map by trimming content. |
| 134 | + void finalize(); |
| 135 | +}; |
| 136 | + |
| 137 | +} // namespace llvm |
| 138 | + |
| 139 | +#endif |
0 commit comments