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/IR/ModuleSummaryIndex.h" |
13 | #include "llvm/Support/YAMLTraits.h" |
14 | |
15 | namespace llvm { |
16 | namespace yaml { |
17 | |
18 | template <> struct ScalarEnumerationTraits<TypeTestResolution::Kind> { |
19 | static void enumeration(IO &io, TypeTestResolution::Kind &value) { |
20 | io.enumCase(Val&: value, Str: "Unknown" , ConstVal: TypeTestResolution::Unknown); |
21 | io.enumCase(Val&: value, Str: "Unsat" , ConstVal: TypeTestResolution::Unsat); |
22 | io.enumCase(Val&: value, Str: "ByteArray" , ConstVal: TypeTestResolution::ByteArray); |
23 | io.enumCase(Val&: value, Str: "Inline" , ConstVal: TypeTestResolution::Inline); |
24 | io.enumCase(Val&: value, Str: "Single" , ConstVal: TypeTestResolution::Single); |
25 | io.enumCase(Val&: value, Str: "AllOnes" , ConstVal: TypeTestResolution::AllOnes); |
26 | } |
27 | }; |
28 | |
29 | template <> struct MappingTraits<TypeTestResolution> { |
30 | static void mapping(IO &io, TypeTestResolution &res) { |
31 | io.mapOptional(Key: "Kind" , Val&: res.TheKind); |
32 | io.mapOptional(Key: "SizeM1BitWidth" , Val&: res.SizeM1BitWidth); |
33 | io.mapOptional(Key: "AlignLog2" , Val&: res.AlignLog2); |
34 | io.mapOptional(Key: "SizeM1" , Val&: res.SizeM1); |
35 | io.mapOptional(Key: "BitMask" , Val&: res.BitMask); |
36 | io.mapOptional(Key: "InlineBits" , Val&: res.InlineBits); |
37 | } |
38 | }; |
39 | |
40 | template <> |
41 | struct ScalarEnumerationTraits<WholeProgramDevirtResolution::ByArg::Kind> { |
42 | static void enumeration(IO &io, |
43 | WholeProgramDevirtResolution::ByArg::Kind &value) { |
44 | io.enumCase(Val&: value, Str: "Indir" , ConstVal: WholeProgramDevirtResolution::ByArg::Indir); |
45 | io.enumCase(Val&: value, Str: "UniformRetVal" , |
46 | ConstVal: WholeProgramDevirtResolution::ByArg::UniformRetVal); |
47 | io.enumCase(Val&: value, Str: "UniqueRetVal" , |
48 | ConstVal: WholeProgramDevirtResolution::ByArg::UniqueRetVal); |
49 | io.enumCase(Val&: value, Str: "VirtualConstProp" , |
50 | ConstVal: WholeProgramDevirtResolution::ByArg::VirtualConstProp); |
51 | } |
52 | }; |
53 | |
54 | template <> struct MappingTraits<WholeProgramDevirtResolution::ByArg> { |
55 | static void mapping(IO &io, WholeProgramDevirtResolution::ByArg &res) { |
56 | io.mapOptional(Key: "Kind" , Val&: res.TheKind); |
57 | io.mapOptional(Key: "Info" , Val&: res.Info); |
58 | io.mapOptional(Key: "Byte" , Val&: res.Byte); |
59 | io.mapOptional(Key: "Bit" , Val&: res.Bit); |
60 | } |
61 | }; |
62 | |
63 | template <> |
64 | struct CustomMappingTraits< |
65 | std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>> { |
66 | static void inputOne( |
67 | IO &io, StringRef Key, |
68 | std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> &V) { |
69 | std::vector<uint64_t> Args; |
70 | std::pair<StringRef, StringRef> P = {"" , Key}; |
71 | while (!P.second.empty()) { |
72 | P = P.second.split(Separator: ','); |
73 | uint64_t Arg; |
74 | if (P.first.getAsInteger(Radix: 0, Result&: Arg)) { |
75 | io.setError("key not an integer" ); |
76 | return; |
77 | } |
78 | Args.push_back(x: Arg); |
79 | } |
80 | io.mapRequired(Key: Key.str().c_str(), Val&: V[Args]); |
81 | } |
82 | static void output( |
83 | IO &io, |
84 | std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> &V) { |
85 | for (auto &P : V) { |
86 | std::string Key; |
87 | for (uint64_t Arg : P.first) { |
88 | if (!Key.empty()) |
89 | Key += ','; |
90 | Key += llvm::utostr(X: Arg); |
91 | } |
92 | io.mapRequired(Key: Key.c_str(), Val&: P.second); |
93 | } |
94 | } |
95 | }; |
96 | |
97 | template <> struct ScalarEnumerationTraits<WholeProgramDevirtResolution::Kind> { |
98 | static void enumeration(IO &io, WholeProgramDevirtResolution::Kind &value) { |
99 | io.enumCase(Val&: value, Str: "Indir" , ConstVal: WholeProgramDevirtResolution::Indir); |
100 | io.enumCase(Val&: value, Str: "SingleImpl" , ConstVal: WholeProgramDevirtResolution::SingleImpl); |
101 | io.enumCase(Val&: value, Str: "BranchFunnel" , |
102 | ConstVal: WholeProgramDevirtResolution::BranchFunnel); |
103 | } |
104 | }; |
105 | |
106 | template <> struct MappingTraits<WholeProgramDevirtResolution> { |
107 | static void mapping(IO &io, WholeProgramDevirtResolution &res) { |
108 | io.mapOptional(Key: "Kind" , Val&: res.TheKind); |
109 | io.mapOptional(Key: "SingleImplName" , Val&: res.SingleImplName); |
110 | io.mapOptional(Key: "ResByArg" , Val&: res.ResByArg); |
111 | } |
112 | }; |
113 | |
114 | template <> |
115 | struct CustomMappingTraits<std::map<uint64_t, WholeProgramDevirtResolution>> { |
116 | static void inputOne(IO &io, StringRef Key, |
117 | std::map<uint64_t, WholeProgramDevirtResolution> &V) { |
118 | uint64_t KeyInt; |
119 | if (Key.getAsInteger(Radix: 0, Result&: KeyInt)) { |
120 | io.setError("key not an integer" ); |
121 | return; |
122 | } |
123 | io.mapRequired(Key: Key.str().c_str(), Val&: V[KeyInt]); |
124 | } |
125 | static void output(IO &io, std::map<uint64_t, WholeProgramDevirtResolution> &V) { |
126 | for (auto &P : V) |
127 | io.mapRequired(Key: llvm::utostr(X: P.first).c_str(), Val&: P.second); |
128 | } |
129 | }; |
130 | |
131 | template <> struct MappingTraits<TypeIdSummary> { |
132 | static void mapping(IO &io, TypeIdSummary& summary) { |
133 | io.mapOptional(Key: "TTRes" , Val&: summary.TTRes); |
134 | io.mapOptional(Key: "WPDRes" , Val&: summary.WPDRes); |
135 | } |
136 | }; |
137 | |
138 | struct FunctionSummaryYaml { |
139 | unsigned Linkage, Visibility; |
140 | bool NotEligibleToImport, Live, IsLocal, CanAutoHide; |
141 | unsigned ImportType; |
142 | std::vector<uint64_t> Refs; |
143 | std::vector<uint64_t> TypeTests; |
144 | std::vector<FunctionSummary::VFuncId> TypeTestAssumeVCalls, |
145 | TypeCheckedLoadVCalls; |
146 | std::vector<FunctionSummary::ConstVCall> TypeTestAssumeConstVCalls, |
147 | TypeCheckedLoadConstVCalls; |
148 | }; |
149 | |
150 | } // End yaml namespace |
151 | } // End llvm namespace |
152 | |
153 | namespace llvm { |
154 | namespace yaml { |
155 | |
156 | template <> struct MappingTraits<FunctionSummary::VFuncId> { |
157 | static void mapping(IO &io, FunctionSummary::VFuncId& id) { |
158 | io.mapOptional(Key: "GUID" , Val&: id.GUID); |
159 | io.mapOptional(Key: "Offset" , Val&: id.Offset); |
160 | } |
161 | }; |
162 | |
163 | template <> struct MappingTraits<FunctionSummary::ConstVCall> { |
164 | static void mapping(IO &io, FunctionSummary::ConstVCall& id) { |
165 | io.mapOptional(Key: "VFunc" , Val&: id.VFunc); |
166 | io.mapOptional(Key: "Args" , Val&: id.Args); |
167 | } |
168 | }; |
169 | |
170 | } // End yaml namespace |
171 | } // End llvm namespace |
172 | |
173 | LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionSummary::VFuncId) |
174 | LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionSummary::ConstVCall) |
175 | |
176 | namespace llvm { |
177 | namespace yaml { |
178 | |
179 | template <> struct MappingTraits<FunctionSummaryYaml> { |
180 | static void mapping(IO &io, FunctionSummaryYaml& summary) { |
181 | io.mapOptional(Key: "Linkage" , Val&: summary.Linkage); |
182 | io.mapOptional(Key: "Visibility" , Val&: summary.Visibility); |
183 | io.mapOptional(Key: "NotEligibleToImport" , Val&: summary.NotEligibleToImport); |
184 | io.mapOptional(Key: "Live" , Val&: summary.Live); |
185 | io.mapOptional(Key: "Local" , Val&: summary.IsLocal); |
186 | io.mapOptional(Key: "CanAutoHide" , Val&: summary.CanAutoHide); |
187 | io.mapOptional(Key: "ImportType" , Val&: summary.ImportType); |
188 | io.mapOptional(Key: "Refs" , Val&: summary.Refs); |
189 | io.mapOptional(Key: "TypeTests" , Val&: summary.TypeTests); |
190 | io.mapOptional(Key: "TypeTestAssumeVCalls" , Val&: summary.TypeTestAssumeVCalls); |
191 | io.mapOptional(Key: "TypeCheckedLoadVCalls" , Val&: summary.TypeCheckedLoadVCalls); |
192 | io.mapOptional(Key: "TypeTestAssumeConstVCalls" , |
193 | Val&: summary.TypeTestAssumeConstVCalls); |
194 | io.mapOptional(Key: "TypeCheckedLoadConstVCalls" , |
195 | Val&: summary.TypeCheckedLoadConstVCalls); |
196 | } |
197 | }; |
198 | |
199 | } // End yaml namespace |
200 | } // End llvm namespace |
201 | |
202 | LLVM_YAML_IS_SEQUENCE_VECTOR(FunctionSummaryYaml) |
203 | |
204 | namespace llvm { |
205 | namespace yaml { |
206 | |
207 | // FIXME: Add YAML mappings for the rest of the module summary. |
208 | template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> { |
209 | static void inputOne(IO &io, StringRef Key, GlobalValueSummaryMapTy &V) { |
210 | std::vector<FunctionSummaryYaml> FSums; |
211 | io.mapRequired(Key: Key.str().c_str(), Val&: FSums); |
212 | uint64_t KeyInt; |
213 | if (Key.getAsInteger(Radix: 0, Result&: KeyInt)) { |
214 | io.setError("key not an integer" ); |
215 | return; |
216 | } |
217 | if (!V.count(x: KeyInt)) |
218 | V.emplace(args&: KeyInt, /*IsAnalysis=*/args: false); |
219 | auto &Elem = V.find(x: KeyInt)->second; |
220 | for (auto &FSum : FSums) { |
221 | std::vector<ValueInfo> Refs; |
222 | for (auto &RefGUID : FSum.Refs) { |
223 | if (!V.count(x: RefGUID)) |
224 | V.emplace(args&: RefGUID, /*IsAnalysis=*/args: false); |
225 | Refs.push_back(x: ValueInfo(/*IsAnalysis=*/false, &*V.find(x: RefGUID))); |
226 | } |
227 | Elem.SummaryList.push_back(x: std::make_unique<FunctionSummary>( |
228 | args: GlobalValueSummary::GVFlags( |
229 | static_cast<GlobalValue::LinkageTypes>(FSum.Linkage), |
230 | static_cast<GlobalValue::VisibilityTypes>(FSum.Visibility), |
231 | FSum.NotEligibleToImport, FSum.Live, FSum.IsLocal, |
232 | FSum.CanAutoHide, |
233 | static_cast<GlobalValueSummary::ImportKind>(FSum.ImportType)), |
234 | /*NumInsts=*/args: 0, args: FunctionSummary::FFlags{}, /*EntryCount=*/args: 0, args&: Refs, |
235 | args: ArrayRef<FunctionSummary::EdgeTy>{}, args: std::move(FSum.TypeTests), |
236 | args: std::move(FSum.TypeTestAssumeVCalls), |
237 | args: std::move(FSum.TypeCheckedLoadVCalls), |
238 | args: std::move(FSum.TypeTestAssumeConstVCalls), |
239 | args: std::move(FSum.TypeCheckedLoadConstVCalls), |
240 | args: ArrayRef<FunctionSummary::ParamAccess>{}, args: ArrayRef<CallsiteInfo>{}, |
241 | args: ArrayRef<AllocInfo>{})); |
242 | } |
243 | } |
244 | static void output(IO &io, GlobalValueSummaryMapTy &V) { |
245 | for (auto &P : V) { |
246 | std::vector<FunctionSummaryYaml> FSums; |
247 | for (auto &Sum : P.second.SummaryList) { |
248 | if (auto *FSum = dyn_cast<FunctionSummary>(Val: Sum.get())) { |
249 | std::vector<uint64_t> Refs; |
250 | for (auto &VI : FSum->refs()) |
251 | Refs.push_back(x: VI.getGUID()); |
252 | FSums.push_back(x: FunctionSummaryYaml{ |
253 | .Linkage: FSum->flags().Linkage, .Visibility: FSum->flags().Visibility, |
254 | .NotEligibleToImport: static_cast<bool>(FSum->flags().NotEligibleToImport), |
255 | .Live: static_cast<bool>(FSum->flags().Live), |
256 | .IsLocal: static_cast<bool>(FSum->flags().DSOLocal), |
257 | .CanAutoHide: static_cast<bool>(FSum->flags().CanAutoHide), |
258 | .ImportType: FSum->flags().ImportType, .Refs: Refs, .TypeTests: FSum->type_tests(), |
259 | .TypeTestAssumeVCalls: FSum->type_test_assume_vcalls(), .TypeCheckedLoadVCalls: FSum->type_checked_load_vcalls(), |
260 | .TypeTestAssumeConstVCalls: FSum->type_test_assume_const_vcalls(), |
261 | .TypeCheckedLoadConstVCalls: FSum->type_checked_load_const_vcalls()}); |
262 | } |
263 | } |
264 | if (!FSums.empty()) |
265 | io.mapRequired(Key: llvm::utostr(X: P.first).c_str(), Val&: FSums); |
266 | } |
267 | } |
268 | }; |
269 | |
270 | template <> struct CustomMappingTraits<TypeIdSummaryMapTy> { |
271 | static void inputOne(IO &io, StringRef Key, TypeIdSummaryMapTy &V) { |
272 | TypeIdSummary TId; |
273 | io.mapRequired(Key: Key.str().c_str(), Val&: TId); |
274 | V.insert(x: {GlobalValue::getGUID(GlobalName: Key), {std::string(Key), TId}}); |
275 | } |
276 | static void output(IO &io, TypeIdSummaryMapTy &V) { |
277 | for (auto &TidIter : V) |
278 | io.mapRequired(Key: TidIter.second.first.c_str(), Val&: TidIter.second.second); |
279 | } |
280 | }; |
281 | |
282 | template <> struct MappingTraits<ModuleSummaryIndex> { |
283 | static void mapping(IO &io, ModuleSummaryIndex& index) { |
284 | io.mapOptional(Key: "GlobalValueMap" , Val&: index.GlobalValueMap); |
285 | io.mapOptional(Key: "TypeIdMap" , Val&: index.TypeIdMap); |
286 | io.mapOptional(Key: "WithGlobalValueDeadStripping" , |
287 | Val&: index.WithGlobalValueDeadStripping); |
288 | |
289 | if (io.outputting()) { |
290 | std::vector<std::string> CfiFunctionDefs(index.CfiFunctionDefs.begin(), |
291 | index.CfiFunctionDefs.end()); |
292 | io.mapOptional(Key: "CfiFunctionDefs" , Val&: CfiFunctionDefs); |
293 | std::vector<std::string> CfiFunctionDecls(index.CfiFunctionDecls.begin(), |
294 | index.CfiFunctionDecls.end()); |
295 | io.mapOptional(Key: "CfiFunctionDecls" , Val&: CfiFunctionDecls); |
296 | } else { |
297 | std::vector<std::string> CfiFunctionDefs; |
298 | io.mapOptional(Key: "CfiFunctionDefs" , Val&: CfiFunctionDefs); |
299 | index.CfiFunctionDefs = {CfiFunctionDefs.begin(), CfiFunctionDefs.end()}; |
300 | std::vector<std::string> CfiFunctionDecls; |
301 | io.mapOptional(Key: "CfiFunctionDecls" , Val&: CfiFunctionDecls); |
302 | index.CfiFunctionDecls = {CfiFunctionDecls.begin(), |
303 | CfiFunctionDecls.end()}; |
304 | } |
305 | } |
306 | }; |
307 | |
308 | } // End yaml namespace |
309 | } // End llvm namespace |
310 | |
311 | #endif |
312 | |