LLVM  6.0.0svn
InstrProfWriter.h
Go to the documentation of this file.
1 //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
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 instrumentation
11 // based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
16 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
17 
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
24 #include <cstdint>
25 #include <memory>
26 
27 namespace llvm {
28 
29 /// Writer for instrumentation based profile data.
30 class InstrProfRecordWriterTrait;
31 class ProfOStream;
32 class raw_fd_ostream;
33 
35 public:
38 
39 private:
40  bool Sparse;
41  StringMap<ProfilingData> FunctionData;
42  ProfKind ProfileKind = PF_Unknown;
43  // Use raw pointer here for the incomplete type object.
45 
46 public:
47  InstrProfWriter(bool Sparse = false);
49 
50  /// Add function counts for the given function. If there are already counts
51  /// for this function and the hash and number of counts match, each counter is
52  /// summed. Optionally scale counts by \p Weight.
53  void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
54  function_ref<void(Error)> Warn);
56  addRecord(std::move(I), 1, Warn);
57  }
58 
59  /// Merge existing function counts from the given writer.
61  function_ref<void(Error)> Warn);
62 
63  /// Write the profile to \c OS
64  void write(raw_fd_ostream &OS);
65 
66  /// Write the profile in text format to \c OS
68 
69  /// Write \c Record in text format to \c OS
70  static void writeRecordInText(StringRef Name, uint64_t Hash,
71  const InstrProfRecord &Counters,
72  InstrProfSymtab &Symtab, raw_fd_ostream &OS);
73 
74  /// Write the profile, returning the raw data. For testing.
75  std::unique_ptr<MemoryBuffer> writeBuffer();
76 
77  /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
78  Error setIsIRLevelProfile(bool IsIRLevel) {
79  if (ProfileKind == PF_Unknown) {
80  ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE;
81  return Error::success();
82  }
83  return (IsIRLevel == (ProfileKind == PF_IRLevel))
84  ? Error::success()
85  : make_error<InstrProfError>(
87  }
88 
89  // Internal interface for testing purpose only.
91  void setOutputSparse(bool Sparse);
92 
93 private:
94  void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
95  uint64_t Weight, function_ref<void(Error)> Warn);
96  bool shouldEncodeData(const ProfilingData &PD);
97  void writeImpl(ProfOStream &OS);
98 };
99 
100 } // end namespace llvm
101 
102 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
void setValueProfDataEndianness(support::endianness Endianness)
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:411
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
InstrProfWriter(bool Sparse=false)
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, function_ref< void(Error)> Warn)
Add function counts for the given function.
void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
void setOutputSparse(bool Sparse)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:313
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:224
Error setIsIRLevelProfile(bool IsIRLevel)
Set the ProfileKind. Report error if mixing FE and IR level profiles.
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
Profiling information for a single function.
Definition: InstrProf.h:592
void addRecord(NamedInstrProfRecord &&I, function_ref< void(Error)> Warn)
#define I(x, y, z)
Definition: MD5.cpp:58
void write(raw_fd_ostream &OS)
Write the profile to OS.
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.