LLVM  13.0.0git
SampleProfReader.cpp
Go to the documentation of this file.
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 
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/IR/ProfileSummary.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/LEB128.h"
34 #include "llvm/Support/MD5.h"
37 #include <algorithm>
38 #include <cstddef>
39 #include <cstdint>
40 #include <limits>
41 #include <memory>
42 #include <set>
43 #include <system_error>
44 #include <vector>
45 
46 using namespace llvm;
47 using namespace sampleprof;
48 
49 #define DEBUG_TYPE "samplepgo-reader"
50 
51 // This internal option specifies if the profile uses FS discriminators.
52 // It only applies to text, binary and compact binary format profiles.
53 // For ext-binary format profiles, the flag is set in the summary.
55  "profile-isfs", cl::Hidden, cl::init(false),
56  cl::desc("Profile uses flow senstive discriminators"));
57 
58 /// Dump the function profile for \p FName.
59 ///
60 /// \param FName Name of the function to print.
61 /// \param OS Stream to emit the output to.
63  raw_ostream &OS) {
64  OS << "Function: " << FName << ": " << Profiles[FName];
65 }
66 
67 /// Dump all the function profiles found on stream \p OS.
69  for (const auto &I : Profiles)
70  dumpFunctionProfile(I.getKey(), OS);
71 }
72 
73 /// Parse \p Input as function head.
74 ///
75 /// Parse one line of \p Input, and update function name in \p FName,
76 /// function's total sample count in \p NumSamples, function's entry
77 /// count in \p NumHeadSamples.
78 ///
79 /// \returns true if parsing is successful.
80 static bool ParseHead(const StringRef &Input, StringRef &FName,
81  uint64_t &NumSamples, uint64_t &NumHeadSamples) {
82  if (Input[0] == ' ')
83  return false;
84  size_t n2 = Input.rfind(':');
85  size_t n1 = Input.rfind(':', n2 - 1);
86  FName = Input.substr(0, n1);
87  if (Input.substr(n1 + 1, n2 - n1 - 1).getAsInteger(10, NumSamples))
88  return false;
89  if (Input.substr(n2 + 1).getAsInteger(10, NumHeadSamples))
90  return false;
91  return true;
92 }
93 
94 /// Returns true if line offset \p L is legal (only has 16 bits).
95 static bool isOffsetLegal(unsigned L) { return (L & 0xffff) == L; }
96 
97 /// Parse \p Input that contains metadata.
98 /// Possible metadata:
99 /// - CFG Checksum information:
100 /// !CFGChecksum: 12345
101 /// - CFG Checksum information:
102 /// !Attributes: 1
103 /// Stores the FunctionHash (a.k.a. CFG Checksum) into \p FunctionHash.
104 static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash,
105  uint32_t &Attributes) {
106  if (Input.startswith("!CFGChecksum:")) {
107  StringRef CFGInfo = Input.substr(strlen("!CFGChecksum:")).trim();
108  return !CFGInfo.getAsInteger(10, FunctionHash);
109  }
110 
111  if (Input.startswith("!Attributes:")) {
112  StringRef Attrib = Input.substr(strlen("!Attributes:")).trim();
113  return !Attrib.getAsInteger(10, Attributes);
114  }
115 
116  return false;
117 }
118 
119 enum class LineType {
121  BodyProfile,
122  Metadata,
123 };
124 
125 /// Parse \p Input as line sample.
126 ///
127 /// \param Input input line.
128 /// \param LineTy Type of this line.
129 /// \param Depth the depth of the inline stack.
130 /// \param NumSamples total samples of the line/inlined callsite.
131 /// \param LineOffset line offset to the start of the function.
132 /// \param Discriminator discriminator of the line.
133 /// \param TargetCountMap map from indirect call target to count.
134 /// \param FunctionHash the function's CFG hash, used by pseudo probe.
135 ///
136 /// returns true if parsing is successful.
137 static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth,
138  uint64_t &NumSamples, uint32_t &LineOffset,
139  uint32_t &Discriminator, StringRef &CalleeName,
140  DenseMap<StringRef, uint64_t> &TargetCountMap,
141  uint64_t &FunctionHash, uint32_t &Attributes) {
142  for (Depth = 0; Input[Depth] == ' '; Depth++)
143  ;
144  if (Depth == 0)
145  return false;
146 
147  if (Depth == 1 && Input[Depth] == '!') {
148  LineTy = LineType::Metadata;
149  return parseMetadata(Input.substr(Depth), FunctionHash, Attributes);
150  }
151 
152  size_t n1 = Input.find(':');
153  StringRef Loc = Input.substr(Depth, n1 - Depth);
154  size_t n2 = Loc.find('.');
155  if (n2 == StringRef::npos) {
156  if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
157  return false;
158  Discriminator = 0;
159  } else {
160  if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
161  return false;
162  if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
163  return false;
164  }
165 
166  StringRef Rest = Input.substr(n1 + 2);
167  if (isDigit(Rest[0])) {
168  LineTy = LineType::BodyProfile;
169  size_t n3 = Rest.find(' ');
170  if (n3 == StringRef::npos) {
171  if (Rest.getAsInteger(10, NumSamples))
172  return false;
173  } else {
174  if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
175  return false;
176  }
177  // Find call targets and their sample counts.
178  // Note: In some cases, there are symbols in the profile which are not
179  // mangled. To accommodate such cases, use colon + integer pairs as the
180  // anchor points.
181  // An example:
182  // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
183  // ":1000" and ":437" are used as anchor points so the string above will
184  // be interpreted as
185  // target: _M_construct<char *>
186  // count: 1000
187  // target: string_view<std::allocator<char> >
188  // count: 437
189  while (n3 != StringRef::npos) {
190  n3 += Rest.substr(n3).find_first_not_of(' ');
191  Rest = Rest.substr(n3);
192  n3 = Rest.find_first_of(':');
193  if (n3 == StringRef::npos || n3 == 0)
194  return false;
195 
197  uint64_t count, n4;
198  while (true) {
199  // Get the segment after the current colon.
200  StringRef AfterColon = Rest.substr(n3 + 1);
201  // Get the target symbol before the current colon.
202  Target = Rest.substr(0, n3);
203  // Check if the word after the current colon is an integer.
204  n4 = AfterColon.find_first_of(' ');
205  n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
206  StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
207  if (!WordAfterColon.getAsInteger(10, count))
208  break;
209 
210  // Try to find the next colon.
211  uint64_t n5 = AfterColon.find_first_of(':');
212  if (n5 == StringRef::npos)
213  return false;
214  n3 += n5 + 1;
215  }
216 
217  // An anchor point is found. Save the {target, count} pair
218  TargetCountMap[Target] = count;
219  if (n4 == Rest.size())
220  break;
221  // Change n3 to the next blank space after colon + integer pair.
222  n3 = n4;
223  }
224  } else {
225  LineTy = LineType::CallSiteProfile;
226  size_t n3 = Rest.find_last_of(':');
227  CalleeName = Rest.substr(0, n3);
228  if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
229  return false;
230  }
231  return true;
232 }
233 
234 /// Load samples from a text file.
235 ///
236 /// See the documentation at the top of the file for an explanation of
237 /// the expected format.
238 ///
239 /// \returns true if the file was loaded successfully, false otherwise.
241  line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
243 
244  InlineCallStack InlineStack;
245  uint32_t ProbeProfileCount = 0;
246 
247  // SeenMetadata tracks whether we have processed metadata for the current
248  // top-level function profile.
249  bool SeenMetadata = false;
250 
252  for (; !LineIt.is_at_eof(); ++LineIt) {
253  if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
254  continue;
255  // Read the header of each function.
256  //
257  // Note that for function identifiers we are actually expecting
258  // mangled names, but we may not always get them. This happens when
259  // the compiler decides not to emit the function (e.g., it was inlined
260  // and removed). In this case, the binary will not have the linkage
261  // name for the function, so the profiler will emit the function's
262  // unmangled name, which may contain characters like ':' and '>' in its
263  // name (member functions, templates, etc).
264  //
265  // The only requirement we place on the identifier, then, is that it
266  // should not begin with a number.
267  if ((*LineIt)[0] != ' ') {
268  uint64_t NumSamples, NumHeadSamples;
269  StringRef FName;
270  if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
271  reportError(LineIt.line_number(),
272  "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
274  }
275  SeenMetadata = false;
276  SampleContext FContext(FName);
277  if (FContext.hasContext())
278  ++CSProfileCount;
279  Profiles[FContext] = FunctionSamples();
280  FunctionSamples &FProfile = Profiles[FContext];
281  FProfile.setName(FContext.getNameWithoutContext());
282  FProfile.setContext(FContext);
283  MergeResult(Result, FProfile.addTotalSamples(NumSamples));
284  MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
285  InlineStack.clear();
286  InlineStack.push_back(&FProfile);
287  } else {
288  uint64_t NumSamples;
289  StringRef FName;
290  DenseMap<StringRef, uint64_t> TargetCountMap;
291  uint32_t Depth, LineOffset, Discriminator;
292  LineType LineTy;
293  uint64_t FunctionHash = 0;
294  uint32_t Attributes = 0;
295  if (!ParseLine(*LineIt, LineTy, Depth, NumSamples, LineOffset,
296  Discriminator, FName, TargetCountMap, FunctionHash,
297  Attributes)) {
298  reportError(LineIt.line_number(),
299  "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
300  *LineIt);
302  }
303  if (SeenMetadata && LineTy != LineType::Metadata) {
304  // Metadata must be put at the end of a function profile.
305  reportError(LineIt.line_number(),
306  "Found non-metadata after metadata: " + *LineIt);
308  }
309 
310  // Here we handle FS discriminators.
311  Discriminator &= getDiscriminatorMask();
312 
313  while (InlineStack.size() > Depth) {
314  InlineStack.pop_back();
315  }
316  switch (LineTy) {
318  FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
319  LineLocation(LineOffset, Discriminator))[std::string(FName)];
320  FSamples.setName(FName);
321  MergeResult(Result, FSamples.addTotalSamples(NumSamples));
322  InlineStack.push_back(&FSamples);
323  break;
324  }
325  case LineType::BodyProfile: {
326  while (InlineStack.size() > Depth) {
327  InlineStack.pop_back();
328  }
329  FunctionSamples &FProfile = *InlineStack.back();
330  for (const auto &name_count : TargetCountMap) {
331  MergeResult(Result, FProfile.addCalledTargetSamples(
332  LineOffset, Discriminator, name_count.first,
333  name_count.second));
334  }
335  MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
336  NumSamples));
337  break;
338  }
339  case LineType::Metadata: {
340  FunctionSamples &FProfile = *InlineStack.back();
341  if (FunctionHash) {
342  FProfile.setFunctionHash(FunctionHash);
343  ++ProbeProfileCount;
344  }
345  if (Attributes)
346  FProfile.getContext().setAllAttributes(Attributes);
347  SeenMetadata = true;
348  break;
349  }
350  }
351  }
352  }
353 
354  assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
355  "Cannot have both context-sensitive and regular profile");
356  ProfileIsCS = (CSProfileCount > 0);
357  assert((ProbeProfileCount == 0 || ProbeProfileCount == Profiles.size()) &&
358  "Cannot have both probe-based profiles and regular profiles");
359  ProfileIsProbeBased = (ProbeProfileCount > 0);
362 
363  if (Result == sampleprof_error::success)
364  computeSummary();
365 
366  return Result;
367 }
368 
370  bool result = false;
371 
372  // Check that the first non-comment line is a valid function header.
373  line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
374  if (!LineIt.is_at_eof()) {
375  if ((*LineIt)[0] != ' ') {
376  uint64_t NumSamples, NumHeadSamples;
377  StringRef FName;
378  result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
379  }
380  }
381 
382  return result;
383 }
384 
386  unsigned NumBytesRead = 0;
387  std::error_code EC;
388  uint64_t Val = decodeULEB128(Data, &NumBytesRead);
389 
390  if (Val > std::numeric_limits<T>::max())
392  else if (Data + NumBytesRead > End)
394  else
396 
397  if (EC) {
398  reportError(0, EC.message());
399  return EC;
400  }
401 
402  Data += NumBytesRead;
403  return static_cast<T>(Val);
404 }
405 
407  std::error_code EC;
408  StringRef Str(reinterpret_cast<const char *>(Data));
409  if (Data + Str.size() + 1 > End) {
411  reportError(0, EC.message());
412  return EC;
413  }
414 
415  Data += Str.size() + 1;
416  return Str;
417 }
418 
419 template <typename T>
421  std::error_code EC;
422 
423  if (Data + sizeof(T) > End) {
425  reportError(0, EC.message());
426  return EC;
427  }
428 
429  using namespace support;
430  T Val = endian::readNext<T, little, unaligned>(Data);
431  return Val;
432 }
433 
434 template <typename T>
436  std::error_code EC;
437  auto Idx = readNumber<uint32_t>();
438  if (std::error_code EC = Idx.getError())
439  return EC;
440  if (*Idx >= Table.size())
442  return *Idx;
443 }
444 
446  auto Idx = readStringIndex(NameTable);
447  if (std::error_code EC = Idx.getError())
448  return EC;
449 
450  return NameTable[*Idx];
451 }
452 
454  if (!FixedLengthMD5)
456 
457  // read NameTable index.
458  auto Idx = readStringIndex(NameTable);
459  if (std::error_code EC = Idx.getError())
460  return EC;
461 
462  // Check whether the name to be accessed has been accessed before,
463  // if not, read it from memory directly.
464  StringRef &SR = NameTable[*Idx];
465  if (SR.empty()) {
466  const uint8_t *SavedData = Data;
467  Data = MD5NameMemStart + ((*Idx) * sizeof(uint64_t));
468  auto FID = readUnencodedNumber<uint64_t>();
469  if (std::error_code EC = FID.getError())
470  return EC;
471  // Save the string converted from uint64_t in MD5StringBuf. All the
472  // references to the name are all StringRefs refering to the string
473  // in MD5StringBuf.
474  MD5StringBuf->push_back(std::to_string(*FID));
475  SR = MD5StringBuf->back();
476  Data = SavedData;
477  }
478  return SR;
479 }
480 
481 ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
482  auto Idx = readStringIndex(NameTable);
483  if (std::error_code EC = Idx.getError())
484  return EC;
485 
486  return StringRef(NameTable[*Idx]);
487 }
488 
489 std::error_code
491  auto NumSamples = readNumber<uint64_t>();
492  if (std::error_code EC = NumSamples.getError())
493  return EC;
494  FProfile.addTotalSamples(*NumSamples);
495 
496  // Read the samples in the body.
497  auto NumRecords = readNumber<uint32_t>();
498  if (std::error_code EC = NumRecords.getError())
499  return EC;
500 
501  for (uint32_t I = 0; I < *NumRecords; ++I) {
502  auto LineOffset = readNumber<uint64_t>();
503  if (std::error_code EC = LineOffset.getError())
504  return EC;
505 
506  if (!isOffsetLegal(*LineOffset)) {
507  return std::error_code();
508  }
509 
510  auto Discriminator = readNumber<uint64_t>();
511  if (std::error_code EC = Discriminator.getError())
512  return EC;
513 
514  auto NumSamples = readNumber<uint64_t>();
515  if (std::error_code EC = NumSamples.getError())
516  return EC;
517 
518  auto NumCalls = readNumber<uint32_t>();
519  if (std::error_code EC = NumCalls.getError())
520  return EC;
521 
522  // Here we handle FS discriminators:
523  uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
524 
525  for (uint32_t J = 0; J < *NumCalls; ++J) {
526  auto CalledFunction(readStringFromTable());
527  if (std::error_code EC = CalledFunction.getError())
528  return EC;
529 
530  auto CalledFunctionSamples = readNumber<uint64_t>();
531  if (std::error_code EC = CalledFunctionSamples.getError())
532  return EC;
533 
534  FProfile.addCalledTargetSamples(*LineOffset, DiscriminatorVal,
535  *CalledFunction, *CalledFunctionSamples);
536  }
537 
538  FProfile.addBodySamples(*LineOffset, DiscriminatorVal, *NumSamples);
539  }
540 
541  // Read all the samples for inlined function calls.
542  auto NumCallsites = readNumber<uint32_t>();
543  if (std::error_code EC = NumCallsites.getError())
544  return EC;
545 
546  for (uint32_t J = 0; J < *NumCallsites; ++J) {
547  auto LineOffset = readNumber<uint64_t>();
548  if (std::error_code EC = LineOffset.getError())
549  return EC;
550 
551  auto Discriminator = readNumber<uint64_t>();
552  if (std::error_code EC = Discriminator.getError())
553  return EC;
554 
555  auto FName(readStringFromTable());
556  if (std::error_code EC = FName.getError())
557  return EC;
558 
559  // Here we handle FS discriminators:
560  uint32_t DiscriminatorVal = (*Discriminator) & getDiscriminatorMask();
561 
562  FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
563  LineLocation(*LineOffset, DiscriminatorVal))[std::string(*FName)];
564  CalleeProfile.setName(*FName);
565  if (std::error_code EC = readProfile(CalleeProfile))
566  return EC;
567  }
568 
570 }
571 
572 std::error_code
574  Data = Start;
575  auto NumHeadSamples = readNumber<uint64_t>();
576  if (std::error_code EC = NumHeadSamples.getError())
577  return EC;
578 
579  auto FName(readStringFromTable());
580  if (std::error_code EC = FName.getError())
581  return EC;
582 
583  SampleContext FContext(*FName);
584  Profiles[FContext] = FunctionSamples();
585  FunctionSamples &FProfile = Profiles[FContext];
586  FProfile.setName(FContext.getNameWithoutContext());
587  FProfile.setContext(FContext);
588  FProfile.addHeadSamples(*NumHeadSamples);
589 
590  if (FContext.hasContext())
591  CSProfileCount++;
592 
593  if (std::error_code EC = readProfile(FProfile))
594  return EC;
596 }
597 
600  while (!at_eof()) {
601  if (std::error_code EC = readFuncProfile(Data))
602  return EC;
603  }
604 
606 }
607 
609  const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry) {
610  Data = Start;
611  End = Start + Size;
612  switch (Entry.Type) {
613  case SecProfSummary:
614  if (std::error_code EC = readSummary())
615  return EC;
617  Summary->setPartialProfile(true);
622  break;
623  case SecNameTable: {
624  FixedLengthMD5 =
626  bool UseMD5 = hasSecFlag(Entry, SecNameTableFlags::SecFlagMD5Name);
627  assert((!FixedLengthMD5 || UseMD5) &&
628  "If FixedLengthMD5 is true, UseMD5 has to be true");
631  if (std::error_code EC = readNameTableSec(UseMD5))
632  return EC;
633  break;
634  }
635  case SecLBRProfile:
636  if (std::error_code EC = readFuncProfiles())
637  return EC;
638  break;
639  case SecFuncOffsetTable:
640  if (std::error_code EC = readFuncOffsetTable())
641  return EC;
642  break;
643  case SecFuncMetadata: {
647  bool HasAttribute =
649  if (std::error_code EC = readFuncMetadata(HasAttribute))
650  return EC;
651  break;
652  }
654  if (std::error_code EC = readProfileSymbolList())
655  return EC;
656  break;
657  default:
658  if (std::error_code EC = readCustomSection(Entry))
659  return EC;
660  break;
661  }
663 }
664 
666  if (!M)
667  return false;
668  FuncsToUse.clear();
669  for (auto &F : *M)
670  FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
671  return true;
672 }
673 
675  // If there are more than one FuncOffsetTable, the profile read associated
676  // with previous FuncOffsetTable has to be done before next FuncOffsetTable
677  // is read.
678  FuncOffsetTable.clear();
679 
680  auto Size = readNumber<uint64_t>();
681  if (std::error_code EC = Size.getError())
682  return EC;
683 
684  FuncOffsetTable.reserve(*Size);
685  for (uint32_t I = 0; I < *Size; ++I) {
686  auto FName(readStringFromTable());
687  if (std::error_code EC = FName.getError())
688  return EC;
689 
690  auto Offset = readNumber<uint64_t>();
691  if (std::error_code EC = Offset.getError())
692  return EC;
693 
694  FuncOffsetTable[*FName] = *Offset;
695  }
697 }
698 
700  // Collect functions used by current module if the Reader has been
701  // given a module.
702  // collectFuncsFromModule uses FunctionSamples::getCanonicalFnName
703  // which will query FunctionSamples::HasUniqSuffix, so it has to be
704  // called after FunctionSamples::HasUniqSuffix is set, i.e. after
705  // NameTable section is read.
706  bool LoadFuncsToBeUsed = collectFuncsFromModule();
707 
708  // When LoadFuncsToBeUsed is false, load all the function profiles.
709  const uint8_t *Start = Data;
710  if (!LoadFuncsToBeUsed) {
711  while (Data < End) {
712  if (std::error_code EC = readFuncProfile(Data))
713  return EC;
714  }
715  assert(Data == End && "More data is read than expected");
716  } else {
717  // Load function profiles on demand.
718  if (Remapper) {
719  for (auto Name : FuncsToUse) {
720  Remapper->insert(Name);
721  }
722  }
723 
724  if (useMD5()) {
725  for (auto Name : FuncsToUse) {
726  auto GUID = std::to_string(MD5Hash(Name));
727  auto iter = FuncOffsetTable.find(StringRef(GUID));
728  if (iter == FuncOffsetTable.end())
729  continue;
730  const uint8_t *FuncProfileAddr = Start + iter->second;
731  assert(FuncProfileAddr < End && "out of LBRProfile section");
732  if (std::error_code EC = readFuncProfile(FuncProfileAddr))
733  return EC;
734  }
735  } else if (FunctionSamples::ProfileIsCS) {
736  // Compute the ordered set of names, so we can
737  // get all context profiles under a subtree by
738  // iterating through the ordered names.
739  struct Comparer {
740  // Ignore the closing ']' when ordering context
741  bool operator()(const StringRef &L, const StringRef &R) const {
742  return L.substr(0, L.size() - 1) < R.substr(0, R.size() - 1);
743  }
744  };
745  std::set<StringRef, Comparer> OrderedNames;
746  for (auto Name : FuncOffsetTable) {
747  OrderedNames.insert(Name.first);
748  }
749 
750  // For each function in current module, load all
751  // context profiles for the function.
752  for (auto NameOffset : FuncOffsetTable) {
753  StringRef ContextName = NameOffset.first;
754  SampleContext FContext(ContextName);
755  auto FuncName = FContext.getNameWithoutContext();
756  if (!FuncsToUse.count(FuncName) &&
757  (!Remapper || !Remapper->exist(FuncName)))
758  continue;
759 
760  // For each context profile we need, try to load
761  // all context profile in the subtree. This can
762  // help profile guided importing for ThinLTO.
763  auto It = OrderedNames.find(ContextName);
764  while (It != OrderedNames.end() &&
765  It->startswith(ContextName.substr(0, ContextName.size() - 1))) {
766  const uint8_t *FuncProfileAddr = Start + FuncOffsetTable[*It];
767  assert(FuncProfileAddr < End && "out of LBRProfile section");
768  if (std::error_code EC = readFuncProfile(FuncProfileAddr))
769  return EC;
770  // Remove loaded context profile so we won't
771  // load it repeatedly.
772  It = OrderedNames.erase(It);
773  }
774  }
775  } else {
776  for (auto NameOffset : FuncOffsetTable) {
777  SampleContext FContext(NameOffset.first);
778  auto FuncName = FContext.getNameWithoutContext();
779  if (!FuncsToUse.count(FuncName) &&
780  (!Remapper || !Remapper->exist(FuncName)))
781  continue;
782  const uint8_t *FuncProfileAddr = Start + NameOffset.second;
783  assert(FuncProfileAddr < End && "out of LBRProfile section");
784  if (std::error_code EC = readFuncProfile(FuncProfileAddr))
785  return EC;
786  }
787  }
788  Data = End;
789  }
790  assert((CSProfileCount == 0 || CSProfileCount == Profiles.size()) &&
791  "Cannot have both context-sensitive and regular profile");
792  assert(ProfileIsCS == (CSProfileCount > 0) &&
793  "Section flag should be consistent with actual profile");
795 }
796 
798  if (!ProfSymList)
799  ProfSymList = std::make_unique<ProfileSymbolList>();
800 
801  if (std::error_code EC = ProfSymList->read(Data, End - Data))
802  return EC;
803 
804  Data = End;
806 }
807 
808 std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
809  const uint8_t *SecStart, const uint64_t SecSize,
810  const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
811  Data = SecStart;
812  End = SecStart + SecSize;
813  auto DecompressSize = readNumber<uint64_t>();
814  if (std::error_code EC = DecompressSize.getError())
815  return EC;
816  DecompressBufSize = *DecompressSize;
817 
818  auto CompressSize = readNumber<uint64_t>();
819  if (std::error_code EC = CompressSize.getError())
820  return EC;
821 
824 
825  StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
826  *CompressSize);
827  char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
828  size_t UCSize = DecompressBufSize;
829  llvm::Error E =
830  zlib::uncompress(CompressedStrings, Buffer, UCSize);
831  if (E)
833  DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
835 }
836 
838  const uint8_t *BufStart =
839  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
840 
841  for (auto &Entry : SecHdrTable) {
842  // Skip empty section.
843  if (!Entry.Size)
844  continue;
845 
846  // Skip sections without context when SkipFlatProf is true.
847  if (SkipFlatProf && hasSecFlag(Entry, SecCommonFlags::SecFlagFlat))
848  continue;
849 
850  const uint8_t *SecStart = BufStart + Entry.Offset;
851  uint64_t SecSize = Entry.Size;
852 
853  // If the section is compressed, decompress it into a buffer
854  // DecompressBuf before reading the actual data. The pointee of
855  // 'Data' will be changed to buffer hold by DecompressBuf
856  // temporarily when reading the actual data.
857  bool isCompressed = hasSecFlag(Entry, SecCommonFlags::SecFlagCompress);
858  if (isCompressed) {
859  const uint8_t *DecompressBuf;
860  uint64_t DecompressBufSize;
861  if (std::error_code EC = decompressSection(
862  SecStart, SecSize, DecompressBuf, DecompressBufSize))
863  return EC;
864  SecStart = DecompressBuf;
865  SecSize = DecompressBufSize;
866  }
867 
868  if (std::error_code EC = readOneSection(SecStart, SecSize, Entry))
869  return EC;
870  if (Data != SecStart + SecSize)
872 
873  // Change the pointee of 'Data' from DecompressBuf to original Buffer.
874  if (isCompressed) {
875  Data = BufStart + Entry.Offset;
876  End = BufStart + Buffer->getBufferSize();
877  }
878  }
879 
881 }
882 
884  // Collect functions used by current module if the Reader has been
885  // given a module.
886  bool LoadFuncsToBeUsed = collectFuncsFromModule();
888  std::vector<uint64_t> OffsetsToUse;
889  if (!LoadFuncsToBeUsed) {
890  // load all the function profiles.
891  for (auto FuncEntry : FuncOffsetTable) {
892  OffsetsToUse.push_back(FuncEntry.second);
893  }
894  } else {
895  // load function profiles on demand.
896  for (auto Name : FuncsToUse) {
897  auto GUID = std::to_string(MD5Hash(Name));
898  auto iter = FuncOffsetTable.find(StringRef(GUID));
899  if (iter == FuncOffsetTable.end())
900  continue;
901  OffsetsToUse.push_back(iter->second);
902  }
903  }
904 
905  for (auto Offset : OffsetsToUse) {
906  const uint8_t *SavedData = Data;
907  if (std::error_code EC = readFuncProfile(
908  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
909  Offset))
910  return EC;
911  Data = SavedData;
912  }
914 }
915 
916 std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
917  if (Magic == SPMagic())
920 }
921 
922 std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
923  if (Magic == SPMagic(SPF_Ext_Binary))
926 }
927 
928 std::error_code
929 SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
933 }
934 
936  auto Size = readNumber<uint32_t>();
937  if (std::error_code EC = Size.getError())
938  return EC;
939  NameTable.reserve(*Size + NameTable.size());
940  for (uint32_t I = 0; I < *Size; ++I) {
941  auto Name(readString());
942  if (std::error_code EC = Name.getError())
943  return EC;
944  NameTable.push_back(*Name);
945  }
946 
948 }
949 
951  auto Size = readNumber<uint64_t>();
952  if (std::error_code EC = Size.getError())
953  return EC;
954  MD5StringBuf = std::make_unique<std::vector<std::string>>();
955  MD5StringBuf->reserve(*Size);
956  if (FixedLengthMD5) {
957  // Preallocate and initialize NameTable so we can check whether a name
958  // index has been read before by checking whether the element in the
959  // NameTable is empty, meanwhile readStringIndex can do the boundary
960  // check using the size of NameTable.
961  NameTable.resize(*Size + NameTable.size());
962 
963  MD5NameMemStart = Data;
964  Data = Data + (*Size) * sizeof(uint64_t);
966  }
967  NameTable.reserve(*Size);
968  for (uint32_t I = 0; I < *Size; ++I) {
969  auto FID = readNumber<uint64_t>();
970  if (std::error_code EC = FID.getError())
971  return EC;
972  MD5StringBuf->push_back(std::to_string(*FID));
973  // NameTable is a vector of StringRef. Here it is pushing back a
974  // StringRef initialized with the last string in MD5stringBuf.
975  NameTable.push_back(MD5StringBuf->back());
976  }
978 }
979 
981  if (IsMD5)
982  return readMD5NameTable();
984 }
985 
986 std::error_code
988  while (Data < End) {
989  auto FName(readStringFromTable());
990  if (std::error_code EC = FName.getError())
991  return EC;
992 
993  SampleContext FContext(*FName);
994  bool ProfileInMap = Profiles.count(FContext);
995 
996  if (ProfileIsProbeBased) {
997  auto Checksum = readNumber<uint64_t>();
998  if (std::error_code EC = Checksum.getError())
999  return EC;
1000  if (ProfileInMap)
1001  Profiles[FContext].setFunctionHash(*Checksum);
1002  }
1003 
1004  if (ProfileHasAttribute) {
1005  auto Attributes = readNumber<uint32_t>();
1006  if (std::error_code EC = Attributes.getError())
1007  return EC;
1008  if (ProfileInMap)
1009  Profiles[FContext].getContext().setAllAttributes(*Attributes);
1010  }
1011  }
1012 
1013  assert(Data == End && "More data is read than expected");
1015 }
1016 
1017 std::error_code SampleProfileReaderCompactBinary::readNameTable() {
1018  auto Size = readNumber<uint64_t>();
1019  if (std::error_code EC = Size.getError())
1020  return EC;
1021  NameTable.reserve(*Size);
1022  for (uint32_t I = 0; I < *Size; ++I) {
1023  auto FID = readNumber<uint64_t>();
1024  if (std::error_code EC = FID.getError())
1025  return EC;
1026  NameTable.push_back(std::to_string(*FID));
1027  }
1029 }
1030 
1031 std::error_code
1033  SecHdrTableEntry Entry;
1034  auto Type = readUnencodedNumber<uint64_t>();
1035  if (std::error_code EC = Type.getError())
1036  return EC;
1037  Entry.Type = static_cast<SecType>(*Type);
1038 
1039  auto Flags = readUnencodedNumber<uint64_t>();
1040  if (std::error_code EC = Flags.getError())
1041  return EC;
1042  Entry.Flags = *Flags;
1043 
1044  auto Offset = readUnencodedNumber<uint64_t>();
1045  if (std::error_code EC = Offset.getError())
1046  return EC;
1047  Entry.Offset = *Offset;
1048 
1049  auto Size = readUnencodedNumber<uint64_t>();
1050  if (std::error_code EC = Size.getError())
1051  return EC;
1052  Entry.Size = *Size;
1053 
1054  Entry.LayoutIndex = Idx;
1055  SecHdrTable.push_back(std::move(Entry));
1057 }
1058 
1060  auto EntryNum = readUnencodedNumber<uint64_t>();
1061  if (std::error_code EC = EntryNum.getError())
1062  return EC;
1063 
1064  for (uint32_t i = 0; i < (*EntryNum); i++)
1065  if (std::error_code EC = readSecHdrTableEntry(i))
1066  return EC;
1067 
1069 }
1070 
1072  const uint8_t *BufStart =
1073  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1074  Data = BufStart;
1075  End = BufStart + Buffer->getBufferSize();
1076 
1077  if (std::error_code EC = readMagicIdent())
1078  return EC;
1079 
1080  if (std::error_code EC = readSecHdrTable())
1081  return EC;
1082 
1084 }
1085 
1087  uint64_t Size = 0;
1088  for (auto &Entry : SecHdrTable) {
1089  if (Entry.Type == Type)
1090  Size += Entry.Size;
1091  }
1092  return Size;
1093 }
1094 
1096  // Sections in SecHdrTable is not necessarily in the same order as
1097  // sections in the profile because section like FuncOffsetTable needs
1098  // to be written after section LBRProfile but needs to be read before
1099  // section LBRProfile, so we cannot simply use the last entry in
1100  // SecHdrTable to calculate the file size.
1101  uint64_t FileSize = 0;
1102  for (auto &Entry : SecHdrTable) {
1103  FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
1104  }
1105  return FileSize;
1106 }
1107 
1108 static std::string getSecFlagsStr(const SecHdrTableEntry &Entry) {
1109  std::string Flags;
1111  Flags.append("{compressed,");
1112  else
1113  Flags.append("{");
1114 
1116  Flags.append("flat,");
1117 
1118  switch (Entry.Type) {
1119  case SecNameTable:
1121  Flags.append("fixlenmd5,");
1123  Flags.append("md5,");
1125  Flags.append("uniq,");
1126  break;
1127  case SecProfSummary:
1129  Flags.append("partial,");
1131  Flags.append("context,");
1133  Flags.append("fs-discriminator,");
1134  break;
1135  default:
1136  break;
1137  }
1138  char &last = Flags.back();
1139  if (last == ',')
1140  last = '}';
1141  else
1142  Flags.append("}");
1143  return Flags;
1144 }
1145 
1147  uint64_t TotalSecsSize = 0;
1148  for (auto &Entry : SecHdrTable) {
1149  OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
1150  << ", Size: " << Entry.Size << ", Flags: " << getSecFlagsStr(Entry)
1151  << "\n";
1152  ;
1153  TotalSecsSize += Entry.Size;
1154  }
1155  uint64_t HeaderSize = SecHdrTable.front().Offset;
1156  assert(HeaderSize + TotalSecsSize == getFileSize() &&
1157  "Size of 'header + sections' doesn't match the total size of profile");
1158 
1159  OS << "Header Size: " << HeaderSize << "\n";
1160  OS << "Total Sections Size: " << TotalSecsSize << "\n";
1161  OS << "File Size: " << getFileSize() << "\n";
1162  return true;
1163 }
1164 
1166  // Read and check the magic identifier.
1167  auto Magic = readNumber<uint64_t>();
1168  if (std::error_code EC = Magic.getError())
1169  return EC;
1170  else if (std::error_code EC = verifySPMagic(*Magic))
1171  return EC;
1172 
1173  // Read the version number.
1174  auto Version = readNumber<uint64_t>();
1175  if (std::error_code EC = Version.getError())
1176  return EC;
1177  else if (*Version != SPVersion())
1179 
1181 }
1182 
1184  Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
1185  End = Data + Buffer->getBufferSize();
1186 
1187  if (std::error_code EC = readMagicIdent())
1188  return EC;
1189 
1190  if (std::error_code EC = readSummary())
1191  return EC;
1192 
1193  if (std::error_code EC = readNameTable())
1194  return EC;
1196 }
1197 
1198 std::error_code SampleProfileReaderCompactBinary::readHeader() {
1200  if (std::error_code EC = readFuncOffsetTable())
1201  return EC;
1203 }
1204 
1205 std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
1206  auto TableOffset = readUnencodedNumber<uint64_t>();
1207  if (std::error_code EC = TableOffset.getError())
1208  return EC;
1209 
1210  const uint8_t *SavedData = Data;
1211  const uint8_t *TableStart =
1212  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
1213  *TableOffset;
1214  Data = TableStart;
1215 
1216  auto Size = readNumber<uint64_t>();
1217  if (std::error_code EC = Size.getError())
1218  return EC;
1219 
1220  FuncOffsetTable.reserve(*Size);
1221  for (uint32_t I = 0; I < *Size; ++I) {
1222  auto FName(readStringFromTable());
1223  if (std::error_code EC = FName.getError())
1224  return EC;
1225 
1226  auto Offset = readNumber<uint64_t>();
1227  if (std::error_code EC = Offset.getError())
1228  return EC;
1229 
1230  FuncOffsetTable[*FName] = *Offset;
1231  }
1232  End = TableStart;
1233  Data = SavedData;
1235 }
1236 
1238  if (!M)
1239  return false;
1240  FuncsToUse.clear();
1241  for (auto &F : *M)
1242  FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
1243  return true;
1244 }
1245 
1246 std::error_code SampleProfileReaderBinary::readSummaryEntry(
1247  std::vector<ProfileSummaryEntry> &Entries) {
1248  auto Cutoff = readNumber<uint64_t>();
1249  if (std::error_code EC = Cutoff.getError())
1250  return EC;
1251 
1252  auto MinBlockCount = readNumber<uint64_t>();
1253  if (std::error_code EC = MinBlockCount.getError())
1254  return EC;
1255 
1256  auto NumBlocks = readNumber<uint64_t>();
1257  if (std::error_code EC = NumBlocks.getError())
1258  return EC;
1259 
1260  Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
1262 }
1263 
1265  auto TotalCount = readNumber<uint64_t>();
1266  if (std::error_code EC = TotalCount.getError())
1267  return EC;
1268 
1269  auto MaxBlockCount = readNumber<uint64_t>();
1270  if (std::error_code EC = MaxBlockCount.getError())
1271  return EC;
1272 
1273  auto MaxFunctionCount = readNumber<uint64_t>();
1274  if (std::error_code EC = MaxFunctionCount.getError())
1275  return EC;
1276 
1277  auto NumBlocks = readNumber<uint64_t>();
1278  if (std::error_code EC = NumBlocks.getError())
1279  return EC;
1280 
1281  auto NumFunctions = readNumber<uint64_t>();
1282  if (std::error_code EC = NumFunctions.getError())
1283  return EC;
1284 
1285  auto NumSummaryEntries = readNumber<uint64_t>();
1286  if (std::error_code EC = NumSummaryEntries.getError())
1287  return EC;
1288 
1289  std::vector<ProfileSummaryEntry> Entries;
1290  for (unsigned i = 0; i < *NumSummaryEntries; i++) {
1291  std::error_code EC = readSummaryEntry(Entries);
1292  if (EC != sampleprof_error::success)
1293  return EC;
1294  }
1295  Summary = std::make_unique<ProfileSummary>(
1296  ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
1297  *MaxFunctionCount, *NumBlocks, *NumFunctions);
1298 
1300 }
1301 
1303  const uint8_t *Data =
1304  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1305  uint64_t Magic = decodeULEB128(Data);
1306  return Magic == SPMagic();
1307 }
1308 
1310  const uint8_t *Data =
1311  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1312  uint64_t Magic = decodeULEB128(Data);
1313  return Magic == SPMagic(SPF_Ext_Binary);
1314 }
1315 
1317  const uint8_t *Data =
1318  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
1319  uint64_t Magic = decodeULEB128(Data);
1320  return Magic == SPMagic(SPF_Compact_Binary);
1321 }
1322 
1324  uint32_t dummy;
1325  if (!GcovBuffer.readInt(dummy))
1328 }
1329 
1331  if (sizeof(T) <= sizeof(uint32_t)) {
1332  uint32_t Val;
1333  if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
1334  return static_cast<T>(Val);
1335  } else if (sizeof(T) <= sizeof(uint64_t)) {
1336  uint64_t Val;
1337  if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
1338  return static_cast<T>(Val);
1339  }
1340 
1341  std::error_code EC = sampleprof_error::malformed;
1342  reportError(0, EC.message());
1343  return EC;
1344 }
1345 
1347  StringRef Str;
1348  if (!GcovBuffer.readString(Str))
1350  return Str;
1351 }
1352 
1354  // Read the magic identifier.
1355  if (!GcovBuffer.readGCDAFormat())
1357 
1358  // Read the version number. Note - the GCC reader does not validate this
1359  // version, but the profile creator generates v704.
1360  GCOV::GCOVVersion version;
1361  if (!GcovBuffer.readGCOVVersion(version))
1363 
1364  if (version != GCOV::V407)
1366 
1367  // Skip the empty integer.
1368  if (std::error_code EC = skipNextWord())
1369  return EC;
1370 
1372 }
1373 
1375  uint32_t Tag;
1376  if (!GcovBuffer.readInt(Tag))
1378 
1379  if (Tag != Expected)
1381 
1382  if (std::error_code EC = skipNextWord())
1383  return EC;
1384 
1386 }
1387 
1389  if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
1390  return EC;
1391 
1392  uint32_t Size;
1393  if (!GcovBuffer.readInt(Size))
1395 
1396  for (uint32_t I = 0; I < Size; ++I) {
1397  StringRef Str;
1398  if (!GcovBuffer.readString(Str))
1400  Names.push_back(std::string(Str));
1401  }
1402 
1404 }
1405 
1407  if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
1408  return EC;
1409 
1410  uint32_t NumFunctions;
1411  if (!GcovBuffer.readInt(NumFunctions))
1413 
1414  InlineCallStack Stack;
1415  for (uint32_t I = 0; I < NumFunctions; ++I)
1416  if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
1417  return EC;
1418 
1419  computeSummary();
1421 }
1422 
1424  const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
1425  uint64_t HeadCount = 0;
1426  if (InlineStack.size() == 0)
1427  if (!GcovBuffer.readInt64(HeadCount))
1429 
1430  uint32_t NameIdx;
1431  if (!GcovBuffer.readInt(NameIdx))
1433 
1434  StringRef Name(Names[NameIdx]);
1435 
1436  uint32_t NumPosCounts;
1437  if (!GcovBuffer.readInt(NumPosCounts))
1439 
1440  uint32_t NumCallsites;
1441  if (!GcovBuffer.readInt(NumCallsites))
1443 
1444  FunctionSamples *FProfile = nullptr;
1445  if (InlineStack.size() == 0) {
1446  // If this is a top function that we have already processed, do not
1447  // update its profile again. This happens in the presence of
1448  // function aliases. Since these aliases share the same function
1449  // body, there will be identical replicated profiles for the
1450  // original function. In this case, we simply not bother updating
1451  // the profile of the original function.
1452  FProfile = &Profiles[Name];
1453  FProfile->addHeadSamples(HeadCount);
1454  if (FProfile->getTotalSamples() > 0)
1455  Update = false;
1456  } else {
1457  // Otherwise, we are reading an inlined instance. The top of the
1458  // inline stack contains the profile of the caller. Insert this
1459  // callee in the caller's CallsiteMap.
1460  FunctionSamples *CallerProfile = InlineStack.front();
1461  uint32_t LineOffset = Offset >> 16;
1462  uint32_t Discriminator = Offset & 0xffff;
1463  FProfile = &CallerProfile->functionSamplesAt(
1464  LineLocation(LineOffset, Discriminator))[std::string(Name)];
1465  }
1466  FProfile->setName(Name);
1467 
1468  for (uint32_t I = 0; I < NumPosCounts; ++I) {
1469  uint32_t Offset;
1470  if (!GcovBuffer.readInt(Offset))
1472 
1473  uint32_t NumTargets;
1474  if (!GcovBuffer.readInt(NumTargets))
1476 
1477  uint64_t Count;
1478  if (!GcovBuffer.readInt64(Count))
1480 
1481  // The line location is encoded in the offset as:
1482  // high 16 bits: line offset to the start of the function.
1483  // low 16 bits: discriminator.
1484  uint32_t LineOffset = Offset >> 16;
1485  uint32_t Discriminator = Offset & 0xffff;
1486 
1487  InlineCallStack NewStack;
1488  NewStack.push_back(FProfile);
1489  llvm::append_range(NewStack, InlineStack);
1490  if (Update) {
1491  // Walk up the inline stack, adding the samples on this line to
1492  // the total sample count of the callers in the chain.
1493  for (auto CallerProfile : NewStack)
1494  CallerProfile->addTotalSamples(Count);
1495 
1496  // Update the body samples for the current profile.
1497  FProfile->addBodySamples(LineOffset, Discriminator, Count);
1498  }
1499 
1500  // Process the list of functions called at an indirect call site.
1501  // These are all the targets that a function pointer (or virtual
1502  // function) resolved at runtime.
1503  for (uint32_t J = 0; J < NumTargets; J++) {
1504  uint32_t HistVal;
1505  if (!GcovBuffer.readInt(HistVal))
1507 
1508  if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
1510 
1511  uint64_t TargetIdx;
1512  if (!GcovBuffer.readInt64(TargetIdx))
1514  StringRef TargetName(Names[TargetIdx]);
1515 
1516  uint64_t TargetCount;
1517  if (!GcovBuffer.readInt64(TargetCount))
1519 
1520  if (Update)
1521  FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1522  TargetName, TargetCount);
1523  }
1524  }
1525 
1526  // Process all the inlined callers into the current function. These
1527  // are all the callsites that were inlined into this function.
1528  for (uint32_t I = 0; I < NumCallsites; I++) {
1529  // The offset is encoded as:
1530  // high 16 bits: line offset to the start of the function.
1531  // low 16 bits: discriminator.
1532  uint32_t Offset;
1533  if (!GcovBuffer.readInt(Offset))
1535  InlineCallStack NewStack;
1536  NewStack.push_back(FProfile);
1537  llvm::append_range(NewStack, InlineStack);
1538  if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
1539  return EC;
1540  }
1541 
1543 }
1544 
1545 /// Read a GCC AutoFDO profile.
1546 ///
1547 /// This format is generated by the Linux Perf conversion tool at
1548 /// https://github.com/google/autofdo.
1550  assert(!ProfileIsFSDisciminator && "Gcc profiles not support FSDisciminator");
1551  // Read the string table.
1552  if (std::error_code EC = readNameTable())
1553  return EC;
1554 
1555  // Read the source profile.
1556  if (std::error_code EC = readFunctionProfiles())
1557  return EC;
1558 
1560 }
1561 
1563  StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
1564  return Magic == "adcg*704";
1565 }
1566 
1568  // If the reader uses MD5 to represent string, we can't remap it because
1569  // we don't know what the original function names were.
1570  if (Reader.useMD5()) {
1572  Reader.getBuffer()->getBufferIdentifier(),
1573  "Profile data remapping cannot be applied to profile data "
1574  "in compact format (original mangled names are not available).",
1575  DS_Warning));
1576  return;
1577  }
1578 
1579  // CSSPGO-TODO: Remapper is not yet supported.
1580  // We will need to remap the entire context string.
1581  assert(Remappings && "should be initialized while creating remapper");
1582  for (auto &Sample : Reader.getProfiles()) {
1583  DenseSet<StringRef> NamesInSample;
1584  Sample.second.findAllNames(NamesInSample);
1585  for (auto &Name : NamesInSample)
1586  if (auto Key = Remappings->insert(Name))
1587  NameMap.insert({Key, Name});
1588  }
1589 
1590  RemappingApplied = true;
1591 }
1592 
1595  if (auto Key = Remappings->lookup(Fname))
1596  return NameMap.lookup(Key);
1597  return None;
1598 }
1599 
1600 /// Prepare a memory buffer for the contents of \p Filename.
1601 ///
1602 /// \returns an error code indicating the status of the buffer.
1604 setupMemoryBuffer(const Twine &Filename) {
1605  auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
1606  if (std::error_code EC = BufferOrErr.getError())
1607  return EC;
1608  auto Buffer = std::move(BufferOrErr.get());
1609 
1610  // Sanity check the file.
1611  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1613 
1614  return std::move(Buffer);
1615 }
1616 
1617 /// Create a sample profile reader based on the format of the input file.
1618 ///
1619 /// \param Filename The file to open.
1620 ///
1621 /// \param C The LLVM context to use to emit diagnostics.
1622 ///
1623 /// \param P The FSDiscriminatorPass.
1624 ///
1625 /// \param RemapFilename The file used for profile remapping.
1626 ///
1627 /// \returns an error code indicating the status of the created reader.
1629 SampleProfileReader::create(const std::string Filename, LLVMContext &C,
1631  const std::string RemapFilename) {
1632  auto BufferOrError = setupMemoryBuffer(Filename);
1633  if (std::error_code EC = BufferOrError.getError())
1634  return EC;
1635  return create(BufferOrError.get(), C, P, RemapFilename);
1636 }
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 Filename The file to open.
1642 ///
1643 /// \param Reader The profile reader the remapper is going to be applied to.
1644 ///
1645 /// \param C The LLVM context to use to emit diagnostics.
1646 ///
1647 /// \returns an error code indicating the status of the created reader.
1650  SampleProfileReader &Reader,
1651  LLVMContext &C) {
1652  auto BufferOrError = setupMemoryBuffer(Filename);
1653  if (std::error_code EC = BufferOrError.getError())
1654  return EC;
1655  return create(BufferOrError.get(), Reader, C);
1656 }
1657 
1658 /// Create a sample profile remapper from the given input, to remap the
1659 /// function names in the given profile data.
1660 ///
1661 /// \param B The memory buffer to create the reader from (assumes ownership).
1662 ///
1663 /// \param C The LLVM context to use to emit diagnostics.
1664 ///
1665 /// \param Reader The profile reader the remapper is going to be applied to.
1666 ///
1667 /// \returns an error code indicating the status of the created reader.
1669 SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
1670  SampleProfileReader &Reader,
1671  LLVMContext &C) {
1672  auto Remappings = std::make_unique<SymbolRemappingReader>();
1673  if (Error E = Remappings->read(*B.get())) {
1675  std::move(E), [&](const SymbolRemappingParseError &ParseError) {
1676  C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
1677  ParseError.getLineNum(),
1678  ParseError.getMessage()));
1679  });
1681  }
1682 
1683  return std::make_unique<SampleProfileReaderItaniumRemapper>(
1684  std::move(B), std::move(Remappings), Reader);
1685 }
1686 
1687 /// Create a sample profile reader based on the format of the input data.
1688 ///
1689 /// \param B The memory buffer to create the reader from (assumes ownership).
1690 ///
1691 /// \param C The LLVM context to use to emit diagnostics.
1692 ///
1693 /// \param P The FSDiscriminatorPass.
1694 ///
1695 /// \param RemapFilename The file used for profile remapping.
1696 ///
1697 /// \returns an error code indicating the status of the created reader.
1699 SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
1701  const std::string RemapFilename) {
1702  std::unique_ptr<SampleProfileReader> Reader;
1704  Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1706  Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1708  Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
1710  Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
1712  Reader.reset(new SampleProfileReaderText(std::move(B), C));
1713  else
1715 
1716  if (!RemapFilename.empty()) {
1717  auto ReaderOrErr =
1718  SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
1719  if (std::error_code EC = ReaderOrErr.getError()) {
1720  std::string Msg = "Could not create remapper: " + EC.message();
1721  C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
1722  return EC;
1723  }
1724  Reader->Remapper = std::move(ReaderOrErr.get());
1725  }
1726 
1727  FunctionSamples::Format = Reader->getFormat();
1728  if (std::error_code EC = Reader->readHeader()) {
1729  return EC;
1730  }
1731 
1732  Reader->setDiscriminatorMaskedBitFrom(P);
1733 
1734  return std::move(Reader);
1735 }
1736 
1737 // For text and GCC file formats, we compute the summary after reading the
1738 // profile. Binary format has the profile summary in its header.
1741  Summary = Builder.computeSummaryForProfiles(Profiles);
1742 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::sampleprof::SampleProfileReader::ProfileIsFS
bool ProfileIsFS
Whether the function profiles use FS discriminators.
Definition: SampleProfReader.h:530
i
i
Definition: README.txt:29
MemoryBuffer.h
llvm::sampleprof::SampleProfileReader::dump
void dump(raw_ostream &OS=dbgs())
Print all the profiles on stream OS.
Definition: SampleProfReader.cpp:68
llvm::sampleprof_error::uncompress_failed
@ uncompress_failed
llvm::sampleprof::SampleProfileReaderText
Definition: SampleProfReader.h:545
llvm::StringRef::back
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:168
llvm::sampleprof::SampleProfileReaderBinary::readNameTable
virtual std::error_code readNameTable()
Read the whole name table.
Definition: SampleProfReader.cpp:935
Compression.h
llvm::sampleprof::SampleProfileReader::CSProfileCount
uint32_t CSProfileCount
Number of context-sensitive profiles.
Definition: SampleProfReader.h:527
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
llvm::sampleprof::SampleProfileReaderText::readImpl
std::error_code readImpl() override
Read sample profiles from the associated file.
Definition: SampleProfReader.cpp:240
llvm::sampleprof::SampleProfileReaderItaniumRemapper::create
static ErrorOr< std::unique_ptr< SampleProfileReaderItaniumRemapper > > create(const std::string Filename, SampleProfileReader &Reader, LLVMContext &C)
Create a remapper from the given remapping file.
Definition: SampleProfReader.cpp:1649
llvm
Definition: AllocatorList.h:23
llvm::sampleprof::SecFuncMetadataFlags::SecFlagHasAttribute
@ SecFlagHasAttribute
llvm::sampleprof::SampleProfileReader::M
const Module * M
The current module being compiled if SampleProfileReader is used by compiler.
Definition: SampleProfReader.h:538
llvm::sampleprof::SecProfSummaryFlags::SecFlagFSDiscriminator
@ SecFlagFSDiscriminator
SecFlagFSDiscriminator means this profile uses flow-sensitive discriminators.
llvm::sampleprof::SampleProfileReader::Remapper
std::unique_ptr< SampleProfileReaderItaniumRemapper > Remapper
Definition: SampleProfReader.h:518
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::sampleprof::FunctionSamples::ProfileIsProbeBased
static bool ProfileIsProbeBased
Definition: SampleProf.h:881
llvm::sampleprof::FunctionSamples::ProfileIsCS
static bool ProfileIsCS
Definition: SampleProf.h:883
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
llvm::sampleprof::SampleProfileReaderGCC::readFunctionProfiles
std::error_code readFunctionProfiles()
Definition: SampleProfReader.cpp:1406
llvm::StringRef::rfind
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:375
llvm::sampleprof::SampleProfileReaderGCC::readSectionTag
std::error_code readSectionTag(uint32_t Expected)
Read the section tag and check that it's the same as Expected.
Definition: SampleProfReader.cpp:1374
ProfileSummary.h
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::sampleprof::SecHdrTableEntry
Definition: SampleProf.h:152
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:125
llvm::sampleprof::SecNameTableFlags::SecFlagFixedLengthMD5
@ SecFlagFixedLengthMD5
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:60
llvm::sampleprof::SampleProfileReaderBinary::readProfile
std::error_code readProfile(FunctionSamples &FProfile)
Read the contents of the given profile instance.
Definition: SampleProfReader.cpp:490
llvm::StringRef::find
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:315
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1167
llvm::zlib::uncompress
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:63
llvm::sampleprof::SecProfSummaryFlags::SecFlagFullContext
@ SecFlagFullContext
SecFlagContext means this is context-sensitive profile for CSSPGO.
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readStringFromTable
virtual ErrorOr< StringRef > readStringFromTable() override
Read a string indirectly via the name table.
Definition: SampleProfReader.cpp:453
llvm::GCOV::GCOVVersion
GCOVVersion
Definition: GCOV.h:45
llvm::sampleprof::SampleProfileReaderGCC::hasFormat
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Definition: SampleProfReader.cpp:1562
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readNameTableSec
std::error_code readNameTableSec(bool IsMD5)
Definition: SampleProfReader.cpp:980
llvm::SymbolRemappingParseError::getMessage
StringRef getMessage() const
Definition: SymbolRemappingReader.h:83
llvm::sampleprof_error::unsupported_version
@ unsupported_version
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::sampleprof::hasSecFlag
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:248
llvm::sampleprof::SampleProfileReaderItaniumRemapper::applyRemapping
void applyRemapping(LLVMContext &Ctx)
Apply remappings to the profile read by Reader.
Definition: SampleProfReader.cpp:1567
llvm::sampleprof::SampleContext::getNameWithoutContext
StringRef getNameWithoutContext() const
Definition: SampleProf.h:466
llvm::Depth
@ Depth
Definition: SIMachineScheduler.h:34
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
DenseMap.h
llvm::sampleprof::SampleProfileReaderGCC::readNameTable
std::error_code readNameTable()
Definition: SampleProfReader.cpp:1388
llvm::sampleprof::SecFuncOffsetTable
@ SecFuncOffsetTable
Definition: SampleProf.h:123
llvm::AMDGPU::Exp::Target
Target
Definition: SIDefines.h:732
llvm::Optional
Definition: APInt.h:33
llvm::sampleprof::SecCommonFlags::SecFlagCompress
@ SecFlagCompress
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readMD5NameTable
std::error_code readMD5NameTable()
Definition: SampleProfReader.cpp:950
T
#define T
Definition: Mips16ISelLowering.cpp:341
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readSecHdrTableEntry
std::error_code readSecHdrTableEntry(uint32_t Idx)
Definition: SampleProfReader.cpp:1032
llvm::sampleprof::SecNameTableFlags::SecFlagMD5Name
@ SecFlagMD5Name
llvm::sampleprof::SampleProfileReader::collectFuncsFromModule
virtual bool collectFuncsFromModule()
Collect functions with definitions in Module M.
Definition: SampleProfReader.h:390
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
STLExtras.h
llvm::detail::DenseSetImpl::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
ProfileCommon.h
isOffsetLegal
static bool isOffsetLegal(unsigned L)
Returns true if line offset L is legal (only has 16 bits).
Definition: SampleProfReader.cpp:95
llvm::sampleprof::SampleProfileReader::Summary
std::unique_ptr< ProfileSummary > Summary
Profile summary information.
Definition: SampleProfReader.h:507
llvm::sampleprof::SampleProfileReaderGCC::readOneFunctionProfile
std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack, bool Update, uint32_t Offset)
Definition: SampleProfReader.cpp:1423
llvm::SampleProfileSummaryBuilder
Definition: ProfileCommon.h:88
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::sampleprof::SampleProfileReaderGCC::readNumber
ErrorOr< T > readNumber()
Definition: SampleProfReader.cpp:1330
ProfileIsFSDisciminator
static cl::opt< bool > ProfileIsFSDisciminator("profile-isfs", cl::Hidden, cl::init(false), cl::desc("Profile uses flow senstive discriminators"))
llvm::sampleprof::SampleProfileReaderBinary::readStringFromTable
virtual ErrorOr< StringRef > readStringFromTable()
Read a string indirectly via the name table.
Definition: SampleProfReader.cpp:445
result
It looks like we only need to define PPCfmarto for these because according to these instructions perform RTO on fma s result
Definition: README_P9.txt:256
llvm::sampleprof::FSDiscriminatorPass
FSDiscriminatorPass
Definition: Discriminator.h:57
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:609
llvm::sampleprof::FunctionSamples::HasUniqSuffix
static bool HasUniqSuffix
Whether the profile contains any ".__uniq." suffix in a name.
Definition: SampleProf.h:895
CommandLine.h
llvm::decodeULEB128
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition: LEB128.h:128
llvm::sampleprof::SampleProfileReader::getDiscriminatorMask
uint32_t getDiscriminatorMask() const
Get the bitmask the discriminators: For FS profiles, return the bit mask for this pass.
Definition: SampleProfReader.h:363
llvm::sampleprof::SecFuncMetadataFlags::SecFlagIsProbeBased
@ SecFlagIsProbeBased
llvm::StringRef::find_last_of
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:436
llvm::sampleprof::SampleProfileReaderBinary::NameTable
std::vector< StringRef > NameTable
Function name table.
Definition: SampleProfReader.h:625
llvm::SymbolRemappingParseError::getLineNum
int64_t getLineNum() const
Definition: SymbolRemappingReader.h:82
MD5.h
llvm::sampleprof::SampleProfileReaderCompactBinary::readImpl
std::error_code readImpl() override
Read samples only for functions to use.
Definition: SampleProfReader.cpp:883
llvm::sampleprof::FunctionSamples::getTotalSamples
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:650
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DS_Warning
@ DS_Warning
Definition: DiagnosticInfo.h:46
llvm::sampleprof::SampleProfileReaderRawBinary::hasFormat
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Definition: SampleProfReader.cpp:1302
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::sampleprof::SampleProfileReaderBinary::readFuncProfile
std::error_code readFuncProfile(const uint8_t *Start)
Read the next function profile instance.
Definition: SampleProfReader.cpp:573
llvm::GCOV::V407
@ V407
Definition: GCOV.h:45
llvm::sampleprof::SecProfSummaryFlags::SecFlagPartial
@ SecFlagPartial
SecFlagPartial means the profile is for common/shared code.
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
SampleProf.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::sampleprof::SampleProfileReaderItaniumRemapper::lookUpNameInProfile
Optional< StringRef > lookUpNameInProfile(StringRef FunctionName)
Return the equivalent name in the profile for FunctionName if it exists.
Definition: SampleProfReader.cpp:1594
llvm::sampleprof::FunctionSamples::Format
static SampleProfileFormat Format
Definition: SampleProf.h:889
llvm::sampleprof::SampleProfileReaderBinary::readString
ErrorOr< StringRef > readString()
Read a string from the profile.
Definition: SampleProfReader.cpp:406
llvm::sampleprof::SPMagic
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:96
LineIterator.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
getSecFlagsStr
static std::string getSecFlagsStr(const SecHdrTableEntry &Entry)
Definition: SampleProfReader.cpp:1108
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:991
llvm::sampleprof::SampleProfileReaderText::hasFormat
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Definition: SampleProfReader.cpp:369
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::sampleprof::SampleContext
Definition: SampleProf.h:408
llvm::None
const NoneType None
Definition: None.h:23
llvm::StringRef::getAsInteger
std::enable_if_t< std::numeric_limits< T >::is_signed, bool > getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:508
parseMetadata
static bool parseMetadata(const StringRef &Input, uint64_t &FunctionHash, uint32_t &Attributes)
Parse Input that contains metadata.
Definition: SampleProfReader.cpp:104
llvm::sampleprof::FunctionSamples::ProfileIsFS
static bool ProfileIsFS
If this profile uses flow sensitive discriminators.
Definition: SampleProf.h:898
llvm::StringRef::trim
LLVM_NODISCARD StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition: StringRef.h:848
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::cl::opt< bool >
llvm::sampleprof::SPF_Compact_Binary
@ SPF_Compact_Binary
Definition: SampleProf.h:90
llvm::sampleprof::SecProfSummary
@ SecProfSummary
Definition: SampleProf.h:120
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1565
llvm::sampleprof::FunctionSamples::addHeadSamples
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:550
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readSecHdrTable
std::error_code readSecHdrTable()
Definition: SampleProfReader.cpp:1059
setupMemoryBuffer
static ErrorOr< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename)
Prepare a memory buffer for the contents of Filename.
Definition: SampleProfReader.cpp:1604
llvm::sampleprof::SampleProfileReaderExtBinaryBase::getSectionSize
uint64_t getSectionSize(SecType Type)
Get the total size of all Type sections.
Definition: SampleProfReader.cpp:1086
llvm::sampleprof::SampleProfileReaderGCC
Definition: SampleProfReader.h:818
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:533
LEB128.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::sampleprof::SPF_Ext_Binary
@ SPF_Ext_Binary
Definition: SampleProf.h:92
llvm::DenseMap
Definition: DenseMap.h:714
llvm::sampleprof::FunctionSamples::functionSamplesAt
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
Definition: SampleProf.h:624
ErrorOr.h
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
llvm::sampleprof::SecLBRProfile
@ SecLBRProfile
Definition: SampleProf.h:127
I
#define I(x, y, z)
Definition: MD5.cpp:59
SampleProfReader.h
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
llvm::sampleprof::FunctionSamples::setContext
void setContext(const SampleContext &FContext)
Definition: SampleProf.h:887
Magic
const char Magic[]
Definition: Archive.cpp:41
llvm::sampleprof::FunctionSamples::addCalledTargetSamples
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:564
llvm::sampleprof::SampleContext::setAllAttributes
void setAllAttributes(uint32_t A)
Definition: SampleProf.h:460
llvm::sampleprof_error::malformed
@ malformed
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readFuncMetadata
std::error_code readFuncMetadata(bool ProfileHasAttribute)
Definition: SampleProfReader.cpp:987
llvm::sampleprof::SampleProfileReaderGCC::readString
ErrorOr< StringRef > readString()
Definition: SampleProfReader.cpp:1346
llvm::StringRef::find_first_not_of
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:244
llvm::sampleprof::SampleProfileReader::create
static ErrorOr< std::unique_ptr< SampleProfileReader > > create(const std::string Filename, LLVMContext &C, FSDiscriminatorPass P=FSDiscriminatorPass::Base, const std::string RemapFilename="")
Create a sample profile reader appropriate to the file format.
Definition: SampleProfReader.cpp:1629
llvm::zlib::isAvailable
bool isAvailable()
Definition: Compression.cpp:47
llvm::sampleprof::LineLocation
Represents the relative location of an instruction.
Definition: SampleProf.h:264
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:651
llvm::DiagnosticInfoSampleProfile
Diagnostic information for the sample profiler.
Definition: DiagnosticInfo.h:285
llvm::sampleprof::SampleProfileReaderExtBinary
Definition: SampleProfReader.h:751
llvm::sampleprof::SampleProfileReaderBinary::readMagicIdent
std::error_code readMagicIdent()
Read the contents of Magic number and Version number.
Definition: SampleProfReader.cpp:1165
llvm::sampleprof::SampleProfileReaderBinary::End
const uint8_t * End
Points to the end of the buffer.
Definition: SampleProfReader.h:622
llvm::sampleprof::SampleProfileReaderCompactBinary
Definition: SampleProfReader.h:768
llvm::MergeResult
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
Definition: SampleProf.h:65
isDigit
static bool isDigit(const char C)
Definition: RustDemangle.cpp:81
LineType::CallSiteProfile
@ CallSiteProfile
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readImpl
std::error_code readImpl() override
Read sample profiles in extensible format from the associated file.
Definition: SampleProfReader.cpp:837
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::sampleprof_error::unrecognized_format
@ unrecognized_format
uint32_t
llvm::sampleprof::FunctionSamples::addBodySamples
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:558
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1672
llvm::sampleprof::SampleProfileReader::reportError
void reportError(int64_t LineNumber, const Twine &Msg) const
Report a parse error message.
Definition: SampleProfReader.h:435
llvm::sampleprof::SampleProfileReader::computeSummary
void computeSummary()
Compute summary for this profile.
Definition: SampleProfReader.cpp:1739
llvm::sampleprof::getSecName
static std::string getSecName(SecType Type)
Definition: SampleProf.h:130
llvm::sampleprof::SampleProfileReaderRawBinary
Definition: SampleProfReader.h:635
ParseHead
static bool ParseHead(const StringRef &Input, StringRef &FName, uint64_t &NumSamples, uint64_t &NumHeadSamples)
Parse Input as function head.
Definition: SampleProfReader.cpp:80
llvm::LLVMContext::diagnose
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Definition: LLVMContext.cpp:228
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::sampleprof::SampleProfileReaderGCC::skipNextWord
std::error_code skipNextWord()
Definition: SampleProfReader.cpp:1323
llvm::sampleprof::SPVersion
static uint64_t SPVersion()
Definition: SampleProf.h:113
llvm::sampleprof::SampleProfileReaderBinary::readSummary
std::error_code readSummary()
Read profile summary.
Definition: SampleProfReader.cpp:1264
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::sampleprof::SampleProfileReaderGCC::readHeader
std::error_code readHeader() override
Read and validate the file header.
Definition: SampleProfReader.cpp:1353
llvm::BTF::HeaderSize
@ HeaderSize
Definition: BTF.h:58
llvm::sampleprof::SecProfileSymbolList
@ SecProfileSymbolList
Definition: SampleProf.h:122
llvm::sampleprof::FunctionSamples::addTotalSamples
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:540
llvm::sampleprof::SecType
SecType
Definition: SampleProf.h:118
LineType
LineType
Definition: SampleProfReader.cpp:119
llvm::sampleprof::SampleProfileReader
Sample-based profile reader.
Definition: SampleProfReader.h:343
llvm::sampleprof::SampleProfileReaderExtBinaryBase::getFileSize
uint64_t getFileSize()
Get the total size of header and all sections.
Definition: SampleProfReader.cpp:1095
llvm::sampleprof_error
sampleprof_error
Definition: SampleProf.h:42
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:584
llvm::sampleprof_error::zlib_unavailable
@ zlib_unavailable
llvm::sampleprof::SampleContext::hasContext
bool hasContext() const
Definition: SampleProf.h:464
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readHeader
virtual std::error_code readHeader() override
Read and validate the file header.
Definition: SampleProfReader.cpp:1071
llvm::sampleprof_error::too_large
@ too_large
llvm::line_iterator::is_at_eof
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
llvm::sampleprof::HIST_TYPE_INDIR_CALL_TOPN
@ HIST_TYPE_INDIR_CALL_TOPN
Definition: SampleProfReader.h:815
llvm::sampleprof::FunctionSamples::getContext
SampleContext & getContext() const
Definition: SampleProf.h:885
llvm::sampleprof_error::success
@ success
llvm::sampleprof::SecNameTableFlags::SecFlagUniqSuffix
@ SecFlagUniqSuffix
llvm::sampleprof::SampleProfileReaderBinary::readUnencodedNumber
ErrorOr< T > readUnencodedNumber()
Read a numeric value of type T from the profile.
Definition: SampleProfReader.cpp:420
llvm::line_iterator::line_number
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
llvm::sampleprof::FunctionSamples::getCanonicalFnName
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes.
Definition: SampleProf.h:793
llvm::sampleprof::SampleProfileReaderBinary::readNumber
ErrorOr< T > readNumber()
Read a numeric value of type T from the profile.
Definition: SampleProfReader.cpp:385
llvm::sampleprof::SampleProfileReaderBinary::readImpl
std::error_code readImpl() override
Read sample profiles from the associated file.
Definition: SampleProfReader.cpp:598
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readFuncProfiles
std::error_code readFuncProfiles()
Definition: SampleProfReader.cpp:699
llvm::sampleprof::SampleProfileReader::Ctx
LLVMContext & Ctx
LLVM context used to emit diagnostics.
Definition: SampleProfReader.h:501
llvm::sampleprof::SampleProfileReader::dumpFunctionProfile
void dumpFunctionProfile(StringRef FName, raw_ostream &OS=dbgs())
Print the profile for FName on stream OS.
Definition: SampleProfReader.cpp:62
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:146
llvm::sampleprof::SecCommonFlags::SecFlagFlat
@ SecFlagFlat
llvm::sampleprof::SampleProfileReaderCompactBinary::collectFuncsFromModule
bool collectFuncsFromModule() override
Collect functions with definitions in Module M.
Definition: SampleProfReader.cpp:1237
LineType::Metadata
@ Metadata
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::sampleprof::SampleProfileReader::useMD5
virtual bool useMD5()
Return whether names in the profile are all MD5 numbers.
Definition: SampleProfReader.h:480
llvm::to_string
std::string to_string(const T &Value)
Definition: ScopedPrinter.h:62
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readOneSection
virtual std::error_code readOneSection(const uint8_t *Start, uint64_t Size, const SecHdrTableEntry &Entry)
Definition: SampleProfReader.cpp:608
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readFuncOffsetTable
std::error_code readFuncOffsetTable()
Definition: SampleProfReader.cpp:674
llvm::SymbolRemappingParseError
Definition: SymbolRemappingReader.h:69
llvm::sampleprof::SecNameTable
@ SecNameTable
Definition: SampleProf.h:121
ParseLine
static bool ParseLine(const StringRef &Input, LineType &LineTy, uint32_t &Depth, uint64_t &NumSamples, uint32_t &LineOffset, uint32_t &Discriminator, StringRef &CalleeName, DenseMap< StringRef, uint64_t > &TargetCountMap, uint64_t &FunctionHash, uint32_t &Attributes)
Parse Input as line sample.
Definition: SampleProfReader.cpp:137
llvm::sampleprof::SampleProfileReaderExtBinaryBase::readProfileSymbolList
std::error_code readProfileSymbolList()
Definition: SampleProfReader.cpp:797
llvm::sampleprof::SampleProfileReaderCompactBinary::hasFormat
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Definition: SampleProfReader.cpp:1316
llvm::sampleprof::SecFuncMetadata
@ SecFuncMetadata
Definition: SampleProf.h:124
llvm::StringRef::find_first_of
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:410
llvm::sampleprof_error::truncated_name_table
@ truncated_name_table
llvm::sampleprof::SampleProfileReader::ProfileIsCS
bool ProfileIsCS
Whether function profiles are context-sensitive.
Definition: SampleProfReader.h:524
LineType::BodyProfile
@ BodyProfile
llvm::sampleprof::SampleProfileReaderExtBinaryBase::dumpSectionInfo
virtual bool dumpSectionInfo(raw_ostream &OS=dbgs()) override
Definition: SampleProfReader.cpp:1146
llvm::cl::desc
Definition: CommandLine.h:414
raw_ostream.h
llvm::sampleprof::SampleProfileReader::Profiles
StringMap< FunctionSamples > Profiles
Map every function to its associated profile.
Definition: SampleProfReader.h:498
llvm::sampleprof::SampleProfileReaderExtBinary::hasFormat
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Definition: SampleProfReader.cpp:1309
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::sampleprof::SampleProfileReaderGCC::readImpl
std::error_code readImpl() override
Read sample profiles from the associated file.
Definition: SampleProfReader.cpp:1549
llvm::sampleprof::SampleProfileReaderBinary::readStringIndex
ErrorOr< uint32_t > readStringIndex(T &Table)
Read the string index and check whether it overflows the table.
Definition: SampleProfReader.cpp:435
llvm::ProfileSummaryBuilder::DefaultCutoffs
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
llvm::handleAllErrors
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:929
llvm::sampleprof_error::truncated
@ truncated
llvm::sampleprof::SampleProfileReaderBinary::Data
const uint8_t * Data
Points to the current location in the buffer.
Definition: SampleProfReader.h:619
llvm::sampleprof::SampleProfileReaderBinary::readHeader
virtual std::error_code readHeader() override
Read and validate the file header.
Definition: SampleProfReader.cpp:1183
llvm::sampleprof::SampleProfileReader::Buffer
std::unique_ptr< MemoryBuffer > Buffer
Memory buffer holding the profile file.
Definition: SampleProfReader.h:504
llvm::sampleprof_error::bad_magic
@ bad_magic
llvm::sampleprof::FunctionSamples::setFunctionHash
void setFunctionHash(uint64_t Hash)
Definition: SampleProf.h:787
llvm::MD5Hash
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string's MD5 hash.
Definition: MD5.h:109
llvm::sampleprof::SampleProfileReaderExtBinaryBase::collectFuncsFromModule
bool collectFuncsFromModule() override
Collect functions with definitions in Module M.
Definition: SampleProfReader.cpp:665
llvm::sampleprof::SampleProfileReader::ProfileIsProbeBased
bool ProfileIsProbeBased
Whether samples are collected based on pseudo probes.
Definition: SampleProfReader.h:521
llvm::sampleprof::SampleProfileReaderBinary::at_eof
bool at_eof() const
Return true if we've reached the end of file.
Definition: SampleProfReader.h:601
llvm::sampleprof::FunctionSamples::setName
void setName(StringRef FunctionName)
Set the name of the function.
Definition: SampleProf.h:774