17a7e6055SDimitry Andric //===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
291bc56edSDimitry Andric //
391bc56edSDimitry Andric //                     The LLVM Compiler Infrastructure
491bc56edSDimitry Andric //
591bc56edSDimitry Andric // This file is distributed under the University of Illinois Open Source
691bc56edSDimitry Andric // License. See LICENSE.TXT for details.
791bc56edSDimitry Andric //
891bc56edSDimitry Andric //===----------------------------------------------------------------------===//
991bc56edSDimitry Andric //
1091bc56edSDimitry Andric // This file contains support for writing profiling data for clang's
1191bc56edSDimitry Andric // instrumentation based PGO and coverage.
1291bc56edSDimitry Andric //
1391bc56edSDimitry Andric //===----------------------------------------------------------------------===//
1491bc56edSDimitry Andric 
15db17bf38SDimitry Andric #include "llvm/ProfileData/InstrProfWriter.h"
167a7e6055SDimitry Andric #include "llvm/ADT/STLExtras.h"
17d88c1a5aSDimitry Andric #include "llvm/ADT/StringRef.h"
18d88c1a5aSDimitry Andric #include "llvm/IR/ProfileSummary.h"
197a7e6055SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
20d88c1a5aSDimitry Andric #include "llvm/ProfileData/ProfileCommon.h"
217a7e6055SDimitry Andric #include "llvm/Support/Endian.h"
2291bc56edSDimitry Andric #include "llvm/Support/EndianStream.h"
237a7e6055SDimitry Andric #include "llvm/Support/Error.h"
24d88c1a5aSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
2591bc56edSDimitry Andric #include "llvm/Support/OnDiskHashTable.h"
26d88c1a5aSDimitry Andric #include "llvm/Support/raw_ostream.h"
27d88c1a5aSDimitry Andric #include <algorithm>
287a7e6055SDimitry Andric #include <cstdint>
297a7e6055SDimitry Andric #include <memory>
30d88c1a5aSDimitry Andric #include <string>
317d523365SDimitry Andric #include <tuple>
32d88c1a5aSDimitry Andric #include <utility>
33d88c1a5aSDimitry Andric #include <vector>
3491bc56edSDimitry Andric 
3591bc56edSDimitry Andric using namespace llvm;
3691bc56edSDimitry Andric 
373ca95b02SDimitry Andric // A struct to define how the data stream should be patched. For Indexed
383ca95b02SDimitry Andric // profiling, only uint64_t data type is needed.
393ca95b02SDimitry Andric struct PatchItem {
403ca95b02SDimitry Andric   uint64_t Pos; // Where to patch.
413ca95b02SDimitry Andric   uint64_t *D;  // Pointer to an array of source data.
423ca95b02SDimitry Andric   int N;        // Number of elements in \c D array.
433ca95b02SDimitry Andric };
447d523365SDimitry Andric 
453ca95b02SDimitry Andric namespace llvm {
46d88c1a5aSDimitry Andric 
473ca95b02SDimitry Andric // A wrapper class to abstract writer stream with support of bytes
483ca95b02SDimitry Andric // back patching.
493ca95b02SDimitry Andric class ProfOStream {
503ca95b02SDimitry Andric public:
ProfOStream(raw_fd_ostream & FD)51*4ba319b5SDimitry Andric   ProfOStream(raw_fd_ostream &FD)
52*4ba319b5SDimitry Andric       : IsFDOStream(true), OS(FD), LE(FD, support::little) {}
ProfOStream(raw_string_ostream & STR)537a7e6055SDimitry Andric   ProfOStream(raw_string_ostream &STR)
54*4ba319b5SDimitry Andric       : IsFDOStream(false), OS(STR), LE(STR, support::little) {}
553ca95b02SDimitry Andric 
tell()563ca95b02SDimitry Andric   uint64_t tell() { return OS.tell(); }
write(uint64_t V)573ca95b02SDimitry Andric   void write(uint64_t V) { LE.write<uint64_t>(V); }
58d88c1a5aSDimitry Andric 
593ca95b02SDimitry Andric   // \c patch can only be called when all data is written and flushed.
603ca95b02SDimitry Andric   // For raw_string_ostream, the patch is done on the target string
613ca95b02SDimitry Andric   // directly and it won't be reflected in the stream's internal buffer.
patch(PatchItem * P,int NItems)623ca95b02SDimitry Andric   void patch(PatchItem *P, int NItems) {
633ca95b02SDimitry Andric     using namespace support;
647a7e6055SDimitry Andric 
653ca95b02SDimitry Andric     if (IsFDOStream) {
667a7e6055SDimitry Andric       raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
673ca95b02SDimitry Andric       for (int K = 0; K < NItems; K++) {
683ca95b02SDimitry Andric         FDOStream.seek(P[K].Pos);
693ca95b02SDimitry Andric         for (int I = 0; I < P[K].N; I++)
703ca95b02SDimitry Andric           write(P[K].D[I]);
713ca95b02SDimitry Andric       }
723ca95b02SDimitry Andric     } else {
73edd7eaddSDimitry Andric       raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
743ca95b02SDimitry Andric       std::string &Data = SOStream.str(); // with flush
753ca95b02SDimitry Andric       for (int K = 0; K < NItems; K++) {
763ca95b02SDimitry Andric         for (int I = 0; I < P[K].N; I++) {
773ca95b02SDimitry Andric           uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
783ca95b02SDimitry Andric           Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
793ca95b02SDimitry Andric                        (const char *)&Bytes, sizeof(uint64_t));
803ca95b02SDimitry Andric         }
813ca95b02SDimitry Andric       }
823ca95b02SDimitry Andric     }
833ca95b02SDimitry Andric   }
84d88c1a5aSDimitry Andric 
853ca95b02SDimitry Andric   // If \c OS is an instance of \c raw_fd_ostream, this field will be
863ca95b02SDimitry Andric   // true. Otherwise, \c OS will be an raw_string_ostream.
873ca95b02SDimitry Andric   bool IsFDOStream;
883ca95b02SDimitry Andric   raw_ostream &OS;
89*4ba319b5SDimitry Andric   support::endian::Writer LE;
903ca95b02SDimitry Andric };
913ca95b02SDimitry Andric 
923ca95b02SDimitry Andric class InstrProfRecordWriterTrait {
9391bc56edSDimitry Andric public:
94edd7eaddSDimitry Andric   using key_type = StringRef;
95edd7eaddSDimitry Andric   using key_type_ref = StringRef;
9691bc56edSDimitry Andric 
97edd7eaddSDimitry Andric   using data_type = const InstrProfWriter::ProfilingData *const;
98edd7eaddSDimitry Andric   using data_type_ref = const InstrProfWriter::ProfilingData *const;
9991bc56edSDimitry Andric 
100edd7eaddSDimitry Andric   using hash_value_type = uint64_t;
101edd7eaddSDimitry Andric   using offset_type = uint64_t;
10291bc56edSDimitry Andric 
1037a7e6055SDimitry Andric   support::endianness ValueProfDataEndianness = support::little;
1043ca95b02SDimitry Andric   InstrProfSummaryBuilder *SummaryBuilder;
1053ca95b02SDimitry Andric 
1067a7e6055SDimitry Andric   InstrProfRecordWriterTrait() = default;
1077a7e6055SDimitry Andric 
ComputeHash(key_type_ref K)10891bc56edSDimitry Andric   static hash_value_type ComputeHash(key_type_ref K) {
1097d523365SDimitry Andric     return IndexedInstrProf::ComputeHash(K);
11091bc56edSDimitry Andric   }
11191bc56edSDimitry Andric 
11291bc56edSDimitry Andric   static std::pair<offset_type, offset_type>
EmitKeyDataLength(raw_ostream & Out,key_type_ref K,data_type_ref V)11391bc56edSDimitry Andric   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
1147a7e6055SDimitry Andric     using namespace support;
1157a7e6055SDimitry Andric 
116*4ba319b5SDimitry Andric     endian::Writer LE(Out, little);
11791bc56edSDimitry Andric 
11891bc56edSDimitry Andric     offset_type N = K.size();
11991bc56edSDimitry Andric     LE.write<offset_type>(N);
12091bc56edSDimitry Andric 
12139d628a0SDimitry Andric     offset_type M = 0;
1227d523365SDimitry Andric     for (const auto &ProfileData : *V) {
1237d523365SDimitry Andric       const InstrProfRecord &ProfRecord = ProfileData.second;
1247d523365SDimitry Andric       M += sizeof(uint64_t); // The function hash
1257d523365SDimitry Andric       M += sizeof(uint64_t); // The size of the Counts vector
1267d523365SDimitry Andric       M += ProfRecord.Counts.size() * sizeof(uint64_t);
1277d523365SDimitry Andric 
1287d523365SDimitry Andric       // Value data
1297d523365SDimitry Andric       M += ValueProfData::getSize(ProfileData.second);
1307d523365SDimitry Andric     }
13191bc56edSDimitry Andric     LE.write<offset_type>(M);
13291bc56edSDimitry Andric 
13391bc56edSDimitry Andric     return std::make_pair(N, M);
13491bc56edSDimitry Andric   }
13591bc56edSDimitry Andric 
EmitKey(raw_ostream & Out,key_type_ref K,offset_type N)1363ca95b02SDimitry Andric   void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
13791bc56edSDimitry Andric     Out.write(K.data(), N);
13891bc56edSDimitry Andric   }
13991bc56edSDimitry Andric 
EmitData(raw_ostream & Out,key_type_ref,data_type_ref V,offset_type)1403ca95b02SDimitry Andric   void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
1417a7e6055SDimitry Andric     using namespace support;
1427a7e6055SDimitry Andric 
143*4ba319b5SDimitry Andric     endian::Writer LE(Out, little);
1447d523365SDimitry Andric     for (const auto &ProfileData : *V) {
1457d523365SDimitry Andric       const InstrProfRecord &ProfRecord = ProfileData.second;
1463ca95b02SDimitry Andric       SummaryBuilder->addRecord(ProfRecord);
14739d628a0SDimitry Andric 
1487d523365SDimitry Andric       LE.write<uint64_t>(ProfileData.first); // Function hash
1497d523365SDimitry Andric       LE.write<uint64_t>(ProfRecord.Counts.size());
1507d523365SDimitry Andric       for (uint64_t I : ProfRecord.Counts)
15191bc56edSDimitry Andric         LE.write<uint64_t>(I);
1527d523365SDimitry Andric 
1537d523365SDimitry Andric       // Write value data
1547d523365SDimitry Andric       std::unique_ptr<ValueProfData> VDataPtr =
1557d523365SDimitry Andric           ValueProfData::serializeFrom(ProfileData.second);
1567d523365SDimitry Andric       uint32_t S = VDataPtr->getSize();
1577d523365SDimitry Andric       VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
1587d523365SDimitry Andric       Out.write((const char *)VDataPtr.get(), S);
15991bc56edSDimitry Andric     }
16039d628a0SDimitry Andric   }
16191bc56edSDimitry Andric };
162d88c1a5aSDimitry Andric 
163d88c1a5aSDimitry Andric } // end namespace llvm
16491bc56edSDimitry Andric 
InstrProfWriter(bool Sparse)1653ca95b02SDimitry Andric InstrProfWriter::InstrProfWriter(bool Sparse)
1667a7e6055SDimitry Andric     : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
1673ca95b02SDimitry Andric 
~InstrProfWriter()1683ca95b02SDimitry Andric InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
1693ca95b02SDimitry Andric 
1707d523365SDimitry Andric // Internal interface for testing purpose only.
setValueProfDataEndianness(support::endianness Endianness)1717d523365SDimitry Andric void InstrProfWriter::setValueProfDataEndianness(
1727d523365SDimitry Andric     support::endianness Endianness) {
1733ca95b02SDimitry Andric   InfoObj->ValueProfDataEndianness = Endianness;
1743ca95b02SDimitry Andric }
175d88c1a5aSDimitry Andric 
setOutputSparse(bool Sparse)1763ca95b02SDimitry Andric void InstrProfWriter::setOutputSparse(bool Sparse) {
1773ca95b02SDimitry Andric   this->Sparse = Sparse;
1787d523365SDimitry Andric }
17939d628a0SDimitry Andric 
addRecord(NamedInstrProfRecord && I,uint64_t Weight,function_ref<void (Error)> Warn)180c4394386SDimitry Andric void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
181c4394386SDimitry Andric                                 function_ref<void(Error)> Warn) {
182c4394386SDimitry Andric   auto Name = I.Name;
183c4394386SDimitry Andric   auto Hash = I.Hash;
184c4394386SDimitry Andric   addRecord(Name, Hash, std::move(I), Weight, Warn);
185c4394386SDimitry Andric }
186c4394386SDimitry Andric 
addRecord(StringRef Name,uint64_t Hash,InstrProfRecord && I,uint64_t Weight,function_ref<void (Error)> Warn)187c4394386SDimitry Andric void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
188c4394386SDimitry Andric                                 InstrProfRecord &&I, uint64_t Weight,
189c4394386SDimitry Andric                                 function_ref<void(Error)> Warn) {
190c4394386SDimitry Andric   auto &ProfileDataMap = FunctionData[Name];
1917d523365SDimitry Andric 
1927d523365SDimitry Andric   bool NewFunc;
1937d523365SDimitry Andric   ProfilingData::iterator Where;
1947d523365SDimitry Andric   std::tie(Where, NewFunc) =
195c4394386SDimitry Andric       ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
1967d523365SDimitry Andric   InstrProfRecord &Dest = Where->second;
1977d523365SDimitry Andric 
198c4394386SDimitry Andric   auto MapWarn = [&](instrprof_error E) {
199c4394386SDimitry Andric     Warn(make_error<InstrProfError>(E));
200c4394386SDimitry Andric   };
201c4394386SDimitry Andric 
2027d523365SDimitry Andric   if (NewFunc) {
20339d628a0SDimitry Andric     // We've never seen a function with this name and hash, add it.
2047d523365SDimitry Andric     Dest = std::move(I);
205444ed5c5SDimitry Andric     if (Weight > 1)
206c4394386SDimitry Andric       Dest.scale(Weight, MapWarn);
2077d523365SDimitry Andric   } else {
20839d628a0SDimitry Andric     // We're updating a function we've seen before.
209c4394386SDimitry Andric     Dest.merge(I, Weight, MapWarn);
21091bc56edSDimitry Andric   }
21139d628a0SDimitry Andric 
212444ed5c5SDimitry Andric   Dest.sortValueData();
21391bc56edSDimitry Andric }
21491bc56edSDimitry Andric 
mergeRecordsFromWriter(InstrProfWriter && IPW,function_ref<void (Error)> Warn)215c4394386SDimitry Andric void InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW,
216c4394386SDimitry Andric                                              function_ref<void(Error)> Warn) {
217d88c1a5aSDimitry Andric   for (auto &I : IPW.FunctionData)
218d88c1a5aSDimitry Andric     for (auto &Func : I.getValue())
219c4394386SDimitry Andric       addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
220d88c1a5aSDimitry Andric }
221d88c1a5aSDimitry Andric 
shouldEncodeData(const ProfilingData & PD)2223ca95b02SDimitry Andric bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
2233ca95b02SDimitry Andric   if (!Sparse)
2243ca95b02SDimitry Andric     return true;
2253ca95b02SDimitry Andric   for (const auto &Func : PD) {
2263ca95b02SDimitry Andric     const InstrProfRecord &IPR = Func.second;
2277a7e6055SDimitry Andric     if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
2283ca95b02SDimitry Andric       return true;
2293ca95b02SDimitry Andric   }
2303ca95b02SDimitry Andric   return false;
2313ca95b02SDimitry Andric }
2323ca95b02SDimitry Andric 
setSummary(IndexedInstrProf::Summary * TheSummary,ProfileSummary & PS)2333ca95b02SDimitry Andric static void setSummary(IndexedInstrProf::Summary *TheSummary,
2343ca95b02SDimitry Andric                        ProfileSummary &PS) {
2353ca95b02SDimitry Andric   using namespace IndexedInstrProf;
2367a7e6055SDimitry Andric 
2373ca95b02SDimitry Andric   std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
2383ca95b02SDimitry Andric   TheSummary->NumSummaryFields = Summary::NumKinds;
2393ca95b02SDimitry Andric   TheSummary->NumCutoffEntries = Res.size();
2403ca95b02SDimitry Andric   TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
2413ca95b02SDimitry Andric   TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
2423ca95b02SDimitry Andric   TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
2433ca95b02SDimitry Andric   TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
2443ca95b02SDimitry Andric   TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
2453ca95b02SDimitry Andric   TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
2463ca95b02SDimitry Andric   for (unsigned I = 0; I < Res.size(); I++)
2473ca95b02SDimitry Andric     TheSummary->setEntry(I, Res[I]);
2483ca95b02SDimitry Andric }
2493ca95b02SDimitry Andric 
writeImpl(ProfOStream & OS)2503ca95b02SDimitry Andric void InstrProfWriter::writeImpl(ProfOStream &OS) {
2517a7e6055SDimitry Andric   using namespace IndexedInstrProf;
2527a7e6055SDimitry Andric 
2533ca95b02SDimitry Andric   OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
2543ca95b02SDimitry Andric 
2553ca95b02SDimitry Andric   InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
2563ca95b02SDimitry Andric   InfoObj->SummaryBuilder = &ISB;
25791bc56edSDimitry Andric 
25891bc56edSDimitry Andric   // Populate the hash table generator.
25939d628a0SDimitry Andric   for (const auto &I : FunctionData)
2603ca95b02SDimitry Andric     if (shouldEncodeData(I.getValue()))
26191bc56edSDimitry Andric       Generator.insert(I.getKey(), &I.getValue());
26291bc56edSDimitry Andric   // Write the header.
2637d523365SDimitry Andric   IndexedInstrProf::Header Header;
2647d523365SDimitry Andric   Header.Magic = IndexedInstrProf::Magic;
2653ca95b02SDimitry Andric   Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
2663ca95b02SDimitry Andric   if (ProfileKind == PF_IRLevel)
2673ca95b02SDimitry Andric     Header.Version |= VARIANT_MASK_IR_PROF;
2683ca95b02SDimitry Andric   Header.Unused = 0;
2697d523365SDimitry Andric   Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
2707d523365SDimitry Andric   Header.HashOffset = 0;
2717d523365SDimitry Andric   int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
2727d523365SDimitry Andric 
2733ca95b02SDimitry Andric   // Only write out all the fields except 'HashOffset'. We need
2747d523365SDimitry Andric   // to remember the offset of that field to allow back patching
2757d523365SDimitry Andric   // later.
2767d523365SDimitry Andric   for (int I = 0; I < N - 1; I++)
2773ca95b02SDimitry Andric     OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
27891bc56edSDimitry Andric 
2793ca95b02SDimitry Andric   // Save the location of Header.HashOffset field in \c OS.
2803ca95b02SDimitry Andric   uint64_t HashTableStartFieldOffset = OS.tell();
2817d523365SDimitry Andric   // Reserve the space for HashOffset field.
2823ca95b02SDimitry Andric   OS.write(0);
28391bc56edSDimitry Andric 
2843ca95b02SDimitry Andric   // Reserve space to write profile summary data.
2853ca95b02SDimitry Andric   uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
2863ca95b02SDimitry Andric   uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
2873ca95b02SDimitry Andric   // Remember the summary offset.
2883ca95b02SDimitry Andric   uint64_t SummaryOffset = OS.tell();
2893ca95b02SDimitry Andric   for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
2903ca95b02SDimitry Andric     OS.write(0);
2913ca95b02SDimitry Andric 
2923ca95b02SDimitry Andric   // Write the hash table.
2933ca95b02SDimitry Andric   uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
2943ca95b02SDimitry Andric 
2953ca95b02SDimitry Andric   // Allocate space for data to be serialized out.
2963ca95b02SDimitry Andric   std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
2973ca95b02SDimitry Andric       IndexedInstrProf::allocSummary(SummarySize);
2983ca95b02SDimitry Andric   // Compute the Summary and copy the data to the data
2993ca95b02SDimitry Andric   // structure to be serialized out (to disk or buffer).
3003ca95b02SDimitry Andric   std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
3013ca95b02SDimitry Andric   setSummary(TheSummary.get(), *PS);
302d88c1a5aSDimitry Andric   InfoObj->SummaryBuilder = nullptr;
3033ca95b02SDimitry Andric 
3043ca95b02SDimitry Andric   // Now do the final patch:
3053ca95b02SDimitry Andric   PatchItem PatchItems[] = {
3063ca95b02SDimitry Andric       // Patch the Header.HashOffset field.
3073ca95b02SDimitry Andric       {HashTableStartFieldOffset, &HashTableStart, 1},
3083ca95b02SDimitry Andric       // Patch the summary data.
3093ca95b02SDimitry Andric       {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
3103ca95b02SDimitry Andric        (int)(SummarySize / sizeof(uint64_t))}};
3113ca95b02SDimitry Andric   OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
312ff0cc061SDimitry Andric }
313ff0cc061SDimitry Andric 
write(raw_fd_ostream & OS)314ff0cc061SDimitry Andric void InstrProfWriter::write(raw_fd_ostream &OS) {
315ff0cc061SDimitry Andric   // Write the hash table.
3163ca95b02SDimitry Andric   ProfOStream POS(OS);
3173ca95b02SDimitry Andric   writeImpl(POS);
3183ca95b02SDimitry Andric }
319ff0cc061SDimitry Andric 
writeBuffer()3203ca95b02SDimitry Andric std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
3213ca95b02SDimitry Andric   std::string Data;
3227a7e6055SDimitry Andric   raw_string_ostream OS(Data);
3233ca95b02SDimitry Andric   ProfOStream POS(OS);
3243ca95b02SDimitry Andric   // Write the hash table.
3253ca95b02SDimitry Andric   writeImpl(POS);
3263ca95b02SDimitry Andric   // Return this in an aligned memory buffer.
3273ca95b02SDimitry Andric   return MemoryBuffer::getMemBufferCopy(Data);
328ff0cc061SDimitry Andric }
329ff0cc061SDimitry Andric 
3307d523365SDimitry Andric static const char *ValueProfKindStr[] = {
3317d523365SDimitry Andric #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
3327d523365SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc"
3337d523365SDimitry Andric };
3347d523365SDimitry Andric 
writeRecordInText(StringRef Name,uint64_t Hash,const InstrProfRecord & Func,InstrProfSymtab & Symtab,raw_fd_ostream & OS)335c4394386SDimitry Andric void InstrProfWriter::writeRecordInText(StringRef Name, uint64_t Hash,
336c4394386SDimitry Andric                                         const InstrProfRecord &Func,
3377d523365SDimitry Andric                                         InstrProfSymtab &Symtab,
3387d523365SDimitry Andric                                         raw_fd_ostream &OS) {
339c4394386SDimitry Andric   OS << Name << "\n";
340c4394386SDimitry Andric   OS << "# Func Hash:\n" << Hash << "\n";
3417d523365SDimitry Andric   OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
3427d523365SDimitry Andric   OS << "# Counter Values:\n";
3437d523365SDimitry Andric   for (uint64_t Count : Func.Counts)
3447d523365SDimitry Andric     OS << Count << "\n";
3457d523365SDimitry Andric 
3467d523365SDimitry Andric   uint32_t NumValueKinds = Func.getNumValueKinds();
3477d523365SDimitry Andric   if (!NumValueKinds) {
3487d523365SDimitry Andric     OS << "\n";
3497d523365SDimitry Andric     return;
3507d523365SDimitry Andric   }
3517d523365SDimitry Andric 
3527d523365SDimitry Andric   OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
3537d523365SDimitry Andric   for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
3547d523365SDimitry Andric     uint32_t NS = Func.getNumValueSites(VK);
3557d523365SDimitry Andric     if (!NS)
3567d523365SDimitry Andric       continue;
3577d523365SDimitry Andric     OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
3587d523365SDimitry Andric     OS << "# NumValueSites:\n" << NS << "\n";
3597d523365SDimitry Andric     for (uint32_t S = 0; S < NS; S++) {
3607d523365SDimitry Andric       uint32_t ND = Func.getNumValueDataForSite(VK, S);
3617d523365SDimitry Andric       OS << ND << "\n";
3627d523365SDimitry Andric       std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
3637d523365SDimitry Andric       for (uint32_t I = 0; I < ND; I++) {
3647d523365SDimitry Andric         if (VK == IPVK_IndirectCallTarget)
365*4ba319b5SDimitry Andric           OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
366*4ba319b5SDimitry Andric              << VD[I].Count << "\n";
3677d523365SDimitry Andric         else
3687d523365SDimitry Andric           OS << VD[I].Value << ":" << VD[I].Count << "\n";
3697d523365SDimitry Andric       }
3707d523365SDimitry Andric     }
3717d523365SDimitry Andric   }
3727d523365SDimitry Andric 
3737d523365SDimitry Andric   OS << "\n";
3747d523365SDimitry Andric }
3757d523365SDimitry Andric 
writeText(raw_fd_ostream & OS)376edd7eaddSDimitry Andric Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
3773ca95b02SDimitry Andric   if (ProfileKind == PF_IRLevel)
3783ca95b02SDimitry Andric     OS << "# IR level Instrumentation Flag\n:ir\n";
3797d523365SDimitry Andric   InstrProfSymtab Symtab;
3807d523365SDimitry Andric   for (const auto &I : FunctionData)
3813ca95b02SDimitry Andric     if (shouldEncodeData(I.getValue()))
382edd7eaddSDimitry Andric       if (Error E = Symtab.addFuncName(I.getKey()))
383edd7eaddSDimitry Andric         return E;
3847d523365SDimitry Andric 
3857d523365SDimitry Andric   for (const auto &I : FunctionData)
3863ca95b02SDimitry Andric     if (shouldEncodeData(I.getValue()))
3877d523365SDimitry Andric       for (const auto &Func : I.getValue())
388c4394386SDimitry Andric         writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS);
389edd7eaddSDimitry Andric   return Error::success();
3907d523365SDimitry Andric }
391