LLVM  13.0.0git
SampleProfWriter.cpp
Go to the documentation of this file.
1 //===- SampleProfWriter.cpp - Write 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 writes LLVM sample profiles. It
10 // supports two file formats: text and binary. The textual representation
11 // is useful for debugging and testing purposes. The binary representation
12 // is more compact, resulting in smaller file sizes. However, they can
13 // both be used interchangeably.
14 //
15 // See lib/ProfileData/SampleProfReader.cpp for documentation on each of the
16 // supported formats.
17 //
18 //===----------------------------------------------------------------------===//
19 
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSet.h"
26 #include "llvm/Support/Endian.h"
28 #include "llvm/Support/ErrorOr.h"
30 #include "llvm/Support/LEB128.h"
31 #include "llvm/Support/MD5.h"
33 #include <algorithm>
34 #include <cstdint>
35 #include <memory>
36 #include <set>
37 #include <system_error>
38 #include <utility>
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace sampleprof;
43 
45  const StringMap<FunctionSamples> &ProfileMap) {
46  // Sort the ProfileMap by total samples.
47  typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
48  std::vector<NameFunctionSamples> V;
49  for (const auto &I : ProfileMap)
50  V.push_back(std::make_pair(I.getKey(), &I.second));
51 
53  V, [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
54  if (A.second->getTotalSamples() == B.second->getTotalSamples())
55  return A.first > B.first;
56  return A.second->getTotalSamples() > B.second->getTotalSamples();
57  });
58 
59  for (const auto &I : V) {
60  if (std::error_code EC = writeSample(*I.second))
61  return EC;
62  }
64 }
65 
66 std::error_code
68  if (std::error_code EC = writeHeader(ProfileMap))
69  return EC;
70 
71  if (std::error_code EC = writeFuncProfiles(ProfileMap))
72  return EC;
73 
75 }
76 
77 /// Return the current position and prepare to use it as the start
78 /// position of a section given the section type \p Type and its position
79 /// \p LayoutIdx in SectionHdrLayout.
80 uint64_t
82  uint32_t LayoutIdx) {
83  uint64_t SectionStart = OutputStream->tell();
84  assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
85  const auto &Entry = SectionHdrLayout[LayoutIdx];
86  assert(Entry.Type == Type && "Unexpected section type");
87  // Use LocalBuf as a temporary output for writting data.
89  LocalBufStream.swap(OutputStream);
90  return SectionStart;
91 }
92 
93 std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
96  std::string &UncompressedStrings =
97  static_cast<raw_string_ostream *>(LocalBufStream.get())->str();
98  if (UncompressedStrings.size() == 0)
100  auto &OS = *OutputStream;
101  SmallString<128> CompressedStrings;
102  llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
104  if (E)
106  encodeULEB128(UncompressedStrings.size(), OS);
107  encodeULEB128(CompressedStrings.size(), OS);
108  OS << CompressedStrings.str();
109  UncompressedStrings.clear();
111 }
112 
113 /// Add a new section into section header table given the section type
114 /// \p Type, its position \p LayoutIdx in SectionHdrLayout and the
115 /// location \p SectionStart where the section should be written to.
117  SecType Type, uint32_t LayoutIdx, uint64_t SectionStart) {
118  assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
119  const auto &Entry = SectionHdrLayout[LayoutIdx];
120  assert(Entry.Type == Type && "Unexpected section type");
122  LocalBufStream.swap(OutputStream);
123  if (std::error_code EC = compressAndOutput())
124  return EC;
125  }
126  SecHdrTable.push_back({Type, Entry.Flags, SectionStart - FileStart,
127  OutputStream->tell() - SectionStart, LayoutIdx});
129 }
130 
132  const StringMap<FunctionSamples> &ProfileMap) {
133  if (std::error_code EC = writeHeader(ProfileMap))
134  return EC;
135 
136  std::string LocalBuf;
137  LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
138  if (std::error_code EC = writeSections(ProfileMap))
139  return EC;
140 
141  if (std::error_code EC = writeSecHdrTable())
142  return EC;
143 
145 }
146 
147 std::error_code
149  uint64_t Offset = OutputStream->tell();
150  StringRef Name = S.getNameWithContext(true);
151  FuncOffsetTable[Name] = Offset - SecLBRProfileStart;
152  encodeULEB128(S.getHeadSamples(), *OutputStream);
153  return writeBody(S);
154 }
155 
157  auto &OS = *OutputStream;
158 
159  // Write out the table size.
160  encodeULEB128(FuncOffsetTable.size(), OS);
161 
162  // Write out FuncOffsetTable.
163  for (auto entry : FuncOffsetTable) {
164  writeNameIdx(entry.first);
165  encodeULEB128(entry.second, OS);
166  }
167  FuncOffsetTable.clear();
169 }
170 
172  const StringMap<FunctionSamples> &Profiles) {
175  auto &OS = *OutputStream;
176  for (const auto &Entry : Profiles) {
177  writeNameIdx(Entry.first());
178  encodeULEB128(Entry.second.getFunctionHash(), OS);
179  }
181 }
182 
184  if (!UseMD5)
186 
187  auto &OS = *OutputStream;
188  std::set<StringRef> V;
190 
191  // Write out the MD5 name table. We wrote unencoded MD5 so reader can
192  // retrieve the name using the name index without having to read the
193  // whole name table.
194  encodeULEB128(NameTable.size(), OS);
196  for (auto N : V)
197  Writer.write(MD5Hash(N));
199 }
200 
202  const StringMap<FunctionSamples> &ProfileMap) {
203  for (const auto &I : ProfileMap) {
204  addName(I.first());
205  addNames(I.second);
206  }
207  if (auto EC = writeNameTable())
208  return EC;
210 }
211 
212 std::error_code
214  if (ProfSymList && ProfSymList->size() > 0)
215  if (std::error_code EC = ProfSymList->write(*OutputStream))
216  return EC;
217 
219 }
220 
222  SecType Type, uint32_t LayoutIdx,
223  const StringMap<FunctionSamples> &ProfileMap) {
224  // The setting of SecFlagCompress should happen before markSectionStart.
225  if (Type == SecProfileSymbolList && ProfSymList && ProfSymList->toCompress())
229 
230  uint64_t SectionStart = markSectionStart(Type, LayoutIdx);
231  switch (Type) {
232  case SecProfSummary:
233  computeSummary(ProfileMap);
234  if (auto EC = writeSummary())
235  return EC;
236  break;
237  case SecNameTable:
238  if (auto EC = writeNameTableSection(ProfileMap))
239  return EC;
240  break;
241  case SecLBRProfile:
243  if (std::error_code EC = writeFuncProfiles(ProfileMap))
244  return EC;
245  break;
246  case SecFuncOffsetTable:
247  if (auto EC = writeFuncOffsetTable())
248  return EC;
249  break;
250  case SecFuncMetadata:
251  if (std::error_code EC = writeFuncMetadata(ProfileMap))
252  return EC;
253  break;
255  if (auto EC = writeProfileSymbolListSection())
256  return EC;
257  break;
258  default:
259  if (auto EC = writeCustomSection(Type))
260  return EC;
261  break;
262  }
263  if (std::error_code EC = addNewSection(Type, LayoutIdx, SectionStart))
264  return EC;
266 }
267 
268 std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
269  const StringMap<FunctionSamples> &ProfileMap) {
270  // The const indices passed to writeOneSection below are specifying the
271  // positions of the sections in SectionHdrLayout. Look at
272  // initSectionHdrLayout to find out where each section is located in
273  // SectionHdrLayout.
274  if (auto EC = writeOneSection(SecProfSummary, 0, ProfileMap))
275  return EC;
276  if (auto EC = writeOneSection(SecNameTable, 1, ProfileMap))
277  return EC;
278  if (auto EC = writeOneSection(SecLBRProfile, 3, ProfileMap))
279  return EC;
280  if (auto EC = writeOneSection(SecProfileSymbolList, 4, ProfileMap))
281  return EC;
282  if (auto EC = writeOneSection(SecFuncOffsetTable, 2, ProfileMap))
283  return EC;
284  if (auto EC = writeOneSection(SecFuncMetadata, 5, ProfileMap))
285  return EC;
287 }
288 
289 static void
291  StringMap<FunctionSamples> &ContextProfileMap,
292  StringMap<FunctionSamples> &NoContextProfileMap) {
293  for (const auto &I : ProfileMap) {
294  if (I.second.getCallsiteSamples().size())
295  ContextProfileMap.insert({I.first(), I.second});
296  else
297  NoContextProfileMap.insert({I.first(), I.second});
298  }
299 }
300 
301 std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
302  const StringMap<FunctionSamples> &ProfileMap) {
303  StringMap<FunctionSamples> ContextProfileMap, NoContextProfileMap;
304  splitProfileMapToTwo(ProfileMap, ContextProfileMap, NoContextProfileMap);
305 
306  if (auto EC = writeOneSection(SecProfSummary, 0, ProfileMap))
307  return EC;
308  if (auto EC = writeOneSection(SecNameTable, 1, ProfileMap))
309  return EC;
310  if (auto EC = writeOneSection(SecLBRProfile, 3, ContextProfileMap))
311  return EC;
312  if (auto EC = writeOneSection(SecFuncOffsetTable, 2, ContextProfileMap))
313  return EC;
314  // Mark the section to have no context. Note section flag needs to be set
315  // before writing the section.
317  if (auto EC = writeOneSection(SecLBRProfile, 5, NoContextProfileMap))
318  return EC;
319  // Mark the section to have no context. Note section flag needs to be set
320  // before writing the section.
322  if (auto EC = writeOneSection(SecFuncOffsetTable, 4, NoContextProfileMap))
323  return EC;
324  if (auto EC = writeOneSection(SecProfileSymbolList, 6, ProfileMap))
325  return EC;
326  if (auto EC = writeOneSection(SecFuncMetadata, 7, ProfileMap))
327  return EC;
328 
330 }
331 
332 std::error_code SampleProfileWriterExtBinary::writeSections(
333  const StringMap<FunctionSamples> &ProfileMap) {
334  std::error_code EC;
335  if (SecLayout == DefaultLayout)
336  EC = writeDefaultLayout(ProfileMap);
337  else if (SecLayout == CtxSplitLayout)
338  EC = writeCtxSplitLayout(ProfileMap);
339  else
340  llvm_unreachable("Unsupported layout");
341  return EC;
342 }
343 
345  const StringMap<FunctionSamples> &ProfileMap) {
346  if (std::error_code EC = SampleProfileWriter::write(ProfileMap))
347  return EC;
348  if (std::error_code EC = writeFuncOffsetTable())
349  return EC;
351 }
352 
353 /// Write samples to a text file.
354 ///
355 /// Note: it may be tempting to implement this in terms of
356 /// FunctionSamples::print(). Please don't. The dump functionality is intended
357 /// for debugging and has no specified form.
358 ///
359 /// The format used here is more structured and deliberate because
360 /// it needs to be parsed by the SampleProfileReaderText class.
362  auto &OS = *OutputStream;
363  OS << S.getNameWithContext(true) << ":" << S.getTotalSamples();
364  if (Indent == 0)
365  OS << ":" << S.getHeadSamples();
366  OS << "\n";
367 
368  SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
369  for (const auto &I : SortedSamples.get()) {
370  LineLocation Loc = I->first;
371  const SampleRecord &Sample = I->second;
372  OS.indent(Indent + 1);
373  if (Loc.Discriminator == 0)
374  OS << Loc.LineOffset << ": ";
375  else
376  OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
377 
378  OS << Sample.getSamples();
379 
380  for (const auto &J : Sample.getSortedCallTargets())
381  OS << " " << J.first << ":" << J.second;
382  OS << "\n";
383  }
384 
386  S.getCallsiteSamples());
387  Indent += 1;
388  for (const auto &I : SortedCallsiteSamples.get())
389  for (const auto &FS : I->second) {
390  LineLocation Loc = I->first;
391  const FunctionSamples &CalleeSamples = FS.second;
392  OS.indent(Indent);
393  if (Loc.Discriminator == 0)
394  OS << Loc.LineOffset << ": ";
395  else
396  OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
397  if (std::error_code EC = writeSample(CalleeSamples))
398  return EC;
399  }
400  Indent -= 1;
401 
402  if (Indent == 0) {
404  OS.indent(Indent + 1);
405  OS << "!CFGChecksum: " << S.getFunctionHash() << "\n";
406  }
407  }
408 
410 }
411 
413  const auto &ret = NameTable.find(FName);
414  if (ret == NameTable.end())
416  encodeULEB128(ret->second, *OutputStream);
418 }
419 
421  NameTable.insert(std::make_pair(FName, 0));
422 }
423 
425  // Add all the names in indirect call targets.
426  for (const auto &I : S.getBodySamples()) {
427  const SampleRecord &Sample = I.second;
428  for (const auto &J : Sample.getCallTargets())
429  addName(J.first());
430  }
431 
432  // Recursively add all the names for inlined callsites.
433  for (const auto &J : S.getCallsiteSamples())
434  for (const auto &FS : J.second) {
435  const FunctionSamples &CalleeSamples = FS.second;
436  addName(CalleeSamples.getName());
437  addNames(CalleeSamples);
438  }
439 }
440 
441 void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) {
442  // Sort the names to make NameTable deterministic.
443  for (const auto &I : NameTable)
444  V.insert(I.first);
445  int i = 0;
446  for (const StringRef &N : V)
447  NameTable[N] = i++;
448 }
449 
451  auto &OS = *OutputStream;
452  std::set<StringRef> V;
454 
455  // Write out the name table.
456  encodeULEB128(NameTable.size(), OS);
457  for (auto N : V) {
458  OS << N;
459  encodeULEB128(0, OS);
460  }
462 }
463 
465  auto &OS = *OutputStream;
466 
467  // Fill the slot remembered by TableOffset with the offset of FuncOffsetTable.
468  auto &OFS = static_cast<raw_fd_ostream &>(OS);
469  uint64_t FuncOffsetTableStart = OS.tell();
470  if (OFS.seek(TableOffset) == (uint64_t)-1)
473  Writer.write(FuncOffsetTableStart);
474  if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1)
476 
477  // Write out the table size.
478  encodeULEB128(FuncOffsetTable.size(), OS);
479 
480  // Write out FuncOffsetTable.
481  for (auto entry : FuncOffsetTable) {
482  writeNameIdx(entry.first);
483  encodeULEB128(entry.second, OS);
484  }
486 }
487 
489  auto &OS = *OutputStream;
490  std::set<StringRef> V;
492 
493  // Write out the name table.
494  encodeULEB128(NameTable.size(), OS);
495  for (auto N : V) {
496  encodeULEB128(MD5Hash(N), OS);
497  }
499 }
500 
501 std::error_code
503  auto &OS = *OutputStream;
504  // Write file magic identifier.
506  encodeULEB128(SPVersion(), OS);
508 }
509 
511  const StringMap<FunctionSamples> &ProfileMap) {
513 
514  computeSummary(ProfileMap);
515  if (auto EC = writeSummary())
516  return EC;
517 
518  // Generate the name table for all the functions referenced in the profile.
519  for (const auto &I : ProfileMap) {
520  addName(I.first());
521  addNames(I.second);
522  }
523 
524  writeNameTable();
526 }
527 
529  for (auto &Entry : SectionHdrLayout)
531 }
532 
535 }
536 
537 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
539 
540  Writer.write(static_cast<uint64_t>(SectionHdrLayout.size()));
541  SecHdrTableOffset = OutputStream->tell();
542  for (uint32_t i = 0; i < SectionHdrLayout.size(); i++) {
543  Writer.write(static_cast<uint64_t>(-1));
544  Writer.write(static_cast<uint64_t>(-1));
545  Writer.write(static_cast<uint64_t>(-1));
546  Writer.write(static_cast<uint64_t>(-1));
547  }
548 }
549 
550 std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
551  auto &OFS = static_cast<raw_fd_ostream &>(*OutputStream);
552  uint64_t Saved = OutputStream->tell();
553 
554  // Set OutputStream to the location saved in SecHdrTableOffset.
555  if (OFS.seek(SecHdrTableOffset) == (uint64_t)-1)
558 
559  assert(SecHdrTable.size() == SectionHdrLayout.size() &&
560  "SecHdrTable entries doesn't match SectionHdrLayout");
561  SmallVector<uint32_t, 16> IndexMap(SecHdrTable.size(), -1);
562  for (uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
563  IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
564  }
565 
566  // Write the section header table in the order specified in
567  // SectionHdrLayout. SectionHdrLayout specifies the sections
568  // order in which profile reader expect to read, so the section
569  // header table should be written in the order in SectionHdrLayout.
570  // Note that the section order in SecHdrTable may be different
571  // from the order in SectionHdrLayout, for example, SecFuncOffsetTable
572  // needs to be computed after SecLBRProfile (the order in SecHdrTable),
573  // but it needs to be read before SecLBRProfile (the order in
574  // SectionHdrLayout). So we use IndexMap above to switch the order.
575  for (uint32_t LayoutIdx = 0; LayoutIdx < SectionHdrLayout.size();
576  LayoutIdx++) {
577  assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
578  "Incorrect LayoutIdx in SecHdrTable");
579  auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
580  Writer.write(static_cast<uint64_t>(Entry.Type));
581  Writer.write(static_cast<uint64_t>(Entry.Flags));
582  Writer.write(static_cast<uint64_t>(Entry.Offset));
583  Writer.write(static_cast<uint64_t>(Entry.Size));
584  }
585 
586  // Reset OutputStream.
587  if (OFS.seek(Saved) == (uint64_t)-1)
589 
591 }
592 
593 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
594  const StringMap<FunctionSamples> &ProfileMap) {
595  auto &OS = *OutputStream;
596  FileStart = OS.tell();
598 
599  allocSecHdrTable();
601 }
602 
604  const StringMap<FunctionSamples> &ProfileMap) {
606  if (auto EC = SampleProfileWriterBinary::writeHeader(ProfileMap))
607  return EC;
608 
609  // Reserve a slot for the offset of function offset table. The slot will
610  // be populated with the offset of FuncOffsetTable later.
611  TableOffset = OutputStream->tell();
612  Writer.write(static_cast<uint64_t>(-2));
614 }
615 
617  auto &OS = *OutputStream;
618  encodeULEB128(Summary->getTotalCount(), OS);
619  encodeULEB128(Summary->getMaxCount(), OS);
620  encodeULEB128(Summary->getMaxFunctionCount(), OS);
621  encodeULEB128(Summary->getNumCounts(), OS);
622  encodeULEB128(Summary->getNumFunctions(), OS);
623  std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
624  encodeULEB128(Entries.size(), OS);
625  for (auto Entry : Entries) {
626  encodeULEB128(Entry.Cutoff, OS);
627  encodeULEB128(Entry.MinCount, OS);
628  encodeULEB128(Entry.NumCounts, OS);
629  }
631 }
633  auto &OS = *OutputStream;
634 
635  if (std::error_code EC = writeNameIdx(S.getNameWithContext(true)))
636  return EC;
637 
638  encodeULEB128(S.getTotalSamples(), OS);
639 
640  // Emit all the body samples.
641  encodeULEB128(S.getBodySamples().size(), OS);
642  for (const auto &I : S.getBodySamples()) {
643  LineLocation Loc = I.first;
644  const SampleRecord &Sample = I.second;
645  encodeULEB128(Loc.LineOffset, OS);
646  encodeULEB128(Loc.Discriminator, OS);
647  encodeULEB128(Sample.getSamples(), OS);
648  encodeULEB128(Sample.getCallTargets().size(), OS);
649  for (const auto &J : Sample.getSortedCallTargets()) {
650  StringRef Callee = J.first;
651  uint64_t CalleeSamples = J.second;
652  if (std::error_code EC = writeNameIdx(Callee))
653  return EC;
654  encodeULEB128(CalleeSamples, OS);
655  }
656  }
657 
658  // Recursively emit all the callsite samples.
659  uint64_t NumCallsites = 0;
660  for (const auto &J : S.getCallsiteSamples())
661  NumCallsites += J.second.size();
662  encodeULEB128(NumCallsites, OS);
663  for (const auto &J : S.getCallsiteSamples())
664  for (const auto &FS : J.second) {
665  LineLocation Loc = J.first;
666  const FunctionSamples &CalleeSamples = FS.second;
667  encodeULEB128(Loc.LineOffset, OS);
668  encodeULEB128(Loc.Discriminator, OS);
669  if (std::error_code EC = writeBody(CalleeSamples))
670  return EC;
671  }
672 
674 }
675 
676 /// Write samples of a top-level function to a binary file.
677 ///
678 /// \returns true if the samples were written successfully, false otherwise.
679 std::error_code
681  encodeULEB128(S.getHeadSamples(), *OutputStream);
682  return writeBody(S);
683 }
684 
685 std::error_code
687  uint64_t Offset = OutputStream->tell();
688  StringRef Name = S.getName();
690  encodeULEB128(S.getHeadSamples(), *OutputStream);
691  return writeBody(S);
692 }
693 
694 /// Create a sample profile file writer based on the specified format.
695 ///
696 /// \param Filename The file to create.
697 ///
698 /// \param Format Encoding format for the profile file.
699 ///
700 /// \returns an error code indicating the status of the created writer.
703  std::error_code EC;
704  std::unique_ptr<raw_ostream> OS;
705  if (Format == SPF_Binary || Format == SPF_Ext_Binary ||
707  OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None));
708  else
709  OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text));
710  if (EC)
711  return EC;
712 
713  return create(OS, Format);
714 }
715 
716 /// Create a sample profile stream writer based on the specified format.
717 ///
718 /// \param OS The output stream to store the profile data to.
719 ///
720 /// \param Format Encoding format for the profile file.
721 ///
722 /// \returns an error code indicating the status of the created writer.
724 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
725  SampleProfileFormat Format) {
726  std::error_code EC;
727  std::unique_ptr<SampleProfileWriter> Writer;
728 
729  if (Format == SPF_Binary)
730  Writer.reset(new SampleProfileWriterRawBinary(OS));
731  else if (Format == SPF_Ext_Binary)
732  Writer.reset(new SampleProfileWriterExtBinary(OS));
733  else if (Format == SPF_Compact_Binary)
734  Writer.reset(new SampleProfileWriterCompactBinary(OS));
735  else if (Format == SPF_Text)
736  Writer.reset(new SampleProfileWriterText(OS));
737  else if (Format == SPF_GCC)
739  else
741 
742  if (EC)
743  return EC;
744 
745  Writer->Format = Format;
746  return std::move(Writer);
747 }
748 
750  const StringMap<FunctionSamples> &ProfileMap) {
752  Summary = Builder.computeSummaryForProfiles(ProfileMap);
753 }
i
i
Definition: README.txt:29
llvm::sampleprof::SampleProfileWriter::OutputStream
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
Definition: SampleProfWriter.h:88
llvm::sampleprof::SampleRecord::getSamples
uint64_t getSamples() const
Definition: SampleProf.h:335
llvm::raw_ostream::tell
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:132
llvm::sampleprof::SampleProfileWriter::writeHeader
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap)=0
Write a file header for the profile file.
llvm::sampleprof::SampleProfileWriterBinary::NameTable
MapVector< StringRef, uint32_t > NameTable
Definition: SampleProfWriter.h:143
Compression.h
llvm::sampleprof::SampleProfileWriterText
Sample-based profile writer (text format).
Definition: SampleProfWriter.h:101
llvm::sampleprof::SampleProfileWriter::create
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
Definition: SampleProfWriter.cpp:702
llvm::sampleprof::CtxSplitLayout
@ CtxSplitLayout
Definition: SampleProfWriter.h:37
llvm
This class represents lattice values for constants.
Definition: AllocatorList.h:23
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeNameTable
virtual std::error_code writeNameTable() override
Definition: SampleProfWriter.cpp:183
llvm::sampleprof::SampleSorter::get
const SamplesWithLocList & get() const
Definition: SampleProf.h:938
FileSystem.h
llvm::sys::fs::OF_None
@ OF_None
Definition: FileSystem.h:742
llvm::sampleprof::FunctionSamples::ProfileIsProbeBased
static bool ProfileIsProbeBased
Definition: SampleProf.h:844
llvm::sampleprof::SampleProfileWriter::Format
SampleProfileFormat Format
Profile format.
Definition: SampleProfWriter.h:97
llvm::sampleprof::SampleProfileWriterBinary::writeBody
std::error_code writeBody(const FunctionSamples &S)
Definition: SampleProfWriter.cpp:632
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeFuncOffsetTable
std::error_code writeFuncOffsetTable()
Definition: SampleProfWriter.cpp:156
StringRef.h
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::SmallVector< uint32_t, 16 >
llvm::sampleprof::ProfileSymbolList::size
unsigned size()
Definition: SampleProf.h:967
llvm::sampleprof::DefaultLayout
@ DefaultLayout
Definition: SampleProfWriter.h:33
llvm::sampleprof::hasSecFlag
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:238
Saved
Fixup Statepoint Caller Saved
Definition: FixupStatepointCallerSaved.cpp:93
llvm::sampleprof::SPF_GCC
@ SPF_GCC
Definition: SampleProf.h:91
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::sampleprof::SampleProfileWriterBinary::addName
void addName(StringRef FName)
Definition: SampleProfWriter.cpp:420
llvm::sampleprof::SecFuncOffsetTable
@ SecFuncOffsetTable
Definition: SampleProf.h:123
llvm::sampleprof::addSecFlag
static void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
Definition: SampleProf.h:222
llvm::sampleprof::SecCommonFlags::SecFlagCompress
@ SecFlagCompress
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeOneSection
virtual std::error_code writeOneSection(SecType Type, uint32_t LayoutIdx, const StringMap< FunctionSamples > &ProfileMap)
Definition: SampleProfWriter.cpp:221
ret
to esp esp setne al movzbw ax esp setg cl movzbw cx cmove cx cl jne LBB1_2 esp ret(also really horrible code on ppc). This is due to the expand code for 64-bit compares. GCC produces multiple branches
llvm::sampleprof::SampleProfileWriter::writeFuncProfiles
virtual std::error_code writeFuncProfiles(const StringMap< FunctionSamples > &ProfileMap)
Definition: SampleProfWriter.cpp:44
llvm::sampleprof::SampleProfileWriterCompactBinary::writeFuncOffsetTable
std::error_code writeFuncOffsetTable()
Definition: SampleProfWriter.cpp:464
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::sampleprof::FunctionSamples::getName
StringRef getName() const
Return the function name.
Definition: SampleProf.h:752
llvm::X86AS::FS
@ FS
Definition: X86.h:182
llvm::sampleprof::SampleProfileWriterExtBinaryBase::setToCompressAllSections
virtual void setToCompressAllSections() override
Definition: SampleProfWriter.cpp:528
llvm::support::endian::Writer
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:51
ProfileCommon.h
llvm::sampleprof::SampleProfileWriterBinary::writeSample
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
Definition: SampleProfWriter.cpp:680
llvm::SampleProfileSummaryBuilder
Definition: ProfileCommon.h:86
llvm::sampleprof::SPF_Binary
@ SPF_Binary
Definition: SampleProf.h:93
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeSample
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
Definition: SampleProfWriter.cpp:148
llvm::sampleprof::LineLocation::Discriminator
uint32_t Discriminator
Definition: SampleProf.h:274
splitProfileMapToTwo
static void splitProfileMapToTwo(const StringMap< FunctionSamples > &ProfileMap, StringMap< FunctionSamples > &ContextProfileMap, StringMap< FunctionSamples > &NoContextProfileMap)
Definition: SampleProfWriter.cpp:290
llvm::sampleprof::LineLocation::LineOffset
uint32_t LineOffset
Definition: SampleProf.h:273
llvm::sampleprof::SampleProfileWriterBinary::writeHeader
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
Definition: SampleProfWriter.cpp:510
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeCustomSection
virtual std::error_code writeCustomSection(SecType Type)=0
llvm::sampleprof::SampleProfileWriter::Summary
std::unique_ptr< ProfileSummary > Summary
Profile summary.
Definition: SampleProfWriter.h:91
llvm::sampleprof::SecFuncMetadataFlags::SecFlagIsProbeBased
@ SecFlagIsProbeBased
llvm::sampleprof_error::unsupported_writing_format
@ unsupported_writing_format
llvm::sampleprof::SampleProfileFormat
SampleProfileFormat
Definition: SampleProf.h:87
MD5.h
llvm::sampleprof::SampleProfileWriterBinary::addNames
void addNames(const FunctionSamples &S)
Definition: SampleProfWriter.cpp:424
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::zlib::BestSizeCompression
static constexpr int BestSizeCompression
Definition: Compression.h:28
llvm::support::little
@ little
Definition: Endian.h:27
llvm::StringMap::insert
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:276
llvm::sampleprof::SampleProfileWriterRawBinary
Definition: SampleProfWriter.h:154
llvm::sampleprof::SampleProfileWriterBinary::writeNameTable
virtual std::error_code writeNameTable()
Definition: SampleProfWriter.cpp:450
SampleProf.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::sampleprof::SampleProfileWriter::writeSample
virtual std::error_code writeSample(const FunctionSamples &S)=0
Write sample profiles in S.
llvm::sampleprof::SampleProfileWriterCompactBinary::writeHeader
virtual std::error_code writeHeader(const StringMap< FunctionSamples > &ProfileMap) override
Write a file header for the profile file.
Definition: SampleProfWriter.cpp:603
llvm::sampleprof::SPMagic
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition: SampleProf.h:96
llvm::zlib::compress
Error compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
Definition: Compression.cpp:49
llvm::sampleprof::SampleProfileWriterExtBinaryBase::SectionHdrLayout
SmallVector< SecHdrTableEntry, 8 > SectionHdrLayout
Definition: SampleProfWriter.h:278
llvm::sampleprof::SampleProfileWriterCompactBinary::TableOffset
uint64_t TableOffset
The offset of the slot to be filled with the offset of FuncOffsetTable towards profile start.
Definition: SampleProfWriter.h:390
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::SmallString< 128 >
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeSections
virtual std::error_code writeSections(const StringMap< FunctionSamples > &ProfileMap)=0
llvm::sampleprof::SampleProfileWriterCompactBinary::writeNameTable
virtual std::error_code writeNameTable() override
Definition: SampleProfWriter.cpp:488
llvm::sampleprof::SampleRecord::getCallTargets
const CallTargetMap & getCallTargets() const
Definition: SampleProf.h:336
llvm::sampleprof::SampleProfileWriterCompactBinary::FuncOffsetTable
MapVector< StringRef, uint64_t > FuncOffsetTable
The table mapping from function name to the offset of its FunctionSample towards profile start.
Definition: SampleProfWriter.h:387
llvm::sampleprof::SampleProfileWriterExtBinaryBase::markSectionStart
uint64_t markSectionStart(SecType Type, uint32_t LayoutIdx)
Return the current position and prepare to use it as the start position of a section given the sectio...
Definition: SampleProfWriter.cpp:81
llvm::sampleprof::SPF_Compact_Binary
@ SPF_Compact_Binary
Definition: SampleProf.h:90
llvm::sampleprof::SecProfSummary
@ SecProfSummary
Definition: SampleProf.h:120
llvm::sys::fs::OF_Text
@ OF_Text
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:747
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::sampleprof::SPF_Ext_Binary
@ SPF_Ext_Binary
Definition: SampleProf.h:92
ErrorOr.h
llvm::sampleprof::SecLBRProfile
@ SecLBRProfile
Definition: SampleProf.h:127
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::sampleprof::SampleProfileWriterBinary::stablizeNameTable
void stablizeNameTable(std::set< StringRef > &V)
Definition: SampleProfWriter.cpp:441
llvm::sampleprof::SampleRecord::getSortedCallTargets
const SortedCallTargetSet getSortedCallTargets() const
Definition: SampleProf.h:337
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::sampleprof::SampleProfileWriter::computeSummary
void computeSummary(const StringMap< FunctionSamples > &ProfileMap)
Compute summary for this profile.
Definition: SampleProfWriter.cpp:749
llvm::sampleprof::SampleProfileWriter::write
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap)
Write all the sample profiles in the given map of samples.
Definition: SampleProfWriter.cpp:67
llvm::sampleprof::SampleProfileWriterExtBinaryBase::addSectionFlag
void addSectionFlag(SecType Type, SecFlagType Flag)
Definition: SampleProfWriter.h:236
llvm::codeview::CompileSym2Flags::EC
@ EC
llvm::sampleprof::SampleProfileWriterText::writeSample
std::error_code writeSample(const FunctionSamples &S) override
Write samples to a text file.
Definition: SampleProfWriter.cpp:361
llvm::sampleprof::SampleProfileWriterBinary::writeNameIdx
std::error_code writeNameIdx(StringRef FName)
Definition: SampleProfWriter.cpp:412
llvm::zlib::isAvailable
bool isAvailable()
Definition: Compression.cpp:47
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:93
llvm::sampleprof::LineLocation
Represents the relative location of an instruction.
Definition: SampleProf.h:254
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:643
llvm::sampleprof::SampleProfileWriterExtBinaryBase::addNewSection
std::error_code addNewSection(SecType Sec, uint32_t LayoutIdx, uint64_t SectionStart)
Add a new section into section header table given the section type Type, its position LayoutIdx in Se...
Definition: SampleProfWriter.cpp:116
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::sampleprof::SampleSorter
Sort a LocationT->SampleT map by LocationT.
Definition: SampleProf.h:925
llvm::support::endian::Writer::write
void write(ArrayRef< value_type > Val)
Definition: EndianStream.h:55
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::sampleprof_error::unrecognized_format
@ unrecognized_format
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeFuncMetadata
std::error_code writeFuncMetadata(const StringMap< FunctionSamples > &Profiles)
Definition: SampleProfWriter.cpp:171
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::sampleprof::SPF_Text
@ SPF_Text
Definition: SampleProf.h:89
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:432
llvm::sampleprof::ProfileSymbolList::toCompress
bool toCompress()
Definition: SampleProf.h:970
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:205
llvm::sampleprof_error::ostream_seek_unsupported
@ ostream_seek_unsupported
llvm::sampleprof::SampleProfileWriterExtBinary
Definition: SampleProfWriter.h:322
StringSet.h
llvm::stable_sort
void stable_sort(R &&Range)
Definition: STLExtras.h:1633
llvm::sampleprof::ProfileSymbolList::write
std::error_code write(raw_ostream &OS)
Definition: SampleProf.cpp:316
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
EndianStream.h
llvm::sampleprof::SPVersion
static uint64_t SPVersion()
Definition: SampleProf.h:113
llvm::sampleprof::SampleProfileWriterBinary::writeSummary
std::error_code writeSummary()
Definition: SampleProfWriter.cpp:616
llvm::sampleprof::SampleProfileWriterExtBinaryBase::setToCompressSection
void setToCompressSection(SecType Type)
Definition: SampleProfWriter.cpp:533
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::sampleprof::SecProfileSymbolList
@ SecProfileSymbolList
Definition: SampleProf.h:122
llvm::sampleprof::SampleProfileWriterExtBinaryBase::SecLBRProfileStart
uint64_t SecLBRProfileStart
Definition: SampleProfWriter.h:284
llvm::sampleprof::SecType
SecType
Definition: SampleProf.h:118
llvm::sampleprof::SampleRecord
Representation of a single sample record.
Definition: SampleProf.h:289
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeProfileSymbolListSection
std::error_code writeProfileSymbolListSection()
Definition: SampleProfWriter.cpp:213
llvm::sampleprof::SampleProfileWriterCompactBinary::write
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap) override
Write all the sample profiles in the given map of samples.
Definition: SampleProfWriter.cpp:344
llvm::sampleprof_error::compress_failed
@ compress_failed
llvm::sampleprof_error::zlib_unavailable
@ zlib_unavailable
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:259
llvm::sampleprof_error::success
@ success
llvm::sampleprof::SampleProfileWriterCompactBinary::writeSample
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
Definition: SampleProfWriter.cpp:686
llvm::sampleprof::SecCommonFlags::SecFlagFlat
@ SecFlagFlat
SampleProfWriter.h
N
#define N
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::sampleprof::SampleProfileWriterExtBinaryBase::write
virtual std::error_code write(const StringMap< FunctionSamples > &ProfileMap) override
Write all the sample profiles in the given map of samples.
Definition: SampleProfWriter.cpp:131
llvm::encodeULEB128
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
llvm::sampleprof::SecNameTable
@ SecNameTable
Definition: SampleProf.h:121
llvm::sampleprof::SampleProfileWriterBinary::writeMagicIdent
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
Definition: SampleProfWriter.cpp:502
llvm::sampleprof::SecFuncMetadata
@ SecFuncMetadata
Definition: SampleProf.h:124
llvm::sampleprof_error::truncated_name_table
@ truncated_name_table
raw_ostream.h
Endian.h
llvm::sampleprof::SampleProfileWriterCompactBinary
Definition: SampleProfWriter.h:376
llvm::ProfileSummaryBuilder::DefaultCutoffs
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
llvm::sampleprof::SampleProfileWriterExtBinaryBase::SecLayout
SectionLayout SecLayout
Definition: SampleProfWriter.h:273
entry
print Instructions which execute on loop entry
Definition: MustExecute.cpp:339
llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeNameTableSection
std::error_code writeNameTableSection(const StringMap< FunctionSamples > &ProfileMap)
Definition: SampleProfWriter.cpp:201
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