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)
95 UncompressedStrings.clear();
106 assert(Entry.Type ==
Type &&
"Unexpected section type");
109 if (std::error_code EC = compressAndOutput())
112 SecHdrTable.push_back({
Type, Entry.Flags, SectionStart - FileStart,
119 if (std::error_code EC = writeHeader(ProfileMap))
122 std::string LocalBuf;
123 LocalBufStream = std::make_unique<raw_string_ostream>(LocalBuf);
127 if (std::error_code EC = writeSecHdrTable())
144 if (
Ret == CSNameTable.end())
177 std::map<SampleContext, uint64_t> OrderedFuncOffsetTable(
178 FuncOffsetTable.begin(), FuncOffsetTable.end());
179 for (
const auto &Entry : OrderedFuncOffsetTable) {
180 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
185 for (
const auto &Entry : FuncOffsetTable) {
186 if (std::error_code EC = WriteItem(Entry.first, Entry.second))
191 FuncOffsetTable.clear();
211 NumCallsites += J.second.size();
214 for (
const auto &
FS : J.second) {
232 for (
const auto &Entry : Profiles) {
244 std::set<StringRef> V;
259 for (
const auto &
I : ProfileMap) {
260 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
282 std::set<SampleContext> OrderedContexts;
283 for (
const auto &
I : CSNameTable)
284 OrderedContexts.insert(
I.first);
285 assert(OrderedContexts.size() == CSNameTable.size() &&
286 "Unmatched ordered and unordered contexts");
288 for (
auto &
Context : OrderedContexts)
294 for (
auto Context : OrderedContexts) {
295 auto Frames =
Context.getContextFrames();
297 for (
auto &Callsite : Frames) {
298 if (std::error_code EC =
writeNameIdx(Callsite.FuncName))
310 if (ProfSymList && ProfSymList->
size() > 0)
376 std::error_code SampleProfileWriterExtBinary::writeDefaultLayout(
402 for (
const auto &
I : ProfileMap) {
403 if (
I.second.getCallsiteSamples().size())
404 ContextProfileMap.insert({
I.first,
I.second});
406 NoContextProfileMap.insert({
I.first,
I.second});
410 std::error_code SampleProfileWriterExtBinary::writeCtxSplitLayout(
441 std::error_code SampleProfileWriterExtBinary::writeSections(
445 EC = writeDefaultLayout(ProfileMap);
447 EC = writeCtxSplitLayout(ProfileMap);
473 OS <<
"[" <<
S.getContext().toString() <<
"]:" <<
S.getTotalSamples();
475 OS <<
S.getName() <<
":" <<
S.getTotalSamples();
478 OS <<
":" <<
S.getHeadSamples();
482 for (
const auto &
I : SortedSamples.
get()) {
485 OS.indent(Indent + 1);
494 OS <<
" " << J.first <<
":" << J.second;
499 S.getCallsiteSamples());
501 for (
const auto &
I : SortedCallsiteSamples.
get())
502 for (
const auto &
FS :
I->second) {
510 if (std::error_code EC =
writeSample(CalleeSamples))
516 OS.indent(Indent + 1);
517 OS <<
"!CFGChecksum: " <<
S.getFunctionHash() <<
"\n";
520 if (
S.getContext().getAllAttributes()) {
521 OS.indent(Indent + 1);
522 OS <<
"!Attributes: " <<
S.getContext().getAllAttributes() <<
"\n";
530 assert(!
Context.hasContext() &&
"cs profile is not supported");
536 const auto &
Ret = NTable.find(FName);
537 if (
Ret == NTable.end())
545 NTable.insert(std::make_pair(FName, 0));
554 for (
const auto &
I :
S.getBodySamples()) {
561 for (
const auto &J :
S.getCallsiteSamples())
562 for (
const auto &
FS : J.second) {
572 for (
auto &Callsite :
Context.getContextFrames())
574 CSNameTable.insert(std::make_pair(
Context, 0));
592 std::set<StringRef> V;
609 uint64_t FuncOffsetTableStart = OS.tell();
613 Writer.
write(FuncOffsetTableStart);
614 if (OFS.seek(FuncOffsetTableStart) == (
uint64_t)-1)
631 std::set<StringRef> V;
660 for (
const auto &
I : ProfileMap) {
661 assert(
I.first ==
I.second.getContext() &&
"Inconsistent profile map");
679 void SampleProfileWriterExtBinaryBase::allocSecHdrTable() {
685 Writer.write(
static_cast<uint64_t>(-1));
686 Writer.write(
static_cast<uint64_t>(-1));
687 Writer.write(
static_cast<uint64_t>(-1));
688 Writer.write(
static_cast<uint64_t>(-1));
692 std::error_code SampleProfileWriterExtBinaryBase::writeSecHdrTable() {
697 if (OFS.seek(SecHdrTableOffset) == (
uint64_t)-1)
702 "SecHdrTable entries doesn't match SectionHdrLayout");
704 for (
uint32_t TableIdx = 0; TableIdx < SecHdrTable.size(); TableIdx++) {
705 IndexMap[SecHdrTable[TableIdx].LayoutIndex] = TableIdx;
719 assert(IndexMap[LayoutIdx] < SecHdrTable.size() &&
720 "Incorrect LayoutIdx in SecHdrTable");
721 auto Entry = SecHdrTable[IndexMap[LayoutIdx]];
722 Writer.write(
static_cast<uint64_t>(Entry.Type));
723 Writer.write(
static_cast<uint64_t>(Entry.Flags));
724 Writer.write(
static_cast<uint64_t>(Entry.Offset));
725 Writer.write(
static_cast<uint64_t>(Entry.Size));
735 std::error_code SampleProfileWriterExtBinaryBase::writeHeader(
738 FileStart = OS.tell();
765 const std::vector<ProfileSummaryEntry> &Entries =
768 for (
auto Entry : Entries) {
784 for (
const auto &
I :
S.getBodySamples()) {
802 for (
const auto &J :
S.getCallsiteSamples())
803 NumCallsites += J.second.size();
805 for (
const auto &J :
S.getCallsiteSamples())
806 for (
const auto &
FS : J.second) {
811 if (std::error_code EC =
writeBody(CalleeSamples))
846 std::unique_ptr<raw_ostream> OS;
869 std::unique_ptr<SampleProfileWriter> Writer;
std::unique_ptr< raw_ostream > OutputStream
Output stream where to emit the profile to.
uint64_t getSamples() const
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)
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()
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)
void setToCompressAllSections() override
Adapter to write values to a stream in a particular byte order.
std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
std::error_code writeSample(const FunctionSamples &S) override
Write samples of a top-level function to a binary file.
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
virtual std::error_code writeCustomSection(SecType Type)=0
std::unique_ptr< ProfileSummary > Summary
Profile summary.
constexpr int BestSizeCompression
@ unsupported_writing_format
virtual std::error_code writeSections(const SampleProfileMap &ProfileMap)=0
void addNames(const FunctionSamples &S)
virtual std::error_code write(const SampleProfileMap &ProfileMap)
Write all the sample profiles in the given map of samples.
std::error_code writeHeader(const SampleProfileMap &ProfileMap) override
Write a file header for the profile file.
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.
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 '.
std::error_code writeNameTable() override
const CallTargetMap & getCallTargets() const
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()
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
const SortedCallTargetSet getSortedCallTargets() 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)
void compress(ArrayRef< uint8_t > Input, SmallVectorImpl< uint8_t > &CompressedBuffer, int Level=DefaultCompression)
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)
SampleContext & getContext() const
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.
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