LLVM  8.0.1
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:
52  : IsFDOStream(true), OS(FD), LE(FD, support::little) {}
54  : IsFDOStream(false), OS(STR), LE(STR, support::little) {}
55 
56  uint64_t tell() { return OS.tell(); }
57  void write(uint64_t V) { LE.write<uint64_t>(V); }
58 
59  // \c patch can only be called when all data is written and flushed.
60  // For raw_string_ostream, the patch is done on the target string
61  // directly and it won't be reflected in the stream's internal buffer.
62  void patch(PatchItem *P, int NItems) {
63  using namespace support;
64 
65  if (IsFDOStream) {
66  raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
67  for (int K = 0; K < NItems; K++) {
68  FDOStream.seek(P[K].Pos);
69  for (int I = 0; I < P[K].N; I++)
70  write(P[K].D[I]);
71  }
72  } else {
73  raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
74  std::string &Data = SOStream.str(); // with flush
75  for (int K = 0; K < NItems; K++) {
76  for (int I = 0; I < P[K].N; I++) {
77  uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
78  Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
79  (const char *)&Bytes, sizeof(uint64_t));
80  }
81  }
82  }
83  }
84 
85  // If \c OS is an instance of \c raw_fd_ostream, this field will be
86  // true. Otherwise, \c OS will be an raw_string_ostream.
90 };
91 
93 public:
96 
99 
100  using hash_value_type = uint64_t;
101  using offset_type = uint64_t;
102 
103  support::endianness ValueProfDataEndianness = support::little;
105 
106  InstrProfRecordWriterTrait() = default;
107 
110  }
111 
112  static std::pair<offset_type, offset_type>
114  using namespace support;
115 
116  endian::Writer LE(Out, little);
117 
118  offset_type N = K.size();
119  LE.write<offset_type>(N);
120 
121  offset_type M = 0;
122  for (const auto &ProfileData : *V) {
123  const InstrProfRecord &ProfRecord = ProfileData.second;
124  M += sizeof(uint64_t); // The function hash
125  M += sizeof(uint64_t); // The size of the Counts vector
126  M += ProfRecord.Counts.size() * sizeof(uint64_t);
127 
128  // Value data
129  M += ValueProfData::getSize(ProfileData.second);
130  }
131  LE.write<offset_type>(M);
132 
133  return std::make_pair(N, M);
134  }
135 
137  Out.write(K.data(), N);
138  }
139 
141  using namespace support;
142 
143  endian::Writer LE(Out, little);
144  for (const auto &ProfileData : *V) {
145  const InstrProfRecord &ProfRecord = ProfileData.second;
146  SummaryBuilder->addRecord(ProfRecord);
147 
148  LE.write<uint64_t>(ProfileData.first); // Function hash
149  LE.write<uint64_t>(ProfRecord.Counts.size());
150  for (uint64_t I : ProfRecord.Counts)
151  LE.write<uint64_t>(I);
152 
153  // Write value data
154  std::unique_ptr<ValueProfData> VDataPtr =
155  ValueProfData::serializeFrom(ProfileData.second);
156  uint32_t S = VDataPtr->getSize();
157  VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
158  Out.write((const char *)VDataPtr.get(), S);
159  }
160  }
161 };
162 
163 } // end namespace llvm
164 
166  : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
167 
169 
170 // Internal interface for testing purpose only.
174 }
175 
177  this->Sparse = Sparse;
178 }
179 
181  function_ref<void(Error)> Warn) {
182  auto Name = I.Name;
183  auto Hash = I.Hash;
184  addRecord(Name, Hash, std::move(I), Weight, Warn);
185 }
186 
187 void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
188  InstrProfRecord &&I, uint64_t Weight,
189  function_ref<void(Error)> Warn) {
190  auto &ProfileDataMap = FunctionData[Name];
191 
192  bool NewFunc;
194  std::tie(Where, NewFunc) =
195  ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
196  InstrProfRecord &Dest = Where->second;
197 
198  auto MapWarn = [&](instrprof_error E) {
199  Warn(make_error<InstrProfError>(E));
200  };
201 
202  if (NewFunc) {
203  // We've never seen a function with this name and hash, add it.
204  Dest = std::move(I);
205  if (Weight > 1)
206  Dest.scale(Weight, MapWarn);
207  } else {
208  // We're updating a function we've seen before.
209  Dest.merge(I, Weight, MapWarn);
210  }
211 
212  Dest.sortValueData();
213 }
214 
216  function_ref<void(Error)> Warn) {
217  for (auto &I : IPW.FunctionData)
218  for (auto &Func : I.getValue())
219  addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
220 }
221 
222 bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
223  if (!Sparse)
224  return true;
225  for (const auto &Func : PD) {
226  const InstrProfRecord &IPR = Func.second;
227  if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
228  return true;
229  }
230  return false;
231 }
232 
233 static void setSummary(IndexedInstrProf::Summary *TheSummary,
234  ProfileSummary &PS) {
235  using namespace IndexedInstrProf;
236 
237  std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
238  TheSummary->NumSummaryFields = Summary::NumKinds;
239  TheSummary->NumCutoffEntries = Res.size();
240  TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
241  TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
242  TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
243  TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
244  TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
245  TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
246  for (unsigned I = 0; I < Res.size(); I++)
247  TheSummary->setEntry(I, Res[I]);
248 }
249 
250 void InstrProfWriter::writeImpl(ProfOStream &OS) {
251  using namespace IndexedInstrProf;
252 
254 
256  InfoObj->SummaryBuilder = &ISB;
257 
258  // Populate the hash table generator.
259  for (const auto &I : FunctionData)
260  if (shouldEncodeData(I.getValue()))
261  Generator.insert(I.getKey(), &I.getValue());
262  // Write the header.
266  if (ProfileKind == PF_IRLevel)
267  Header.Version |= VARIANT_MASK_IR_PROF;
268  Header.Unused = 0;
269  Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
270  Header.HashOffset = 0;
271  int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
272 
273  // Only write out all the fields except 'HashOffset'. We need
274  // to remember the offset of that field to allow back patching
275  // later.
276  for (int I = 0; I < N - 1; I++)
277  OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
278 
279  // Save the location of Header.HashOffset field in \c OS.
280  uint64_t HashTableStartFieldOffset = OS.tell();
281  // Reserve the space for HashOffset field.
282  OS.write(0);
283 
284  // Reserve space to write profile summary data.
286  uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
287  // Remember the summary offset.
288  uint64_t SummaryOffset = OS.tell();
289  for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
290  OS.write(0);
291 
292  // Write the hash table.
293  uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
294 
295  // Allocate space for data to be serialized out.
296  std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
297  IndexedInstrProf::allocSummary(SummarySize);
298  // Compute the Summary and copy the data to the data
299  // structure to be serialized out (to disk or buffer).
300  std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
301  setSummary(TheSummary.get(), *PS);
302  InfoObj->SummaryBuilder = nullptr;
303 
304  // Now do the final patch:
305  PatchItem PatchItems[] = {
306  // Patch the Header.HashOffset field.
307  {HashTableStartFieldOffset, &HashTableStart, 1},
308  // Patch the summary data.
309  {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
310  (int)(SummarySize / sizeof(uint64_t))}};
311  OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
312 }
313 
315  // Write the hash table.
316  ProfOStream POS(OS);
317  writeImpl(POS);
318 }
319 
320 std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
321  std::string Data;
322  raw_string_ostream OS(Data);
323  ProfOStream POS(OS);
324  // Write the hash table.
325  writeImpl(POS);
326  // Return this in an aligned memory buffer.
327  return MemoryBuffer::getMemBufferCopy(Data);
328 }
329 
330 static const char *ValueProfKindStr[] = {
331 #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
333 };
334 
336  const InstrProfRecord &Func,
337  InstrProfSymtab &Symtab,
338  raw_fd_ostream &OS) {
339  OS << Name << "\n";
340  OS << "# Func Hash:\n" << Hash << "\n";
341  OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
342  OS << "# Counter Values:\n";
343  for (uint64_t Count : Func.Counts)
344  OS << Count << "\n";
345 
346  uint32_t NumValueKinds = Func.getNumValueKinds();
347  if (!NumValueKinds) {
348  OS << "\n";
349  return;
350  }
351 
352  OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
353  for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
354  uint32_t NS = Func.getNumValueSites(VK);
355  if (!NS)
356  continue;
357  OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
358  OS << "# NumValueSites:\n" << NS << "\n";
359  for (uint32_t S = 0; S < NS; S++) {
360  uint32_t ND = Func.getNumValueDataForSite(VK, S);
361  OS << ND << "\n";
362  std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
363  for (uint32_t I = 0; I < ND; I++) {
364  if (VK == IPVK_IndirectCallTarget)
365  OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
366  << VD[I].Count << "\n";
367  else
368  OS << VD[I].Value << ":" << VD[I].Count << "\n";
369  }
370  }
371  }
372 
373  OS << "\n";
374 }
375 
377  if (ProfileKind == PF_IRLevel)
378  OS << "# IR level Instrumentation Flag\n:ir\n";
379  InstrProfSymtab Symtab;
380  for (const auto &I : FunctionData)
381  if (shouldEncodeData(I.getValue()))
382  if (Error E = Symtab.addFuncName(I.getKey()))
383  return E;
384 
385  for (const auto &I : FunctionData)
386  if (shouldEncodeData(I.getValue()))
387  for (const auto &Func : I.getValue())
388  writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS);
389  return Error::success();
390 }
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:537
void sortValueData()
Sort value profile data (per site) by count.
Definition: InstrProf.h:692
This class represents lattice values for constants.
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:795
void setEntry(uint32_t I, const ProfileSummaryEntry &E)
Definition: InstrProf.h:984
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:117
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
Definition: InstrProf.h:992
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:25
block Block Frequency true
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:791
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:871
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.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const HashT HashType
Definition: InstrProf.h:897
void scale(uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Scale up profile counts (including value profile data) by Weight.
Definition: InstrProf.cpp:565
static std::pair< offset_type, offset_type > EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V)
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:777
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:1193
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:899
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:499
raw_ostream & write(unsigned char C)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
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
support::endian::Writer LE
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:623
static void writeRecordInText(StringRef Name, uint64_t Hash, const InstrProfRecord &Counters, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:366
uint32_t getNumFunctions()
Profiling information for a single function.
Definition: InstrProf.h:622
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:472
#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:483
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:158
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
ProfOStream(raw_fd_ostream &FD)
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:801
StringRef getFuncNameOrExternalSymbol(uint64_t FuncMD5Hash)
Just like getFuncName, except that it will return a non-empty StringRef if the function is external t...
Definition: InstrProf.h:555
void set(SummaryFieldKind K, uint64_t V)
Definition: InstrProf.h:978
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.