SampleProfReader.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
  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 implements the class that reads LLVM sample profiles. It
  10. // supports three file formats: text, binary and gcov.
  11. //
  12. // The textual representation is useful for debugging and testing purposes. The
  13. // binary representation is more compact, resulting in smaller file sizes.
  14. //
  15. // The gcov encoding is the one generated by GCC's AutoFDO profile creation
  16. // tool (https://github.com/google/autofdo)
  17. //
  18. // All three encodings can be used interchangeably as an input sample profile.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm/ProfileData/SampleProfReader.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/IR/ProfileSummary.h"
  26. #include "llvm/ProfileData/ProfileCommon.h"
  27. #include "llvm/ProfileData/SampleProf.h"
  28. #include "llvm/Support/Compression.h"
  29. #include "llvm/Support/ErrorOr.h"
  30. #include "llvm/Support/LEB128.h"
  31. #include "llvm/Support/LineIterator.h"
  32. #include "llvm/Support/MD5.h"
  33. #include "llvm/Support/MemoryBuffer.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include <algorithm>
  36. #include <cstddef>
  37. #include <cstdint>
  38. #include <limits>
  39. #include <memory>
  40. #include <system_error>
  41. #include <vector>
  42. using namespace llvm;
  43. using namespace sampleprof;
  44. /// Dump the function profile for \p FName.
  45. ///
  46. /// \param FName Name of the function to print.
  47. /// \param OS Stream to emit the output to.
  48. void SampleProfileReader::dumpFunctionProfile(StringRef FName,
  49. raw_ostream &OS) {
  50. OS << "Function: " << FName << ": " << Profiles[FName];
  51. }
  52. /// Dump all the function profiles found on stream \p OS.
  53. void SampleProfileReader::dump(raw_ostream &OS) {
  54. for (const auto &I : Profiles)
  55. dumpFunctionProfile(I.getKey(), OS);
  56. }
  57. /// Parse \p Input as function head.
  58. ///
  59. /// Parse one line of \p Input, and update function name in \p FName,
  60. /// function's total sample count in \p NumSamples, function's entry
  61. /// count in \p NumHeadSamples.
  62. ///
  63. /// \returns true if parsing is successful.
  64. static bool ParseHead(const StringRef &Input, StringRef &FName,
  65. uint64_t &NumSamples, uint64_t &NumHeadSamples) {
  66. if (Input[0] == ' ')
  67. return false;
  68. size_t n2 = Input.rfind(':');
  69. size_t n1 = Input.rfind(':', n2 - 1);
  70. FName = Input.substr(0, n1);
  71. if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
  72. return false;
  73. if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
  74. return false;
  75. return true;
  76. }
  77. /// Returns true if line offset \p L is legal (only has 16 bits).
  78. static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
  79. /// Parse \p Input as line sample.
  80. ///
  81. /// \param Input input line.
  82. /// \param IsCallsite true if the line represents an inlined callsite.
  83. /// \param Depth the depth of the inline stack.
  84. /// \param NumSamples total samples of the line/inlined callsite.
  85. /// \param LineOffset line offset to the start of the function.
  86. /// \param Discriminator discriminator of the line.
  87. /// \param TargetCountMap map from indirect call target to count.
  88. ///
  89. /// returns true if parsing is successful.
  90. static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth,
  91. uint64_t &NumSamples, uint32_t &LineOffset,
  92. uint32_t &Discriminator, StringRef &CalleeName,
  93. DenseMap<StringRef, uint64_t> &TargetCountMap) {
  94. for (Depth = 0; Input[Depth] == ' '; Depth++)
  95. ;
  96. if (Depth == 0)
  97. return false;
  98. size_t n1 = Input.find(':');
  99. StringRef Loc = Input.substr(Depth, n1 - Depth);
  100. size_t n2 = Loc.find('.');
  101. if (n2 == StringRef::npos) {
  102. if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
  103. return false;
  104. Discriminator = 0;
  105. } else {
  106. if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
  107. return false;
  108. if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
  109. return false;
  110. }
  111. StringRef Rest = Input.substr(n1 + 2);
  112. if (Rest[0] >= '0' && Rest[0] <= '9') {
  113. IsCallsite = false;
  114. size_t n3 = Rest.find(' ');
  115. if (n3 == StringRef::npos) {
  116. if (Rest.getAsInteger(10, NumSamples))
  117. return false;
  118. } else {
  119. if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
  120. return false;
  121. }
  122. // Find call targets and their sample counts.
  123. // Note: In some cases, there are symbols in the profile which are not
  124. // mangled. To accommodate such cases, use colon + integer pairs as the
  125. // anchor points.
  126. // An example:
  127. // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
  128. // ":1000" and ":437" are used as anchor points so the string above will
  129. // be interpreted as
  130. // target: _M_construct<char *>
  131. // count: 1000
  132. // target: string_view<std::allocator<char> >
  133. // count: 437
  134. while (n3 != StringRef::npos) {
  135. n3 += Rest.substr(n3).find_first_not_of(' ');
  136. Rest = Rest.substr(n3);
  137. n3 = Rest.find_first_of(':');
  138. if (n3 == StringRef::npos || n3 == 0)
  139. return false;
  140. StringRef Target;
  141. uint64_t count, n4;
  142. while (true) {
  143. // Get the segment after the current colon.
  144. StringRef AfterColon = Rest.substr(n3 + 1);
  145. // Get the target symbol before the current colon.
  146. Target = Rest.substr(0, n3);
  147. // Check if the word after the current colon is an integer.
  148. n4 = AfterColon.find_first_of(' ');
  149. n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
  150. StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
  151. if (!WordAfterColon.getAsInteger(10, count))
  152. break;
  153. // Try to find the next colon.
  154. uint64_t n5 = AfterColon.find_first_of(':');
  155. if (n5 == StringRef::npos)
  156. return false;
  157. n3 += n5 + 1;
  158. }
  159. // An anchor point is found. Save the {target, count} pair
  160. TargetCountMap[Target] = count;
  161. if (n4 == Rest.size())
  162. break;
  163. // Change n3 to the next blank space after colon + integer pair.
  164. n3 = n4;
  165. }
  166. } else {
  167. IsCallsite = true;
  168. size_t n3 = Rest.find_last_of(':');
  169. CalleeName = Rest.substr(0, n3);
  170. if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
  171. return false;
  172. }
  173. return true;
  174. }
  175. /// Load samples from a text file.
  176. ///
  177. /// See the documentation at the top of the file for an explanation of
  178. /// the expected format.
  179. ///
  180. /// \returns true if the file was loaded successfully, false otherwise.
  181. std::error_code SampleProfileReaderText::readImpl() {
  182. line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
  183. sampleprof_error Result = sampleprof_error::success;
  184. InlineCallStack InlineStack;
  185. for (; !LineIt.is_at_eof(); ++LineIt) {
  186. if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
  187. continue;
  188. // Read the header of each function.
  189. //
  190. // Note that for function identifiers we are actually expecting
  191. // mangled names, but we may not always get them. This happens when
  192. // the compiler decides not to emit the function (e.g., it was inlined
  193. // and removed). In this case, the binary will not have the linkage
  194. // name for the function, so the profiler will emit the function's
  195. // unmangled name, which may contain characters like ':' and '>' in its
  196. // name (member functions, templates, etc).
  197. //
  198. // The only requirement we place on the identifier, then, is that it
  199. // should not begin with a number.
  200. if ((*LineIt)[0] != ' ') {
  201. uint64_t NumSamples, NumHeadSamples;
  202. StringRef FName;
  203. if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
  204. reportError(LineIt.line_number(),
  205. "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
  206. return sampleprof_error::malformed;
  207. }
  208. Profiles[FName] = FunctionSamples();
  209. FunctionSamples &FProfile = Profiles[FName];
  210. FProfile.setName(FName);
  211. MergeResult(Result, FProfile.addTotalSamples(NumSamples));
  212. MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
  213. InlineStack.clear();
  214. InlineStack.push_back(&FProfile);
  215. } else {
  216. uint64_t NumSamples;
  217. StringRef FName;
  218. DenseMap<StringRef, uint64_t> TargetCountMap;
  219. bool IsCallsite;
  220. uint32_t Depth, LineOffset, Discriminator;
  221. if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
  222. Discriminator, FName, TargetCountMap)) {
  223. reportError(LineIt.line_number(),
  224. "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
  225. *LineIt);
  226. return sampleprof_error::malformed;
  227. }
  228. if (IsCallsite) {
  229. while (InlineStack.size() > Depth) {
  230. InlineStack.pop_back();
  231. }
  232. FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
  233. LineLocation(LineOffset, Discriminator))[FName];
  234. FSamples.setName(FName);
  235. MergeResult(Result, FSamples.addTotalSamples(NumSamples));
  236. InlineStack.push_back(&FSamples);
  237. } else {
  238. while (InlineStack.size() > Depth) {
  239. InlineStack.pop_back();
  240. }
  241. FunctionSamples &FProfile = *InlineStack.back();
  242. for (const auto &name_count : TargetCountMap) {
  243. MergeResult(Result, FProfile.addCalledTargetSamples(
  244. LineOffset, Discriminator, name_count.first,
  245. name_count.second));
  246. }
  247. MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
  248. NumSamples));
  249. }
  250. }
  251. }
  252. if (Result == sampleprof_error::success)
  253. computeSummary();
  254. return Result;
  255. }
  256. bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
  257. bool result = false;
  258. // Check that the first non-comment line is a valid function header.
  259. line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
  260. if (!LineIt.is_at_eof()) {
  261. if ((*LineIt)[0] != ' ') {
  262. uint64_t NumSamples, NumHeadSamples;
  263. StringRef FName;
  264. result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
  265. }
  266. }
  267. return result;
  268. }
  269. template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
  270. unsigned NumBytesRead = 0;
  271. std::error_code EC;
  272. uint64_t Val = decodeULEB128(Data, &NumBytesRead);
  273. if (Val > std::numeric_limits<T>::max())
  274. EC = sampleprof_error::malformed;
  275. else if (Data + NumBytesRead > End)
  276. EC = sampleprof_error::truncated;
  277. else
  278. EC = sampleprof_error::success;
  279. if (EC) {
  280. reportError(0, EC.message());
  281. return EC;
  282. }
  283. Data += NumBytesRead;
  284. return static_cast<T>(Val);
  285. }
  286. ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
  287. std::error_code EC;
  288. StringRef Str(reinterpret_cast<const char *>(Data));
  289. if (Data + Str.size() + 1 > End) {
  290. EC = sampleprof_error::truncated;
  291. reportError(0, EC.message());
  292. return EC;
  293. }
  294. Data += Str.size() + 1;
  295. return Str;
  296. }
  297. template <typename T>
  298. ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
  299. std::error_code EC;
  300. if (Data + sizeof(T) > End) {
  301. EC = sampleprof_error::truncated;
  302. reportError(0, EC.message());
  303. return EC;
  304. }
  305. using namespace support;
  306. T Val = endian::readNext<T, little, unaligned>(Data);
  307. return Val;
  308. }
  309. template <typename T>
  310. inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
  311. std::error_code EC;
  312. auto Idx = readNumber<uint32_t>();
  313. if (std::error_code EC = Idx.getError())
  314. return EC;
  315. if (*Idx >= Table.size())
  316. return sampleprof_error::truncated_name_table;
  317. return *Idx;
  318. }
  319. ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
  320. auto Idx = readStringIndex(NameTable);
  321. if (std::error_code EC = Idx.getError())
  322. return EC;
  323. return NameTable[*Idx];
  324. }
  325. ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
  326. auto Idx = readStringIndex(NameTable);
  327. if (std::error_code EC = Idx.getError())
  328. return EC;
  329. return StringRef(NameTable[*Idx]);
  330. }
  331. std::error_code
  332. SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
  333. auto NumSamples = readNumber<uint64_t>();
  334. if (std::error_code EC = NumSamples.getError())
  335. return EC;
  336. FProfile.addTotalSamples(*NumSamples);
  337. // Read the samples in the body.
  338. auto NumRecords = readNumber<uint32_t>();
  339. if (std::error_code EC = NumRecords.getError())
  340. return EC;
  341. for (uint32_t I = 0; I < *NumRecords; ++I) {
  342. auto LineOffset = readNumber<uint64_t>();
  343. if (std::error_code EC = LineOffset.getError())
  344. return EC;
  345. if (!isOffsetLegal(*LineOffset)) {
  346. return std::error_code();
  347. }
  348. auto Discriminator = readNumber<uint64_t>();
  349. if (std::error_code EC = Discriminator.getError())
  350. return EC;
  351. auto NumSamples = readNumber<uint64_t>();
  352. if (std::error_code EC = NumSamples.getError())
  353. return EC;
  354. auto NumCalls = readNumber<uint32_t>();
  355. if (std::error_code EC = NumCalls.getError())
  356. return EC;
  357. for (uint32_t J = 0; J < *NumCalls; ++J) {
  358. auto CalledFunction(readStringFromTable());
  359. if (std::error_code EC = CalledFunction.getError())
  360. return EC;
  361. auto CalledFunctionSamples = readNumber<uint64_t>();
  362. if (std::error_code EC = CalledFunctionSamples.getError())
  363. return EC;
  364. FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
  365. *CalledFunction, *CalledFunctionSamples);
  366. }
  367. FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
  368. }
  369. // Read all the samples for inlined function calls.
  370. auto NumCallsites = readNumber<uint32_t>();
  371. if (std::error_code EC = NumCallsites.getError())
  372. return EC;
  373. for (uint32_t J = 0; J < *NumCallsites; ++J) {
  374. auto LineOffset = readNumber<uint64_t>();
  375. if (std::error_code EC = LineOffset.getError())
  376. return EC;
  377. auto Discriminator = readNumber<uint64_t>();
  378. if (std::error_code EC = Discriminator.getError())
  379. return EC;
  380. auto FName(readStringFromTable());
  381. if (std::error_code EC = FName.getError())
  382. return EC;
  383. FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
  384. LineLocation(*LineOffset, *Discriminator))[*FName];
  385. CalleeProfile.setName(*FName);
  386. if (std::error_code EC = readProfile(CalleeProfile))
  387. return EC;
  388. }
  389. return sampleprof_error::success;
  390. }
  391. std::error_code
  392. SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
  393. Data = Start;
  394. auto NumHeadSamples = readNumber<uint64_t>();
  395. if (std::error_code EC = NumHeadSamples.getError())
  396. return EC;
  397. auto FName(readStringFromTable());
  398. if (std::error_code EC = FName.getError())
  399. return EC;
  400. Profiles[*FName] = FunctionSamples();
  401. FunctionSamples &FProfile = Profiles[*FName];
  402. FProfile.setName(*FName);
  403. FProfile.addHeadSamples(*NumHeadSamples);
  404. if (std::error_code EC = readProfile(FProfile))
  405. return EC;
  406. return sampleprof_error::success;
  407. }
  408. std::error_code SampleProfileReaderBinary::readImpl() {
  409. while (!at_eof()) {
  410. if (std::error_code EC = readFuncProfile(Data))
  411. return EC;
  412. }
  413. return sampleprof_error::success;
  414. }
  415. std::error_code
  416. SampleProfileReaderExtBinary::readOneSection(const uint8_t *Start,
  417. uint64_t Size, SecType Type) {
  418. Data = Start;
  419. End = Start + Size;
  420. switch (Type) {
  421. case SecProfSummary:
  422. if (std::error_code EC = readSummary())
  423. return EC;
  424. break;
  425. case SecNameTable:
  426. if (std::error_code EC = readNameTable())
  427. return EC;
  428. break;
  429. case SecLBRProfile:
  430. if (std::error_code EC = readFuncProfiles())
  431. return EC;
  432. break;
  433. case SecProfileSymbolList:
  434. if (std::error_code EC = readProfileSymbolList())
  435. return EC;
  436. break;
  437. case SecFuncOffsetTable:
  438. if (std::error_code EC = readFuncOffsetTable())
  439. return EC;
  440. break;
  441. default:
  442. break;
  443. }
  444. return sampleprof_error::success;
  445. }
  446. void SampleProfileReaderExtBinary::collectFuncsFrom(const Module &M) {
  447. UseAllFuncs = false;
  448. FuncsToUse.clear();
  449. for (auto &F : M)
  450. FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
  451. }
  452. std::error_code SampleProfileReaderExtBinary::readFuncOffsetTable() {
  453. auto Size = readNumber<uint64_t>();
  454. if (std::error_code EC = Size.getError())
  455. return EC;
  456. FuncOffsetTable.reserve(*Size);
  457. for (uint32_t I = 0; I < *Size; ++I) {
  458. auto FName(readStringFromTable());
  459. if (std::error_code EC = FName.getError())
  460. return EC;
  461. auto Offset = readNumber<uint64_t>();
  462. if (std::error_code EC = Offset.getError())
  463. return EC;
  464. FuncOffsetTable[*FName] = *Offset;
  465. }
  466. return sampleprof_error::success;
  467. }
  468. std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
  469. const uint8_t *Start = Data;
  470. if (UseAllFuncs) {
  471. while (Data < End) {
  472. if (std::error_code EC = readFuncProfile(Data))
  473. return EC;
  474. }
  475. assert(Data == End && "More data is read than expected");
  476. return sampleprof_error::success;
  477. }
  478. if (Remapper) {
  479. for (auto Name : FuncsToUse) {
  480. Remapper->insert(Name);
  481. }
  482. }
  483. for (auto NameOffset : FuncOffsetTable) {
  484. auto FuncName = NameOffset.first;
  485. if (!FuncsToUse.count(FuncName) &&
  486. (!Remapper || !Remapper->exist(FuncName)))
  487. continue;
  488. const uint8_t *FuncProfileAddr = Start + NameOffset.second;
  489. assert(FuncProfileAddr < End && "out of LBRProfile section");
  490. if (std::error_code EC = readFuncProfile(FuncProfileAddr))
  491. return EC;
  492. }
  493. Data = End;
  494. return sampleprof_error::success;
  495. }
  496. std::error_code SampleProfileReaderExtBinary::readProfileSymbolList() {
  497. if (!ProfSymList)
  498. ProfSymList = std::make_unique<ProfileSymbolList>();
  499. if (std::error_code EC = ProfSymList->read(Data, End - Data))
  500. return EC;
  501. Data = End;
  502. return sampleprof_error::success;
  503. }
  504. std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
  505. const uint8_t *SecStart, const uint64_t SecSize,
  506. const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
  507. Data = SecStart;
  508. End = SecStart + SecSize;
  509. auto DecompressSize = readNumber<uint64_t>();
  510. if (std::error_code EC = DecompressSize.getError())
  511. return EC;
  512. DecompressBufSize = *DecompressSize;
  513. auto CompressSize = readNumber<uint64_t>();
  514. if (std::error_code EC = CompressSize.getError())
  515. return EC;
  516. if (!llvm::zlib::isAvailable())
  517. return sampleprof_error::zlib_unavailable;
  518. StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
  519. *CompressSize);
  520. char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
  521. size_t UCSize = DecompressBufSize;
  522. llvm::Error E =
  523. zlib::uncompress(CompressedStrings, Buffer, UCSize);
  524. if (E)
  525. return sampleprof_error::uncompress_failed;
  526. DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
  527. return sampleprof_error::success;
  528. }
  529. std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
  530. const uint8_t *BufStart =
  531. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  532. for (auto &Entry : SecHdrTable) {
  533. // Skip empty section.
  534. if (!Entry.Size)
  535. continue;
  536. const uint8_t *SecStart = BufStart + Entry.Offset;
  537. uint64_t SecSize = Entry.Size;
  538. // If the section is compressed, decompress it into a buffer
  539. // DecompressBuf before reading the actual data. The pointee of
  540. // 'Data' will be changed to buffer hold by DecompressBuf
  541. // temporarily when reading the actual data.
  542. bool isCompressed = hasSecFlag(Entry, SecFlagCompress);
  543. if (isCompressed) {
  544. const uint8_t *DecompressBuf;
  545. uint64_t DecompressBufSize;
  546. if (std::error_code EC = decompressSection(
  547. SecStart, SecSize, DecompressBuf, DecompressBufSize))
  548. return EC;
  549. SecStart = DecompressBuf;
  550. SecSize = DecompressBufSize;
  551. }
  552. if (std::error_code EC = readOneSection(SecStart, SecSize, Entry.Type))
  553. return EC;
  554. if (Data != SecStart + SecSize)
  555. return sampleprof_error::malformed;
  556. // Change the pointee of 'Data' from DecompressBuf to original Buffer.
  557. if (isCompressed) {
  558. Data = BufStart + Entry.Offset;
  559. End = BufStart + Buffer->getBufferSize();
  560. }
  561. }
  562. return sampleprof_error::success;
  563. }
  564. std::error_code SampleProfileReaderCompactBinary::readImpl() {
  565. std::vector<uint64_t> OffsetsToUse;
  566. if (UseAllFuncs) {
  567. for (auto FuncEntry : FuncOffsetTable) {
  568. OffsetsToUse.push_back(FuncEntry.second);
  569. }
  570. }
  571. else {
  572. for (auto Name : FuncsToUse) {
  573. auto GUID = std::to_string(MD5Hash(Name));
  574. auto iter = FuncOffsetTable.find(StringRef(GUID));
  575. if (iter == FuncOffsetTable.end())
  576. continue;
  577. OffsetsToUse.push_back(iter->second);
  578. }
  579. }
  580. for (auto Offset : OffsetsToUse) {
  581. const uint8_t *SavedData = Data;
  582. if (std::error_code EC = readFuncProfile(
  583. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
  584. Offset))
  585. return EC;
  586. Data = SavedData;
  587. }
  588. return sampleprof_error::success;
  589. }
  590. std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
  591. if (Magic == SPMagic())
  592. return sampleprof_error::success;
  593. return sampleprof_error::bad_magic;
  594. }
  595. std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
  596. if (Magic == SPMagic(SPF_Ext_Binary))
  597. return sampleprof_error::success;
  598. return sampleprof_error::bad_magic;
  599. }
  600. std::error_code
  601. SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
  602. if (Magic == SPMagic(SPF_Compact_Binary))
  603. return sampleprof_error::success;
  604. return sampleprof_error::bad_magic;
  605. }
  606. std::error_code SampleProfileReaderBinary::readNameTable() {
  607. auto Size = readNumber<uint32_t>();
  608. if (std::error_code EC = Size.getError())
  609. return EC;
  610. NameTable.reserve(*Size);
  611. for (uint32_t I = 0; I < *Size; ++I) {
  612. auto Name(readString());
  613. if (std::error_code EC = Name.getError())
  614. return EC;
  615. NameTable.push_back(*Name);
  616. }
  617. return sampleprof_error::success;
  618. }
  619. std::error_code SampleProfileReaderCompactBinary::readNameTable() {
  620. auto Size = readNumber<uint64_t>();
  621. if (std::error_code EC = Size.getError())
  622. return EC;
  623. NameTable.reserve(*Size);
  624. for (uint32_t I = 0; I < *Size; ++I) {
  625. auto FID = readNumber<uint64_t>();
  626. if (std::error_code EC = FID.getError())
  627. return EC;
  628. NameTable.push_back(std::to_string(*FID));
  629. }
  630. return sampleprof_error::success;
  631. }
  632. std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTableEntry() {
  633. SecHdrTableEntry Entry;
  634. auto Type = readUnencodedNumber<uint64_t>();
  635. if (std::error_code EC = Type.getError())
  636. return EC;
  637. Entry.Type = static_cast<SecType>(*Type);
  638. auto Flags = readUnencodedNumber<uint64_t>();
  639. if (std::error_code EC = Flags.getError())
  640. return EC;
  641. Entry.Flags = *Flags;
  642. auto Offset = readUnencodedNumber<uint64_t>();
  643. if (std::error_code EC = Offset.getError())
  644. return EC;
  645. Entry.Offset = *Offset;
  646. auto Size = readUnencodedNumber<uint64_t>();
  647. if (std::error_code EC = Size.getError())
  648. return EC;
  649. Entry.Size = *Size;
  650. SecHdrTable.push_back(std::move(Entry));
  651. return sampleprof_error::success;
  652. }
  653. std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
  654. auto EntryNum = readUnencodedNumber<uint64_t>();
  655. if (std::error_code EC = EntryNum.getError())
  656. return EC;
  657. for (uint32_t i = 0; i < (*EntryNum); i++)
  658. if (std::error_code EC = readSecHdrTableEntry())
  659. return EC;
  660. return sampleprof_error::success;
  661. }
  662. std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
  663. const uint8_t *BufStart =
  664. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  665. Data = BufStart;
  666. End = BufStart + Buffer->getBufferSize();
  667. if (std::error_code EC = readMagicIdent())
  668. return EC;
  669. if (std::error_code EC = readSecHdrTable())
  670. return EC;
  671. return sampleprof_error::success;
  672. }
  673. uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
  674. for (auto &Entry : SecHdrTable) {
  675. if (Entry.Type == Type)
  676. return Entry.Size;
  677. }
  678. return 0;
  679. }
  680. uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
  681. // Sections in SecHdrTable is not necessarily in the same order as
  682. // sections in the profile because section like FuncOffsetTable needs
  683. // to be written after section LBRProfile but needs to be read before
  684. // section LBRProfile, so we cannot simply use the last entry in
  685. // SecHdrTable to calculate the file size.
  686. uint64_t FileSize = 0;
  687. for (auto &Entry : SecHdrTable) {
  688. FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
  689. }
  690. return FileSize;
  691. }
  692. bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
  693. uint64_t TotalSecsSize = 0;
  694. for (auto &Entry : SecHdrTable) {
  695. OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
  696. << ", Size: " << Entry.Size << "\n";
  697. TotalSecsSize += getSectionSize(Entry.Type);
  698. }
  699. uint64_t HeaderSize = SecHdrTable.front().Offset;
  700. assert(HeaderSize + TotalSecsSize == getFileSize() &&
  701. "Size of 'header + sections' doesn't match the total size of profile");
  702. OS << "Header Size: " << HeaderSize << "\n";
  703. OS << "Total Sections Size: " << TotalSecsSize << "\n";
  704. OS << "File Size: " << getFileSize() << "\n";
  705. return true;
  706. }
  707. std::error_code SampleProfileReaderBinary::readMagicIdent() {
  708. // Read and check the magic identifier.
  709. auto Magic = readNumber<uint64_t>();
  710. if (std::error_code EC = Magic.getError())
  711. return EC;
  712. else if (std::error_code EC = verifySPMagic(*Magic))
  713. return EC;
  714. // Read the version number.
  715. auto Version = readNumber<uint64_t>();
  716. if (std::error_code EC = Version.getError())
  717. return EC;
  718. else if (*Version != SPVersion())
  719. return sampleprof_error::unsupported_version;
  720. return sampleprof_error::success;
  721. }
  722. std::error_code SampleProfileReaderBinary::readHeader() {
  723. Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  724. End = Data + Buffer->getBufferSize();
  725. if (std::error_code EC = readMagicIdent())
  726. return EC;
  727. if (std::error_code EC = readSummary())
  728. return EC;
  729. if (std::error_code EC = readNameTable())
  730. return EC;
  731. return sampleprof_error::success;
  732. }
  733. std::error_code SampleProfileReaderCompactBinary::readHeader() {
  734. SampleProfileReaderBinary::readHeader();
  735. if (std::error_code EC = readFuncOffsetTable())
  736. return EC;
  737. return sampleprof_error::success;
  738. }
  739. std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
  740. auto TableOffset = readUnencodedNumber<uint64_t>();
  741. if (std::error_code EC = TableOffset.getError())
  742. return EC;
  743. const uint8_t *SavedData = Data;
  744. const uint8_t *TableStart =
  745. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
  746. *TableOffset;
  747. Data = TableStart;
  748. auto Size = readNumber<uint64_t>();
  749. if (std::error_code EC = Size.getError())
  750. return EC;
  751. FuncOffsetTable.reserve(*Size);
  752. for (uint32_t I = 0; I < *Size; ++I) {
  753. auto FName(readStringFromTable());
  754. if (std::error_code EC = FName.getError())
  755. return EC;
  756. auto Offset = readNumber<uint64_t>();
  757. if (std::error_code EC = Offset.getError())
  758. return EC;
  759. FuncOffsetTable[*FName] = *Offset;
  760. }
  761. End = TableStart;
  762. Data = SavedData;
  763. return sampleprof_error::success;
  764. }
  765. void SampleProfileReaderCompactBinary::collectFuncsFrom(const Module &M) {
  766. UseAllFuncs = false;
  767. FuncsToUse.clear();
  768. for (auto &F : M)
  769. FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
  770. }
  771. std::error_code SampleProfileReaderBinary::readSummaryEntry(
  772. std::vector<ProfileSummaryEntry> &Entries) {
  773. auto Cutoff = readNumber<uint64_t>();
  774. if (std::error_code EC = Cutoff.getError())
  775. return EC;
  776. auto MinBlockCount = readNumber<uint64_t>();
  777. if (std::error_code EC = MinBlockCount.getError())
  778. return EC;
  779. auto NumBlocks = readNumber<uint64_t>();
  780. if (std::error_code EC = NumBlocks.getError())
  781. return EC;
  782. Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
  783. return sampleprof_error::success;
  784. }
  785. std::error_code SampleProfileReaderBinary::readSummary() {
  786. auto TotalCount = readNumber<uint64_t>();
  787. if (std::error_code EC = TotalCount.getError())
  788. return EC;
  789. auto MaxBlockCount = readNumber<uint64_t>();
  790. if (std::error_code EC = MaxBlockCount.getError())
  791. return EC;
  792. auto MaxFunctionCount = readNumber<uint64_t>();
  793. if (std::error_code EC = MaxFunctionCount.getError())
  794. return EC;
  795. auto NumBlocks = readNumber<uint64_t>();
  796. if (std::error_code EC = NumBlocks.getError())
  797. return EC;
  798. auto NumFunctions = readNumber<uint64_t>();
  799. if (std::error_code EC = NumFunctions.getError())
  800. return EC;
  801. auto NumSummaryEntries = readNumber<uint64_t>();
  802. if (std::error_code EC = NumSummaryEntries.getError())
  803. return EC;
  804. std::vector<ProfileSummaryEntry> Entries;
  805. for (unsigned i = 0; i < *NumSummaryEntries; i++) {
  806. std::error_code EC = readSummaryEntry(Entries);
  807. if (EC != sampleprof_error::success)
  808. return EC;
  809. }
  810. Summary = std::make_unique<ProfileSummary>(
  811. ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
  812. *MaxFunctionCount, *NumBlocks, *NumFunctions);
  813. return sampleprof_error::success;
  814. }
  815. bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
  816. const uint8_t *Data =
  817. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  818. uint64_t Magic = decodeULEB128(Data);
  819. return Magic == SPMagic();
  820. }
  821. bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
  822. const uint8_t *Data =
  823. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  824. uint64_t Magic = decodeULEB128(Data);
  825. return Magic == SPMagic(SPF_Ext_Binary);
  826. }
  827. bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
  828. const uint8_t *Data =
  829. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  830. uint64_t Magic = decodeULEB128(Data);
  831. return Magic == SPMagic(SPF_Compact_Binary);
  832. }
  833. std::error_code SampleProfileReaderGCC::skipNextWord() {
  834. uint32_t dummy;
  835. if (!GcovBuffer.readInt(dummy))
  836. return sampleprof_error::truncated;
  837. return sampleprof_error::success;
  838. }
  839. template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
  840. if (sizeof(T) <= sizeof(uint32_t)) {
  841. uint32_t Val;
  842. if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
  843. return static_cast<T>(Val);
  844. } else if (sizeof(T) <= sizeof(uint64_t)) {
  845. uint64_t Val;
  846. if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
  847. return static_cast<T>(Val);
  848. }
  849. std::error_code EC = sampleprof_error::malformed;
  850. reportError(0, EC.message());
  851. return EC;
  852. }
  853. ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
  854. StringRef Str;
  855. if (!GcovBuffer.readString(Str))
  856. return sampleprof_error::truncated;
  857. return Str;
  858. }
  859. std::error_code SampleProfileReaderGCC::readHeader() {
  860. // Read the magic identifier.
  861. if (!GcovBuffer.readGCDAFormat())
  862. return sampleprof_error::unrecognized_format;
  863. // Read the version number. Note - the GCC reader does not validate this
  864. // version, but the profile creator generates v704.
  865. GCOV::GCOVVersion version;
  866. if (!GcovBuffer.readGCOVVersion(version))
  867. return sampleprof_error::unrecognized_format;
  868. if (version != GCOV::V704)
  869. return sampleprof_error::unsupported_version;
  870. // Skip the empty integer.
  871. if (std::error_code EC = skipNextWord())
  872. return EC;
  873. return sampleprof_error::success;
  874. }
  875. std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
  876. uint32_t Tag;
  877. if (!GcovBuffer.readInt(Tag))
  878. return sampleprof_error::truncated;
  879. if (Tag != Expected)
  880. return sampleprof_error::malformed;
  881. if (std::error_code EC = skipNextWord())
  882. return EC;
  883. return sampleprof_error::success;
  884. }
  885. std::error_code SampleProfileReaderGCC::readNameTable() {
  886. if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
  887. return EC;
  888. uint32_t Size;
  889. if (!GcovBuffer.readInt(Size))
  890. return sampleprof_error::truncated;
  891. for (uint32_t I = 0; I < Size; ++I) {
  892. StringRef Str;
  893. if (!GcovBuffer.readString(Str))
  894. return sampleprof_error::truncated;
  895. Names.push_back(Str);
  896. }
  897. return sampleprof_error::success;
  898. }
  899. std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
  900. if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
  901. return EC;
  902. uint32_t NumFunctions;
  903. if (!GcovBuffer.readInt(NumFunctions))
  904. return sampleprof_error::truncated;
  905. InlineCallStack Stack;
  906. for (uint32_t I = 0; I < NumFunctions; ++I)
  907. if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
  908. return EC;
  909. computeSummary();
  910. return sampleprof_error::success;
  911. }
  912. std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
  913. const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
  914. uint64_t HeadCount = 0;
  915. if (InlineStack.size() == 0)
  916. if (!GcovBuffer.readInt64(HeadCount))
  917. return sampleprof_error::truncated;
  918. uint32_t NameIdx;
  919. if (!GcovBuffer.readInt(NameIdx))
  920. return sampleprof_error::truncated;
  921. StringRef Name(Names[NameIdx]);
  922. uint32_t NumPosCounts;
  923. if (!GcovBuffer.readInt(NumPosCounts))
  924. return sampleprof_error::truncated;
  925. uint32_t NumCallsites;
  926. if (!GcovBuffer.readInt(NumCallsites))
  927. return sampleprof_error::truncated;
  928. FunctionSamples *FProfile = nullptr;
  929. if (InlineStack.size() == 0) {
  930. // If this is a top function that we have already processed, do not
  931. // update its profile again. This happens in the presence of
  932. // function aliases. Since these aliases share the same function
  933. // body, there will be identical replicated profiles for the
  934. // original function. In this case, we simply not bother updating
  935. // the profile of the original function.
  936. FProfile = &Profiles[Name];
  937. FProfile->addHeadSamples(HeadCount);
  938. if (FProfile->getTotalSamples() > 0)
  939. Update = false;
  940. } else {
  941. // Otherwise, we are reading an inlined instance. The top of the
  942. // inline stack contains the profile of the caller. Insert this
  943. // callee in the caller's CallsiteMap.
  944. FunctionSamples *CallerProfile = InlineStack.front();
  945. uint32_t LineOffset = Offset >> 16;
  946. uint32_t Discriminator = Offset & 0xffff;
  947. FProfile = &CallerProfile->functionSamplesAt(
  948. LineLocation(LineOffset, Discriminator))[Name];
  949. }
  950. FProfile->setName(Name);
  951. for (uint32_t I = 0; I < NumPosCounts; ++I) {
  952. uint32_t Offset;
  953. if (!GcovBuffer.readInt(Offset))
  954. return sampleprof_error::truncated;
  955. uint32_t NumTargets;
  956. if (!GcovBuffer.readInt(NumTargets))
  957. return sampleprof_error::truncated;
  958. uint64_t Count;
  959. if (!GcovBuffer.readInt64(Count))
  960. return sampleprof_error::truncated;
  961. // The line location is encoded in the offset as:
  962. // high 16 bits: line offset to the start of the function.
  963. // low 16 bits: discriminator.
  964. uint32_t LineOffset = Offset >> 16;
  965. uint32_t Discriminator = Offset & 0xffff;
  966. InlineCallStack NewStack;
  967. NewStack.push_back(FProfile);
  968. NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
  969. if (Update) {
  970. // Walk up the inline stack, adding the samples on this line to
  971. // the total sample count of the callers in the chain.
  972. for (auto CallerProfile : NewStack)
  973. CallerProfile->addTotalSamples(Count);
  974. // Update the body samples for the current profile.
  975. FProfile->addBodySamples(LineOffset, Discriminator, Count);
  976. }
  977. // Process the list of functions called at an indirect call site.
  978. // These are all the targets that a function pointer (or virtual
  979. // function) resolved at runtime.
  980. for (uint32_t J = 0; J < NumTargets; J++) {
  981. uint32_t HistVal;
  982. if (!GcovBuffer.readInt(HistVal))
  983. return sampleprof_error::truncated;
  984. if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
  985. return sampleprof_error::malformed;
  986. uint64_t TargetIdx;
  987. if (!GcovBuffer.readInt64(TargetIdx))
  988. return sampleprof_error::truncated;
  989. StringRef TargetName(Names[TargetIdx]);
  990. uint64_t TargetCount;
  991. if (!GcovBuffer.readInt64(TargetCount))
  992. return sampleprof_error::truncated;
  993. if (Update)
  994. FProfile->addCalledTargetSamples(LineOffset, Discriminator,
  995. TargetName, TargetCount);
  996. }
  997. }
  998. // Process all the inlined callers into the current function. These
  999. // are all the callsites that were inlined into this function.
  1000. for (uint32_t I = 0; I < NumCallsites; I++) {
  1001. // The offset is encoded as:
  1002. // high 16 bits: line offset to the start of the function.
  1003. // low 16 bits: discriminator.
  1004. uint32_t Offset;
  1005. if (!GcovBuffer.readInt(Offset))
  1006. return sampleprof_error::truncated;
  1007. InlineCallStack NewStack;
  1008. NewStack.push_back(FProfile);
  1009. NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
  1010. if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
  1011. return EC;
  1012. }
  1013. return sampleprof_error::success;
  1014. }
  1015. /// Read a GCC AutoFDO profile.
  1016. ///
  1017. /// This format is generated by the Linux Perf conversion tool at
  1018. /// https://github.com/google/autofdo.
  1019. std::error_code SampleProfileReaderGCC::readImpl() {
  1020. // Read the string table.
  1021. if (std::error_code EC = readNameTable())
  1022. return EC;
  1023. // Read the source profile.
  1024. if (std::error_code EC = readFunctionProfiles())
  1025. return EC;
  1026. return sampleprof_error::success;
  1027. }
  1028. bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
  1029. StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
  1030. return Magic == "adcg*704";
  1031. }
  1032. void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
  1033. // If the reader is in compact format, we can't remap it because
  1034. // we don't know what the original function names were.
  1035. if (Reader.getFormat() == SPF_Compact_Binary) {
  1036. Ctx.diagnose(DiagnosticInfoSampleProfile(
  1037. Reader.getBuffer()->getBufferIdentifier(),
  1038. "Profile data remapping cannot be applied to profile data "
  1039. "in compact format (original mangled names are not available).",
  1040. DS_Warning));
  1041. return;
  1042. }
  1043. assert(Remappings && "should be initialized while creating remapper");
  1044. for (auto &Sample : Reader.getProfiles())
  1045. if (auto Key = Remappings->insert(Sample.first()))
  1046. SampleMap.insert({Key, &Sample.second});
  1047. RemappingApplied = true;
  1048. }
  1049. FunctionSamples *
  1050. SampleProfileReaderItaniumRemapper::getSamplesFor(StringRef Fname) {
  1051. if (auto Key = Remappings->lookup(Fname))
  1052. return SampleMap.lookup(Key);
  1053. return nullptr;
  1054. }
  1055. /// Prepare a memory buffer for the contents of \p Filename.
  1056. ///
  1057. /// \returns an error code indicating the status of the buffer.
  1058. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  1059. setupMemoryBuffer(const Twine &Filename) {
  1060. auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
  1061. if (std::error_code EC = BufferOrErr.getError())
  1062. return EC;
  1063. auto Buffer = std::move(BufferOrErr.get());
  1064. // Sanity check the file.
  1065. if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
  1066. return sampleprof_error::too_large;
  1067. return std::move(Buffer);
  1068. }
  1069. /// Create a sample profile reader based on the format of the input file.
  1070. ///
  1071. /// \param Filename The file to open.
  1072. ///
  1073. /// \param C The LLVM context to use to emit diagnostics.
  1074. ///
  1075. /// \param RemapFilename The file used for profile remapping.
  1076. ///
  1077. /// \returns an error code indicating the status of the created reader.
  1078. ErrorOr<std::unique_ptr<SampleProfileReader>>
  1079. SampleProfileReader::create(const std::string Filename, LLVMContext &C,
  1080. const std::string RemapFilename) {
  1081. auto BufferOrError = setupMemoryBuffer(Filename);
  1082. if (std::error_code EC = BufferOrError.getError())
  1083. return EC;
  1084. return create(BufferOrError.get(), C, RemapFilename);
  1085. }
  1086. /// Create a sample profile remapper from the given input, to remap the
  1087. /// function names in the given profile data.
  1088. ///
  1089. /// \param Filename The file to open.
  1090. ///
  1091. /// \param Reader The profile reader the remapper is going to be applied to.
  1092. ///
  1093. /// \param C The LLVM context to use to emit diagnostics.
  1094. ///
  1095. /// \returns an error code indicating the status of the created reader.
  1096. ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
  1097. SampleProfileReaderItaniumRemapper::create(const std::string Filename,
  1098. SampleProfileReader &Reader,
  1099. LLVMContext &C) {
  1100. auto BufferOrError = setupMemoryBuffer(Filename);
  1101. if (std::error_code EC = BufferOrError.getError())
  1102. return EC;
  1103. return create(BufferOrError.get(), Reader, C);
  1104. }
  1105. /// Create a sample profile remapper from the given input, to remap the
  1106. /// function names in the given profile data.
  1107. ///
  1108. /// \param B The memory buffer to create the reader from (assumes ownership).
  1109. ///
  1110. /// \param C The LLVM context to use to emit diagnostics.
  1111. ///
  1112. /// \param Reader The profile reader the remapper is going to be applied to.
  1113. ///
  1114. /// \returns an error code indicating the status of the created reader.
  1115. ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
  1116. SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
  1117. SampleProfileReader &Reader,
  1118. LLVMContext &C) {
  1119. auto Remappings = std::make_unique<SymbolRemappingReader>();
  1120. if (Error E = Remappings->read(*B.get())) {
  1121. handleAllErrors(
  1122. std::move(E), [&](const SymbolRemappingParseError &ParseError) {
  1123. C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
  1124. ParseError.getLineNum(),
  1125. ParseError.getMessage()));
  1126. });
  1127. return sampleprof_error::malformed;
  1128. }
  1129. return std::make_unique<SampleProfileReaderItaniumRemapper>(
  1130. std::move(B), std::move(Remappings), Reader);
  1131. }
  1132. /// Create a sample profile reader based on the format of the input data.
  1133. ///
  1134. /// \param B The memory buffer to create the reader from (assumes ownership).
  1135. ///
  1136. /// \param C The LLVM context to use to emit diagnostics.
  1137. ///
  1138. /// \param RemapFilename The file used for profile remapping.
  1139. ///
  1140. /// \returns an error code indicating the status of the created reader.
  1141. ErrorOr<std::unique_ptr<SampleProfileReader>>
  1142. SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
  1143. const std::string RemapFilename) {
  1144. std::unique_ptr<SampleProfileReader> Reader;
  1145. if (SampleProfileReaderRawBinary::hasFormat(*B))
  1146. Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
  1147. else if (SampleProfileReaderExtBinary::hasFormat(*B))
  1148. Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
  1149. else if (SampleProfileReaderCompactBinary::hasFormat(*B))
  1150. Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
  1151. else if (SampleProfileReaderGCC::hasFormat(*B))
  1152. Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
  1153. else if (SampleProfileReaderText::hasFormat(*B))
  1154. Reader.reset(new SampleProfileReaderText(std::move(B), C));
  1155. else
  1156. return sampleprof_error::unrecognized_format;
  1157. if (!RemapFilename.empty()) {
  1158. auto ReaderOrErr =
  1159. SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
  1160. if (std::error_code EC = ReaderOrErr.getError()) {
  1161. std::string Msg = "Could not create remapper: " + EC.message();
  1162. C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
  1163. return EC;
  1164. }
  1165. Reader->Remapper = std::move(ReaderOrErr.get());
  1166. }
  1167. FunctionSamples::Format = Reader->getFormat();
  1168. if (std::error_code EC = Reader->readHeader()) {
  1169. return EC;
  1170. }
  1171. return std::move(Reader);
  1172. }
  1173. // For text and GCC file formats, we compute the summary after reading the
  1174. // profile. Binary format has the profile summary in its header.
  1175. void SampleProfileReader::computeSummary() {
  1176. SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
  1177. for (const auto &I : Profiles) {
  1178. const FunctionSamples &Profile = I.second;
  1179. Builder.addRecord(Profile);
  1180. }
  1181. Summary = Builder.getSummary();
  1182. }