Go to the documentation of this file.
36 #include <system_error>
41 using namespace sampleprof;
45 std::vector<NameFunctionSamples> V;
47 for (
const auto &
I : V) {
73 assert(Entry.Type ==
Type &&
"Unexpected section type");
80 std::error_code SampleProfileWriterExtBinaryBase::compressAndOutput() {
83 std::string &UncompressedStrings =
85 if (UncompressedStrings.size() == 0)
93 OS << CompressedStrings.
str();
94 UncompressedStrings.clear();
105 assert(Entry.Type ==
Type &&
"Unexpected section type");
108 if (std::error_code EC = compressAndOutput())
111 SecHdrTable.push_back({
Type, Entry.Flags, SectionStart - FileStart,
118 if (std::error_code EC = writeHeader(ProfileMap))
121 std::string LocalBuf;
122 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
126 if (std::error_code EC = writeSecHdrTable())
143 if (
Ret == CSNameTable.end())
176 std::map<SampleContext, uint64_t> OrderedFuncOffsetTable(
177 FuncOffsetTable.begin(), FuncOffsetTable.end());
178 for (
const auto &Entry : OrderedFuncOffsetTable) {
179 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
184 for (
const auto &Entry : FuncOffsetTable) {
185 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
190 FuncOffsetTable.clear();
210 NumCallsites += J.second.size();
213 for (
const auto &
FS : J.second) {
231 for (
const auto &Entry : Profiles) {
243 std::set<StringRef> V;
258 for (
const auto &
I : ProfileMap) {
259 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
281 std::set<SampleContext> OrderedContexts;
282 for (
const auto &
I : CSNameTable)
283 OrderedContexts.insert(
I.first);
284 assert(OrderedContexts.size() == CSNameTable.size() &&
285 "Unmatched ordered and unordered contexts");
287 for (
auto &
Context : OrderedContexts)
293 for (
auto Context : OrderedContexts) {
294 auto Frames =
Context.getContextFrames();
296 for (
auto &Callsite : Frames) {
297 if (std::error_code EC =
writeNameIdx(Callsite.FuncName))
309 if (ProfSymList && ProfSymList->
size() > 0)
375 std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
401 for (
const auto &
I : ProfileMap) {
402 if (
I.second.getCallsiteSamples().size())
403 ContextProfileMap.insert({
I.first,
I.second});
405 NoContextProfileMap.insert({
I.first,
I.second});
409 std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
440 std::error_code SampleProfileWriterExtBinary::writeSections(
444 EC = writeDefaultLayout(ProfileMap);
446 EC = writeCtxSplitLayout(ProfileMap);
472 OS <<
"[" <<
S.getContext().toString() <<
"]:" <<
S.getTotalSamples();
474 OS <<
S.getName() <<
":" <<
S.getTotalSamples();
477 OS <<
":" <<
S.getHeadSamples();
481 for (
const auto &
I : SortedSamples.
get()) {
484 OS.indent(Indent + 1);
490 OS << Sample.getSamples();
492 for (
const auto &J : Sample.getSortedCallTargets())
493 OS <<
" " << J.first <<
":" << J.second;
498 S.getCallsiteSamples());
500 for (
const auto &
I : SortedCallsiteSamples.
get())
501 for (
const auto &
FS :
I->second) {
509 if (std::error_code EC =
writeSample(CalleeSamples))
515 OS.indent(Indent + 1);
516 OS <<
"!CFGChecksum: " <<
S.getFunctionHash() <<
"\n";
519 if (
S.getContext().getAllAttributes()) {
520 OS.indent(Indent + 1);
521 OS <<
"!Attributes: " <<
S.getContext().getAllAttributes() <<
"\n";
529 assert(!
Context.hasContext() &&
"cs profile is not supported");
535 const auto &
Ret = NTable.find(FName);
536 if (
Ret == NTable.end())
544 NTable.insert(std::make_pair(FName, 0));
553 for (
const auto &
I :
S.getBodySamples()) {
555 for (
const auto &J : Sample.getCallTargets())
560 for (
const auto &J :
S.getCallsiteSamples())
561 for (
const auto &
FS : J.second) {
571 for (
auto &Callsite :
Context.getContextFrames())
573 CSNameTable.insert(std::make_pair(
Context, 0));
591 std::set<StringRef> V;
608 uint64_t FuncOffsetTableStart = OS.tell();
612 Writer.
write(FuncOffsetTableStart);
613 if (OFS.seek(FuncOffsetTableStart) == (
uint64_t)-1)
630 std::set<StringRef> V;
659 for (
const auto &
I : ProfileMap) {
660 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
678 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
684 Writer.write(
static_cast<uint64_t>(-1));
685 Writer.write(
static_cast<uint64_t>(-1));
686 Writer.write(
static_cast<uint64_t>(-1));
687 Writer.write(
static_cast<uint64_t>(-1));
691 std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
696 if (OFS.seek(SecHdrTableOffset) == (
uint64_t)-1)
701 "SecHdrTable entries doesn't match SectionHdrLayout");
703 for (
uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
704 IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
718 assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
719 "Incorrect LayoutIdx in SecHdrTable");
720 auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
721 Writer.write(
static_cast<uint64_t>(Entry.Type));
722 Writer.write(
static_cast<uint64_t>(Entry.Flags));
723 Writer.write(
static_cast<uint64_t>(Entry.Offset));
724 Writer.write(
static_cast<uint64_t>(Entry.Size));
734 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
737 FileStart = OS.tell();
764 const std::vector<ProfileSummaryEntry> &Entries =
767 for (
auto Entry : Entries) {
783 for (
const auto &
I :
S.getBodySamples()) {
790 for (
const auto &J : Sample.getSortedCallTargets()) {
801 for (
const auto &J :
S.getCallsiteSamples())
802 NumCallsites += J.second.size();
804 for (
const auto &J :
S.getCallsiteSamples())
805 for (
const auto &
FS : J.second) {
810 if (std::error_code EC =
writeBody(CalleeSamples))
845 std::unique_ptr<raw_ostream> OS;
868 std::unique_ptr<SampleProfileWriter> Writer;
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
MapVector< StringRef, uint32_t > NameTable
Sample-based profile writer (text format).
static ErrorOr< std::unique_ptr< SampleProfileWriter > > create(StringRef Filename, SampleProfileFormat Format)
Profile writer factory.
This is an optimization pass for GlobalISel generic memory operations.
void stablizeNameTable(MapVector< StringRef, uint32_t > &NameTable, std::set< StringRef > &V)
virtual std::error_code writeNameTable() override
@ SecFlagFSDiscriminator
SecFlagFSDiscriminator means this profile uses flow-sensitive discriminators.
const SamplesWithLocList & get() const
static bool ProfileIsProbeBased
SampleProfileFormat Format
Profile format.
std::error_code writeBody(const FunctionSamples &S)
std::error_code writeFuncOffsetTable()
A raw_ostream that writes to an std::string.
static void splitProfileMapToTwo(const SampleProfileMap &ProfileMap, SampleProfileMap &ContextProfileMap, SampleProfileMap &NoContextProfileMap)
@ SecFlagFullContext
SecFlagContext means this is context-sensitive flat profile for CSSPGO.
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
static bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag)
Fixup Statepoint Caller Saved
The instances of the Type class are immutable: once they are created, they are never changed.
void addName(StringRef FName)
void computeSummary(const SampleProfileMap &ProfileMap)
Compute summary for this profile.
static void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag)
std::error_code writeFuncOffsetTable()
This class implements a map that also provides access to all stored values in a deterministic order.
uint32_t getAllAttributes()
virtual std::error_code writeHeader(const SampleProfileMap &ProfileMap) override
Write a file header for the profile file.
std::error_code writeNameTableSection(const SampleProfileMap &ProfileMap)
StringRef getName() const
Return the function name.
virtual std::error_code writeFuncProfiles(const SampleProfileMap &ProfileMap)
virtual void setToCompressAllSections() override
Adapter to write values to a stream in a particular byte order.
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
virtual std::error_code writeCustomSection(SecType Type)=0
std::unique_ptr< ProfileSummary > Summary
Profile summary.
@ unsupported_writing_format
virtual std::error_code writeSections(const SampleProfileMap &ProfileMap)=0
void addNames(const FunctionSamples &S)
static constexpr int BestSizeCompression
void compress(StringRef InputBuffer, SmallVectorImpl< char > &CompressedBuffer, int Level=DefaultCompression)
virtual std::error_code write(const SampleProfileMap &ProfileMap)
Write all the sample profiles in the given map of samples.
virtual std::error_code writeHeader(const SampleProfileMap &ProfileMap) override
Write a file header for the profile file.
virtual void addContext(const SampleContext &Context) override
virtual std::error_code writeNameTable()
virtual std::error_code writeSample(const FunctionSamples &S)=0
Write sample profiles in S.
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
SmallVector< SecHdrTableEntry, 8 > SectionHdrLayout
uint64_t TableOffset
The offset of the slot to be filled with the offset of FuncOffsetTable towards profile start.
virtual std::error_code write(const SampleProfileMap &ProfileMap) override
Write all the sample profiles in the given map of samples.
void sortFuncProfiles(const SampleProfileMap &ProfileMap, std::vector< NameFunctionSamples > &SortedProfiles)
static bool ProfileIsFS
If this profile uses flow sensitive discriminators.
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
virtual std::error_code writeNameTable() override
MapVector< StringRef, uint64_t > FuncOffsetTable
The table mapping from function name to the offset of its FunctionSample towards profile start.
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...
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
virtual MapVector< StringRef, uint32_t > & getNameTable()
virtual std::error_code write(const SampleProfileMap &ProfileMap) override
Write all the sample profiles in the given map of samples.
Representation of the samples collected for a function.
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
std::error_code writeCSNameIdx(const SampleContext &Context)
uint64_t getFunctionHash() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual void addContext(const SampleContext &Context)
void addSectionFlag(SecType Type, SecFlagType Flag)
std::error_code writeSample(const FunctionSamples &S) override
Write samples to a text file.
std::error_code writeNameIdx(StringRef FName)
Represents the relative location of an instruction.
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...
StringRef - Represent a constant reference to a string, i.e.
Sort a LocationT->SampleT map by LocationT.
void write(ArrayRef< value_type > Val)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::error_code writeFuncMetadata(const SampleProfileMap &Profiles)
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
A raw_ostream that writes to a file descriptor.
virtual std::error_code writeHeader(const SampleProfileMap &ProfileMap)=0
Write a file header for the profile file.
amdgpu Simplify well known AMD library false FunctionCallee Callee
@ ostream_seek_unsupported
virtual std::error_code writeOneSection(SecType Type, uint32_t LayoutIdx, const SampleProfileMap &ProfileMap)
std::error_code write(raw_ostream &OS)
static uint64_t SPVersion()
std::error_code writeSummary()
void setToCompressSection(SecType Type)
uint64_t SecLBRProfileStart
std::error_code writeCSNameTableSection()
Representation of a single sample record.
std::error_code writeProfileSymbolListSection()
virtual std::error_code writeContextIdx(const SampleContext &Context)
StringRef str() const
Explicit conversion to StringRef.
SampleContext & getContext() const
virtual std::error_code writeContextIdx(const SampleContext &Context) override
virtual std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
static constexpr const char * UniqSuffix
Represents either an error or a value T.
@ SecFlagIsPreInlined
SecFlagIsPreInlined means this profile contains ShouldBeInlined contexts thus this is CS preinliner c...
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
virtual std::error_code writeMagicIdent(SampleProfileFormat Format)
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
uint64_t MD5Hash(StringRef Str)
Helper to compute and return lower 64 bits of the given string's MD5 hash.
static bool ProfileIsPreInlined