LLVM  10.0.0svn
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 as line sample.
87 ///
88 /// \param Input input line.
89 /// \param IsCallsite true if the line represents an inlined callsite.
90 /// \param Depth the depth of the inline stack.
91 /// \param NumSamples total samples of the line/inlined callsite.
92 /// \param LineOffset line offset to the start of the function.
93 /// \param Discriminator discriminator of the line.
94 /// \param TargetCountMap map from indirect call target to count.
95 ///
96 /// returns true if parsing is successful.
97 static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth,
98  uint64_t &NumSamples, uint32_t &LineOffset,
99  uint32_t &Discriminator, StringRef &CalleeName,
100  DenseMap<StringRef, uint64_t> &TargetCountMap) {
101  for (Depth = 0; Input[Depth] == ' '; Depth++)
102  ;
103  if (Depth == 0)
104  return false;
105 
106  size_t n1 = Input.find(':');
107  StringRef Loc = Input.substr(Depth, n1 - Depth);
108  size_t n2 = Loc.find('.');
109  if (n2 == StringRef::npos) {
110  if (Loc.getAsInteger(10, LineOffset) || !isOffsetLegal(LineOffset))
111  return false;
112  Discriminator = 0;
113  } else {
114  if (Loc.substr(0, n2).getAsInteger(10, LineOffset))
115  return false;
116  if (Loc.substr(n2 + 1).getAsInteger(10, Discriminator))
117  return false;
118  }
119 
120  StringRef Rest = Input.substr(n1 + 2);
121  if (Rest[0] >= '0' && Rest[0] <= '9') {
122  IsCallsite = false;
123  size_t n3 = Rest.find(' ');
124  if (n3 == StringRef::npos) {
125  if (Rest.getAsInteger(10, NumSamples))
126  return false;
127  } else {
128  if (Rest.substr(0, n3).getAsInteger(10, NumSamples))
129  return false;
130  }
131  // Find call targets and their sample counts.
132  // Note: In some cases, there are symbols in the profile which are not
133  // mangled. To accommodate such cases, use colon + integer pairs as the
134  // anchor points.
135  // An example:
136  // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
137  // ":1000" and ":437" are used as anchor points so the string above will
138  // be interpreted as
139  // target: _M_construct<char *>
140  // count: 1000
141  // target: string_view<std::allocator<char> >
142  // count: 437
143  while (n3 != StringRef::npos) {
144  n3 += Rest.substr(n3).find_first_not_of(' ');
145  Rest = Rest.substr(n3);
146  n3 = Rest.find_first_of(':');
147  if (n3 == StringRef::npos || n3 == 0)
148  return false;
149 
151  uint64_t count, n4;
152  while (true) {
153  // Get the segment after the current colon.
154  StringRef AfterColon = Rest.substr(n3 + 1);
155  // Get the target symbol before the current colon.
156  Target = Rest.substr(0, n3);
157  // Check if the word after the current colon is an integer.
158  n4 = AfterColon.find_first_of(' ');
159  n4 = (n4 != StringRef::npos) ? n3 + n4 + 1 : Rest.size();
160  StringRef WordAfterColon = Rest.substr(n3 + 1, n4 - n3 - 1);
161  if (!WordAfterColon.getAsInteger(10, count))
162  break;
163 
164  // Try to find the next colon.
165  uint64_t n5 = AfterColon.find_first_of(':');
166  if (n5 == StringRef::npos)
167  return false;
168  n3 += n5 + 1;
169  }
170 
171  // An anchor point is found. Save the {target, count} pair
172  TargetCountMap[Target] = count;
173  if (n4 == Rest.size())
174  break;
175  // Change n3 to the next blank space after colon + integer pair.
176  n3 = n4;
177  }
178  } else {
179  IsCallsite = true;
180  size_t n3 = Rest.find_last_of(':');
181  CalleeName = Rest.substr(0, n3);
182  if (Rest.substr(n3 + 1).getAsInteger(10, NumSamples))
183  return false;
184  }
185  return true;
186 }
187 
188 /// Load samples from a text file.
189 ///
190 /// See the documentation at the top of the file for an explanation of
191 /// the expected format.
192 ///
193 /// \returns true if the file was loaded successfully, false otherwise.
195  line_iterator LineIt(*Buffer, /*SkipBlanks=*/true, '#');
197 
198  InlineCallStack InlineStack;
199 
200  for (; !LineIt.is_at_eof(); ++LineIt) {
201  if ((*LineIt)[(*LineIt).find_first_not_of(' ')] == '#')
202  continue;
203  // Read the header of each function.
204  //
205  // Note that for function identifiers we are actually expecting
206  // mangled names, but we may not always get them. This happens when
207  // the compiler decides not to emit the function (e.g., it was inlined
208  // and removed). In this case, the binary will not have the linkage
209  // name for the function, so the profiler will emit the function's
210  // unmangled name, which may contain characters like ':' and '>' in its
211  // name (member functions, templates, etc).
212  //
213  // The only requirement we place on the identifier, then, is that it
214  // should not begin with a number.
215  if ((*LineIt)[0] != ' ') {
216  uint64_t NumSamples, NumHeadSamples;
217  StringRef FName;
218  if (!ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
219  reportError(LineIt.line_number(),
220  "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
222  }
223  Profiles[FName] = FunctionSamples();
224  FunctionSamples &FProfile = Profiles[FName];
225  FProfile.setName(FName);
226  MergeResult(Result, FProfile.addTotalSamples(NumSamples));
227  MergeResult(Result, FProfile.addHeadSamples(NumHeadSamples));
228  InlineStack.clear();
229  InlineStack.push_back(&FProfile);
230  } else {
231  uint64_t NumSamples;
232  StringRef FName;
233  DenseMap<StringRef, uint64_t> TargetCountMap;
234  bool IsCallsite;
235  uint32_t Depth, LineOffset, Discriminator;
236  if (!ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
237  Discriminator, FName, TargetCountMap)) {
238  reportError(LineIt.line_number(),
239  "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
240  *LineIt);
242  }
243  if (IsCallsite) {
244  while (InlineStack.size() > Depth) {
245  InlineStack.pop_back();
246  }
247  FunctionSamples &FSamples = InlineStack.back()->functionSamplesAt(
248  LineLocation(LineOffset, Discriminator))[FName];
249  FSamples.setName(FName);
250  MergeResult(Result, FSamples.addTotalSamples(NumSamples));
251  InlineStack.push_back(&FSamples);
252  } else {
253  while (InlineStack.size() > Depth) {
254  InlineStack.pop_back();
255  }
256  FunctionSamples &FProfile = *InlineStack.back();
257  for (const auto &name_count : TargetCountMap) {
258  MergeResult(Result, FProfile.addCalledTargetSamples(
259  LineOffset, Discriminator, name_count.first,
260  name_count.second));
261  }
262  MergeResult(Result, FProfile.addBodySamples(LineOffset, Discriminator,
263  NumSamples));
264  }
265  }
266  }
267  if (Result == sampleprof_error::success)
268  computeSummary();
269 
270  return Result;
271 }
272 
274  bool result = false;
275 
276  // Check that the first non-comment line is a valid function header.
277  line_iterator LineIt(Buffer, /*SkipBlanks=*/true, '#');
278  if (!LineIt.is_at_eof()) {
279  if ((*LineIt)[0] != ' ') {
280  uint64_t NumSamples, NumHeadSamples;
281  StringRef FName;
282  result = ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
283  }
284  }
285 
286  return result;
287 }
288 
290  unsigned NumBytesRead = 0;
291  std::error_code EC;
292  uint64_t Val = decodeULEB128(Data, &NumBytesRead);
293 
294  if (Val > std::numeric_limits<T>::max())
296  else if (Data + NumBytesRead > End)
298  else
300 
301  if (EC) {
302  reportError(0, EC.message());
303  return EC;
304  }
305 
306  Data += NumBytesRead;
307  return static_cast<T>(Val);
308 }
309 
311  std::error_code EC;
312  StringRef Str(reinterpret_cast<const char *>(Data));
313  if (Data + Str.size() + 1 > End) {
315  reportError(0, EC.message());
316  return EC;
317  }
318 
319  Data += Str.size() + 1;
320  return Str;
321 }
322 
323 template <typename T>
325  std::error_code EC;
326 
327  if (Data + sizeof(T) > End) {
329  reportError(0, EC.message());
330  return EC;
331  }
332 
333  using namespace support;
334  T Val = endian::readNext<T, little, unaligned>(Data);
335  return Val;
336 }
337 
338 template <typename T>
340  std::error_code EC;
341  auto Idx = readNumber<uint32_t>();
342  if (std::error_code EC = Idx.getError())
343  return EC;
344  if (*Idx >= Table.size())
346  return *Idx;
347 }
348 
350  auto Idx = readStringIndex(NameTable);
351  if (std::error_code EC = Idx.getError())
352  return EC;
353 
354  return NameTable[*Idx];
355 }
356 
357 ErrorOr<StringRef> SampleProfileReaderCompactBinary::readStringFromTable() {
358  auto Idx = readStringIndex(NameTable);
359  if (std::error_code EC = Idx.getError())
360  return EC;
361 
362  return StringRef(NameTable[*Idx]);
363 }
364 
365 std::error_code
367  auto NumSamples = readNumber<uint64_t>();
368  if (std::error_code EC = NumSamples.getError())
369  return EC;
370  FProfile.addTotalSamples(*NumSamples);
371 
372  // Read the samples in the body.
373  auto NumRecords = readNumber<uint32_t>();
374  if (std::error_code EC = NumRecords.getError())
375  return EC;
376 
377  for (uint32_t I = 0; I < *NumRecords; ++I) {
378  auto LineOffset = readNumber<uint64_t>();
379  if (std::error_code EC = LineOffset.getError())
380  return EC;
381 
382  if (!isOffsetLegal(*LineOffset)) {
383  return std::error_code();
384  }
385 
386  auto Discriminator = readNumber<uint64_t>();
387  if (std::error_code EC = Discriminator.getError())
388  return EC;
389 
390  auto NumSamples = readNumber<uint64_t>();
391  if (std::error_code EC = NumSamples.getError())
392  return EC;
393 
394  auto NumCalls = readNumber<uint32_t>();
395  if (std::error_code EC = NumCalls.getError())
396  return EC;
397 
398  for (uint32_t J = 0; J < *NumCalls; ++J) {
399  auto CalledFunction(readStringFromTable());
400  if (std::error_code EC = CalledFunction.getError())
401  return EC;
402 
403  auto CalledFunctionSamples = readNumber<uint64_t>();
404  if (std::error_code EC = CalledFunctionSamples.getError())
405  return EC;
406 
407  FProfile.addCalledTargetSamples(*LineOffset, *Discriminator,
408  *CalledFunction, *CalledFunctionSamples);
409  }
410 
411  FProfile.addBodySamples(*LineOffset, *Discriminator, *NumSamples);
412  }
413 
414  // Read all the samples for inlined function calls.
415  auto NumCallsites = readNumber<uint32_t>();
416  if (std::error_code EC = NumCallsites.getError())
417  return EC;
418 
419  for (uint32_t J = 0; J < *NumCallsites; ++J) {
420  auto LineOffset = readNumber<uint64_t>();
421  if (std::error_code EC = LineOffset.getError())
422  return EC;
423 
424  auto Discriminator = readNumber<uint64_t>();
425  if (std::error_code EC = Discriminator.getError())
426  return EC;
427 
428  auto FName(readStringFromTable());
429  if (std::error_code EC = FName.getError())
430  return EC;
431 
432  FunctionSamples &CalleeProfile = FProfile.functionSamplesAt(
433  LineLocation(*LineOffset, *Discriminator))[*FName];
434  CalleeProfile.setName(*FName);
435  if (std::error_code EC = readProfile(CalleeProfile))
436  return EC;
437  }
438 
440 }
441 
442 std::error_code
444  Data = Start;
445  auto NumHeadSamples = readNumber<uint64_t>();
446  if (std::error_code EC = NumHeadSamples.getError())
447  return EC;
448 
449  auto FName(readStringFromTable());
450  if (std::error_code EC = FName.getError())
451  return EC;
452 
453  Profiles[*FName] = FunctionSamples();
454  FunctionSamples &FProfile = Profiles[*FName];
455  FProfile.setName(*FName);
456 
457  FProfile.addHeadSamples(*NumHeadSamples);
458 
459  if (std::error_code EC = readProfile(FProfile))
460  return EC;
462 }
463 
465  while (!at_eof()) {
466  if (std::error_code EC = readFuncProfile(Data))
467  return EC;
468  }
469 
471 }
472 
473 std::error_code
474 SampleProfileReaderExtBinary::readOneSection(const uint8_t *Start,
475  uint64_t Size, SecType Type) {
476  Data = Start;
477  End = Start + Size;
478  switch (Type) {
479  case SecProfSummary:
480  if (std::error_code EC = readSummary())
481  return EC;
482  break;
483  case SecNameTable:
484  if (std::error_code EC = readNameTable())
485  return EC;
486  break;
487  case SecLBRProfile:
488  if (std::error_code EC = readFuncProfiles())
489  return EC;
490  break;
492  if (std::error_code EC = readProfileSymbolList())
493  return EC;
494  break;
495  case SecFuncOffsetTable:
496  if (std::error_code EC = readFuncOffsetTable())
497  return EC;
498  break;
499  default:
500  break;
501  }
503 }
504 
506  UseAllFuncs = false;
507  FuncsToUse.clear();
508  for (auto &F : M)
509  FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
510 }
511 
512 std::error_code SampleProfileReaderExtBinary::readFuncOffsetTable() {
513  auto Size = readNumber<uint64_t>();
514  if (std::error_code EC = Size.getError())
515  return EC;
516 
517  FuncOffsetTable.reserve(*Size);
518  for (uint32_t I = 0; I < *Size; ++I) {
519  auto FName(readStringFromTable());
520  if (std::error_code EC = FName.getError())
521  return EC;
522 
523  auto Offset = readNumber<uint64_t>();
524  if (std::error_code EC = Offset.getError())
525  return EC;
526 
527  FuncOffsetTable[*FName] = *Offset;
528  }
530 }
531 
532 std::error_code SampleProfileReaderExtBinary::readFuncProfiles() {
533  const uint8_t *Start = Data;
534  if (UseAllFuncs) {
535  while (Data < End) {
536  if (std::error_code EC = readFuncProfile(Data))
537  return EC;
538  }
539  assert(Data == End && "More data is read than expected");
541  }
542 
543  if (Remapper) {
544  for (auto Name : FuncsToUse) {
545  Remapper->insert(Name);
546  }
547  }
548 
549  for (auto NameOffset : FuncOffsetTable) {
550  auto FuncName = NameOffset.first;
551  if (!FuncsToUse.count(FuncName) &&
552  (!Remapper || !Remapper->exist(FuncName)))
553  continue;
554  const uint8_t *FuncProfileAddr = Start + NameOffset.second;
555  assert(FuncProfileAddr < End && "out of LBRProfile section");
556  if (std::error_code EC = readFuncProfile(FuncProfileAddr))
557  return EC;
558  }
559 
560  Data = End;
562 }
563 
564 std::error_code SampleProfileReaderExtBinary::readProfileSymbolList() {
565  if (!ProfSymList)
566  ProfSymList = std::make_unique<ProfileSymbolList>();
567 
568  if (std::error_code EC = ProfSymList->read(Data, End - Data))
569  return EC;
570 
571  Data = End;
573 }
574 
575 std::error_code SampleProfileReaderExtBinaryBase::decompressSection(
576  const uint8_t *SecStart, const uint64_t SecSize,
577  const uint8_t *&DecompressBuf, uint64_t &DecompressBufSize) {
578  Data = SecStart;
579  End = SecStart + SecSize;
580  auto DecompressSize = readNumber<uint64_t>();
581  if (std::error_code EC = DecompressSize.getError())
582  return EC;
583  DecompressBufSize = *DecompressSize;
584 
585  auto CompressSize = readNumber<uint64_t>();
586  if (std::error_code EC = CompressSize.getError())
587  return EC;
588 
591 
592  StringRef CompressedStrings(reinterpret_cast<const char *>(Data),
593  *CompressSize);
594  char *Buffer = Allocator.Allocate<char>(DecompressBufSize);
595  size_t UCSize = DecompressBufSize;
596  llvm::Error E =
597  zlib::uncompress(CompressedStrings, Buffer, UCSize);
598  if (E)
600  DecompressBuf = reinterpret_cast<const uint8_t *>(Buffer);
602 }
603 
605  const uint8_t *BufStart =
606  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
607 
608  for (auto &Entry : SecHdrTable) {
609  // Skip empty section.
610  if (!Entry.Size)
611  continue;
612 
613  const uint8_t *SecStart = BufStart + Entry.Offset;
614  uint64_t SecSize = Entry.Size;
615 
616  // If the section is compressed, decompress it into a buffer
617  // DecompressBuf before reading the actual data. The pointee of
618  // 'Data' will be changed to buffer hold by DecompressBuf
619  // temporarily when reading the actual data.
620  bool isCompressed = hasSecFlag(Entry, SecFlagCompress);
621  if (isCompressed) {
622  const uint8_t *DecompressBuf;
623  uint64_t DecompressBufSize;
624  if (std::error_code EC = decompressSection(
625  SecStart, SecSize, DecompressBuf, DecompressBufSize))
626  return EC;
627  SecStart = DecompressBuf;
628  SecSize = DecompressBufSize;
629  }
630 
631  if (std::error_code EC = readOneSection(SecStart, SecSize, Entry.Type))
632  return EC;
633  if (Data != SecStart + SecSize)
635 
636  // Change the pointee of 'Data' from DecompressBuf to original Buffer.
637  if (isCompressed) {
638  Data = BufStart + Entry.Offset;
639  End = BufStart + Buffer->getBufferSize();
640  }
641  }
642 
644 }
645 
647  std::vector<uint64_t> OffsetsToUse;
648  if (UseAllFuncs) {
649  for (auto FuncEntry : FuncOffsetTable) {
650  OffsetsToUse.push_back(FuncEntry.second);
651  }
652  }
653  else {
654  for (auto Name : FuncsToUse) {
655  auto GUID = std::to_string(MD5Hash(Name));
656  auto iter = FuncOffsetTable.find(StringRef(GUID));
657  if (iter == FuncOffsetTable.end())
658  continue;
659  OffsetsToUse.push_back(iter->second);
660  }
661  }
662 
663  for (auto Offset : OffsetsToUse) {
664  const uint8_t *SavedData = Data;
665  if (std::error_code EC = readFuncProfile(
666  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
667  Offset))
668  return EC;
669  Data = SavedData;
670  }
672 }
673 
674 std::error_code SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic) {
675  if (Magic == SPMagic())
678 }
679 
680 std::error_code SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic) {
681  if (Magic == SPMagic(SPF_Ext_Binary))
684 }
685 
686 std::error_code
687 SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic) {
688  if (Magic == SPMagic(SPF_Compact_Binary))
691 }
692 
694  auto Size = readNumber<uint32_t>();
695  if (std::error_code EC = Size.getError())
696  return EC;
697  NameTable.reserve(*Size);
698  for (uint32_t I = 0; I < *Size; ++I) {
699  auto Name(readString());
700  if (std::error_code EC = Name.getError())
701  return EC;
702  NameTable.push_back(*Name);
703  }
704 
706 }
707 
708 std::error_code SampleProfileReaderCompactBinary::readNameTable() {
709  auto Size = readNumber<uint64_t>();
710  if (std::error_code EC = Size.getError())
711  return EC;
712  NameTable.reserve(*Size);
713  for (uint32_t I = 0; I < *Size; ++I) {
714  auto FID = readNumber<uint64_t>();
715  if (std::error_code EC = FID.getError())
716  return EC;
717  NameTable.push_back(std::to_string(*FID));
718  }
720 }
721 
724  auto Type = readUnencodedNumber<uint64_t>();
725  if (std::error_code EC = Type.getError())
726  return EC;
727  Entry.Type = static_cast<SecType>(*Type);
728 
729  auto Flags = readUnencodedNumber<uint64_t>();
730  if (std::error_code EC = Flags.getError())
731  return EC;
732  Entry.Flags = *Flags;
733 
734  auto Offset = readUnencodedNumber<uint64_t>();
735  if (std::error_code EC = Offset.getError())
736  return EC;
737  Entry.Offset = *Offset;
738 
739  auto Size = readUnencodedNumber<uint64_t>();
740  if (std::error_code EC = Size.getError())
741  return EC;
742  Entry.Size = *Size;
743 
744  SecHdrTable.push_back(std::move(Entry));
746 }
747 
749  auto EntryNum = readUnencodedNumber<uint64_t>();
750  if (std::error_code EC = EntryNum.getError())
751  return EC;
752 
753  for (uint32_t i = 0; i < (*EntryNum); i++)
754  if (std::error_code EC = readSecHdrTableEntry())
755  return EC;
756 
758 }
759 
761  const uint8_t *BufStart =
762  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
763  Data = BufStart;
764  End = BufStart + Buffer->getBufferSize();
765 
766  if (std::error_code EC = readMagicIdent())
767  return EC;
768 
769  if (std::error_code EC = readSecHdrTable())
770  return EC;
771 
773 }
774 
776  for (auto &Entry : SecHdrTable) {
777  if (Entry.Type == Type)
778  return Entry.Size;
779  }
780  return 0;
781 }
782 
784  // Sections in SecHdrTable is not necessarily in the same order as
785  // sections in the profile because section like FuncOffsetTable needs
786  // to be written after section LBRProfile but needs to be read before
787  // section LBRProfile, so we cannot simply use the last entry in
788  // SecHdrTable to calculate the file size.
789  uint64_t FileSize = 0;
790  for (auto &Entry : SecHdrTable) {
791  FileSize = std::max(Entry.Offset + Entry.Size, FileSize);
792  }
793  return FileSize;
794 }
795 
797  uint64_t TotalSecsSize = 0;
798  for (auto &Entry : SecHdrTable) {
799  OS << getSecName(Entry.Type) << " - Offset: " << Entry.Offset
800  << ", Size: " << Entry.Size << "\n";
801  TotalSecsSize += getSectionSize(Entry.Type);
802  }
803  uint64_t HeaderSize = SecHdrTable.front().Offset;
804  assert(HeaderSize + TotalSecsSize == getFileSize() &&
805  "Size of 'header + sections' doesn't match the total size of profile");
806 
807  OS << "Header Size: " << HeaderSize << "\n";
808  OS << "Total Sections Size: " << TotalSecsSize << "\n";
809  OS << "File Size: " << getFileSize() << "\n";
810  return true;
811 }
812 
814  // Read and check the magic identifier.
815  auto Magic = readNumber<uint64_t>();
816  if (std::error_code EC = Magic.getError())
817  return EC;
818  else if (std::error_code EC = verifySPMagic(*Magic))
819  return EC;
820 
821  // Read the version number.
822  auto Version = readNumber<uint64_t>();
823  if (std::error_code EC = Version.getError())
824  return EC;
825  else if (*Version != SPVersion())
827 
829 }
830 
832  Data = reinterpret_cast<const uint8_t *>(Buffer->getBufferStart());
833  End = Data + Buffer->getBufferSize();
834 
835  if (std::error_code EC = readMagicIdent())
836  return EC;
837 
838  if (std::error_code EC = readSummary())
839  return EC;
840 
841  if (std::error_code EC = readNameTable())
842  return EC;
844 }
845 
846 std::error_code SampleProfileReaderCompactBinary::readHeader() {
848  if (std::error_code EC = readFuncOffsetTable())
849  return EC;
851 }
852 
853 std::error_code SampleProfileReaderCompactBinary::readFuncOffsetTable() {
854  auto TableOffset = readUnencodedNumber<uint64_t>();
855  if (std::error_code EC = TableOffset.getError())
856  return EC;
857 
858  const uint8_t *SavedData = Data;
859  const uint8_t *TableStart =
860  reinterpret_cast<const uint8_t *>(Buffer->getBufferStart()) +
861  *TableOffset;
862  Data = TableStart;
863 
864  auto Size = readNumber<uint64_t>();
865  if (std::error_code EC = Size.getError())
866  return EC;
867 
868  FuncOffsetTable.reserve(*Size);
869  for (uint32_t I = 0; I < *Size; ++I) {
870  auto FName(readStringFromTable());
871  if (std::error_code EC = FName.getError())
872  return EC;
873 
874  auto Offset = readNumber<uint64_t>();
875  if (std::error_code EC = Offset.getError())
876  return EC;
877 
878  FuncOffsetTable[*FName] = *Offset;
879  }
880  End = TableStart;
881  Data = SavedData;
883 }
884 
886  UseAllFuncs = false;
887  FuncsToUse.clear();
888  for (auto &F : M)
889  FuncsToUse.insert(FunctionSamples::getCanonicalFnName(F));
890 }
891 
892 std::error_code SampleProfileReaderBinary::readSummaryEntry(
893  std::vector<ProfileSummaryEntry> &Entries) {
894  auto Cutoff = readNumber<uint64_t>();
895  if (std::error_code EC = Cutoff.getError())
896  return EC;
897 
898  auto MinBlockCount = readNumber<uint64_t>();
899  if (std::error_code EC = MinBlockCount.getError())
900  return EC;
901 
902  auto NumBlocks = readNumber<uint64_t>();
903  if (std::error_code EC = NumBlocks.getError())
904  return EC;
905 
906  Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
908 }
909 
911  auto TotalCount = readNumber<uint64_t>();
912  if (std::error_code EC = TotalCount.getError())
913  return EC;
914 
915  auto MaxBlockCount = readNumber<uint64_t>();
916  if (std::error_code EC = MaxBlockCount.getError())
917  return EC;
918 
919  auto MaxFunctionCount = readNumber<uint64_t>();
920  if (std::error_code EC = MaxFunctionCount.getError())
921  return EC;
922 
923  auto NumBlocks = readNumber<uint64_t>();
924  if (std::error_code EC = NumBlocks.getError())
925  return EC;
926 
927  auto NumFunctions = readNumber<uint64_t>();
928  if (std::error_code EC = NumFunctions.getError())
929  return EC;
930 
931  auto NumSummaryEntries = readNumber<uint64_t>();
932  if (std::error_code EC = NumSummaryEntries.getError())
933  return EC;
934 
935  std::vector<ProfileSummaryEntry> Entries;
936  for (unsigned i = 0; i < *NumSummaryEntries; i++) {
937  std::error_code EC = readSummaryEntry(Entries);
938  if (EC != sampleprof_error::success)
939  return EC;
940  }
941  Summary = std::make_unique<ProfileSummary>(
942  ProfileSummary::PSK_Sample, Entries, *TotalCount, *MaxBlockCount, 0,
943  *MaxFunctionCount, *NumBlocks, *NumFunctions);
944 
946 }
947 
949  const uint8_t *Data =
950  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
951  uint64_t Magic = decodeULEB128(Data);
952  return Magic == SPMagic();
953 }
954 
956  const uint8_t *Data =
957  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
958  uint64_t Magic = decodeULEB128(Data);
959  return Magic == SPMagic(SPF_Ext_Binary);
960 }
961 
963  const uint8_t *Data =
964  reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
965  uint64_t Magic = decodeULEB128(Data);
966  return Magic == SPMagic(SPF_Compact_Binary);
967 }
968 
970  uint32_t dummy;
971  if (!GcovBuffer.readInt(dummy))
974 }
975 
977  if (sizeof(T) <= sizeof(uint32_t)) {
978  uint32_t Val;
979  if (GcovBuffer.readInt(Val) && Val <= std::numeric_limits<T>::max())
980  return static_cast<T>(Val);
981  } else if (sizeof(T) <= sizeof(uint64_t)) {
982  uint64_t Val;
983  if (GcovBuffer.readInt64(Val) && Val <= std::numeric_limits<T>::max())
984  return static_cast<T>(Val);
985  }
986 
987  std::error_code EC = sampleprof_error::malformed;
988  reportError(0, EC.message());
989  return EC;
990 }
991 
993  StringRef Str;
994  if (!GcovBuffer.readString(Str))
996  return Str;
997 }
998 
1000  // Read the magic identifier.
1001  if (!GcovBuffer.readGCDAFormat())
1003 
1004  // Read the version number. Note - the GCC reader does not validate this
1005  // version, but the profile creator generates v704.
1006  GCOV::GCOVVersion version;
1007  if (!GcovBuffer.readGCOVVersion(version))
1009 
1010  if (version != GCOV::V704)
1012 
1013  // Skip the empty integer.
1014  if (std::error_code EC = skipNextWord())
1015  return EC;
1016 
1018 }
1019 
1021  uint32_t Tag;
1022  if (!GcovBuffer.readInt(Tag))
1024 
1025  if (Tag != Expected)
1027 
1028  if (std::error_code EC = skipNextWord())
1029  return EC;
1030 
1032 }
1033 
1035  if (std::error_code EC = readSectionTag(GCOVTagAFDOFileNames))
1036  return EC;
1037 
1038  uint32_t Size;
1039  if (!GcovBuffer.readInt(Size))
1041 
1042  for (uint32_t I = 0; I < Size; ++I) {
1043  StringRef Str;
1044  if (!GcovBuffer.readString(Str))
1046  Names.push_back(Str);
1047  }
1048 
1050 }
1051 
1053  if (std::error_code EC = readSectionTag(GCOVTagAFDOFunction))
1054  return EC;
1055 
1056  uint32_t NumFunctions;
1057  if (!GcovBuffer.readInt(NumFunctions))
1059 
1060  InlineCallStack Stack;
1061  for (uint32_t I = 0; I < NumFunctions; ++I)
1062  if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
1063  return EC;
1064 
1065  computeSummary();
1067 }
1068 
1070  const InlineCallStack &InlineStack, bool Update, uint32_t Offset) {
1071  uint64_t HeadCount = 0;
1072  if (InlineStack.size() == 0)
1073  if (!GcovBuffer.readInt64(HeadCount))
1075 
1076  uint32_t NameIdx;
1077  if (!GcovBuffer.readInt(NameIdx))
1079 
1080  StringRef Name(Names[NameIdx]);
1081 
1082  uint32_t NumPosCounts;
1083  if (!GcovBuffer.readInt(NumPosCounts))
1085 
1086  uint32_t NumCallsites;
1087  if (!GcovBuffer.readInt(NumCallsites))
1089 
1090  FunctionSamples *FProfile = nullptr;
1091  if (InlineStack.size() == 0) {
1092  // If this is a top function that we have already processed, do not
1093  // update its profile again. This happens in the presence of
1094  // function aliases. Since these aliases share the same function
1095  // body, there will be identical replicated profiles for the
1096  // original function. In this case, we simply not bother updating
1097  // the profile of the original function.
1098  FProfile = &Profiles[Name];
1099  FProfile->addHeadSamples(HeadCount);
1100  if (FProfile->getTotalSamples() > 0)
1101  Update = false;
1102  } else {
1103  // Otherwise, we are reading an inlined instance. The top of the
1104  // inline stack contains the profile of the caller. Insert this
1105  // callee in the caller's CallsiteMap.
1106  FunctionSamples *CallerProfile = InlineStack.front();
1107  uint32_t LineOffset = Offset >> 16;
1108  uint32_t Discriminator = Offset & 0xffff;
1109  FProfile = &CallerProfile->functionSamplesAt(
1110  LineLocation(LineOffset, Discriminator))[Name];
1111  }
1112  FProfile->setName(Name);
1113 
1114  for (uint32_t I = 0; I < NumPosCounts; ++I) {
1115  uint32_t Offset;
1116  if (!GcovBuffer.readInt(Offset))
1118 
1119  uint32_t NumTargets;
1120  if (!GcovBuffer.readInt(NumTargets))
1122 
1123  uint64_t Count;
1124  if (!GcovBuffer.readInt64(Count))
1126 
1127  // The line location is encoded in the offset as:
1128  // high 16 bits: line offset to the start of the function.
1129  // low 16 bits: discriminator.
1130  uint32_t LineOffset = Offset >> 16;
1131  uint32_t Discriminator = Offset & 0xffff;
1132 
1133  InlineCallStack NewStack;
1134  NewStack.push_back(FProfile);
1135  NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
1136  if (Update) {
1137  // Walk up the inline stack, adding the samples on this line to
1138  // the total sample count of the callers in the chain.
1139  for (auto CallerProfile : NewStack)
1140  CallerProfile->addTotalSamples(Count);
1141 
1142  // Update the body samples for the current profile.
1143  FProfile->addBodySamples(LineOffset, Discriminator, Count);
1144  }
1145 
1146  // Process the list of functions called at an indirect call site.
1147  // These are all the targets that a function pointer (or virtual
1148  // function) resolved at runtime.
1149  for (uint32_t J = 0; J < NumTargets; J++) {
1150  uint32_t HistVal;
1151  if (!GcovBuffer.readInt(HistVal))
1153 
1154  if (HistVal != HIST_TYPE_INDIR_CALL_TOPN)
1156 
1157  uint64_t TargetIdx;
1158  if (!GcovBuffer.readInt64(TargetIdx))
1160  StringRef TargetName(Names[TargetIdx]);
1161 
1162  uint64_t TargetCount;
1163  if (!GcovBuffer.readInt64(TargetCount))
1165 
1166  if (Update)
1167  FProfile->addCalledTargetSamples(LineOffset, Discriminator,
1168  TargetName, TargetCount);
1169  }
1170  }
1171 
1172  // Process all the inlined callers into the current function. These
1173  // are all the callsites that were inlined into this function.
1174  for (uint32_t I = 0; I < NumCallsites; I++) {
1175  // The offset is encoded as:
1176  // high 16 bits: line offset to the start of the function.
1177  // low 16 bits: discriminator.
1178  uint32_t Offset;
1179  if (!GcovBuffer.readInt(Offset))
1181  InlineCallStack NewStack;
1182  NewStack.push_back(FProfile);
1183  NewStack.insert(NewStack.end(), InlineStack.begin(), InlineStack.end());
1184  if (std::error_code EC = readOneFunctionProfile(NewStack, Update, Offset))
1185  return EC;
1186  }
1187 
1189 }
1190 
1191 /// Read a GCC AutoFDO profile.
1192 ///
1193 /// This format is generated by the Linux Perf conversion tool at
1194 /// https://github.com/google/autofdo.
1196  // Read the string table.
1197  if (std::error_code EC = readNameTable())
1198  return EC;
1199 
1200  // Read the source profile.
1201  if (std::error_code EC = readFunctionProfiles())
1202  return EC;
1203 
1205 }
1206 
1208  StringRef Magic(reinterpret_cast<const char *>(Buffer.getBufferStart()));
1209  return Magic == "adcg*704";
1210 }
1211 
1213  // If the reader is in compact format, we can't remap it because
1214  // we don't know what the original function names were.
1215  if (Reader.getFormat() == SPF_Compact_Binary) {
1217  Reader.getBuffer()->getBufferIdentifier(),
1218  "Profile data remapping cannot be applied to profile data "
1219  "in compact format (original mangled names are not available).",
1220  DS_Warning));
1221  return;
1222  }
1223 
1224  assert(Remappings && "should be initialized while creating remapper");
1225  for (auto &Sample : Reader.getProfiles())
1226  if (auto Key = Remappings->insert(Sample.first()))
1227  SampleMap.insert({Key, &Sample.second});
1228 
1229  RemappingApplied = true;
1230 }
1231 
1234  if (auto Key = Remappings->lookup(Fname))
1235  return SampleMap.lookup(Key);
1236  return nullptr;
1237 }
1238 
1239 /// Prepare a memory buffer for the contents of \p Filename.
1240 ///
1241 /// \returns an error code indicating the status of the buffer.
1243 setupMemoryBuffer(const Twine &Filename) {
1244  auto BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename);
1245  if (std::error_code EC = BufferOrErr.getError())
1246  return EC;
1247  auto Buffer = std::move(BufferOrErr.get());
1248 
1249  // Sanity check the file.
1250  if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint32_t>::max())
1252 
1253  return std::move(Buffer);
1254 }
1255 
1256 /// Create a sample profile reader based on the format of the input file.
1257 ///
1258 /// \param Filename The file to open.
1259 ///
1260 /// \param C The LLVM context to use to emit diagnostics.
1261 ///
1262 /// \param RemapFilename The file used for profile remapping.
1263 ///
1264 /// \returns an error code indicating the status of the created reader.
1266 SampleProfileReader::create(const std::string Filename, LLVMContext &C,
1267  const std::string RemapFilename) {
1268  auto BufferOrError = setupMemoryBuffer(Filename);
1269  if (std::error_code EC = BufferOrError.getError())
1270  return EC;
1271  return create(BufferOrError.get(), C, RemapFilename);
1272 }
1273 
1274 /// Create a sample profile remapper from the given input, to remap the
1275 /// function names in the given profile data.
1276 ///
1277 /// \param Filename The file to open.
1278 ///
1279 /// \param Reader The profile reader the remapper is going to be applied to.
1280 ///
1281 /// \param C The LLVM context to use to emit diagnostics.
1282 ///
1283 /// \returns an error code indicating the status of the created reader.
1286  SampleProfileReader &Reader,
1287  LLVMContext &C) {
1288  auto BufferOrError = setupMemoryBuffer(Filename);
1289  if (std::error_code EC = BufferOrError.getError())
1290  return EC;
1291  return create(BufferOrError.get(), Reader, C);
1292 }
1293 
1294 /// Create a sample profile remapper from the given input, to remap the
1295 /// function names in the given profile data.
1296 ///
1297 /// \param B The memory buffer to create the reader from (assumes ownership).
1298 ///
1299 /// \param C The LLVM context to use to emit diagnostics.
1300 ///
1301 /// \param Reader The profile reader the remapper is going to be applied to.
1302 ///
1303 /// \returns an error code indicating the status of the created reader.
1305 SampleProfileReaderItaniumRemapper::create(std::unique_ptr<MemoryBuffer> &B,
1306  SampleProfileReader &Reader,
1307  LLVMContext &C) {
1308  auto Remappings = std::make_unique<SymbolRemappingReader>();
1309  if (Error E = Remappings->read(*B.get())) {
1311  std::move(E), [&](const SymbolRemappingParseError &ParseError) {
1312  C.diagnose(DiagnosticInfoSampleProfile(B->getBufferIdentifier(),
1313  ParseError.getLineNum(),
1314  ParseError.getMessage()));
1315  });
1317  }
1318 
1319  return std::make_unique<SampleProfileReaderItaniumRemapper>(
1320  std::move(B), std::move(Remappings), Reader);
1321 }
1322 
1323 /// Create a sample profile reader based on the format of the input data.
1324 ///
1325 /// \param B The memory buffer to create the reader from (assumes ownership).
1326 ///
1327 /// \param C The LLVM context to use to emit diagnostics.
1328 ///
1329 /// \param RemapFilename The file used for profile remapping.
1330 ///
1331 /// \returns an error code indicating the status of the created reader.
1333 SampleProfileReader::create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C,
1334  const std::string RemapFilename) {
1335  std::unique_ptr<SampleProfileReader> Reader;
1337  Reader.reset(new SampleProfileReaderRawBinary(std::move(B), C));
1339  Reader.reset(new SampleProfileReaderExtBinary(std::move(B), C));
1341  Reader.reset(new SampleProfileReaderCompactBinary(std::move(B), C));
1342  else if (SampleProfileReaderGCC::hasFormat(*B))
1343  Reader.reset(new SampleProfileReaderGCC(std::move(B), C));
1345  Reader.reset(new SampleProfileReaderText(std::move(B), C));
1346  else
1348 
1349  if (!RemapFilename.empty()) {
1350  auto ReaderOrErr =
1351  SampleProfileReaderItaniumRemapper::create(RemapFilename, *Reader, C);
1352  if (std::error_code EC = ReaderOrErr.getError()) {
1353  std::string Msg = "Could not create remapper: " + EC.message();
1354  C.diagnose(DiagnosticInfoSampleProfile(RemapFilename, Msg));
1355  return EC;
1356  }
1357  Reader->Remapper = std::move(ReaderOrErr.get());
1358  }
1359 
1360  FunctionSamples::Format = Reader->getFormat();
1361  if (std::error_code EC = Reader->readHeader()) {
1362  return EC;
1363  }
1364 
1365  return std::move(Reader);
1366 }
1367 
1368 // For text and GCC file formats, we compute the summary after reading the
1369 // profile. Binary format has the profile summary in its header.
1372  for (const auto &I : Profiles) {
1373  const FunctionSamples &Profile = I.second;
1374  Builder.addRecord(Profile);
1375  }
1376  Summary = Builder.getSummary();
1377 }
uint64_t CallInst * C
std::unique_ptr< MemoryBuffer > Buffer
Memory buffer holding the profile file.
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::error_code readImpl() override
Read sample profiles from the associated file.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
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.
FunctionSamples * getSamplesFor(StringRef FunctionName)
Return the samples collected for function F if remapper knows it is present in SampleMap.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:359
void push_back(const T &Elt)
Definition: SmallVector.h:211
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
std::error_code readSummary()
Read profile summary.
virtual std::error_code readHeader() override
Read and validate the file header.
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string&#39;s MD5 hash.
Definition: MD5.h:109
void applyRemapping(LLVMContext &Ctx)
Apply remappings to the profile read by Reader.
std::error_code readImpl() override
Read sample profiles in extensible format from the associated file.
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:420
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:64
std::error_code readProfile(FunctionSamples &FProfile)
Read the contents of the given profile instance.
F(f)
static SampleProfileFormat Format
Definition: SampleProf.h:557
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Representation of the samples collected for a function.
Definition: SampleProf.h:300
static std::string getSecName(SecType Type)
Definition: SampleProf.h:129
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
static bool hasSecFlag(SecHdrTableEntry &Entry, SecFlags Flag)
Definition: SampleProf.h:166
virtual std::error_code readNameTable()
Read the whole name table.
StringMap< FunctionSamples > Profiles
Map every function to its associated profile.
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
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:592
void collectFuncsFrom(const Module &M) override
Collect functions with definitions in Module M.
bool is_at_eof() const
Return true if we&#39;ve reached EOF or are an "end" iterator.
Definition: LineIterator.h:49
Key
PAL metadata keys.
std::error_code readImpl() override
Read sample profiles from the associated file.
static bool ParseHead(const StringRef &Input, StringRef &FName, uint64_t &NumSamples, uint64_t &NumHeadSamples)
Parse Input as function head.
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
void setName(StringRef FunctionName)
Set the name of the function.
Definition: SampleProf.h:487
std::error_code readImpl() override
Read samples only for functions to use.
static uint64_t SPVersion()
Definition: SampleProf.h:113
std::error_code readFuncProfile(const uint8_t *Start)
Read the next function profile instance.
static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, uint64_t &NumSamples, uint32_t &LineOffset, uint32_t &Discriminator, StringRef &CalleeName, DenseMap< StringRef, uint64_t > &TargetCountMap)
Parse Input as line sample.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1231
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:55
std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack, bool Update, uint32_t Offset)
bool isAvailable()
Definition: Compression.cpp:47
uint64_t getFileSize()
Get the total size of header and all sections.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static ErrorOr< std::unique_ptr< SampleProfileReaderItaniumRemapper > > create(const std::string Filename, SampleProfileReader &Reader, LLVMContext &C)
Create a remapper from the given remapping file.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
sampleprof_error
Definition: SampleProf.h:43
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:323
ErrorOr< T > readNumber()
Read a numeric value of type T from the profile.
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:249
LLVMContext & Ctx
LLVM context used to emit diagnostics.
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes...
Definition: SampleProf.h:499
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:299
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
ErrorOr< StringRef > readString()
Read a string from the profile.
virtual std::error_code readHeader() override
Read and validate the file header.
size_t size() const
Definition: SmallVector.h:52
std::error_code readMagicIdent()
Read the contents of Magic number and Version number.
GCOVVersion
Definition: GCOV.h:43
std::error_code readImpl() override
Read sample profiles from the associated file.
void dumpFunctionProfile(StringRef FName, raw_ostream &OS=dbgs())
Print the profile for FName on stream OS.
static const char *const Magic
Definition: Archive.cpp:41
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:492
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:924
Basic Register Allocator
virtual bool dumpSectionInfo(raw_ostream &OS=dbgs()) override
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:390
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:307
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
uint64_t getSectionSize(SecType Type)
Get the total size of all Type sections.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
Definition: SampleProf.h:361
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
static StringRef readString(WasmObjectFile::ReadContext &Ctx)
Target - Wrapper for Target specific information.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:467
std::unique_ptr< ProfileSummary > getSummary()
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 "-".
ErrorOr< T > readUnencodedNumber()
Read a numeric value of type T from the profile.
static const size_t npos
Definition: StringRef.h:50
Represents the relative location of an instruction.
Definition: SampleProf.h:179
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:394
#define I(x, y, z)
Definition: MD5.cpp:58
uint32_t Size
Definition: Profile.cpp:46
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const char * getBufferStart() const
Definition: MemoryBuffer.h:59
void collectFuncsFrom(const Module &M) override
Collect functions to be used when compiling Module M.
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:96
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:315
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::error_code readSectionTag(uint32_t Expected)
Read the section tag and check that it&#39;s the same as Expected.
static ErrorOr< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename)
Prepare a memory buffer for the contents of Filename.
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition: SampleProf.h:404
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
ErrorOr< uint32_t > readStringIndex(T &Table)
Read the string index and check whether it overflows the table.
void reportError(int64_t LineNumber, Twine Msg) const
Report a parse error message.
Sample-based profile reader.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
virtual ErrorOr< StringRef > readStringFromTable()
Read a string indirectly via the name table.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Diagnostic information for the sample profiler.
std::error_code readHeader() override
Read and validate the file header.
void computeSummary()
Compute summary for this profile.
void dump(raw_ostream &OS=dbgs())
Print all the profiles on stream OS.
Error uncompress(StringRef InputBuffer, char *UncompressedBuffer, size_t &UncompressedSize)
Definition: Compression.cpp:63
const uint64_t Version
Definition: InstrProf.h:980
static bool isOffsetLegal(unsigned L)
Returns true if line offset L is legal (only has 16 bits).
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
Definition: SampleProf.h:329
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
Definition: SampleProf.h:65
std::unique_ptr< ProfileSummary > Summary
Profile summary information.
std::unique_ptr< SampleProfileReaderItaniumRemapper > Remapper