LLVM  4.0.0
InstrProfWriter.cpp
Go to the documentation of this file.
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 
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/ProfileSummary.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:
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.
84 };
85 
87 public:
90 
93 
94  typedef uint64_t hash_value_type;
95  typedef uint64_t offset_type;
96 
99 
100  InstrProfRecordWriterTrait() : ValueProfDataEndianness(support::little) {}
103  }
104 
105  static std::pair<offset_type, offset_type>
107  using namespace llvm::support;
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 
129  Out.write(K.data(), N);
130  }
131 
133  using namespace llvm::support;
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 
157  : Sparse(Sparse), FunctionData(), ProfileKind(PF_Unknown),
158  InfoObj(new InstrProfRecordWriterTrait()) {}
159 
161 
162 // Internal interface for testing purpose only.
164  support::endianness Endianness) {
165  InfoObj->ValueProfDataEndianness = Endianness;
166 }
167 
169  this->Sparse = Sparse;
170 }
171 
173  auto &ProfileDataMap = FunctionData[I.Name];
174 
175  bool NewFunc;
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 
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) {
235 
236  using namespace IndexedInstrProf;
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.
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.
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 
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,
315 };
316 
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 
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 }
void scale(uint64_t Weight)
Scale up profile counts (including value profile data) by Weight.
Definition: InstrProf.cpp:422
void setValueProfDataEndianness(support::endianness Endianness)
static const char * ValueProfKindStr[]
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:416
void sortValueData()
Sort value profile data (per site) by count.
Definition: InstrProf.h:630
Error addRecord(InstrProfRecord &&I, uint64_t Weight=1)
Add function counts for the given function.
Defines facilities for reading and writing on-disk hash tables.
void setEntry(uint32_t I, const ProfileSummaryEntry &E)
Definition: InstrProf.h:880
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)
void writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
Definition: InstrProf.h:893
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:66
uint64_t getMaxInternalCount()
static void setSummary(IndexedInstrProf::Summary *TheSummary, ProfileSummary &PS)
const uint64_t Magic
Definition: InstrProf.h:777
void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N)
Error takeError()
Get the error contained within the record's soft error counter.
Definition: InstrProf.h:645
const HashT HashType
Definition: InstrProf.h:801
ProfOStream(llvm::raw_fd_ostream &FD)
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
void merge(InstrProfRecord &Other, uint64_t Weight=1)
Merge the counts in Other into this one.
Definition: InstrProf.cpp:394
static std::pair< offset_type, offset_type > EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V)
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:98
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()
void addFuncName(StringRef FuncName)
Update the symtab by adding FuncName to the table.
Definition: InstrProf.h:468
Function Alias Analysis false
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
SummaryEntryVector & getDetailedSummary()
const InstrProfWriter::ProfilingData *const data_type
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
#define P(N)
void write(ArrayRef< value_type > Vals)
Definition: EndianStream.h:30
void setOutputSparse(bool Sparse)
static hash_value_type ComputeHash(key_type_ref K)
uint64_t Pos
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:743
Error mergeRecordsFromWriter(InstrProfWriter &&IPW)
Merge existing function counts from the given writer.
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:803
uint32_t getNumValueDataForSite(uint32_t ValueKind, uint32_t Site) const
Return the number of value data collected for ValueKind at profiling site: Site.
Definition: InstrProf.h:700
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:479
raw_ostream & write(unsigned char C)
static ErrorSuccess success()
Create a success value.
void write(uint64_t V)
offset_type Emit(raw_ostream &Out)
Emit the table to Out, which must not be at offset 0.
uint64_t getTotalCount()
ProfOStream(llvm::raw_string_ostream &STR)
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
Definition: InstrProf.h:587
Basic Alias true
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:357
uint32_t getNumFunctions()
Profiling information for a single function.
Definition: InstrProf.h:581
uint32_t getNumValueKinds() const
Return the number of value profile kinds with non-zero number of profile sites.
Definition: InstrProf.h:679
void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type)
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
void patch(PatchItem *P, int NItems)
InstrProfSummaryBuilder * SummaryBuilder
uint32_t getNumValueSites(uint32_t ValueKind) const
Return the number of instrumented sites for ValueKind.
Definition: InstrProf.h:696
uint64_t * D
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:463
void write(raw_fd_ostream &OS)
Write the profile to OS.
std::unique_ptr< InstrProfValueData[]> getValueForSite(uint32_t ValueKind, uint32_t Site, uint64_t *TotalC=0) const
Return the array of profiled values at Site.
Definition: InstrProf.h:706
LLVM Value Representation.
Definition: Value.h:71
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...
Definition: raw_ostream.h:44
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).
Definition: StringRef.h:125
support::endian::Writer< support::little > LE
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void set(SummaryFieldKind K, uint64_t V)
Definition: InstrProf.h:875
const InstrProfWriter::ProfilingData *const data_type_ref
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.
static void writeRecordInText(const InstrProfRecord &Record, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.