LLVM  6.0.0svn
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/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/ProfileSummary.h"
21 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/Error.h"
27 #include <algorithm>
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <tuple>
32 #include <utility>
33 #include <vector>
34 
35 using namespace llvm;
36 
37 // A struct to define how the data stream should be patched. For Indexed
38 // profiling, only uint64_t data type is needed.
39 struct PatchItem {
40  uint64_t Pos; // Where to patch.
41  uint64_t *D; // Pointer to an array of source data.
42  int N; // Number of elements in \c D array.
43 };
44 
45 namespace llvm {
46 
47 // A wrapper class to abstract writer stream with support of bytes
48 // back patching.
49 class ProfOStream {
50 public:
51  ProfOStream(raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {}
53  : IsFDOStream(false), OS(STR), LE(STR) {}
54 
55  uint64_t tell() { return OS.tell(); }
56  void write(uint64_t V) { LE.write<uint64_t>(V); }
57 
58  // \c patch can only be called when all data is written and flushed.
59  // For raw_string_ostream, the patch is done on the target string
60  // directly and it won't be reflected in the stream's internal buffer.
61  void patch(PatchItem *P, int NItems) {
62  using namespace support;
63 
64  if (IsFDOStream) {
65  raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
66  for (int K = 0; K < NItems; K++) {
67  FDOStream.seek(P[K].Pos);
68  for (int I = 0; I < P[K].N; I++)
69  write(P[K].D[I]);
70  }
71  } else {
72  raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
73  std::string &Data = SOStream.str(); // with flush
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));
79  }
80  }
81  }
82  }
83 
84  // If \c OS is an instance of \c raw_fd_ostream, this field will be
85  // true. Otherwise, \c OS will be an raw_string_ostream.
89 };
90 
92 public:
95 
98 
99  using hash_value_type = uint64_t;
100  using offset_type = uint64_t;
101 
102  support::endianness ValueProfDataEndianness = support::little;
104 
105  InstrProfRecordWriterTrait() = default;
106 
109  }
110 
111  static std::pair<offset_type, offset_type>
113  using namespace support;
114 
115  endian::Writer<little> LE(Out);
116 
117  offset_type N = K.size();
118  LE.write<offset_type>(N);
119 
120  offset_type M = 0;
121  for (const auto &ProfileData : *V) {
122  const InstrProfRecord &ProfRecord = ProfileData.second;
123  M += sizeof(uint64_t); // The function hash
124  M += sizeof(uint64_t); // The size of the Counts vector
125  M += ProfRecord.Counts.size() * sizeof(uint64_t);
126 
127  // Value data
128  M += ValueProfData::getSize(ProfileData.second);
129  }
130  LE.write<offset_type>(M);
131 
132  return std::make_pair(N, M);
133  }
134 
136  Out.write(K.data(), N);
137  }
138 
140  using namespace support;
141 
142  endian::Writer<little> LE(Out);
143  for (const auto &ProfileData : *V) {
144  const InstrProfRecord &ProfRecord = ProfileData.second;
145  SummaryBuilder->addRecord(ProfRecord);
146 
147  LE.write<uint64_t>(ProfileData.first); // Function hash
148  LE.write<uint64_t>(ProfRecord.Counts.size());
149  for (uint64_t I : ProfRecord.Counts)
150  LE.write<uint64_t>(I);
151 
152  // Write value data
153  std::unique_ptr<ValueProfData> VDataPtr =
154  ValueProfData::serializeFrom(ProfileData.second);
155  uint32_t S = VDataPtr->getSize();
156  VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
157  Out.write((const char *)VDataPtr.get(), S);
158  }
159  }
160 };
161 
162 } // end namespace llvm
163 
165  : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
166 
168 
169 // Internal interface for testing purpose only.
171  support::endianness Endianness) {
172  InfoObj->ValueProfDataEndianness = Endianness;
173 }
174 
176  this->Sparse = Sparse;
177 }
178 
180  function_ref<void(Error)> Warn) {
181  auto Name = I.Name;
182  auto Hash = I.Hash;
183  addRecord(Name, Hash, std::move(I), Weight, Warn);
184 }
185 
186 void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
187  InstrProfRecord &&I, uint64_t Weight,
188  function_ref<void(Error)> Warn) {
189  auto &ProfileDataMap = FunctionData[Name];
190 
191  bool NewFunc;
193  std::tie(Where, NewFunc) =
194  ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
195  InstrProfRecord &Dest = Where->second;
196 
197  auto MapWarn = [&](instrprof_error E) {
198  Warn(make_error<InstrProfError>(E));
199  };
200 
201  if (NewFunc) {
202  // We've never seen a function with this name and hash, add it.
203  Dest = std::move(I);
204  if (Weight > 1)
205  Dest.scale(Weight, MapWarn);
206  } else {
207  // We're updating a function we've seen before.
208  Dest.merge(I, Weight, MapWarn);
209  }
210 
211  Dest.sortValueData();
212 }
213 
215  function_ref<void(Error)> Warn) {
216  for (auto &I : IPW.FunctionData)
217  for (auto &Func : I.getValue())
218  addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
219 }
220 
221 bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
222  if (!Sparse)
223  return true;
224  for (const auto &Func : PD) {
225  const InstrProfRecord &IPR = Func.second;
226  if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
227  return true;
228  }
229  return false;
230 }
231 
232 static void setSummary(IndexedInstrProf::Summary *TheSummary,
233  ProfileSummary &PS) {
234  using namespace IndexedInstrProf;
235 
236  std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
237  TheSummary->NumSummaryFields = Summary::NumKinds;
238  TheSummary->NumCutoffEntries = Res.size();
239  TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
240  TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
241  TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
242  TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
243  TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
244  TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
245  for (unsigned I = 0; I < Res.size(); I++)
246  TheSummary->setEntry(I, Res[I]);
247 }
248 
249 void InstrProfWriter::writeImpl(ProfOStream &OS) {
250  using namespace IndexedInstrProf;
251 
253 
255  InfoObj->SummaryBuilder = &ISB;
256 
257  // Populate the hash table generator.
258  for (const auto &I : FunctionData)
259  if (shouldEncodeData(I.getValue()))
260  Generator.insert(I.getKey(), &I.getValue());
261  // Write the header.
265  if (ProfileKind == PF_IRLevel)
266  Header.Version |= VARIANT_MASK_IR_PROF;
267  Header.Unused = 0;
268  Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
269  Header.HashOffset = 0;
270  int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
271 
272  // Only write out all the fields except 'HashOffset'. We need
273  // to remember the offset of that field to allow back patching
274  // later.
275  for (int I = 0; I < N - 1; I++)
276  OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
277 
278  // Save the location of Header.HashOffset field in \c OS.
279  uint64_t HashTableStartFieldOffset = OS.tell();
280  // Reserve the space for HashOffset field.
281  OS.write(0);
282 
283  // Reserve space to write profile summary data.
285  uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
286  // Remember the summary offset.
287  uint64_t SummaryOffset = OS.tell();
288  for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
289  OS.write(0);
290 
291  // Write the hash table.
292  uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
293 
294  // Allocate space for data to be serialized out.
295  std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
296  IndexedInstrProf::allocSummary(SummarySize);
297  // Compute the Summary and copy the data to the data
298  // structure to be serialized out (to disk or buffer).
299  std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
300  setSummary(TheSummary.get(), *PS);
301  InfoObj->SummaryBuilder = nullptr;
302 
303  // Now do the final patch:
304  PatchItem PatchItems[] = {
305  // Patch the Header.HashOffset field.
306  {HashTableStartFieldOffset, &HashTableStart, 1},
307  // Patch the summary data.
308  {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
309  (int)(SummarySize / sizeof(uint64_t))}};
310  OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
311 }
312 
314  // Write the hash table.
315  ProfOStream POS(OS);
316  writeImpl(POS);
317 }
318 
319 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
320  std::string Data;
321  raw_string_ostream OS(Data);
322  ProfOStream POS(OS);
323  // Write the hash table.
324  writeImpl(POS);
325  // Return this in an aligned memory buffer.
326  return MemoryBuffer::getMemBufferCopy(Data);
327 }
328 
329 static const char *ValueProfKindStr[] = {
330 #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
332 };
333 
335  const InstrProfRecord &Func,
336  InstrProfSymtab &Symtab,
337  raw_fd_ostream &OS) {
338  OS << Name << "\n";
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)
343  OS << Count << "\n";
344 
345  uint32_t NumValueKinds = Func.getNumValueKinds();
346  if (!NumValueKinds) {
347  OS << "\n";
348  return;
349  }
350 
351  OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
352  for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
353  uint32_t NS = Func.getNumValueSites(VK);
354  if (!NS)
355  continue;
356  OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
357  OS << "# NumValueSites:\n" << NS << "\n";
358  for (uint32_t S = 0; S < NS; S++) {
359  uint32_t ND = Func.getNumValueDataForSite(VK, S);
360  OS << ND << "\n";
361  std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
362  for (uint32_t I = 0; I < ND; I++) {
363  if (VK == IPVK_IndirectCallTarget)
364  OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
365  else
366  OS << VD[I].Value << ":" << VD[I].Count << "\n";
367  }
368  }
369  }
370 
371  OS << "\n";
372 }
373 
375  if (ProfileKind == PF_IRLevel)
376  OS << "# IR level Instrumentation Flag\n:ir\n";
377  InstrProfSymtab Symtab;
378  for (const auto &I : FunctionData)
379  if (shouldEncodeData(I.getValue()))
380  if (Error E = Symtab.addFuncName(I.getKey()))
381  return E;
382  Symtab.finalizeSymtab();
383 
384  for (const auto &I : FunctionData)
385  if (shouldEncodeData(I.getValue()))
386  for (const auto &Func : I.getValue())
387  writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS);
388  return Error::success();
389 }
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...
Definition: InstrProf.h:411
void merge(InstrProfRecord &Other, uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Merge the counts in Other into this one.
Definition: InstrProf.cpp:522
void sortValueData()
Sort value profile data (per site) by count.
Definition: InstrProf.h:664
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
ProfOStream(raw_string_ostream &STR)
Defines facilities for reading and writing on-disk hash tables.
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:767
void setEntry(uint32_t I, const ProfileSummaryEntry &E)
Definition: InstrProf.h:956
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
Definition: InstrProf.h:964
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
uint64_t getMaxInternalCount()
void addRecord(const InstrProfRecord &)
uint32_t getNumValueSites(uint32_t ValueKind) const
Return the number of instrumented sites for ValueKind.
Definition: InstrProf.h:763
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:128
static void setSummary(IndexedInstrProf::Summary *TheSummary, ProfileSummary &PS)
const uint64_t Magic
Definition: InstrProf.h:843
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.
const HashT HashType
Definition: InstrProf.h:869
void scale(uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Scale up profile counts (including value profile data) by Weight.
Definition: InstrProf.cpp:550
static std::pair< offset_type, offset_type > EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V)
StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize)
Return function&#39;s PGO name from the function name&#39;s symbol address in the object file.
uint64_t getMaxFunctionCount()
SummaryEntryVector & getDetailedSummary()
void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
#define P(N)
void setOutputSparse(bool Sparse)
static hash_value_type ComputeHash(key_type_ref K)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t Pos
uint32_t getNumValueKinds() const
Return the number of value profile kinds with non-zero number of profile sites.
Definition: InstrProf.h:749
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:774
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:871
static void write(bool isBE, void *P, T V)
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:478
raw_ostream & write(unsigned char C)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
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()
std::unique_ptr< ProfileSummary > getSummary()
support::endianness ValueProfDataEndianness
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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:593
Basic Alias true
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.
Definition: raw_ostream.h:361
uint32_t getNumFunctions()
Profiling information for a single function.
Definition: InstrProf.h:592
instrprof_error
Definition: InstrProf.h:280
void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type)
Error addFuncName(StringRef FuncName)
Update the symtab by adding FuncName to the table.
Definition: InstrProf.h:467
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
void patch(PatchItem *P, int NItems)
InstrProfSummaryBuilder * SummaryBuilder
uint64_t * D
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:462
void write(raw_fd_ostream &OS)
Write the profile to OS.
LLVM Value Representation.
Definition: Value.h:73
Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
ProfOStream(raw_fd_ostream &FD)
support::endian::Writer< support::little > LE
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::unique_ptr< InstrProfValueData[]> getValueForSite(uint32_t ValueKind, uint32_t Site, uint64_t *TotalC=nullptr) const
Return the array of profiled values at Site.
Definition: InstrProf.h:773
void set(SummaryFieldKind K, uint64_t V)
Definition: InstrProf.h:950
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.