blob: e1e2df55699a7060bca9e341992375656bc35de3 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001/*===-- InstrProfData.inc - instr profiling runtime structures -*- C++ -*-=== *\
2|*
Andrew Walbran16937d02019-10-22 13:54:20 +01003|* 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006|*
7\*===----------------------------------------------------------------------===*/
8/*
9 * This is the master file that defines all the data structure, signature,
10 * constant literals that are shared across profiling runtime library,
11 * compiler (instrumentation), and host tools (reader/writer). The entities
12 * defined in this file affect the profile runtime ABI, the raw profile format,
13 * or both.
14 *
15 * The file has two identical copies. The master copy lives in LLVM and
16 * the other one sits in compiler-rt/lib/profile directory. To make changes
17 * in this file, first modify the master copy and copy it over to compiler-rt.
18 * Testing of any change in this file can start only after the two copies are
19 * synced up.
20 *
21 * The first part of the file includes macros that defines types, names, and
22 * initializers for the member fields of the core data structures. The field
23 * declarations for one structure is enabled by defining the field activation
24 * macro associated with that structure. Only one field activation record
25 * can be defined at one time and the rest definitions will be filtered out by
26 * the preprocessor.
27 *
28 * Examples of how the template is used to instantiate structure definition:
29 * 1. To declare a structure:
30 *
31 * struct ProfData {
32 * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
33 * Type Name;
34 * #include "llvm/ProfileData/InstrProfData.inc"
35 * };
36 *
37 * 2. To construct LLVM type arrays for the struct type:
38 *
39 * Type *DataTypes[] = {
40 * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
41 * LLVMType,
42 * #include "llvm/ProfileData/InstrProfData.inc"
43 * };
44 *
45 * 4. To construct constant array for the initializers:
46 * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
47 * Initializer,
48 * Constant *ConstantVals[] = {
49 * #include "llvm/ProfileData/InstrProfData.inc"
50 * };
51 *
52 *
53 * The second part of the file includes definitions all other entities that
54 * are related to runtime ABI and format. When no field activation macro is
55 * defined, this file can be included to introduce the definitions.
56 *
57\*===----------------------------------------------------------------------===*/
58
59/* Functions marked with INSTR_PROF_VISIBILITY must have hidden visibility in
60 * the compiler runtime. */
61#ifndef INSTR_PROF_VISIBILITY
62#define INSTR_PROF_VISIBILITY
63#endif
64
65/* INSTR_PROF_DATA start. */
66/* Definition of member fields of the per-function control structure. */
67#ifndef INSTR_PROF_DATA
68#define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)
69#else
70#define INSTR_PROF_DATA_DEFINED
71#endif
72INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
73 ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
74 IndexedInstrProf::ComputeHash(getPGOFuncNameVarInitializer(Inc->getName()))))
75INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
76 ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
77 Inc->getHash()->getZExtValue()))
78INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt64PtrTy(Ctx), CounterPtr, \
79 ConstantExpr::getBitCast(CounterPtr, \
80 llvm::Type::getInt64PtrTy(Ctx)))
81/* This is used to map function pointers for the indirect call targets to
82 * function name hashes during the conversion from raw to merged profile
83 * data.
84 */
85INSTR_PROF_DATA(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), FunctionPointer, \
86 FunctionAddr)
87INSTR_PROF_DATA(IntPtrT, llvm::Type::getInt8PtrTy(Ctx), Values, \
88 ValuesPtrExpr)
89INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
90 ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
91INSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \
92 ConstantArray::get(Int16ArrayTy, Int16ArrayVals))
93#undef INSTR_PROF_DATA
94/* INSTR_PROF_DATA end. */
95
96
97/* This is an internal data structure used by value profiler. It
98 * is defined here to allow serialization code sharing by LLVM
99 * to be used in unit test.
100 *
101 * typedef struct ValueProfNode {
102 * // InstrProfValueData VData;
103 * uint64_t Value;
104 * uint64_t Count;
105 * struct ValueProfNode *Next;
106 * } ValueProfNode;
107 */
108/* INSTR_PROF_VALUE_NODE start. */
109#ifndef INSTR_PROF_VALUE_NODE
110#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer)
111#else
112#define INSTR_PROF_DATA_DEFINED
113#endif
114INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Value, \
115 ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
116INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
117 ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
118INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::Type::getInt8PtrTy(Ctx), Next, \
119 ConstantInt::get(llvm::Type::GetInt8PtrTy(Ctx), 0))
120#undef INSTR_PROF_VALUE_NODE
121/* INSTR_PROF_VALUE_NODE end. */
122
123/* INSTR_PROF_RAW_HEADER start */
124/* Definition of member fields of the raw profile header data structure. */
125#ifndef INSTR_PROF_RAW_HEADER
126#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer)
127#else
128#define INSTR_PROF_DATA_DEFINED
129#endif
130INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
131INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
132INSTR_PROF_RAW_HEADER(uint64_t, DataSize, DataSize)
133INSTR_PROF_RAW_HEADER(uint64_t, CountersSize, CountersSize)
134INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
135INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
136INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
137INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
138#undef INSTR_PROF_RAW_HEADER
139/* INSTR_PROF_RAW_HEADER end */
140
141/* VALUE_PROF_FUNC_PARAM start */
142/* Definition of parameter types of the runtime API used to do value profiling
143 * for a given value site.
144 */
145#ifndef VALUE_PROF_FUNC_PARAM
146#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType)
147#define INSTR_PROF_COMMA
148#else
149#define INSTR_PROF_DATA_DEFINED
150#define INSTR_PROF_COMMA ,
151#endif
152VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
153 INSTR_PROF_COMMA
154VALUE_PROF_FUNC_PARAM(void *, Data, Type::getInt8PtrTy(Ctx)) INSTR_PROF_COMMA
155#ifndef VALUE_RANGE_PROF
156VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
157#else /* VALUE_RANGE_PROF */
158VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx)) \
159 INSTR_PROF_COMMA
160VALUE_PROF_FUNC_PARAM(uint64_t, PreciseRangeStart, Type::getInt64Ty(Ctx)) \
161 INSTR_PROF_COMMA
162VALUE_PROF_FUNC_PARAM(uint64_t, PreciseRangeLast, Type::getInt64Ty(Ctx)) \
163 INSTR_PROF_COMMA
164VALUE_PROF_FUNC_PARAM(uint64_t, LargeValue, Type::getInt64Ty(Ctx))
165#endif /*VALUE_RANGE_PROF */
166#undef VALUE_PROF_FUNC_PARAM
167#undef INSTR_PROF_COMMA
168/* VALUE_PROF_FUNC_PARAM end */
169
170/* VALUE_PROF_KIND start */
171#ifndef VALUE_PROF_KIND
172#define VALUE_PROF_KIND(Enumerator, Value)
173#else
174#define INSTR_PROF_DATA_DEFINED
175#endif
176/* For indirect function call value profiling, the addresses of the target
177 * functions are profiled by the instrumented code. The target addresses are
178 * written in the raw profile data and converted to target function name's MD5
179 * hash by the profile reader during deserialization. Typically, this happens
180 * when the raw profile data is read during profile merging.
181 *
182 * For this remapping the ProfData is used. ProfData contains both the function
183 * name hash and the function address.
184 */
185VALUE_PROF_KIND(IPVK_IndirectCallTarget, 0)
186/* For memory intrinsic functions size profiling. */
187VALUE_PROF_KIND(IPVK_MemOPSize, 1)
188/* These two kinds must be the last to be
189 * declared. This is to make sure the string
190 * array created with the template can be
191 * indexed with the kind value.
192 */
193VALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget)
194VALUE_PROF_KIND(IPVK_Last, IPVK_MemOPSize)
195
196#undef VALUE_PROF_KIND
197/* VALUE_PROF_KIND end */
198
199/* COVMAP_FUNC_RECORD start */
200/* Definition of member fields of the function record structure in coverage
201 * map.
202 */
203#ifndef COVMAP_FUNC_RECORD
204#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
205#else
206#define INSTR_PROF_DATA_DEFINED
207#endif
208#ifdef COVMAP_V1
209COVMAP_FUNC_RECORD(const IntPtrT, llvm::Type::getInt8PtrTy(Ctx), \
210 NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
211 llvm::Type::getInt8PtrTy(Ctx)))
212COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
213 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
214 NameValue.size()))
215#else
216COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
217 llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
218 llvm::IndexedInstrProf::ComputeHash(NameValue)))
219#endif
220COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
221 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx),\
222 CoverageMapping.size()))
223COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
224 llvm::ConstantInt::get(llvm::Type::getInt64Ty(Ctx), FuncHash))
225#undef COVMAP_FUNC_RECORD
226/* COVMAP_FUNC_RECORD end. */
227
228/* COVMAP_HEADER start */
229/* Definition of member fields of coverage map header.
230 */
231#ifndef COVMAP_HEADER
232#define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
233#else
234#define INSTR_PROF_DATA_DEFINED
235#endif
236COVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
237 llvm::ConstantInt::get(Int32Ty, FunctionRecords.size()))
238COVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
239 llvm::ConstantInt::get(Int32Ty, FilenamesSize))
240COVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
241 llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
242COVMAP_HEADER(uint32_t, Int32Ty, Version, \
243 llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
244#undef COVMAP_HEADER
245/* COVMAP_HEADER end. */
246
247
248#ifdef INSTR_PROF_SECT_ENTRY
249#define INSTR_PROF_DATA_DEFINED
250INSTR_PROF_SECT_ENTRY(IPSK_data, \
251 INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100252 INSTR_PROF_DATA_COFF, "__DATA,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100253INSTR_PROF_SECT_ENTRY(IPSK_cnts, \
254 INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100255 INSTR_PROF_CNTS_COFF, "__DATA,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100256INSTR_PROF_SECT_ENTRY(IPSK_name, \
257 INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100258 INSTR_PROF_NAME_COFF, "__DATA,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100259INSTR_PROF_SECT_ENTRY(IPSK_vals, \
260 INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100261 INSTR_PROF_VALS_COFF, "__DATA,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100262INSTR_PROF_SECT_ENTRY(IPSK_vnodes, \
263 INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100264 INSTR_PROF_VNODES_COFF, "__DATA,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100265INSTR_PROF_SECT_ENTRY(IPSK_covmap, \
266 INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
Andrew Walbran16937d02019-10-22 13:54:20 +0100267 INSTR_PROF_COVMAP_COFF, "__LLVM_COV,")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268
269#undef INSTR_PROF_SECT_ENTRY
270#endif
271
272
273#ifdef INSTR_PROF_VALUE_PROF_DATA
274#define INSTR_PROF_DATA_DEFINED
275
276#define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
277/*!
278 * This is the header of the data structure that defines the on-disk
279 * layout of the value profile data of a particular kind for one function.
280 */
281typedef struct ValueProfRecord {
282 /* The kind of the value profile record. */
283 uint32_t Kind;
284 /*
285 * The number of value profile sites. It is guaranteed to be non-zero;
286 * otherwise the record for this kind won't be emitted.
287 */
288 uint32_t NumValueSites;
289 /*
290 * The first element of the array that stores the number of profiled
291 * values for each value site. The size of the array is NumValueSites.
292 * Since NumValueSites is greater than zero, there is at least one
293 * element in the array.
294 */
295 uint8_t SiteCountArray[1];
296
297 /*
298 * The fake declaration is for documentation purpose only.
299 * Align the start of next field to be on 8 byte boundaries.
300 uint8_t Padding[X];
301 */
302
303 /* The array of value profile data. The size of the array is the sum
304 * of all elements in SiteCountArray[].
305 InstrProfValueData ValueData[];
306 */
307
308#ifdef __cplusplus
309 /*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100310 * Return the number of value sites.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100311 */
312 uint32_t getNumValueSites() const { return NumValueSites; }
313 /*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100314 * Read data from this record and save it to Record.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100315 */
316 void deserializeTo(InstrProfRecord &Record,
317 InstrProfSymtab *SymTab);
318 /*
319 * In-place byte swap:
320 * Do byte swap for this instance. \c Old is the original order before
321 * the swap, and \c New is the New byte order.
322 */
323 void swapBytes(support::endianness Old, support::endianness New);
324#endif
325} ValueProfRecord;
326
327/*!
328 * Per-function header/control data structure for value profiling
329 * data in indexed format.
330 */
331typedef struct ValueProfData {
332 /*
333 * Total size in bytes including this field. It must be a multiple
334 * of sizeof(uint64_t).
335 */
336 uint32_t TotalSize;
337 /*
338 *The number of value profile kinds that has value profile data.
339 * In this implementation, a value profile kind is considered to
340 * have profile data if the number of value profile sites for the
341 * kind is not zero. More aggressively, the implementation can
342 * choose to check the actual data value: if none of the value sites
343 * has any profiled values, the kind can be skipped.
344 */
345 uint32_t NumValueKinds;
346
347 /*
348 * Following are a sequence of variable length records. The prefix/header
349 * of each record is defined by ValueProfRecord type. The number of
350 * records is NumValueKinds.
351 * ValueProfRecord Record_1;
352 * ValueProfRecord Record_N;
353 */
354
355#if __cplusplus
356 /*!
357 * Return the total size in bytes of the on-disk value profile data
358 * given the data stored in Record.
359 */
360 static uint32_t getSize(const InstrProfRecord &Record);
361 /*!
362 * Return a pointer to \c ValueProfData instance ready to be streamed.
363 */
364 static std::unique_ptr<ValueProfData>
365 serializeFrom(const InstrProfRecord &Record);
366 /*!
367 * Check the integrity of the record.
368 */
369 Error checkIntegrity();
370 /*!
371 * Return a pointer to \c ValueProfileData instance ready to be read.
372 * All data in the instance are properly byte swapped. The input
373 * data is assumed to be in little endian order.
374 */
375 static Expected<std::unique_ptr<ValueProfData>>
376 getValueProfData(const unsigned char *SrcBuffer,
377 const unsigned char *const SrcBufferEnd,
378 support::endianness SrcDataEndianness);
379 /*!
380 * Swap byte order from \c Endianness order to host byte order.
381 */
382 void swapBytesToHost(support::endianness Endianness);
383 /*!
384 * Swap byte order from host byte order to \c Endianness order.
385 */
386 void swapBytesFromHost(support::endianness Endianness);
387 /*!
388 * Return the total size of \c ValueProfileData.
389 */
390 uint32_t getSize() const { return TotalSize; }
391 /*!
392 * Read data from this data and save it to \c Record.
393 */
394 void deserializeTo(InstrProfRecord &Record,
395 InstrProfSymtab *SymTab);
396 void operator delete(void *ptr) { ::operator delete(ptr); }
397#endif
398} ValueProfData;
399
400/*
401 * The closure is designed to abstact away two types of value profile data:
402 * - InstrProfRecord which is the primary data structure used to
403 * represent profile data in host tools (reader, writer, and profile-use)
404 * - value profile runtime data structure suitable to be used by C
405 * runtime library.
406 *
407 * Both sources of data need to serialize to disk/memory-buffer in common
408 * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
409 * writer to share the same format and code with indexed profile writer.
410 *
411 * For documentation of the member methods below, refer to corresponding methods
412 * in class InstrProfRecord.
413 */
414typedef struct ValueProfRecordClosure {
415 const void *Record;
416 uint32_t (*GetNumValueKinds)(const void *Record);
417 uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
418 uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
419 uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
420
421 /*
422 * After extracting the value profile data from the value profile record,
423 * this method is used to map the in-memory value to on-disk value. If
424 * the method is null, value will be written out untranslated.
425 */
426 uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
427 void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
428 uint32_t S);
429 ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
430} ValueProfRecordClosure;
431
432INSTR_PROF_VISIBILITY ValueProfRecord *
433getFirstValueProfRecord(ValueProfData *VPD);
434INSTR_PROF_VISIBILITY ValueProfRecord *
435getValueProfRecordNext(ValueProfRecord *VPR);
436INSTR_PROF_VISIBILITY InstrProfValueData *
437getValueProfRecordValueData(ValueProfRecord *VPR);
438INSTR_PROF_VISIBILITY uint32_t
439getValueProfRecordHeaderSize(uint32_t NumValueSites);
440
441#undef INSTR_PROF_VALUE_PROF_DATA
442#endif /* INSTR_PROF_VALUE_PROF_DATA */
443
444
445#ifdef INSTR_PROF_COMMON_API_IMPL
446#define INSTR_PROF_DATA_DEFINED
447#ifdef __cplusplus
448#define INSTR_PROF_INLINE inline
449#define INSTR_PROF_NULLPTR nullptr
450#else
451#define INSTR_PROF_INLINE
452#define INSTR_PROF_NULLPTR NULL
453#endif
454
455#ifndef offsetof
456#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
457#endif
458
459/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100460 * Return the \c ValueProfRecord header size including the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100461 * padding bytes.
462 */
463INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
464uint32_t getValueProfRecordHeaderSize(uint32_t NumValueSites) {
465 uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
466 sizeof(uint8_t) * NumValueSites;
467 /* Round the size to multiple of 8 bytes. */
468 Size = (Size + 7) & ~7;
469 return Size;
470}
471
472/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100473 * Return the total size of the value profile record including the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100474 * header and the value data.
475 */
476INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
477uint32_t getValueProfRecordSize(uint32_t NumValueSites,
478 uint32_t NumValueData) {
479 return getValueProfRecordHeaderSize(NumValueSites) +
480 sizeof(InstrProfValueData) * NumValueData;
481}
482
483/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100484 * Return the pointer to the start of value data array.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100485 */
486INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
487InstrProfValueData *getValueProfRecordValueData(ValueProfRecord *This) {
488 return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
489 This->NumValueSites));
490}
491
492/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100493 * Return the total number of value data for \c This record.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100494 */
495INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
496uint32_t getValueProfRecordNumValueData(ValueProfRecord *This) {
497 uint32_t NumValueData = 0;
498 uint32_t I;
499 for (I = 0; I < This->NumValueSites; I++)
500 NumValueData += This->SiteCountArray[I];
501 return NumValueData;
502}
503
504/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100505 * Use this method to advance to the next \c This \c ValueProfRecord.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100506 */
507INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
508ValueProfRecord *getValueProfRecordNext(ValueProfRecord *This) {
509 uint32_t NumValueData = getValueProfRecordNumValueData(This);
510 return (ValueProfRecord *)((char *)This +
511 getValueProfRecordSize(This->NumValueSites,
512 NumValueData));
513}
514
515/*!
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100516 * Return the first \c ValueProfRecord instance.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100517 */
518INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
519ValueProfRecord *getFirstValueProfRecord(ValueProfData *This) {
520 return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
521}
522
523/* Closure based interfaces. */
524
525/*!
526 * Return the total size in bytes of the on-disk value profile data
527 * given the data stored in Record.
528 */
529INSTR_PROF_VISIBILITY uint32_t
530getValueProfDataSize(ValueProfRecordClosure *Closure) {
531 uint32_t Kind;
532 uint32_t TotalSize = sizeof(ValueProfData);
533 const void *Record = Closure->Record;
534
535 for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
536 uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
537 if (!NumValueSites)
538 continue;
539 TotalSize += getValueProfRecordSize(NumValueSites,
540 Closure->GetNumValueData(Record, Kind));
541 }
542 return TotalSize;
543}
544
545/*!
546 * Extract value profile data of a function for the profile kind \c ValueKind
547 * from the \c Closure and serialize the data into \c This record instance.
548 */
549INSTR_PROF_VISIBILITY void
550serializeValueProfRecordFrom(ValueProfRecord *This,
551 ValueProfRecordClosure *Closure,
552 uint32_t ValueKind, uint32_t NumValueSites) {
553 uint32_t S;
554 const void *Record = Closure->Record;
555 This->Kind = ValueKind;
556 This->NumValueSites = NumValueSites;
557 InstrProfValueData *DstVD = getValueProfRecordValueData(This);
558
559 for (S = 0; S < NumValueSites; S++) {
560 uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
561 This->SiteCountArray[S] = ND;
562 Closure->GetValueForSite(Record, DstVD, ValueKind, S);
563 DstVD += ND;
564 }
565}
566
567/*!
568 * Extract value profile data of a function from the \c Closure
569 * and serialize the data into \c DstData if it is not NULL or heap
570 * memory allocated by the \c Closure's allocator method. If \c
571 * DstData is not null, the caller is expected to set the TotalSize
572 * in DstData.
573 */
574INSTR_PROF_VISIBILITY ValueProfData *
575serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
576 ValueProfData *DstData) {
577 uint32_t Kind;
578 uint32_t TotalSize =
579 DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
580
581 ValueProfData *VPD =
582 DstData ? DstData : Closure->AllocValueProfData(TotalSize);
583
584 VPD->TotalSize = TotalSize;
585 VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
586 ValueProfRecord *VR = getFirstValueProfRecord(VPD);
587 for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
588 uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
589 if (!NumValueSites)
590 continue;
591 serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
592 VR = getValueProfRecordNext(VR);
593 }
594 return VPD;
595}
596
597#undef INSTR_PROF_COMMON_API_IMPL
598#endif /* INSTR_PROF_COMMON_API_IMPL */
599
600/*============================================================================*/
601
602#ifndef INSTR_PROF_DATA_DEFINED
603
604#ifndef INSTR_PROF_DATA_INC
605#define INSTR_PROF_DATA_INC
606
607/* Helper macros. */
608#define INSTR_PROF_SIMPLE_QUOTE(x) #x
609#define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
610#define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
611#define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
612
613/* Magic number to detect file format and endianness.
614 * Use 255 at one end, since no UTF-8 file can use that character. Avoid 0,
615 * so that utilities, like strings, don't grab it as a string. 129 is also
616 * invalid UTF-8, and high enough to be interesting.
617 * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
618 * for 32-bit platforms.
619 */
620#define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
621 (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
622 (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
623#define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
624 (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
625 (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
626
627/* Raw profile format version (start from 1). */
628#define INSTR_PROF_RAW_VERSION 4
629/* Indexed profile format version (start from 1). */
630#define INSTR_PROF_INDEX_VERSION 5
631/* Coverage mapping format vresion (start from 0). */
632#define INSTR_PROF_COVMAP_VERSION 2
633
634/* Profile version is always of type uint64_t. Reserve the upper 8 bits in the
635 * version for other variants of profile. We set the lowest bit of the upper 8
636 * bits (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentaiton
637 * generated profile, and 0 if this is a Clang FE generated profile.
638 */
639#define VARIANT_MASKS_ALL 0xff00000000000000ULL
640#define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
641#define VARIANT_MASK_IR_PROF (0x1ULL << 56)
642#define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
643#define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
644
645/* The variable that holds the name of the profile data
646 * specified via command line. */
647#define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
648
649/* section name strings common to all targets other
650 than WIN32 */
651#define INSTR_PROF_DATA_COMMON __llvm_prf_data
652#define INSTR_PROF_NAME_COMMON __llvm_prf_names
653#define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
654#define INSTR_PROF_VALS_COMMON __llvm_prf_vals
655#define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
656#define INSTR_PROF_COVMAP_COMMON __llvm_covmap
Andrew Walbran16937d02019-10-22 13:54:20 +0100657/* Windows section names. Because these section names contain dollar characters,
658 * they must be quoted.
659 */
660#define INSTR_PROF_DATA_COFF ".lprfd$M"
661#define INSTR_PROF_NAME_COFF ".lprfn$M"
662#define INSTR_PROF_CNTS_COFF ".lprfc$M"
663#define INSTR_PROF_VALS_COFF ".lprfv$M"
664#define INSTR_PROF_VNODES_COFF ".lprfnd$M"
665#define INSTR_PROF_COVMAP_COFF ".lcovmap$M"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100666
667#ifdef _WIN32
668/* Runtime section names and name strings. */
669#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
670#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
671#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
672/* Array of pointers. Each pointer points to a list
673 * of value nodes associated with one value site.
674 */
675#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
676/* Value profile nodes section. */
677#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
678#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
679#else
680/* Runtime section names and name strings. */
Andrew Walbran16937d02019-10-22 13:54:20 +0100681#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON)
682#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON)
683#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100684/* Array of pointers. Each pointer points to a list
685 * of value nodes associated with one value site.
686 */
Andrew Walbran16937d02019-10-22 13:54:20 +0100687#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100688/* Value profile nodes section. */
Andrew Walbran16937d02019-10-22 13:54:20 +0100689#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON)
690#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100691#endif
692
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100693/* Macros to define start/stop section symbol for a given
694 * section on Linux. For instance
695 * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
696 * expand to __start___llvm_prof_data
697 */
698#define INSTR_PROF_SECT_START(Sect) \
699 INSTR_PROF_CONCAT(__start_,Sect)
700#define INSTR_PROF_SECT_STOP(Sect) \
701 INSTR_PROF_CONCAT(__stop_,Sect)
702
703/* Value Profiling API linkage name. */
704#define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
705#define INSTR_PROF_VALUE_PROF_FUNC_STR \
706 INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
707#define INSTR_PROF_VALUE_RANGE_PROF_FUNC __llvm_profile_instrument_range
708#define INSTR_PROF_VALUE_RANGE_PROF_FUNC_STR \
709 INSTR_PROF_QUOTE(INSTR_PROF_VALUE_RANGE_PROF_FUNC)
710
711/* InstrProfile per-function control data alignment. */
712#define INSTR_PROF_DATA_ALIGNMENT 8
713
714/* The data structure that represents a tracked value by the
715 * value profiler.
716 */
717typedef struct InstrProfValueData {
718 /* Profiled value. */
719 uint64_t Value;
720 /* Number of times the value appears in the training run. */
721 uint64_t Count;
722} InstrProfValueData;
723
724#endif /* INSTR_PROF_DATA_INC */
725
726#else
727#undef INSTR_PROF_DATA_DEFINED
728#endif