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 
20 #include "llvm/ProfileData/SampleProfWriter.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ProfileData/ProfileCommon.h"
23 #include "llvm/ProfileData/SampleProf.h"
24 #include "llvm/Support/Compression.h"
25 #include "llvm/Support/Endian.h"
26 #include "llvm/Support/EndianStream.h"
27 #include "llvm/Support/ErrorOr.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/LEB128.h"
30 #include "llvm/Support/MD5.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm>
33 #include <cstdint>
34 #include <memory>
35 #include <set>
36 #include <system_error>
37 #include <utility>
38 #include <vector>
39 
40 using namespace llvm;
41 using namespace sampleprof;
42 
43 std::error_code SampleProfileWriter::writeFuncProfiles(
44     const StringMap<FunctionSamples> &ProfileMap) {
45   // Sort the ProfileMap by total samples.
46   typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
47   std::vector<NameFunctionSamples> V;
48   for (const auto &I : ProfileMap)
49     V.push_back(std::make_pair(I.getKey(), &I.second));
50 
51   llvm::stable_sort(
52       V, [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
53         if (A.second->getTotalSamples() == B.second->getTotalSamples())
54           return A.first > B.first;
55         return A.second->getTotalSamples() > B.second->getTotalSamples();
56       });
57 
58   for (const auto &I : V) {
59     if (std::error_code EC = writeSample(*I.second))
60       return EC;
61   }
62   return sampleprof_error::success;
63 }
64 
65 std::error_code
66 SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
67   if (std::error_code EC = writeHeader(ProfileMap))
68     return EC;
69 
70   if (std::error_code EC = writeFuncProfiles(ProfileMap))
71     return EC;
72 
73   return sampleprof_error::success;
74 }
75 
76 /// Return the current position and prepare to use it as the start
77 /// position of a section given the section type \p Type and its position
78 /// \p LayoutIdx in SectionHdrLayout.
79 uint64_t
80 SampleProfileWriterExtBinaryBase::markSectionStart(SecType Type,
81                                                    uint32_t LayoutIdx) {
82   uint64_t SectionStart = OutputStream->tell();
83   assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
84   const auto &Entry = SectionHdrLayout[LayoutIdx];
85   assert(Entry.Type == Type && "Unexpected section type");
86   // Use LocalBuf as a temporary output for writting data.
87   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
88     LocalBufStream.swap(OutputStream);
89   return SectionStart;
90 }
91 
92 std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
93   if (!llvm::zlib::isAvailable())
94     return sampleprof_error::zlib_unavailable;
95   std::string &UncompressedStrings =
96       static_cast<raw_string_ostream *>(LocalBufStream.get())->str();
97   if (UncompressedStrings.size() == 0)
98     return sampleprof_error::success;
99   auto &OS = *OutputStream;
100   SmallString<128> CompressedStrings;
101   llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
102                                  zlib::BestSizeCompression);
103   if (E)
104     return sampleprof_error::compress_failed;
105   encodeULEB128(UncompressedStrings.size(), OS);
106   encodeULEB128(CompressedStrings.size(), OS);
107   OS << CompressedStrings.str();
108   UncompressedStrings.clear();
109   return sampleprof_error::success;
110 }
111 
112 /// Add a new section into section header table given the section type
113 /// \p Type, its position \p LayoutIdx in SectionHdrLayout and the
114 /// location \p SectionStart where the section should be written to.
115 std::error_code SampleProfileWriterExtBinaryBase::addNewSection(
116     SecType Type, uint32_t LayoutIdx, uint64_t SectionStart) {
117   assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
118   const auto &Entry = SectionHdrLayout[LayoutIdx];
119   assert(Entry.Type == Type && "Unexpected section type");
120   if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress)) {
121     LocalBufStream.swap(OutputStream);
122     if (std::error_code EC = compressAndOutput())
123       return EC;
124   }
125   SecHdrTable.push_back({Type, Entry.Flags, SectionStart - FileStart,
126                          OutputStream->tell() - SectionStart, LayoutIdx});
127   return sampleprof_error::success;
128 }
129 
130 std::error_code SampleProfileWriterExtBinaryBase::write(
131     const StringMap<FunctionSamples> &ProfileMap) {
132   if (std::error_code EC = writeHeader(ProfileMap))
133     return EC;
134 
135   std::string LocalBuf;
136   LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
137   if (std::error_code EC = writeSections(ProfileMap))
138     return EC;
139 
140   if (std::error_code EC = writeSecHdrTable())
141     return EC;
142 
143   return sampleprof_error::success;
144 }
145 
146 std::error_code
147 SampleProfileWriterExtBinaryBase::writeSample(const FunctionSamples &S) {
148   uint64_t Offset = OutputStream->tell();
149   StringRef Name = S.getName();
150   FuncOffsetTable[Name] = Offset - SecLBRProfileStart;
151   encodeULEB128(S.getHeadSamples(), *OutputStream);
152   return writeBody(S);
153 }
154 
155 std::error_code SampleProfileWriterExtBinaryBase::writeFuncOffsetTable() {
156   auto &OS = *OutputStream;
157 
158   // Write out the table size.
159   encodeULEB128(FuncOffsetTable.size(), OS);
160 
161   // Write out FuncOffsetTable.
162   for (auto entry : FuncOffsetTable) {
163     writeNameIdx(entry.first);
164     encodeULEB128(entry.second, OS);
165   }
166   FuncOffsetTable.clear();
167   return sampleprof_error::success;
168 }
169 
170 std::error_code SampleProfileWriterExtBinaryBase::writeFuncMetadata(
171     const StringMap<FunctionSamples> &Profiles) {
172   if (!FunctionSamples::ProfileIsProbeBased)
173     return sampleprof_error::success;
174   auto &OS = *OutputStream;
175   for (const auto &Entry : Profiles) {
176     writeNameIdx(Entry.first());
177     encodeULEB128(Entry.second.getFunctionHash(), OS);
178   }
179   return sampleprof_error::success;
180 }
181 
182 std::error_code SampleProfileWriterExtBinaryBase::writeNameTable() {
183   if (!UseMD5)
184     return SampleProfileWriterBinary::writeNameTable();
185 
186   auto &OS = *OutputStream;
187   std::set<StringRef> V;
188   stablizeNameTable(V);
189 
190   // Write out the MD5 name table. We wrote unencoded MD5 so reader can
191   // retrieve the name using the name index without having to read the
192   // whole name table.
193   encodeULEB128(NameTable.size(), OS);
194   support::endian::Writer Writer(OS, support::little);
195   for (auto N : V)
196     Writer.write(MD5Hash(N));
197   return sampleprof_error::success;
198 }
199 
200 std::error_code SampleProfileWriterExtBinaryBase::writeNameTableSection(
201     const StringMap<FunctionSamples> &ProfileMap) {
202   for (const auto &I : ProfileMap) {
203     addName(I.first());
204     addNames(I.second);
205   }
206   if (auto EC = writeNameTable())
207     return EC;
208   return sampleprof_error::success;
209 }
210 
211 std::error_code
212 SampleProfileWriterExtBinaryBase::writeProfileSymbolListSection() {
213   if (ProfSymList && ProfSymList->size() > 0)
214     if (std::error_code EC = ProfSymList->write(*OutputStream))
215       return EC;
216 
217   return sampleprof_error::success;
218 }
219 
220 std::error_code SampleProfileWriterExtBinaryBase::writeOneSection(
221     SecType Type, uint32_t LayoutIdx,
222     const StringMap<FunctionSamples> &ProfileMap) {
223   // The setting of SecFlagCompress should happen before markSectionStart.
224   if (Type == SecProfileSymbolList && ProfSymList && ProfSymList->toCompress())
225     setToCompressSection(SecProfileSymbolList);
226   if (Type == SecFuncMetadata && FunctionSamples::ProfileIsProbeBased)
227     addSectionFlag(SecFuncMetadata, SecFuncMetadataFlags::SecFlagIsProbeBased);
228 
229   uint64_t SectionStart = markSectionStart(Type, LayoutIdx);
230   switch (Type) {
231   case SecProfSummary:
232     computeSummary(ProfileMap);
233     if (auto EC = writeSummary())
234       return EC;
235     break;
236   case SecNameTable:
237     if (auto EC = writeNameTableSection(ProfileMap))
238       return EC;
239     break;
240   case SecLBRProfile:
241     SecLBRProfileStart = OutputStream->tell();
242     if (std::error_code EC = writeFuncProfiles(ProfileMap))
243       return EC;
244     break;
245   case SecFuncOffsetTable:
246     if (auto EC = writeFuncOffsetTable())
247       return EC;
248     break;
249   case SecFuncMetadata:
250     if (std::error_code EC = writeFuncMetadata(ProfileMap))
251       return EC;
252     break;
253   case SecProfileSymbolList:
254     if (auto EC = writeProfileSymbolListSection())
255       return EC;
256     break;
257   default:
258     if (auto EC = writeCustomSection(Type))
259       return EC;
260     break;
261   }
262   if (std::error_code EC = addNewSection(Type, LayoutIdx, SectionStart))
263     return EC;
264   return sampleprof_error::success;
265 }
266 
267 std::error_code SampleProfileWriterExtBinary::writeSections(
268     const StringMap<FunctionSamples> &ProfileMap) {
269   // The const indices passed to writeOneSection below are specifying the
270   // positions of the sections in SectionHdrLayout. Look at
271   // initSectionHdrLayout to find out where each section is located in
272   // SectionHdrLayout.
273   if (auto EC = writeOneSection(SecProfSummary, 0, ProfileMap))
274     return EC;
275   if (auto EC = writeOneSection(SecNameTable, 1, ProfileMap))
276     return EC;
277   if (auto EC = writeOneSection(SecLBRProfile, 3, ProfileMap))
278     return EC;
279   if (auto EC = writeOneSection(SecProfileSymbolList, 4, ProfileMap))
280     return EC;
281   if (auto EC = writeOneSection(SecFuncOffsetTable, 2, ProfileMap))
282     return EC;
283   if (auto EC = writeOneSection(SecFuncMetadata, 5, ProfileMap))
284     return EC;
285   return sampleprof_error::success;
286 }
287 
288 std::error_code SampleProfileWriterCompactBinary::write(
289     const StringMap<FunctionSamples> &ProfileMap) {
290   if (std::error_code EC = SampleProfileWriter::write(ProfileMap))
291     return EC;
292   if (std::error_code EC = writeFuncOffsetTable())
293     return EC;
294   return sampleprof_error::success;
295 }
296 
297 /// Write samples to a text file.
298 ///
299 /// Note: it may be tempting to implement this in terms of
300 /// FunctionSamples::print().  Please don't.  The dump functionality is intended
301 /// for debugging and has no specified form.
302 ///
303 /// The format used here is more structured and deliberate because
304 /// it needs to be parsed by the SampleProfileReaderText class.
305 std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
306   auto &OS = *OutputStream;
307   if (FunctionSamples::ProfileIsCS)
308     OS << "[" << S.getNameWithContext() << "]:" << S.getTotalSamples();
309   else
310     OS << S.getName() << ":" << S.getTotalSamples();
311   if (Indent == 0)
312     OS << ":" << S.getHeadSamples();
313   OS << "\n";
314 
315   SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
316   for (const auto &I : SortedSamples.get()) {
317     LineLocation Loc = I->first;
318     const SampleRecord &Sample = I->second;
319     OS.indent(Indent + 1);
320     if (Loc.Discriminator == 0)
321       OS << Loc.LineOffset << ": ";
322     else
323       OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
324 
325     OS << Sample.getSamples();
326 
327     for (const auto &J : Sample.getSortedCallTargets())
328       OS << " " << J.first << ":" << J.second;
329     OS << "\n";
330   }
331 
332   SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
333       S.getCallsiteSamples());
334   Indent += 1;
335   for (const auto &I : SortedCallsiteSamples.get())
336     for (const auto &FS : I->second) {
337       LineLocation Loc = I->first;
338       const FunctionSamples &CalleeSamples = FS.second;
339       OS.indent(Indent);
340       if (Loc.Discriminator == 0)
341         OS << Loc.LineOffset << ": ";
342       else
343         OS << Loc.LineOffset << "." << Loc.Discriminator << ": ";
344       if (std::error_code EC = writeSample(CalleeSamples))
345         return EC;
346     }
347   Indent -= 1;
348 
349   if (Indent == 0) {
350     if (FunctionSamples::ProfileIsProbeBased) {
351       OS.indent(Indent + 1);
352       OS << "!CFGChecksum: " << S.getFunctionHash() << "\n";
353     }
354   }
355 
356   return sampleprof_error::success;
357 }
358 
359 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
360   const auto &ret = NameTable.find(FName);
361   if (ret == NameTable.end())
362     return sampleprof_error::truncated_name_table;
363   encodeULEB128(ret->second, *OutputStream);
364   return sampleprof_error::success;
365 }
366 
367 void SampleProfileWriterBinary::addName(StringRef FName) {
368   NameTable.insert(std::make_pair(FName, 0));
369 }
370 
371 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
372   // Add all the names in indirect call targets.
373   for (const auto &I : S.getBodySamples()) {
374     const SampleRecord &Sample = I.second;
375     for (const auto &J : Sample.getCallTargets())
376       addName(J.first());
377   }
378 
379   // Recursively add all the names for inlined callsites.
380   for (const auto &J : S.getCallsiteSamples())
381     for (const auto &FS : J.second) {
382       const FunctionSamples &CalleeSamples = FS.second;
383       addName(CalleeSamples.getName());
384       addNames(CalleeSamples);
385     }
386 }
387 
388 void SampleProfileWriterBinary::stablizeNameTable(std::set<StringRef> &V) {
389   // Sort the names to make NameTable deterministic.
390   for (const auto &I : NameTable)
391     V.insert(I.first);
392   int i = 0;
393   for (const StringRef &N : V)
394     NameTable[N] = i++;
395 }
396 
397 std::error_code SampleProfileWriterBinary::writeNameTable() {
398   auto &OS = *OutputStream;
399   std::set<StringRef> V;
400   stablizeNameTable(V);
401 
402   // Write out the name table.
403   encodeULEB128(NameTable.size(), OS);
404   for (auto N : V) {
405     OS << N;
406     encodeULEB128(0, OS);
407   }
408   return sampleprof_error::success;
409 }
410 
411 std::error_code SampleProfileWriterCompactBinary::writeFuncOffsetTable() {
412   auto &OS = *OutputStream;
413 
414   // Fill the slot remembered by TableOffset with the offset of FuncOffsetTable.
415   auto &OFS = static_cast<raw_fd_ostream &>(OS);
416   uint64_t FuncOffsetTableStart = OS.tell();
417   if (OFS.seek(TableOffset) == (uint64_t)-1)
418     return sampleprof_error::ostream_seek_unsupported;
419   support::endian::Writer Writer(*OutputStream, support::little);
420   Writer.write(FuncOffsetTableStart);
421   if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1)
422     return sampleprof_error::ostream_seek_unsupported;
423 
424   // Write out the table size.
425   encodeULEB128(FuncOffsetTable.size(), OS);
426 
427   // Write out FuncOffsetTable.
428   for (auto entry : FuncOffsetTable) {
429     writeNameIdx(entry.first);
430     encodeULEB128(entry.second, OS);
431   }
432   return sampleprof_error::success;
433 }
434 
435 std::error_code SampleProfileWriterCompactBinary::writeNameTable() {
436   auto &OS = *OutputStream;
437   std::set<StringRef> V;
438   stablizeNameTable(V);
439 
440   // Write out the name table.
441   encodeULEB128(NameTable.size(), OS);
442   for (auto N : V) {
443     encodeULEB128(MD5Hash(N), OS);
444   }
445   return sampleprof_error::success;
446 }
447 
448 std::error_code
449 SampleProfileWriterBinary::writeMagicIdent(SampleProfileFormat Format) {
450   auto &OS = *OutputStream;
451   // Write file magic identifier.
452   encodeULEB128(SPMagic(Format), OS);
453   encodeULEB128(SPVersion(), OS);
454   return sampleprof_error::success;
455 }
456 
457 std::error_code SampleProfileWriterBinary::writeHeader(
458     const StringMap<FunctionSamples> &ProfileMap) {
459   writeMagicIdent(Format);
460 
461   computeSummary(ProfileMap);
462   if (auto EC = writeSummary())
463     return EC;
464 
465   // Generate the name table for all the functions referenced in the profile.
466   for (const auto &I : ProfileMap) {
467     addName(I.first());
468     addNames(I.second);
469   }
470 
471   writeNameTable();
472   return sampleprof_error::success;
473 }
474 
475 void SampleProfileWriterExtBinaryBase::setToCompressAllSections() {
476   for (auto &Entry : SectionHdrLayout)
477     addSecFlag(Entry, SecCommonFlags::SecFlagCompress);
478 }
479 
480 void SampleProfileWriterExtBinaryBase::setToCompressSection(SecType Type) {
481   addSectionFlag(Type, SecCommonFlags::SecFlagCompress);
482 }
483 
484 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
485   support::endian::Writer Writer(*OutputStream, support::little);
486 
487   Writer.write(static_cast<uint64_t>(SectionHdrLayout.size()));
488   SecHdrTableOffset = OutputStream->tell();
489   for (uint32_t i = 0; i < SectionHdrLayout.size(); i++) {
490     Writer.write(static_cast<uint64_t>(-1));
491     Writer.write(static_cast<uint64_t>(-1));
492     Writer.write(static_cast<uint64_t>(-1));
493     Writer.write(static_cast<uint64_t>(-1));
494   }
495 }
496 
497 std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
498   auto &OFS = static_cast<raw_fd_ostream &>(*OutputStream);
499   uint64_t Saved = OutputStream->tell();
500 
501   // Set OutputStream to the location saved in SecHdrTableOffset.
502   if (OFS.seek(SecHdrTableOffset) == (uint64_t)-1)
503     return sampleprof_error::ostream_seek_unsupported;
504   support::endian::Writer Writer(*OutputStream, support::little);
505 
506   assert(SecHdrTable.size() == SectionHdrLayout.size() &&
507          "SecHdrTable entries doesn't match SectionHdrLayout");
508   SmallVector<uint32_t, 16> IndexMap(SecHdrTable.size(), -1);
509   for (uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
510     IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
511   }
512 
513   // Write the section header table in the order specified in
514   // SectionHdrLayout. SectionHdrLayout specifies the sections
515   // order in which profile reader expect to read, so the section
516   // header table should be written in the order in SectionHdrLayout.
517   // Note that the section order in SecHdrTable may be different
518   // from the order in SectionHdrLayout, for example, SecFuncOffsetTable
519   // needs to be computed after SecLBRProfile (the order in SecHdrTable),
520   // but it needs to be read before SecLBRProfile (the order in
521   // SectionHdrLayout). So we use IndexMap above to switch the order.
522   for (uint32_t LayoutIdx = 0; LayoutIdx < SectionHdrLayout.size();
523        LayoutIdx++) {
524     assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
525            "Incorrect LayoutIdx in SecHdrTable");
526     auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
527     Writer.write(static_cast<uint64_t>(Entry.Type));
528     Writer.write(static_cast<uint64_t>(Entry.Flags));
529     Writer.write(static_cast<uint64_t>(Entry.Offset));
530     Writer.write(static_cast<uint64_t>(Entry.Size));
531   }
532 
533   // Reset OutputStream.
534   if (OFS.seek(Saved) == (uint64_t)-1)
535     return sampleprof_error::ostream_seek_unsupported;
536 
537   return sampleprof_error::success;
538 }
539 
540 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
541     const StringMap<FunctionSamples> &ProfileMap) {
542   auto &OS = *OutputStream;
543   FileStart = OS.tell();
544   writeMagicIdent(Format);
545 
546   allocSecHdrTable();
547   return sampleprof_error::success;
548 }
549 
550 std::error_code SampleProfileWriterCompactBinary::writeHeader(
551     const StringMap<FunctionSamples> &ProfileMap) {
552   support::endian::Writer Writer(*OutputStream, support::little);
553   if (auto EC = SampleProfileWriterBinary::writeHeader(ProfileMap))
554     return EC;
555 
556   // Reserve a slot for the offset of function offset table. The slot will
557   // be populated with the offset of FuncOffsetTable later.
558   TableOffset = OutputStream->tell();
559   Writer.write(static_cast<uint64_t>(-2));
560   return sampleprof_error::success;
561 }
562 
563 std::error_code SampleProfileWriterBinary::writeSummary() {
564   auto &OS = *OutputStream;
565   encodeULEB128(Summary->getTotalCount(), OS);
566   encodeULEB128(Summary->getMaxCount(), OS);
567   encodeULEB128(Summary->getMaxFunctionCount(), OS);
568   encodeULEB128(Summary->getNumCounts(), OS);
569   encodeULEB128(Summary->getNumFunctions(), OS);
570   std::vector<ProfileSummaryEntry> &Entries = Summary->getDetailedSummary();
571   encodeULEB128(Entries.size(), OS);
572   for (auto Entry : Entries) {
573     encodeULEB128(Entry.Cutoff, OS);
574     encodeULEB128(Entry.MinCount, OS);
575     encodeULEB128(Entry.NumCounts, OS);
576   }
577   return sampleprof_error::success;
578 }
579 std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
580   auto &OS = *OutputStream;
581 
582   if (std::error_code EC = writeNameIdx(S.getName()))
583     return EC;
584 
585   encodeULEB128(S.getTotalSamples(), OS);
586 
587   // Emit all the body samples.
588   encodeULEB128(S.getBodySamples().size(), OS);
589   for (const auto &I : S.getBodySamples()) {
590     LineLocation Loc = I.first;
591     const SampleRecord &Sample = I.second;
592     encodeULEB128(Loc.LineOffset, OS);
593     encodeULEB128(Loc.Discriminator, OS);
594     encodeULEB128(Sample.getSamples(), OS);
595     encodeULEB128(Sample.getCallTargets().size(), OS);
596     for (const auto &J : Sample.getSortedCallTargets()) {
597       StringRef Callee = J.first;
598       uint64_t CalleeSamples = J.second;
599       if (std::error_code EC = writeNameIdx(Callee))
600         return EC;
601       encodeULEB128(CalleeSamples, OS);
602     }
603   }
604 
605   // Recursively emit all the callsite samples.
606   uint64_t NumCallsites = 0;
607   for (const auto &J : S.getCallsiteSamples())
608     NumCallsites += J.second.size();
609   encodeULEB128(NumCallsites, OS);
610   for (const auto &J : S.getCallsiteSamples())
611     for (const auto &FS : J.second) {
612       LineLocation Loc = J.first;
613       const FunctionSamples &CalleeSamples = FS.second;
614       encodeULEB128(Loc.LineOffset, OS);
615       encodeULEB128(Loc.Discriminator, OS);
616       if (std::error_code EC = writeBody(CalleeSamples))
617         return EC;
618     }
619 
620   return sampleprof_error::success;
621 }
622 
623 /// Write samples of a top-level function to a binary file.
624 ///
625 /// \returns true if the samples were written successfully, false otherwise.
626 std::error_code
627 SampleProfileWriterBinary::writeSample(const FunctionSamples &S) {
628   encodeULEB128(S.getHeadSamples(), *OutputStream);
629   return writeBody(S);
630 }
631 
632 std::error_code
633 SampleProfileWriterCompactBinary::writeSample(const FunctionSamples &S) {
634   uint64_t Offset = OutputStream->tell();
635   StringRef Name = S.getName();
636   FuncOffsetTable[Name] = Offset;
637   encodeULEB128(S.getHeadSamples(), *OutputStream);
638   return writeBody(S);
639 }
640 
641 /// Create a sample profile file writer based on the specified format.
642 ///
643 /// \param Filename The file to create.
644 ///
645 /// \param Format Encoding format for the profile file.
646 ///
647 /// \returns an error code indicating the status of the created writer.
648 ErrorOr<std::unique_ptr<SampleProfileWriter>>
649 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
650   std::error_code EC;
651   std::unique_ptr<raw_ostream> OS;
652   if (Format == SPF_Binary || Format == SPF_Ext_Binary ||
653       Format == SPF_Compact_Binary)
654     OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None));
655   else
656     OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_Text));
657   if (EC)
658     return EC;
659 
660   return create(OS, Format);
661 }
662 
663 /// Create a sample profile stream writer based on the specified format.
664 ///
665 /// \param OS The output stream to store the profile data to.
666 ///
667 /// \param Format Encoding format for the profile file.
668 ///
669 /// \returns an error code indicating the status of the created writer.
670 ErrorOr<std::unique_ptr<SampleProfileWriter>>
671 SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
672                             SampleProfileFormat Format) {
673   std::error_code EC;
674   std::unique_ptr<SampleProfileWriter> Writer;
675 
676   if (Format == SPF_Binary)
677     Writer.reset(new SampleProfileWriterRawBinary(OS));
678   else if (Format == SPF_Ext_Binary)
679     Writer.reset(new SampleProfileWriterExtBinary(OS));
680   else if (Format == SPF_Compact_Binary)
681     Writer.reset(new SampleProfileWriterCompactBinary(OS));
682   else if (Format == SPF_Text)
683     Writer.reset(new SampleProfileWriterText(OS));
684   else if (Format == SPF_GCC)
685     EC = sampleprof_error::unsupported_writing_format;
686   else
687     EC = sampleprof_error::unrecognized_format;
688 
689   if (EC)
690     return EC;
691 
692   Writer->Format = Format;
693   return std::move(Writer);
694 }
695 
696 void SampleProfileWriter::computeSummary(
697     const StringMap<FunctionSamples> &ProfileMap) {
698   SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
699   for (const auto &I : ProfileMap) {
700     const FunctionSamples &Profile = I.second;
701     Builder.addRecord(Profile);
702   }
703   Summary = Builder.getSummary();
704 }
705