LLVM 18.0.0git
InstrProfData.inc
Go to the documentation of this file.
1/*===-- InstrProfData.inc - instr profiling runtime structures -*- 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 * This is the main 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 primary 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 primary 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
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, IntPtrTy, CounterPtr, RelativeCounterPtr)
79INSTR_PROF_DATA(const IntPtrT, IntPtrTy, BitmapPtr, RelativeBitmapPtr)
80/* This is used to map function pointers for the indirect call targets to
81 * function name hashes during the conversion from raw to merged profile
82 * data.
83 */
84INSTR_PROF_DATA(const IntPtrT, llvm::PointerType::getUnqual(Ctx), FunctionPointer, \
85 FunctionAddr)
86INSTR_PROF_DATA(IntPtrT, llvm::PointerType::getUnqual(Ctx), Values, \
87 ValuesPtrExpr)
88INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
89 ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
90INSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \
91 ConstantArray::get(Int16ArrayTy, Int16ArrayVals)) \
92INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumBitmapBytes, \
93 ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumBitmapBytes))
94#undef INSTR_PROF_DATA
95/* INSTR_PROF_DATA end. */
96
97
98/* This is an internal data structure used by value profiler. It
99 * is defined here to allow serialization code sharing by LLVM
100 * to be used in unit test.
101 *
102 * typedef struct ValueProfNode {
103 * // InstrProfValueData VData;
104 * uint64_t Value;
105 * uint64_t Count;
106 * struct ValueProfNode *Next;
107 * } ValueProfNode;
108 */
109/* INSTR_PROF_VALUE_NODE start. */
110#ifndef INSTR_PROF_VALUE_NODE
111#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer)
112#else
113#define INSTR_PROF_DATA_DEFINED
114#endif
116 ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
117INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
118 ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
119INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::PointerType::getUnqual(Ctx), Next, \
120 ConstantInt::get(llvm::Type::GetInt8PtrTy(Ctx), 0))
121#undef INSTR_PROF_VALUE_NODE
122/* INSTR_PROF_VALUE_NODE end. */
123
124/* INSTR_PROF_RAW_HEADER start */
125/* Definition of member fields of the raw profile header data structure. */
126#ifndef INSTR_PROF_RAW_HEADER
127#define INSTR_PROF_RAW_HEADER(Type, Name, Initializer)
128#else
129#define INSTR_PROF_DATA_DEFINED
130#endif
131INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
132INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
133INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
134INSTR_PROF_RAW_HEADER(uint64_t, NumData, NumData)
135INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
136INSTR_PROF_RAW_HEADER(uint64_t, NumCounters, NumCounters)
137INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterCounters, PaddingBytesAfterCounters)
138INSTR_PROF_RAW_HEADER(uint64_t, NumBitmapBytes, NumBitmapBytes)
139INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterBitmapBytes, PaddingBytesAfterBitmapBytes)
140INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
141INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta,
142 (uintptr_t)CountersBegin - (uintptr_t)DataBegin)
144 (uintptr_t)BitmapBegin - (uintptr_t)DataBegin)
145INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
146INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
147#undef INSTR_PROF_RAW_HEADER
148/* INSTR_PROF_RAW_HEADER end */
149
150/* VALUE_PROF_FUNC_PARAM start */
151/* Definition of parameter types of the runtime API used to do value profiling
152 * for a given value site.
153 */
154#ifndef VALUE_PROF_FUNC_PARAM
155#define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType)
156#define INSTR_PROF_COMMA
157#else
158#define INSTR_PROF_DATA_DEFINED
159#define INSTR_PROF_COMMA ,
160#endif
161VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
162 INSTR_PROF_COMMA
163VALUE_PROF_FUNC_PARAM(void *, Data, PointerType::getUnqual(Ctx)) INSTR_PROF_COMMA
164VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
165#undef VALUE_PROF_FUNC_PARAM
166#undef INSTR_PROF_COMMA
167/* VALUE_PROF_FUNC_PARAM end */
168
169/* VALUE_PROF_KIND start */
170#ifndef VALUE_PROF_KIND
171#define VALUE_PROF_KIND(Enumerator, Value, Descr)
172#else
173#define INSTR_PROF_DATA_DEFINED
174#endif
175/* For indirect function call value profiling, the addresses of the target
176 * functions are profiled by the instrumented code. The target addresses are
177 * written in the raw profile data and converted to target function name's MD5
178 * hash by the profile reader during deserialization. Typically, this happens
179 * when the raw profile data is read during profile merging.
180 *
181 * For this remapping the ProfData is used. ProfData contains both the function
182 * name hash and the function address.
183 */
184VALUE_PROF_KIND(IPVK_IndirectCallTarget, 0, "indirect call target")
185/* For memory intrinsic functions size profiling. */
186VALUE_PROF_KIND(IPVK_MemOPSize, 1, "memory intrinsic functions size")
187/* These two kinds must be the last to be
188 * declared. This is to make sure the string
189 * array created with the template can be
190 * indexed with the kind value.
191 */
192VALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget, "first")
193VALUE_PROF_KIND(IPVK_Last, IPVK_MemOPSize, "last")
194
195#undef VALUE_PROF_KIND
196/* VALUE_PROF_KIND end */
197
198#undef COVMAP_V2_OR_V3
199#ifdef COVMAP_V2
200#define COVMAP_V2_OR_V3
201#endif
202#ifdef COVMAP_V3
203#define COVMAP_V2_OR_V3
204#endif
205
206/* COVMAP_FUNC_RECORD start */
207/* Definition of member fields of the function record structure in coverage
208 * map.
209 */
210#ifndef COVMAP_FUNC_RECORD
211#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
212#else
213#define INSTR_PROF_DATA_DEFINED
214#endif
215#ifdef COVMAP_V1
217 NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
221 NameValue.size()))
222#endif
223#ifdef COVMAP_V2_OR_V3
224COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
226 llvm::Type::getInt64Ty(Ctx), NameHash))
227#endif
230 llvm::Type::getInt32Ty(Ctx), CoverageMapping.size()))
231COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
232 llvm::ConstantInt::get( \
233 llvm::Type::getInt64Ty(Ctx), FuncHash))
234#ifdef COVMAP_V3
235COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FilenamesRef, \
237 llvm::Type::getInt64Ty(Ctx), FilenamesRef))
238COVMAP_FUNC_RECORD(const char, \
240 CoverageMapping.size()), \
241 CoverageMapping,
243 CoverageMapping, CoverageMapping.size(), \
245#endif
246#undef COVMAP_FUNC_RECORD
247/* COVMAP_FUNC_RECORD end. */
248
249/* COVMAP_HEADER start */
250/* Definition of member fields of coverage map header.
251 */
252#ifndef COVMAP_HEADER
253#define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
254#else
255#define INSTR_PROF_DATA_DEFINED
256#endif
257COVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
259COVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
260 llvm::ConstantInt::get(Int32Ty, FilenamesSize))
261COVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
262 llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
263COVMAP_HEADER(uint32_t, Int32Ty, Version, \
264 llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
265#undef COVMAP_HEADER
266/* COVMAP_HEADER end. */
267
268
269#ifdef INSTR_PROF_SECT_ENTRY
270#define INSTR_PROF_DATA_DEFINED
271INSTR_PROF_SECT_ENTRY(IPSK_data, \
272 INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
273 INSTR_PROF_DATA_COFF, "__DATA,")
274INSTR_PROF_SECT_ENTRY(IPSK_cnts, \
275 INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
276 INSTR_PROF_CNTS_COFF, "__DATA,")
277INSTR_PROF_SECT_ENTRY(IPSK_bitmap, \
278 INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON), \
279 INSTR_PROF_BITS_COFF, "__DATA,")
280INSTR_PROF_SECT_ENTRY(IPSK_name, \
281 INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
282 INSTR_PROF_NAME_COFF, "__DATA,")
283INSTR_PROF_SECT_ENTRY(IPSK_vals, \
284 INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
285 INSTR_PROF_VALS_COFF, "__DATA,")
286INSTR_PROF_SECT_ENTRY(IPSK_vnodes, \
287 INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
288 INSTR_PROF_VNODES_COFF, "__DATA,")
289INSTR_PROF_SECT_ENTRY(IPSK_covmap, \
290 INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
291 INSTR_PROF_COVMAP_COFF, "__LLVM_COV,")
292INSTR_PROF_SECT_ENTRY(IPSK_covfun, \
293 INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON), \
294 INSTR_PROF_COVFUN_COFF, "__LLVM_COV,")
295INSTR_PROF_SECT_ENTRY(IPSK_orderfile, \
296 INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON), \
297 INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COFF), "__DATA,")
298
299#undef INSTR_PROF_SECT_ENTRY
300#endif
301
302
303#ifdef INSTR_PROF_VALUE_PROF_DATA
304#define INSTR_PROF_DATA_DEFINED
305
306#define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
307/*!
308 * This is the header of the data structure that defines the on-disk
309 * layout of the value profile data of a particular kind for one function.
310 */
311typedef struct ValueProfRecord {
312 /* The kind of the value profile record. */
314 /*
315 * The number of value profile sites. It is guaranteed to be non-zero;
316 * otherwise the record for this kind won't be emitted.
317 */
318 uint32_t NumValueSites;
319 /*
320 * The first element of the array that stores the number of profiled
321 * values for each value site. The size of the array is NumValueSites.
322 * Since NumValueSites is greater than zero, there is at least one
323 * element in the array.
324 */
325 uint8_t SiteCountArray[1];
326
327 /*
328 * The fake declaration is for documentation purpose only.
329 * Align the start of next field to be on 8 byte boundaries.
330 uint8_t Padding[X];
331 */
332
333 /* The array of value profile data. The size of the array is the sum
334 * of all elements in SiteCountArray[].
335 InstrProfValueData ValueData[];
336 */
337
338#ifdef __cplusplus
339 /*!
340 * Return the number of value sites.
341 */
342 uint32_t getNumValueSites() const { return NumValueSites; }
343 /*!
344 * Read data from this record and save it to Record.
345 */
346 void deserializeTo(InstrProfRecord &Record,
347 InstrProfSymtab *SymTab);
348 /*
349 * In-place byte swap:
350 * Do byte swap for this instance. \c Old is the original order before
351 * the swap, and \c New is the New byte order.
352 */
354#endif
355} ValueProfRecord;
356
357/*!
358 * Per-function header/control data structure for value profiling
359 * data in indexed format.
360 */
361typedef struct ValueProfData {
362 /*
363 * Total size in bytes including this field. It must be a multiple
364 * of sizeof(uint64_t).
365 */
366 uint32_t TotalSize;
367 /*
368 *The number of value profile kinds that has value profile data.
369 * In this implementation, a value profile kind is considered to
370 * have profile data if the number of value profile sites for the
371 * kind is not zero. More aggressively, the implementation can
372 * choose to check the actual data value: if none of the value sites
373 * has any profiled values, the kind can be skipped.
374 */
375 uint32_t NumValueKinds;
376
377 /*
378 * Following are a sequence of variable length records. The prefix/header
379 * of each record is defined by ValueProfRecord type. The number of
380 * records is NumValueKinds.
381 * ValueProfRecord Record_1;
382 * ValueProfRecord Record_N;
383 */
384
385#if __cplusplus
386 /*!
387 * Return the total size in bytes of the on-disk value profile data
388 * given the data stored in Record.
389 */
390 static uint32_t getSize(const InstrProfRecord &Record);
391 /*!
392 * Return a pointer to \c ValueProfData instance ready to be streamed.
393 */
394 static std::unique_ptr<ValueProfData>
395 serializeFrom(const InstrProfRecord &Record);
396 /*!
397 * Check the integrity of the record.
398 */
399 Error checkIntegrity();
400 /*!
401 * Return a pointer to \c ValueProfileData instance ready to be read.
402 * All data in the instance are properly byte swapped. The input
403 * data is assumed to be in little endian order.
404 */
405 static Expected<std::unique_ptr<ValueProfData>>
406 getValueProfData(const unsigned char *SrcBuffer,
407 const unsigned char *const SrcBufferEnd,
408 llvm::endianness SrcDataEndianness);
409 /*!
410 * Swap byte order from \c Endianness order to host byte order.
411 */
412 void swapBytesToHost(llvm::endianness Endianness);
413 /*!
414 * Swap byte order from host byte order to \c Endianness order.
415 */
416 void swapBytesFromHost(llvm::endianness Endianness);
417 /*!
418 * Return the total size of \c ValueProfileData.
419 */
420 uint32_t getSize() const { return TotalSize; }
421 /*!
422 * Read data from this data and save it to \c Record.
423 */
424 void deserializeTo(InstrProfRecord &Record,
425 InstrProfSymtab *SymTab);
426 void operator delete(void *ptr) { ::operator delete(ptr); }
427#endif
428} ValueProfData;
429
430/*
431 * The closure is designed to abstact away two types of value profile data:
432 * - InstrProfRecord which is the primary data structure used to
433 * represent profile data in host tools (reader, writer, and profile-use)
434 * - value profile runtime data structure suitable to be used by C
435 * runtime library.
436 *
437 * Both sources of data need to serialize to disk/memory-buffer in common
438 * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
439 * writer to share the same format and code with indexed profile writer.
440 *
441 * For documentation of the member methods below, refer to corresponding methods
442 * in class InstrProfRecord.
443 */
444typedef struct ValueProfRecordClosure {
445 const void *Record;
446 uint32_t (*GetNumValueKinds)(const void *Record);
447 uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
448 uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
449 uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
450
451 /*
452 * After extracting the value profile data from the value profile record,
453 * this method is used to map the in-memory value to on-disk value. If
454 * the method is null, value will be written out untranslated.
455 */
456 uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
457 void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
458 uint32_t S);
459 ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
460} ValueProfRecordClosure;
461
462INSTR_PROF_VISIBILITY ValueProfRecord *
463getFirstValueProfRecord(ValueProfData *VPD);
464INSTR_PROF_VISIBILITY ValueProfRecord *
465getValueProfRecordNext(ValueProfRecord *VPR);
466INSTR_PROF_VISIBILITY InstrProfValueData *
467getValueProfRecordValueData(ValueProfRecord *VPR);
468INSTR_PROF_VISIBILITY uint32_t
469getValueProfRecordHeaderSize(uint32_t NumValueSites);
470
471#undef INSTR_PROF_VALUE_PROF_DATA
472#endif /* INSTR_PROF_VALUE_PROF_DATA */
473
474
475#ifdef INSTR_PROF_COMMON_API_IMPL
476#define INSTR_PROF_DATA_DEFINED
477#ifdef __cplusplus
478#define INSTR_PROF_INLINE inline
479#define INSTR_PROF_NULLPTR nullptr
480#else
481#define INSTR_PROF_INLINE
482#define INSTR_PROF_NULLPTR NULL
483#endif
484
485#ifndef offsetof
486#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
487#endif
488
489/*!
490 * Return the \c ValueProfRecord header size including the
491 * padding bytes.
492 */
493INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
494uint32_t getValueProfRecordHeaderSize(uint32_t NumValueSites) {
495 uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
496 sizeof(uint8_t) * NumValueSites;
497 /* Round the size to multiple of 8 bytes. */
498 Size = (Size + 7) & ~7;
499 return Size;
500}
501
502/*!
503 * Return the total size of the value profile record including the
504 * header and the value data.
505 */
506INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
507uint32_t getValueProfRecordSize(uint32_t NumValueSites,
508 uint32_t NumValueData) {
509 return getValueProfRecordHeaderSize(NumValueSites) +
510 sizeof(InstrProfValueData) * NumValueData;
511}
512
513/*!
514 * Return the pointer to the start of value data array.
515 */
516INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
517InstrProfValueData *getValueProfRecordValueData(ValueProfRecord *This) {
518 return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
519 This->NumValueSites));
520}
521
522/*!
523 * Return the total number of value data for \c This record.
524 */
525INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
526uint32_t getValueProfRecordNumValueData(ValueProfRecord *This) {
527 uint32_t NumValueData = 0;
528 uint32_t I;
529 for (I = 0; I < This->NumValueSites; I++)
530 NumValueData += This->SiteCountArray[I];
531 return NumValueData;
532}
533
534/*!
535 * Use this method to advance to the next \c This \c ValueProfRecord.
536 */
537INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
538ValueProfRecord *getValueProfRecordNext(ValueProfRecord *This) {
539 uint32_t NumValueData = getValueProfRecordNumValueData(This);
540 return (ValueProfRecord *)((char *)This +
541 getValueProfRecordSize(This->NumValueSites,
542 NumValueData));
543}
544
545/*!
546 * Return the first \c ValueProfRecord instance.
547 */
548INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
549ValueProfRecord *getFirstValueProfRecord(ValueProfData *This) {
550 return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
551}
552
553/* Closure based interfaces. */
554
555/*!
556 * Return the total size in bytes of the on-disk value profile data
557 * given the data stored in Record.
558 */
559INSTR_PROF_VISIBILITY uint32_t
560getValueProfDataSize(ValueProfRecordClosure *Closure) {
562 uint32_t TotalSize = sizeof(ValueProfData);
563 const void *Record = Closure->Record;
564
565 for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
566 uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
567 if (!NumValueSites)
568 continue;
569 TotalSize += getValueProfRecordSize(NumValueSites,
570 Closure->GetNumValueData(Record, Kind));
571 }
572 return TotalSize;
573}
574
575/*!
576 * Extract value profile data of a function for the profile kind \c ValueKind
577 * from the \c Closure and serialize the data into \c This record instance.
578 */
579INSTR_PROF_VISIBILITY void
580serializeValueProfRecordFrom(ValueProfRecord *This,
581 ValueProfRecordClosure *Closure,
582 uint32_t ValueKind, uint32_t NumValueSites) {
583 uint32_t S;
584 const void *Record = Closure->Record;
585 This->Kind = ValueKind;
586 This->NumValueSites = NumValueSites;
587 InstrProfValueData *DstVD = getValueProfRecordValueData(This);
588
589 for (S = 0; S < NumValueSites; S++) {
590 uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
591 This->SiteCountArray[S] = ND;
592 Closure->GetValueForSite(Record, DstVD, ValueKind, S);
593 DstVD += ND;
594 }
595}
596
597/*!
598 * Extract value profile data of a function from the \c Closure
599 * and serialize the data into \c DstData if it is not NULL or heap
600 * memory allocated by the \c Closure's allocator method. If \c
601 * DstData is not null, the caller is expected to set the TotalSize
602 * in DstData.
603 */
604INSTR_PROF_VISIBILITY ValueProfData *
605serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
606 ValueProfData *DstData) {
608 uint32_t TotalSize =
609 DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
610
611 ValueProfData *VPD =
612 DstData ? DstData : Closure->AllocValueProfData(TotalSize);
613
614 VPD->TotalSize = TotalSize;
615 VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
616 ValueProfRecord *VR = getFirstValueProfRecord(VPD);
617 for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
618 uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
619 if (!NumValueSites)
620 continue;
621 serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
622 VR = getValueProfRecordNext(VR);
623 }
624 return VPD;
625}
626
627#undef INSTR_PROF_COMMON_API_IMPL
628#endif /* INSTR_PROF_COMMON_API_IMPL */
629
630/*============================================================================*/
631
632#ifndef INSTR_PROF_DATA_DEFINED
633
634#ifndef INSTR_PROF_DATA_INC
635#define INSTR_PROF_DATA_INC
636
637/* Helper macros. */
638#define INSTR_PROF_SIMPLE_QUOTE(x) #x
639#define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
640#define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
641#define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
642
643/* Magic number to detect file format and endianness.
644 * Use 255 at one end, since no UTF-8 file can use that character. Avoid 0,
645 * so that utilities, like strings, don't grab it as a string. 129 is also
646 * invalid UTF-8, and high enough to be interesting.
647 * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
648 * for 32-bit platforms.
649 */
650#define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
651 (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
652 (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
653#define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
654 (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
655 (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
656
657/* Raw profile format version (start from 1). */
658#define INSTR_PROF_RAW_VERSION 9
659/* Indexed profile format version (start from 1). */
660#define INSTR_PROF_INDEX_VERSION 11
661/* Coverage mapping format version (start from 0). */
662#define INSTR_PROF_COVMAP_VERSION 6
663
664/* Profile version is always of type uint64_t. Reserve the upper 32 bits in the
665 * version for other variants of profile. We set the 8th most significant bit
666 * (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentation
667 * generated profile, and 0 if this is a Clang FE generated profile.
668 * 1 in bit 57 indicates there are context-sensitive records in the profile.
669 * The 59th bit indicates whether to use debug info to correlate profiles.
670 * The 60th bit indicates single byte coverage instrumentation.
671 * The 61st bit indicates function entry instrumentation only.
672 * The 62nd bit indicates whether memory profile information is present.
673 * The 63rd bit indicates if this is a temporal profile.
674 */
675#define VARIANT_MASKS_ALL 0xffffffff00000000ULL
676#define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
677#define VARIANT_MASK_IR_PROF (0x1ULL << 56)
678#define VARIANT_MASK_CSIR_PROF (0x1ULL << 57)
679#define VARIANT_MASK_INSTR_ENTRY (0x1ULL << 58)
680#define VARIANT_MASK_DBG_CORRELATE (0x1ULL << 59)
681#define VARIANT_MASK_BYTE_COVERAGE (0x1ULL << 60)
682#define VARIANT_MASK_FUNCTION_ENTRY_ONLY (0x1ULL << 61)
683#define VARIANT_MASK_MEMPROF (0x1ULL << 62)
684#define VARIANT_MASK_TEMPORAL_PROF (0x1ULL << 63)
685#define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
686#define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
687#define INSTR_PROF_PROFILE_COUNTER_BIAS_VAR __llvm_profile_counter_bias
688#define INSTR_PROF_PROFILE_SET_TIMESTAMP __llvm_profile_set_timestamp
689
690/* The variable that holds the name of the profile data
691 * specified via command line. */
692#define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
693
694/* section name strings common to all targets other
695 than WIN32 */
696#define INSTR_PROF_DATA_COMMON __llvm_prf_data
697#define INSTR_PROF_NAME_COMMON __llvm_prf_names
698#define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
699#define INSTR_PROF_BITS_COMMON __llvm_prf_bits
700#define INSTR_PROF_VALS_COMMON __llvm_prf_vals
701#define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
702#define INSTR_PROF_COVMAP_COMMON __llvm_covmap
703#define INSTR_PROF_COVFUN_COMMON __llvm_covfun
704#define INSTR_PROF_ORDERFILE_COMMON __llvm_orderfile
705/* Windows section names. Because these section names contain dollar characters,
706 * they must be quoted.
707 */
708#define INSTR_PROF_DATA_COFF ".lprfd$M"
709#define INSTR_PROF_NAME_COFF ".lprfn$M"
710#define INSTR_PROF_CNTS_COFF ".lprfc$M"
711#define INSTR_PROF_BITS_COFF ".lprfb$M"
712#define INSTR_PROF_VALS_COFF ".lprfv$M"
713#define INSTR_PROF_VNODES_COFF ".lprfnd$M"
714#define INSTR_PROF_COVMAP_COFF ".lcovmap$M"
715#define INSTR_PROF_COVFUN_COFF ".lcovfun$M"
716#define INSTR_PROF_ORDERFILE_COFF ".lorderfile$M"
717
718#ifdef _WIN32
719/* Runtime section names and name strings. */
720#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
721#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
722#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
723#define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_BITS_COFF
724/* Array of pointers. Each pointer points to a list
725 * of value nodes associated with one value site.
726 */
727#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
728/* Value profile nodes section. */
729#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
730#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
731#define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_COVFUN_COFF
732#define INSTR_PROF_ORDERFILE_SECT_NAME INSTR_PROF_ORDERFILE_COFF
733#else
734/* Runtime section names and name strings. */
735#define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON)
736#define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON)
737#define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON)
738#define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON)
739/* Array of pointers. Each pointer points to a list
740 * of value nodes associated with one value site.
741 */
742#define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON)
743/* Value profile nodes section. */
744#define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON)
745#define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON)
746#define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON)
747/* Order file instrumentation. */
748#define INSTR_PROF_ORDERFILE_SECT_NAME \
749 INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON)
750#endif
751
752#define INSTR_PROF_ORDERFILE_BUFFER_NAME _llvm_order_file_buffer
753#define INSTR_PROF_ORDERFILE_BUFFER_NAME_STR \
754 INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_NAME)
755#define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME _llvm_order_file_buffer_idx
756#define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME_STR \
757 INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME)
758
759/* Macros to define start/stop section symbol for a given
760 * section on Linux. For instance
761 * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
762 * expand to __start___llvm_prof_data
763 */
764#define INSTR_PROF_SECT_START(Sect) \
765 INSTR_PROF_CONCAT(__start_,Sect)
766#define INSTR_PROF_SECT_STOP(Sect) \
767 INSTR_PROF_CONCAT(__stop_,Sect)
768
769/* Value Profiling API linkage name. */
770#define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
771#define INSTR_PROF_VALUE_PROF_FUNC_STR \
772 INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
773#define INSTR_PROF_VALUE_PROF_MEMOP_FUNC __llvm_profile_instrument_memop
774#define INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR \
775 INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_MEMOP_FUNC)
776
777/* InstrProfile per-function control data alignment. */
778#define INSTR_PROF_DATA_ALIGNMENT 8
779
780/* The data structure that represents a tracked value by the
781 * value profiler.
782 */
783typedef struct InstrProfValueData {
784 /* Profiled value. */
786 /* Number of times the value appears in the training run. */
787 uint64_t Count;
788} InstrProfValueData;
789
790#endif /* INSTR_PROF_DATA_INC */
791
792#ifndef INSTR_ORDER_FILE_INC
793/* The maximal # of functions: 128*1024 (the buffer size will be 128*4 KB). */
794#define INSTR_ORDER_FILE_BUFFER_SIZE 131072
795#define INSTR_ORDER_FILE_BUFFER_BITS 17
796#define INSTR_ORDER_FILE_BUFFER_MASK 0x1ffff
797#endif /* INSTR_ORDER_FILE_INC */
798#else
799#undef INSTR_PROF_DATA_DEFINED
800#endif
801
802#undef COVMAP_V2_OR_V3
803
804#ifdef INSTR_PROF_VALUE_PROF_MEMOP_API
805
806#ifdef __cplusplus
807#define INSTR_PROF_INLINE inline
808#else
809#define INSTR_PROF_INLINE
810#endif
811
812/* The value range buckets (22 buckets) for the memop size value profiling looks
813 * like:
814 *
815 * [0, 0]
816 * [1, 1]
817 * [2, 2]
818 * [3, 3]
819 * [4, 4]
820 * [5, 5]
821 * [6, 6]
822 * [7, 7]
823 * [8, 8]
824 * [9, 15]
825 * [16, 16]
826 * [17, 31]
827 * [32, 32]
828 * [33, 63]
829 * [64, 64]
830 * [65, 127]
831 * [128, 128]
832 * [129, 255]
833 * [256, 256]
834 * [257, 511]
835 * [512, 512]
836 * [513, UINT64_MAX]
837 *
838 * Each range has a 'representative value' which is the lower end value of the
839 * range and used to store in the runtime profile data records and the VP
840 * metadata. For example, it's 2 for [2, 2] and 64 for [65, 127].
841 */
842#define INSTR_PROF_NUM_BUCKETS 22
843
844/*
845 * Clz and Popcount. This code was copied from
846 * compiler-rt/lib/fuzzer/{FuzzerBuiltins.h,FuzzerBuiltinsMsvc.h} and
847 * llvm/include/llvm/Support/MathExtras.h.
848 */
849#if defined(_MSC_VER) && !defined(__clang__)
850
851#include <intrin.h>
852INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
853int InstProfClzll(unsigned long long X) {
854 unsigned long LeadZeroIdx = 0;
855#if !defined(_M_ARM64) && !defined(_M_X64)
856 // Scan the high 32 bits.
857 if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X >> 32)))
858 return (int)(63 - (LeadZeroIdx + 32)); // Create a bit offset
859 // from the MSB.
860 // Scan the low 32 bits.
861 if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X)))
862 return (int)(63 - LeadZeroIdx);
863#else
864 if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
865#endif
866 return 64;
867}
868INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
869int InstProfPopcountll(unsigned long long X) {
870 // This code originates from https://reviews.llvm.org/rG30626254510f.
871 unsigned long long v = X;
872 v = v - ((v >> 1) & 0x5555555555555555ULL);
873 v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
874 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
875 return (int)((unsigned long long)(v * 0x0101010101010101ULL) >> 56);
876}
877
878#else
879
880INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
881int InstProfClzll(unsigned long long X) { return __builtin_clzll(X); }
882INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
883int InstProfPopcountll(unsigned long long X) { return __builtin_popcountll(X); }
884
885#endif /* defined(_MSC_VER) && !defined(__clang__) */
886
887/* Map an (observed) memop size value to the representative value of its range.
888 * For example, 5 -> 5, 22 -> 17, 99 -> 65, 256 -> 256, 1001 -> 513. */
889INSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint64_t
890InstrProfGetRangeRepValue(uint64_t Value) {
891 if (Value <= 8)
892 // The first ranges are individually tracked. Use the value as is.
893 return Value;
894 else if (Value >= 513)
895 // The last range is mapped to its lowest value.
896 return 513;
897 else if (InstProfPopcountll(Value) == 1)
898 // If it's a power of two, use it as is.
899 return Value;
900 else
901 // Otherwise, take to the previous power of two + 1.
902 return (UINT64_C(1) << (64 - InstProfClzll(Value) - 1)) + 1;
903}
904
905/* Return true if the range that an (observed) memop size value belongs to has
906 * only a single value in the range. For example, 0 -> true, 8 -> true, 10 ->
907 * false, 64 -> true, 100 -> false, 513 -> false. */
908INSTR_PROF_VISIBILITY INSTR_PROF_INLINE unsigned
909InstrProfIsSingleValRange(uint64_t Value) {
910 if (Value <= 8)
911 // The first ranges are individually tracked.
912 return 1;
913 else if (InstProfPopcountll(Value) == 1)
914 // If it's a power of two, there's only one value.
915 return 1;
916 else
917 // Otherwise, there's more than one value in the range.
918 return 0;
919}
920
921#endif /* INSTR_PROF_VALUE_PROF_MEMOP_API */
aarch64 promote const
Lower uses of LDS variables from non kernel functions
#define offsetof(TYPE, MEMBER)
RelocType Type
Definition: COFFYAML.cpp:391
#define COVMAP_HEADER(Type, LLVMType, Name, Init)
#define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Init)
uint64_t Size
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#define INSTR_PROF_RAW_HEADER(Type, Name, Init)
Definition: InstrProf.h:1189
#define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix)
Definition: InstrProf.h:59
#define INSTR_PROF_DATA(Type, LLVMType, Name, Init)
Definition: InstrProf.h:1180
#define VALUE_PROF_KIND(Enumerator, Value, Descr)
Definition: InstrProf.h:255
#define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init)
#define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType)
#define I(x, y, z)
Definition: MD5.cpp:58
IntegerType * Int32Ty
static unsigned getSize(unsigned Kind)
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:654
static Constant * getRaw(StringRef Data, uint64_t NumElements, Type *ElementTy)
getRaw() constructor - Return a constant with array type with an element count and element type match...
Definition: Constants.h:710
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2056
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
static IntegerType * getInt8Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
ValueKind
Value kinds.
std::enable_if_t< std::is_arithmetic< T >::value, void > swapBytes(T &value)
Definition: DXContainer.h:32
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar)
Return the initializer in string of the PGO name var NameVar.
Definition: InstrProf.cpp:605
endianness
Definition: bit.h:70