LLVM 22.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"
21#include "llvm/Object/BuildID.h"
27#include "llvm/Support/Error.h"
28#include <cstdint>
29#include <memory>
30#include <random>
31
32namespace llvm {
33
34/// Writer for instrumentation based profile data.
35class InstrProfRecordWriterTrait;
36class ProfOStream;
37class MemoryBuffer;
38class raw_fd_ostream;
39
41public:
43
44private:
45 bool Sparse;
46 StringMap<ProfilingData> FunctionData;
47 /// The maximum length of a single temporal profile trace.
48 uint64_t MaxTemporalProfTraceLength;
49 /// The maximum number of stored temporal profile traces.
50 uint64_t TemporalProfTraceReservoirSize;
51 /// The total number of temporal profile traces seen.
52 uint64_t TemporalProfTraceStreamSize = 0;
53 /// The list of temporal profile traces.
54 SmallVector<TemporalProfTraceTy> TemporalProfTraces;
55 std::mt19937 RNG;
56
57 // The MemProf data.
59
60 // List of binary ids.
61 std::vector<llvm::object::BuildID> BinaryIds;
62
63 // Read the vtable names from raw instr profile reader.
64 StringSet<> VTableNames;
65
66 // An enum describing the attributes of the profile.
68 // Use raw pointer here for the incomplete type object.
70
71 // Temporary support for writing the previous version of the format, to enable
72 // some forward compatibility. Currently this suppresses the writing of the
73 // new vtable names section and header fields.
74 // TODO: Consider enabling this with future version changes as well, to ease
75 // deployment of newer versions of llvm-profdata.
76 bool WritePrevVersion = false;
77
78 // The MemProf version we should write.
79 memprof::IndexedVersion MemProfVersionRequested;
80
81 // Whether to serialize the full schema.
82 bool MemProfFullSchema;
83
84 // Whether to generated random memprof hotness for testing.
85 bool MemprofGenerateRandomHotness;
86
87 std::unique_ptr<memprof::DataAccessProfData> DataAccessProfileData;
88
89 // MemProf summary builder to which records are added as MemProf data is added
90 // to the writer.
91 memprof::MemProfSummaryBuilder MemProfSumBuilder;
92
93public:
94 // For memprof testing, random hotness can be assigned to the contexts if
95 // MemprofGenerateRandomHotness is enabled. The random seed can be either
96 // provided by MemprofGenerateRandomHotnessSeed, or if that is 0, one will be
97 // generated in the writer using the current time.
98 LLVM_ABI InstrProfWriter(bool Sparse = false,
99 uint64_t TemporalProfTraceReservoirSize = 0,
100 uint64_t MaxTemporalProfTraceLength = 0,
101 bool WritePrevVersion = false,
102 memprof::IndexedVersion MemProfVersionRequested =
103 static_cast<memprof::IndexedVersion>(
105 bool MemProfFullSchema = false,
106 bool MemprofGenerateRandomHotness = false,
107 unsigned MemprofGenerateRandomHotnessSeed = 0);
109
110 StringMap<ProfilingData> &getProfileData() { return FunctionData; }
111
112 /// Add function counts for the given function. If there are already counts
113 /// for this function and the hash and number of counts match, each counter is
114 /// summed. Optionally scale counts by \p Weight.
116 function_ref<void(Error)> Warn);
118 addRecord(std::move(I), 1, Warn);
119 }
120 void addVTableName(StringRef VTableName) { VTableNames.insert(VTableName); }
121
122 /// Add \p SrcTraces using reservoir sampling where \p SrcStreamSize is the
123 /// total number of temporal profiling traces the source has seen.
124 LLVM_ABI void
126 uint64_t SrcStreamSize);
127
128 /// Add the entire MemProfData \p Incoming to the writer context.
130 function_ref<void(Error)> Warn);
131
132 // Add a binary id to the binary ids list.
134
136 std::unique_ptr<memprof::DataAccessProfData> DataAccessProfile);
137
138 /// Merge existing function counts from the given writer.
140 function_ref<void(Error)> Warn);
141
142 /// Write the profile to \c OS
144
145 /// Write the profile to a string output stream \c OS
147
148 /// Write the profile in text format to \c OS
150
151 /// Write temporal profile trace data to the header in text format to \c OS
153 InstrProfSymtab &Symtab);
154
156
157 /// Write \c Record in text format to \c OS
160 InstrProfSymtab &Symtab,
162
163 /// Write the profile, returning the raw data. For testing.
164 LLVM_ABI std::unique_ptr<MemoryBuffer> writeBuffer();
165
166 /// Update the attributes of the current profile from the attributes
167 /// specified. An error is returned if IR and FE profiles are mixed.
169 // If the kind is unset, this is the first profile we are merging so just
170 // set it to the given type.
171 if (ProfileKind == InstrProfKind::Unknown) {
172 ProfileKind = Other;
173 return Error::success();
174 }
175
176 // Returns true if merging is should fail assuming A and B are incompatible.
177 auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
178 return (static_cast<bool>(ProfileKind & A) &&
179 static_cast<bool>(Other & B)) ||
180 (static_cast<bool>(ProfileKind & B) &&
181 static_cast<bool>(Other & A));
182 };
183
184 // Check if the profiles are in-compatible. Clang frontend profiles can't be
185 // merged with other profile types.
186 if (static_cast<bool>(
189 return make_error<InstrProfError>(instrprof_error::unsupported_version);
190 }
191 if (testIncompatible(InstrProfKind::FunctionEntryOnly,
193 testIncompatible(InstrProfKind::FunctionEntryOnly,
195 return make_error<InstrProfError>(
197 "cannot merge FunctionEntryOnly profiles and BB profiles together");
198 }
199
200 // Now we update the profile type with the bits that are set.
201 ProfileKind |= Other;
202 return Error::success();
203 }
204
205 InstrProfKind getProfileKind() const { return ProfileKind; }
206
208 return static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage);
209 }
210
211 // Internal interfaces for testing purpose only.
213 LLVM_ABI void setOutputSparse(bool Sparse);
215 MemProfVersionRequested = Version;
216 }
217 void setMemProfFullSchema(bool Full) { MemProfFullSchema = Full; }
218 // Compute the overlap b/w this object and Other. Program level result is
219 // stored in Overlap and function level result is stored in FuncLevelOverlap.
221 OverlapStats &Overlap,
222 OverlapStats &FuncLevelOverlap,
223 const OverlapFuncFilters &FuncFilter);
224
225private:
227 uint64_t Weight, function_ref<void(Error)> Warn);
228 bool shouldEncodeData(const ProfilingData &PD);
229
230 /// Add a memprof record for a function identified by its \p Id.
231 void addMemProfRecord(const GlobalValue::GUID Id,
233
234 /// Add a memprof frame identified by the hash of the contents of the frame in
235 /// \p FrameId.
236 bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
237 function_ref<void(Error)> Warn);
238
239 /// Add a call stack identified by the hash of the contents of the call stack
240 /// in \p CallStack.
241 bool addMemProfCallStack(const memprof::CallStackId CSId,
243 function_ref<void(Error)> Warn);
244
245 Error writeImpl(ProfOStream &OS);
246
247 // Writes known header fields and reserves space for fields whose value are
248 // known only after payloads are written. Returns the start byte offset for
249 // back patching.
250 uint64_t writeHeader(const IndexedInstrProf::Header &header,
251 const bool WritePrevVersion, ProfOStream &OS);
252
253 // Writes binary IDs.
254 Error writeBinaryIds(ProfOStream &OS);
255
256 // Writes compressed vtable names to profiles.
257 Error writeVTableNames(ProfOStream &OS);
258};
259
260} // end namespace llvm
261
262#endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
This file defines the StringMap class.
This file declares a library for handling Build IDs and using them to find debug info.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
std::string Name
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
Definition: InstrProf.h:506
bool hasSingleByteCoverage() const
void addVTableName(StringRef VTableName)
LLVM_ABI Error write(raw_fd_ostream &OS)
Write the profile to OS.
LLVM_ABI void addTemporalProfileTraces(SmallVectorImpl< TemporalProfTraceTy > &SrcTraces, uint64_t SrcStreamSize)
Add SrcTraces using reservoir sampling where SrcStreamSize is the total number of temporal profiling ...
LLVM_ABI void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, const OverlapFuncFilters &FuncFilter)
LLVM_ABI Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
InstrProfKind getProfileKind() const
void addRecord(NamedInstrProfRecord &&I, function_ref< void(Error)> Warn)
LLVM_ABI void addBinaryIds(ArrayRef< llvm::object::BuildID > BIs)
void setMemProfVersionRequested(memprof::IndexedVersion Version)
static LLVM_ABI void writeRecordInText(StringRef Name, uint64_t Hash, const InstrProfRecord &Counters, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.
LLVM_ABI void setValueProfDataEndianness(llvm::endianness Endianness)
LLVM_ABI void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, function_ref< void(Error)> Warn)
Add function counts for the given function.
LLVM_ABI void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
Error mergeProfileKind(const InstrProfKind Other)
Update the attributes of the current profile from the attributes specified.
LLVM_ABI void writeTextTemporalProfTraceData(raw_fd_ostream &OS, InstrProfSymtab &Symtab)
Write temporal profile trace data to the header in text format to OS.
SmallDenseMap< uint64_t, InstrProfRecord > ProfilingData
LLVM_ABI std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.
LLVM_ABI void setOutputSparse(bool Sparse)
StringMap< ProfilingData > & getProfileData()
LLVM_ABI bool addMemProfData(memprof::IndexedMemProfData Incoming, function_ref< void(Error)> Warn)
Add the entire MemProfData Incoming to the writer context.
LLVM_ABI void addDataAccessProfData(std::unique_ptr< memprof::DataAccessProfData > DataAccessProfile)
void setMemProfFullSchema(bool Full)
LLVM_ABI Error validateRecord(const InstrProfRecord &Func)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:25
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:39
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:461
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
constexpr uint64_t MinimumSupportedVersion
Definition: MemProf.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
endianness
Definition: bit.h:71
InstrProfKind
An enum describing the attributes of an instrumented profile.
Definition: InstrProf.h:374
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
Profiling information for a single function.
Definition: InstrProf.h:895