InstrProf.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. //===- InstrProf.cpp - Instrumented profiling format support --------------===//
  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 file contains support for clang's instrumentation based PGO and
  10. // coverage.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ProfileData/InstrProf.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/Config/config.h"
  21. #include "llvm/IR/Constant.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/GlobalValue.h"
  25. #include "llvm/IR/GlobalVariable.h"
  26. #include "llvm/IR/Instruction.h"
  27. #include "llvm/IR/LLVMContext.h"
  28. #include "llvm/IR/MDBuilder.h"
  29. #include "llvm/IR/Metadata.h"
  30. #include "llvm/IR/Module.h"
  31. #include "llvm/IR/Type.h"
  32. #include "llvm/ProfileData/InstrProfReader.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Compiler.h"
  36. #include "llvm/Support/Compression.h"
  37. #include "llvm/Support/Endian.h"
  38. #include "llvm/Support/Error.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/LEB128.h"
  41. #include "llvm/Support/ManagedStatic.h"
  42. #include "llvm/Support/MathExtras.h"
  43. #include "llvm/Support/Path.h"
  44. #include "llvm/Support/SwapByteOrder.h"
  45. #include <algorithm>
  46. #include <cassert>
  47. #include <cstddef>
  48. #include <cstdint>
  49. #include <cstring>
  50. #include <memory>
  51. #include <string>
  52. #include <system_error>
  53. #include <utility>
  54. #include <vector>
  55. using namespace llvm;
  56. static cl::opt<bool> StaticFuncFullModulePrefix(
  57. "static-func-full-module-prefix", cl::init(true), cl::Hidden,
  58. cl::desc("Use full module build paths in the profile counter names for "
  59. "static functions."));
  60. // This option is tailored to users that have different top-level directory in
  61. // profile-gen and profile-use compilation. Users need to specific the number
  62. // of levels to strip. A value larger than the number of directories in the
  63. // source file will strip all the directory names and only leave the basename.
  64. //
  65. // Note current ThinLTO module importing for the indirect-calls assumes
  66. // the source directory name not being stripped. A non-zero option value here
  67. // can potentially prevent some inter-module indirect-call-promotions.
  68. static cl::opt<unsigned> StaticFuncStripDirNamePrefix(
  69. "static-func-strip-dirname-prefix", cl::init(0), cl::Hidden,
  70. cl::desc("Strip specified level of directory name from source path in "
  71. "the profile counter name for static functions."));
  72. static std::string getInstrProfErrString(instrprof_error Err,
  73. const std::string &ErrMsg = "") {
  74. std::string Msg;
  75. raw_string_ostream OS(Msg);
  76. switch (Err) {
  77. case instrprof_error::success:
  78. OS << "success";
  79. break;
  80. case instrprof_error::eof:
  81. OS << "end of File";
  82. break;
  83. case instrprof_error::unrecognized_format:
  84. OS << "unrecognized instrumentation profile encoding format";
  85. break;
  86. case instrprof_error::bad_magic:
  87. OS << "invalid instrumentation profile data (bad magic)";
  88. break;
  89. case instrprof_error::bad_header:
  90. OS << "invalid instrumentation profile data (file header is corrupt)";
  91. break;
  92. case instrprof_error::unsupported_version:
  93. OS << "unsupported instrumentation profile format version";
  94. break;
  95. case instrprof_error::unsupported_hash_type:
  96. OS << "unsupported instrumentation profile hash type";
  97. break;
  98. case instrprof_error::too_large:
  99. OS << "too much profile data";
  100. break;
  101. case instrprof_error::truncated:
  102. OS << "truncated profile data";
  103. break;
  104. case instrprof_error::malformed:
  105. OS << "malformed instrumentation profile data";
  106. break;
  107. case instrprof_error::missing_debug_info_for_correlation:
  108. OS << "debug info for correlation is required";
  109. break;
  110. case instrprof_error::unexpected_debug_info_for_correlation:
  111. OS << "debug info for correlation is not necessary";
  112. break;
  113. case instrprof_error::unable_to_correlate_profile:
  114. OS << "unable to correlate profile";
  115. break;
  116. case instrprof_error::invalid_prof:
  117. OS << "invalid profile created. Please file a bug "
  118. "at: " BUG_REPORT_URL
  119. " and include the profraw files that caused this error.";
  120. break;
  121. case instrprof_error::unknown_function:
  122. OS << "no profile data available for function";
  123. break;
  124. case instrprof_error::hash_mismatch:
  125. OS << "function control flow change detected (hash mismatch)";
  126. break;
  127. case instrprof_error::count_mismatch:
  128. OS << "function basic block count change detected (counter mismatch)";
  129. break;
  130. case instrprof_error::counter_overflow:
  131. OS << "counter overflow";
  132. break;
  133. case instrprof_error::value_site_count_mismatch:
  134. OS << "function value site count change detected (counter mismatch)";
  135. break;
  136. case instrprof_error::compress_failed:
  137. OS << "failed to compress data (zlib)";
  138. break;
  139. case instrprof_error::uncompress_failed:
  140. OS << "failed to uncompress data (zlib)";
  141. break;
  142. case instrprof_error::empty_raw_profile:
  143. OS << "empty raw profile file";
  144. break;
  145. case instrprof_error::zlib_unavailable:
  146. OS << "profile uses zlib compression but the profile reader was built "
  147. "without zlib support";
  148. break;
  149. }
  150. // If optional error message is not empty, append it to the message.
  151. if (!ErrMsg.empty())
  152. OS << ": " << ErrMsg;
  153. return OS.str();
  154. }
  155. namespace {
  156. // FIXME: This class is only here to support the transition to llvm::Error. It
  157. // will be removed once this transition is complete. Clients should prefer to
  158. // deal with the Error value directly, rather than converting to error_code.
  159. class InstrProfErrorCategoryType : public std::error_category {
  160. const char *name() const noexcept override { return "llvm.instrprof"; }
  161. std::string message(int IE) const override {
  162. return getInstrProfErrString(static_cast<instrprof_error>(IE));
  163. }
  164. };
  165. } // end anonymous namespace
  166. static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
  167. const std::error_category &llvm::instrprof_category() {
  168. return *ErrorCategory;
  169. }
  170. namespace {
  171. const char *InstrProfSectNameCommon[] = {
  172. #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
  173. SectNameCommon,
  174. #include "llvm/ProfileData/InstrProfData.inc"
  175. };
  176. const char *InstrProfSectNameCoff[] = {
  177. #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
  178. SectNameCoff,
  179. #include "llvm/ProfileData/InstrProfData.inc"
  180. };
  181. const char *InstrProfSectNamePrefix[] = {
  182. #define INSTR_PROF_SECT_ENTRY(Kind, SectNameCommon, SectNameCoff, Prefix) \
  183. Prefix,
  184. #include "llvm/ProfileData/InstrProfData.inc"
  185. };
  186. } // namespace
  187. namespace llvm {
  188. cl::opt<bool> DoInstrProfNameCompression(
  189. "enable-name-compression",
  190. cl::desc("Enable name/filename string compression"), cl::init(true));
  191. std::string getInstrProfSectionName(InstrProfSectKind IPSK,
  192. Triple::ObjectFormatType OF,
  193. bool AddSegmentInfo) {
  194. std::string SectName;
  195. if (OF == Triple::MachO && AddSegmentInfo)
  196. SectName = InstrProfSectNamePrefix[IPSK];
  197. if (OF == Triple::COFF)
  198. SectName += InstrProfSectNameCoff[IPSK];
  199. else
  200. SectName += InstrProfSectNameCommon[IPSK];
  201. if (OF == Triple::MachO && IPSK == IPSK_data && AddSegmentInfo)
  202. SectName += ",regular,live_support";
  203. return SectName;
  204. }
  205. void SoftInstrProfErrors::addError(instrprof_error IE) {
  206. if (IE == instrprof_error::success)
  207. return;
  208. if (FirstError == instrprof_error::success)
  209. FirstError = IE;
  210. switch (IE) {
  211. case instrprof_error::hash_mismatch:
  212. ++NumHashMismatches;
  213. break;
  214. case instrprof_error::count_mismatch:
  215. ++NumCountMismatches;
  216. break;
  217. case instrprof_error::counter_overflow:
  218. ++NumCounterOverflows;
  219. break;
  220. case instrprof_error::value_site_count_mismatch:
  221. ++NumValueSiteCountMismatches;
  222. break;
  223. default:
  224. llvm_unreachable("Not a soft error");
  225. }
  226. }
  227. std::string InstrProfError::message() const {
  228. return getInstrProfErrString(Err, Msg);
  229. }
  230. char InstrProfError::ID = 0;
  231. std::string getPGOFuncName(StringRef RawFuncName,
  232. GlobalValue::LinkageTypes Linkage,
  233. StringRef FileName,
  234. uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
  235. return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
  236. }
  237. // Strip NumPrefix level of directory name from PathNameStr. If the number of
  238. // directory separators is less than NumPrefix, strip all the directories and
  239. // leave base file name only.
  240. static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
  241. uint32_t Count = NumPrefix;
  242. uint32_t Pos = 0, LastPos = 0;
  243. for (auto & CI : PathNameStr) {
  244. ++Pos;
  245. if (llvm::sys::path::is_separator(CI)) {
  246. LastPos = Pos;
  247. --Count;
  248. }
  249. if (Count == 0)
  250. break;
  251. }
  252. return PathNameStr.substr(LastPos);
  253. }
  254. // Return the PGOFuncName. This function has some special handling when called
  255. // in LTO optimization. The following only applies when calling in LTO passes
  256. // (when \c InLTO is true): LTO's internalization privatizes many global linkage
  257. // symbols. This happens after value profile annotation, but those internal
  258. // linkage functions should not have a source prefix.
  259. // Additionally, for ThinLTO mode, exported internal functions are promoted
  260. // and renamed. We need to ensure that the original internal PGO name is
  261. // used when computing the GUID that is compared against the profiled GUIDs.
  262. // To differentiate compiler generated internal symbols from original ones,
  263. // PGOFuncName meta data are created and attached to the original internal
  264. // symbols in the value profile annotation step
  265. // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
  266. // data, its original linkage must be non-internal.
  267. std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
  268. if (!InLTO) {
  269. StringRef FileName(F.getParent()->getSourceFileName());
  270. uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t)-1;
  271. if (StripLevel < StaticFuncStripDirNamePrefix)
  272. StripLevel = StaticFuncStripDirNamePrefix;
  273. if (StripLevel)
  274. FileName = stripDirPrefix(FileName, StripLevel);
  275. return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
  276. }
  277. // In LTO mode (when InLTO is true), first check if there is a meta data.
  278. if (MDNode *MD = getPGOFuncNameMetadata(F)) {
  279. StringRef S = cast<MDString>(MD->getOperand(0))->getString();
  280. return S.str();
  281. }
  282. // If there is no meta data, the function must be a global before the value
  283. // profile annotation pass. Its current linkage may be internal if it is
  284. // internalized in LTO mode.
  285. return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
  286. }
  287. StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
  288. if (FileName.empty())
  289. return PGOFuncName;
  290. // Drop the file name including ':'. See also getPGOFuncName.
  291. if (PGOFuncName.startswith(FileName))
  292. PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
  293. return PGOFuncName;
  294. }
  295. // \p FuncName is the string used as profile lookup key for the function. A
  296. // symbol is created to hold the name. Return the legalized symbol name.
  297. std::string getPGOFuncNameVarName(StringRef FuncName,
  298. GlobalValue::LinkageTypes Linkage) {
  299. std::string VarName = std::string(getInstrProfNameVarPrefix());
  300. VarName += FuncName;
  301. if (!GlobalValue::isLocalLinkage(Linkage))
  302. return VarName;
  303. // Now fix up illegal chars in local VarName that may upset the assembler.
  304. const char *InvalidChars = "-:<>/\"'";
  305. size_t found = VarName.find_first_of(InvalidChars);
  306. while (found != std::string::npos) {
  307. VarName[found] = '_';
  308. found = VarName.find_first_of(InvalidChars, found + 1);
  309. }
  310. return VarName;
  311. }
  312. GlobalVariable *createPGOFuncNameVar(Module &M,
  313. GlobalValue::LinkageTypes Linkage,
  314. StringRef PGOFuncName) {
  315. // We generally want to match the function's linkage, but available_externally
  316. // and extern_weak both have the wrong semantics, and anything that doesn't
  317. // need to link across compilation units doesn't need to be visible at all.
  318. if (Linkage == GlobalValue::ExternalWeakLinkage)
  319. Linkage = GlobalValue::LinkOnceAnyLinkage;
  320. else if (Linkage == GlobalValue::AvailableExternallyLinkage)
  321. Linkage = GlobalValue::LinkOnceODRLinkage;
  322. else if (Linkage == GlobalValue::InternalLinkage ||
  323. Linkage == GlobalValue::ExternalLinkage)
  324. Linkage = GlobalValue::PrivateLinkage;
  325. auto *Value =
  326. ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
  327. auto FuncNameVar =
  328. new GlobalVariable(M, Value->getType(), true, Linkage, Value,
  329. getPGOFuncNameVarName(PGOFuncName, Linkage));
  330. // Hide the symbol so that we correctly get a copy for each executable.
  331. if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
  332. FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
  333. return FuncNameVar;
  334. }
  335. GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
  336. return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
  337. }
  338. Error InstrProfSymtab::create(Module &M, bool InLTO) {
  339. for (Function &F : M) {
  340. // Function may not have a name: like using asm("") to overwrite the name.
  341. // Ignore in this case.
  342. if (!F.hasName())
  343. continue;
  344. const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
  345. if (Error E = addFuncName(PGOFuncName))
  346. return E;
  347. MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
  348. // In ThinLTO, local function may have been promoted to global and have
  349. // suffix ".llvm." added to the function name. We need to add the
  350. // stripped function name to the symbol table so that we can find a match
  351. // from profile.
  352. //
  353. // We may have other suffixes similar as ".llvm." which are needed to
  354. // be stripped before the matching, but ".__uniq." suffix which is used
  355. // to differentiate internal linkage functions in different modules
  356. // should be kept. Now this is the only suffix with the pattern ".xxx"
  357. // which is kept before matching.
  358. const std::string UniqSuffix = ".__uniq.";
  359. auto pos = PGOFuncName.find(UniqSuffix);
  360. // Search '.' after ".__uniq." if ".__uniq." exists, otherwise
  361. // search '.' from the beginning.
  362. if (pos != std::string::npos)
  363. pos += UniqSuffix.length();
  364. else
  365. pos = 0;
  366. pos = PGOFuncName.find('.', pos);
  367. if (pos != std::string::npos && pos != 0) {
  368. const std::string &OtherFuncName = PGOFuncName.substr(0, pos);
  369. if (Error E = addFuncName(OtherFuncName))
  370. return E;
  371. MD5FuncMap.emplace_back(Function::getGUID(OtherFuncName), &F);
  372. }
  373. }
  374. Sorted = false;
  375. finalizeSymtab();
  376. return Error::success();
  377. }
  378. uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
  379. finalizeSymtab();
  380. auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
  381. return A.first < Address;
  382. });
  383. // Raw function pointer collected by value profiler may be from
  384. // external functions that are not instrumented. They won't have
  385. // mapping data to be used by the deserializer. Force the value to
  386. // be 0 in this case.
  387. if (It != AddrToMD5Map.end() && It->first == Address)
  388. return (uint64_t)It->second;
  389. return 0;
  390. }
  391. Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
  392. bool doCompression, std::string &Result) {
  393. assert(!NameStrs.empty() && "No name data to emit");
  394. uint8_t Header[16], *P = Header;
  395. std::string UncompressedNameStrings =
  396. join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
  397. assert(StringRef(UncompressedNameStrings)
  398. .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
  399. "PGO name is invalid (contains separator token)");
  400. unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
  401. P += EncLen;
  402. auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
  403. EncLen = encodeULEB128(CompressedLen, P);
  404. P += EncLen;
  405. char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
  406. unsigned HeaderLen = P - &Header[0];
  407. Result.append(HeaderStr, HeaderLen);
  408. Result += InputStr;
  409. return Error::success();
  410. };
  411. if (!doCompression) {
  412. return WriteStringToResult(0, UncompressedNameStrings);
  413. }
  414. SmallString<128> CompressedNameStrings;
  415. Error E = zlib::compress(StringRef(UncompressedNameStrings),
  416. CompressedNameStrings, zlib::BestSizeCompression);
  417. if (E) {
  418. consumeError(std::move(E));
  419. return make_error<InstrProfError>(instrprof_error::compress_failed);
  420. }
  421. return WriteStringToResult(CompressedNameStrings.size(),
  422. CompressedNameStrings);
  423. }
  424. StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
  425. auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
  426. StringRef NameStr =
  427. Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
  428. return NameStr;
  429. }
  430. Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
  431. std::string &Result, bool doCompression) {
  432. std::vector<std::string> NameStrs;
  433. for (auto *NameVar : NameVars) {
  434. NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar)));
  435. }
  436. return collectPGOFuncNameStrings(
  437. NameStrs, zlib::isAvailable() && doCompression, Result);
  438. }
  439. Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
  440. const uint8_t *P = NameStrings.bytes_begin();
  441. const uint8_t *EndP = NameStrings.bytes_end();
  442. while (P < EndP) {
  443. uint32_t N;
  444. uint64_t UncompressedSize = decodeULEB128(P, &N);
  445. P += N;
  446. uint64_t CompressedSize = decodeULEB128(P, &N);
  447. P += N;
  448. bool isCompressed = (CompressedSize != 0);
  449. SmallString<128> UncompressedNameStrings;
  450. StringRef NameStrings;
  451. if (isCompressed) {
  452. if (!llvm::zlib::isAvailable())
  453. return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
  454. StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
  455. CompressedSize);
  456. if (Error E =
  457. zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
  458. UncompressedSize)) {
  459. consumeError(std::move(E));
  460. return make_error<InstrProfError>(instrprof_error::uncompress_failed);
  461. }
  462. P += CompressedSize;
  463. NameStrings = StringRef(UncompressedNameStrings.data(),
  464. UncompressedNameStrings.size());
  465. } else {
  466. NameStrings =
  467. StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
  468. P += UncompressedSize;
  469. }
  470. // Now parse the name strings.
  471. SmallVector<StringRef, 0> Names;
  472. NameStrings.split(Names, getInstrProfNameSeparator());
  473. for (StringRef &Name : Names)
  474. if (Error E = Symtab.addFuncName(Name))
  475. return E;
  476. while (P < EndP && *P == 0)
  477. P++;
  478. }
  479. return Error::success();
  480. }
  481. void InstrProfRecord::accumulateCounts(CountSumOrPercent &Sum) const {
  482. uint64_t FuncSum = 0;
  483. Sum.NumEntries += Counts.size();
  484. for (uint64_t Count : Counts)
  485. FuncSum += Count;
  486. Sum.CountSum += FuncSum;
  487. for (uint32_t VK = IPVK_First; VK <= IPVK_Last; ++VK) {
  488. uint64_t KindSum = 0;
  489. uint32_t NumValueSites = getNumValueSites(VK);
  490. for (size_t I = 0; I < NumValueSites; ++I) {
  491. uint32_t NV = getNumValueDataForSite(VK, I);
  492. std::unique_ptr<InstrProfValueData[]> VD = getValueForSite(VK, I);
  493. for (uint32_t V = 0; V < NV; V++)
  494. KindSum += VD[V].Count;
  495. }
  496. Sum.ValueCounts[VK] += KindSum;
  497. }
  498. }
  499. void InstrProfValueSiteRecord::overlap(InstrProfValueSiteRecord &Input,
  500. uint32_t ValueKind,
  501. OverlapStats &Overlap,
  502. OverlapStats &FuncLevelOverlap) {
  503. this->sortByTargetValues();
  504. Input.sortByTargetValues();
  505. double Score = 0.0f, FuncLevelScore = 0.0f;
  506. auto I = ValueData.begin();
  507. auto IE = ValueData.end();
  508. auto J = Input.ValueData.begin();
  509. auto JE = Input.ValueData.end();
  510. while (I != IE && J != JE) {
  511. if (I->Value == J->Value) {
  512. Score += OverlapStats::score(I->Count, J->Count,
  513. Overlap.Base.ValueCounts[ValueKind],
  514. Overlap.Test.ValueCounts[ValueKind]);
  515. FuncLevelScore += OverlapStats::score(
  516. I->Count, J->Count, FuncLevelOverlap.Base.ValueCounts[ValueKind],
  517. FuncLevelOverlap.Test.ValueCounts[ValueKind]);
  518. ++I;
  519. } else if (I->Value < J->Value) {
  520. ++I;
  521. continue;
  522. }
  523. ++J;
  524. }
  525. Overlap.Overlap.ValueCounts[ValueKind] += Score;
  526. FuncLevelOverlap.Overlap.ValueCounts[ValueKind] += FuncLevelScore;
  527. }
  528. // Return false on mismatch.
  529. void InstrProfRecord::overlapValueProfData(uint32_t ValueKind,
  530. InstrProfRecord &Other,
  531. OverlapStats &Overlap,
  532. OverlapStats &FuncLevelOverlap) {
  533. uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
  534. assert(ThisNumValueSites == Other.getNumValueSites(ValueKind));
  535. if (!ThisNumValueSites)
  536. return;
  537. std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
  538. getOrCreateValueSitesForKind(ValueKind);
  539. MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
  540. Other.getValueSitesForKind(ValueKind);
  541. for (uint32_t I = 0; I < ThisNumValueSites; I++)
  542. ThisSiteRecords[I].overlap(OtherSiteRecords[I], ValueKind, Overlap,
  543. FuncLevelOverlap);
  544. }
  545. void InstrProfRecord::overlap(InstrProfRecord &Other, OverlapStats &Overlap,
  546. OverlapStats &FuncLevelOverlap,
  547. uint64_t ValueCutoff) {
  548. // FuncLevel CountSum for other should already computed and nonzero.
  549. assert(FuncLevelOverlap.Test.CountSum >= 1.0f);
  550. accumulateCounts(FuncLevelOverlap.Base);
  551. bool Mismatch = (Counts.size() != Other.Counts.size());
  552. // Check if the value profiles mismatch.
  553. if (!Mismatch) {
  554. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
  555. uint32_t ThisNumValueSites = getNumValueSites(Kind);
  556. uint32_t OtherNumValueSites = Other.getNumValueSites(Kind);
  557. if (ThisNumValueSites != OtherNumValueSites) {
  558. Mismatch = true;
  559. break;
  560. }
  561. }
  562. }
  563. if (Mismatch) {
  564. Overlap.addOneMismatch(FuncLevelOverlap.Test);
  565. return;
  566. }
  567. // Compute overlap for value counts.
  568. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  569. overlapValueProfData(Kind, Other, Overlap, FuncLevelOverlap);
  570. double Score = 0.0;
  571. uint64_t MaxCount = 0;
  572. // Compute overlap for edge counts.
  573. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
  574. Score += OverlapStats::score(Counts[I], Other.Counts[I],
  575. Overlap.Base.CountSum, Overlap.Test.CountSum);
  576. MaxCount = std::max(Other.Counts[I], MaxCount);
  577. }
  578. Overlap.Overlap.CountSum += Score;
  579. Overlap.Overlap.NumEntries += 1;
  580. if (MaxCount >= ValueCutoff) {
  581. double FuncScore = 0.0;
  582. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I)
  583. FuncScore += OverlapStats::score(Counts[I], Other.Counts[I],
  584. FuncLevelOverlap.Base.CountSum,
  585. FuncLevelOverlap.Test.CountSum);
  586. FuncLevelOverlap.Overlap.CountSum = FuncScore;
  587. FuncLevelOverlap.Overlap.NumEntries = Other.Counts.size();
  588. FuncLevelOverlap.Valid = true;
  589. }
  590. }
  591. void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
  592. uint64_t Weight,
  593. function_ref<void(instrprof_error)> Warn) {
  594. this->sortByTargetValues();
  595. Input.sortByTargetValues();
  596. auto I = ValueData.begin();
  597. auto IE = ValueData.end();
  598. for (const InstrProfValueData &J : Input.ValueData) {
  599. while (I != IE && I->Value < J.Value)
  600. ++I;
  601. if (I != IE && I->Value == J.Value) {
  602. bool Overflowed;
  603. I->Count = SaturatingMultiplyAdd(J.Count, Weight, I->Count, &Overflowed);
  604. if (Overflowed)
  605. Warn(instrprof_error::counter_overflow);
  606. ++I;
  607. continue;
  608. }
  609. ValueData.insert(I, J);
  610. }
  611. }
  612. void InstrProfValueSiteRecord::scale(uint64_t N, uint64_t D,
  613. function_ref<void(instrprof_error)> Warn) {
  614. for (InstrProfValueData &I : ValueData) {
  615. bool Overflowed;
  616. I.Count = SaturatingMultiply(I.Count, N, &Overflowed) / D;
  617. if (Overflowed)
  618. Warn(instrprof_error::counter_overflow);
  619. }
  620. }
  621. // Merge Value Profile data from Src record to this record for ValueKind.
  622. // Scale merged value counts by \p Weight.
  623. void InstrProfRecord::mergeValueProfData(
  624. uint32_t ValueKind, InstrProfRecord &Src, uint64_t Weight,
  625. function_ref<void(instrprof_error)> Warn) {
  626. uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
  627. uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
  628. if (ThisNumValueSites != OtherNumValueSites) {
  629. Warn(instrprof_error::value_site_count_mismatch);
  630. return;
  631. }
  632. if (!ThisNumValueSites)
  633. return;
  634. std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
  635. getOrCreateValueSitesForKind(ValueKind);
  636. MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
  637. Src.getValueSitesForKind(ValueKind);
  638. for (uint32_t I = 0; I < ThisNumValueSites; I++)
  639. ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight, Warn);
  640. }
  641. void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
  642. function_ref<void(instrprof_error)> Warn) {
  643. // If the number of counters doesn't match we either have bad data
  644. // or a hash collision.
  645. if (Counts.size() != Other.Counts.size()) {
  646. Warn(instrprof_error::count_mismatch);
  647. return;
  648. }
  649. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
  650. bool Overflowed;
  651. Counts[I] =
  652. SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
  653. if (Overflowed)
  654. Warn(instrprof_error::counter_overflow);
  655. }
  656. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  657. mergeValueProfData(Kind, Other, Weight, Warn);
  658. }
  659. void InstrProfRecord::scaleValueProfData(
  660. uint32_t ValueKind, uint64_t N, uint64_t D,
  661. function_ref<void(instrprof_error)> Warn) {
  662. for (auto &R : getValueSitesForKind(ValueKind))
  663. R.scale(N, D, Warn);
  664. }
  665. void InstrProfRecord::scale(uint64_t N, uint64_t D,
  666. function_ref<void(instrprof_error)> Warn) {
  667. assert(D != 0 && "D cannot be 0");
  668. for (auto &Count : this->Counts) {
  669. bool Overflowed;
  670. Count = SaturatingMultiply(Count, N, &Overflowed) / D;
  671. if (Overflowed)
  672. Warn(instrprof_error::counter_overflow);
  673. }
  674. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  675. scaleValueProfData(Kind, N, D, Warn);
  676. }
  677. // Map indirect call target name hash to name string.
  678. uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
  679. InstrProfSymtab *SymTab) {
  680. if (!SymTab)
  681. return Value;
  682. if (ValueKind == IPVK_IndirectCallTarget)
  683. return SymTab->getFunctionHashFromAddress(Value);
  684. return Value;
  685. }
  686. void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
  687. InstrProfValueData *VData, uint32_t N,
  688. InstrProfSymtab *ValueMap) {
  689. for (uint32_t I = 0; I < N; I++) {
  690. VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
  691. }
  692. std::vector<InstrProfValueSiteRecord> &ValueSites =
  693. getOrCreateValueSitesForKind(ValueKind);
  694. if (N == 0)
  695. ValueSites.emplace_back();
  696. else
  697. ValueSites.emplace_back(VData, VData + N);
  698. }
  699. #define INSTR_PROF_COMMON_API_IMPL
  700. #include "llvm/ProfileData/InstrProfData.inc"
  701. /*!
  702. * ValueProfRecordClosure Interface implementation for InstrProfRecord
  703. * class. These C wrappers are used as adaptors so that C++ code can be
  704. * invoked as callbacks.
  705. */
  706. uint32_t getNumValueKindsInstrProf(const void *Record) {
  707. return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
  708. }
  709. uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
  710. return reinterpret_cast<const InstrProfRecord *>(Record)
  711. ->getNumValueSites(VKind);
  712. }
  713. uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
  714. return reinterpret_cast<const InstrProfRecord *>(Record)
  715. ->getNumValueData(VKind);
  716. }
  717. uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
  718. uint32_t S) {
  719. return reinterpret_cast<const InstrProfRecord *>(R)
  720. ->getNumValueDataForSite(VK, S);
  721. }
  722. void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
  723. uint32_t K, uint32_t S) {
  724. reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
  725. }
  726. ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
  727. ValueProfData *VD =
  728. (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
  729. memset(VD, 0, TotalSizeInBytes);
  730. return VD;
  731. }
  732. static ValueProfRecordClosure InstrProfRecordClosure = {
  733. nullptr,
  734. getNumValueKindsInstrProf,
  735. getNumValueSitesInstrProf,
  736. getNumValueDataInstrProf,
  737. getNumValueDataForSiteInstrProf,
  738. nullptr,
  739. getValueForSiteInstrProf,
  740. allocValueProfDataInstrProf};
  741. // Wrapper implementation using the closure mechanism.
  742. uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
  743. auto Closure = InstrProfRecordClosure;
  744. Closure.Record = &Record;
  745. return getValueProfDataSize(&Closure);
  746. }
  747. // Wrapper implementation using the closure mechanism.
  748. std::unique_ptr<ValueProfData>
  749. ValueProfData::serializeFrom(const InstrProfRecord &Record) {
  750. InstrProfRecordClosure.Record = &Record;
  751. std::unique_ptr<ValueProfData> VPD(
  752. serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
  753. return VPD;
  754. }
  755. void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
  756. InstrProfSymtab *SymTab) {
  757. Record.reserveSites(Kind, NumValueSites);
  758. InstrProfValueData *ValueData = getValueProfRecordValueData(this);
  759. for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
  760. uint8_t ValueDataCount = this->SiteCountArray[VSite];
  761. Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
  762. ValueData += ValueDataCount;
  763. }
  764. }
  765. // For writing/serializing, Old is the host endianness, and New is
  766. // byte order intended on disk. For Reading/deserialization, Old
  767. // is the on-disk source endianness, and New is the host endianness.
  768. void ValueProfRecord::swapBytes(support::endianness Old,
  769. support::endianness New) {
  770. using namespace support;
  771. if (Old == New)
  772. return;
  773. if (getHostEndianness() != Old) {
  774. sys::swapByteOrder<uint32_t>(NumValueSites);
  775. sys::swapByteOrder<uint32_t>(Kind);
  776. }
  777. uint32_t ND = getValueProfRecordNumValueData(this);
  778. InstrProfValueData *VD = getValueProfRecordValueData(this);
  779. // No need to swap byte array: SiteCountArrray.
  780. for (uint32_t I = 0; I < ND; I++) {
  781. sys::swapByteOrder<uint64_t>(VD[I].Value);
  782. sys::swapByteOrder<uint64_t>(VD[I].Count);
  783. }
  784. if (getHostEndianness() == Old) {
  785. sys::swapByteOrder<uint32_t>(NumValueSites);
  786. sys::swapByteOrder<uint32_t>(Kind);
  787. }
  788. }
  789. void ValueProfData::deserializeTo(InstrProfRecord &Record,
  790. InstrProfSymtab *SymTab) {
  791. if (NumValueKinds == 0)
  792. return;
  793. ValueProfRecord *VR = getFirstValueProfRecord(this);
  794. for (uint32_t K = 0; K < NumValueKinds; K++) {
  795. VR->deserializeTo(Record, SymTab);
  796. VR = getValueProfRecordNext(VR);
  797. }
  798. }
  799. template <class T>
  800. static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
  801. using namespace support;
  802. if (Orig == little)
  803. return endian::readNext<T, little, unaligned>(D);
  804. else
  805. return endian::readNext<T, big, unaligned>(D);
  806. }
  807. static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
  808. return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
  809. ValueProfData());
  810. }
  811. Error ValueProfData::checkIntegrity() {
  812. if (NumValueKinds > IPVK_Last + 1)
  813. return make_error<InstrProfError>(
  814. instrprof_error::malformed, "number of value profile kinds is invalid");
  815. // Total size needs to be multiple of quadword size.
  816. if (TotalSize % sizeof(uint64_t))
  817. return make_error<InstrProfError>(
  818. instrprof_error::malformed, "total size is not multiples of quardword");
  819. ValueProfRecord *VR = getFirstValueProfRecord(this);
  820. for (uint32_t K = 0; K < this->NumValueKinds; K++) {
  821. if (VR->Kind > IPVK_Last)
  822. return make_error<InstrProfError>(instrprof_error::malformed,
  823. "value kind is invalid");
  824. VR = getValueProfRecordNext(VR);
  825. if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
  826. return make_error<InstrProfError>(
  827. instrprof_error::malformed,
  828. "value profile address is greater than total size");
  829. }
  830. return Error::success();
  831. }
  832. Expected<std::unique_ptr<ValueProfData>>
  833. ValueProfData::getValueProfData(const unsigned char *D,
  834. const unsigned char *const BufferEnd,
  835. support::endianness Endianness) {
  836. using namespace support;
  837. if (D + sizeof(ValueProfData) > BufferEnd)
  838. return make_error<InstrProfError>(instrprof_error::truncated);
  839. const unsigned char *Header = D;
  840. uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
  841. if (D + TotalSize > BufferEnd)
  842. return make_error<InstrProfError>(instrprof_error::too_large);
  843. std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
  844. memcpy(VPD.get(), D, TotalSize);
  845. // Byte swap.
  846. VPD->swapBytesToHost(Endianness);
  847. Error E = VPD->checkIntegrity();
  848. if (E)
  849. return std::move(E);
  850. return std::move(VPD);
  851. }
  852. void ValueProfData::swapBytesToHost(support::endianness Endianness) {
  853. using namespace support;
  854. if (Endianness == getHostEndianness())
  855. return;
  856. sys::swapByteOrder<uint32_t>(TotalSize);
  857. sys::swapByteOrder<uint32_t>(NumValueKinds);
  858. ValueProfRecord *VR = getFirstValueProfRecord(this);
  859. for (uint32_t K = 0; K < NumValueKinds; K++) {
  860. VR->swapBytes(Endianness, getHostEndianness());
  861. VR = getValueProfRecordNext(VR);
  862. }
  863. }
  864. void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
  865. using namespace support;
  866. if (Endianness == getHostEndianness())
  867. return;
  868. ValueProfRecord *VR = getFirstValueProfRecord(this);
  869. for (uint32_t K = 0; K < NumValueKinds; K++) {
  870. ValueProfRecord *NVR = getValueProfRecordNext(VR);
  871. VR->swapBytes(getHostEndianness(), Endianness);
  872. VR = NVR;
  873. }
  874. sys::swapByteOrder<uint32_t>(TotalSize);
  875. sys::swapByteOrder<uint32_t>(NumValueKinds);
  876. }
  877. void annotateValueSite(Module &M, Instruction &Inst,
  878. const InstrProfRecord &InstrProfR,
  879. InstrProfValueKind ValueKind, uint32_t SiteIdx,
  880. uint32_t MaxMDCount) {
  881. uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
  882. if (!NV)
  883. return;
  884. uint64_t Sum = 0;
  885. std::unique_ptr<InstrProfValueData[]> VD =
  886. InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
  887. ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
  888. annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
  889. }
  890. void annotateValueSite(Module &M, Instruction &Inst,
  891. ArrayRef<InstrProfValueData> VDs,
  892. uint64_t Sum, InstrProfValueKind ValueKind,
  893. uint32_t MaxMDCount) {
  894. LLVMContext &Ctx = M.getContext();
  895. MDBuilder MDHelper(Ctx);
  896. SmallVector<Metadata *, 3> Vals;
  897. // Tag
  898. Vals.push_back(MDHelper.createString("VP"));
  899. // Value Kind
  900. Vals.push_back(MDHelper.createConstant(
  901. ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
  902. // Total Count
  903. Vals.push_back(
  904. MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
  905. // Value Profile Data
  906. uint32_t MDCount = MaxMDCount;
  907. for (auto &VD : VDs) {
  908. Vals.push_back(MDHelper.createConstant(
  909. ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
  910. Vals.push_back(MDHelper.createConstant(
  911. ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
  912. if (--MDCount == 0)
  913. break;
  914. }
  915. Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
  916. }
  917. bool getValueProfDataFromInst(const Instruction &Inst,
  918. InstrProfValueKind ValueKind,
  919. uint32_t MaxNumValueData,
  920. InstrProfValueData ValueData[],
  921. uint32_t &ActualNumValueData, uint64_t &TotalC,
  922. bool GetNoICPValue) {
  923. MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
  924. if (!MD)
  925. return false;
  926. unsigned NOps = MD->getNumOperands();
  927. if (NOps < 5)
  928. return false;
  929. // Operand 0 is a string tag "VP":
  930. MDString *Tag = cast<MDString>(MD->getOperand(0));
  931. if (!Tag)
  932. return false;
  933. if (!Tag->getString().equals("VP"))
  934. return false;
  935. // Now check kind:
  936. ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
  937. if (!KindInt)
  938. return false;
  939. if (KindInt->getZExtValue() != ValueKind)
  940. return false;
  941. // Get total count
  942. ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
  943. if (!TotalCInt)
  944. return false;
  945. TotalC = TotalCInt->getZExtValue();
  946. ActualNumValueData = 0;
  947. for (unsigned I = 3; I < NOps; I += 2) {
  948. if (ActualNumValueData >= MaxNumValueData)
  949. break;
  950. ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
  951. ConstantInt *Count =
  952. mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
  953. if (!Value || !Count)
  954. return false;
  955. uint64_t CntValue = Count->getZExtValue();
  956. if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
  957. continue;
  958. ValueData[ActualNumValueData].Value = Value->getZExtValue();
  959. ValueData[ActualNumValueData].Count = CntValue;
  960. ActualNumValueData++;
  961. }
  962. return true;
  963. }
  964. MDNode *getPGOFuncNameMetadata(const Function &F) {
  965. return F.getMetadata(getPGOFuncNameMetadataName());
  966. }
  967. void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
  968. // Only for internal linkage functions.
  969. if (PGOFuncName == F.getName())
  970. return;
  971. // Don't create duplicated meta-data.
  972. if (getPGOFuncNameMetadata(F))
  973. return;
  974. LLVMContext &C = F.getContext();
  975. MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
  976. F.setMetadata(getPGOFuncNameMetadataName(), N);
  977. }
  978. bool needsComdatForCounter(const Function &F, const Module &M) {
  979. if (F.hasComdat())
  980. return true;
  981. if (!Triple(M.getTargetTriple()).supportsCOMDAT())
  982. return false;
  983. // See createPGOFuncNameVar for more details. To avoid link errors, profile
  984. // counters for function with available_externally linkage needs to be changed
  985. // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
  986. // created. Without using comdat, duplicate entries won't be removed by the
  987. // linker leading to increased data segement size and raw profile size. Even
  988. // worse, since the referenced counter from profile per-function data object
  989. // will be resolved to the common strong definition, the profile counts for
  990. // available_externally functions will end up being duplicated in raw profile
  991. // data. This can result in distorted profile as the counts of those dups
  992. // will be accumulated by the profile merger.
  993. GlobalValue::LinkageTypes Linkage = F.getLinkage();
  994. if (Linkage != GlobalValue::ExternalWeakLinkage &&
  995. Linkage != GlobalValue::AvailableExternallyLinkage)
  996. return false;
  997. return true;
  998. }
  999. // Check if INSTR_PROF_RAW_VERSION_VAR is defined.
  1000. bool isIRPGOFlagSet(const Module *M) {
  1001. auto IRInstrVar =
  1002. M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
  1003. if (!IRInstrVar || IRInstrVar->hasLocalLinkage())
  1004. return false;
  1005. // For CSPGO+LTO, this variable might be marked as non-prevailing and we only
  1006. // have the decl.
  1007. if (IRInstrVar->isDeclaration())
  1008. return true;
  1009. // Check if the flag is set.
  1010. if (!IRInstrVar->hasInitializer())
  1011. return false;
  1012. auto *InitVal = dyn_cast_or_null<ConstantInt>(IRInstrVar->getInitializer());
  1013. if (!InitVal)
  1014. return false;
  1015. return (InitVal->getZExtValue() & VARIANT_MASK_IR_PROF) != 0;
  1016. }
  1017. // Check if we can safely rename this Comdat function.
  1018. bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken) {
  1019. if (F.getName().empty())
  1020. return false;
  1021. if (!needsComdatForCounter(F, *(F.getParent())))
  1022. return false;
  1023. // Unsafe to rename the address-taken function (which can be used in
  1024. // function comparison).
  1025. if (CheckAddressTaken && F.hasAddressTaken())
  1026. return false;
  1027. // Only safe to do if this function may be discarded if it is not used
  1028. // in the compilation unit.
  1029. if (!GlobalValue::isDiscardableIfUnused(F.getLinkage()))
  1030. return false;
  1031. // For AvailableExternallyLinkage functions.
  1032. if (!F.hasComdat()) {
  1033. assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage);
  1034. return true;
  1035. }
  1036. return true;
  1037. }
  1038. // Create the variable for the profile file name.
  1039. void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput) {
  1040. if (InstrProfileOutput.empty())
  1041. return;
  1042. Constant *ProfileNameConst =
  1043. ConstantDataArray::getString(M.getContext(), InstrProfileOutput, true);
  1044. GlobalVariable *ProfileNameVar = new GlobalVariable(
  1045. M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
  1046. ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
  1047. Triple TT(M.getTargetTriple());
  1048. if (TT.supportsCOMDAT()) {
  1049. ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
  1050. ProfileNameVar->setComdat(M.getOrInsertComdat(
  1051. StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
  1052. }
  1053. }
  1054. Error OverlapStats::accumulateCounts(const std::string &BaseFilename,
  1055. const std::string &TestFilename,
  1056. bool IsCS) {
  1057. auto getProfileSum = [IsCS](const std::string &Filename,
  1058. CountSumOrPercent &Sum) -> Error {
  1059. auto ReaderOrErr = InstrProfReader::create(Filename);
  1060. if (Error E = ReaderOrErr.takeError()) {
  1061. return E;
  1062. }
  1063. auto Reader = std::move(ReaderOrErr.get());
  1064. Reader->accumulateCounts(Sum, IsCS);
  1065. return Error::success();
  1066. };
  1067. auto Ret = getProfileSum(BaseFilename, Base);
  1068. if (Ret)
  1069. return Ret;
  1070. Ret = getProfileSum(TestFilename, Test);
  1071. if (Ret)
  1072. return Ret;
  1073. this->BaseFilename = &BaseFilename;
  1074. this->TestFilename = &TestFilename;
  1075. Valid = true;
  1076. return Error::success();
  1077. }
  1078. void OverlapStats::addOneMismatch(const CountSumOrPercent &MismatchFunc) {
  1079. Mismatch.NumEntries += 1;
  1080. Mismatch.CountSum += MismatchFunc.CountSum / Test.CountSum;
  1081. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1082. if (Test.ValueCounts[I] >= 1.0f)
  1083. Mismatch.ValueCounts[I] +=
  1084. MismatchFunc.ValueCounts[I] / Test.ValueCounts[I];
  1085. }
  1086. }
  1087. void OverlapStats::addOneUnique(const CountSumOrPercent &UniqueFunc) {
  1088. Unique.NumEntries += 1;
  1089. Unique.CountSum += UniqueFunc.CountSum / Test.CountSum;
  1090. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1091. if (Test.ValueCounts[I] >= 1.0f)
  1092. Unique.ValueCounts[I] += UniqueFunc.ValueCounts[I] / Test.ValueCounts[I];
  1093. }
  1094. }
  1095. void OverlapStats::dump(raw_fd_ostream &OS) const {
  1096. if (!Valid)
  1097. return;
  1098. const char *EntryName =
  1099. (Level == ProgramLevel ? "functions" : "edge counters");
  1100. if (Level == ProgramLevel) {
  1101. OS << "Profile overlap infomation for base_profile: " << *BaseFilename
  1102. << " and test_profile: " << *TestFilename << "\nProgram level:\n";
  1103. } else {
  1104. OS << "Function level:\n"
  1105. << " Function: " << FuncName << " (Hash=" << FuncHash << ")\n";
  1106. }
  1107. OS << " # of " << EntryName << " overlap: " << Overlap.NumEntries << "\n";
  1108. if (Mismatch.NumEntries)
  1109. OS << " # of " << EntryName << " mismatch: " << Mismatch.NumEntries
  1110. << "\n";
  1111. if (Unique.NumEntries)
  1112. OS << " # of " << EntryName
  1113. << " only in test_profile: " << Unique.NumEntries << "\n";
  1114. OS << " Edge profile overlap: " << format("%.3f%%", Overlap.CountSum * 100)
  1115. << "\n";
  1116. if (Mismatch.NumEntries)
  1117. OS << " Mismatched count percentage (Edge): "
  1118. << format("%.3f%%", Mismatch.CountSum * 100) << "\n";
  1119. if (Unique.NumEntries)
  1120. OS << " Percentage of Edge profile only in test_profile: "
  1121. << format("%.3f%%", Unique.CountSum * 100) << "\n";
  1122. OS << " Edge profile base count sum: " << format("%.0f", Base.CountSum)
  1123. << "\n"
  1124. << " Edge profile test count sum: " << format("%.0f", Test.CountSum)
  1125. << "\n";
  1126. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1127. if (Base.ValueCounts[I] < 1.0f && Test.ValueCounts[I] < 1.0f)
  1128. continue;
  1129. char ProfileKindName[20];
  1130. switch (I) {
  1131. case IPVK_IndirectCallTarget:
  1132. strncpy(ProfileKindName, "IndirectCall", 19);
  1133. break;
  1134. case IPVK_MemOPSize:
  1135. strncpy(ProfileKindName, "MemOP", 19);
  1136. break;
  1137. default:
  1138. snprintf(ProfileKindName, 19, "VP[%d]", I);
  1139. break;
  1140. }
  1141. OS << " " << ProfileKindName
  1142. << " profile overlap: " << format("%.3f%%", Overlap.ValueCounts[I] * 100)
  1143. << "\n";
  1144. if (Mismatch.NumEntries)
  1145. OS << " Mismatched count percentage (" << ProfileKindName
  1146. << "): " << format("%.3f%%", Mismatch.ValueCounts[I] * 100) << "\n";
  1147. if (Unique.NumEntries)
  1148. OS << " Percentage of " << ProfileKindName
  1149. << " profile only in test_profile: "
  1150. << format("%.3f%%", Unique.ValueCounts[I] * 100) << "\n";
  1151. OS << " " << ProfileKindName
  1152. << " profile base count sum: " << format("%.0f", Base.ValueCounts[I])
  1153. << "\n"
  1154. << " " << ProfileKindName
  1155. << " profile test count sum: " << format("%.0f", Test.ValueCounts[I])
  1156. << "\n";
  1157. }
  1158. }
  1159. } // end namespace llvm