LLVM 20.0.0git
StableFunctionMapRecord.cpp
Go to the documentation of this file.
1//===-- StableFunctionMapRecord.cpp ---------------------------------------===//
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 implements the functionality for the StableFunctionMapRecord class,
10// including methods for serialization and deserialization of stable function
11// maps to and from raw and YAML streams. It also includes utilities for
12// managing function entries and their metadata.
13//
14//===----------------------------------------------------------------------===//
15
18
19#define DEBUG_TYPE "stable-function-map-record"
20
21using namespace llvm;
22using namespace llvm::support;
23
26
27namespace llvm {
28namespace yaml {
29
30template <> struct MappingTraits<IndexPairHash> {
31 static void mapping(IO &IO, IndexPairHash &Key) {
32 IO.mapRequired("InstIndex", Key.first.first);
33 IO.mapRequired("OpndIndex", Key.first.second);
34 IO.mapRequired("OpndHash", Key.second);
35 }
36};
37
38template <> struct MappingTraits<StableFunction> {
39 static void mapping(IO &IO, StableFunction &Func) {
40 IO.mapRequired("Hash", Func.Hash);
41 IO.mapRequired("FunctionName", Func.FunctionName);
42 IO.mapRequired("ModuleName", Func.ModuleName);
43 IO.mapRequired("InstCount", Func.InstCount);
44 IO.mapRequired("IndexOperandHashes", Func.IndexOperandHashes);
45 }
46};
47
48} // namespace yaml
49} // namespace llvm
50
51// Get a sorted vector of StableFunctionEntry pointers.
55 for (const auto &P : SFM.getFunctionMap())
56 for (auto &Func : P.second)
57 FuncEntries.emplace_back(Func.get());
58
59 std::stable_sort(
60 FuncEntries.begin(), FuncEntries.end(), [&](auto &A, auto &B) {
61 return std::tuple(A->Hash, SFM.getNameForId(A->ModuleNameId),
62 SFM.getNameForId(A->FunctionNameId)) <
63 std::tuple(B->Hash, SFM.getNameForId(B->ModuleNameId),
64 SFM.getNameForId(B->FunctionNameId));
65 });
66 return FuncEntries;
67}
68
69// Get a sorted vector of IndexOperandHashes.
72 IndexOperandHashVecType IndexOperandHashes;
73 for (auto &[Indices, OpndHash] : *FuncEntry->IndexOperandHashMap)
74 IndexOperandHashes.emplace_back(Indices, OpndHash);
75 // The indices are unique, so we can just sort by the first.
76 llvm::sort(IndexOperandHashes);
77 return IndexOperandHashes;
78}
79
81 serialize(OS, FunctionMap.get());
82}
83
85 const StableFunctionMap *FunctionMap) {
87
88 // Write Names.
89 auto &Names = FunctionMap->getNames();
90 uint32_t ByteSize = 4;
91 Writer.write<uint32_t>(Names.size());
92 for (auto &Name : Names) {
93 Writer.OS << Name << '\0';
94 ByteSize += Name.size() + 1;
95 }
96 // Align ByteSize to 4 bytes.
97 uint32_t Padding = offsetToAlignment(ByteSize, Align(4));
98 for (uint32_t I = 0; I < Padding; ++I)
99 Writer.OS << '\0';
100
101 // Write StableFunctionEntries whose pointers are sorted.
102 auto FuncEntries = getStableFunctionEntries(*FunctionMap);
103 Writer.write<uint32_t>(FuncEntries.size());
104
105 for (const auto *FuncRef : FuncEntries) {
106 Writer.write<stable_hash>(FuncRef->Hash);
107 Writer.write<uint32_t>(FuncRef->FunctionNameId);
108 Writer.write<uint32_t>(FuncRef->ModuleNameId);
109 Writer.write<uint32_t>(FuncRef->InstCount);
110
111 // Emit IndexOperandHashes sorted from IndexOperandHashMap.
112 IndexOperandHashVecType IndexOperandHashes =
114 Writer.write<uint32_t>(IndexOperandHashes.size());
115 for (auto &IndexOperandHash : IndexOperandHashes) {
116 Writer.write<uint32_t>(IndexOperandHash.first.first);
117 Writer.write<uint32_t>(IndexOperandHash.first.second);
118 Writer.write<stable_hash>(IndexOperandHash.second);
119 }
120 }
121}
122
123void StableFunctionMapRecord::deserialize(const unsigned char *&Ptr) {
124 // Assert that Ptr is 4-byte aligned
125 assert(((uintptr_t)Ptr % 4) == 0);
126 // Read Names.
127 auto NumNames =
128 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
129 // Early exit if there is no name.
130 if (NumNames == 0)
131 return;
132 for (unsigned I = 0; I < NumNames; ++I) {
133 StringRef Name(reinterpret_cast<const char *>(Ptr));
134 Ptr += Name.size() + 1;
135 FunctionMap->getIdOrCreateForName(Name);
136 }
137 // Align Ptr to 4 bytes.
138 Ptr = reinterpret_cast<const uint8_t *>(alignAddr(Ptr, Align(4)));
139
140 // Read StableFunctionEntries.
141 auto NumFuncs =
142 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
143 for (unsigned I = 0; I < NumFuncs; ++I) {
144 auto Hash =
145 endian::readNext<stable_hash, endianness::little, unaligned>(Ptr);
146 auto FunctionNameId =
147 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
148 assert(FunctionMap->getNameForId(FunctionNameId) &&
149 "FunctionNameId out of range");
150 auto ModuleNameId =
151 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
152 assert(FunctionMap->getNameForId(ModuleNameId) &&
153 "ModuleNameId out of range");
154 auto InstCount =
155 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
156
157 // Read IndexOperandHashes to build IndexOperandHashMap
158 auto NumIndexOperandHashes =
159 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
160 auto IndexOperandHashMap = std::make_unique<IndexOperandHashMapType>();
161 for (unsigned J = 0; J < NumIndexOperandHashes; ++J) {
162 auto InstIndex =
163 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
164 auto OpndIndex =
165 endian::readNext<uint32_t, endianness::little, unaligned>(Ptr);
166 auto OpndHash =
167 endian::readNext<stable_hash, endianness::little, unaligned>(Ptr);
168 assert(InstIndex < InstCount && "InstIndex out of range");
169
170 IndexOperandHashMap->try_emplace({InstIndex, OpndIndex}, OpndHash);
171 }
172
173 // Insert a new StableFunctionEntry into the map.
174 auto FuncEntry = std::make_unique<StableFunctionMap::StableFunctionEntry>(
175 Hash, FunctionNameId, ModuleNameId, InstCount,
176 std::move(IndexOperandHashMap));
177
178 FunctionMap->insert(std::move(FuncEntry));
179 }
180}
181
182void StableFunctionMapRecord::serializeYAML(yaml::Output &YOS) const {
183 auto FuncEntries = getStableFunctionEntries(*FunctionMap);
185 for (const auto *FuncEntry : FuncEntries) {
186 auto IndexOperandHashes = getStableIndexOperandHashes(FuncEntry);
187 Functions.emplace_back(
188 FuncEntry->Hash, *FunctionMap->getNameForId(FuncEntry->FunctionNameId),
189 *FunctionMap->getNameForId(FuncEntry->ModuleNameId),
190 FuncEntry->InstCount, std::move(IndexOperandHashes));
191 }
192
193 YOS << Functions;
194}
195
197 std::vector<StableFunction> Funcs;
198 YIS >> Funcs;
199 for (auto &Func : Funcs)
200 FunctionMap->insert(Func);
201 YIS.nextDocument();
202}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static IndexOperandHashVecType getStableIndexOperandHashes(const StableFunctionMap::StableFunctionEntry *FuncEntry)
static SmallVector< const StableFunctionMap::StableFunctionEntry * > getStableFunctionEntries(const StableFunctionMap &SFM)
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::pair< IndexPair, stable_hash > IndexPairHash
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: Alignment.h:197
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition: Alignment.h:187
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
void deserialize(const unsigned char *&Ptr)
Deserialize the stable function map from a raw_ostream.
std::unique_ptr< StableFunctionMap > FunctionMap
static void serialize(raw_ostream &OS, const StableFunctionMap *FunctionMap)
A static helper function to serialize the stable function map without owning the stable function map.
void deserializeYAML(yaml::Input &YIS)
Deserialize the stable function map from a YAML stream.
void serializeYAML(yaml::Output &YOS) const
Serialize the stable function map to a YAML stream.
An efficient form of StableFunction for fast look-up.
std::unique_ptr< IndexOperandHashMapType > IndexOperandHashMap
A map from an IndexPair to a stable_hash which was skipped.
const HashFuncsMapType & getFunctionMap() const
Get the HashToFuncs map for serialization.
A stable function is a function with a stable hash while tracking the locations of ignored operands a...
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:71
static void mapping(IO &IO, IndexPairHash &Key)
static void mapping(IO &IO, StableFunction &Func)