InstrProf.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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/MathExtras.h"
  42. #include "llvm/Support/Path.h"
  43. #include "llvm/Support/SwapByteOrder.h"
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstddef>
  47. #include <cstdint>
  48. #include <cstring>
  49. #include <memory>
  50. #include <string>
  51. #include <system_error>
  52. #include <type_traits>
  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. const std::error_category &llvm::instrprof_category() {
  167. static InstrProfErrorCategoryType ErrorCategory;
  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. SmallVector<uint8_t, 128> CompressedNameStrings;
  415. compression::zlib::compress(arrayRefFromStringRef(UncompressedNameStrings),
  416. CompressedNameStrings,
  417. compression::zlib::BestSizeCompression);
  418. return WriteStringToResult(CompressedNameStrings.size(),
  419. toStringRef(CompressedNameStrings));
  420. }
  421. StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
  422. auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
  423. StringRef NameStr =
  424. Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
  425. return NameStr;
  426. }
  427. Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
  428. std::string &Result, bool doCompression) {
  429. std::vector<std::string> NameStrs;
  430. for (auto *NameVar : NameVars) {
  431. NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar)));
  432. }
  433. return collectPGOFuncNameStrings(
  434. NameStrs, compression::zlib::isAvailable() && doCompression, Result);
  435. }
  436. Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
  437. const uint8_t *P = NameStrings.bytes_begin();
  438. const uint8_t *EndP = NameStrings.bytes_end();
  439. while (P < EndP) {
  440. uint32_t N;
  441. uint64_t UncompressedSize = decodeULEB128(P, &N);
  442. P += N;
  443. uint64_t CompressedSize = decodeULEB128(P, &N);
  444. P += N;
  445. bool isCompressed = (CompressedSize != 0);
  446. SmallVector<uint8_t, 128> UncompressedNameStrings;
  447. StringRef NameStrings;
  448. if (isCompressed) {
  449. if (!llvm::compression::zlib::isAvailable())
  450. return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
  451. if (Error E = compression::zlib::decompress(ArrayRef(P, CompressedSize),
  452. UncompressedNameStrings,
  453. UncompressedSize)) {
  454. consumeError(std::move(E));
  455. return make_error<InstrProfError>(instrprof_error::uncompress_failed);
  456. }
  457. P += CompressedSize;
  458. NameStrings = toStringRef(UncompressedNameStrings);
  459. } else {
  460. NameStrings =
  461. StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
  462. P += UncompressedSize;
  463. }
  464. // Now parse the name strings.
  465. SmallVector<StringRef, 0> Names;
  466. NameStrings.split(Names, getInstrProfNameSeparator());
  467. for (StringRef &Name : Names)
  468. if (Error E = Symtab.addFuncName(Name))
  469. return E;
  470. while (P < EndP && *P == 0)
  471. P++;
  472. }
  473. return Error::success();
  474. }
  475. void InstrProfRecord::accumulateCounts(CountSumOrPercent &Sum) const {
  476. uint64_t FuncSum = 0;
  477. Sum.NumEntries += Counts.size();
  478. for (uint64_t Count : Counts)
  479. FuncSum += Count;
  480. Sum.CountSum += FuncSum;
  481. for (uint32_t VK = IPVK_First; VK <= IPVK_Last; ++VK) {
  482. uint64_t KindSum = 0;
  483. uint32_t NumValueSites = getNumValueSites(VK);
  484. for (size_t I = 0; I < NumValueSites; ++I) {
  485. uint32_t NV = getNumValueDataForSite(VK, I);
  486. std::unique_ptr<InstrProfValueData[]> VD = getValueForSite(VK, I);
  487. for (uint32_t V = 0; V < NV; V++)
  488. KindSum += VD[V].Count;
  489. }
  490. Sum.ValueCounts[VK] += KindSum;
  491. }
  492. }
  493. void InstrProfValueSiteRecord::overlap(InstrProfValueSiteRecord &Input,
  494. uint32_t ValueKind,
  495. OverlapStats &Overlap,
  496. OverlapStats &FuncLevelOverlap) {
  497. this->sortByTargetValues();
  498. Input.sortByTargetValues();
  499. double Score = 0.0f, FuncLevelScore = 0.0f;
  500. auto I = ValueData.begin();
  501. auto IE = ValueData.end();
  502. auto J = Input.ValueData.begin();
  503. auto JE = Input.ValueData.end();
  504. while (I != IE && J != JE) {
  505. if (I->Value == J->Value) {
  506. Score += OverlapStats::score(I->Count, J->Count,
  507. Overlap.Base.ValueCounts[ValueKind],
  508. Overlap.Test.ValueCounts[ValueKind]);
  509. FuncLevelScore += OverlapStats::score(
  510. I->Count, J->Count, FuncLevelOverlap.Base.ValueCounts[ValueKind],
  511. FuncLevelOverlap.Test.ValueCounts[ValueKind]);
  512. ++I;
  513. } else if (I->Value < J->Value) {
  514. ++I;
  515. continue;
  516. }
  517. ++J;
  518. }
  519. Overlap.Overlap.ValueCounts[ValueKind] += Score;
  520. FuncLevelOverlap.Overlap.ValueCounts[ValueKind] += FuncLevelScore;
  521. }
  522. // Return false on mismatch.
  523. void InstrProfRecord::overlapValueProfData(uint32_t ValueKind,
  524. InstrProfRecord &Other,
  525. OverlapStats &Overlap,
  526. OverlapStats &FuncLevelOverlap) {
  527. uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
  528. assert(ThisNumValueSites == Other.getNumValueSites(ValueKind));
  529. if (!ThisNumValueSites)
  530. return;
  531. std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
  532. getOrCreateValueSitesForKind(ValueKind);
  533. MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
  534. Other.getValueSitesForKind(ValueKind);
  535. for (uint32_t I = 0; I < ThisNumValueSites; I++)
  536. ThisSiteRecords[I].overlap(OtherSiteRecords[I], ValueKind, Overlap,
  537. FuncLevelOverlap);
  538. }
  539. void InstrProfRecord::overlap(InstrProfRecord &Other, OverlapStats &Overlap,
  540. OverlapStats &FuncLevelOverlap,
  541. uint64_t ValueCutoff) {
  542. // FuncLevel CountSum for other should already computed and nonzero.
  543. assert(FuncLevelOverlap.Test.CountSum >= 1.0f);
  544. accumulateCounts(FuncLevelOverlap.Base);
  545. bool Mismatch = (Counts.size() != Other.Counts.size());
  546. // Check if the value profiles mismatch.
  547. if (!Mismatch) {
  548. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) {
  549. uint32_t ThisNumValueSites = getNumValueSites(Kind);
  550. uint32_t OtherNumValueSites = Other.getNumValueSites(Kind);
  551. if (ThisNumValueSites != OtherNumValueSites) {
  552. Mismatch = true;
  553. break;
  554. }
  555. }
  556. }
  557. if (Mismatch) {
  558. Overlap.addOneMismatch(FuncLevelOverlap.Test);
  559. return;
  560. }
  561. // Compute overlap for value counts.
  562. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  563. overlapValueProfData(Kind, Other, Overlap, FuncLevelOverlap);
  564. double Score = 0.0;
  565. uint64_t MaxCount = 0;
  566. // Compute overlap for edge counts.
  567. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
  568. Score += OverlapStats::score(Counts[I], Other.Counts[I],
  569. Overlap.Base.CountSum, Overlap.Test.CountSum);
  570. MaxCount = std::max(Other.Counts[I], MaxCount);
  571. }
  572. Overlap.Overlap.CountSum += Score;
  573. Overlap.Overlap.NumEntries += 1;
  574. if (MaxCount >= ValueCutoff) {
  575. double FuncScore = 0.0;
  576. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I)
  577. FuncScore += OverlapStats::score(Counts[I], Other.Counts[I],
  578. FuncLevelOverlap.Base.CountSum,
  579. FuncLevelOverlap.Test.CountSum);
  580. FuncLevelOverlap.Overlap.CountSum = FuncScore;
  581. FuncLevelOverlap.Overlap.NumEntries = Other.Counts.size();
  582. FuncLevelOverlap.Valid = true;
  583. }
  584. }
  585. void InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
  586. uint64_t Weight,
  587. function_ref<void(instrprof_error)> Warn) {
  588. this->sortByTargetValues();
  589. Input.sortByTargetValues();
  590. auto I = ValueData.begin();
  591. auto IE = ValueData.end();
  592. for (const InstrProfValueData &J : Input.ValueData) {
  593. while (I != IE && I->Value < J.Value)
  594. ++I;
  595. if (I != IE && I->Value == J.Value) {
  596. bool Overflowed;
  597. I->Count = SaturatingMultiplyAdd(J.Count, Weight, I->Count, &Overflowed);
  598. if (Overflowed)
  599. Warn(instrprof_error::counter_overflow);
  600. ++I;
  601. continue;
  602. }
  603. ValueData.insert(I, J);
  604. }
  605. }
  606. void InstrProfValueSiteRecord::scale(uint64_t N, uint64_t D,
  607. function_ref<void(instrprof_error)> Warn) {
  608. for (InstrProfValueData &I : ValueData) {
  609. bool Overflowed;
  610. I.Count = SaturatingMultiply(I.Count, N, &Overflowed) / D;
  611. if (Overflowed)
  612. Warn(instrprof_error::counter_overflow);
  613. }
  614. }
  615. // Merge Value Profile data from Src record to this record for ValueKind.
  616. // Scale merged value counts by \p Weight.
  617. void InstrProfRecord::mergeValueProfData(
  618. uint32_t ValueKind, InstrProfRecord &Src, uint64_t Weight,
  619. function_ref<void(instrprof_error)> Warn) {
  620. uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
  621. uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
  622. if (ThisNumValueSites != OtherNumValueSites) {
  623. Warn(instrprof_error::value_site_count_mismatch);
  624. return;
  625. }
  626. if (!ThisNumValueSites)
  627. return;
  628. std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
  629. getOrCreateValueSitesForKind(ValueKind);
  630. MutableArrayRef<InstrProfValueSiteRecord> OtherSiteRecords =
  631. Src.getValueSitesForKind(ValueKind);
  632. for (uint32_t I = 0; I < ThisNumValueSites; I++)
  633. ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight, Warn);
  634. }
  635. void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight,
  636. function_ref<void(instrprof_error)> Warn) {
  637. // If the number of counters doesn't match we either have bad data
  638. // or a hash collision.
  639. if (Counts.size() != Other.Counts.size()) {
  640. Warn(instrprof_error::count_mismatch);
  641. return;
  642. }
  643. // Special handling of the first count as the PseudoCount.
  644. CountPseudoKind OtherKind = Other.getCountPseudoKind();
  645. CountPseudoKind ThisKind = getCountPseudoKind();
  646. if (OtherKind != NotPseudo || ThisKind != NotPseudo) {
  647. // We don't allow the merge of a profile with pseudo counts and
  648. // a normal profile (i.e. without pesudo counts).
  649. // Profile supplimenation should be done after the profile merge.
  650. if (OtherKind == NotPseudo || ThisKind == NotPseudo) {
  651. Warn(instrprof_error::count_mismatch);
  652. return;
  653. }
  654. if (OtherKind == PseudoHot || ThisKind == PseudoHot)
  655. setPseudoCount(PseudoHot);
  656. else
  657. setPseudoCount(PseudoWarm);
  658. return;
  659. }
  660. for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
  661. bool Overflowed;
  662. uint64_t Value =
  663. SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
  664. if (Value > getInstrMaxCountValue()) {
  665. Value = getInstrMaxCountValue();
  666. Overflowed = true;
  667. }
  668. Counts[I] = Value;
  669. if (Overflowed)
  670. Warn(instrprof_error::counter_overflow);
  671. }
  672. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  673. mergeValueProfData(Kind, Other, Weight, Warn);
  674. }
  675. void InstrProfRecord::scaleValueProfData(
  676. uint32_t ValueKind, uint64_t N, uint64_t D,
  677. function_ref<void(instrprof_error)> Warn) {
  678. for (auto &R : getValueSitesForKind(ValueKind))
  679. R.scale(N, D, Warn);
  680. }
  681. void InstrProfRecord::scale(uint64_t N, uint64_t D,
  682. function_ref<void(instrprof_error)> Warn) {
  683. assert(D != 0 && "D cannot be 0");
  684. for (auto &Count : this->Counts) {
  685. bool Overflowed;
  686. Count = SaturatingMultiply(Count, N, &Overflowed) / D;
  687. if (Count > getInstrMaxCountValue()) {
  688. Count = getInstrMaxCountValue();
  689. Overflowed = true;
  690. }
  691. if (Overflowed)
  692. Warn(instrprof_error::counter_overflow);
  693. }
  694. for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
  695. scaleValueProfData(Kind, N, D, Warn);
  696. }
  697. // Map indirect call target name hash to name string.
  698. uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
  699. InstrProfSymtab *SymTab) {
  700. if (!SymTab)
  701. return Value;
  702. if (ValueKind == IPVK_IndirectCallTarget)
  703. return SymTab->getFunctionHashFromAddress(Value);
  704. return Value;
  705. }
  706. void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
  707. InstrProfValueData *VData, uint32_t N,
  708. InstrProfSymtab *ValueMap) {
  709. for (uint32_t I = 0; I < N; I++) {
  710. VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
  711. }
  712. std::vector<InstrProfValueSiteRecord> &ValueSites =
  713. getOrCreateValueSitesForKind(ValueKind);
  714. if (N == 0)
  715. ValueSites.emplace_back();
  716. else
  717. ValueSites.emplace_back(VData, VData + N);
  718. }
  719. #define INSTR_PROF_COMMON_API_IMPL
  720. #include "llvm/ProfileData/InstrProfData.inc"
  721. /*!
  722. * ValueProfRecordClosure Interface implementation for InstrProfRecord
  723. * class. These C wrappers are used as adaptors so that C++ code can be
  724. * invoked as callbacks.
  725. */
  726. uint32_t getNumValueKindsInstrProf(const void *Record) {
  727. return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
  728. }
  729. uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
  730. return reinterpret_cast<const InstrProfRecord *>(Record)
  731. ->getNumValueSites(VKind);
  732. }
  733. uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
  734. return reinterpret_cast<const InstrProfRecord *>(Record)
  735. ->getNumValueData(VKind);
  736. }
  737. uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
  738. uint32_t S) {
  739. return reinterpret_cast<const InstrProfRecord *>(R)
  740. ->getNumValueDataForSite(VK, S);
  741. }
  742. void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
  743. uint32_t K, uint32_t S) {
  744. reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
  745. }
  746. ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
  747. ValueProfData *VD =
  748. (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
  749. memset(VD, 0, TotalSizeInBytes);
  750. return VD;
  751. }
  752. static ValueProfRecordClosure InstrProfRecordClosure = {
  753. nullptr,
  754. getNumValueKindsInstrProf,
  755. getNumValueSitesInstrProf,
  756. getNumValueDataInstrProf,
  757. getNumValueDataForSiteInstrProf,
  758. nullptr,
  759. getValueForSiteInstrProf,
  760. allocValueProfDataInstrProf};
  761. // Wrapper implementation using the closure mechanism.
  762. uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
  763. auto Closure = InstrProfRecordClosure;
  764. Closure.Record = &Record;
  765. return getValueProfDataSize(&Closure);
  766. }
  767. // Wrapper implementation using the closure mechanism.
  768. std::unique_ptr<ValueProfData>
  769. ValueProfData::serializeFrom(const InstrProfRecord &Record) {
  770. InstrProfRecordClosure.Record = &Record;
  771. std::unique_ptr<ValueProfData> VPD(
  772. serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
  773. return VPD;
  774. }
  775. void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
  776. InstrProfSymtab *SymTab) {
  777. Record.reserveSites(Kind, NumValueSites);
  778. InstrProfValueData *ValueData = getValueProfRecordValueData(this);
  779. for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
  780. uint8_t ValueDataCount = this->SiteCountArray[VSite];
  781. Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
  782. ValueData += ValueDataCount;
  783. }
  784. }
  785. // For writing/serializing, Old is the host endianness, and New is
  786. // byte order intended on disk. For Reading/deserialization, Old
  787. // is the on-disk source endianness, and New is the host endianness.
  788. void ValueProfRecord::swapBytes(support::endianness Old,
  789. support::endianness New) {
  790. using namespace support;
  791. if (Old == New)
  792. return;
  793. if (getHostEndianness() != Old) {
  794. sys::swapByteOrder<uint32_t>(NumValueSites);
  795. sys::swapByteOrder<uint32_t>(Kind);
  796. }
  797. uint32_t ND = getValueProfRecordNumValueData(this);
  798. InstrProfValueData *VD = getValueProfRecordValueData(this);
  799. // No need to swap byte array: SiteCountArrray.
  800. for (uint32_t I = 0; I < ND; I++) {
  801. sys::swapByteOrder<uint64_t>(VD[I].Value);
  802. sys::swapByteOrder<uint64_t>(VD[I].Count);
  803. }
  804. if (getHostEndianness() == Old) {
  805. sys::swapByteOrder<uint32_t>(NumValueSites);
  806. sys::swapByteOrder<uint32_t>(Kind);
  807. }
  808. }
  809. void ValueProfData::deserializeTo(InstrProfRecord &Record,
  810. InstrProfSymtab *SymTab) {
  811. if (NumValueKinds == 0)
  812. return;
  813. ValueProfRecord *VR = getFirstValueProfRecord(this);
  814. for (uint32_t K = 0; K < NumValueKinds; K++) {
  815. VR->deserializeTo(Record, SymTab);
  816. VR = getValueProfRecordNext(VR);
  817. }
  818. }
  819. template <class T>
  820. static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
  821. using namespace support;
  822. if (Orig == little)
  823. return endian::readNext<T, little, unaligned>(D);
  824. else
  825. return endian::readNext<T, big, unaligned>(D);
  826. }
  827. static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
  828. return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
  829. ValueProfData());
  830. }
  831. Error ValueProfData::checkIntegrity() {
  832. if (NumValueKinds > IPVK_Last + 1)
  833. return make_error<InstrProfError>(
  834. instrprof_error::malformed, "number of value profile kinds is invalid");
  835. // Total size needs to be multiple of quadword size.
  836. if (TotalSize % sizeof(uint64_t))
  837. return make_error<InstrProfError>(
  838. instrprof_error::malformed, "total size is not multiples of quardword");
  839. ValueProfRecord *VR = getFirstValueProfRecord(this);
  840. for (uint32_t K = 0; K < this->NumValueKinds; K++) {
  841. if (VR->Kind > IPVK_Last)
  842. return make_error<InstrProfError>(instrprof_error::malformed,
  843. "value kind is invalid");
  844. VR = getValueProfRecordNext(VR);
  845. if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
  846. return make_error<InstrProfError>(
  847. instrprof_error::malformed,
  848. "value profile address is greater than total size");
  849. }
  850. return Error::success();
  851. }
  852. Expected<std::unique_ptr<ValueProfData>>
  853. ValueProfData::getValueProfData(const unsigned char *D,
  854. const unsigned char *const BufferEnd,
  855. support::endianness Endianness) {
  856. using namespace support;
  857. if (D + sizeof(ValueProfData) > BufferEnd)
  858. return make_error<InstrProfError>(instrprof_error::truncated);
  859. const unsigned char *Header = D;
  860. uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
  861. if (D + TotalSize > BufferEnd)
  862. return make_error<InstrProfError>(instrprof_error::too_large);
  863. std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
  864. memcpy(VPD.get(), D, TotalSize);
  865. // Byte swap.
  866. VPD->swapBytesToHost(Endianness);
  867. Error E = VPD->checkIntegrity();
  868. if (E)
  869. return std::move(E);
  870. return std::move(VPD);
  871. }
  872. void ValueProfData::swapBytesToHost(support::endianness Endianness) {
  873. using namespace support;
  874. if (Endianness == getHostEndianness())
  875. return;
  876. sys::swapByteOrder<uint32_t>(TotalSize);
  877. sys::swapByteOrder<uint32_t>(NumValueKinds);
  878. ValueProfRecord *VR = getFirstValueProfRecord(this);
  879. for (uint32_t K = 0; K < NumValueKinds; K++) {
  880. VR->swapBytes(Endianness, getHostEndianness());
  881. VR = getValueProfRecordNext(VR);
  882. }
  883. }
  884. void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
  885. using namespace support;
  886. if (Endianness == getHostEndianness())
  887. return;
  888. ValueProfRecord *VR = getFirstValueProfRecord(this);
  889. for (uint32_t K = 0; K < NumValueKinds; K++) {
  890. ValueProfRecord *NVR = getValueProfRecordNext(VR);
  891. VR->swapBytes(getHostEndianness(), Endianness);
  892. VR = NVR;
  893. }
  894. sys::swapByteOrder<uint32_t>(TotalSize);
  895. sys::swapByteOrder<uint32_t>(NumValueKinds);
  896. }
  897. void annotateValueSite(Module &M, Instruction &Inst,
  898. const InstrProfRecord &InstrProfR,
  899. InstrProfValueKind ValueKind, uint32_t SiteIdx,
  900. uint32_t MaxMDCount) {
  901. uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
  902. if (!NV)
  903. return;
  904. uint64_t Sum = 0;
  905. std::unique_ptr<InstrProfValueData[]> VD =
  906. InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
  907. ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
  908. annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
  909. }
  910. void annotateValueSite(Module &M, Instruction &Inst,
  911. ArrayRef<InstrProfValueData> VDs,
  912. uint64_t Sum, InstrProfValueKind ValueKind,
  913. uint32_t MaxMDCount) {
  914. LLVMContext &Ctx = M.getContext();
  915. MDBuilder MDHelper(Ctx);
  916. SmallVector<Metadata *, 3> Vals;
  917. // Tag
  918. Vals.push_back(MDHelper.createString("VP"));
  919. // Value Kind
  920. Vals.push_back(MDHelper.createConstant(
  921. ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
  922. // Total Count
  923. Vals.push_back(
  924. MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
  925. // Value Profile Data
  926. uint32_t MDCount = MaxMDCount;
  927. for (auto &VD : VDs) {
  928. Vals.push_back(MDHelper.createConstant(
  929. ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
  930. Vals.push_back(MDHelper.createConstant(
  931. ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
  932. if (--MDCount == 0)
  933. break;
  934. }
  935. Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
  936. }
  937. bool getValueProfDataFromInst(const Instruction &Inst,
  938. InstrProfValueKind ValueKind,
  939. uint32_t MaxNumValueData,
  940. InstrProfValueData ValueData[],
  941. uint32_t &ActualNumValueData, uint64_t &TotalC,
  942. bool GetNoICPValue) {
  943. MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
  944. if (!MD)
  945. return false;
  946. unsigned NOps = MD->getNumOperands();
  947. if (NOps < 5)
  948. return false;
  949. // Operand 0 is a string tag "VP":
  950. MDString *Tag = cast<MDString>(MD->getOperand(0));
  951. if (!Tag)
  952. return false;
  953. if (!Tag->getString().equals("VP"))
  954. return false;
  955. // Now check kind:
  956. ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
  957. if (!KindInt)
  958. return false;
  959. if (KindInt->getZExtValue() != ValueKind)
  960. return false;
  961. // Get total count
  962. ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
  963. if (!TotalCInt)
  964. return false;
  965. TotalC = TotalCInt->getZExtValue();
  966. ActualNumValueData = 0;
  967. for (unsigned I = 3; I < NOps; I += 2) {
  968. if (ActualNumValueData >= MaxNumValueData)
  969. break;
  970. ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
  971. ConstantInt *Count =
  972. mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
  973. if (!Value || !Count)
  974. return false;
  975. uint64_t CntValue = Count->getZExtValue();
  976. if (!GetNoICPValue && (CntValue == NOMORE_ICP_MAGICNUM))
  977. continue;
  978. ValueData[ActualNumValueData].Value = Value->getZExtValue();
  979. ValueData[ActualNumValueData].Count = CntValue;
  980. ActualNumValueData++;
  981. }
  982. return true;
  983. }
  984. MDNode *getPGOFuncNameMetadata(const Function &F) {
  985. return F.getMetadata(getPGOFuncNameMetadataName());
  986. }
  987. void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
  988. // Only for internal linkage functions.
  989. if (PGOFuncName == F.getName())
  990. return;
  991. // Don't create duplicated meta-data.
  992. if (getPGOFuncNameMetadata(F))
  993. return;
  994. LLVMContext &C = F.getContext();
  995. MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
  996. F.setMetadata(getPGOFuncNameMetadataName(), N);
  997. }
  998. bool needsComdatForCounter(const Function &F, const Module &M) {
  999. if (F.hasComdat())
  1000. return true;
  1001. if (!Triple(M.getTargetTriple()).supportsCOMDAT())
  1002. return false;
  1003. // See createPGOFuncNameVar for more details. To avoid link errors, profile
  1004. // counters for function with available_externally linkage needs to be changed
  1005. // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
  1006. // created. Without using comdat, duplicate entries won't be removed by the
  1007. // linker leading to increased data segement size and raw profile size. Even
  1008. // worse, since the referenced counter from profile per-function data object
  1009. // will be resolved to the common strong definition, the profile counts for
  1010. // available_externally functions will end up being duplicated in raw profile
  1011. // data. This can result in distorted profile as the counts of those dups
  1012. // will be accumulated by the profile merger.
  1013. GlobalValue::LinkageTypes Linkage = F.getLinkage();
  1014. if (Linkage != GlobalValue::ExternalWeakLinkage &&
  1015. Linkage != GlobalValue::AvailableExternallyLinkage)
  1016. return false;
  1017. return true;
  1018. }
  1019. // Check if INSTR_PROF_RAW_VERSION_VAR is defined.
  1020. bool isIRPGOFlagSet(const Module *M) {
  1021. auto IRInstrVar =
  1022. M->getNamedGlobal(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
  1023. if (!IRInstrVar || IRInstrVar->hasLocalLinkage())
  1024. return false;
  1025. // For CSPGO+LTO, this variable might be marked as non-prevailing and we only
  1026. // have the decl.
  1027. if (IRInstrVar->isDeclaration())
  1028. return true;
  1029. // Check if the flag is set.
  1030. if (!IRInstrVar->hasInitializer())
  1031. return false;
  1032. auto *InitVal = dyn_cast_or_null<ConstantInt>(IRInstrVar->getInitializer());
  1033. if (!InitVal)
  1034. return false;
  1035. return (InitVal->getZExtValue() & VARIANT_MASK_IR_PROF) != 0;
  1036. }
  1037. // Check if we can safely rename this Comdat function.
  1038. bool canRenameComdatFunc(const Function &F, bool CheckAddressTaken) {
  1039. if (F.getName().empty())
  1040. return false;
  1041. if (!needsComdatForCounter(F, *(F.getParent())))
  1042. return false;
  1043. // Unsafe to rename the address-taken function (which can be used in
  1044. // function comparison).
  1045. if (CheckAddressTaken && F.hasAddressTaken())
  1046. return false;
  1047. // Only safe to do if this function may be discarded if it is not used
  1048. // in the compilation unit.
  1049. if (!GlobalValue::isDiscardableIfUnused(F.getLinkage()))
  1050. return false;
  1051. // For AvailableExternallyLinkage functions.
  1052. if (!F.hasComdat()) {
  1053. assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage);
  1054. return true;
  1055. }
  1056. return true;
  1057. }
  1058. // Create the variable for the profile file name.
  1059. void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput) {
  1060. if (InstrProfileOutput.empty())
  1061. return;
  1062. Constant *ProfileNameConst =
  1063. ConstantDataArray::getString(M.getContext(), InstrProfileOutput, true);
  1064. GlobalVariable *ProfileNameVar = new GlobalVariable(
  1065. M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage,
  1066. ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR));
  1067. ProfileNameVar->setVisibility(GlobalValue::HiddenVisibility);
  1068. Triple TT(M.getTargetTriple());
  1069. if (TT.supportsCOMDAT()) {
  1070. ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
  1071. ProfileNameVar->setComdat(M.getOrInsertComdat(
  1072. StringRef(INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR))));
  1073. }
  1074. }
  1075. Error OverlapStats::accumulateCounts(const std::string &BaseFilename,
  1076. const std::string &TestFilename,
  1077. bool IsCS) {
  1078. auto getProfileSum = [IsCS](const std::string &Filename,
  1079. CountSumOrPercent &Sum) -> Error {
  1080. auto ReaderOrErr = InstrProfReader::create(Filename);
  1081. if (Error E = ReaderOrErr.takeError()) {
  1082. return E;
  1083. }
  1084. auto Reader = std::move(ReaderOrErr.get());
  1085. Reader->accumulateCounts(Sum, IsCS);
  1086. return Error::success();
  1087. };
  1088. auto Ret = getProfileSum(BaseFilename, Base);
  1089. if (Ret)
  1090. return Ret;
  1091. Ret = getProfileSum(TestFilename, Test);
  1092. if (Ret)
  1093. return Ret;
  1094. this->BaseFilename = &BaseFilename;
  1095. this->TestFilename = &TestFilename;
  1096. Valid = true;
  1097. return Error::success();
  1098. }
  1099. void OverlapStats::addOneMismatch(const CountSumOrPercent &MismatchFunc) {
  1100. Mismatch.NumEntries += 1;
  1101. Mismatch.CountSum += MismatchFunc.CountSum / Test.CountSum;
  1102. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1103. if (Test.ValueCounts[I] >= 1.0f)
  1104. Mismatch.ValueCounts[I] +=
  1105. MismatchFunc.ValueCounts[I] / Test.ValueCounts[I];
  1106. }
  1107. }
  1108. void OverlapStats::addOneUnique(const CountSumOrPercent &UniqueFunc) {
  1109. Unique.NumEntries += 1;
  1110. Unique.CountSum += UniqueFunc.CountSum / Test.CountSum;
  1111. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1112. if (Test.ValueCounts[I] >= 1.0f)
  1113. Unique.ValueCounts[I] += UniqueFunc.ValueCounts[I] / Test.ValueCounts[I];
  1114. }
  1115. }
  1116. void OverlapStats::dump(raw_fd_ostream &OS) const {
  1117. if (!Valid)
  1118. return;
  1119. const char *EntryName =
  1120. (Level == ProgramLevel ? "functions" : "edge counters");
  1121. if (Level == ProgramLevel) {
  1122. OS << "Profile overlap infomation for base_profile: " << *BaseFilename
  1123. << " and test_profile: " << *TestFilename << "\nProgram level:\n";
  1124. } else {
  1125. OS << "Function level:\n"
  1126. << " Function: " << FuncName << " (Hash=" << FuncHash << ")\n";
  1127. }
  1128. OS << " # of " << EntryName << " overlap: " << Overlap.NumEntries << "\n";
  1129. if (Mismatch.NumEntries)
  1130. OS << " # of " << EntryName << " mismatch: " << Mismatch.NumEntries
  1131. << "\n";
  1132. if (Unique.NumEntries)
  1133. OS << " # of " << EntryName
  1134. << " only in test_profile: " << Unique.NumEntries << "\n";
  1135. OS << " Edge profile overlap: " << format("%.3f%%", Overlap.CountSum * 100)
  1136. << "\n";
  1137. if (Mismatch.NumEntries)
  1138. OS << " Mismatched count percentage (Edge): "
  1139. << format("%.3f%%", Mismatch.CountSum * 100) << "\n";
  1140. if (Unique.NumEntries)
  1141. OS << " Percentage of Edge profile only in test_profile: "
  1142. << format("%.3f%%", Unique.CountSum * 100) << "\n";
  1143. OS << " Edge profile base count sum: " << format("%.0f", Base.CountSum)
  1144. << "\n"
  1145. << " Edge profile test count sum: " << format("%.0f", Test.CountSum)
  1146. << "\n";
  1147. for (unsigned I = 0; I < IPVK_Last - IPVK_First + 1; I++) {
  1148. if (Base.ValueCounts[I] < 1.0f && Test.ValueCounts[I] < 1.0f)
  1149. continue;
  1150. char ProfileKindName[20];
  1151. switch (I) {
  1152. case IPVK_IndirectCallTarget:
  1153. strncpy(ProfileKindName, "IndirectCall", 19);
  1154. break;
  1155. case IPVK_MemOPSize:
  1156. strncpy(ProfileKindName, "MemOP", 19);
  1157. break;
  1158. default:
  1159. snprintf(ProfileKindName, 19, "VP[%d]", I);
  1160. break;
  1161. }
  1162. OS << " " << ProfileKindName
  1163. << " profile overlap: " << format("%.3f%%", Overlap.ValueCounts[I] * 100)
  1164. << "\n";
  1165. if (Mismatch.NumEntries)
  1166. OS << " Mismatched count percentage (" << ProfileKindName
  1167. << "): " << format("%.3f%%", Mismatch.ValueCounts[I] * 100) << "\n";
  1168. if (Unique.NumEntries)
  1169. OS << " Percentage of " << ProfileKindName
  1170. << " profile only in test_profile: "
  1171. << format("%.3f%%", Unique.ValueCounts[I] * 100) << "\n";
  1172. OS << " " << ProfileKindName
  1173. << " profile base count sum: " << format("%.0f", Base.ValueCounts[I])
  1174. << "\n"
  1175. << " " << ProfileKindName
  1176. << " profile test count sum: " << format("%.0f", Test.ValueCounts[I])
  1177. << "\n";
  1178. }
  1179. }
  1180. namespace IndexedInstrProf {
  1181. // A C++14 compatible version of the offsetof macro.
  1182. template <typename T1, typename T2>
  1183. inline size_t constexpr offsetOf(T1 T2::*Member) {
  1184. constexpr T2 Object{};
  1185. return size_t(&(Object.*Member)) - size_t(&Object);
  1186. }
  1187. static inline uint64_t read(const unsigned char *Buffer, size_t Offset) {
  1188. return *reinterpret_cast<const uint64_t *>(Buffer + Offset);
  1189. }
  1190. uint64_t Header::formatVersion() const {
  1191. using namespace support;
  1192. return endian::byte_swap<uint64_t, little>(Version);
  1193. }
  1194. Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
  1195. using namespace support;
  1196. static_assert(std::is_standard_layout_v<Header>,
  1197. "The header should be standard layout type since we use offset "
  1198. "of fields to read.");
  1199. Header H;
  1200. H.Magic = read(Buffer, offsetOf(&Header::Magic));
  1201. // Check the magic number.
  1202. uint64_t Magic = endian::byte_swap<uint64_t, little>(H.Magic);
  1203. if (Magic != IndexedInstrProf::Magic)
  1204. return make_error<InstrProfError>(instrprof_error::bad_magic);
  1205. // Read the version.
  1206. H.Version = read(Buffer, offsetOf(&Header::Version));
  1207. if (GET_VERSION(H.formatVersion()) >
  1208. IndexedInstrProf::ProfVersion::CurrentVersion)
  1209. return make_error<InstrProfError>(instrprof_error::unsupported_version);
  1210. switch (GET_VERSION(H.formatVersion())) {
  1211. // When a new field is added in the header add a case statement here to
  1212. // populate it.
  1213. static_assert(
  1214. IndexedInstrProf::ProfVersion::CurrentVersion == Version9,
  1215. "Please update the reading code below if a new field has been added, "
  1216. "if not add a case statement to fall through to the latest version.");
  1217. case 9ull:
  1218. H.BinaryIdOffset = read(Buffer, offsetOf(&Header::BinaryIdOffset));
  1219. [[fallthrough]];
  1220. case 8ull:
  1221. H.MemProfOffset = read(Buffer, offsetOf(&Header::MemProfOffset));
  1222. [[fallthrough]];
  1223. default: // Version7 (when the backwards compatible header was introduced).
  1224. H.HashType = read(Buffer, offsetOf(&Header::HashType));
  1225. H.HashOffset = read(Buffer, offsetOf(&Header::HashOffset));
  1226. }
  1227. return H;
  1228. }
  1229. size_t Header::size() const {
  1230. switch (GET_VERSION(formatVersion())) {
  1231. // When a new field is added to the header add a case statement here to
  1232. // compute the size as offset of the new field + size of the new field. This
  1233. // relies on the field being added to the end of the list.
  1234. static_assert(IndexedInstrProf::ProfVersion::CurrentVersion == Version9,
  1235. "Please update the size computation below if a new field has "
  1236. "been added to the header, if not add a case statement to "
  1237. "fall through to the latest version.");
  1238. case 9ull:
  1239. return offsetOf(&Header::BinaryIdOffset) + sizeof(Header::BinaryIdOffset);
  1240. case 8ull:
  1241. return offsetOf(&Header::MemProfOffset) + sizeof(Header::MemProfOffset);
  1242. default: // Version7 (when the backwards compatible header was introduced).
  1243. return offsetOf(&Header::HashOffset) + sizeof(Header::HashOffset);
  1244. }
  1245. }
  1246. } // namespace IndexedInstrProf
  1247. } // end namespace llvm