LLVM 23.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"
28#include "llvm/Support/LEB128.h"
29#include "llvm/Support/MD5.h"
31#include <cmath>
32#include <cstdint>
33#include <memory>
34#include <set>
35#include <system_error>
36#include <utility>
37#include <vector>
38
39#define DEBUG_TYPE "llvm-profdata"
40
41using namespace llvm;
42using namespace sampleprof;
43
44// To begin with, make this option off by default.
46 "extbinary-write-vtable-type-prof", cl::init(false), cl::Hidden,
47 cl::desc("Write vtable type profile in ext-binary sample profile writer"));
48
49namespace llvm {
50namespace support {
51namespace endian {
52namespace {
53
54// Adapter class to llvm::support::endian::Writer for pwrite().
55struct SeekableWriter {
57 endianness Endian;
58 SeekableWriter(raw_pwrite_stream &OS, endianness Endian)
59 : OS(OS), Endian(Endian) {}
60
61 template <typename ValueType> void pwrite(ValueType Val, size_t Offset) {
62 std::string StringBuf;
63 raw_string_ostream SStream(StringBuf);
64 Writer(SStream, Endian).write(Val);
65 OS.pwrite(StringBuf.data(), StringBuf.size(), Offset);
66 }
67};
68
69} // namespace
70} // namespace endian
71} // namespace support
72} // namespace llvm
73
79
80void DefaultFunctionPruningStrategy::Erase(size_t CurrentOutputSize) {
81 double D = (double)OutputSizeLimit / CurrentOutputSize;
82 size_t NewSize = (size_t)round(ProfileMap.size() * D * D);
83 size_t NumToRemove = ProfileMap.size() - NewSize;
84 if (NumToRemove < 1)
85 NumToRemove = 1;
86
87 assert(NumToRemove <= SortedFunctions.size());
88 for (const NameFunctionSamples &E :
89 llvm::drop_begin(SortedFunctions, SortedFunctions.size() - NumToRemove))
90 ProfileMap.erase(E.first);
91 SortedFunctions.resize(SortedFunctions.size() - NumToRemove);
92}
93
95 SampleProfileMap &ProfileMap, size_t OutputSizeLimit,
96 FunctionPruningStrategy *Strategy) {
97 if (OutputSizeLimit == 0)
98 return write(ProfileMap);
99
100 size_t OriginalFunctionCount = ProfileMap.size();
101
102 std::unique_ptr<raw_ostream> OriginalOutputStream;
103 OutputStream.swap(OriginalOutputStream);
104
105 size_t IterationCount = 0;
106 size_t TotalSize;
107
108 SmallVector<char> StringBuffer;
109 do {
110 StringBuffer.clear();
111 OutputStream.reset(new raw_svector_ostream(StringBuffer));
112 if (std::error_code EC = write(ProfileMap))
113 return EC;
114
115 TotalSize = StringBuffer.size();
116 // On Windows every "\n" is actually written as "\r\n" to disk but not to
117 // memory buffer, this difference should be added when considering the total
118 // output size.
119#ifdef _WIN32
120 if (Format == SPF_Text)
121 TotalSize += LineCount;
122#endif
123 if (TotalSize <= OutputSizeLimit)
124 break;
125
126 Strategy->Erase(TotalSize);
127 IterationCount++;
128 } while (ProfileMap.size() != 0);
129
130 if (ProfileMap.size() == 0)
132
133 OutputStream.swap(OriginalOutputStream);
134 OutputStream->write(StringBuffer.data(), StringBuffer.size());
135 LLVM_DEBUG(dbgs() << "Profile originally has " << OriginalFunctionCount
136 << " functions, reduced to " << ProfileMap.size() << " in "
137 << IterationCount << " iterations\n");
138 // Silence warning on Release build.
139 (void)OriginalFunctionCount;
140 (void)IterationCount;
142}
143
144std::error_code
146 std::vector<NameFunctionSamples> V;
147 sortFuncProfiles(ProfileMap, V);
148 for (const auto &I : V) {
149 if (std::error_code EC = writeSample(*I.second))
150 return EC;
151 }
153}
154
155std::error_code SampleProfileWriter::write(const SampleProfileMap &ProfileMap) {
156 if (std::error_code EC = writeHeader(ProfileMap))
157 return EC;
158
159 if (std::error_code EC = writeFuncProfiles(ProfileMap))
160 return EC;
161
163}
164
165/// Return the current position and prepare to use it as the start
166/// position of a section given the section type \p Type and its position
167/// \p LayoutIdx in SectionHdrLayout.
170 uint32_t LayoutIdx) {
171 uint64_t SectionStart = OutputStream->tell();
172 assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
173 const auto &Entry = SectionHdrLayout[LayoutIdx];
174 assert(Entry.Type == Type && "Unexpected section type");
175 // Use LocalBuf as a temporary output for writting data.
177 LocalBufStream.swap(OutputStream);
178 return SectionStart;
179}
180
181std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
184 std::string &UncompressedStrings =
185 static_cast<raw_string_ostream *>(LocalBufStream.get())->str();
186 if (UncompressedStrings.size() == 0)
188 auto &OS = *OutputStream;
189 SmallVector<uint8_t, 128> CompressedStrings;
191 CompressedStrings,
193 encodeULEB128(UncompressedStrings.size(), OS);
194 encodeULEB128(CompressedStrings.size(), OS);
195 OS << toStringRef(CompressedStrings);
196 UncompressedStrings.clear();
198}
199
200/// Add a new section into section header table given the section type
201/// \p Type, its position \p LayoutIdx in SectionHdrLayout and the
202/// location \p SectionStart where the section should be written to.
204 SecType Type, uint32_t LayoutIdx, uint64_t SectionStart) {
205 assert(LayoutIdx < SectionHdrLayout.size() && "LayoutIdx out of range");
206 const auto &Entry = SectionHdrLayout[LayoutIdx];
207 assert(Entry.Type == Type && "Unexpected section type");
209 LocalBufStream.swap(OutputStream);
210 if (std::error_code EC = compressAndOutput())
211 return EC;
212 }
213 SecHdrTable.push_back({Type, Entry.Flags, SectionStart - FileStart,
214 OutputStream->tell() - SectionStart, LayoutIdx});
216}
217
218std::error_code
220 // When calling write on a different profile map, existing states should be
221 // cleared.
222 NameTable.clear();
223 CSNameTable.clear();
224 SecHdrTable.clear();
225
226 if (std::error_code EC = writeHeader(ProfileMap))
227 return EC;
228
229 std::string LocalBuf;
230 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
231 if (std::error_code EC = writeSections(ProfileMap))
232 return EC;
233
234 if (std::error_code EC = writeSecHdrTable())
235 return EC;
236
238}
239
241 const SampleContext &Context) {
242 if (Context.hasContext())
243 return writeCSNameIdx(Context);
244 else
245 return SampleProfileWriterBinary::writeNameIdx(Context.getFunction());
246}
247
248std::error_code
250 const auto &Ret = CSNameTable.find(Context);
251 if (Ret == CSNameTable.end())
253 encodeULEB128(Ret->second, *OutputStream);
255}
256
257std::error_code
259 uint64_t Offset = OutputStream->tell();
260 auto &Context = S.getContext();
261 FuncOffsetTable[Context] = Offset - SecLBRProfileStart;
263 return writeBody(S);
264}
265
267 auto &OS = *OutputStream;
268
269 // Write out the table size.
270 encodeULEB128(FuncOffsetTable.size(), OS);
271
272 // Write out FuncOffsetTable.
273 auto WriteItem = [&](const SampleContext &Context, uint64_t Offset) {
274 if (std::error_code EC = writeContextIdx(Context))
275 return EC;
277 return (std::error_code)sampleprof_error::success;
278 };
279
281 // Sort the contexts before writing them out. This is to help fast load all
282 // context profiles for a function as well as their callee contexts which
283 // can help profile-guided importing for ThinLTO.
284 std::map<SampleContext, uint64_t> OrderedFuncOffsetTable(
285 FuncOffsetTable.begin(), FuncOffsetTable.end());
286 for (const auto &Entry : OrderedFuncOffsetTable) {
287 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
288 return EC;
289 }
291 } else {
292 for (const auto &Entry : FuncOffsetTable) {
293 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
294 return EC;
295 }
296 }
297
298 FuncOffsetTable.clear();
300}
301
303 const FunctionSamples &FunctionProfile) {
304 auto &OS = *OutputStream;
305 if (std::error_code EC = writeContextIdx(FunctionProfile.getContext()))
306 return EC;
307
309 encodeULEB128(FunctionProfile.getFunctionHash(), OS);
311 encodeULEB128(FunctionProfile.getContext().getAllAttributes(), OS);
312 }
313
315 // Recursively emit attributes for all callee samples.
316 uint64_t NumCallsites = 0;
317 for (const auto &J : FunctionProfile.getCallsiteSamples())
318 NumCallsites += J.second.size();
319 encodeULEB128(NumCallsites, OS);
320 for (const auto &J : FunctionProfile.getCallsiteSamples()) {
321 for (const auto &FS : J.second) {
322 LineLocation Loc = J.first;
323 encodeULEB128(Loc.LineOffset, OS);
324 encodeULEB128(Loc.Discriminator, OS);
325 if (std::error_code EC = writeFuncMetadata(FS.second))
326 return EC;
327 }
328 }
329 }
330
332}
333
335 const SampleProfileMap &Profiles) {
339 for (const auto &Entry : Profiles) {
340 if (std::error_code EC = writeFuncMetadata(Entry.second))
341 return EC;
342 }
344}
345
347 if (!UseMD5)
349
350 auto &OS = *OutputStream;
351 std::set<FunctionId> V;
353
354 // Write out the MD5 name table. We wrote unencoded MD5 so reader can
355 // retrieve the name using the name index without having to read the
356 // whole name table.
357 encodeULEB128(NameTable.size(), OS);
359 for (auto N : V)
360 Writer.write(N.getHashCode());
362}
363
365 const SampleProfileMap &ProfileMap) {
366 for (const auto &I : ProfileMap) {
367 addContext(I.second.getContext());
368 addNames(I.second);
369 }
370
371 // If NameTable contains ".__uniq." suffix, set SecFlagUniqSuffix flag
372 // so compiler won't strip the suffix during profile matching after
373 // seeing the flag in the profile.
374 // Original names are unavailable if using MD5, so this option has no use.
375 if (!UseMD5) {
376 for (const auto &I : NameTable) {
377 if (I.first.stringRef().contains(FunctionSamples::UniqSuffix)) {
379 break;
380 }
381 }
382 }
383
384 if (auto EC = writeNameTable())
385 return EC;
387}
388
390 // Sort the names to make CSNameTable deterministic.
391 std::set<SampleContext> OrderedContexts;
392 for (const auto &I : CSNameTable)
393 OrderedContexts.insert(I.first);
394 assert(OrderedContexts.size() == CSNameTable.size() &&
395 "Unmatched ordered and unordered contexts");
396 uint64_t I = 0;
397 for (auto &Context : OrderedContexts)
398 CSNameTable[Context] = I++;
399
400 auto &OS = *OutputStream;
401 encodeULEB128(OrderedContexts.size(), OS);
403 for (auto Context : OrderedContexts) {
404 auto Frames = Context.getContextFrames();
405 encodeULEB128(Frames.size(), OS);
406 for (auto &Callsite : Frames) {
407 if (std::error_code EC = writeNameIdx(Callsite.Func))
408 return EC;
409 encodeULEB128(Callsite.Location.LineOffset, OS);
410 encodeULEB128(Callsite.Location.Discriminator, OS);
411 }
412 }
413
415}
416
417std::error_code
419 if (ProfSymList && ProfSymList->size() > 0)
420 if (std::error_code EC = ProfSymList->write(*OutputStream))
421 return EC;
422
424}
425
427 SecType Type, uint32_t LayoutIdx, const SampleProfileMap &ProfileMap) {
428 // The setting of SecFlagCompress should happen before markSectionStart.
429 if (Type == SecProfileSymbolList && ProfSymList && ProfSymList->toCompress())
433 if (Type == SecFuncMetadata &&
445
446 uint64_t SectionStart = markSectionStart(Type, LayoutIdx);
447 switch (Type) {
448 case SecProfSummary:
449 computeSummary(ProfileMap);
450 if (auto EC = writeSummary())
451 return EC;
452 break;
453 case SecNameTable:
454 if (auto EC = writeNameTableSection(ProfileMap))
455 return EC;
456 break;
457 case SecCSNameTable:
458 if (auto EC = writeCSNameTableSection())
459 return EC;
460 break;
461 case SecLBRProfile:
463 if (std::error_code EC = writeFuncProfiles(ProfileMap))
464 return EC;
465 break;
467 if (auto EC = writeFuncOffsetTable())
468 return EC;
469 break;
470 case SecFuncMetadata:
471 if (std::error_code EC = writeFuncMetadata(ProfileMap))
472 return EC;
473 break;
475 if (auto EC = writeProfileSymbolListSection())
476 return EC;
477 break;
478 default:
479 if (auto EC = writeCustomSection(Type))
480 return EC;
481 break;
482 }
483 if (std::error_code EC = addNewSection(Type, LayoutIdx, SectionStart))
484 return EC;
486}
487
493
494std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
495 const SampleProfileMap &ProfileMap) {
496 // The const indices passed to writeOneSection below are specifying the
497 // positions of the sections in SectionHdrLayout. Look at
498 // initSectionHdrLayout to find out where each section is located in
499 // SectionHdrLayout.
500 if (auto EC = writeOneSection(SecProfSummary, 0, ProfileMap))
501 return EC;
502 if (auto EC = writeOneSection(SecNameTable, 1, ProfileMap))
503 return EC;
504 if (auto EC = writeOneSection(SecCSNameTable, 2, ProfileMap))
505 return EC;
506 if (auto EC = writeOneSection(SecLBRProfile, 4, ProfileMap))
507 return EC;
508 if (auto EC = writeOneSection(SecProfileSymbolList, 5, ProfileMap))
509 return EC;
510 if (auto EC = writeOneSection(SecFuncOffsetTable, 3, ProfileMap))
511 return EC;
512 if (auto EC = writeOneSection(SecFuncMetadata, 6, ProfileMap))
513 return EC;
515}
516
517static void splitProfileMapToTwo(const SampleProfileMap &ProfileMap,
518 SampleProfileMap &ContextProfileMap,
519 SampleProfileMap &NoContextProfileMap) {
520 for (const auto &I : ProfileMap) {
521 if (I.second.getCallsiteSamples().size())
522 ContextProfileMap.insert({I.first, I.second});
523 else
524 NoContextProfileMap.insert({I.first, I.second});
525 }
526}
527
528std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
529 const SampleProfileMap &ProfileMap) {
530 SampleProfileMap ContextProfileMap, NoContextProfileMap;
531 splitProfileMapToTwo(ProfileMap, ContextProfileMap, NoContextProfileMap);
532
533 if (auto EC = writeOneSection(SecProfSummary, 0, ProfileMap))
534 return EC;
535 if (auto EC = writeOneSection(SecNameTable, 1, ProfileMap))
536 return EC;
537 if (auto EC = writeOneSection(SecLBRProfile, 3, ContextProfileMap))
538 return EC;
539 if (auto EC = writeOneSection(SecFuncOffsetTable, 2, ContextProfileMap))
540 return EC;
541 // Mark the section to have no context. Note section flag needs to be set
542 // before writing the section.
544 if (auto EC = writeOneSection(SecLBRProfile, 5, NoContextProfileMap))
545 return EC;
546 // Mark the section to have no context. Note section flag needs to be set
547 // before writing the section.
549 if (auto EC = writeOneSection(SecFuncOffsetTable, 4, NoContextProfileMap))
550 return EC;
551 if (auto EC = writeOneSection(SecProfileSymbolList, 6, ProfileMap))
552 return EC;
553 if (auto EC = writeOneSection(SecFuncMetadata, 7, ProfileMap))
554 return EC;
555
557}
558
559std::error_code SampleProfileWriterExtBinary::writeSections(
560 const SampleProfileMap &ProfileMap) {
561 std::error_code EC;
563 EC = writeDefaultLayout(ProfileMap);
564 else if (SecLayout == CtxSplitLayout)
565 EC = writeCtxSplitLayout(ProfileMap);
566 else
567 llvm_unreachable("Unsupported layout");
568 return EC;
569}
570
571/// Write samples to a text file.
572///
573/// Note: it may be tempting to implement this in terms of
574/// FunctionSamples::print(). Please don't. The dump functionality is intended
575/// for debugging and has no specified form.
576///
577/// The format used here is more structured and deliberate because
578/// it needs to be parsed by the SampleProfileReaderText class.
580 auto &OS = *OutputStream;
582 OS << "[" << S.getContext().toString() << "]:" << S.getTotalSamples();
583 else
584 OS << S.getFunction() << ":" << S.getTotalSamples();
585
586 if (Indent == 0)
587 OS << ":" << S.getHeadSamples();
588 OS << "\n";
589 LineCount++;
590
592 for (const auto &I : SortedSamples.get()) {
593 LineLocation Loc = I->first;
594 const SampleRecord &Sample = I->second;
595 OS.indent(Indent + 1);
596 Loc.print(OS);
597 OS << ": " << Sample.getSamples();
598
599 for (const auto &J : Sample.getSortedCallTargets())
600 OS << " " << J.first << ":" << J.second;
601 OS << "\n";
602 LineCount++;
603
604 if (const TypeCountMap *Map = S.findCallsiteTypeSamplesAt(Loc);
605 Map && !Map->empty()) {
606 OS.indent(Indent + 1);
607 Loc.print(OS);
608 OS << ": ";
609 OS << kVTableProfPrefix;
610 for (const auto [TypeName, Count] : *Map) {
611 OS << TypeName << ":" << Count << " ";
612 }
613 OS << "\n";
614 LineCount++;
615 }
616 }
617
620 Indent += 1;
621 for (const auto *Element : SortedCallsiteSamples.get()) {
622 // Element is a pointer to a pair of LineLocation and FunctionSamplesMap.
623 const auto &[Loc, FunctionSamplesMap] = *Element;
624 for (const FunctionSamples &CalleeSamples :
626 OS.indent(Indent);
627 Loc.print(OS);
628 OS << ": ";
629 if (std::error_code EC = writeSample(CalleeSamples))
630 return EC;
631 }
632
633 if (const TypeCountMap *Map = S.findCallsiteTypeSamplesAt(Loc);
634 Map && !Map->empty()) {
635 OS.indent(Indent);
636 Loc.print(OS);
637 OS << ": ";
638 OS << kVTableProfPrefix;
639 for (const auto [TypeId, Count] : *Map) {
640 OS << TypeId << ":" << Count << " ";
641 }
642 OS << "\n";
643 LineCount++;
644 }
645 }
646
647 Indent -= 1;
648
650 OS.indent(Indent + 1);
651 OS << "!CFGChecksum: " << S.getFunctionHash() << "\n";
652 LineCount++;
653 }
654
655 if (S.getContext().getAllAttributes()) {
656 OS.indent(Indent + 1);
657 OS << "!Attributes: " << S.getContext().getAllAttributes() << "\n";
658 LineCount++;
659 }
660
661 if (Indent == 0 && MarkFlatProfiles && S.getCallsiteSamples().size() == 0)
662 OS << " !Flat\n";
663
665}
666
667std::error_code
669 assert(!Context.hasContext() && "cs profile is not supported");
670 return writeNameIdx(Context.getFunction());
671}
672
674 auto &NTable = getNameTable();
675 const auto &Ret = NTable.find(FName);
676 if (Ret == NTable.end())
678 encodeULEB128(Ret->second, *OutputStream);
680}
681
683 auto &NTable = getNameTable();
684 NTable.insert(std::make_pair(FName, 0));
685}
686
688 addName(Context.getFunction());
689}
690
692 // Add all the names in indirect call targets.
693 for (const auto &I : S.getBodySamples()) {
694 const SampleRecord &Sample = I.second;
695 for (const auto &J : Sample.getCallTargets())
696 addName(J.first);
697 }
698
699 // Recursively add all the names for inlined callsites.
700 for (const auto &J : S.getCallsiteSamples())
701 for (const auto &FS : J.second) {
702 const FunctionSamples &CalleeSamples = FS.second;
703 addName(CalleeSamples.getFunction());
704 addNames(CalleeSamples);
705 }
706
707 if (!WriteVTableProf)
708 return;
709 // Add all the vtable names to NameTable.
710 for (const auto &VTableAccessCountMap :
712 // Add type name to NameTable.
713 for (const auto Type : llvm::make_first_range(VTableAccessCountMap)) {
714 addName(Type);
715 }
716 }
717}
718
720 const SampleContext &Context) {
721 if (Context.hasContext()) {
722 for (auto &Callsite : Context.getContextFrames())
724 CSNameTable.insert(std::make_pair(Context, 0));
725 } else {
726 SampleProfileWriterBinary::addName(Context.getFunction());
727 }
728}
729
731 MapVector<FunctionId, uint32_t> &NameTable, std::set<FunctionId> &V) {
732 // Sort the names to make NameTable deterministic.
733 for (const auto &I : NameTable)
734 V.insert(I.first);
735 int i = 0;
736 for (const FunctionId &N : V)
737 NameTable[N] = i++;
738}
739
741 auto &OS = *OutputStream;
742 std::set<FunctionId> V;
744
745 // Write out the name table.
746 encodeULEB128(NameTable.size(), OS);
747 for (auto N : V) {
748 OS << N;
749 encodeULEB128(0, OS);
750 }
752}
753
754std::error_code
756 auto &OS = *OutputStream;
757 // Write file magic identifier.
761}
762
763std::error_code
765 // When calling write on a different profile map, existing names should be
766 // cleared.
767 NameTable.clear();
768
770
771 computeSummary(ProfileMap);
772 if (auto EC = writeSummary())
773 return EC;
774
775 // Generate the name table for all the functions referenced in the profile.
776 for (const auto &I : ProfileMap) {
777 addContext(I.second.getContext());
778 addNames(I.second);
779 }
780
783}
784
789
793
794void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
796
797 Writer.write(static_cast<uint64_t>(SectionHdrLayout.size()));
798 SecHdrTableOffset = OutputStream->tell();
799 for (uint32_t i = 0; i < SectionHdrLayout.size(); i++) {
800 Writer.write(static_cast<uint64_t>(-1));
801 Writer.write(static_cast<uint64_t>(-1));
802 Writer.write(static_cast<uint64_t>(-1));
803 Writer.write(static_cast<uint64_t>(-1));
804 }
805}
806
807std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
808 assert(SecHdrTable.size() == SectionHdrLayout.size() &&
809 "SecHdrTable entries doesn't match SectionHdrLayout");
810 SmallVector<uint32_t, 16> IndexMap(SecHdrTable.size(), -1);
811 for (uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
812 IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
813 }
814
815 // Write the section header table in the order specified in
816 // SectionHdrLayout. SectionHdrLayout specifies the sections
817 // order in which profile reader expect to read, so the section
818 // header table should be written in the order in SectionHdrLayout.
819 // Note that the section order in SecHdrTable may be different
820 // from the order in SectionHdrLayout, for example, SecFuncOffsetTable
821 // needs to be computed after SecLBRProfile (the order in SecHdrTable),
822 // but it needs to be read before SecLBRProfile (the order in
823 // SectionHdrLayout). So we use IndexMap above to switch the order.
824 support::endian::SeekableWriter Writer(
825 static_cast<raw_pwrite_stream &>(*OutputStream),
827 for (uint32_t LayoutIdx = 0; LayoutIdx < SectionHdrLayout.size();
828 LayoutIdx++) {
829 assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
830 "Incorrect LayoutIdx in SecHdrTable");
831 auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
832 Writer.pwrite(static_cast<uint64_t>(Entry.Type),
833 SecHdrTableOffset + 4 * LayoutIdx * sizeof(uint64_t));
834 Writer.pwrite(static_cast<uint64_t>(Entry.Flags),
835 SecHdrTableOffset + (4 * LayoutIdx + 1) * sizeof(uint64_t));
836 Writer.pwrite(static_cast<uint64_t>(Entry.Offset),
837 SecHdrTableOffset + (4 * LayoutIdx + 2) * sizeof(uint64_t));
838 Writer.pwrite(static_cast<uint64_t>(Entry.Size),
839 SecHdrTableOffset + (4 * LayoutIdx + 3) * sizeof(uint64_t));
840 }
841
843}
844
845std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
846 const SampleProfileMap &ProfileMap) {
847 auto &OS = *OutputStream;
848 FileStart = OS.tell();
850
851 allocSecHdrTable();
853}
854
858 "writeCallsiteVTableProf should not be called if WriteVTableProf is "
859 "false");
860
861 encodeULEB128(CallsiteTypeMap.size(), OS);
862 for (const auto &[Loc, TypeMap] : CallsiteTypeMap) {
863 Loc.serialize(OS);
864 if (std::error_code EC = serializeTypeMap(TypeMap, getNameTable(), OS))
865 return EC;
866 }
867
869}
870
872 auto &OS = *OutputStream;
873 encodeULEB128(Summary->getTotalCount(), OS);
874 encodeULEB128(Summary->getMaxCount(), OS);
875 encodeULEB128(Summary->getMaxFunctionCount(), OS);
876 encodeULEB128(Summary->getNumCounts(), OS);
877 encodeULEB128(Summary->getNumFunctions(), OS);
878 ArrayRef<ProfileSummaryEntry> Entries = Summary->getDetailedSummary();
879 encodeULEB128(Entries.size(), OS);
880 for (auto Entry : Entries) {
881 encodeULEB128(Entry.Cutoff, OS);
882 encodeULEB128(Entry.MinCount, OS);
883 encodeULEB128(Entry.NumCounts, OS);
884 }
886}
888 auto &OS = *OutputStream;
889 if (std::error_code EC = writeContextIdx(S.getContext()))
890 return EC;
891
893
894 // Emit all the body samples.
895 encodeULEB128(S.getBodySamples().size(), OS);
896 for (const auto &I : S.getBodySamples()) {
897 LineLocation Loc = I.first;
898 const SampleRecord &Sample = I.second;
899 Loc.serialize(OS);
900 Sample.serialize(OS, getNameTable());
901 }
902
903 // Recursively emit all the callsite samples.
904 uint64_t NumCallsites = 0;
905 for (const auto &J : S.getCallsiteSamples())
906 NumCallsites += J.second.size();
907 encodeULEB128(NumCallsites, OS);
908 for (const auto &J : S.getCallsiteSamples())
909 for (const auto &FS : J.second) {
910 J.first.serialize(OS);
911 if (std::error_code EC = writeBody(FS.second))
912 return EC;
913 }
914
915 if (WriteVTableProf)
917
919}
920
921/// Write samples of a top-level function to a binary file.
922///
923/// \returns true if the samples were written successfully, false otherwise.
924std::error_code
929
930/// Create a sample profile file writer based on the specified format.
931///
932/// \param Filename The file to create.
933///
934/// \param Format Encoding format for the profile file.
935///
936/// \returns an error code indicating the status of the created writer.
939 std::error_code EC;
940 std::unique_ptr<raw_ostream> OS;
942 OS.reset(new raw_fd_ostream(Filename, EC, sys::fs::OF_None));
943 else
945 if (EC)
946 return EC;
947
948 return create(OS, Format);
949}
950
951/// Create a sample profile stream writer based on the specified format.
952///
953/// \param OS The output stream to store the profile data to.
954///
955/// \param Format Encoding format for the profile file.
956///
957/// \returns an error code indicating the status of the created writer.
959SampleProfileWriter::create(std::unique_ptr<raw_ostream> &OS,
961 std::error_code EC;
962 std::unique_ptr<SampleProfileWriter> Writer;
963
964 // Currently only Text and Extended Binary format are supported for CSSPGO.
968
969 if (Format == SPF_Binary)
970 Writer.reset(new SampleProfileWriterRawBinary(OS));
971 else if (Format == SPF_Ext_Binary)
972 Writer.reset(new SampleProfileWriterExtBinary(OS));
973 else if (Format == SPF_Text)
974 Writer.reset(new SampleProfileWriterText(OS));
975 else if (Format == SPF_GCC)
977 else
979
980 if (EC)
981 return EC;
982
983 Writer->Format = Format;
984 return std::move(Writer);
985}
986
989 Summary = Builder.computeSummaryForProfiles(ProfileMap);
990}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Provides ErrorOr<T> smart pointer.
static uint64_t round(uint64_t Acc, uint64_t Input)
Definition KCFIHash.cpp:29
#define I(x, y, z)
Definition MD5.cpp:57
static constexpr StringLiteral Filename
static void splitProfileMapToTwo(const SampleProfileMap &ProfileMap, SampleProfileMap &ContextProfileMap, SampleProfileMap &NoContextProfileMap)
static cl::opt< bool > ExtBinaryWriteVTableTypeProf("extbinary-write-vtable-type-prof", cl::init(false), cl::Hidden, cl::desc("Write vtable type profile in ext-binary sample profile writer"))
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Represents either an error or a value T.
Definition ErrorOr.h:56
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
static LLVM_ABI const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
An abstract base class for streams implementations that also support a pwrite operation.
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
A raw_ostream that writes to an SmallVector or SmallString.
DefaultFunctionPruningStrategy(SampleProfileMap &ProfileMap, size_t OutputSizeLimit)
void Erase(size_t CurrentOutputSize) override
In this default implementation, functions with fewest samples are dropped first.
This class represents a function that is read from a sample profile.
Definition FunctionId.h:36
When writing a profile with size limit, user may want to use a different strategy to reduce function ...
virtual void Erase(size_t CurrentOutputSize)=0
SampleProfileWriter::writeWithSizeLimit() calls this after every write iteration if the output size s...
FunctionPruningStrategy(SampleProfileMap &ProfileMap, size_t OutputSizeLimit)
ProfileMap A reference to the original profile map.
Representation of the samples collected for a function.
Definition SampleProf.h:789
static LLVM_ABI bool ProfileIsPreInlined
static constexpr const char * UniqSuffix
uint64_t getHeadSamples() const
For top-level functions, return the total number of branch samples that have the function as the bran...
static LLVM_ABI bool ProfileIsCS
FunctionId getFunction() const
Return the function name.
const CallsiteTypeMap & getCallsiteTypeCounts() const
Returns vtable access samples for the C++ types collected in this function.
const TypeCountMap * findCallsiteTypeSamplesAt(const LineLocation &Loc) const
Returns the TypeCountMap for inlined callsites at the given Loc.
Definition SampleProf.h:972
static LLVM_ABI bool ProfileIsProbeBased
static LLVM_ABI bool ProfileIsFS
If this profile uses flow sensitive discriminators.
SampleContext & getContext() const
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition SampleProf.h:994
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
std::string toString() const
Definition SampleProf.h:677
This class provides operator overloads to the map container using MD5 as the key type,...
void stablizeNameTable(MapVector< FunctionId, uint32_t > &NameTable, std::set< FunctionId > &V)
virtual void addContext(const SampleContext &Context)
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
MapVector< FunctionId, uint32_t > NameTable
std::error_code writeCallsiteVTableProf(const CallsiteTypeMap &CallsiteTypeMap, raw_ostream &OS)
Write CallsiteTypeMap to the output stream OS.
virtual std::error_code writeContextIdx(const SampleContext &Context)
std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
std::error_code writeHeader(const SampleProfileMap &ProfileMap) override
Write a file header for the profile file.
virtual MapVector< FunctionId, uint32_t > & getNameTable()
std::error_code writeBody(const FunctionSamples &S)
std::error_code writeNameIdx(FunctionId FName)
std::error_code writeNameTableSection(const SampleProfileMap &ProfileMap)
SmallVector< SecHdrTableEntry, 8 > SectionHdrLayout
std::error_code writeFuncMetadata(const SampleProfileMap &Profiles)
virtual std::error_code writeCustomSection(SecType Type)=0
virtual std::error_code writeOneSection(SecType Type, uint32_t LayoutIdx, const SampleProfileMap &ProfileMap)
std::error_code writeCSNameIdx(const SampleContext &Context)
virtual std::error_code writeSections(const SampleProfileMap &ProfileMap)=0
void addSectionFlag(SecType Type, SecFlagType Flag)
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...
void addContext(const SampleContext &Context) override
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...
std::error_code write(const SampleProfileMap &ProfileMap) override
Write all the sample profiles in the given map of samples.
std::error_code writeContextIdx(const SampleContext &Context) override
std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
SampleProfileWriterExtBinary(std::unique_ptr< raw_ostream > &OS)
Sample-based profile writer (text format).
std::error_code writeSample(const FunctionSamples &S) override
Write samples to a text file.
std::unique_ptr< ProfileSummary > Summary
Profile summary.
virtual std::error_code writeSample(const FunctionSamples &S)=0
Write sample profiles in S.
SampleProfileFormat Format
Profile format.
std::error_code writeWithSizeLimitInternal(SampleProfileMap &ProfileMap, size_t OutputSizeLimit, FunctionPruningStrategy *Strategy)
void computeSummary(const SampleProfileMap &ProfileMap)
Compute summary for this profile.
virtual std::error_code writeFuncProfiles(const SampleProfileMap &ProfileMap)
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
size_t LineCount
For writeWithSizeLimit in text mode, each newline takes 1 additional byte on Windows when actually wr...
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
virtual std::error_code writeHeader(const SampleProfileMap &ProfileMap)=0
Write a file header for the profile file.
virtual std::error_code write(const SampleProfileMap &ProfileMap)
Write all the sample profiles in the given map of samples.
Representation of a single sample record.
Definition SampleProf.h:360
LLVM_ABI std::error_code serialize(raw_ostream &OS, const MapVector< FunctionId, uint32_t > &NameTable) const
Serialize the sample record to the output stream using ULEB128 encoding.
const CallTargetMap & getCallTargets() const
Definition SampleProf.h:428
const SortedCallTargetSet getSortedCallTargets() const
Definition SampleProf.h:429
Sort a LocationT->SampleT map by LocationT.
const SamplesWithLocList & get() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition COFF.h:862
initializer< Ty > init(const Ty &Val)
LLVM_ABI void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
LLVM_ABI bool isAvailable()
constexpr int BestSizeCompression
Definition Compression.h:40
LLVM_ABI void sortFuncProfiles(const SampleProfileMap &ProfileMap, std::vector< NameFunctionSamples > &SortedProfiles)
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
Definition SampleProf.h:112
static void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
Definition SampleProf.h:257
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
Definition SampleProf.h:273
std::pair< hash_code, const FunctionSamples * > NameFunctionSamples
@ SecFlagIsPreInlined
SecFlagIsPreInlined means this profile contains ShouldBeInlined contexts thus this is CS preinliner c...
Definition SampleProf.h:209
@ SecFlagHasVTableTypeProf
SecFlagHasVTableTypeProf means this profile contains vtable type profiles.
Definition SampleProf.h:212
@ SecFlagFSDiscriminator
SecFlagFSDiscriminator means this profile uses flow-sensitive discriminators.
Definition SampleProf.h:206
@ SecFlagFullContext
SecFlagContext means this is context-sensitive flat profile for CSSPGO.
Definition SampleProf.h:203
std::map< FunctionId, FunctionSamples > FunctionSamplesMap
Definition SampleProf.h:779
std::map< FunctionId, uint64_t > TypeCountMap
Key represents type of a C++ polymorphic class type by its vtable and value represents its counter.
Definition SampleProf.h:340
constexpr char kVTableProfPrefix[]
Definition SampleProf.h:95
LLVM_ABI std::error_code serializeTypeMap(const TypeCountMap &Map, const MapVector< FunctionId, uint32_t > &NameTable, raw_ostream &OS)
Write Map to the output stream.
static uint64_t SPVersion()
Definition SampleProf.h:119
std::map< LineLocation, TypeCountMap > CallsiteTypeMap
Definition SampleProf.h:781
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition FileSystem.h:804
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
@ Offset
Definition DWP.cpp:573
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
Definition STLExtras.h:1399
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
Definition STLExtras.h:1409
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:79
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
endianness
Definition bit.h:71
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
#define N
Represents the relative location of an instruction.
Definition SampleProf.h:289
Adapter to write values to a stream in a particular byte order.
void write(ArrayRef< value_type > Val)