40 #include <system_error>
44 using namespace sampleprof;
52 OS <<
"Function: " << FName <<
": " <<
Profiles[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);
97 uint64_t &NumSamples,
uint32_t &LineOffset,
100 for (Depth = 0; Input[
Depth] ==
' '; Depth++)
105 size_t n1 = Input.
find(
':');
107 size_t n2 = Loc.
find(
'.');
120 if (Rest[0] >=
'0' && Rest[0] <=
'9') {
122 size_t n3 = Rest.
find(
' ');
136 pair = Rest.
substr(0, n3);
138 size_t n4 = pair.
find(
':');
147 CalleeName = Rest.
substr(0, n3);
167 if ((*LineIt)[(*LineIt).find_first_not_of(
' ')] ==
'#')
181 if ((*LineIt)[0] !=
' ') {
182 uint64_t NumSamples, NumHeadSamples;
184 if (!
ParseHead(*LineIt, FName, NumSamples, NumHeadSamples)) {
186 "Expected 'mangled_name:NUM:NUM', found " + *LineIt);
202 if (!
ParseLine(*LineIt, IsCallsite, Depth, NumSamples, LineOffset,
203 Discriminator, FName, TargetCountMap)) {
205 "Expected 'NUM[.NUM]: NUM[ mangled_name:NUM]*', found " +
223 for (
const auto &name_count : TargetCountMap) {
225 LineOffset, Discriminator, name_count.first,
245 if ((*LineIt)[0] !=
' ') {
246 uint64_t NumSamples, NumHeadSamples;
248 result =
ParseHead(*LineIt, FName, NumSamples, NumHeadSamples);
256 unsigned NumBytesRead = 0;
262 else if (
Data + NumBytesRead >
End)
272 Data += NumBytesRead;
273 return static_cast<T>(Val);
291 auto Idx = readNumber<uint32_t>();
292 if (std::error_code EC = Idx.getError())
301 auto NumSamples = readNumber<uint64_t>();
302 if (std::error_code EC = NumSamples.getError())
307 auto NumRecords = readNumber<uint32_t>();
308 if (std::error_code EC = NumRecords.getError())
312 auto LineOffset = readNumber<uint64_t>();
313 if (std::error_code EC = LineOffset.getError())
317 return std::error_code();
320 auto Discriminator = readNumber<uint64_t>();
321 if (std::error_code EC = Discriminator.getError())
324 auto NumSamples = readNumber<uint64_t>();
325 if (std::error_code EC = NumSamples.getError())
328 auto NumCalls = readNumber<uint32_t>();
329 if (std::error_code EC = NumCalls.getError())
332 for (
uint32_t J = 0; J < *NumCalls; ++J) {
334 if (std::error_code EC = CalledFunction.getError())
337 auto CalledFunctionSamples = readNumber<uint64_t>();
338 if (std::error_code EC = CalledFunctionSamples.getError())
342 *CalledFunction, *CalledFunctionSamples);
345 FProfile.
addBodySamples(*LineOffset, *Discriminator, *NumSamples);
349 auto NumCallsites = readNumber<uint32_t>();
350 if (std::error_code EC = NumCallsites.getError())
353 for (
uint32_t J = 0; J < *NumCallsites; ++J) {
354 auto LineOffset = readNumber<uint64_t>();
355 if (std::error_code EC = LineOffset.getError())
358 auto Discriminator = readNumber<uint64_t>();
359 if (std::error_code EC = Discriminator.getError())
363 if (std::error_code EC = FName.getError())
369 if (std::error_code EC =
readProfile(CalleeProfile))
378 auto NumHeadSamples = readNumber<uint64_t>();
379 if (std::error_code EC = NumHeadSamples.getError())
383 if (std::error_code EC = FName.getError())
400 Data =
reinterpret_cast<const uint8_t *
>(
Buffer->getBufferStart());
404 auto Magic = readNumber<uint64_t>();
405 if (std::error_code EC =
Magic.getError())
411 auto Version = readNumber<uint64_t>();
412 if (std::error_code EC =
Version.getError())
417 if (std::error_code EC = readSummary())
421 auto Size = readNumber<uint32_t>();
422 if (std::error_code EC =
Size.getError())
427 if (std::error_code EC =
Name.getError())
435 std::error_code SampleProfileReaderBinary::readSummaryEntry(
436 std::vector<ProfileSummaryEntry> &Entries) {
437 auto Cutoff = readNumber<uint64_t>();
438 if (std::error_code EC = Cutoff.getError())
441 auto MinBlockCount = readNumber<uint64_t>();
442 if (std::error_code EC = MinBlockCount.getError())
445 auto NumBlocks = readNumber<uint64_t>();
446 if (std::error_code EC = NumBlocks.getError())
449 Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
453 std::error_code SampleProfileReaderBinary::readSummary() {
454 auto TotalCount = readNumber<uint64_t>();
455 if (std::error_code EC = TotalCount.getError())
458 auto MaxBlockCount = readNumber<uint64_t>();
459 if (std::error_code EC = MaxBlockCount.getError())
462 auto MaxFunctionCount = readNumber<uint64_t>();
463 if (std::error_code EC = MaxFunctionCount.getError())
466 auto NumBlocks = readNumber<uint64_t>();
467 if (std::error_code EC = NumBlocks.getError())
470 auto NumFunctions = readNumber<uint64_t>();
471 if (std::error_code EC = NumFunctions.getError())
474 auto NumSummaryEntries = readNumber<uint64_t>();
475 if (std::error_code EC = NumSummaryEntries.getError())
478 std::vector<ProfileSummaryEntry> Entries;
479 for (
unsigned i = 0; i < *NumSummaryEntries; i++) {
480 std::error_code EC = readSummaryEntry(Entries);
484 Summary = llvm::make_unique<ProfileSummary>(
486 *MaxFunctionCount, *NumBlocks, *NumFunctions);
492 const uint8_t *
Data =
509 return static_cast<T>(Val);
510 }
else if (
sizeof(
T) <=
sizeof(uint64_t)) {
513 return static_cast<T>(Val);
575 Names.push_back(Str);
600 uint64_t HeadCount = 0;
601 if (InlineStack.
size() == 0)
620 if (InlineStack.
size() == 0) {
637 uint32_t Discriminator = Offset & 0xffff;
660 uint32_t Discriminator = Offset & 0xffff;
668 for (
auto CallerProfile : NewStack)
669 CallerProfile->addTotalSamples(Count);
678 for (
uint32_t J = 0; J < NumTargets; J++) {
691 uint64_t TargetCount;
697 TargetName, TargetCount);
738 return Magic ==
"adcg*704";
747 if (std::error_code EC = BufferOrErr.getError())
749 auto Buffer = std::move(BufferOrErr.get());
755 return std::move(Buffer);
770 if (std::error_code EC = BufferOrError.getError())
772 return create(BufferOrError.get(),
C);
786 std::unique_ptr<SampleProfileReader> Reader;
796 if (std::error_code EC = Reader->readHeader())
799 return std::move(Reader);
std::vector< std::string > Names
Function names in this profile.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
void push_back(const T &Elt)
std::unique_ptr< MemoryBuffer > Buffer
Memory buffer holding the profile file.
Represents either an error or a value T.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
bool readInt(uint32_t &Val)
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
const char * getBufferStart() const
int64_t line_number() const
Return the current line number. May return any number at EOF.
std::error_code read() override
Read sample profiles from the associated file.
const uint8_t * Data
Points to the current location in the buffer.
bool readString(StringRef &Str)
std::error_code readHeader() override
Read and validate the file header.
static ErrorOr< std::unique_ptr< SampleProfileReader > > create(const Twine &Filename, LLVMContext &C)
Create a sample profile reader appropriate to the file format.
std::error_code read() override
Read sample profiles from the associated file.
A forward iterator which reads text lines from a buffer.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
std::error_code readProfile(FunctionSamples &FProfile)
Read the contents of the given profile instance.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
Representation of the samples collected for a function.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
StringMap< FunctionSamples > Profiles
Map every function to its associated profile.
Tagged union holding either a T or a Error.
ErrorOr< T > readNumber()
std::error_code readNameTable()
static bool ParseHead(const StringRef &Input, StringRef &FName, uint64_t &NumSamples, uint64_t &NumHeadSamples)
Parse Input as function head.
void setName(StringRef FunctionName)
Set the name of the function.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
std::error_code readFunctionProfiles()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
static uint64_t SPVersion()
static bool ParseLine(const StringRef &Input, bool &IsCallsite, uint32_t &Depth, uint64_t &NumSamples, uint32_t &LineOffset, uint32_t &Discriminator, StringRef &CalleeName, DenseMap< StringRef, uint64_t > &TargetCountMap)
Parse Input as line sample.
bool readInt64(uint64_t &Val)
std::error_code readOneFunctionProfile(const InlineCallStack &InlineStack, bool Update, uint32_t Offset)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
void reportError(int64_t LineNumber, Twine Msg) const
Report a parse error message.
This is an important class for using LLVM in a threaded context.
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
ErrorOr< T > readNumber()
Read a numeric value of type T from the profile.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
ErrorOr< StringRef > readString()
Read a string from the profile.
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
void dumpFunctionProfile(StringRef FName, raw_ostream &OS=dbgs())
Print the profile for FName on stream OS.
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
static const char *const Magic
bool at_eof() const
Return true if we've reached the end of file.
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, const std::string &FName, uint64_t Num, uint64_t Weight=1)
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
std::vector< StringRef > NameTable
Function name table.
void addRecord(const sampleprof::FunctionSamples &FS)
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static const uint32_t GCOVTagAFDOFileNames
GCOV tags used to separate sections in the profile file.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if Buffer is in the format supported by this class.
iterator insert(iterator I, T &&Elt)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
std::unique_ptr< ProfileSummary > getSummary()
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static uint64_t SPMagic()
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
ErrorOr< StringRef > readString()
Represents the relative location of an instruction.
const uint8_t * End
Points to the end of the buffer.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
static const uint32_t GCOVTagAFDOFunction
Provides ErrorOr<T> smart pointer.
std::error_code readSectionTag(uint32_t Expected)
Read the section tag and check that it's the same as Expected.
static ErrorOr< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename)
Prepare a memory buffer for the contents of Filename.
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
This class implements an extremely fast bulk output stream that can only output to a stream...
ErrorOr< StringRef > readStringFromTable()
Read a string indirectly via the name table.
StringRef - Represent a constant reference to a string, i.e.
GCOVBuffer GcovBuffer
GCOV buffer containing the profile.
std::error_code readHeader() override
Read and validate the file header.
void computeSummary()
Compute summary for this profile.
bool readGCOVVersion(GCOV::GCOVVersion &Version)
readGCOVVersion - Read GCOV version.
void dump(raw_ostream &OS=dbgs())
Print all the profiles on stream OS.
std::error_code read() override
Read sample profiles from the associated file.
static bool isOffsetLegal(unsigned L)
Returns true if line offset L is legal (only has 16 bits).
std::error_code skipNextWord()
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
std::unique_ptr< ProfileSummary > Summary
Profile summary information.