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