1 //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing profiling data for clang's
11 // instrumentation based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ProfileData/InstrProfWriter.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/ProfileSummary.h"
18 #include "llvm/ProfileData/ProfileCommon.h"
19 #include "llvm/Support/EndianStream.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/OnDiskHashTable.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <algorithm>
24 #include <string>
25 #include <tuple>
26 #include <utility>
27 #include <vector>
28 
29 using namespace llvm;
30 
31 // A struct to define how the data stream should be patched. For Indexed
32 // profiling, only uint64_t data type is needed.
33 struct PatchItem {
34   uint64_t Pos; // Where to patch.
35   uint64_t *D;  // Pointer to an array of source data.
36   int N;        // Number of elements in \c D array.
37 };
38 
39 namespace llvm {
40 
41 // A wrapper class to abstract writer stream with support of bytes
42 // back patching.
43 class ProfOStream {
44 
45 public:
46   ProfOStream(llvm::raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {}
47   ProfOStream(llvm::raw_string_ostream &STR)
48       : IsFDOStream(false), OS(STR), LE(STR) {}
49 
50   uint64_t tell() { return OS.tell(); }
51   void write(uint64_t V) { LE.write<uint64_t>(V); }
52 
53   // \c patch can only be called when all data is written and flushed.
54   // For raw_string_ostream, the patch is done on the target string
55   // directly and it won't be reflected in the stream's internal buffer.
56   void patch(PatchItem *P, int NItems) {
57     using namespace support;
58     if (IsFDOStream) {
59       llvm::raw_fd_ostream &FDOStream = static_cast<llvm::raw_fd_ostream &>(OS);
60       for (int K = 0; K < NItems; K++) {
61         FDOStream.seek(P[K].Pos);
62         for (int I = 0; I < P[K].N; I++)
63           write(P[K].D[I]);
64       }
65     } else {
66       llvm::raw_string_ostream &SOStream =
67           static_cast<llvm::raw_string_ostream &>(OS);
68       std::string &Data = SOStream.str(); // with flush
69       for (int K = 0; K < NItems; K++) {
70         for (int I = 0; I < P[K].N; I++) {
71           uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
72           Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
73                        (const char *)&Bytes, sizeof(uint64_t));
74         }
75       }
76     }
77   }
78 
79   // If \c OS is an instance of \c raw_fd_ostream, this field will be
80   // true. Otherwise, \c OS will be an raw_string_ostream.
81   bool IsFDOStream;
82   raw_ostream &OS;
83   support::endian::Writer<support::little> LE;
84 };
85 
86 class InstrProfRecordWriterTrait {
87 public:
88   typedef StringRef key_type;
89   typedef StringRef key_type_ref;
90 
91   typedef const InstrProfWriter::ProfilingData *const data_type;
92   typedef const InstrProfWriter::ProfilingData *const data_type_ref;
93 
94   typedef uint64_t hash_value_type;
95   typedef uint64_t offset_type;
96 
97   support::endianness ValueProfDataEndianness;
98   InstrProfSummaryBuilder *SummaryBuilder;
99 
100   InstrProfRecordWriterTrait() : ValueProfDataEndianness(support::little) {}
101   static hash_value_type ComputeHash(key_type_ref K) {
102     return IndexedInstrProf::ComputeHash(K);
103   }
104 
105   static std::pair<offset_type, offset_type>
106   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
107     using namespace llvm::support;
108     endian::Writer<little> LE(Out);
109 
110     offset_type N = K.size();
111     LE.write<offset_type>(N);
112 
113     offset_type M = 0;
114     for (const auto &ProfileData : *V) {
115       const InstrProfRecord &ProfRecord = ProfileData.second;
116       M += sizeof(uint64_t); // The function hash
117       M += sizeof(uint64_t); // The size of the Counts vector
118       M += ProfRecord.Counts.size() * sizeof(uint64_t);
119 
120       // Value data
121       M += ValueProfData::getSize(ProfileData.second);
122     }
123     LE.write<offset_type>(M);
124 
125     return std::make_pair(N, M);
126   }
127 
128   void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
129     Out.write(K.data(), N);
130   }
131 
132   void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
133     using namespace llvm::support;
134     endian::Writer<little> LE(Out);
135     for (const auto &ProfileData : *V) {
136       const InstrProfRecord &ProfRecord = ProfileData.second;
137       SummaryBuilder->addRecord(ProfRecord);
138 
139       LE.write<uint64_t>(ProfileData.first); // Function hash
140       LE.write<uint64_t>(ProfRecord.Counts.size());
141       for (uint64_t I : ProfRecord.Counts)
142         LE.write<uint64_t>(I);
143 
144       // Write value data
145       std::unique_ptr<ValueProfData> VDataPtr =
146           ValueProfData::serializeFrom(ProfileData.second);
147       uint32_t S = VDataPtr->getSize();
148       VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
149       Out.write((const char *)VDataPtr.get(), S);
150     }
151   }
152 };
153 
154 } // end namespace llvm
155 
156 InstrProfWriter::InstrProfWriter(bool Sparse)
157     : Sparse(Sparse), FunctionData(), ProfileKind(PF_Unknown),
158       InfoObj(new InstrProfRecordWriterTrait()) {}
159 
160 InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
161 
162 // Internal interface for testing purpose only.
163 void InstrProfWriter::setValueProfDataEndianness(
164     support::endianness Endianness) {
165   InfoObj->ValueProfDataEndianness = Endianness;
166 }
167 
168 void InstrProfWriter::setOutputSparse(bool Sparse) {
169   this->Sparse = Sparse;
170 }
171 
172 Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) {
173   auto &ProfileDataMap = FunctionData[I.Name];
174 
175   bool NewFunc;
176   ProfilingData::iterator Where;
177   std::tie(Where, NewFunc) =
178       ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
179   InstrProfRecord &Dest = Where->second;
180 
181   if (NewFunc) {
182     // We've never seen a function with this name and hash, add it.
183     Dest = std::move(I);
184     // Fix up the name to avoid dangling reference.
185     Dest.Name = FunctionData.find(Dest.Name)->getKey();
186     if (Weight > 1)
187       Dest.scale(Weight);
188   } else {
189     // We're updating a function we've seen before.
190     Dest.merge(I, Weight);
191   }
192 
193   Dest.sortValueData();
194 
195   return Dest.takeError();
196 }
197 
198 Error InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW) {
199   for (auto &I : IPW.FunctionData)
200     for (auto &Func : I.getValue())
201       if (Error E = addRecord(std::move(Func.second), 1))
202         return E;
203   return Error::success();
204 }
205 
206 bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
207   if (!Sparse)
208     return true;
209   for (const auto &Func : PD) {
210     const InstrProfRecord &IPR = Func.second;
211     if (any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
212       return true;
213   }
214   return false;
215 }
216 
217 static void setSummary(IndexedInstrProf::Summary *TheSummary,
218                        ProfileSummary &PS) {
219   using namespace IndexedInstrProf;
220   std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
221   TheSummary->NumSummaryFields = Summary::NumKinds;
222   TheSummary->NumCutoffEntries = Res.size();
223   TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
224   TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
225   TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
226   TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
227   TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
228   TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
229   for (unsigned I = 0; I < Res.size(); I++)
230     TheSummary->setEntry(I, Res[I]);
231 }
232 
233 void InstrProfWriter::writeImpl(ProfOStream &OS) {
234   OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
235 
236   using namespace IndexedInstrProf;
237   InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
238   InfoObj->SummaryBuilder = &ISB;
239 
240   // Populate the hash table generator.
241   for (const auto &I : FunctionData)
242     if (shouldEncodeData(I.getValue()))
243       Generator.insert(I.getKey(), &I.getValue());
244   // Write the header.
245   IndexedInstrProf::Header Header;
246   Header.Magic = IndexedInstrProf::Magic;
247   Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
248   if (ProfileKind == PF_IRLevel)
249     Header.Version |= VARIANT_MASK_IR_PROF;
250   Header.Unused = 0;
251   Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
252   Header.HashOffset = 0;
253   int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
254 
255   // Only write out all the fields except 'HashOffset'. We need
256   // to remember the offset of that field to allow back patching
257   // later.
258   for (int I = 0; I < N - 1; I++)
259     OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
260 
261   // Save the location of Header.HashOffset field in \c OS.
262   uint64_t HashTableStartFieldOffset = OS.tell();
263   // Reserve the space for HashOffset field.
264   OS.write(0);
265 
266   // Reserve space to write profile summary data.
267   uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
268   uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
269   // Remember the summary offset.
270   uint64_t SummaryOffset = OS.tell();
271   for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
272     OS.write(0);
273 
274   // Write the hash table.
275   uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
276 
277   // Allocate space for data to be serialized out.
278   std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
279       IndexedInstrProf::allocSummary(SummarySize);
280   // Compute the Summary and copy the data to the data
281   // structure to be serialized out (to disk or buffer).
282   std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
283   setSummary(TheSummary.get(), *PS);
284   InfoObj->SummaryBuilder = nullptr;
285 
286   // Now do the final patch:
287   PatchItem PatchItems[] = {
288       // Patch the Header.HashOffset field.
289       {HashTableStartFieldOffset, &HashTableStart, 1},
290       // Patch the summary data.
291       {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
292        (int)(SummarySize / sizeof(uint64_t))}};
293   OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
294 }
295 
296 void InstrProfWriter::write(raw_fd_ostream &OS) {
297   // Write the hash table.
298   ProfOStream POS(OS);
299   writeImpl(POS);
300 }
301 
302 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
303   std::string Data;
304   llvm::raw_string_ostream OS(Data);
305   ProfOStream POS(OS);
306   // Write the hash table.
307   writeImpl(POS);
308   // Return this in an aligned memory buffer.
309   return MemoryBuffer::getMemBufferCopy(Data);
310 }
311 
312 static const char *ValueProfKindStr[] = {
313 #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
314 #include "llvm/ProfileData/InstrProfData.inc"
315 };
316 
317 void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
318                                         InstrProfSymtab &Symtab,
319                                         raw_fd_ostream &OS) {
320   OS << Func.Name << "\n";
321   OS << "# Func Hash:\n" << Func.Hash << "\n";
322   OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
323   OS << "# Counter Values:\n";
324   for (uint64_t Count : Func.Counts)
325     OS << Count << "\n";
326 
327   uint32_t NumValueKinds = Func.getNumValueKinds();
328   if (!NumValueKinds) {
329     OS << "\n";
330     return;
331   }
332 
333   OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
334   for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
335     uint32_t NS = Func.getNumValueSites(VK);
336     if (!NS)
337       continue;
338     OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
339     OS << "# NumValueSites:\n" << NS << "\n";
340     for (uint32_t S = 0; S < NS; S++) {
341       uint32_t ND = Func.getNumValueDataForSite(VK, S);
342       OS << ND << "\n";
343       std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
344       for (uint32_t I = 0; I < ND; I++) {
345         if (VK == IPVK_IndirectCallTarget)
346           OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
347         else
348           OS << VD[I].Value << ":" << VD[I].Count << "\n";
349       }
350     }
351   }
352 
353   OS << "\n";
354 }
355 
356 void InstrProfWriter::writeText(raw_fd_ostream &OS) {
357   if (ProfileKind == PF_IRLevel)
358     OS << "# IR level Instrumentation Flag\n:ir\n";
359   InstrProfSymtab Symtab;
360   for (const auto &I : FunctionData)
361     if (shouldEncodeData(I.getValue()))
362       Symtab.addFuncName(I.getKey());
363   Symtab.finalizeSymtab();
364 
365   for (const auto &I : FunctionData)
366     if (shouldEncodeData(I.getValue()))
367       for (const auto &Func : I.getValue())
368         writeRecordInText(Func.second, Symtab, OS);
369 }
370