SampleProfReader.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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/CommandLine.h"
  29. #include "llvm/Support/Compression.h"
  30. #include "llvm/Support/ErrorOr.h"
  31. #include "llvm/Support/LEB128.h"
  32. #include "llvm/Support/LineIterator.h"
  33. #include "llvm/Support/MD5.h"
  34. #include "llvm/Support/MemoryBuffer.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <algorithm>
  37. #include <cstddef>
  38. #include <cstdint>
  39. #include <limits>
  40. #include <memory>
  41. #include <set>
  42. #include <system_error>
  43. #include <vector>
  44. using namespace llvm;
  45. using namespace sampleprof;
  46. #define DEBUG_TYPE "samplepgo-reader"
  47. // This internal option specifies if the profile uses FS discriminators.
  48. // It only applies to text, binary and compact binary format profiles.
  49. // For ext-binary format profiles, the flag is set in the summary.
  50. static cl::opt<bool> ProfileIsFSDisciminator(
  51. "profile-isfs", cl::Hidden, cl::init(false),
  52. cl::desc("Profile uses flow sensitive discriminators"));
  53. /// Dump the function profile for \p FName.
  54. ///
  55. /// \param FContext Name + context of the function to print.
  56. /// \param OS Stream to emit the output to.
  57. void SampleProfileReader::dumpFunctionProfile(SampleContext FContext,
  58. raw_ostream &OS) {
  59. OS << "Function: " << FContext.toString() << ": " << Profiles[FContext];
  60. }
  61. /// Dump all the function profiles found on stream \p OS.
  62. void SampleProfileReader::dump(raw_ostream &OS) {
  63. std::vector<NameFunctionSamples> V;
  64. sortFuncProfiles(Profiles, V);
  65. for (const auto &I : V)
  66. dumpFunctionProfile(I.first, OS);
  67. }
  68. /// Parse \p Input as function head.
  69. ///
  70. /// Parse one line of \p Input, and update function name in \p FName,
  71. /// function's total sample count in \p NumSamples, function's entry
  72. /// count in \p NumHeadSamples.
  73. ///
  74. /// \returns true if parsing is successful.
  75. static bool ParseHead(const StringRef &Input, StringRef &FName,
  76. uint64_t &NumSamples, uint64_t &NumHeadSamples) {
  77. if (Input[0] == ' ')
  78. return false;
  79. size_t n2 = Input.rfind(':');
  80. size_t n1 = Input.rfind(':', n2 - 1);
  81. FName = Input.substr(0, n1);
  82. if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
  83. return false;
  84. if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
  85. return false;
  86. return true;
  87. }
  88. /// Returns true if line offset \p L is legal (only has 16 bits).
  89. static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
  90. /// Parse \p Input that contains metadata.
  91. /// Possible metadata:
  92. /// - CFG Checksum information:
  93. /// !CFGChecksum: 12345
  94. /// - CFG Checksum information:
  95. /// !Attributes: 1
  96. /// Stores the FunctionHash (a.k.a. CFG Checksum) into \p FunctionHash.
  97. static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash,
  98. uint32_t &Attributes) {
  99. if (Input.startswith("!CFGChecksum:")) {
  100. StringRef CFGInfo = Input.substr(strlen("!CFGChecksum:")).trim();
  101. return !CFGInfo.getAsInteger(10, FunctionHash);
  102. }
  103. if (Input.startswith("!Attributes:")) {
  104. StringRef Attrib = Input.substr(strlen("!Attributes:")).trim();
  105. return !Attrib.getAsInteger(10, Attributes);
  106. }
  107. return false;
  108. }
  109. enum class LineType {
  110. CallSiteProfile,
  111. BodyProfile,
  112. Metadata,
  113. };
  114. /// Parse \p Input as line sample.
  115. ///
  116. /// \param Input input line.
  117. /// \param LineTy Type of this line.
  118. /// \param Depth the depth of the inline stack.
  119. /// \param NumSamples total samples of the line/inlined callsite.
  120. /// \param LineOffset line offset to the start of the function.
  121. /// \param Discriminator discriminator of the line.
  122. /// \param TargetCountMap map from indirect call target to count.
  123. /// \param FunctionHash the function's CFG hash, used by pseudo probe.
  124. ///
  125. /// returns true if parsing is successful.
  126. static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth,
  127. uint64_t &NumSamples, uint32_t &LineOffset,
  128. uint32_t &Discriminator, StringRef &CalleeName,
  129. DenseMap<StringRef, uint64_t> &TargetCountMap,
  130. uint64_t &FunctionHash, uint32_t &Attributes) {
  131. for (Depth = 0; Input[Depth] == ' '; Depth++)
  132. ;
  133. if (Depth == 0)
  134. return false;
  135. if (Input[Depth] == '!') {
  136. LineTy = LineType::Metadata;
  137. return parseMetadata(Input.substr(Depth), FunctionHash, Attributes);
  138. }
  139. size_t n1 = Input.find(':');
  140. StringRef Loc = Input.substr(Depth, n1 - Depth);
  141. size_t n2 = Loc.find('.');
  142. if (n2 == StringRef::npos) {
  143. if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
  144. return false;
  145. Discriminator = 0;
  146. } else {
  147. if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
  148. return false;
  149. if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
  150. return false;
  151. }
  152. StringRef Rest = Input.substr(n1 + 2);
  153. if (isDigit(Rest[0])) {
  154. LineTy = LineType::BodyProfile;
  155. size_t n3 = Rest.find(' ');
  156. if (n3 == StringRef::npos) {
  157. if (Rest.getAsInteger(10, NumSamples))
  158. return false;
  159. } else {
  160. if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
  161. return false;
  162. }
  163. // Find call targets and their sample counts.
  164. // Note: In some cases, there are symbols in the profile which are not
  165. // mangled. To accommodate such cases, use colon + integer pairs as the
  166. // anchor points.
  167. // An example:
  168. // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
  169. // ":1000" and ":437" are used as anchor points so the string above will
  170. // be interpreted as
  171. // target: _M_construct<char *>
  172. // count: 1000
  173. // target: string_view<std::allocator<char> >
  174. // count: 437
  175. while (n3 != StringRef::npos) {
  176. n3 += Rest.substr(n3).find_first_not_of(' ');
  177. Rest = Rest.substr(n3);
  178. n3 = Rest.find_first_of(':');
  179. if (n3 == StringRef::npos || n3 == 0)
  180. return false;
  181. StringRef Target;
  182. uint64_t count, n4;
  183. while (true) {
  184. // Get the segment after the current colon.
  185. StringRef AfterColon = Rest.substr(n3 + 1);
  186. // Get the target symbol before the current colon.
  187. Target = Rest.substr(0, n3);
  188. // Check if the word after the current colon is an integer.
  189. n4 = AfterColon.find_first_of(' ');
  190. n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
  191. StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
  192. if (!WordAfterColon.getAsInteger(10, count))
  193. break;
  194. // Try to find the next colon.
  195. uint64_t n5 = AfterColon.find_first_of(':');
  196. if (n5 == StringRef::npos)
  197. return false;
  198. n3 += n5 + 1;
  199. }
  200. // An anchor point is found. Save the {target, count} pair
  201. TargetCountMap[Target] = count;
  202. if (n4 == Rest.size())
  203. break;
  204. // Change n3 to the next blank space after colon + integer pair.
  205. n3 = n4;
  206. }
  207. } else {
  208. LineTy = LineType::CallSiteProfile;
  209. size_t n3 = Rest.find_last_of(':');
  210. CalleeName = Rest.substr(0, n3);
  211. if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
  212. return false;
  213. }
  214. return true;
  215. }
  216. /// Load samples from a text file.
  217. ///
  218. /// See the documentation at the top of the file for an explanation of
  219. /// the expected format.
  220. ///
  221. /// \returns true if the file was loaded successfully, false otherwise.
  222. std::error_code SampleProfileReaderText::readImpl() {
  223. line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
  224. sampleprof_error Result = sampleprof_error::success;
  225. InlineCallStack InlineStack;
  226. uint32_t TopLevelProbeProfileCount = 0;
  227. // DepthMetadata tracks whether we have processed metadata for the current
  228. // top-level or nested function profile.
  229. uint32_t DepthMetadata = 0;
  230. ProfileIsFS = ProfileIsFSDisciminator;
  231. FunctionSamples::ProfileIsFS = ProfileIsFS;
  232. for (; !LineIt.is_at_eof(); ++LineIt) {
  233. if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
  234. continue;
  235. // Read the header of each function.
  236. //
  237. // Note that for function identifiers we are actually expecting
  238. // mangled names, but we may not always get them. This happens when
  239. // the compiler decides not to emit the function (e.g., it was inlined
  240. // and removed). In this case, the binary will not have the linkage
  241. // name for the function, so the profiler will emit the function's
  242. // unmangled name, which may contain characters like ':' and '>' in its
  243. // name (member functions, templates, etc).
  244. //
  245. // The only requirement we place on the identifier, then, is that it
  246. // should not begin with a number.
  247. if ((*LineIt)[0] != ' ') {
  248. uint64_t NumSamples, NumHeadSamples;
  249. StringRef FName;
  250. if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
  251. reportError(LineIt.line_number(),
  252. "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
  253. return sampleprof_error::malformed;
  254. }
  255. DepthMetadata = 0;
  256. SampleContext FContext(FName, CSNameTable);
  257. if (FContext.hasContext())
  258. ++CSProfileCount;
  259. Profiles[FContext] = FunctionSamples();
  260. FunctionSamples &FProfile = Profiles[FContext];
  261. FProfile.setContext(FContext);
  262. MergeResult(Result, FProfile.addTotalSamples(NumSamples));
  263. MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
  264. InlineStack.clear();
  265. InlineStack.push_back(&FProfile);
  266. } else {
  267. uint64_t NumSamples;
  268. StringRef FName;
  269. DenseMap<StringRef, uint64_t> TargetCountMap;
  270. uint32_t Depth, LineOffset, Discriminator;
  271. LineType LineTy;
  272. uint64_t FunctionHash = 0;
  273. uint32_t Attributes = 0;
  274. if (!ParseLine(*LineIt, LineTy, Depth, NumSamples, LineOffset,
  275. Discriminator, FName, TargetCountMap, FunctionHash,
  276. Attributes)) {
  277. reportError(LineIt.line_number(),
  278. "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
  279. *LineIt);
  280. return sampleprof_error::malformed;
  281. }
  282. if (LineTy != LineType::Metadata && Depth == DepthMetadata) {
  283. // Metadata must be put at the end of a function profile.
  284. reportError(LineIt.line_number(),
  285. "Found non-metadata after metadata: " + *LineIt);
  286. return sampleprof_error::malformed;
  287. }
  288. // Here we handle FS discriminators.
  289. Discriminator &= getDiscriminatorMask();
  290. while (InlineStack.size() > Depth) {
  291. InlineStack.pop_back();
  292. }
  293. switch (LineTy) {
  294. case LineType::CallSiteProfile: {
  295. FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
  296. LineLocation(LineOffset, Discriminator))[std::string(FName)];
  297. FSamples.setName(FName);
  298. MergeResult(Result, FSamples.addTotalSamples(NumSamples));
  299. InlineStack.push_back(&FSamples);
  300. DepthMetadata = 0;
  301. break;
  302. }
  303. case LineType::BodyProfile: {
  304. while (InlineStack.size() > Depth) {
  305. InlineStack.pop_back();
  306. }
  307. FunctionSamples &FProfile = *InlineStack.back();
  308. for (const auto &name_count : TargetCountMap) {
  309. MergeResult(Result, FProfile.addCalledTargetSamples(
  310. LineOffset, Discriminator, name_count.first,
  311. name_count.second));
  312. }
  313. MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
  314. NumSamples));
  315. break;
  316. }
  317. case LineType::Metadata: {
  318. FunctionSamples &FProfile = *InlineStack.back();
  319. if (FunctionHash) {
  320. FProfile.setFunctionHash(FunctionHash);
  321. if (Depth == 1)
  322. ++TopLevelProbeProfileCount;
  323. }
  324. FProfile.getContext().setAllAttributes(Attributes);
  325. if (Attributes & (uint32_t)ContextShouldBeInlined)
  326. ProfileIsCSNested = true;
  327. DepthMetadata = Depth;
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
  334. "Cannot have both context-sensitive and regular profile");
  335. ProfileIsCSFlat = (CSProfileCount > 0);
  336. assert((TopLevelProbeProfileCount == 0 ||
  337. TopLevelProbeProfileCount == Profiles.size()) &&
  338. "Cannot have both probe-based profiles and regular profiles");
  339. ProfileIsProbeBased = (TopLevelProbeProfileCount > 0);
  340. FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
  341. FunctionSamples::ProfileIsCSFlat = ProfileIsCSFlat;
  342. FunctionSamples::ProfileIsCSNested = ProfileIsCSNested;
  343. if (Result == sampleprof_error::success)
  344. computeSummary();
  345. return Result;
  346. }
  347. bool SampleProfileReaderText::hasFormat(const MemoryBuffer &Buffer) {
  348. bool result = false;
  349. // Check that the first non-comment line is a valid function header.
  350. line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
  351. if (!LineIt.is_at_eof()) {
  352. if ((*LineIt)[0] != ' ') {
  353. uint64_t NumSamples, NumHeadSamples;
  354. StringRef FName;
  355. result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
  356. }
  357. }
  358. return result;
  359. }
  360. template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() {
  361. unsigned NumBytesRead = 0;
  362. std::error_code EC;
  363. uint64_t Val = decodeULEB128(Data, &NumBytesRead);
  364. if (Val > std::numeric_limits<T>::max())
  365. EC = sampleprof_error::malformed;
  366. else if (Data + NumBytesRead > End)
  367. EC = sampleprof_error::truncated;
  368. else
  369. EC = sampleprof_error::success;
  370. if (EC) {
  371. reportError(0, EC.message());
  372. return EC;
  373. }
  374. Data += NumBytesRead;
  375. return static_cast<T>(Val);
  376. }
  377. ErrorOr<StringRef> SampleProfileReaderBinary::readString() {
  378. std::error_code EC;
  379. StringRef Str(reinterpret_cast<const char *>(Data));
  380. if (Data + Str.size() + 1 > End) {
  381. EC = sampleprof_error::truncated;
  382. reportError(0, EC.message());
  383. return EC;
  384. }
  385. Data += Str.size() + 1;
  386. return Str;
  387. }
  388. template <typename T>
  389. ErrorOr<T> SampleProfileReaderBinary::readUnencodedNumber() {
  390. std::error_code EC;
  391. if (Data + sizeof(T) > End) {
  392. EC = sampleprof_error::truncated;
  393. reportError(0, EC.message());
  394. return EC;
  395. }
  396. using namespace support;
  397. T Val = endian::readNext<T, little, unaligned>(Data);
  398. return Val;
  399. }
  400. template <typename T>
  401. inline ErrorOr<uint32_t> SampleProfileReaderBinary::readStringIndex(T &Table) {
  402. std::error_code EC;
  403. auto Idx = readNumber<uint32_t>();
  404. if (std::error_code EC = Idx.getError())
  405. return EC;
  406. if (*Idx >= Table.size())
  407. return sampleprof_error::truncated_name_table;
  408. return *Idx;
  409. }
  410. ErrorOr<StringRef> SampleProfileReaderBinary::readStringFromTable() {
  411. auto Idx = readStringIndex(NameTable);
  412. if (std::error_code EC = Idx.getError())
  413. return EC;
  414. return NameTable[*Idx];
  415. }
  416. ErrorOr<SampleContext> SampleProfileReaderBinary::readSampleContextFromTable() {
  417. auto FName(readStringFromTable());
  418. if (std::error_code EC = FName.getError())
  419. return EC;
  420. return SampleContext(*FName);
  421. }
  422. ErrorOr<StringRef> SampleProfileReaderExtBinaryBase::readStringFromTable() {
  423. if (!FixedLengthMD5)
  424. return SampleProfileReaderBinary::readStringFromTable();
  425. // read NameTable index.
  426. auto Idx = readStringIndex(NameTable);
  427. if (std::error_code EC = Idx.getError())
  428. return EC;
  429. // Check whether the name to be accessed has been accessed before,
  430. // if not, read it from memory directly.
  431. StringRef &SR = NameTable[*Idx];
  432. if (SR.empty()) {
  433. const uint8_t *SavedData = Data;
  434. Data = MD5NameMemStart + ((*Idx) * sizeof(uint64_t));
  435. auto FID = readUnencodedNumber<uint64_t>();
  436. if (std::error_code EC = FID.getError())
  437. return EC;
  438. // Save the string converted from uint64_t in MD5StringBuf. All the
  439. // references to the name are all StringRefs refering to the string
  440. // in MD5StringBuf.
  441. MD5StringBuf->push_back(std::to_string(*FID));
  442. SR = MD5StringBuf->back();
  443. Data = SavedData;
  444. }
  445. return SR;
  446. }
  447. ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
  448. auto Idx = readStringIndex(NameTable);
  449. if (std::error_code EC = Idx.getError())
  450. return EC;
  451. return StringRef(NameTable[*Idx]);
  452. }
  453. std::error_code
  454. SampleProfileReaderBinary::readProfile(FunctionSamples &FProfile) {
  455. auto NumSamples = readNumber<uint64_t>();
  456. if (std::error_code EC = NumSamples.getError())
  457. return EC;
  458. FProfile.addTotalSamples(*NumSamples);
  459. // Read the samples in the body.
  460. auto NumRecords = readNumber<uint32_t>();
  461. if (std::error_code EC = NumRecords.getError())
  462. return EC;
  463. for (uint32_t I = 0; I < *NumRecords; ++I) {
  464. auto LineOffset = readNumber<uint64_t>();
  465. if (std::error_code EC = LineOffset.getError())
  466. return EC;
  467. if (!isOffsetLegal(*LineOffset)) {
  468. return std::error_code();
  469. }
  470. auto Discriminator = readNumber<uint64_t>();
  471. if (std::error_code EC = Discriminator.getError())
  472. return EC;
  473. auto NumSamples = readNumber<uint64_t>();
  474. if (std::error_code EC = NumSamples.getError())
  475. return EC;
  476. auto NumCalls = readNumber<uint32_t>();
  477. if (std::error_code EC = NumCalls.getError())
  478. return EC;
  479. // Here we handle FS discriminators:
  480. uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
  481. for (uint32_t J = 0; J < *NumCalls; ++J) {
  482. auto CalledFunction(readStringFromTable());
  483. if (std::error_code EC = CalledFunction.getError())
  484. return EC;
  485. auto CalledFunctionSamples = readNumber<uint64_t>();
  486. if (std::error_code EC = CalledFunctionSamples.getError())
  487. return EC;
  488. FProfile.addCalledTargetSamples(*LineOffset, DiscriminatorVal,
  489. *CalledFunction, *CalledFunctionSamples);
  490. }
  491. FProfile.addBodySamples(*LineOffset, DiscriminatorVal, *NumSamples);
  492. }
  493. // Read all the samples for inlined function calls.
  494. auto NumCallsites = readNumber<uint32_t>();
  495. if (std::error_code EC = NumCallsites.getError())
  496. return EC;
  497. for (uint32_t J = 0; J < *NumCallsites; ++J) {
  498. auto LineOffset = readNumber<uint64_t>();
  499. if (std::error_code EC = LineOffset.getError())
  500. return EC;
  501. auto Discriminator = readNumber<uint64_t>();
  502. if (std::error_code EC = Discriminator.getError())
  503. return EC;
  504. auto FName(readStringFromTable());
  505. if (std::error_code EC = FName.getError())
  506. return EC;
  507. // Here we handle FS discriminators:
  508. uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
  509. FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
  510. LineLocation(*LineOffset, DiscriminatorVal))[std::string(*FName)];
  511. CalleeProfile.setName(*FName);
  512. if (std::error_code EC = readProfile(CalleeProfile))
  513. return EC;
  514. }
  515. return sampleprof_error::success;
  516. }
  517. std::error_code
  518. SampleProfileReaderBinary::readFuncProfile(const uint8_t *Start) {
  519. Data = Start;
  520. auto NumHeadSamples = readNumber<uint64_t>();
  521. if (std::error_code EC = NumHeadSamples.getError())
  522. return EC;
  523. ErrorOr<SampleContext> FContext(readSampleContextFromTable());
  524. if (std::error_code EC = FContext.getError())
  525. return EC;
  526. Profiles[*FContext] = FunctionSamples();
  527. FunctionSamples &FProfile = Profiles[*FContext];
  528. FProfile.setContext(*FContext);
  529. FProfile.addHeadSamples(*NumHeadSamples);
  530. if (FContext->hasContext())
  531. CSProfileCount++;
  532. if (std::error_code EC = readProfile(FProfile))
  533. return EC;
  534. return sampleprof_error::success;
  535. }
  536. std::error_code SampleProfileReaderBinary::readImpl() {
  537. ProfileIsFS = ProfileIsFSDisciminator;
  538. FunctionSamples::ProfileIsFS = ProfileIsFS;
  539. while (!at_eof()) {
  540. if (std::error_code EC = readFuncProfile(Data))
  541. return EC;
  542. }
  543. return sampleprof_error::success;
  544. }
  545. ErrorOr<SampleContextFrames>
  546. SampleProfileReaderExtBinaryBase::readContextFromTable() {
  547. auto ContextIdx = readNumber<uint32_t>();
  548. if (std::error_code EC = ContextIdx.getError())
  549. return EC;
  550. if (*ContextIdx >= CSNameTable->size())
  551. return sampleprof_error::truncated_name_table;
  552. return (*CSNameTable)[*ContextIdx];
  553. }
  554. ErrorOr<SampleContext>
  555. SampleProfileReaderExtBinaryBase::readSampleContextFromTable() {
  556. if (ProfileIsCSFlat) {
  557. auto FContext(readContextFromTable());
  558. if (std::error_code EC = FContext.getError())
  559. return EC;
  560. return SampleContext(*FContext);
  561. } else {
  562. auto FName(readStringFromTable());
  563. if (std::error_code EC = FName.getError())
  564. return EC;
  565. return SampleContext(*FName);
  566. }
  567. }
  568. std::error_code SampleProfileReaderExtBinaryBase::readOneSection(
  569. const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
  570. Data = Start;
  571. End = Start + Size;
  572. switch (Entry.Type) {
  573. case SecProfSummary:
  574. if (std::error_code EC = readSummary())
  575. return EC;
  576. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
  577. Summary->setPartialProfile(true);
  578. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFullContext))
  579. FunctionSamples::ProfileIsCSFlat = ProfileIsCSFlat = true;
  580. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsCSNested))
  581. FunctionSamples::ProfileIsCSNested = ProfileIsCSNested;
  582. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator))
  583. FunctionSamples::ProfileIsFS = ProfileIsFS = true;
  584. break;
  585. case SecNameTable: {
  586. FixedLengthMD5 =
  587. hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5);
  588. bool UseMD5 = hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name);
  589. assert((!FixedLengthMD5 || UseMD5) &&
  590. "If FixedLengthMD5 is true, UseMD5 has to be true");
  591. FunctionSamples::HasUniqSuffix =
  592. hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix);
  593. if (std::error_code EC = readNameTableSec(UseMD5))
  594. return EC;
  595. break;
  596. }
  597. case SecCSNameTable: {
  598. if (std::error_code EC = readCSNameTableSec())
  599. return EC;
  600. break;
  601. }
  602. case SecLBRProfile:
  603. if (std::error_code EC = readFuncProfiles())
  604. return EC;
  605. break;
  606. case SecFuncOffsetTable:
  607. FuncOffsetsOrdered = hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered);
  608. if (std::error_code EC = readFuncOffsetTable())
  609. return EC;
  610. break;
  611. case SecFuncMetadata: {
  612. ProfileIsProbeBased =
  613. hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased);
  614. FunctionSamples::ProfileIsProbeBased = ProfileIsProbeBased;
  615. bool HasAttribute =
  616. hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagHasAttribute);
  617. if (std::error_code EC = readFuncMetadata(HasAttribute))
  618. return EC;
  619. break;
  620. }
  621. case SecProfileSymbolList:
  622. if (std::error_code EC = readProfileSymbolList())
  623. return EC;
  624. break;
  625. default:
  626. if (std::error_code EC = readCustomSection(Entry))
  627. return EC;
  628. break;
  629. }
  630. return sampleprof_error::success;
  631. }
  632. bool SampleProfileReaderExtBinaryBase::collectFuncsFromModule() {
  633. if (!M)
  634. return false;
  635. FuncsToUse.clear();
  636. for (auto &F : *M)
  637. FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
  638. return true;
  639. }
  640. std::error_code SampleProfileReaderExtBinaryBase::readFuncOffsetTable() {
  641. // If there are more than one FuncOffsetTable, the profile read associated
  642. // with previous FuncOffsetTable has to be done before next FuncOffsetTable
  643. // is read.
  644. FuncOffsetTable.clear();
  645. auto Size = readNumber<uint64_t>();
  646. if (std::error_code EC = Size.getError())
  647. return EC;
  648. FuncOffsetTable.reserve(*Size);
  649. if (FuncOffsetsOrdered) {
  650. OrderedFuncOffsets =
  651. std::make_unique<std::vector<std::pair<SampleContext, uint64_t>>>();
  652. OrderedFuncOffsets->reserve(*Size);
  653. }
  654. for (uint32_t I = 0; I < *Size; ++I) {
  655. auto FContext(readSampleContextFromTable());
  656. if (std::error_code EC = FContext.getError())
  657. return EC;
  658. auto Offset = readNumber<uint64_t>();
  659. if (std::error_code EC = Offset.getError())
  660. return EC;
  661. FuncOffsetTable[*FContext] = *Offset;
  662. if (FuncOffsetsOrdered)
  663. OrderedFuncOffsets->emplace_back(*FContext, *Offset);
  664. }
  665. return sampleprof_error::success;
  666. }
  667. std::error_code SampleProfileReaderExtBinaryBase::readFuncProfiles() {
  668. // Collect functions used by current module if the Reader has been
  669. // given a module.
  670. // collectFuncsFromModule uses FunctionSamples::getCanonicalFnName
  671. // which will query FunctionSamples::HasUniqSuffix, so it has to be
  672. // called after FunctionSamples::HasUniqSuffix is set, i.e. after
  673. // NameTable section is read.
  674. bool LoadFuncsToBeUsed = collectFuncsFromModule();
  675. // When LoadFuncsToBeUsed is false, load all the function profiles.
  676. const uint8_t *Start = Data;
  677. if (!LoadFuncsToBeUsed) {
  678. while (Data < End) {
  679. if (std::error_code EC = readFuncProfile(Data))
  680. return EC;
  681. }
  682. assert(Data == End && "More data is read than expected");
  683. } else {
  684. // Load function profiles on demand.
  685. if (Remapper) {
  686. for (auto Name : FuncsToUse) {
  687. Remapper->insert(Name);
  688. }
  689. }
  690. if (ProfileIsCSFlat) {
  691. DenseSet<uint64_t> FuncGuidsToUse;
  692. if (useMD5()) {
  693. for (auto Name : FuncsToUse)
  694. FuncGuidsToUse.insert(Function::getGUID(Name));
  695. }
  696. // For each function in current module, load all context profiles for
  697. // the function as well as their callee contexts which can help profile
  698. // guided importing for ThinLTO. This can be achieved by walking
  699. // through an ordered context container, where contexts are laid out
  700. // as if they were walked in preorder of a context trie. While
  701. // traversing the trie, a link to the highest common ancestor node is
  702. // kept so that all of its decendants will be loaded.
  703. assert(OrderedFuncOffsets.get() &&
  704. "func offset table should always be sorted in CS profile");
  705. const SampleContext *CommonContext = nullptr;
  706. for (const auto &NameOffset : *OrderedFuncOffsets) {
  707. const auto &FContext = NameOffset.first;
  708. auto FName = FContext.getName();
  709. // For function in the current module, keep its farthest ancestor
  710. // context. This can be used to load itself and its child and
  711. // sibling contexts.
  712. if ((useMD5() && FuncGuidsToUse.count(std::stoull(FName.data()))) ||
  713. (!useMD5() && (FuncsToUse.count(FName) ||
  714. (Remapper && Remapper->exist(FName))))) {
  715. if (!CommonContext || !CommonContext->IsPrefixOf(FContext))
  716. CommonContext = &FContext;
  717. }
  718. if (CommonContext == &FContext ||
  719. (CommonContext && CommonContext->IsPrefixOf(FContext))) {
  720. // Load profile for the current context which originated from
  721. // the common ancestor.
  722. const uint8_t *FuncProfileAddr = Start + NameOffset.second;
  723. assert(FuncProfileAddr < End && "out of LBRProfile section");
  724. if (std::error_code EC = readFuncProfile(FuncProfileAddr))
  725. return EC;
  726. }
  727. }
  728. } else {
  729. if (useMD5()) {
  730. for (auto Name : FuncsToUse) {
  731. auto GUID = std::to_string(MD5Hash(Name));
  732. auto iter = FuncOffsetTable.find(StringRef(GUID));
  733. if (iter == FuncOffsetTable.end())
  734. continue;
  735. const uint8_t *FuncProfileAddr = Start + iter->second;
  736. assert(FuncProfileAddr < End && "out of LBRProfile section");
  737. if (std::error_code EC = readFuncProfile(FuncProfileAddr))
  738. return EC;
  739. }
  740. } else {
  741. for (auto NameOffset : FuncOffsetTable) {
  742. SampleContext FContext(NameOffset.first);
  743. auto FuncName = FContext.getName();
  744. if (!FuncsToUse.count(FuncName) &&
  745. (!Remapper || !Remapper->exist(FuncName)))
  746. continue;
  747. const uint8_t *FuncProfileAddr = Start + NameOffset.second;
  748. assert(FuncProfileAddr < End && "out of LBRProfile section");
  749. if (std::error_code EC = readFuncProfile(FuncProfileAddr))
  750. return EC;
  751. }
  752. }
  753. }
  754. Data = End;
  755. }
  756. assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
  757. "Cannot have both context-sensitive and regular profile");
  758. assert((!CSProfileCount || ProfileIsCSFlat) &&
  759. "Section flag should be consistent with actual profile");
  760. return sampleprof_error::success;
  761. }
  762. std::error_code SampleProfileReaderExtBinaryBase::readProfileSymbolList() {
  763. if (!ProfSymList)
  764. ProfSymList = std::make_unique<ProfileSymbolList>();
  765. if (std::error_code EC = ProfSymList->read(Data, End - Data))
  766. return EC;
  767. Data = End;
  768. return sampleprof_error::success;
  769. }
  770. std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
  771. const uint8_t *SecStart, const uint64_t SecSize,
  772. const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
  773. Data = SecStart;
  774. End = SecStart + SecSize;
  775. auto DecompressSize = readNumber<uint64_t>();
  776. if (std::error_code EC = DecompressSize.getError())
  777. return EC;
  778. DecompressBufSize = *DecompressSize;
  779. auto CompressSize = readNumber<uint64_t>();
  780. if (std::error_code EC = CompressSize.getError())
  781. return EC;
  782. if (!llvm::zlib::isAvailable())
  783. return sampleprof_error::zlib_unavailable;
  784. StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
  785. *CompressSize);
  786. char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
  787. size_t UCSize = DecompressBufSize;
  788. llvm::Error E =
  789. zlib::uncompress(CompressedStrings, Buffer, UCSize);
  790. if (E)
  791. return sampleprof_error::uncompress_failed;
  792. DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
  793. return sampleprof_error::success;
  794. }
  795. std::error_code SampleProfileReaderExtBinaryBase::readImpl() {
  796. const uint8_t *BufStart =
  797. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  798. for (auto &Entry : SecHdrTable) {
  799. // Skip empty section.
  800. if (!Entry.Size)
  801. continue;
  802. // Skip sections without context when SkipFlatProf is true.
  803. if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
  804. continue;
  805. const uint8_t *SecStart = BufStart + Entry.Offset;
  806. uint64_t SecSize = Entry.Size;
  807. // If the section is compressed, decompress it into a buffer
  808. // DecompressBuf before reading the actual data. The pointee of
  809. // 'Data' will be changed to buffer hold by DecompressBuf
  810. // temporarily when reading the actual data.
  811. bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
  812. if (isCompressed) {
  813. const uint8_t *DecompressBuf;
  814. uint64_t DecompressBufSize;
  815. if (std::error_code EC = decompressSection(
  816. SecStart, SecSize, DecompressBuf, DecompressBufSize))
  817. return EC;
  818. SecStart = DecompressBuf;
  819. SecSize = DecompressBufSize;
  820. }
  821. if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
  822. return EC;
  823. if (Data != SecStart + SecSize)
  824. return sampleprof_error::malformed;
  825. // Change the pointee of 'Data' from DecompressBuf to original Buffer.
  826. if (isCompressed) {
  827. Data = BufStart + Entry.Offset;
  828. End = BufStart + Buffer->getBufferSize();
  829. }
  830. }
  831. return sampleprof_error::success;
  832. }
  833. std::error_code SampleProfileReaderCompactBinary::readImpl() {
  834. // Collect functions used by current module if the Reader has been
  835. // given a module.
  836. bool LoadFuncsToBeUsed = collectFuncsFromModule();
  837. ProfileIsFS = ProfileIsFSDisciminator;
  838. FunctionSamples::ProfileIsFS = ProfileIsFS;
  839. std::vector<uint64_t> OffsetsToUse;
  840. if (!LoadFuncsToBeUsed) {
  841. // load all the function profiles.
  842. for (auto FuncEntry : FuncOffsetTable) {
  843. OffsetsToUse.push_back(FuncEntry.second);
  844. }
  845. } else {
  846. // load function profiles on demand.
  847. for (auto Name : FuncsToUse) {
  848. auto GUID = std::to_string(MD5Hash(Name));
  849. auto iter = FuncOffsetTable.find(StringRef(GUID));
  850. if (iter == FuncOffsetTable.end())
  851. continue;
  852. OffsetsToUse.push_back(iter->second);
  853. }
  854. }
  855. for (auto Offset : OffsetsToUse) {
  856. const uint8_t *SavedData = Data;
  857. if (std::error_code EC = readFuncProfile(
  858. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
  859. Offset))
  860. return EC;
  861. Data = SavedData;
  862. }
  863. return sampleprof_error::success;
  864. }
  865. std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
  866. if (Magic == SPMagic())
  867. return sampleprof_error::success;
  868. return sampleprof_error::bad_magic;
  869. }
  870. std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
  871. if (Magic == SPMagic(SPF_Ext_Binary))
  872. return sampleprof_error::success;
  873. return sampleprof_error::bad_magic;
  874. }
  875. std::error_code
  876. SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
  877. if (Magic == SPMagic(SPF_Compact_Binary))
  878. return sampleprof_error::success;
  879. return sampleprof_error::bad_magic;
  880. }
  881. std::error_code SampleProfileReaderBinary::readNameTable() {
  882. auto Size = readNumber<uint32_t>();
  883. if (std::error_code EC = Size.getError())
  884. return EC;
  885. NameTable.reserve(*Size + NameTable.size());
  886. for (uint32_t I = 0; I < *Size; ++I) {
  887. auto Name(readString());
  888. if (std::error_code EC = Name.getError())
  889. return EC;
  890. NameTable.push_back(*Name);
  891. }
  892. return sampleprof_error::success;
  893. }
  894. std::error_code SampleProfileReaderExtBinaryBase::readMD5NameTable() {
  895. auto Size = readNumber<uint64_t>();
  896. if (std::error_code EC = Size.getError())
  897. return EC;
  898. MD5StringBuf = std::make_unique<std::vector<std::string>>();
  899. MD5StringBuf->reserve(*Size);
  900. if (FixedLengthMD5) {
  901. // Preallocate and initialize NameTable so we can check whether a name
  902. // index has been read before by checking whether the element in the
  903. // NameTable is empty, meanwhile readStringIndex can do the boundary
  904. // check using the size of NameTable.
  905. NameTable.resize(*Size + NameTable.size());
  906. MD5NameMemStart = Data;
  907. Data = Data + (*Size) * sizeof(uint64_t);
  908. return sampleprof_error::success;
  909. }
  910. NameTable.reserve(*Size);
  911. for (uint32_t I = 0; I < *Size; ++I) {
  912. auto FID = readNumber<uint64_t>();
  913. if (std::error_code EC = FID.getError())
  914. return EC;
  915. MD5StringBuf->push_back(std::to_string(*FID));
  916. // NameTable is a vector of StringRef. Here it is pushing back a
  917. // StringRef initialized with the last string in MD5stringBuf.
  918. NameTable.push_back(MD5StringBuf->back());
  919. }
  920. return sampleprof_error::success;
  921. }
  922. std::error_code SampleProfileReaderExtBinaryBase::readNameTableSec(bool IsMD5) {
  923. if (IsMD5)
  924. return readMD5NameTable();
  925. return SampleProfileReaderBinary::readNameTable();
  926. }
  927. // Read in the CS name table section, which basically contains a list of context
  928. // vectors. Each element of a context vector, aka a frame, refers to the
  929. // underlying raw function names that are stored in the name table, as well as
  930. // a callsite identifier that only makes sense for non-leaf frames.
  931. std::error_code SampleProfileReaderExtBinaryBase::readCSNameTableSec() {
  932. auto Size = readNumber<uint32_t>();
  933. if (std::error_code EC = Size.getError())
  934. return EC;
  935. std::vector<SampleContextFrameVector> *PNameVec =
  936. new std::vector<SampleContextFrameVector>();
  937. PNameVec->reserve(*Size);
  938. for (uint32_t I = 0; I < *Size; ++I) {
  939. PNameVec->emplace_back(SampleContextFrameVector());
  940. auto ContextSize = readNumber<uint32_t>();
  941. if (std::error_code EC = ContextSize.getError())
  942. return EC;
  943. for (uint32_t J = 0; J < *ContextSize; ++J) {
  944. auto FName(readStringFromTable());
  945. if (std::error_code EC = FName.getError())
  946. return EC;
  947. auto LineOffset = readNumber<uint64_t>();
  948. if (std::error_code EC = LineOffset.getError())
  949. return EC;
  950. if (!isOffsetLegal(*LineOffset))
  951. return std::error_code();
  952. auto Discriminator = readNumber<uint64_t>();
  953. if (std::error_code EC = Discriminator.getError())
  954. return EC;
  955. PNameVec->back().emplace_back(
  956. FName.get(), LineLocation(LineOffset.get(), Discriminator.get()));
  957. }
  958. }
  959. // From this point the underlying object of CSNameTable should be immutable.
  960. CSNameTable.reset(PNameVec);
  961. return sampleprof_error::success;
  962. }
  963. std::error_code
  964. SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute,
  965. FunctionSamples *FProfile) {
  966. if (Data < End) {
  967. if (ProfileIsProbeBased) {
  968. auto Checksum = readNumber<uint64_t>();
  969. if (std::error_code EC = Checksum.getError())
  970. return EC;
  971. if (FProfile)
  972. FProfile->setFunctionHash(*Checksum);
  973. }
  974. if (ProfileHasAttribute) {
  975. auto Attributes = readNumber<uint32_t>();
  976. if (std::error_code EC = Attributes.getError())
  977. return EC;
  978. if (FProfile)
  979. FProfile->getContext().setAllAttributes(*Attributes);
  980. }
  981. if (!ProfileIsCSFlat) {
  982. // Read all the attributes for inlined function calls.
  983. auto NumCallsites = readNumber<uint32_t>();
  984. if (std::error_code EC = NumCallsites.getError())
  985. return EC;
  986. for (uint32_t J = 0; J < *NumCallsites; ++J) {
  987. auto LineOffset = readNumber<uint64_t>();
  988. if (std::error_code EC = LineOffset.getError())
  989. return EC;
  990. auto Discriminator = readNumber<uint64_t>();
  991. if (std::error_code EC = Discriminator.getError())
  992. return EC;
  993. auto FContext(readSampleContextFromTable());
  994. if (std::error_code EC = FContext.getError())
  995. return EC;
  996. FunctionSamples *CalleeProfile = nullptr;
  997. if (FProfile) {
  998. CalleeProfile = const_cast<FunctionSamples *>(
  999. &FProfile->functionSamplesAt(LineLocation(
  1000. *LineOffset,
  1001. *Discriminator))[std::string(FContext.get().getName())]);
  1002. }
  1003. if (std::error_code EC =
  1004. readFuncMetadata(ProfileHasAttribute, CalleeProfile))
  1005. return EC;
  1006. }
  1007. }
  1008. }
  1009. return sampleprof_error::success;
  1010. }
  1011. std::error_code
  1012. SampleProfileReaderExtBinaryBase::readFuncMetadata(bool ProfileHasAttribute) {
  1013. while (Data < End) {
  1014. auto FContext(readSampleContextFromTable());
  1015. if (std::error_code EC = FContext.getError())
  1016. return EC;
  1017. FunctionSamples *FProfile = nullptr;
  1018. auto It = Profiles.find(*FContext);
  1019. if (It != Profiles.end())
  1020. FProfile = &It->second;
  1021. if (std::error_code EC = readFuncMetadata(ProfileHasAttribute, FProfile))
  1022. return EC;
  1023. }
  1024. assert(Data == End && "More data is read than expected");
  1025. return sampleprof_error::success;
  1026. }
  1027. std::error_code SampleProfileReaderCompactBinary::readNameTable() {
  1028. auto Size = readNumber<uint64_t>();
  1029. if (std::error_code EC = Size.getError())
  1030. return EC;
  1031. NameTable.reserve(*Size);
  1032. for (uint32_t I = 0; I < *Size; ++I) {
  1033. auto FID = readNumber<uint64_t>();
  1034. if (std::error_code EC = FID.getError())
  1035. return EC;
  1036. NameTable.push_back(std::to_string(*FID));
  1037. }
  1038. return sampleprof_error::success;
  1039. }
  1040. std::error_code
  1041. SampleProfileReaderExtBinaryBase::readSecHdrTableEntry(uint32_t Idx) {
  1042. SecHdrTableEntry Entry;
  1043. auto Type = readUnencodedNumber<uint64_t>();
  1044. if (std::error_code EC = Type.getError())
  1045. return EC;
  1046. Entry.Type = static_cast<SecType>(*Type);
  1047. auto Flags = readUnencodedNumber<uint64_t>();
  1048. if (std::error_code EC = Flags.getError())
  1049. return EC;
  1050. Entry.Flags = *Flags;
  1051. auto Offset = readUnencodedNumber<uint64_t>();
  1052. if (std::error_code EC = Offset.getError())
  1053. return EC;
  1054. Entry.Offset = *Offset;
  1055. auto Size = readUnencodedNumber<uint64_t>();
  1056. if (std::error_code EC = Size.getError())
  1057. return EC;
  1058. Entry.Size = *Size;
  1059. Entry.LayoutIndex = Idx;
  1060. SecHdrTable.push_back(std::move(Entry));
  1061. return sampleprof_error::success;
  1062. }
  1063. std::error_code SampleProfileReaderExtBinaryBase::readSecHdrTable() {
  1064. auto EntryNum = readUnencodedNumber<uint64_t>();
  1065. if (std::error_code EC = EntryNum.getError())
  1066. return EC;
  1067. for (uint32_t i = 0; i < (*EntryNum); i++)
  1068. if (std::error_code EC = readSecHdrTableEntry(i))
  1069. return EC;
  1070. return sampleprof_error::success;
  1071. }
  1072. std::error_code SampleProfileReaderExtBinaryBase::readHeader() {
  1073. const uint8_t *BufStart =
  1074. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  1075. Data = BufStart;
  1076. End = BufStart + Buffer->getBufferSize();
  1077. if (std::error_code EC = readMagicIdent())
  1078. return EC;
  1079. if (std::error_code EC = readSecHdrTable())
  1080. return EC;
  1081. return sampleprof_error::success;
  1082. }
  1083. uint64_t SampleProfileReaderExtBinaryBase::getSectionSize(SecType Type) {
  1084. uint64_t Size = 0;
  1085. for (auto &Entry : SecHdrTable) {
  1086. if (Entry.Type == Type)
  1087. Size += Entry.Size;
  1088. }
  1089. return Size;
  1090. }
  1091. uint64_t SampleProfileReaderExtBinaryBase::getFileSize() {
  1092. // Sections in SecHdrTable is not necessarily in the same order as
  1093. // sections in the profile because section like FuncOffsetTable needs
  1094. // to be written after section LBRProfile but needs to be read before
  1095. // section LBRProfile, so we cannot simply use the last entry in
  1096. // SecHdrTable to calculate the file size.
  1097. uint64_t FileSize = 0;
  1098. for (auto &Entry : SecHdrTable) {
  1099. FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
  1100. }
  1101. return FileSize;
  1102. }
  1103. static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
  1104. std::string Flags;
  1105. if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
  1106. Flags.append("{compressed,");
  1107. else
  1108. Flags.append("{");
  1109. if (hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
  1110. Flags.append("flat,");
  1111. switch (Entry.Type) {
  1112. case SecNameTable:
  1113. if (hasSecFlag(Entry, SecNameTableFlags::SecFlagFixedLengthMD5))
  1114. Flags.append("fixlenmd5,");
  1115. else if (hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name))
  1116. Flags.append("md5,");
  1117. if (hasSecFlag(Entry, SecNameTableFlags::SecFlagUniqSuffix))
  1118. Flags.append("uniq,");
  1119. break;
  1120. case SecProfSummary:
  1121. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagPartial))
  1122. Flags.append("partial,");
  1123. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFullContext))
  1124. Flags.append("context,");
  1125. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagIsCSNested))
  1126. Flags.append("context-nested,");
  1127. if (hasSecFlag(Entry, SecProfSummaryFlags::SecFlagFSDiscriminator))
  1128. Flags.append("fs-discriminator,");
  1129. break;
  1130. case SecFuncOffsetTable:
  1131. if (hasSecFlag(Entry, SecFuncOffsetFlags::SecFlagOrdered))
  1132. Flags.append("ordered,");
  1133. break;
  1134. case SecFuncMetadata:
  1135. if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagIsProbeBased))
  1136. Flags.append("probe,");
  1137. if (hasSecFlag(Entry, SecFuncMetadataFlags::SecFlagHasAttribute))
  1138. Flags.append("attr,");
  1139. break;
  1140. default:
  1141. break;
  1142. }
  1143. char &last = Flags.back();
  1144. if (last == ',')
  1145. last = '}';
  1146. else
  1147. Flags.append("}");
  1148. return Flags;
  1149. }
  1150. bool SampleProfileReaderExtBinaryBase::dumpSectionInfo(raw_ostream &OS) {
  1151. uint64_t TotalSecsSize = 0;
  1152. for (auto &Entry : SecHdrTable) {
  1153. OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
  1154. << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
  1155. << "\n";
  1156. ;
  1157. TotalSecsSize += Entry.Size;
  1158. }
  1159. uint64_t HeaderSize = SecHdrTable.front().Offset;
  1160. assert(HeaderSize + TotalSecsSize == getFileSize() &&
  1161. "Size of 'header + sections' doesn't match the total size of profile");
  1162. OS << "Header Size: " << HeaderSize << "\n";
  1163. OS << "Total Sections Size: " << TotalSecsSize << "\n";
  1164. OS << "File Size: " << getFileSize() << "\n";
  1165. return true;
  1166. }
  1167. std::error_code SampleProfileReaderBinary::readMagicIdent() {
  1168. // Read and check the magic identifier.
  1169. auto Magic = readNumber<uint64_t>();
  1170. if (std::error_code EC = Magic.getError())
  1171. return EC;
  1172. else if (std::error_code EC = verifySPMagic(*Magic))
  1173. return EC;
  1174. // Read the version number.
  1175. auto Version = readNumber<uint64_t>();
  1176. if (std::error_code EC = Version.getError())
  1177. return EC;
  1178. else if (*Version != SPVersion())
  1179. return sampleprof_error::unsupported_version;
  1180. return sampleprof_error::success;
  1181. }
  1182. std::error_code SampleProfileReaderBinary::readHeader() {
  1183. Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
  1184. End = Data + Buffer->getBufferSize();
  1185. if (std::error_code EC = readMagicIdent())
  1186. return EC;
  1187. if (std::error_code EC = readSummary())
  1188. return EC;
  1189. if (std::error_code EC = readNameTable())
  1190. return EC;
  1191. return sampleprof_error::success;
  1192. }
  1193. std::error_code SampleProfileReaderCompactBinary::readHeader() {
  1194. SampleProfileReaderBinary::readHeader();
  1195. if (std::error_code EC = readFuncOffsetTable())
  1196. return EC;
  1197. return sampleprof_error::success;
  1198. }
  1199. std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
  1200. auto TableOffset = readUnencodedNumber<uint64_t>();
  1201. if (std::error_code EC = TableOffset.getError())
  1202. return EC;
  1203. const uint8_t *SavedData = Data;
  1204. const uint8_t *TableStart =
  1205. reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
  1206. *TableOffset;
  1207. Data = TableStart;
  1208. auto Size = readNumber<uint64_t>();
  1209. if (std::error_code EC = Size.getError())
  1210. return EC;
  1211. FuncOffsetTable.reserve(*Size);
  1212. for (uint32_t I = 0; I < *Size; ++I) {
  1213. auto FName(readStringFromTable());
  1214. if (std::error_code EC = FName.getError())
  1215. return EC;
  1216. auto Offset = readNumber<uint64_t>();
  1217. if (std::error_code EC = Offset.getError())
  1218. return EC;
  1219. FuncOffsetTable[*FName] = *Offset;
  1220. }
  1221. End = TableStart;
  1222. Data = SavedData;
  1223. return sampleprof_error::success;
  1224. }
  1225. bool SampleProfileReaderCompactBinary::collectFuncsFromModule() {
  1226. if (!M)
  1227. return false;
  1228. FuncsToUse.clear();
  1229. for (auto &F : *M)
  1230. FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
  1231. return true;
  1232. }
  1233. std::error_code SampleProfileReaderBinary::readSummaryEntry(
  1234. std::vector<ProfileSummaryEntry> &Entries) {
  1235. auto Cutoff = readNumber<uint64_t>();
  1236. if (std::error_code EC = Cutoff.getError())
  1237. return EC;
  1238. auto MinBlockCount = readNumber<uint64_t>();
  1239. if (std::error_code EC = MinBlockCount.getError())
  1240. return EC;
  1241. auto NumBlocks = readNumber<uint64_t>();
  1242. if (std::error_code EC = NumBlocks.getError())
  1243. return EC;
  1244. Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
  1245. return sampleprof_error::success;
  1246. }
  1247. std::error_code SampleProfileReaderBinary::readSummary() {
  1248. auto TotalCount = readNumber<uint64_t>();
  1249. if (std::error_code EC = TotalCount.getError())
  1250. return EC;
  1251. auto MaxBlockCount = readNumber<uint64_t>();
  1252. if (std::error_code EC = MaxBlockCount.getError())
  1253. return EC;
  1254. auto MaxFunctionCount = readNumber<uint64_t>();
  1255. if (std::error_code EC = MaxFunctionCount.getError())
  1256. return EC;
  1257. auto NumBlocks = readNumber<uint64_t>();
  1258. if (std::error_code EC = NumBlocks.getError())
  1259. return EC;
  1260. auto NumFunctions = readNumber<uint64_t>();
  1261. if (std::error_code EC = NumFunctions.getError())
  1262. return EC;
  1263. auto NumSummaryEntries = readNumber<uint64_t>();
  1264. if (std::error_code EC = NumSummaryEntries.getError())
  1265. return EC;
  1266. std::vector<ProfileSummaryEntry> Entries;
  1267. for (unsigned i = 0; i < *NumSummaryEntries; i++) {
  1268. std::error_code EC = readSummaryEntry(Entries);
  1269. if (EC != sampleprof_error::success)
  1270. return EC;
  1271. }
  1272. Summary = std::make_unique<ProfileSummary>(
  1273. ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
  1274. *MaxFunctionCount, *NumBlocks, *NumFunctions);
  1275. return sampleprof_error::success;
  1276. }
  1277. bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer &Buffer) {
  1278. const uint8_t *Data =
  1279. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  1280. uint64_t Magic = decodeULEB128(Data);
  1281. return Magic == SPMagic();
  1282. }
  1283. bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer &Buffer) {
  1284. const uint8_t *Data =
  1285. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  1286. uint64_t Magic = decodeULEB128(Data);
  1287. return Magic == SPMagic(SPF_Ext_Binary);
  1288. }
  1289. bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer &Buffer) {
  1290. const uint8_t *Data =
  1291. reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
  1292. uint64_t Magic = decodeULEB128(Data);
  1293. return Magic == SPMagic(SPF_Compact_Binary);
  1294. }
  1295. std::error_code SampleProfileReaderGCC::skipNextWord() {
  1296. uint32_t dummy;
  1297. if (!GcovBuffer.readInt(dummy))
  1298. return sampleprof_error::truncated;
  1299. return sampleprof_error::success;
  1300. }
  1301. template <typename T> ErrorOr<T> SampleProfileReaderGCC::readNumber() {
  1302. if (sizeof(T) <= sizeof(uint32_t)) {
  1303. uint32_t Val;
  1304. if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
  1305. return static_cast<T>(Val);
  1306. } else if (sizeof(T) <= sizeof(uint64_t)) {
  1307. uint64_t Val;
  1308. if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
  1309. return static_cast<T>(Val);
  1310. }
  1311. std::error_code EC = sampleprof_error::malformed;
  1312. reportError(0, EC.message());
  1313. return EC;
  1314. }
  1315. ErrorOr<StringRef> SampleProfileReaderGCC::readString() {
  1316. StringRef Str;
  1317. if (!GcovBuffer.readString(Str))
  1318. return sampleprof_error::truncated;
  1319. return Str;
  1320. }
  1321. std::error_code SampleProfileReaderGCC::readHeader() {
  1322. // Read the magic identifier.
  1323. if (!GcovBuffer.readGCDAFormat())
  1324. return sampleprof_error::unrecognized_format;
  1325. // Read the version number. Note - the GCC reader does not validate this
  1326. // version, but the profile creator generates v704.
  1327. GCOV::GCOVVersion version;
  1328. if (!GcovBuffer.readGCOVVersion(version))
  1329. return sampleprof_error::unrecognized_format;
  1330. if (version != GCOV::V407)
  1331. return sampleprof_error::unsupported_version;
  1332. // Skip the empty integer.
  1333. if (std::error_code EC = skipNextWord())
  1334. return EC;
  1335. return sampleprof_error::success;
  1336. }
  1337. std::error_code SampleProfileReaderGCC::readSectionTag(uint32_t Expected) {
  1338. uint32_t Tag;
  1339. if (!GcovBuffer.readInt(Tag))
  1340. return sampleprof_error::truncated;
  1341. if (Tag != Expected)
  1342. return sampleprof_error::malformed;
  1343. if (std::error_code EC = skipNextWord())
  1344. return EC;
  1345. return sampleprof_error::success;
  1346. }
  1347. std::error_code SampleProfileReaderGCC::readNameTable() {
  1348. if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
  1349. return EC;
  1350. uint32_t Size;
  1351. if (!GcovBuffer.readInt(Size))
  1352. return sampleprof_error::truncated;
  1353. for (uint32_t I = 0; I < Size; ++I) {
  1354. StringRef Str;
  1355. if (!GcovBuffer.readString(Str))
  1356. return sampleprof_error::truncated;
  1357. Names.push_back(std::string(Str));
  1358. }
  1359. return sampleprof_error::success;
  1360. }
  1361. std::error_code SampleProfileReaderGCC::readFunctionProfiles() {
  1362. if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
  1363. return EC;
  1364. uint32_t NumFunctions;
  1365. if (!GcovBuffer.readInt(NumFunctions))
  1366. return sampleprof_error::truncated;
  1367. InlineCallStack Stack;
  1368. for (uint32_t I = 0; I < NumFunctions; ++I)
  1369. if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
  1370. return EC;
  1371. computeSummary();
  1372. return sampleprof_error::success;
  1373. }
  1374. std::error_code SampleProfileReaderGCC::readOneFunctionProfile(
  1375. const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
  1376. uint64_t HeadCount = 0;
  1377. if (InlineStack.size() == 0)
  1378. if (!GcovBuffer.readInt64(HeadCount))
  1379. return sampleprof_error::truncated;
  1380. uint32_t NameIdx;
  1381. if (!GcovBuffer.readInt(NameIdx))
  1382. return sampleprof_error::truncated;
  1383. StringRef Name(Names[NameIdx]);
  1384. uint32_t NumPosCounts;
  1385. if (!GcovBuffer.readInt(NumPosCounts))
  1386. return sampleprof_error::truncated;
  1387. uint32_t NumCallsites;
  1388. if (!GcovBuffer.readInt(NumCallsites))
  1389. return sampleprof_error::truncated;
  1390. FunctionSamples *FProfile = nullptr;
  1391. if (InlineStack.size() == 0) {
  1392. // If this is a top function that we have already processed, do not
  1393. // update its profile again. This happens in the presence of
  1394. // function aliases. Since these aliases share the same function
  1395. // body, there will be identical replicated profiles for the
  1396. // original function. In this case, we simply not bother updating
  1397. // the profile of the original function.
  1398. FProfile = &Profiles[Name];
  1399. FProfile->addHeadSamples(HeadCount);
  1400. if (FProfile->getTotalSamples() > 0)
  1401. Update = false;
  1402. } else {
  1403. // Otherwise, we are reading an inlined instance. The top of the
  1404. // inline stack contains the profile of the caller. Insert this
  1405. // callee in the caller's CallsiteMap.
  1406. FunctionSamples *CallerProfile = InlineStack.front();
  1407. uint32_t LineOffset = Offset >> 16;
  1408. uint32_t Discriminator = Offset & 0xffff;
  1409. FProfile = &CallerProfile->functionSamplesAt(
  1410. LineLocation(LineOffset, Discriminator))[std::string(Name)];
  1411. }
  1412. FProfile->setName(Name);
  1413. for (uint32_t I = 0; I < NumPosCounts; ++I) {
  1414. uint32_t Offset;
  1415. if (!GcovBuffer.readInt(Offset))
  1416. return sampleprof_error::truncated;
  1417. uint32_t NumTargets;
  1418. if (!GcovBuffer.readInt(NumTargets))
  1419. return sampleprof_error::truncated;
  1420. uint64_t Count;
  1421. if (!GcovBuffer.readInt64(Count))
  1422. return sampleprof_error::truncated;
  1423. // The line location is encoded in the offset as:
  1424. // high 16 bits: line offset to the start of the function.
  1425. // low 16 bits: discriminator.
  1426. uint32_t LineOffset = Offset >> 16;
  1427. uint32_t Discriminator = Offset & 0xffff;
  1428. InlineCallStack NewStack;
  1429. NewStack.push_back(FProfile);
  1430. llvm::append_range(NewStack, InlineStack);
  1431. if (Update) {
  1432. // Walk up the inline stack, adding the samples on this line to
  1433. // the total sample count of the callers in the chain.
  1434. for (auto CallerProfile : NewStack)
  1435. CallerProfile->addTotalSamples(Count);
  1436. // Update the body samples for the current profile.
  1437. FProfile->addBodySamples(LineOffset, Discriminator, Count);
  1438. }
  1439. // Process the list of functions called at an indirect call site.
  1440. // These are all the targets that a function pointer (or virtual
  1441. // function) resolved at runtime.
  1442. for (uint32_t J = 0; J < NumTargets; J++) {
  1443. uint32_t HistVal;
  1444. if (!GcovBuffer.readInt(HistVal))
  1445. return sampleprof_error::truncated;
  1446. if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
  1447. return sampleprof_error::malformed;
  1448. uint64_t TargetIdx;
  1449. if (!GcovBuffer.readInt64(TargetIdx))
  1450. return sampleprof_error::truncated;
  1451. StringRef TargetName(Names[TargetIdx]);
  1452. uint64_t TargetCount;
  1453. if (!GcovBuffer.readInt64(TargetCount))
  1454. return sampleprof_error::truncated;
  1455. if (Update)
  1456. FProfile->addCalledTargetSamples(LineOffset, Discriminator,
  1457. TargetName, TargetCount);
  1458. }
  1459. }
  1460. // Process all the inlined callers into the current function. These
  1461. // are all the callsites that were inlined into this function.
  1462. for (uint32_t I = 0; I < NumCallsites; I++) {
  1463. // The offset is encoded as:
  1464. // high 16 bits: line offset to the start of the function.
  1465. // low 16 bits: discriminator.
  1466. uint32_t Offset;
  1467. if (!GcovBuffer.readInt(Offset))
  1468. return sampleprof_error::truncated;
  1469. InlineCallStack NewStack;
  1470. NewStack.push_back(FProfile);
  1471. llvm::append_range(NewStack, InlineStack);
  1472. if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
  1473. return EC;
  1474. }
  1475. return sampleprof_error::success;
  1476. }
  1477. /// Read a GCC AutoFDO profile.
  1478. ///
  1479. /// This format is generated by the Linux Perf conversion tool at
  1480. /// https://github.com/google/autofdo.
  1481. std::error_code SampleProfileReaderGCC::readImpl() {
  1482. assert(!ProfileIsFSDisciminator && "Gcc profiles not support FSDisciminator");
  1483. // Read the string table.
  1484. if (std::error_code EC = readNameTable())
  1485. return EC;
  1486. // Read the source profile.
  1487. if (std::error_code EC = readFunctionProfiles())
  1488. return EC;
  1489. return sampleprof_error::success;
  1490. }
  1491. bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer &Buffer) {
  1492. StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
  1493. return Magic == "adcg*704";
  1494. }
  1495. void SampleProfileReaderItaniumRemapper::applyRemapping(LLVMContext &Ctx) {
  1496. // If the reader uses MD5 to represent string, we can't remap it because
  1497. // we don't know what the original function names were.
  1498. if (Reader.useMD5()) {
  1499. Ctx.diagnose(DiagnosticInfoSampleProfile(
  1500. Reader.getBuffer()->getBufferIdentifier(),
  1501. "Profile data remapping cannot be applied to profile data "
  1502. "in compact format (original mangled names are not available).",
  1503. DS_Warning));
  1504. return;
  1505. }
  1506. // CSSPGO-TODO: Remapper is not yet supported.
  1507. // We will need to remap the entire context string.
  1508. assert(Remappings && "should be initialized while creating remapper");
  1509. for (auto &Sample : Reader.getProfiles()) {
  1510. DenseSet<StringRef> NamesInSample;
  1511. Sample.second.findAllNames(NamesInSample);
  1512. for (auto &Name : NamesInSample)
  1513. if (auto Key = Remappings->insert(Name))
  1514. NameMap.insert({Key, Name});
  1515. }
  1516. RemappingApplied = true;
  1517. }
  1518. Optional<StringRef>
  1519. SampleProfileReaderItaniumRemapper::lookUpNameInProfile(StringRef Fname) {
  1520. if (auto Key = Remappings->lookup(Fname))
  1521. return NameMap.lookup(Key);
  1522. return None;
  1523. }
  1524. /// Prepare a memory buffer for the contents of \p Filename.
  1525. ///
  1526. /// \returns an error code indicating the status of the buffer.
  1527. static ErrorOr<std::unique_ptr<MemoryBuffer>>
  1528. setupMemoryBuffer(const Twine &Filename) {
  1529. auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
  1530. if (std::error_code EC = BufferOrErr.getError())
  1531. return EC;
  1532. auto Buffer = std::move(BufferOrErr.get());
  1533. // Check the file.
  1534. if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
  1535. return sampleprof_error::too_large;
  1536. return std::move(Buffer);
  1537. }
  1538. /// Create a sample profile reader based on the format of the input file.
  1539. ///
  1540. /// \param Filename The file to open.
  1541. ///
  1542. /// \param C The LLVM context to use to emit diagnostics.
  1543. ///
  1544. /// \param P The FSDiscriminatorPass.
  1545. ///
  1546. /// \param RemapFilename The file used for profile remapping.
  1547. ///
  1548. /// \returns an error code indicating the status of the created reader.
  1549. ErrorOr<std::unique_ptr<SampleProfileReader>>
  1550. SampleProfileReader::create(const std::string Filename, LLVMContext &C,
  1551. FSDiscriminatorPass P,
  1552. const std::string RemapFilename) {
  1553. auto BufferOrError = setupMemoryBuffer(Filename);
  1554. if (std::error_code EC = BufferOrError.getError())
  1555. return EC;
  1556. return create(BufferOrError.get(), C, P, RemapFilename);
  1557. }
  1558. /// Create a sample profile remapper from the given input, to remap the
  1559. /// function names in the given profile data.
  1560. ///
  1561. /// \param Filename The file to open.
  1562. ///
  1563. /// \param Reader The profile reader the remapper is going to be applied to.
  1564. ///
  1565. /// \param C The LLVM context to use to emit diagnostics.
  1566. ///
  1567. /// \returns an error code indicating the status of the created reader.
  1568. ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
  1569. SampleProfileReaderItaniumRemapper::create(const std::string Filename,
  1570. SampleProfileReader &Reader,
  1571. LLVMContext &C) {
  1572. auto BufferOrError = setupMemoryBuffer(Filename);
  1573. if (std::error_code EC = BufferOrError.getError())
  1574. return EC;
  1575. return create(BufferOrError.get(), Reader, C);
  1576. }
  1577. /// Create a sample profile remapper from the given input, to remap the
  1578. /// function names in the given profile data.
  1579. ///
  1580. /// \param B The memory buffer to create the reader from (assumes ownership).
  1581. ///
  1582. /// \param C The LLVM context to use to emit diagnostics.
  1583. ///
  1584. /// \param Reader The profile reader the remapper is going to be applied to.
  1585. ///
  1586. /// \returns an error code indicating the status of the created reader.
  1587. ErrorOr<std::unique_ptr<SampleProfileReaderItaniumRemapper>>
  1588. SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
  1589. SampleProfileReader &Reader,
  1590. LLVMContext &C) {
  1591. auto Remappings = std::make_unique<SymbolRemappingReader>();
  1592. if (Error E = Remappings->read(*B.get())) {
  1593. handleAllErrors(
  1594. std::move(E), [&](const SymbolRemappingParseError &ParseError) {
  1595. C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
  1596. ParseError.getLineNum(),
  1597. ParseError.getMessage()));
  1598. });
  1599. return sampleprof_error::malformed;
  1600. }
  1601. return std::make_unique<SampleProfileReaderItaniumRemapper>(
  1602. std::move(B), std::move(Remappings), Reader);
  1603. }
  1604. /// Create a sample profile reader based on the format of the input data.
  1605. ///
  1606. /// \param B The memory buffer to create the reader from (assumes ownership).
  1607. ///
  1608. /// \param C The LLVM context to use to emit diagnostics.
  1609. ///
  1610. /// \param P The FSDiscriminatorPass.
  1611. ///
  1612. /// \param RemapFilename The file used for profile remapping.
  1613. ///
  1614. /// \returns an error code indicating the status of the created reader.
  1615. ErrorOr<std::unique_ptr<SampleProfileReader>>
  1616. SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
  1617. FSDiscriminatorPass P,
  1618. const std::string RemapFilename) {
  1619. std::unique_ptr<SampleProfileReader> Reader;
  1620. if (SampleProfileReaderRawBinary::hasFormat(*B))
  1621. Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
  1622. else if (SampleProfileReaderExtBinary::hasFormat(*B))
  1623. Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
  1624. else if (SampleProfileReaderCompactBinary::hasFormat(*B))
  1625. Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
  1626. else if (SampleProfileReaderGCC::hasFormat(*B))
  1627. Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
  1628. else if (SampleProfileReaderText::hasFormat(*B))
  1629. Reader.reset(new SampleProfileReaderText(std::move(B), C));
  1630. else
  1631. return sampleprof_error::unrecognized_format;
  1632. if (!RemapFilename.empty()) {
  1633. auto ReaderOrErr =
  1634. SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
  1635. if (std::error_code EC = ReaderOrErr.getError()) {
  1636. std::string Msg = "Could not create remapper: " + EC.message();
  1637. C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
  1638. return EC;
  1639. }
  1640. Reader->Remapper = std::move(ReaderOrErr.get());
  1641. }
  1642. FunctionSamples::Format = Reader->getFormat();
  1643. if (std::error_code EC = Reader->readHeader()) {
  1644. return EC;
  1645. }
  1646. Reader->setDiscriminatorMaskedBitFrom(P);
  1647. return std::move(Reader);
  1648. }
  1649. // For text and GCC file formats, we compute the summary after reading the
  1650. // profile. Binary format has the profile summary in its header.
  1651. void SampleProfileReader::computeSummary() {
  1652. SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
  1653. Summary = Builder.computeSummaryForProfiles(Profiles);
  1654. }