LLVM 24.0.0git
ModuleSummaryIndexYAML.h
Go to the documentation of this file.
1//===-- llvm/ModuleSummaryIndexYAML.h - YAML I/O for summary ----*- 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#ifndef LLVM_IR_MODULESUMMARYINDEXYAML_H
10#define LLVM_IR_MODULESUMMARYINDEXYAML_H
11
12#include "llvm/ADT/StringRef.h"
15#include <algorithm>
16
17namespace llvm {
18namespace yaml {
19
21 static void enumeration(IO &io, TypeTestResolution::Kind &value) {
22 io.enumCase(value, "Unknown", TypeTestResolution::Unknown);
23 io.enumCase(value, "Unsat", TypeTestResolution::Unsat);
24 io.enumCase(value, "ByteArray", TypeTestResolution::ByteArray);
25 io.enumCase(value, "Inline", TypeTestResolution::Inline);
26 io.enumCase(value, "Single", TypeTestResolution::Single);
27 io.enumCase(value, "AllOnes", TypeTestResolution::AllOnes);
28 }
29};
30
31template <> struct MappingTraits<TypeTestResolution> {
32 static void mapping(IO &io, TypeTestResolution &res) {
33 io.mapOptional("Kind", res.TheKind);
34 io.mapOptional("SizeM1BitWidth", res.SizeM1BitWidth);
35 io.mapOptional("AlignLog2", res.AlignLog2);
36 io.mapOptional("SizeM1", res.SizeM1);
37 io.mapOptional("BitMask", res.BitMask);
38 io.mapOptional("InlineBits", res.InlineBits);
39 }
40};
41
42template <>
55
56template <> struct MappingTraits<WholeProgramDevirtResolution::ByArg> {
58 io.mapOptional("Kind", res.TheKind);
59 io.mapOptional("Info", res.Info);
60 io.mapOptional("Byte", res.Byte);
61 io.mapOptional("Bit", res.Bit);
62 }
63};
64
65template <>
67 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>> {
68 static void inputOne(
69 IO &io, StringRef Key,
70 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> &V) {
71 std::vector<uint64_t> Args;
72 std::pair<StringRef, StringRef> P = {"", Key};
73 while (!P.second.empty()) {
74 P = P.second.split(',');
75 uint64_t Arg;
76 if (P.first.getAsInteger(0, Arg)) {
77 io.setError("key not an integer");
78 return;
79 }
80 Args.push_back(Arg);
81 }
82 io.mapRequired(Key, V[Args]);
83 }
84 static void output(
85 IO &io,
86 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> &V) {
87 for (auto &P : V) {
88 std::string Key;
89 for (uint64_t Arg : P.first) {
90 if (!Key.empty())
91 Key += ',';
92 Key += llvm::utostr(Arg);
93 }
94 io.mapRequired(Key, P.second);
95 }
96 }
97};
98
102 io.enumCase(value, "SingleImpl", WholeProgramDevirtResolution::SingleImpl);
103 io.enumCase(value, "BranchFunnel",
105 io.enumCase(value, "Speculative",
107 }
108};
109
111 static void mapping(IO &io, WholeProgramDevirtResolution &res) {
112 io.mapOptional("Kind", res.TheKind);
113 io.mapOptional("SingleImplName", res.SingleImplName);
114 io.mapOptional("ResByArg", res.ResByArg);
115 }
116};
117
118template <>
119struct CustomMappingTraits<std::map<uint64_t, WholeProgramDevirtResolution>> {
120 static void inputOne(IO &io, StringRef Key,
121 std::map<uint64_t, WholeProgramDevirtResolution> &V) {
122 uint64_t KeyInt;
123 if (Key.getAsInteger(0, KeyInt)) {
124 io.setError("key not an integer");
125 return;
126 }
127 io.mapRequired(Key, V[KeyInt]);
128 }
129 static void output(IO &io, std::map<uint64_t, WholeProgramDevirtResolution> &V) {
130 for (auto &P : V)
131 io.mapRequired(llvm::utostr(P.first), P.second);
132 }
133};
134
135template <> struct MappingTraits<TypeIdSummary> {
136 static void mapping(IO &io, TypeIdSummary& summary) {
137 io.mapOptional("TTRes", summary.TTRes);
138 io.mapOptional("WPDRes", summary.WPDRes);
139 }
140};
141
143 // Commonly used fields
146 unsigned ImportType;
148 // Fields for AliasSummary
149 std::optional<uint64_t> Aliasee;
150 // Fields for FunctionSummary
151 std::vector<uint64_t> Refs = {};
152 std::vector<uint64_t> TypeTests = {};
153 std::vector<FunctionSummary::VFuncId> TypeTestAssumeVCalls = {};
154 std::vector<FunctionSummary::VFuncId> TypeCheckedLoadVCalls = {};
155 std::vector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls = {};
156 std::vector<FunctionSummary::ConstVCall> TypeCheckedLoadConstVCalls = {};
157};
158
159} // End yaml namespace
160} // End llvm namespace
161
162namespace llvm {
163namespace yaml {
164
165template <> struct MappingTraits<FunctionSummary::VFuncId> {
166 static void mapping(IO &io, FunctionSummary::VFuncId& id) {
167 io.mapOptional("GUID", id.GUID);
168 io.mapOptional("Offset", id.Offset);
169 }
170};
171
172template <> struct MappingTraits<FunctionSummary::ConstVCall> {
173 static void mapping(IO &io, FunctionSummary::ConstVCall& id) {
174 io.mapOptional("VFunc", id.VFunc);
175 io.mapOptional("Args", id.Args);
176 }
177};
178
179} // End yaml namespace
180} // End llvm namespace
181
182LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionSummary::VFuncId)
183LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionSummary::ConstVCall)
184
185namespace llvm {
186namespace yaml {
187
189 static void mapping(IO &io, GlobalValueSummaryYaml &summary) {
190 io.mapOptional("Linkage", summary.Linkage);
191 io.mapOptional("Visibility", summary.Visibility);
192 io.mapOptional("NotEligibleToImport", summary.NotEligibleToImport);
193 io.mapOptional("Live", summary.Live);
194 io.mapOptional("Local", summary.IsLocal);
195 io.mapOptional("CanAutoHide", summary.CanAutoHide);
196 io.mapOptional("ImportType", summary.ImportType);
197 io.mapOptional("NoRenameOnPromotion", summary.NoRenameOnPromotion);
198 io.mapOptional("Aliasee", summary.Aliasee);
199 io.mapOptional("Refs", summary.Refs);
200 io.mapOptional("TypeTests", summary.TypeTests);
201 io.mapOptional("TypeTestAssumeVCalls", summary.TypeTestAssumeVCalls);
202 io.mapOptional("TypeCheckedLoadVCalls", summary.TypeCheckedLoadVCalls);
203 io.mapOptional("TypeTestAssumeConstVCalls",
205 io.mapOptional("TypeCheckedLoadConstVCalls",
207 }
208};
209
210} // End yaml namespace
211} // End llvm namespace
212
213LLVM_YAML_IS_SEQUENCE_VECTOR(GlobalValueSummaryYaml)
214
215namespace llvm {
216namespace yaml {
217
218// FIXME: Add YAML mappings for the rest of the module summary.
221 std::vector<GlobalValueSummaryYaml> GVSums;
222 io.mapRequired(Key, GVSums);
223 uint64_t KeyInt;
224 if (Key.getAsInteger(0, KeyInt)) {
225 io.setError("key not an integer");
226 return;
227 }
228 auto &Elem = V.try_emplace(KeyInt, /*IsAnalysis=*/false).first->second;
229 for (auto &GVSum : GVSums) {
231 static_cast<GlobalValue::LinkageTypes>(GVSum.Linkage),
232 static_cast<GlobalValue::VisibilityTypes>(GVSum.Visibility),
233 GVSum.NotEligibleToImport, GVSum.Live, GVSum.IsLocal,
234 GVSum.CanAutoHide,
235 static_cast<GlobalValueSummary::ImportKind>(GVSum.ImportType),
236 GVSum.NoRenameOnPromotion);
237 if (GVSum.Aliasee) {
238 auto ASum = std::make_unique<AliasSummary>(GVFlags);
239 V.try_emplace(*GVSum.Aliasee, /*IsAnalysis=*/false);
240 ValueInfo AliaseeVI(/*IsAnalysis=*/false, &*V.find(*GVSum.Aliasee));
241 // Note: Aliasee cannot be filled until all summaries are loaded.
242 // This is done in fixAliaseeLinks() which is called in
243 // MappingTraits<ModuleSummaryIndex>::mapping().
244 ASum->setAliasee(AliaseeVI, /*Aliasee=*/nullptr);
245 Elem.addSummary(std::move(ASum));
246 continue;
247 }
249 Refs.reserve(GVSum.Refs.size());
250 for (auto &RefGUID : GVSum.Refs) {
251 auto It = V.try_emplace(RefGUID, /*IsAnalysis=*/false).first;
252 Refs.push_back(ValueInfo(/*IsAnalysis=*/false, &*It));
253 }
254 Elem.addSummary(std::make_unique<FunctionSummary>(
255 GVFlags, /*NumInsts=*/0, FunctionSummary::FFlags{}, std::move(Refs),
256 SmallVector<FunctionSummary::EdgeTy, 0>{}, std::move(GVSum.TypeTests),
257 std::move(GVSum.TypeTestAssumeVCalls),
258 std::move(GVSum.TypeCheckedLoadVCalls),
259 std::move(GVSum.TypeTestAssumeConstVCalls),
260 std::move(GVSum.TypeCheckedLoadConstVCalls),
263 }
264 }
265 static void output(IO &io, GlobalValueSummaryMapTy &V) {
266 // Sort by GUID for deterministic output.
267 for (const auto &P : V.sortedRange()) {
268 std::vector<GlobalValueSummaryYaml> GVSums;
269 for (auto &Sum : P.second.getSummaryList()) {
270 if (auto *FSum = dyn_cast<FunctionSummary>(Sum.get())) {
271 std::vector<uint64_t> Refs;
272 Refs.reserve(FSum->refs().size());
273 for (auto &VI : FSum->refs())
274 Refs.push_back(VI.getGUID());
275 GVSums.push_back(GlobalValueSummaryYaml{
276 FSum->flags().Linkage, FSum->flags().Visibility,
277 static_cast<bool>(FSum->flags().NotEligibleToImport),
278 static_cast<bool>(FSum->flags().Live),
279 static_cast<bool>(FSum->flags().DSOLocal),
280 static_cast<bool>(FSum->flags().CanAutoHide),
281 FSum->flags().ImportType,
282 static_cast<bool>(FSum->flags().NoRenameOnPromotion),
283 /*Aliasee=*/std::nullopt, Refs, FSum->type_tests(),
284 FSum->type_test_assume_vcalls(), FSum->type_checked_load_vcalls(),
285 FSum->type_test_assume_const_vcalls(),
286 FSum->type_checked_load_const_vcalls()});
287 } else if (auto *ASum = dyn_cast<AliasSummary>(Sum.get());
288 ASum && ASum->hasAliasee()) {
289 GVSums.push_back(GlobalValueSummaryYaml{
290 ASum->flags().Linkage, ASum->flags().Visibility,
291 static_cast<bool>(ASum->flags().NotEligibleToImport),
292 static_cast<bool>(ASum->flags().Live),
293 static_cast<bool>(ASum->flags().DSOLocal),
294 static_cast<bool>(ASum->flags().CanAutoHide),
295 ASum->flags().ImportType,
296 static_cast<bool>(ASum->flags().NoRenameOnPromotion),
297 /*Aliasee=*/ASum->getAliaseeGUID()});
298 }
299 }
300 if (!GVSums.empty())
301 io.mapRequired(llvm::utostr(P.first), GVSums);
302 }
303 }
305 for (auto &P : V) {
306 for (auto &Sum : P.second.getSummaryList()) {
307 if (auto *Alias = dyn_cast<AliasSummary>(Sum.get())) {
308 ValueInfo AliaseeVI = Alias->getAliaseeVI();
309 auto AliaseeSL = AliaseeVI.getSummaryList();
310 if (AliaseeSL.empty()) {
312 Alias->setAliasee(EmptyVI, nullptr);
313 } else
314 Alias->setAliasee(AliaseeVI, AliaseeSL[0].get());
315 }
316 }
317 }
318 }
319};
320
323 TypeIdSummary TId;
324 io.mapRequired(Key, TId);
326 }
327 static void output(IO &io, TypeIdSummaryMapTy &V) {
328 for (auto &TidIter : V)
329 io.mapRequired(TidIter.second.first, TidIter.second.second);
330 }
331};
332
333template <> struct MappingTraits<std::pair<StringRef, GlobalValue::GUID>> {
334 static void mapping(IO &io,
335 std::pair<StringRef, GlobalValue::GUID> &NameAndGUID) {
336 io.mapRequired("Name", NameAndGUID.first);
337 io.mapRequired("GUID", NameAndGUID.second);
338 }
339};
340
341using StringAndGUID = std::pair<llvm::StringRef, llvm::GlobalValue::GUID>;
342
343} // namespace yaml
344} // namespace llvm
345
347
348namespace llvm {
349namespace yaml {
350
352 static void mapping(IO &io, ModuleSummaryIndex& index) {
353 io.mapOptional("GlobalValueMap", index.GlobalValueMap);
354 if (!io.outputting())
356 index.GlobalValueMap);
357
358 if (io.outputting()) {
359 io.mapOptional("TypeIdMap", index.TypeIdMap);
360 } else {
361 TypeIdSummaryMapTy TypeIdMap;
362 io.mapOptional("TypeIdMap", TypeIdMap);
363 for (auto &[TypeGUID, TypeIdSummaryMap] : TypeIdMap) {
364 // Save type id references in index and point TypeIdMap to use the
365 // references owned by index.
366 StringRef KeyRef = index.TypeIdSaver.save(TypeIdSummaryMap.first);
367 index.TypeIdMap.insert(
368 {TypeGUID, {KeyRef, std::move(TypeIdSummaryMap.second)}});
369 }
370 }
371
372 io.mapOptional("WithGlobalValueDeadStripping",
373 index.WithGlobalValueDeadStripping);
374
375 if (io.outputting()) {
376 auto CfiFunctionDefs = index.CfiFunctionDefs.getSortedSymbols();
377 io.mapOptional("CfiFunctionDefs", CfiFunctionDefs);
378 auto CfiFunctionDecls(index.CfiFunctionDecls.getSortedSymbols());
379 io.mapOptional("CfiFunctionDecls", CfiFunctionDecls);
380 } else {
381 std::vector<std::pair<StringRef, GlobalValue::GUID>> CfiFunctionDefs;
382 io.mapOptional("CfiFunctionDefs", CfiFunctionDefs);
383 for (auto &[S, G] : CfiFunctionDefs)
384 index.CfiFunctionDefs.addSymbolWithThinLTOGUID(S, G);
385 std::vector<std::pair<StringRef, GlobalValue::GUID>> CfiFunctionDecls;
386 io.mapOptional("CfiFunctionDecls", CfiFunctionDecls);
387 for (auto &[S, G] : CfiFunctionDecls)
388 index.CfiFunctionDecls.addSymbolWithThinLTOGUID(S, G);
389 }
390 }
391};
392
393} // namespace yaml
394} // namespace llvm
395
396#endif
uint64_t uint64_t
static ValueInfo EmptyVI
#define G(x, y, z)
Definition MD5.cpp:55
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
#define P(N)
#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)
Utility for declaring that a std::vector of a particular type should be considered a YAML sequence.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
std::vector< std::pair< StringRef, GlobalValue::GUID > > getSortedSymbols() const
API used for serialization, e.g. YAML.
void addSymbolWithThinLTOGUID(StringRef Name, GlobalValue::GUID GUID)
Add the function name and the GUID that ThinLTO uses for it.
Function summary information to aid decisions and implementation of importing.
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
Class to hold module path string table and global value map, and encapsulate methods for operating on...
void reserve(size_type N)
void push_back(const T &Elt)
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
StringRef save(const char *S)
Definition StringSaver.h:53
virtual bool outputting() const =0
void enumCase(T &Val, StringRef Str, const T ConstVal)
Definition YAMLTraits.h:735
void mapOptional(StringRef Key, T &Val)
Definition YAMLTraits.h:800
virtual void setError(const Twine &)=0
void mapRequired(StringRef Key, T &Val)
Definition YAMLTraits.h:790
std::pair< llvm::StringRef, llvm::GlobalValue::GUID > StringAndGUID
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
std::multimap< GlobalValue::GUID, std::pair< StringRef, TypeIdSummary > > TypeIdSummaryMapTy
Map of a type GUID to type id string and summary (multimap used in case of GUID conflicts).
std::string utostr(uint64_t X, bool isNeg=false)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
GlobalValueSummaryMap GlobalValueSummaryMapTy
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
A specification for a virtual function call with all constant integer arguments.
Flags specific to function summaries.
An "identifier" for a virtual function.
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair.
TypeTestResolution TTRes
Kind
Specifies which kind of type check we should emit for this byte array.
@ Unknown
Unknown (analysis not performed, don't lower)
@ Single
Single element (last example in "Short Inline Bit Vectors")
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
@ ByteArray
Test a byte array (first example)
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
enum llvm::TypeTestResolution::Kind TheKind
Struct that holds a reference to a particular GUID in a global value summary.
ArrayRef< std::unique_ptr< GlobalValueSummary > > getSummaryList() const
@ UniformRetVal
Uniform return value optimization.
@ VirtualConstProp
Virtual constant propagation.
@ UniqueRetVal
Unique return value optimization.
@ Indir
Just do a regular virtual call.
uint64_t Info
Additional information for the resolution:
enum llvm::WholeProgramDevirtResolution::ByArg::Kind TheKind
enum llvm::WholeProgramDevirtResolution::Kind TheKind
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument,...
@ SingleImpl
Single implementation devirtualization.
@ Indir
Just do a regular virtual call.
@ Speculative
Speculative devirtualization.
@ BranchFunnel
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module.
static void output(IO &io, GlobalValueSummaryMapTy &V)
static void inputOne(IO &io, StringRef Key, GlobalValueSummaryMapTy &V)
static void output(IO &io, TypeIdSummaryMapTy &V)
static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V)
static void inputOne(IO &io, StringRef Key, std::map< std::vector< uint64_t >, WholeProgramDevirtResolution::ByArg > &V)
static void output(IO &io, std::map< std::vector< uint64_t >, WholeProgramDevirtResolution::ByArg > &V)
static void inputOne(IO &io, StringRef Key, std::map< uint64_t, WholeProgramDevirtResolution > &V)
static void output(IO &io, std::map< uint64_t, WholeProgramDevirtResolution > &V)
This class should be specialized by any type that needs to be converted to/from a YAML mapping in the...
Definition YAMLTraits.h:274
std::vector< FunctionSummary::ConstVCall > TypeTestAssumeConstVCalls
std::vector< FunctionSummary::VFuncId > TypeCheckedLoadVCalls
std::vector< FunctionSummary::ConstVCall > TypeCheckedLoadConstVCalls
std::vector< FunctionSummary::VFuncId > TypeTestAssumeVCalls
static void mapping(IO &io, FunctionSummary::ConstVCall &id)
static void mapping(IO &io, FunctionSummary::VFuncId &id)
static void mapping(IO &io, GlobalValueSummaryYaml &summary)
static void mapping(IO &io, ModuleSummaryIndex &index)
static void mapping(IO &io, TypeIdSummary &summary)
static void mapping(IO &io, TypeTestResolution &res)
static void mapping(IO &io, WholeProgramDevirtResolution &res)
static void mapping(IO &io, WholeProgramDevirtResolution::ByArg &res)
static void mapping(IO &io, std::pair< StringRef, GlobalValue::GUID > &NameAndGUID)
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:63
static void enumeration(IO &io, TypeTestResolution::Kind &value)
static void enumeration(IO &io, WholeProgramDevirtResolution::ByArg::Kind &value)
static void enumeration(IO &io, WholeProgramDevirtResolution::Kind &value)
This class should be specialized by any integral type that converts to/from a YAML scalar where there...
Definition YAMLTraits.h:108