InstrProfData.inc 33 KB

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