62 using namespace support;
66 for (
int K = 0; K < NItems; K++) {
67 FDOStream.
seek(P[K].Pos);
68 for (
int I = 0;
I < P[K].
N;
I++)
73 std::string &
Data = SOStream.
str();
74 for (
int K = 0; K < NItems; K++) {
75 for (
int I = 0;
I < P[K].
N;
I++) {
76 uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].
D[
I]);
77 Data.replace(P[K].Pos +
I *
sizeof(uint64_t),
sizeof(uint64_t),
78 (
const char *)&Bytes,
sizeof(uint64_t));
111 static std::pair<offset_type, offset_type>
113 using namespace support;
115 endian::Writer<little>
LE(Out);
121 for (
const auto &ProfileData : *V) {
123 M +=
sizeof(uint64_t);
124 M +=
sizeof(uint64_t);
125 M += ProfRecord.
Counts.size() *
sizeof(uint64_t);
128 M += ValueProfData::getSize(ProfileData.second);
132 return std::make_pair(
N, M);
140 using namespace support;
142 endian::Writer<little>
LE(Out);
143 for (
const auto &ProfileData : *V) {
145 SummaryBuilder->addRecord(ProfRecord);
147 LE.write<uint64_t>(ProfileData.first);
148 LE.write<uint64_t>(ProfRecord.
Counts.size());
149 for (uint64_t
I : ProfRecord.
Counts)
150 LE.write<uint64_t>(
I);
153 std::unique_ptr<ValueProfData> VDataPtr =
154 ValueProfData::serializeFrom(ProfileData.second);
156 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
157 Out.
write((
const char *)VDataPtr.get(), S);
176 this->Sparse = Sparse;
189 auto &ProfileDataMap = FunctionData[
Name];
193 std::tie(Where, NewFunc) =
198 Warn(make_error<InstrProfError>(
E));
205 Dest.
scale(Weight, MapWarn);
208 Dest.
merge(
I, Weight, MapWarn);
216 for (
auto &
I : IPW.FunctionData)
217 for (
auto &Func :
I.getValue())
218 addRecord(
I.getKey(), Func.first, std::move(Func.second), 1, Warn);
221 bool InstrProfWriter::shouldEncodeData(
const ProfilingData &
PD) {
224 for (
const auto &Func : PD) {
234 using namespace IndexedInstrProf;
245 for (
unsigned I = 0;
I < Res.size();
I++)
250 using namespace IndexedInstrProf;
255 InfoObj->SummaryBuilder = &ISB;
258 for (
const auto &
I : FunctionData)
259 if (shouldEncodeData(
I.getValue()))
260 Generator.insert(
I.getKey(), &
I.getValue());
266 Header.Version |= VARIANT_MASK_IR_PROF;
269 Header.HashOffset = 0;
275 for (
int I = 0;
I <
N - 1;
I++)
276 OS.
write(reinterpret_cast<uint64_t *>(&Header)[
I]);
279 uint64_t HashTableStartFieldOffset = OS.
tell();
285 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
287 uint64_t SummaryOffset = OS.
tell();
288 for (
unsigned I = 0; I < SummarySize /
sizeof(uint64_t); I++)
292 uint64_t HashTableStart = Generator.Emit(OS.
OS, *InfoObj);
295 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
299 std::unique_ptr<ProfileSummary>
PS = ISB.getSummary();
301 InfoObj->SummaryBuilder =
nullptr;
306 {HashTableStartFieldOffset, &HashTableStart, 1},
308 {SummaryOffset,
reinterpret_cast<uint64_t *
>(TheSummary.get()),
309 (
int)(SummarySize /
sizeof(uint64_t))}};
310 OS.
patch(PatchItems,
sizeof(PatchItems) /
sizeof(*PatchItems));
330 #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
339 OS <<
"# Func Hash:\n" << Hash <<
"\n";
340 OS <<
"# Num Counters:\n" << Func.
Counts.size() <<
"\n";
341 OS <<
"# Counter Values:\n";
342 for (uint64_t Count : Func.
Counts)
346 if (!NumValueKinds) {
352 for (
uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
356 OS <<
"# ValueKind = " << ValueProfKindStr[VK] <<
":\n" << VK <<
"\n";
357 OS <<
"# NumValueSites:\n" << NS <<
"\n";
361 std::unique_ptr<InstrProfValueData[]> VD = Func.
getValueForSite(VK, S);
363 if (VK == IPVK_IndirectCallTarget)
366 OS << VD[
I].Value <<
":" << VD[
I].Count <<
"\n";
376 OS <<
"# IR level Instrumentation Flag\n:ir\n";
378 for (
const auto &I : FunctionData)
379 if (shouldEncodeData(I.getValue()))
384 for (
const auto &I : FunctionData)
385 if (shouldEncodeData(I.getValue()))
386 for (
const auto &Func : I.getValue())
void setValueProfDataEndianness(support::endianness Endianness)
const InstrProfWriter::ProfilingData *const data_type
static const char * ValueProfKindStr[]
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
void merge(InstrProfRecord &Other, uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Merge the counts in Other into this one.
uint64_t NumCutoffEntries
void sortValueData()
Sort value profile data (per site) by count.
ProfOStream(raw_string_ostream &STR)
Defines facilities for reading and writing on-disk hash tables.
void setEntry(uint32_t I, const ProfileSummaryEntry &E)
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
InstrProfWriter(bool Sparse=false)
An efficient, type-erasing, non-owning reference to a callable.
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
uint64_t getMaxInternalCount()
static void setSummary(IndexedInstrProf::Summary *TheSummary, ProfileSummary &PS)
void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N)
const InstrProfWriter::ProfilingData *const data_type_ref
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, function_ref< void(Error)> Warn)
Add function counts for the given function.
void scale(uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Scale up profile counts (including value profile data) by Weight.
static std::pair< offset_type, offset_type > EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V)
std::unique_ptr< InstrProfValueData[]> getValueForSite(uint32_t ValueKind, uint32_t Site, uint64_t *TotalC=nullptr) const
Return the array of profiled values at Site.
uint64_t NumSummaryFields
uint64_t tell() const
tell - Return the current offset with the file.
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize)
Return function's PGO name from the function name's symbol address in the object file.
uint64_t getMaxFunctionCount()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
size_t size() const
size - Get the array size.
SummaryEntryVector & getDetailedSummary()
void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
Function Alias Analysis false
void write(ArrayRef< value_type > Vals)
void setOutputSparse(bool Sparse)
static hash_value_type ComputeHash(key_type_ref K)
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
uint64_t ComputeHash(StringRef K)
uint32_t getNumValueDataForSite(uint32_t ValueKind, uint32_t Site) const
Return the number of value data collected for ValueKind at profiling site: Site.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
raw_ostream & write(unsigned char C)
static ErrorSuccess success()
Create a success value.
support::endianness ValueProfDataEndianness
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
Generates an on disk hash table.
std::vector< uint64_t > Counts
static void writeRecordInText(StringRef Name, uint64_t Hash, const InstrProfRecord &Counters, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.
A raw_ostream that writes to a file descriptor.
uint32_t getNumFunctions()
Profiling information for a single function.
uint32_t getNumValueKinds() const
Return the number of value profile kinds with non-zero number of profile sites.
void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Error addFuncName(StringRef FuncName)
Update the symtab by adding FuncName to the table.
void patch(PatchItem *P, int NItems)
InstrProfSummaryBuilder * SummaryBuilder
uint32_t getNumValueSites(uint32_t ValueKind) const
Return the number of instrumented sites for ValueKind.
A raw_ostream that writes to an std::string.
void write(raw_fd_ostream &OS)
Write the profile to OS.
LLVM Value Representation.
Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
Lightweight error class with error context and mandatory checking.
This class implements an extremely fast bulk output stream that can only output to a stream...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
std::string Hash(const Unit &U)
ProfOStream(raw_fd_ostream &FD)
support::endian::Writer< support::little > LE
StringRef - Represent a constant reference to a string, i.e.
void set(SummaryFieldKind K, uint64_t V)
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.