LLVM 24.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
29namespace cl {
31} // namespace cl
32
33using IndexPairHash = std::pair<IndexPair, stable_hash>;
35
36/// A stable function is a function with a stable hash while tracking the
37/// locations of ignored operands and their hashes.
39 /// The combined stable hash of the function.
41 /// The name of the function.
42 std::string FunctionName;
43 /// The name of the module the function is in.
44 std::string ModuleName;
45 /// The number of instructions.
46 unsigned InstCount;
47 /// A vector of pairs of IndexPair and operand hash which was skipped.
49
56 StableFunction() = default;
57};
58
60 /// An efficient form of StableFunction for fast look-up
62 /// The combined stable hash of the function.
64 /// Id of the function name.
66 /// Id of the module name.
67 unsigned ModuleNameId;
68 /// The number of instructions.
69 unsigned InstCount;
70 /// A map from an IndexPair to a stable_hash which was skipped.
71 std::unique_ptr<IndexOperandHashMapType> IndexOperandHashMap;
72
80 };
81
84
85 /// In addition to the deserialized StableFunctionEntry, the struct stores
86 /// the offsets of corresponding serialized stable function entries, and a
87 /// once flag for safe lazy loading in a multithreaded environment.
88 struct EntryStorage {
89 /// The actual storage of deserialized stable function entries. If the map
90 /// is lazily loaded, this will be empty until the first access by the
91 /// corresponding function hash.
93
94 private:
95 /// This is used to deserialize the entry lazily. Each element is the
96 /// corresponding serialized stable function entry's offset in the memory
97 /// buffer (StableFunctionMap::Buffer).
98 /// The offsets are only populated when loading the map lazily, otherwise
99 /// it is empty.
100 SmallVector<uint64_t> Offsets;
101 std::once_flag LazyLoadFlag;
102 friend struct StableFunctionMap;
104 };
105
106 // Note: EntryStorage contains a std::once_flag, which is neither copyable
107 // nor movable, and the lazy-loading design relies on value addresses being
108 // stable. DenseMap relocates values on rehash, so use std::unordered_map.
109 using HashFuncsMapType = std::unordered_map<stable_hash, EntryStorage>;
110
111 /// Get the HashToFuncs map for serialization.
113
114 /// Get the NameToId vector for serialization.
115 ArrayRef<std::string> getNames() const { return IdToName; }
116
117 /// Get an existing ID associated with the given name or create a new ID if it
118 /// doesn't exist.
120
121 /// Get the name associated with a given ID
122 LLVM_ABI std::optional<std::string> getNameForId(unsigned Id) const;
123
124 /// Insert a `StableFunction` object into the function map. This method
125 /// handles the uniquing of string names and create a `StableFunctionEntry`
126 /// for insertion.
127 LLVM_ABI void insert(const StableFunction &Func);
128
129 /// Merge a \p OtherMap into this function map.
130 LLVM_ABI void merge(const StableFunctionMap &OtherMap);
131
132 /// \returns true if there is no stable function entry.
133 bool empty() const { return size() == 0; }
134
135 /// \returns true if there is an entry for the given function hash.
136 /// This does not trigger lazy loading.
137 bool contains(HashFuncsMapType::key_type FunctionHash) const {
138 return HashToFuncs.count(FunctionHash) > 0;
139 }
140
141 /// \returns the stable function entries for the given function hash. If the
142 /// map is lazily loaded, it will deserialize the entries if it is not already
143 /// done, other requests to the same hash at the same time will be blocked
144 /// until the entries are deserialized.
146 at(HashFuncsMapType::key_type FunctionHash) const;
147
148 enum SizeType {
149 UniqueHashCount, // The number of unique hashes in HashToFuncs.
150 TotalFunctionCount, // The number of total functions in HashToFuncs.
151 MergeableFunctionCount, // The number of functions that can be merged based
152 // on their hash.
153 };
154
155 /// \returns the size of StableFunctionMap.
156 /// \p Type is the type of size to return.
157 LLVM_ABI size_t size(SizeType Type = UniqueHashCount) const;
158
159 /// Finalize the stable function map by trimming content.
160 LLVM_ABI void finalize(const cl::LibraryOptionsContext &LibraryOptions,
161 bool SkipTrim = false);
162
163private:
164 /// Insert a `StableFunctionEntry` into the function map directly. This
165 /// method assumes that string names have already been uniqued and the
166 /// `StableFunctionEntry` is ready for insertion.
167 void insert(std::unique_ptr<StableFunctionEntry> FuncEntry) {
168 assert(!Finalized && "Cannot insert after finalization");
169 HashToFuncs[FuncEntry->Hash].Entries.emplace_back(std::move(FuncEntry));
170 }
171
172 void deserializeLazyLoadingEntry(HashFuncsMapType::iterator It) const;
173
174 /// Eagerly deserialize all the unloaded entries in the lazy loading map.
175 void deserializeLazyLoadingEntries() const;
176
177 bool isLazilyLoaded() const { return (bool)Buffer; }
178
179 /// A map from a stable_hash to a vector of functions with that hash.
180 mutable HashFuncsMapType HashToFuncs;
181 /// A vector of strings to hold names.
182 SmallVector<std::string> IdToName;
183 /// A map from StringRef (name) to an ID.
184 StringMap<unsigned> NameToId;
185 /// True if the function map is finalized with minimal content.
186 bool Finalized = false;
187 /// The memory buffer that contains the serialized stable function map for
188 /// lazy loading.
189 /// Non-empty only if this StableFunctionMap is created from a MemoryBuffer
190 /// (i.e. by IndexedCodeGenDataReader::read()) and lazily deserialized.
191 std::shared_ptr<MemoryBuffer> Buffer;
192 /// Whether to read stable function names from the buffer.
193 bool ReadStableFunctionMapNames = true;
194
196};
197
198} // namespace llvm
199
200#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:211
This file defines the DenseMap class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Assignment Tracking (at).
Definition DebugInfo.h:180
This namespace contains all of the command line option processing machinery.
Definition CodeGenData.h:33
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:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
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.
LLVM_ABI 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.