1//===- StableFunctionMap.h -------------------------------------*- C++ -*-===//
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
7//===---------------------------------------------------------------------===//
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.
14//===---------------------------------------------------------------------===//
16#ifndef LLVM_CGDATA_STABLEFUNCTIONMAP_H
17#define LLVM_CGDATA_STABLEFUNCTIONMAP_H
25#include <unordered_map>
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.
39 /// The name of the module the function is in.
41 /// The number of instructions.
43 /// A vector of pairs of IndexPair and operand hash which was skipped.
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.
64 /// The number of instructions.
66 /// A map from an IndexPair to a stable_hash which was skipped.
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.
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.
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
97 std::once_flag LazyLoadFlag;
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().
107 /// Get the HashToFuncs map for serialization.
110 /// Get the NameToId vector for serialization.
113 /// Get an existing ID associated with the given name or create a new ID if it
117 /// Get the name associated with a given ID
120 /// Insert a `StableFunction` object into the function map. This method
121 /// handles the uniquing of string names and create a `StableFunctionEntry`
125 /// Merge a \p OtherMap into this function map.
128 /// \returns true if there is no stable function entry.
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;
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;
151 /// \returns the size of StableFunctionMap.
152 /// \p Type is the type of size to return.
155 /// Finalize the stable function map by trimming content.
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));
167 void deserializeLazyLoadingEntry(HashFuncsMapType::iterator It)
const;
169 /// Eagerly deserialize all the unloaded entries in the lazy loading map.
170 void deserializeLazyLoadingEntries()
const;
172 bool isLazilyLoaded()
const {
return (
bool)Buffer; }
174 /// A map from a stable_hash to a vector of functions with that hash.
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
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;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
This file defines the DenseMap class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
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.
The instances of the Type class are immutable: once they are created, they are never changed.
Assignment Tracking (at).
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.
Implement std::hash so that hash_code can be used in STL containers.
In addition to the deserialized StableFunctionEntry, the struct stores the offsets of corresponding s...
StableFunctionEntries Entries
The actual storage of deserialized stable function entries.
friend struct StableFunctionMap
friend struct StableFunctionMapRecord
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(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.