SampleProfReader.cpp 65 KB

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