1//===- SampleProfWriter.cpp - Write 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 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. 15// See lib/ProfileData/SampleProfReader.cpp for documentation on each of the 18//===----------------------------------------------------------------------===// 35#include <system_error> 39#define DEBUG_TYPE "llvm-profdata" 42using namespacesampleprof;
49// Adapter class to llvm::support::endian::Writer for pwrite(). 56template <
typename ValueType>
58 std::string StringBuf;
60 Writer(SStream, Endian).write(Val);
79size_t NumToRemove =
ProfileMap.size() - NewSize;
83assert(NumToRemove <= SortedFunctions.size());
87 SortedFunctions.resize(SortedFunctions.size() - NumToRemove);
93if (OutputSizeLimit == 0)
94returnwrite(ProfileMap);
96size_t OriginalFunctionCount = ProfileMap.size();
98 std::unique_ptr<raw_ostream> OriginalOutputStream;
101size_t IterationCount = 0;
106 StringBuffer.
clear();
108if (std::error_code EC =
write(ProfileMap))
111 TotalSize = StringBuffer.
size();
112// On Windows every "\n" is actually written as "\r\n" to disk but not to 113// memory buffer, this difference should be added when considering the total 119if (TotalSize <= OutputSizeLimit)
122 Strategy->
Erase(TotalSize);
124 }
while (ProfileMap.size() != 0);
126if (ProfileMap.size() == 0)
131LLVM_DEBUG(
dbgs() <<
"Profile originally has " << OriginalFunctionCount
132 <<
" functions, reduced to " << ProfileMap.size() <<
" in " 133 << IterationCount <<
" iterations\n");
134// Silence warning on Release build. 135 (void)OriginalFunctionCount;
136 (void)IterationCount;
142 std::vector<NameFunctionSamples> V;
144for (
constauto &
I : V) {
161/// Return the current position and prepare to use it as the start 162/// position of a section given the section type \p Type and its position 163/// \p LayoutIdx in SectionHdrLayout. 170assert(Entry.Type ==
Type &&
"Unexpected section type");
171// Use LocalBuf as a temporary output for writting data. 177std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
180 std::string &UncompressedStrings =
182if (UncompressedStrings.size() == 0)
191OS << toStringRef(CompressedStrings);
192 UncompressedStrings.clear();
196/// Add a new section into section header table given the section type 197/// \p Type, its position \p LayoutIdx in SectionHdrLayout and the 198/// location \p SectionStart where the section should be written to. 203assert(Entry.Type ==
Type &&
"Unexpected section type");
206if (std::error_code EC = compressAndOutput())
209 SecHdrTable.push_back({
Type, Entry.Flags, SectionStart - FileStart,
216// When calling write on a different profile map, existing states should be 222if (std::error_code EC = writeHeader(ProfileMap))
225 std::string LocalBuf;
226 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
230if (std::error_code EC = writeSecHdrTable())
246constauto &Ret = CSNameTable.find(Context);
247if (Ret == CSNameTable.end())
265// Write out the table size. 268// Write out FuncOffsetTable. 277// Sort the contexts before writing them out. This is to help fast load all 278// context profiles for a function as well as their callee contexts which 279// can help profile-guided importing for ThinLTO. 280 std::map<SampleContext, uint64_t> OrderedFuncOffsetTable(
281 FuncOffsetTable.begin(), FuncOffsetTable.end());
282for (
constauto &Entry : OrderedFuncOffsetTable) {
283if (std::error_code EC = WriteItem(Entry.first, Entry.second))
288for (
constauto &Entry : FuncOffsetTable) {
289if (std::error_code EC = WriteItem(Entry.first, Entry.second))
294 FuncOffsetTable.clear();
311// Recursively emit attributes for all callee samples. 314 NumCallsites += J.second.size();
317for (
constauto &FS : J.second) {
335for (
constauto &Entry : Profiles) {
347 std::set<FunctionId> V;
350// Write out the MD5 name table. We wrote unencoded MD5 so reader can 351// retrieve the name using the name index without having to read the 356 Writer.
write(
N.getHashCode());
362for (
constauto &
I : ProfileMap) {
367// If NameTable contains ".__uniq." suffix, set SecFlagUniqSuffix flag 368// so compiler won't strip the suffix during profile matching after 369// seeing the flag in the profile. 370// Original names are unavailable if using MD5, so this option has no use. 386// Sort the names to make CSNameTable deterministic. 387 std::set<SampleContext> OrderedContexts;
388for (
constauto &
I : CSNameTable)
389 OrderedContexts.insert(
I.first);
390assert(OrderedContexts.size() == CSNameTable.size() &&
391"Unmatched ordered and unordered contexts");
393for (
auto &Context : OrderedContexts)
394 CSNameTable[Context] =
I++;
399for (
auto Context : OrderedContexts) {
400auto Frames = Context.getContextFrames();
402for (
auto &Callsite : Frames) {
415if (ProfSymList && ProfSymList->
size() > 0)
424// The setting of SecFlagCompress should happen before markSectionStart. 481std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
483// The const indices passed to writeOneSection below are specifying the 484// positions of the sections in SectionHdrLayout. Look at 485// initSectionHdrLayout to find out where each section is located in 507for (
constauto &
I : ProfileMap) {
508if (
I.second.getCallsiteSamples().size())
509 ContextProfileMap.insert({
I.first,
I.second});
511 NoContextProfileMap.insert({
I.first,
I.second});
515std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
528// Mark the section to have no context. Note section flag needs to be set 529// before writing the section. 533// Mark the section to have no context. Note section flag needs to be set 534// before writing the section. 546std::error_code SampleProfileWriterExtBinary::writeSections(
550EC = writeDefaultLayout(ProfileMap);
552EC = writeCtxSplitLayout(ProfileMap);
558/// Write samples to a text file. 560/// Note: it may be tempting to implement this in terms of 561/// FunctionSamples::print(). Please don't. The dump functionality is intended 562/// for debugging and has no specified form. 564/// The format used here is more structured and deliberate because 565/// it needs to be parsed by the SampleProfileReaderText class. 579for (
constauto &
I : SortedSamples.
get()) {
591OS <<
" " << J.first <<
":" << J.second;
599for (
constauto &
I : SortedCallsiteSamples.
get())
600for (
constauto &FS :
I->second) {
639constauto &Ret = NTable.find(FName);
640if (Ret == NTable.end())
648 NTable.insert(std::make_pair(FName, 0));
656// Add all the names in indirect call targets. 663// Recursively add all the names for inlined callsites. 665for (
constauto &FS : J.second) {
677 CSNameTable.insert(std::make_pair(Context, 0));
685// Sort the names to make NameTable deterministic. 695 std::set<FunctionId> V;
698// Write out the name table. 710// Write file magic identifier. 718// When calling write on a different profile map, existing names should be 728// Generate the name table for all the functions referenced in the profile. 729for (
constauto &
I : ProfileMap) {
747void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
753 Writer.write(
static_cast<uint64_t>(-1));
754 Writer.write(
static_cast<uint64_t>(-1));
755 Writer.write(
static_cast<uint64_t>(-1));
756 Writer.write(
static_cast<uint64_t>(-1));
760std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
762"SecHdrTable entries doesn't match SectionHdrLayout");
764for (
uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
765 IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
768// Write the section header table in the order specified in 769// SectionHdrLayout. SectionHdrLayout specifies the sections 770// order in which profile reader expect to read, so the section 771// header table should be written in the order in SectionHdrLayout. 772// Note that the section order in SecHdrTable may be different 773// from the order in SectionHdrLayout, for example, SecFuncOffsetTable 774// needs to be computed after SecLBRProfile (the order in SecHdrTable), 775// but it needs to be read before SecLBRProfile (the order in 776// SectionHdrLayout). So we use IndexMap above to switch the order. 777 support::endian::SeekableWriter Writer(
782assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
783"Incorrect LayoutIdx in SecHdrTable");
784autoEntry = SecHdrTable[IndexMap[LayoutIdx]];
786 SecHdrTableOffset + 4 * LayoutIdx *
sizeof(
uint64_t));
788 SecHdrTableOffset + (4 * LayoutIdx + 1) *
sizeof(
uint64_t));
790 SecHdrTableOffset + (4 * LayoutIdx + 2) *
sizeof(
uint64_t));
792 SecHdrTableOffset + (4 * LayoutIdx + 3) *
sizeof(
uint64_t));
798std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
817for (
auto Entry : Entries) {
831// Emit all the body samples. 849// Recursively emit all the callsite samples. 852 NumCallsites += J.second.size();
855for (
constauto &FS : J.second) {
860if (std::error_code EC =
writeBody(CalleeSamples))
867/// Write samples of a top-level function to a binary file. 869/// \returns true if the samples were written successfully, false otherwise. 876/// Create a sample profile file writer based on the specified format. 878/// \param Filename The file to create. 880/// \param Format Encoding format for the profile file. 882/// \returns an error code indicating the status of the created writer. 886 std::unique_ptr<raw_ostream>
OS;
897/// Create a sample profile stream writer based on the specified format. 899/// \param OS The output stream to store the profile data to. 901/// \param Format Encoding format for the profile file. 903/// \returns an error code indicating the status of the created writer. 908 std::unique_ptr<SampleProfileWriter> Writer;
910// Currently only Text and Extended Binary format are supported for CSSPGO. 930return std::move(Writer);
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void splitProfileMapToTwo(const SampleProfileMap &ProfileMap, SampleProfileMap &ContextProfileMap, SampleProfileMap &NoContextProfileMap)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents either an error or a value T.
This class implements a map that also provides access to all stored values in a deterministic order.
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
std::unique_ptr< ProfileSummary > computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles)
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.
StringRef - Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
A raw_ostream that writes to a file descriptor.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
An abstract base class for streams implementations that also support a pwrite operation.
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
A raw_ostream that writes to an std::string.
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.
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...
SampleProfileMap & ProfileMap
Representation of the samples collected for a function.
static 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...
FunctionId getFunction() const
Return the function name.
uint64_t getFunctionHash() const
static bool ProfileIsProbeBased
static 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.
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::error_code write(raw_ostream &OS)
SampleContextFrames getContextFrames() const
FunctionId getFunction() const
uint32_t getAllAttributes()
std::string toString() const
This class provides operator overloads to the map container using MD5 as the key type,...
size_t erase(const SampleContext &Ctx)
void stablizeNameTable(MapVector< FunctionId, uint32_t > &NameTable, std::set< FunctionId > &V)
virtual void addContext(const SampleContext &Context)
virtual std::error_code writeNameTable()
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
std::error_code writeSummary()
MapVector< FunctionId, uint32_t > NameTable
void addNames(const FunctionSamples &S)
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)
void addName(FunctionId FName)
std::error_code writeNameTableSection(const SampleProfileMap &ProfileMap)
SmallVector< SecHdrTableEntry, 8 > SectionHdrLayout
std::error_code writeFuncMetadata(const SampleProfileMap &Profiles)
void setToCompressSection(SecType Type)
virtual std::error_code writeCustomSection(SecType Type)=0
virtual std::error_code writeOneSection(SecType Type, uint32_t LayoutIdx, const SampleProfileMap &ProfileMap)
std::error_code writeFuncOffsetTable()
std::error_code writeCSNameTableSection()
std::error_code writeCSNameIdx(const SampleContext &Context)
virtual std::error_code writeSections(const SampleProfileMap &ProfileMap)=0
std::error_code writeNameTable() override
void addSectionFlag(SecType Type, SecFlagType Flag)
std::error_code writeProfileSymbolListSection()
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...
void setToCompressAllSections() override
uint64_t SecLBRProfileStart
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.
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.
const CallTargetMap & getCallTargets() const
uint64_t getSamples() const
const SortedCallTargetSet getSortedCallTargets() const
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.
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
constexpr int BestSizeCompression
void sortFuncProfiles(const SampleProfileMap &ProfileMap, std::vector< NameFunctionSamples > &SortedProfiles)
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
std::pair< hash_code, const FunctionSamples * > NameFunctionSamples
static void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
@ SecFlagIsPreInlined
SecFlagIsPreInlined means this profile contains ShouldBeInlined contexts thus this is CS preinliner c...
@ SecFlagFSDiscriminator
SecFlagFSDiscriminator means this profile uses flow-sensitive discriminators.
@ SecFlagFullContext
SecFlagContext means this is context-sensitive flat profile for CSSPGO.
static uint64_t SPVersion()
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
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.
@ unsupported_writing_format
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Represents the relative location of an instruction.
Adapter to write values to a stream in a particular byte order.
void write(ArrayRef< value_type > Val)
static uint64_t round(uint64_t Acc, uint64_t Input)