InstrProfData.inc 33 KB

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