37#include <system_error>
41#define DEBUG_TYPE "llvm-profdata"
44using namespace sampleprof;
52struct SeekableWriter {
58 template <
typename ValueType>
60 std::string StringBuf;
62 Writer(SStream, Endian).write(Val);
81 size_t NumToRemove =
ProfileMap.size() - NewSize;
85 assert(NumToRemove <= SortedFunctions.size());
89 SortedFunctions.end()),
91 SortedFunctions.resize(SortedFunctions.size() - NumToRemove);
97 if (OutputSizeLimit == 0)
98 return write(ProfileMap);
100 size_t OriginalFunctionCount = ProfileMap.size();
102 std::unique_ptr<raw_ostream> OriginalOutputStream;
105 size_t IterationCount = 0;
110 StringBuffer.
clear();
112 if (std::error_code EC =
write(ProfileMap))
115 TotalSize = StringBuffer.
size();
123 if (TotalSize <= OutputSizeLimit)
126 Strategy->
Erase(TotalSize);
128 }
while (ProfileMap.size() != 0);
130 if (ProfileMap.size() == 0)
135 LLVM_DEBUG(
dbgs() <<
"Profile originally has " << OriginalFunctionCount
136 <<
" functions, reduced to " << ProfileMap.size() <<
" in "
137 << IterationCount <<
" iterations\n");
139 (void)OriginalFunctionCount;
140 (void)IterationCount;
146 std::vector<NameFunctionSamples> V;
148 for (
const auto &
I : V) {
174 assert(Entry.Type ==
Type &&
"Unexpected section type");
181std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
184 std::string &UncompressedStrings =
186 if (UncompressedStrings.size() == 0)
196 UncompressedStrings.clear();
207 assert(Entry.Type ==
Type &&
"Unexpected section type");
210 if (std::error_code EC = compressAndOutput())
213 SecHdrTable.push_back({
Type, Entry.Flags, SectionStart - FileStart,
226 if (std::error_code EC = writeHeader(ProfileMap))
229 std::string LocalBuf;
230 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
234 if (std::error_code EC = writeSecHdrTable())
250 const auto &Ret = CSNameTable.find(
Context);
251 if (Ret == CSNameTable.end())
284 std::map<SampleContext, uint64_t> OrderedFuncOffsetTable(
285 FuncOffsetTable.begin(), FuncOffsetTable.end());
286 for (
const auto &Entry : OrderedFuncOffsetTable) {
287 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
292 for (
const auto &Entry : FuncOffsetTable) {
293 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
298 FuncOffsetTable.clear();
318 NumCallsites += J.second.size();
321 for (
const auto &FS : J.second) {
339 for (
const auto &Entry : Profiles) {
351 std::set<StringRef> V;
366 for (
const auto &
I : ProfileMap) {
367 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
389 std::set<SampleContext> OrderedContexts;
390 for (
const auto &
I : CSNameTable)
391 OrderedContexts.insert(
I.first);
392 assert(OrderedContexts.size() == CSNameTable.size() &&
393 "Unmatched ordered and unordered contexts");
395 for (
auto &
Context : OrderedContexts)
401 for (
auto Context : OrderedContexts) {
402 auto Frames =
Context.getContextFrames();
404 for (
auto &Callsite : Frames) {
405 if (std::error_code EC =
writeNameIdx(Callsite.FuncName))
417 if (ProfSymList && ProfSymList->
size() > 0)
483std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
509 for (
const auto &
I : ProfileMap) {
510 if (
I.second.getCallsiteSamples().size())
511 ContextProfileMap.insert({
I.first,
I.second});
513 NoContextProfileMap.insert({
I.first,
I.second});
517std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
548std::error_code SampleProfileWriterExtBinary::writeSections(
552 EC = writeDefaultLayout(ProfileMap);
554 EC = writeCtxSplitLayout(ProfileMap);
581 for (
const auto &
I : SortedSamples.
get()) {
593 OS <<
" " << J.first <<
":" << J.second;
601 for (
const auto &
I : SortedCallsiteSamples.
get())
602 for (
const auto &FS :
I->second) {
610 if (std::error_code EC =
writeSample(CalleeSamples))
632 assert(!
Context.hasContext() &&
"cs profile is not supported");
638 const auto &Ret = NTable.find(FName);
639 if (Ret == NTable.end())
647 NTable.insert(std::make_pair(FName, 0));
664 for (
const auto &FS : J.second) {
674 for (
auto &Callsite :
Context.getContextFrames())
676 CSNameTable.insert(std::make_pair(
Context, 0));
694 std::set<StringRef> V;
728 for (
const auto &
I : ProfileMap) {
729 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
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");
764 for (
uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
765 IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
777 support::endian::SeekableWriter Writer(
781 assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
782 "Incorrect LayoutIdx in SecHdrTable");
783 auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
784 Writer.pwrite(
static_cast<uint64_t>(Entry.Type),
785 SecHdrTableOffset + 4 * LayoutIdx *
sizeof(
uint64_t));
786 Writer.pwrite(
static_cast<uint64_t>(Entry.Flags),
787 SecHdrTableOffset + (4 * LayoutIdx + 1) *
sizeof(
uint64_t));
788 Writer.pwrite(
static_cast<uint64_t>(Entry.Offset),
789 SecHdrTableOffset + (4 * LayoutIdx + 2) *
sizeof(
uint64_t));
790 Writer.pwrite(
static_cast<uint64_t>(Entry.Size),
791 SecHdrTableOffset + (4 * LayoutIdx + 3) *
sizeof(
uint64_t));
797std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
814 const std::vector<ProfileSummaryEntry> &Entries =
817 for (
auto Entry : Entries) {
852 NumCallsites += J.second.size();
855 for (
const auto &FS : J.second) {
860 if (std::error_code EC =
writeBody(CalleeSamples))
886 std::unique_ptr<raw_ostream>
OS;
908 std::unique_ptr<SampleProfileWriter> Writer;
930 return std::move(Writer);
amdgpu Simplify well known AMD library false FunctionCallee Callee
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void splitProfileMapToTwo(const SampleProfileMap &ProfileMap, SampleProfileMap &ContextProfileMap, SampleProfileMap &NoContextProfileMap)
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.
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.
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...
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.
StringRef getName() const
Return the function name.
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
std::error_code write(raw_ostream &OS)
uint32_t getAllAttributes()
std::string toString() const
void stablizeNameTable(MapVector< StringRef, uint32_t > &NameTable, std::set< StringRef > &V)
virtual void addContext(const SampleContext &Context)
virtual std::error_code writeNameTable()
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
std::error_code writeSummary()
std::error_code writeNameIdx(StringRef FName)
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< StringRef, uint32_t > & getNameTable()
void addName(StringRef FName)
MapVector< StringRef, uint32_t > NameTable
std::error_code writeBody(const FunctionSamples &S)
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
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
void sortFuncProfiles(const SampleProfileMap &ProfileMap, std::vector< NameFunctionSamples > &SortedProfiles)
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
std::pair< SampleContext, 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.
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
@ unsupported_writing_format
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string's MD5 hash.
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)