LLVM  15.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/MapVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include <cstdint>
26 #include <memory>
27 
28 namespace llvm {
29 
30 /// Writer for instrumentation based profile data.
31 class InstrProfRecordWriterTrait;
32 class ProfOStream;
33 class MemoryBuffer;
34 class raw_fd_ostream;
35 
37 public:
39 
40 private:
41  bool Sparse;
42  StringMap<ProfilingData> FunctionData;
43 
44  // A map to hold memprof data per function. The lower 64 bits obtained from
45  // the md5 hash of the function name is used to index into the map.
47  MemProfRecordData;
48  // A map to hold frame id to frame mappings. The mappings are used to
49  // convert IndexedMemProfRecord to MemProfRecords with frame information
50  // inline.
52 
53  // An enum describing the attributes of the profile.
55  // Use raw pointer here for the incomplete type object.
57 
58 public:
59  InstrProfWriter(bool Sparse = false);
61 
62  StringMap<ProfilingData> &getProfileData() { return FunctionData; }
63 
64  /// Add function counts for the given function. If there are already counts
65  /// for this function and the hash and number of counts match, each counter is
66  /// summed. Optionally scale counts by \p Weight.
68  function_ref<void(Error)> Warn);
70  addRecord(std::move(I), 1, Warn);
71  }
72 
73  /// Add a memprof record for a function identified by its \p Id.
76 
77  /// Add a memprof frame identified by the hash of the contents of the frame in
78  /// \p FrameId.
80  function_ref<void(Error)> Warn);
81 
82  /// Merge existing function counts from the given writer.
84  function_ref<void(Error)> Warn);
85 
86  /// Write the profile to \c OS
88 
89  /// Write the profile in text format to \c OS
91 
93 
94  /// Write \c Record in text format to \c OS
95  static void writeRecordInText(StringRef Name, uint64_t Hash,
96  const InstrProfRecord &Counters,
97  InstrProfSymtab &Symtab, raw_fd_ostream &OS);
98 
99  /// Write the profile, returning the raw data. For testing.
100  std::unique_ptr<MemoryBuffer> writeBuffer();
101 
102  /// Update the attributes of the current profile from the attributes
103  /// specified. An error is returned if IR and FE profiles are mixed.
105  // If the kind is unset, this is the first profile we are merging so just
106  // set it to the given type.
107  if (ProfileKind == InstrProfKind::Unknown) {
108  ProfileKind = Other;
109  return Error::success();
110  }
111 
112  // Returns true if merging is should fail assuming A and B are incompatible.
113  auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
114  return (static_cast<bool>(ProfileKind & A) &&
115  static_cast<bool>(Other & B)) ||
116  (static_cast<bool>(ProfileKind & B) &&
117  static_cast<bool>(Other & A));
118  };
119 
120  // Check if the profiles are in-compatible. Clang frontend profiles can't be
121  // merged with other profile types.
122  if (static_cast<bool>(
125  return make_error<InstrProfError>(instrprof_error::unsupported_version);
126  }
127  if (testIncompatible(InstrProfKind::FunctionEntryOnly,
129  return make_error<InstrProfError>(
131  "cannot merge FunctionEntryOnly profiles and BB profiles together");
132  }
133 
134  // Now we update the profile type with the bits that are set.
135  ProfileKind |= Other;
136  return Error::success();
137  }
138 
139  InstrProfKind getProfileKind() const { return ProfileKind; }
140 
141  // Internal interface for testing purpose only.
143  void setOutputSparse(bool Sparse);
144  // Compute the overlap b/w this object and Other. Program level result is
145  // stored in Overlap and function level result is stored in FuncLevelOverlap.
146  void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
147  OverlapStats &FuncLevelOverlap,
148  const OverlapFuncFilters &FuncFilter);
149 
150 private:
152  uint64_t Weight, function_ref<void(Error)> Warn);
153  bool shouldEncodeData(const ProfilingData &PD);
154 
155  Error writeImpl(ProfOStream &OS);
156 };
157 
158 } // end namespace llvm
159 
160 #endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
llvm::GlobalValue::GUID
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
Definition: GlobalValue.h:508
llvm::InstrProfWriter::writeText
Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
Definition: InstrProfWriter.cpp:587
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::msgpack::Endianness
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
Definition: MsgPack.h:24
llvm::memprof::IndexedMemProfRecord
Definition: MemProf.h:315
llvm::InstrProfKind::FunctionEntryInstrumentation
@ FunctionEntryInstrumentation
llvm::InstrProfKind::FunctionEntryOnly
@ FunctionEntryOnly
llvm::InstrProfWriter::write
Error write(raw_fd_ostream &OS)
Write the profile to OS.
Definition: InstrProfWriter.cpp:503
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
MapVector.h
llvm::SmallDenseMap
Definition: DenseMap.h:882
Error.h
llvm::OverlapStats
Definition: InstrProf.h:645
DenseMap.h
llvm::InstrProfWriter::mergeProfileKind
Error mergeProfileKind(const InstrProfKind Other)
Update the attributes of the current profile from the attributes specified.
Definition: InstrProfWriter.h:104
llvm::MapVector
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:37
llvm::X86II::PD
@ PD
Definition: X86BaseInfo.h:787
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:725
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::InstrProfRecordWriterTrait
Definition: InstrProfWriter.cpp:96
llvm::OverlapFuncFilters
Definition: InstrProf.h:688
GlobalValue.h
llvm::InstrProfWriter::getProfileData
StringMap< ProfilingData > & getProfileData()
Definition: InstrProfWriter.h:62
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:546
llvm::InstrProfWriter::getProfileKind
InstrProfKind getProfileKind() const
Definition: InstrProfWriter.h:139
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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:110
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::memprof::Frame
Definition: MemProf.h:141
llvm::InstrProfWriter
Definition: InstrProfWriter.h:36
uint64_t
llvm::InstrProfWriter::setValueProfDataEndianness
void setValueProfDataEndianness(support::endianness Endianness)
Definition: InstrProfWriter.cpp:179
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
llvm::InstrProfWriter::addMemProfFrame
bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F, function_ref< void(Error)> Warn)
Add a memprof frame identified by the hash of the contents of the frame in FrameId.
Definition: InstrProfWriter.cpp:267
llvm::InstrProfWriter::addMemProfRecord
void addMemProfRecord(const GlobalValue::GUID Id, const memprof::IndexedMemProfRecord &Record)
Add a memprof record for a function identified by its Id.
Definition: InstrProfWriter.cpp:256
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::InstrProfKind::FrontendInstrumentation
@ FrontendInstrumentation
llvm::InstrProfWriter::InstrProfWriter
InstrProfWriter(bool Sparse=false)
Definition: InstrProfWriter.cpp:173
llvm::Record
Definition: Record.h:1508
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:188
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:444
MemProf.h
llvm::InstrProfWriter::writeBuffer
std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.
Definition: InstrProfWriter.cpp:509
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::InstrProfSymtab
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Definition: InstrProf.h:441
llvm::InstrProfWriter::~InstrProfWriter
~InstrProfWriter()
Definition: InstrProfWriter.cpp:176
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::InstrProfWriter::validateRecord
Error validateRecord(const InstrProfRecord &Func)
Definition: InstrProfWriter.cpp:525
llvm::InstrProfWriter::addRecord
void addRecord(NamedInstrProfRecord &&I, function_ref< void(Error)> Warn)
Definition: InstrProfWriter.h:69
llvm::InstrProfKind::Unknown
@ Unknown
llvm::InstrProfWriter::ProfilingData
SmallDenseMap< uint64_t, InstrProfRecord > ProfilingData
Definition: InstrProfWriter.h:38
llvm::InstrProfKind
InstrProfKind
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:282
llvm::support::endianness
endianness
Definition: Endian.h:27
llvm::NamedInstrProfRecord
Definition: InstrProf.h:882
llvm::ProfOStream
Definition: InstrProfWriter.cpp:48
llvm::InstrProfWriter::setOutputSparse
void setOutputSparse(bool Sparse)
Definition: InstrProfWriter.cpp:184
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:235
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:283
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1225
llvm::InstrProfWriter::overlapRecord
void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, const OverlapFuncFilters &FuncFilter)
Definition: InstrProfWriter.cpp:195