LLVM  14.0.0git
InstrProfWriter.h
Go to the documentation of this file.
1 //===- InstrProfWriter.h - Instrumented profiling writer --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing profiling data for instrumentation
10 // based PGO and coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_PROFILEDATA_INSTRPROFWRITER_H
15 #define LLVM_PROFILEDATA_INSTRPROFWRITER_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
23 #include <cstdint>
24 #include <memory>
25 
26 namespace llvm {
27 
28 /// Writer for instrumentation based profile data.
29 class InstrProfRecordWriterTrait;
30 class ProfOStream;
31 class raw_fd_ostream;
32 
34 public:
36  // PF_IRLevelWithCS is the profile from context sensitive IR instrumentation.
38 
39 private:
40  bool Sparse;
41  StringMap<ProfilingData> FunctionData;
42  ProfKind ProfileKind = PF_Unknown;
43  bool InstrEntryBBEnabled;
44  // Use raw pointer here for the incomplete type object.
46 
47 public:
48  InstrProfWriter(bool Sparse = false, bool InstrEntryBBEnabled = false);
50 
51  StringMap<ProfilingData> &getProfileData() { return FunctionData; }
52 
53  /// Add function counts for the given function. If there are already counts
54  /// for this function and the hash and number of counts match, each counter is
55  /// summed. Optionally scale counts by \p Weight.
57  function_ref<void(Error)> Warn);
59  addRecord(std::move(I), 1, Warn);
60  }
61 
62  /// Merge existing function counts from the given writer.
64  function_ref<void(Error)> Warn);
65 
66  /// Write the profile to \c OS
68 
69  /// Write the profile in text format to \c OS
71 
73 
74  /// Write \c Record in text format to \c OS
75  static void writeRecordInText(StringRef Name, uint64_t Hash,
76  const InstrProfRecord &Counters,
77  InstrProfSymtab &Symtab, raw_fd_ostream &OS);
78 
79  /// Write the profile, returning the raw data. For testing.
80  std::unique_ptr<MemoryBuffer> writeBuffer();
81 
82  /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
83  /// \c WithCS indicates if this is for contenxt sensitive instrumentation.
84  Error setIsIRLevelProfile(bool IsIRLevel, bool WithCS) {
85  if (ProfileKind == PF_Unknown) {
86  if (IsIRLevel)
87  ProfileKind = WithCS ? PF_IRLevelWithCS : PF_IRLevel;
88  else
89  ProfileKind = PF_FE;
90  return Error::success();
91  }
92 
93  if (((ProfileKind != PF_FE) && !IsIRLevel) ||
94  ((ProfileKind == PF_FE) && IsIRLevel))
95  return make_error<InstrProfError>(instrprof_error::unsupported_version);
96 
97  // When merging a context-sensitive profile (WithCS == true) with an IRLevel
98  // profile, set the kind to PF_IRLevelWithCS.
99  if (ProfileKind == PF_IRLevel && WithCS)
100  ProfileKind = PF_IRLevelWithCS;
101 
102  return Error::success();
103  }
104 
105  void setInstrEntryBBEnabled(bool Enabled) { InstrEntryBBEnabled = Enabled; }
106  // Internal interface for testing purpose only.
108  void setOutputSparse(bool Sparse);
109  // Compute the overlap b/w this object and Other. Program level result is
110  // stored in Overlap and function level result is stored in FuncLevelOverlap.
111  void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
112  OverlapStats &FuncLevelOverlap,
113  const OverlapFuncFilters &FuncFilter);
114 
115 private:
117  uint64_t Weight, function_ref<void(Error)> Warn);
118  bool shouldEncodeData(const ProfilingData &PD);
119 
120  Error writeImpl(ProfOStream &OS);
121 };
122 
123 } // end namespace llvm
124 
125 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
MemoryBuffer.h
llvm::InstrProfWriter::writeText
Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
Definition: InstrProfWriter.cpp:472
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::msgpack::Endianness
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:24
llvm::InstrProfWriter::write
Error write(raw_fd_ostream &OS)
Write the profile to OS.
Definition: InstrProfWriter.cpp:388
llvm::InstrProfWriter::PF_FE
@ PF_FE
Definition: InstrProfWriter.h:37
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::SmallDenseMap
Definition: DenseMap.h:880
Error.h
llvm::X86II::PD
@ PD
Definition: X86BaseInfo.h:782
llvm::OverlapStats
Definition: InstrProf.h:611
DenseMap.h
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:691
llvm::InstrProfRecordWriterTrait
Definition: InstrProfWriter.cpp:91
llvm::OverlapFuncFilters
Definition: InstrProf.h:654
llvm::InstrProfWriter::InstrProfWriter
InstrProfWriter(bool Sparse=false, bool InstrEntryBBEnabled=false)
Definition: InstrProfWriter.cpp:168
llvm::InstrProfWriter::getProfileData
StringMap< ProfilingData > & getProfileData()
Definition: InstrProfWriter.h:51
llvm::InstrProfWriter::writeRecordInText
static void writeRecordInText(StringRef Name, uint64_t Hash, const InstrProfRecord &Counters, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.
Definition: InstrProfWriter.cpp:431
llvm::InstrProfWriter::PF_IRLevelWithCS
@ PF_IRLevelWithCS
Definition: InstrProfWriter.h:37
llvm::InstrProfWriter::setIsIRLevelProfile
Error setIsIRLevelProfile(bool IsIRLevel, bool WithCS)
Set the ProfileKind.
Definition: InstrProfWriter.h:84
InstrProf.h
StringMap.h
llvm::instrprof_error::unsupported_version
@ unsupported_version
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:108
llvm::InstrProfWriter::PF_IRLevel
@ PF_IRLevel
Definition: InstrProfWriter.h:37
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::InstrProfWriter
Definition: InstrProfWriter.h:33
uint64_t
llvm::InstrProfWriter::setValueProfDataEndianness
void setValueProfDataEndianness(support::endianness Endianness)
Definition: InstrProfWriter.cpp:175
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::InstrProfWriter::ProfKind
ProfKind
Definition: InstrProfWriter.h:37
llvm::InstrProfWriter::addRecord
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, function_ref< void(Error)> Warn)
Add function counts for the given function.
Definition: InstrProfWriter.cpp:184
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:443
llvm::InstrProfWriter::writeBuffer
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.
Definition: InstrProfWriter.cpp:394
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:414
llvm::InstrProfWriter::setInstrEntryBBEnabled
void setInstrEntryBBEnabled(bool Enabled)
Definition: InstrProfWriter.h:105
llvm::InstrProfWriter::~InstrProfWriter
~InstrProfWriter()
Definition: InstrProfWriter.cpp:172
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Enabled
static bool Enabled
Definition: Statistic.cpp:46
llvm::InstrProfWriter::validateRecord
Error validateRecord(const InstrProfRecord &Func)
Definition: InstrProfWriter.cpp:410
llvm::InstrProfWriter::addRecord
void addRecord(NamedInstrProfRecord &&I, function_ref< void(Error)> Warn)
Definition: InstrProfWriter.h:58
llvm::InstrProfWriter::ProfilingData
SmallDenseMap< uint64_t, InstrProfRecord > ProfilingData
Definition: InstrProfWriter.h:35
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::NamedInstrProfRecord
Definition: InstrProf.h:848
llvm::InstrProfWriter::PF_Unknown
@ PF_Unknown
Definition: InstrProfWriter.h:37
llvm::ProfOStream
Definition: InstrProfWriter.cpp:48
llvm::InstrProfWriter::setOutputSparse
void setOutputSparse(bool Sparse)
Definition: InstrProfWriter.cpp:180
Endian.h
llvm::InstrProfWriter::mergeRecordsFromWriter
void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
Definition: InstrProfWriter.cpp:253
llvm::InstrProfWriter::overlapRecord
void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, const OverlapFuncFilters &FuncFilter)
Definition: InstrProfWriter.cpp:191