1 //===- SampleProfReader.cpp - Read LLVM sample profile data ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the class that reads LLVM sample profiles. It
10 // supports three file formats: text, binary and gcov.
12 // The textual representation is useful for debugging and testing purposes. The
13 // binary representation is more compact, resulting in smaller file sizes.
15 // The gcov encoding is the one generated by GCC's AutoFDO profile creation
16 // tool (https://github.com/google/autofdo)
18 // All three encodings can be used interchangeably as an input sample profile.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/ProfileData/SampleProfReader.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/IR/ProfileSummary.h"
27 #include "llvm/ProfileData/ProfileCommon.h"
28 #include "llvm/ProfileData/SampleProf.h"
29 #include "llvm/Support/ErrorOr.h"
30 #include "llvm/Support/LEB128.h"
31 #include "llvm/Support/LineIterator.h"
32 #include "llvm/Support/MD5.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/raw_ostream.h"
40 #include <system_error>
44 using namespace sampleprof
;
46 /// Dump the function profile for \p FName.
48 /// \param FName Name of the function to print.
49 /// \param OS Stream to emit the output to.
50 void SampleProfileReader::dumpFunctionProfile(StringRef FName
,
52 OS
<< "Function: " << FName
<< ": " << Profiles
[FName
];
55 /// Dump all the function profiles found on stream \p OS.
56 void SampleProfileReader::dump(raw_ostream
&OS
) {
57 for (const auto &I
: Profiles
)
58 dumpFunctionProfile(I
.getKey(), OS
);
61 /// Parse \p Input as function head.
63 /// Parse one line of \p Input, and update function name in \p FName,
64 /// function's total sample count in \p NumSamples, function's entry
65 /// count in \p NumHeadSamples.
67 /// \returns true if parsing is successful.
68 static bool ParseHead(const StringRef
&Input
, StringRef
&FName
,
69 uint64_t &NumSamples
, uint64_t &NumHeadSamples
) {
72 size_t n2
= Input
.rfind(':');
73 size_t n1
= Input
.rfind(':', n2
- 1);
74 FName
= Input
.substr(0, n1
);
75 if (Input
.substr(n1
+ 1, n2
- n1
- 1).getAsInteger(10, NumSamples
))
77 if (Input
.substr(n2
+ 1).getAsInteger(10, NumHeadSamples
))
82 /// Returns true if line offset \p L is legal (only has 16 bits).
83 static bool isOffsetLegal(unsigned L
) { return (L
& 0xffff) == L
; }
85 /// Parse \p Input as line sample.
87 /// \param Input input line.
88 /// \param IsCallsite true if the line represents an inlined callsite.
89 /// \param Depth the depth of the inline stack.
90 /// \param NumSamples total samples of the line/inlined callsite.
91 /// \param LineOffset line offset to the start of the function.
92 /// \param Discriminator discriminator of the line.
93 /// \param TargetCountMap map from indirect call target to count.
95 /// returns true if parsing is successful.
96 static bool ParseLine(const StringRef
&Input
, bool &IsCallsite
, uint32_t &Depth
,
97 uint64_t &NumSamples
, uint32_t &LineOffset
,
98 uint32_t &Discriminator
, StringRef
&CalleeName
,
99 DenseMap
<StringRef
, uint64_t> &TargetCountMap
) {
100 for (Depth
= 0; Input
[Depth
] == ' '; Depth
++)
105 size_t n1
= Input
.find(':');
106 StringRef Loc
= Input
.substr(Depth
, n1
- Depth
);
107 size_t n2
= Loc
.find('.');
108 if (n2
== StringRef::npos
) {
109 if (Loc
.getAsInteger(10, LineOffset
) || !isOffsetLegal(LineOffset
))
113 if (Loc
.substr(0, n2
).getAsInteger(10, LineOffset
))
115 if (Loc
.substr(n2
+ 1).getAsInteger(10, Discriminator
))
119 StringRef Rest
= Input
.substr(n1
+ 2);
120 if (Rest
[0] >= '0' && Rest
[0] <= '9') {
122 size_t n3
= Rest
.find(' ');
123 if (n3
== StringRef::npos
) {
124 if (Rest
.getAsInteger(10, NumSamples
))
127 if (Rest
.substr(0, n3
).getAsInteger(10, NumSamples
))
130 // Find call targets and their sample counts.
131 // Note: In some cases, there are symbols in the profile which are not
132 // mangled. To accommodate such cases, use colon + integer pairs as the
135 // _M_construct<char *>:1000 string_view<std::allocator<char> >:437
136 // ":1000" and ":437" are used as anchor points so the string above will
138 // target: _M_construct<char *>
140 // target: string_view<std::allocator<char> >
142 while (n3
!= StringRef::npos
) {
143 n3
+= Rest
.substr(n3
).find_first_not_of(' ');
144 Rest
= Rest
.substr(n3
);
145 n3
= Rest
.find_first_of(':');
146 if (n3
== StringRef::npos
|| n3
== 0)
152 // Get the segment after the current colon.
153 StringRef AfterColon
= Rest
.substr(n3
+ 1);
154 // Get the target symbol before the current colon.
155 Target
= Rest
.substr(0, n3
);
156 // Check if the word after the current colon is an integer.
157 n4
= AfterColon
.find_first_of(' ');
158 n4
= (n4
!= StringRef::npos
) ? n3
+ n4
+ 1 : Rest
.size();
159 StringRef WordAfterColon
= Rest
.substr(n3
+ 1, n4
- n3
- 1);
160 if (!WordAfterColon
.getAsInteger(10, count
))
163 // Try to find the next colon.
164 uint64_t n5
= AfterColon
.find_first_of(':');
165 if (n5
== StringRef::npos
)
170 // An anchor point is found. Save the {target, count} pair
171 TargetCountMap
[Target
] = count
;
172 if (n4
== Rest
.size())
174 // Change n3 to the next blank space after colon + integer pair.
179 size_t n3
= Rest
.find_last_of(':');
180 CalleeName
= Rest
.substr(0, n3
);
181 if (Rest
.substr(n3
+ 1).getAsInteger(10, NumSamples
))
187 /// Load samples from a text file.
189 /// See the documentation at the top of the file for an explanation of
190 /// the expected format.
192 /// \returns true if the file was loaded successfully, false otherwise.
193 std::error_code
SampleProfileReaderText::read() {
194 line_iterator
LineIt(*Buffer
, /*SkipBlanks=*/true, '#');
195 sampleprof_error Result
= sampleprof_error::success
;
197 InlineCallStack InlineStack
;
199 for (; !LineIt
.is_at_eof(); ++LineIt
) {
200 if ((*LineIt
)[(*LineIt
).find_first_not_of(' ')] == '#')
202 // Read the header of each function.
204 // Note that for function identifiers we are actually expecting
205 // mangled names, but we may not always get them. This happens when
206 // the compiler decides not to emit the function (e.g., it was inlined
207 // and removed). In this case, the binary will not have the linkage
208 // name for the function, so the profiler will emit the function's
209 // unmangled name, which may contain characters like ':' and '>' in its
210 // name (member functions, templates, etc).
212 // The only requirement we place on the identifier, then, is that it
213 // should not begin with a number.
214 if ((*LineIt
)[0] != ' ') {
215 uint64_t NumSamples
, NumHeadSamples
;
217 if (!ParseHead(*LineIt
, FName
, NumSamples
, NumHeadSamples
)) {
218 reportError(LineIt
.line_number(),
219 "Expected 'mangled_name:NUM:NUM', found " + *LineIt
);
220 return sampleprof_error::malformed
;
222 Profiles
[FName
] = FunctionSamples();
223 FunctionSamples
&FProfile
= Profiles
[FName
];
224 FProfile
.setName(FName
);
225 MergeResult(Result
, FProfile
.addTotalSamples(NumSamples
));
226 MergeResult(Result
, FProfile
.addHeadSamples(NumHeadSamples
));
228 InlineStack
.push_back(&FProfile
);
232 DenseMap
<StringRef
, uint64_t> TargetCountMap
;
234 uint32_t Depth
, LineOffset
, Discriminator
;
235 if (!ParseLine(*LineIt
, IsCallsite
, Depth
, NumSamples
, LineOffset
,
236 Discriminator
, FName
, TargetCountMap
)) {
237 reportError(LineIt
.line_number(),
238 "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
240 return sampleprof_error::malformed
;
243 while (InlineStack
.size() > Depth
) {
244 InlineStack
.pop_back();
246 FunctionSamples
&FSamples
= InlineStack
.back()->functionSamplesAt(
247 LineLocation(LineOffset
, Discriminator
))[FName
];
248 FSamples
.setName(FName
);
249 MergeResult(Result
, FSamples
.addTotalSamples(NumSamples
));
250 InlineStack
.push_back(&FSamples
);
252 while (InlineStack
.size() > Depth
) {
253 InlineStack
.pop_back();
255 FunctionSamples
&FProfile
= *InlineStack
.back();
256 for (const auto &name_count
: TargetCountMap
) {
257 MergeResult(Result
, FProfile
.addCalledTargetSamples(
258 LineOffset
, Discriminator
, name_count
.first
,
261 MergeResult(Result
, FProfile
.addBodySamples(LineOffset
, Discriminator
,
266 if (Result
== sampleprof_error::success
)
272 bool SampleProfileReaderText::hasFormat(const MemoryBuffer
&Buffer
) {
275 // Check that the first non-comment line is a valid function header.
276 line_iterator
LineIt(Buffer
, /*SkipBlanks=*/true, '#');
277 if (!LineIt
.is_at_eof()) {
278 if ((*LineIt
)[0] != ' ') {
279 uint64_t NumSamples
, NumHeadSamples
;
281 result
= ParseHead(*LineIt
, FName
, NumSamples
, NumHeadSamples
);
288 template <typename T
> ErrorOr
<T
> SampleProfileReaderBinary::readNumber() {
289 unsigned NumBytesRead
= 0;
291 uint64_t Val
= decodeULEB128(Data
, &NumBytesRead
);
293 if (Val
> std::numeric_limits
<T
>::max())
294 EC
= sampleprof_error::malformed
;
295 else if (Data
+ NumBytesRead
> End
)
296 EC
= sampleprof_error::truncated
;
298 EC
= sampleprof_error::success
;
301 reportError(0, EC
.message());
305 Data
+= NumBytesRead
;
306 return static_cast<T
>(Val
);
309 ErrorOr
<StringRef
> SampleProfileReaderBinary::readString() {
311 StringRef
Str(reinterpret_cast<const char *>(Data
));
312 if (Data
+ Str
.size() + 1 > End
) {
313 EC
= sampleprof_error::truncated
;
314 reportError(0, EC
.message());
318 Data
+= Str
.size() + 1;
322 template <typename T
>
323 ErrorOr
<T
> SampleProfileReaderBinary::readUnencodedNumber() {
326 if (Data
+ sizeof(T
) > End
) {
327 EC
= sampleprof_error::truncated
;
328 reportError(0, EC
.message());
332 using namespace support
;
333 T Val
= endian::readNext
<T
, little
, unaligned
>(Data
);
337 template <typename T
>
338 inline ErrorOr
<uint32_t> SampleProfileReaderBinary::readStringIndex(T
&Table
) {
340 auto Idx
= readNumber
<uint32_t>();
341 if (std::error_code EC
= Idx
.getError())
343 if (*Idx
>= Table
.size())
344 return sampleprof_error::truncated_name_table
;
348 ErrorOr
<StringRef
> SampleProfileReaderBinary::readStringFromTable() {
349 auto Idx
= readStringIndex(NameTable
);
350 if (std::error_code EC
= Idx
.getError())
353 return NameTable
[*Idx
];
356 ErrorOr
<StringRef
> SampleProfileReaderCompactBinary::readStringFromTable() {
357 auto Idx
= readStringIndex(NameTable
);
358 if (std::error_code EC
= Idx
.getError())
361 return StringRef(NameTable
[*Idx
]);
365 SampleProfileReaderBinary::readProfile(FunctionSamples
&FProfile
) {
366 auto NumSamples
= readNumber
<uint64_t>();
367 if (std::error_code EC
= NumSamples
.getError())
369 FProfile
.addTotalSamples(*NumSamples
);
371 // Read the samples in the body.
372 auto NumRecords
= readNumber
<uint32_t>();
373 if (std::error_code EC
= NumRecords
.getError())
376 for (uint32_t I
= 0; I
< *NumRecords
; ++I
) {
377 auto LineOffset
= readNumber
<uint64_t>();
378 if (std::error_code EC
= LineOffset
.getError())
381 if (!isOffsetLegal(*LineOffset
)) {
382 return std::error_code();
385 auto Discriminator
= readNumber
<uint64_t>();
386 if (std::error_code EC
= Discriminator
.getError())
389 auto NumSamples
= readNumber
<uint64_t>();
390 if (std::error_code EC
= NumSamples
.getError())
393 auto NumCalls
= readNumber
<uint32_t>();
394 if (std::error_code EC
= NumCalls
.getError())
397 for (uint32_t J
= 0; J
< *NumCalls
; ++J
) {
398 auto CalledFunction(readStringFromTable());
399 if (std::error_code EC
= CalledFunction
.getError())
402 auto CalledFunctionSamples
= readNumber
<uint64_t>();
403 if (std::error_code EC
= CalledFunctionSamples
.getError())
406 FProfile
.addCalledTargetSamples(*LineOffset
, *Discriminator
,
407 *CalledFunction
, *CalledFunctionSamples
);
410 FProfile
.addBodySamples(*LineOffset
, *Discriminator
, *NumSamples
);
413 // Read all the samples for inlined function calls.
414 auto NumCallsites
= readNumber
<uint32_t>();
415 if (std::error_code EC
= NumCallsites
.getError())
418 for (uint32_t J
= 0; J
< *NumCallsites
; ++J
) {
419 auto LineOffset
= readNumber
<uint64_t>();
420 if (std::error_code EC
= LineOffset
.getError())
423 auto Discriminator
= readNumber
<uint64_t>();
424 if (std::error_code EC
= Discriminator
.getError())
427 auto FName(readStringFromTable());
428 if (std::error_code EC
= FName
.getError())
431 FunctionSamples
&CalleeProfile
= FProfile
.functionSamplesAt(
432 LineLocation(*LineOffset
, *Discriminator
))[*FName
];
433 CalleeProfile
.setName(*FName
);
434 if (std::error_code EC
= readProfile(CalleeProfile
))
438 return sampleprof_error::success
;
441 std::error_code
SampleProfileReaderBinary::readFuncProfile() {
442 auto NumHeadSamples
= readNumber
<uint64_t>();
443 if (std::error_code EC
= NumHeadSamples
.getError())
446 auto FName(readStringFromTable());
447 if (std::error_code EC
= FName
.getError())
450 Profiles
[*FName
] = FunctionSamples();
451 FunctionSamples
&FProfile
= Profiles
[*FName
];
452 FProfile
.setName(*FName
);
454 FProfile
.addHeadSamples(*NumHeadSamples
);
456 if (std::error_code EC
= readProfile(FProfile
))
458 return sampleprof_error::success
;
461 std::error_code
SampleProfileReaderBinary::read() {
463 if (std::error_code EC
= readFuncProfile())
467 return sampleprof_error::success
;
471 SampleProfileReaderExtBinary::readOneSection(const uint8_t *Start
,
472 uint64_t Size
, SecType Type
) {
476 if (std::error_code EC
= readSummary())
480 if (std::error_code EC
= readNameTable())
484 while (Data
< Start
+ Size
) {
485 if (std::error_code EC
= readFuncProfile())
489 case SecProfileSymbolList
:
490 if (std::error_code EC
= readProfileSymbolList())
496 return sampleprof_error::success
;
499 std::error_code
SampleProfileReaderExtBinary::readProfileSymbolList() {
500 auto UncompressSize
= readNumber
<uint64_t>();
501 if (std::error_code EC
= UncompressSize
.getError())
504 auto CompressSize
= readNumber
<uint64_t>();
505 if (std::error_code EC
= CompressSize
.getError())
509 ProfSymList
= std::make_unique
<ProfileSymbolList
>();
511 if (std::error_code EC
=
512 ProfSymList
->read(*CompressSize
, *UncompressSize
, Data
))
515 // CompressSize is zero only when ProfileSymbolList is not compressed.
516 if (*CompressSize
== 0)
517 Data
= Data
+ *UncompressSize
;
519 Data
= Data
+ *CompressSize
;
520 return sampleprof_error::success
;
523 std::error_code
SampleProfileReaderExtBinaryBase::read() {
524 const uint8_t *BufStart
=
525 reinterpret_cast<const uint8_t *>(Buffer
->getBufferStart());
527 for (auto &Entry
: SecHdrTable
) {
528 // Skip empty section.
531 const uint8_t *SecStart
= BufStart
+ Entry
.Offset
;
532 if (std::error_code EC
= readOneSection(SecStart
, Entry
.Size
, Entry
.Type
))
534 if (Data
!= SecStart
+ Entry
.Size
)
535 return sampleprof_error::malformed
;
538 return sampleprof_error::success
;
541 std::error_code
SampleProfileReaderCompactBinary::read() {
542 std::vector
<uint64_t> OffsetsToUse
;
544 for (auto FuncEntry
: FuncOffsetTable
) {
545 OffsetsToUse
.push_back(FuncEntry
.second
);
549 for (auto Name
: FuncsToUse
) {
550 auto GUID
= std::to_string(MD5Hash(Name
));
551 auto iter
= FuncOffsetTable
.find(StringRef(GUID
));
552 if (iter
== FuncOffsetTable
.end())
554 OffsetsToUse
.push_back(iter
->second
);
558 for (auto Offset
: OffsetsToUse
) {
559 const uint8_t *SavedData
= Data
;
560 Data
= reinterpret_cast<const uint8_t *>(Buffer
->getBufferStart()) +
562 if (std::error_code EC
= readFuncProfile())
566 return sampleprof_error::success
;
569 std::error_code
SampleProfileReaderRawBinary::verifySPMagic(uint64_t Magic
) {
570 if (Magic
== SPMagic())
571 return sampleprof_error::success
;
572 return sampleprof_error::bad_magic
;
575 std::error_code
SampleProfileReaderExtBinary::verifySPMagic(uint64_t Magic
) {
576 if (Magic
== SPMagic(SPF_Ext_Binary
))
577 return sampleprof_error::success
;
578 return sampleprof_error::bad_magic
;
582 SampleProfileReaderCompactBinary::verifySPMagic(uint64_t Magic
) {
583 if (Magic
== SPMagic(SPF_Compact_Binary
))
584 return sampleprof_error::success
;
585 return sampleprof_error::bad_magic
;
588 std::error_code
SampleProfileReaderBinary::readNameTable() {
589 auto Size
= readNumber
<uint32_t>();
590 if (std::error_code EC
= Size
.getError())
592 NameTable
.reserve(*Size
);
593 for (uint32_t I
= 0; I
< *Size
; ++I
) {
594 auto Name(readString());
595 if (std::error_code EC
= Name
.getError())
597 NameTable
.push_back(*Name
);
600 return sampleprof_error::success
;
603 std::error_code
SampleProfileReaderCompactBinary::readNameTable() {
604 auto Size
= readNumber
<uint64_t>();
605 if (std::error_code EC
= Size
.getError())
607 NameTable
.reserve(*Size
);
608 for (uint32_t I
= 0; I
< *Size
; ++I
) {
609 auto FID
= readNumber
<uint64_t>();
610 if (std::error_code EC
= FID
.getError())
612 NameTable
.push_back(std::to_string(*FID
));
614 return sampleprof_error::success
;
617 std::error_code
SampleProfileReaderExtBinaryBase::readSecHdrTableEntry() {
618 SecHdrTableEntry Entry
;
619 auto Type
= readUnencodedNumber
<uint64_t>();
620 if (std::error_code EC
= Type
.getError())
622 Entry
.Type
= static_cast<SecType
>(*Type
);
624 auto Flag
= readUnencodedNumber
<uint64_t>();
625 if (std::error_code EC
= Flag
.getError())
629 auto Offset
= readUnencodedNumber
<uint64_t>();
630 if (std::error_code EC
= Offset
.getError())
632 Entry
.Offset
= *Offset
;
634 auto Size
= readUnencodedNumber
<uint64_t>();
635 if (std::error_code EC
= Size
.getError())
639 SecHdrTable
.push_back(std::move(Entry
));
640 return sampleprof_error::success
;
643 std::error_code
SampleProfileReaderExtBinaryBase::readSecHdrTable() {
644 auto EntryNum
= readUnencodedNumber
<uint64_t>();
645 if (std::error_code EC
= EntryNum
.getError())
648 for (uint32_t i
= 0; i
< (*EntryNum
); i
++)
649 if (std::error_code EC
= readSecHdrTableEntry())
652 return sampleprof_error::success
;
655 std::error_code
SampleProfileReaderExtBinaryBase::readHeader() {
656 const uint8_t *BufStart
=
657 reinterpret_cast<const uint8_t *>(Buffer
->getBufferStart());
659 End
= BufStart
+ Buffer
->getBufferSize();
661 if (std::error_code EC
= readMagicIdent())
664 if (std::error_code EC
= readSecHdrTable())
667 return sampleprof_error::success
;
670 std::error_code
SampleProfileReaderBinary::readMagicIdent() {
671 // Read and check the magic identifier.
672 auto Magic
= readNumber
<uint64_t>();
673 if (std::error_code EC
= Magic
.getError())
675 else if (std::error_code EC
= verifySPMagic(*Magic
))
678 // Read the version number.
679 auto Version
= readNumber
<uint64_t>();
680 if (std::error_code EC
= Version
.getError())
682 else if (*Version
!= SPVersion())
683 return sampleprof_error::unsupported_version
;
685 return sampleprof_error::success
;
688 std::error_code
SampleProfileReaderBinary::readHeader() {
689 Data
= reinterpret_cast<const uint8_t *>(Buffer
->getBufferStart());
690 End
= Data
+ Buffer
->getBufferSize();
692 if (std::error_code EC
= readMagicIdent())
695 if (std::error_code EC
= readSummary())
698 if (std::error_code EC
= readNameTable())
700 return sampleprof_error::success
;
703 std::error_code
SampleProfileReaderCompactBinary::readHeader() {
704 SampleProfileReaderBinary::readHeader();
705 if (std::error_code EC
= readFuncOffsetTable())
707 return sampleprof_error::success
;
710 std::error_code
SampleProfileReaderCompactBinary::readFuncOffsetTable() {
711 auto TableOffset
= readUnencodedNumber
<uint64_t>();
712 if (std::error_code EC
= TableOffset
.getError())
715 const uint8_t *SavedData
= Data
;
716 const uint8_t *TableStart
=
717 reinterpret_cast<const uint8_t *>(Buffer
->getBufferStart()) +
721 auto Size
= readNumber
<uint64_t>();
722 if (std::error_code EC
= Size
.getError())
725 FuncOffsetTable
.reserve(*Size
);
726 for (uint32_t I
= 0; I
< *Size
; ++I
) {
727 auto FName(readStringFromTable());
728 if (std::error_code EC
= FName
.getError())
731 auto Offset
= readNumber
<uint64_t>();
732 if (std::error_code EC
= Offset
.getError())
735 FuncOffsetTable
[*FName
] = *Offset
;
739 return sampleprof_error::success
;
742 void SampleProfileReaderCompactBinary::collectFuncsToUse(const Module
&M
) {
746 StringRef CanonName
= FunctionSamples::getCanonicalFnName(F
);
747 FuncsToUse
.insert(CanonName
);
751 std::error_code
SampleProfileReaderBinary::readSummaryEntry(
752 std::vector
<ProfileSummaryEntry
> &Entries
) {
753 auto Cutoff
= readNumber
<uint64_t>();
754 if (std::error_code EC
= Cutoff
.getError())
757 auto MinBlockCount
= readNumber
<uint64_t>();
758 if (std::error_code EC
= MinBlockCount
.getError())
761 auto NumBlocks
= readNumber
<uint64_t>();
762 if (std::error_code EC
= NumBlocks
.getError())
765 Entries
.emplace_back(*Cutoff
, *MinBlockCount
, *NumBlocks
);
766 return sampleprof_error::success
;
769 std::error_code
SampleProfileReaderBinary::readSummary() {
770 auto TotalCount
= readNumber
<uint64_t>();
771 if (std::error_code EC
= TotalCount
.getError())
774 auto MaxBlockCount
= readNumber
<uint64_t>();
775 if (std::error_code EC
= MaxBlockCount
.getError())
778 auto MaxFunctionCount
= readNumber
<uint64_t>();
779 if (std::error_code EC
= MaxFunctionCount
.getError())
782 auto NumBlocks
= readNumber
<uint64_t>();
783 if (std::error_code EC
= NumBlocks
.getError())
786 auto NumFunctions
= readNumber
<uint64_t>();
787 if (std::error_code EC
= NumFunctions
.getError())
790 auto NumSummaryEntries
= readNumber
<uint64_t>();
791 if (std::error_code EC
= NumSummaryEntries
.getError())
794 std::vector
<ProfileSummaryEntry
> Entries
;
795 for (unsigned i
= 0; i
< *NumSummaryEntries
; i
++) {
796 std::error_code EC
= readSummaryEntry(Entries
);
797 if (EC
!= sampleprof_error::success
)
800 Summary
= std::make_unique
<ProfileSummary
>(
801 ProfileSummary::PSK_Sample
, Entries
, *TotalCount
, *MaxBlockCount
, 0,
802 *MaxFunctionCount
, *NumBlocks
, *NumFunctions
);
804 return sampleprof_error::success
;
807 bool SampleProfileReaderRawBinary::hasFormat(const MemoryBuffer
&Buffer
) {
808 const uint8_t *Data
=
809 reinterpret_cast<const uint8_t *>(Buffer
.getBufferStart());
810 uint64_t Magic
= decodeULEB128(Data
);
811 return Magic
== SPMagic();
814 bool SampleProfileReaderExtBinary::hasFormat(const MemoryBuffer
&Buffer
) {
815 const uint8_t *Data
=
816 reinterpret_cast<const uint8_t *>(Buffer
.getBufferStart());
817 uint64_t Magic
= decodeULEB128(Data
);
818 return Magic
== SPMagic(SPF_Ext_Binary
);
821 bool SampleProfileReaderCompactBinary::hasFormat(const MemoryBuffer
&Buffer
) {
822 const uint8_t *Data
=
823 reinterpret_cast<const uint8_t *>(Buffer
.getBufferStart());
824 uint64_t Magic
= decodeULEB128(Data
);
825 return Magic
== SPMagic(SPF_Compact_Binary
);
828 std::error_code
SampleProfileReaderGCC::skipNextWord() {
830 if (!GcovBuffer
.readInt(dummy
))
831 return sampleprof_error::truncated
;
832 return sampleprof_error::success
;
835 template <typename T
> ErrorOr
<T
> SampleProfileReaderGCC::readNumber() {
836 if (sizeof(T
) <= sizeof(uint32_t)) {
838 if (GcovBuffer
.readInt(Val
) && Val
<= std::numeric_limits
<T
>::max())
839 return static_cast<T
>(Val
);
840 } else if (sizeof(T
) <= sizeof(uint64_t)) {
842 if (GcovBuffer
.readInt64(Val
) && Val
<= std::numeric_limits
<T
>::max())
843 return static_cast<T
>(Val
);
846 std::error_code EC
= sampleprof_error::malformed
;
847 reportError(0, EC
.message());
851 ErrorOr
<StringRef
> SampleProfileReaderGCC::readString() {
853 if (!GcovBuffer
.readString(Str
))
854 return sampleprof_error::truncated
;
858 std::error_code
SampleProfileReaderGCC::readHeader() {
859 // Read the magic identifier.
860 if (!GcovBuffer
.readGCDAFormat())
861 return sampleprof_error::unrecognized_format
;
863 // Read the version number. Note - the GCC reader does not validate this
864 // version, but the profile creator generates v704.
865 GCOV::GCOVVersion version
;
866 if (!GcovBuffer
.readGCOVVersion(version
))
867 return sampleprof_error::unrecognized_format
;
869 if (version
!= GCOV::V704
)
870 return sampleprof_error::unsupported_version
;
872 // Skip the empty integer.
873 if (std::error_code EC
= skipNextWord())
876 return sampleprof_error::success
;
879 std::error_code
SampleProfileReaderGCC::readSectionTag(uint32_t Expected
) {
881 if (!GcovBuffer
.readInt(Tag
))
882 return sampleprof_error::truncated
;
885 return sampleprof_error::malformed
;
887 if (std::error_code EC
= skipNextWord())
890 return sampleprof_error::success
;
893 std::error_code
SampleProfileReaderGCC::readNameTable() {
894 if (std::error_code EC
= readSectionTag(GCOVTagAFDOFileNames
))
898 if (!GcovBuffer
.readInt(Size
))
899 return sampleprof_error::truncated
;
901 for (uint32_t I
= 0; I
< Size
; ++I
) {
903 if (!GcovBuffer
.readString(Str
))
904 return sampleprof_error::truncated
;
905 Names
.push_back(Str
);
908 return sampleprof_error::success
;
911 std::error_code
SampleProfileReaderGCC::readFunctionProfiles() {
912 if (std::error_code EC
= readSectionTag(GCOVTagAFDOFunction
))
915 uint32_t NumFunctions
;
916 if (!GcovBuffer
.readInt(NumFunctions
))
917 return sampleprof_error::truncated
;
919 InlineCallStack Stack
;
920 for (uint32_t I
= 0; I
< NumFunctions
; ++I
)
921 if (std::error_code EC
= readOneFunctionProfile(Stack
, true, 0))
925 return sampleprof_error::success
;
928 std::error_code
SampleProfileReaderGCC::readOneFunctionProfile(
929 const InlineCallStack
&InlineStack
, bool Update
, uint32_t Offset
) {
930 uint64_t HeadCount
= 0;
931 if (InlineStack
.size() == 0)
932 if (!GcovBuffer
.readInt64(HeadCount
))
933 return sampleprof_error::truncated
;
936 if (!GcovBuffer
.readInt(NameIdx
))
937 return sampleprof_error::truncated
;
939 StringRef
Name(Names
[NameIdx
]);
941 uint32_t NumPosCounts
;
942 if (!GcovBuffer
.readInt(NumPosCounts
))
943 return sampleprof_error::truncated
;
945 uint32_t NumCallsites
;
946 if (!GcovBuffer
.readInt(NumCallsites
))
947 return sampleprof_error::truncated
;
949 FunctionSamples
*FProfile
= nullptr;
950 if (InlineStack
.size() == 0) {
951 // If this is a top function that we have already processed, do not
952 // update its profile again. This happens in the presence of
953 // function aliases. Since these aliases share the same function
954 // body, there will be identical replicated profiles for the
955 // original function. In this case, we simply not bother updating
956 // the profile of the original function.
957 FProfile
= &Profiles
[Name
];
958 FProfile
->addHeadSamples(HeadCount
);
959 if (FProfile
->getTotalSamples() > 0)
962 // Otherwise, we are reading an inlined instance. The top of the
963 // inline stack contains the profile of the caller. Insert this
964 // callee in the caller's CallsiteMap.
965 FunctionSamples
*CallerProfile
= InlineStack
.front();
966 uint32_t LineOffset
= Offset
>> 16;
967 uint32_t Discriminator
= Offset
& 0xffff;
968 FProfile
= &CallerProfile
->functionSamplesAt(
969 LineLocation(LineOffset
, Discriminator
))[Name
];
971 FProfile
->setName(Name
);
973 for (uint32_t I
= 0; I
< NumPosCounts
; ++I
) {
975 if (!GcovBuffer
.readInt(Offset
))
976 return sampleprof_error::truncated
;
979 if (!GcovBuffer
.readInt(NumTargets
))
980 return sampleprof_error::truncated
;
983 if (!GcovBuffer
.readInt64(Count
))
984 return sampleprof_error::truncated
;
986 // The line location is encoded in the offset as:
987 // high 16 bits: line offset to the start of the function.
988 // low 16 bits: discriminator.
989 uint32_t LineOffset
= Offset
>> 16;
990 uint32_t Discriminator
= Offset
& 0xffff;
992 InlineCallStack NewStack
;
993 NewStack
.push_back(FProfile
);
994 NewStack
.insert(NewStack
.end(), InlineStack
.begin(), InlineStack
.end());
996 // Walk up the inline stack, adding the samples on this line to
997 // the total sample count of the callers in the chain.
998 for (auto CallerProfile
: NewStack
)
999 CallerProfile
->addTotalSamples(Count
);
1001 // Update the body samples for the current profile.
1002 FProfile
->addBodySamples(LineOffset
, Discriminator
, Count
);
1005 // Process the list of functions called at an indirect call site.
1006 // These are all the targets that a function pointer (or virtual
1007 // function) resolved at runtime.
1008 for (uint32_t J
= 0; J
< NumTargets
; J
++) {
1010 if (!GcovBuffer
.readInt(HistVal
))
1011 return sampleprof_error::truncated
;
1013 if (HistVal
!= HIST_TYPE_INDIR_CALL_TOPN
)
1014 return sampleprof_error::malformed
;
1017 if (!GcovBuffer
.readInt64(TargetIdx
))
1018 return sampleprof_error::truncated
;
1019 StringRef
TargetName(Names
[TargetIdx
]);
1021 uint64_t TargetCount
;
1022 if (!GcovBuffer
.readInt64(TargetCount
))
1023 return sampleprof_error::truncated
;
1026 FProfile
->addCalledTargetSamples(LineOffset
, Discriminator
,
1027 TargetName
, TargetCount
);
1031 // Process all the inlined callers into the current function. These
1032 // are all the callsites that were inlined into this function.
1033 for (uint32_t I
= 0; I
< NumCallsites
; I
++) {
1034 // The offset is encoded as:
1035 // high 16 bits: line offset to the start of the function.
1036 // low 16 bits: discriminator.
1038 if (!GcovBuffer
.readInt(Offset
))
1039 return sampleprof_error::truncated
;
1040 InlineCallStack NewStack
;
1041 NewStack
.push_back(FProfile
);
1042 NewStack
.insert(NewStack
.end(), InlineStack
.begin(), InlineStack
.end());
1043 if (std::error_code EC
= readOneFunctionProfile(NewStack
, Update
, Offset
))
1047 return sampleprof_error::success
;
1050 /// Read a GCC AutoFDO profile.
1052 /// This format is generated by the Linux Perf conversion tool at
1053 /// https://github.com/google/autofdo.
1054 std::error_code
SampleProfileReaderGCC::read() {
1055 // Read the string table.
1056 if (std::error_code EC
= readNameTable())
1059 // Read the source profile.
1060 if (std::error_code EC
= readFunctionProfiles())
1063 return sampleprof_error::success
;
1066 bool SampleProfileReaderGCC::hasFormat(const MemoryBuffer
&Buffer
) {
1067 StringRef
Magic(reinterpret_cast<const char *>(Buffer
.getBufferStart()));
1068 return Magic
== "adcg*704";
1071 std::error_code
SampleProfileReaderItaniumRemapper::read() {
1072 // If the underlying data is in compact format, we can't remap it because
1073 // we don't know what the original function names were.
1074 if (getFormat() == SPF_Compact_Binary
) {
1075 Ctx
.diagnose(DiagnosticInfoSampleProfile(
1076 Buffer
->getBufferIdentifier(),
1077 "Profile data remapping cannot be applied to profile data "
1078 "in compact format (original mangled names are not available).",
1080 return sampleprof_error::success
;
1083 if (Error E
= Remappings
.read(*Buffer
)) {
1085 std::move(E
), [&](const SymbolRemappingParseError
&ParseError
) {
1086 reportError(ParseError
.getLineNum(), ParseError
.getMessage());
1088 return sampleprof_error::malformed
;
1091 for (auto &Sample
: getProfiles())
1092 if (auto Key
= Remappings
.insert(Sample
.first()))
1093 SampleMap
.insert({Key
, &Sample
.second
});
1095 return sampleprof_error::success
;
1099 SampleProfileReaderItaniumRemapper::getSamplesFor(StringRef Fname
) {
1100 if (auto Key
= Remappings
.lookup(Fname
))
1101 return SampleMap
.lookup(Key
);
1102 return SampleProfileReader::getSamplesFor(Fname
);
1105 /// Prepare a memory buffer for the contents of \p Filename.
1107 /// \returns an error code indicating the status of the buffer.
1108 static ErrorOr
<std::unique_ptr
<MemoryBuffer
>>
1109 setupMemoryBuffer(const Twine
&Filename
) {
1110 auto BufferOrErr
= MemoryBuffer::getFileOrSTDIN(Filename
);
1111 if (std::error_code EC
= BufferOrErr
.getError())
1113 auto Buffer
= std::move(BufferOrErr
.get());
1115 // Sanity check the file.
1116 if (uint64_t(Buffer
->getBufferSize()) > std::numeric_limits
<uint32_t>::max())
1117 return sampleprof_error::too_large
;
1119 return std::move(Buffer
);
1122 /// Create a sample profile reader based on the format of the input file.
1124 /// \param Filename The file to open.
1126 /// \param C The LLVM context to use to emit diagnostics.
1128 /// \returns an error code indicating the status of the created reader.
1129 ErrorOr
<std::unique_ptr
<SampleProfileReader
>>
1130 SampleProfileReader::create(const Twine
&Filename
, LLVMContext
&C
) {
1131 auto BufferOrError
= setupMemoryBuffer(Filename
);
1132 if (std::error_code EC
= BufferOrError
.getError())
1134 return create(BufferOrError
.get(), C
);
1137 /// Create a sample profile remapper from the given input, to remap the
1138 /// function names in the given profile data.
1140 /// \param Filename The file to open.
1142 /// \param C The LLVM context to use to emit diagnostics.
1144 /// \param Underlying The underlying profile data reader to remap.
1146 /// \returns an error code indicating the status of the created reader.
1147 ErrorOr
<std::unique_ptr
<SampleProfileReader
>>
1148 SampleProfileReaderItaniumRemapper::create(
1149 const Twine
&Filename
, LLVMContext
&C
,
1150 std::unique_ptr
<SampleProfileReader
> Underlying
) {
1151 auto BufferOrError
= setupMemoryBuffer(Filename
);
1152 if (std::error_code EC
= BufferOrError
.getError())
1154 return std::make_unique
<SampleProfileReaderItaniumRemapper
>(
1155 std::move(BufferOrError
.get()), C
, std::move(Underlying
));
1158 /// Create a sample profile reader based on the format of the input data.
1160 /// \param B The memory buffer to create the reader from (assumes ownership).
1162 /// \param C The LLVM context to use to emit diagnostics.
1164 /// \returns an error code indicating the status of the created reader.
1165 ErrorOr
<std::unique_ptr
<SampleProfileReader
>>
1166 SampleProfileReader::create(std::unique_ptr
<MemoryBuffer
> &B
, LLVMContext
&C
) {
1167 std::unique_ptr
<SampleProfileReader
> Reader
;
1168 if (SampleProfileReaderRawBinary::hasFormat(*B
))
1169 Reader
.reset(new SampleProfileReaderRawBinary(std::move(B
), C
));
1170 else if (SampleProfileReaderExtBinary::hasFormat(*B
))
1171 Reader
.reset(new SampleProfileReaderExtBinary(std::move(B
), C
));
1172 else if (SampleProfileReaderCompactBinary::hasFormat(*B
))
1173 Reader
.reset(new SampleProfileReaderCompactBinary(std::move(B
), C
));
1174 else if (SampleProfileReaderGCC::hasFormat(*B
))
1175 Reader
.reset(new SampleProfileReaderGCC(std::move(B
), C
));
1176 else if (SampleProfileReaderText::hasFormat(*B
))
1177 Reader
.reset(new SampleProfileReaderText(std::move(B
), C
));
1179 return sampleprof_error::unrecognized_format
;
1181 FunctionSamples::Format
= Reader
->getFormat();
1182 if (std::error_code EC
= Reader
->readHeader()) {
1186 return std::move(Reader
);
1189 // For text and GCC file formats, we compute the summary after reading the
1190 // profile. Binary format has the profile summary in its header.
1191 void SampleProfileReader::computeSummary() {
1192 SampleProfileSummaryBuilder
Builder(ProfileSummaryBuilder::DefaultCutoffs
);
1193 for (const auto &I
: Profiles
) {
1194 const FunctionSamples
&Profile
= I
.second
;
1195 Builder
.addRecord(Profile
);
1197 Summary
= Builder
.getSummary();