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/ADT/StringSet.h"
23 #include "llvm/ProfileData/ProfileCommon.h"
24 #include "llvm/ProfileData/SampleProf.h"
25 #include "llvm/Support/Compression.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/EndianStream.h"
28 #include "llvm/Support/ErrorOr.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/LEB128.h"
31 #include "llvm/Support/MD5.h"
32 #include "llvm/Support/raw_ostream.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
writeFuncProfiles(const StringMap<FunctionSamples> & ProfileMap)44 std::error_code SampleProfileWriter::writeFuncProfiles(
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
52 llvm::stable_sort(
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 }
63 return sampleprof_error::success;
64 }
65
66 std::error_code
write(const StringMap<FunctionSamples> & ProfileMap)67 SampleProfileWriter::write(const StringMap<FunctionSamples> &ProfileMap) {
68 if (std::error_code EC = writeHeader(ProfileMap))
69 return EC;
70
71 if (std::error_code EC = writeFuncProfiles(ProfileMap))
72 return EC;
73
74 return sampleprof_error::success;
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
markSectionStart(SecType Type,uint32_t LayoutIdx)81 SampleProfileWriterExtBinaryBase::markSectionStart(SecType Type,
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.
88 if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress))
89 LocalBufStream.swap(OutputStream);
90 return SectionStart;
91 }
92
compressAndOutput()93 std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
94 if (!llvm::zlib::isAvailable())
95 return sampleprof_error::zlib_unavailable;
96 std::string &UncompressedStrings =
97 static_cast<raw_string_ostream *>(LocalBufStream.get())->str();
98 if (UncompressedStrings.size() == 0)
99 return sampleprof_error::success;
100 auto &OS = *OutputStream;
101 SmallString<128> CompressedStrings;
102 llvm::Error E = zlib::compress(UncompressedStrings, CompressedStrings,
103 zlib::BestSizeCompression);
104 if (E)
105 return sampleprof_error::compress_failed;
106 encodeULEB128(UncompressedStrings.size(), OS);
107 encodeULEB128(CompressedStrings.size(), OS);
108 OS << CompressedStrings.str();
109 UncompressedStrings.clear();
110 return sampleprof_error::success;
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.
addNewSection(SecType Type,uint32_t LayoutIdx,uint64_t SectionStart)116 std::error_code SampleProfileWriterExtBinaryBase::addNewSection(
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");
121 if (hasSecFlag(Entry, SecCommonFlags::SecFlagCompress)) {
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});
128 return sampleprof_error::success;
129 }
130
write(const StringMap<FunctionSamples> & ProfileMap)131 std::error_code SampleProfileWriterExtBinaryBase::write(
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
144 return sampleprof_error::success;
145 }
146
147 std::error_code
writeSample(const FunctionSamples & S)148 SampleProfileWriterExtBinaryBase::writeSample(const FunctionSamples &S) {
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
writeFuncOffsetTable()156 std::error_code SampleProfileWriterExtBinaryBase::writeFuncOffsetTable() {
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();
168 return sampleprof_error::success;
169 }
170
writeFuncMetadata(const StringMap<FunctionSamples> & Profiles)171 std::error_code SampleProfileWriterExtBinaryBase::writeFuncMetadata(
172 const StringMap<FunctionSamples> &Profiles) {
173 if (!FunctionSamples::ProfileIsProbeBased)
174 return sampleprof_error::success;
175 auto &OS = *OutputStream;
176 for (const auto &Entry : Profiles) {
177 writeNameIdx(Entry.first());
178 encodeULEB128(Entry.second.getFunctionHash(), OS);
179 }
180 return sampleprof_error::success;
181 }
182
writeNameTable()183 std::error_code SampleProfileWriterExtBinaryBase::writeNameTable() {
184 if (!UseMD5)
185 return SampleProfileWriterBinary::writeNameTable();
186
187 auto &OS = *OutputStream;
188 std::set<StringRef> V;
189 stablizeNameTable(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);
195 support::endian::Writer Writer(OS, support::little);
196 for (auto N : V)
197 Writer.write(MD5Hash(N));
198 return sampleprof_error::success;
199 }
200
writeNameTableSection(const StringMap<FunctionSamples> & ProfileMap)201 std::error_code SampleProfileWriterExtBinaryBase::writeNameTableSection(
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;
209 return sampleprof_error::success;
210 }
211
212 std::error_code
writeProfileSymbolListSection()213 SampleProfileWriterExtBinaryBase::writeProfileSymbolListSection() {
214 if (ProfSymList && ProfSymList->size() > 0)
215 if (std::error_code EC = ProfSymList->write(*OutputStream))
216 return EC;
217
218 return sampleprof_error::success;
219 }
220
writeOneSection(SecType Type,uint32_t LayoutIdx,const StringMap<FunctionSamples> & ProfileMap)221 std::error_code SampleProfileWriterExtBinaryBase::writeOneSection(
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())
226 setToCompressSection(SecProfileSymbolList);
227 if (Type == SecFuncMetadata && FunctionSamples::ProfileIsProbeBased)
228 addSectionFlag(SecFuncMetadata, SecFuncMetadataFlags::SecFlagIsProbeBased);
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:
242 SecLBRProfileStart = OutputStream->tell();
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;
254 case SecProfileSymbolList:
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;
265 return sampleprof_error::success;
266 }
267
writeDefaultLayout(const StringMap<FunctionSamples> & ProfileMap)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;
286 return sampleprof_error::success;
287 }
288
289 static void
splitProfileMapToTwo(const StringMap<FunctionSamples> & ProfileMap,StringMap<FunctionSamples> & ContextProfileMap,StringMap<FunctionSamples> & NoContextProfileMap)290 splitProfileMapToTwo(const StringMap<FunctionSamples> &ProfileMap,
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
writeCtxSplitLayout(const StringMap<FunctionSamples> & ProfileMap)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.
316 addSectionFlag(5, SecCommonFlags::SecFlagFlat);
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.
321 addSectionFlag(4, SecCommonFlags::SecFlagFlat);
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
329 return sampleprof_error::success;
330 }
331
writeSections(const StringMap<FunctionSamples> & ProfileMap)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
write(const StringMap<FunctionSamples> & ProfileMap)344 std::error_code SampleProfileWriterCompactBinary::write(
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;
350 return sampleprof_error::success;
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.
writeSample(const FunctionSamples & S)361 std::error_code SampleProfileWriterText::writeSample(const FunctionSamples &S) {
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
385 SampleSorter<LineLocation, FunctionSamplesMap> SortedCallsiteSamples(
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) {
403 if (FunctionSamples::ProfileIsProbeBased) {
404 OS.indent(Indent + 1);
405 OS << "!CFGChecksum: " << S.getFunctionHash() << "\n";
406 }
407 }
408
409 return sampleprof_error::success;
410 }
411
writeNameIdx(StringRef FName)412 std::error_code SampleProfileWriterBinary::writeNameIdx(StringRef FName) {
413 const auto &ret = NameTable.find(FName);
414 if (ret == NameTable.end())
415 return sampleprof_error::truncated_name_table;
416 encodeULEB128(ret->second, *OutputStream);
417 return sampleprof_error::success;
418 }
419
addName(StringRef FName)420 void SampleProfileWriterBinary::addName(StringRef FName) {
421 NameTable.insert(std::make_pair(FName, 0));
422 }
423
addNames(const FunctionSamples & S)424 void SampleProfileWriterBinary::addNames(const FunctionSamples &S) {
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
stablizeNameTable(std::set<StringRef> & V)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
writeNameTable()450 std::error_code SampleProfileWriterBinary::writeNameTable() {
451 auto &OS = *OutputStream;
452 std::set<StringRef> V;
453 stablizeNameTable(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 }
461 return sampleprof_error::success;
462 }
463
writeFuncOffsetTable()464 std::error_code SampleProfileWriterCompactBinary::writeFuncOffsetTable() {
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)
471 return sampleprof_error::ostream_seek_unsupported;
472 support::endian::Writer Writer(*OutputStream, support::little);
473 Writer.write(FuncOffsetTableStart);
474 if (OFS.seek(FuncOffsetTableStart) == (uint64_t)-1)
475 return sampleprof_error::ostream_seek_unsupported;
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 }
485 return sampleprof_error::success;
486 }
487
writeNameTable()488 std::error_code SampleProfileWriterCompactBinary::writeNameTable() {
489 auto &OS = *OutputStream;
490 std::set<StringRef> V;
491 stablizeNameTable(V);
492
493 // Write out the name table.
494 encodeULEB128(NameTable.size(), OS);
495 for (auto N : V) {
496 encodeULEB128(MD5Hash(N), OS);
497 }
498 return sampleprof_error::success;
499 }
500
501 std::error_code
writeMagicIdent(SampleProfileFormat Format)502 SampleProfileWriterBinary::writeMagicIdent(SampleProfileFormat Format) {
503 auto &OS = *OutputStream;
504 // Write file magic identifier.
505 encodeULEB128(SPMagic(Format), OS);
506 encodeULEB128(SPVersion(), OS);
507 return sampleprof_error::success;
508 }
509
writeHeader(const StringMap<FunctionSamples> & ProfileMap)510 std::error_code SampleProfileWriterBinary::writeHeader(
511 const StringMap<FunctionSamples> &ProfileMap) {
512 writeMagicIdent(Format);
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();
525 return sampleprof_error::success;
526 }
527
setToCompressAllSections()528 void SampleProfileWriterExtBinaryBase::setToCompressAllSections() {
529 for (auto &Entry : SectionHdrLayout)
530 addSecFlag(Entry, SecCommonFlags::SecFlagCompress);
531 }
532
setToCompressSection(SecType Type)533 void SampleProfileWriterExtBinaryBase::setToCompressSection(SecType Type) {
534 addSectionFlag(Type, SecCommonFlags::SecFlagCompress);
535 }
536
allocSecHdrTable()537 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
538 support::endian::Writer Writer(*OutputStream, support::little);
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
writeSecHdrTable()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)
556 return sampleprof_error::ostream_seek_unsupported;
557 support::endian::Writer Writer(*OutputStream, support::little);
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)
588 return sampleprof_error::ostream_seek_unsupported;
589
590 return sampleprof_error::success;
591 }
592
writeHeader(const StringMap<FunctionSamples> & ProfileMap)593 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
594 const StringMap<FunctionSamples> &ProfileMap) {
595 auto &OS = *OutputStream;
596 FileStart = OS.tell();
597 writeMagicIdent(Format);
598
599 allocSecHdrTable();
600 return sampleprof_error::success;
601 }
602
writeHeader(const StringMap<FunctionSamples> & ProfileMap)603 std::error_code SampleProfileWriterCompactBinary::writeHeader(
604 const StringMap<FunctionSamples> &ProfileMap) {
605 support::endian::Writer Writer(*OutputStream, support::little);
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));
613 return sampleprof_error::success;
614 }
615
writeSummary()616 std::error_code SampleProfileWriterBinary::writeSummary() {
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 }
630 return sampleprof_error::success;
631 }
writeBody(const FunctionSamples & S)632 std::error_code SampleProfileWriterBinary::writeBody(const FunctionSamples &S) {
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
673 return sampleprof_error::success;
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
writeSample(const FunctionSamples & S)680 SampleProfileWriterBinary::writeSample(const FunctionSamples &S) {
681 encodeULEB128(S.getHeadSamples(), *OutputStream);
682 return writeBody(S);
683 }
684
685 std::error_code
writeSample(const FunctionSamples & S)686 SampleProfileWriterCompactBinary::writeSample(const FunctionSamples &S) {
687 uint64_t Offset = OutputStream->tell();
688 StringRef Name = S.getName();
689 FuncOffsetTable[Name] = Offset;
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.
701 ErrorOr<std::unique_ptr<SampleProfileWriter>>
create(StringRef Filename,SampleProfileFormat Format)702 SampleProfileWriter::create(StringRef Filename, SampleProfileFormat Format) {
703 std::error_code EC;
704 std::unique_ptr<raw_ostream> OS;
705 if (Format == SPF_Binary || Format == SPF_Ext_Binary ||
706 Format == SPF_Compact_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.
723 ErrorOr<std::unique_ptr<SampleProfileWriter>>
create(std::unique_ptr<raw_ostream> & OS,SampleProfileFormat Format)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)
738 EC = sampleprof_error::unsupported_writing_format;
739 else
740 EC = sampleprof_error::unrecognized_format;
741
742 if (EC)
743 return EC;
744
745 Writer->Format = Format;
746 return std::move(Writer);
747 }
748
computeSummary(const StringMap<FunctionSamples> & ProfileMap)749 void SampleProfileWriter::computeSummary(
750 const StringMap<FunctionSamples> &ProfileMap) {
751 SampleProfileSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
752 Summary = Builder.computeSummaryForProfiles(ProfileMap);
753 }
754