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