LLVM 22.0.0git
StableFunctionMap.h
Go to the documentation of this file.
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"
24#include <mutex>
25#include <unordered_map>
26
27namespace llvm {
28
29using IndexPairHash = std::pair<IndexPair, stable_hash>;
31
32/// A stable function is a function with a stable hash while tracking the
33/// locations of ignored operands and their hashes.
35 /// The combined stable hash of the function.
37 /// The name of the function.
38 std::string FunctionName;
39 /// The name of the module the function is in.
40 std::string ModuleName;
41 /// The number of instructions.
42 unsigned InstCount;
43 /// A vector of pairs of IndexPair and operand hash which was skipped.
45
52 StableFunction() = default;
53};
54
56 /// An efficient form of StableFunction for fast look-up
58 /// The combined stable hash of the function.
60 /// Id of the function name.
62 /// Id of the module name.
63 unsigned ModuleNameId;
64 /// The number of instructions.
65 unsigned InstCount;
66 /// A map from an IndexPair to a stable_hash which was skipped.
67 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap;
68
76 };
77
80
81 /// In addition to the deserialized StableFunctionEntry, the struct stores
82 /// the offsets of corresponding serialized stable function entries, and a
83 /// once flag for safe lazy loading in a multithreaded environment.
84 struct EntryStorage {
85 /// The actual storage of deserialized stable function entries. If the map
86 /// is lazily loaded, this will be empty until the first access by the
87 /// corresponding function hash.
89
90 private:
91 /// This is used to deserialize the entry lazily. Each element is the
92 /// corresponding serialized stable function entry's offset in the memory
93 /// buffer (StableFunctionMap::Buffer).
94 /// The offsets are only populated when loading the map lazily, otherwise
95 /// it is empty.
97 std::once_flag LazyLoadFlag;
98 friend struct StableFunctionMap;
100 };
101
102 // Note: DenseMap requires value type to be copyable even if only using
103 // in-place insertion. Use STL instead. This also affects the
104 // deletion-while-iteration in finalize().
105 using HashFuncsMapType = std::unordered_map<stable_hash, EntryStorage>;
106
107 /// Get the HashToFuncs map for serialization.
108 const HashFuncsMapType &getFunctionMap() const;
109
110 /// Get the NameToId vector for serialization.
111 ArrayRef<std::string> getNames() const { return IdToName; }
112
113 /// Get an existing ID associated with the given name or create a new ID if it
114 /// doesn't exist.
116
117 /// Get the name associated with a given ID
118 LLVM_ABI std::optional<std::string> getNameForId(unsigned Id) const;
119
120 /// Insert a `StableFunction` object into the function map. This method
121 /// handles the uniquing of string names and create a `StableFunctionEntry`
122 /// for insertion.
123 LLVM_ABI void insert(const StableFunction &Func);
124
125 /// Merge a \p OtherMap into this function map.
126 LLVM_ABI void merge(const StableFunctionMap &OtherMap);
127
128 /// \returns true if there is no stable function entry.
129 bool empty() const { return size() == 0; }
130
131 /// \returns true if there is an entry for the given function hash.
132 /// This does not trigger lazy loading.
133 bool contains(HashFuncsMapType::key_type FunctionHash) const {
134 return HashToFuncs.count(FunctionHash) > 0;
135 }
136
137 /// \returns the stable function entries for the given function hash. If the
138 /// map is lazily loaded, it will deserialize the entries if it is not already
139 /// done, other requests to the same hash at the same time will be blocked
140 /// until the entries are deserialized.
142 at(HashFuncsMapType::key_type FunctionHash) const;
143
144 enum SizeType {
145 UniqueHashCount, // The number of unique hashes in HashToFuncs.
146 TotalFunctionCount, // The number of total functions in HashToFuncs.
147 MergeableFunctionCount, // The number of functions that can be merged based
148 // on their hash.
149 };
150
151 /// \returns the size of StableFunctionMap.
152 /// \p Type is the type of size to return.
153 LLVM_ABI size_t size(SizeType Type = UniqueHashCount) const;
154
155 /// Finalize the stable function map by trimming content.
156 LLVM_ABI void finalize(bool SkipTrim = false);
157
158private:
159 /// Insert a `StableFunctionEntry` into the function map directly. This
160 /// method assumes that string names have already been uniqued and the
161 /// `StableFunctionEntry` is ready for insertion.
162 void insert(std::unique_ptr<StableFunctionEntry> FuncEntry) {
163 assert(!Finalized && "Cannot insert after finalization");
164 HashToFuncs[FuncEntry->Hash].Entries.emplace_back(std::move(FuncEntry));
165 }
166
167 void deserializeLazyLoadingEntry(HashFuncsMapType::iterator It) const;
168
169 /// Eagerly deserialize all the unloaded entries in the lazy loading map.
170 void deserializeLazyLoadingEntries() const;
171
172 bool isLazilyLoaded() const { return (bool)Buffer; }
173
174 /// A map from a stable_hash to a vector of functions with that hash.
175 mutable HashFuncsMapType HashToFuncs;
176 /// A vector of strings to hold names.
177 SmallVector<std::string> IdToName;
178 /// A map from StringRef (name) to an ID.
179 StringMap<unsigned> NameToId;
180 /// True if the function map is finalized with minimal content.
181 bool Finalized = false;
182 /// The memory buffer that contains the serialized stable function map for
183 /// lazy loading.
184 /// Non-empty only if this StableFunctionMap is created from a MemoryBuffer
185 /// (i.e. by IndexedCodeGenDataReader::read()) and lazily deserialized.
186 std::shared_ptr<MemoryBuffer> Buffer;
187 /// Whether to read stable function names from the buffer.
188 bool ReadStableFunctionMapNames = true;
189
191};
192
193} // namespace llvm
194
195#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
arc branch finalize
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
Assignment Tracking (at).
Definition DebugInfo.h:179
This is an optimization pass for GlobalISel generic memory operations.
uint64_t stable_hash
An opaque object representing a stable hash code.
SmallVector< IndexPairHash > IndexOperandHashVecType
std::pair< IndexPair, stable_hash > IndexPairHash
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
In addition to the deserialized StableFunctionEntry, the struct stores the offsets of corresponding s...
StableFunctionEntries Entries
The actual storage of deserialized stable function entries.
StableFunctionEntry(stable_hash Hash, unsigned FunctionNameId, unsigned ModuleNameId, unsigned InstCount, std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap)
stable_hash Hash
The combined stable hash of the function.
std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap
A map from an IndexPair to a stable_hash which was skipped.
unsigned InstCount
The number of instructions.
unsigned FunctionNameId
Id of the function name.
unsigned ModuleNameId
Id of the module name.
std::unordered_map< stable_hash, EntryStorage > HashFuncsMapType
SmallVector< std::unique_ptr< StableFunctionEntry > > StableFunctionEntries
LLVM_ABI size_t size(SizeType Type=UniqueHashCount) const
LLVM_ABI void insert(const StableFunction &Func)
Insert a StableFunction object into the function map.
ArrayRef< std::string > getNames() const
Get the NameToId vector for serialization.
LLVM_ABI void merge(const StableFunctionMap &OtherMap)
Merge a OtherMap into this function map.
LLVM_ABI std::optional< std::string > getNameForId(unsigned Id) const
Get the name associated with a given ID.
const HashFuncsMapType & getFunctionMap() const
Get the HashToFuncs map for serialization.
LLVM_ABI unsigned getIdOrCreateForName(StringRef Name)
Get an existing ID associated with the given name or create a new ID if it doesn't exist.
friend struct StableFunctionMapRecord
bool contains(HashFuncsMapType::key_type FunctionHash) const
A stable function is a function with a stable hash while tracking the locations of ignored operands a...
unsigned InstCount
The number of instructions.
StableFunction()=default
StableFunction(stable_hash Hash, const std::string FunctionName, const std::string ModuleName, unsigned InstCount, IndexOperandHashVecType &&IndexOperandHashes)
stable_hash Hash
The combined stable hash of the function.
IndexOperandHashVecType IndexOperandHashes
A vector of pairs of IndexPair and operand hash which was skipped.
std::string FunctionName
The name of the function.
std::string ModuleName
The name of the module the function is in.