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"
22#include "llvm/ProfileData/InstrProf.h"
23#include "llvm/ProfileData/MemProf.h"
24#include "llvm/Support/Error.h"
25#include <cstdint>
26#include <memory>
27#include <random>
28
29namespace llvm {
30
31/// Writer for instrumentation based profile data.
32class InstrProfRecordWriterTrait;
33class ProfOStream;
34class MemoryBuffer;
35class raw_fd_ostream;
36
37class InstrProfWriter {
38public:
39 using ProfilingData = SmallDenseMap<uint64_t, InstrProfRecord>;
40
41private:
42 bool Sparse;
43 StringMap<ProfilingData> FunctionData;
44 /// The maximum length of a single temporal profile trace.
45 uint64_t MaxTemporalProfTraceLength;
46 /// The maximum number of stored temporal profile traces.
47 uint64_t TemporalProfTraceReservoirSize;
48 /// The total number of temporal profile traces seen.
49 uint64_t TemporalProfTraceStreamSize = 0;
50 /// The list of temporal profile traces.
51 SmallVector<TemporalProfTraceTy> TemporalProfTraces;
52 std::mt19937 RNG;
53
54 // A map to hold memprof data per function. The lower 64 bits obtained from
55 // the md5 hash of the function name is used to index into the map.
56 llvm::MapVector<GlobalValue::GUID, memprof::IndexedMemProfRecord>
57 MemProfRecordData;
58 // A map to hold frame id to frame mappings. The mappings are used to
59 // convert IndexedMemProfRecord to MemProfRecords with frame information
60 // inline.
61 llvm::MapVector<memprof::FrameId, memprof::Frame> MemProfFrameData;
62
63 // A map to hold call stack id to call stacks.
64 llvm::MapVector<memprof::CallStackId, llvm::SmallVector<memprof::FrameId>>
65 MemProfCallStackData;
66
67 // List of binary ids.
68 std::vector<llvm::object::BuildID> BinaryIds;
69
70 // Read the vtable names from raw instr profile reader.
71 StringSet<> VTableNames;
72
73 // An enum describing the attributes of the profile.
74 InstrProfKind ProfileKind = InstrProfKind::Unknown;
75 // Use raw pointer here for the incomplete type object.
76 InstrProfRecordWriterTrait *InfoObj;
77
78 // Temporary support for writing the previous version of the format, to enable
79 // some forward compatibility. Currently this suppresses the writing of the
80 // new vtable names section and header fields.
81 // TODO: Consider enabling this with future version changes as well, to ease
82 // deployment of newer versions of llvm-profdata.
83 bool WritePrevVersion = false;
84
85 // The MemProf version we should write.
86 memprof::IndexedVersion MemProfVersionRequested;
87
88public:
89 InstrProfWriter(
90 bool Sparse = false, uint64_t TemporalProfTraceReservoirSize = 0,
91 uint64_t MaxTemporalProfTraceLength = 0, bool WritePrevVersion = false,
92 memprof::IndexedVersion MemProfVersionRequested = memprof::Version0);
93 ~InstrProfWriter();
94
95 StringMap<ProfilingData> &getProfileData() { return FunctionData; }
96
97 /// Add function counts for the given function. If there are already counts
98 /// for this function and the hash and number of counts match, each counter is
99 /// summed. Optionally scale counts by \p Weight.
100 void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
101 function_ref<void(Error)> Warn);
102 void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
103 addRecord(I: std::move(I), Weight: 1, Warn);
104 }
105 void addVTableName(StringRef VTableName) { VTableNames.insert(key: VTableName); }
106
107 /// Add \p SrcTraces using reservoir sampling where \p SrcStreamSize is the
108 /// total number of temporal profiling traces the source has seen.
109 void addTemporalProfileTraces(SmallVectorImpl<TemporalProfTraceTy> &SrcTraces,
110 uint64_t SrcStreamSize);
111
112 /// Add a memprof record for a function identified by its \p Id.
113 void addMemProfRecord(const GlobalValue::GUID Id,
114 const memprof::IndexedMemProfRecord &Record);
115
116 /// Add a memprof frame identified by the hash of the contents of the frame in
117 /// \p FrameId.
118 bool addMemProfFrame(const memprof::FrameId, const memprof::Frame &F,
119 function_ref<void(Error)> Warn);
120
121 /// Add a call stack identified by the hash of the contents of the call stack
122 /// in \p CallStack.
123 bool addMemProfCallStack(const memprof::CallStackId CSId,
124 const llvm::SmallVector<memprof::FrameId> &CallStack,
125 function_ref<void(Error)> Warn);
126
127 // Add a binary id to the binary ids list.
128 void addBinaryIds(ArrayRef<llvm::object::BuildID> BIs);
129
130 /// Merge existing function counts from the given writer.
131 void mergeRecordsFromWriter(InstrProfWriter &&IPW,
132 function_ref<void(Error)> Warn);
133
134 /// Write the profile to \c OS
135 Error write(raw_fd_ostream &OS);
136
137 /// Write the profile to a string output stream \c OS
138 Error write(raw_string_ostream &OS);
139
140 /// Write the profile in text format to \c OS
141 Error writeText(raw_fd_ostream &OS);
142
143 /// Write temporal profile trace data to the header in text format to \c OS
144 void writeTextTemporalProfTraceData(raw_fd_ostream &OS,
145 InstrProfSymtab &Symtab);
146
147 Error validateRecord(const InstrProfRecord &Func);
148
149 /// Write \c Record in text format to \c OS
150 static void writeRecordInText(StringRef Name, uint64_t Hash,
151 const InstrProfRecord &Counters,
152 InstrProfSymtab &Symtab, raw_fd_ostream &OS);
153
154 /// Write the profile, returning the raw data. For testing.
155 std::unique_ptr<MemoryBuffer> writeBuffer();
156
157 /// Update the attributes of the current profile from the attributes
158 /// specified. An error is returned if IR and FE profiles are mixed.
159 Error mergeProfileKind(const InstrProfKind Other) {
160 // If the kind is unset, this is the first profile we are merging so just
161 // set it to the given type.
162 if (ProfileKind == InstrProfKind::Unknown) {
163 ProfileKind = Other;
164 return Error::success();
165 }
166
167 // Returns true if merging is should fail assuming A and B are incompatible.
168 auto testIncompatible = [&](InstrProfKind A, InstrProfKind B) {
169 return (static_cast<bool>(ProfileKind & A) &&
170 static_cast<bool>(Other & B)) ||
171 (static_cast<bool>(ProfileKind & B) &&
172 static_cast<bool>(Other & A));
173 };
174
175 // Check if the profiles are in-compatible. Clang frontend profiles can't be
176 // merged with other profile types.
177 if (static_cast<bool>(
178 (ProfileKind & InstrProfKind::FrontendInstrumentation) ^
179 (Other & InstrProfKind::FrontendInstrumentation))) {
180 return make_error<InstrProfError>(Args: instrprof_error::unsupported_version);
181 }
182 if (testIncompatible(InstrProfKind::FunctionEntryOnly,
183 InstrProfKind::FunctionEntryInstrumentation)) {
184 return make_error<InstrProfError>(
185 Args: instrprof_error::unsupported_version,
186 Args: "cannot merge FunctionEntryOnly profiles and BB profiles together");
187 }
188
189 // Now we update the profile type with the bits that are set.
190 ProfileKind |= Other;
191 return Error::success();
192 }
193
194 InstrProfKind getProfileKind() const { return ProfileKind; }
195
196 bool hasSingleByteCoverage() const {
197 return static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage);
198 }
199
200 // Internal interfaces for testing purpose only.
201 void setValueProfDataEndianness(llvm::endianness Endianness);
202 void setOutputSparse(bool Sparse);
203 void setMemProfVersionRequested(memprof::IndexedVersion Version) {
204 MemProfVersionRequested = Version;
205 }
206 // Compute the overlap b/w this object and Other. Program level result is
207 // stored in Overlap and function level result is stored in FuncLevelOverlap.
208 void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap,
209 OverlapStats &FuncLevelOverlap,
210 const OverlapFuncFilters &FuncFilter);
211
212private:
213 void addRecord(StringRef Name, uint64_t Hash, InstrProfRecord &&I,
214 uint64_t Weight, function_ref<void(Error)> Warn);
215 bool shouldEncodeData(const ProfilingData &PD);
216 /// Add \p Trace using reservoir sampling.
217 void addTemporalProfileTrace(TemporalProfTraceTy Trace);
218
219 Error writeImpl(ProfOStream &OS);
220};
221
222} // end namespace llvm
223
224#endif // LLVM_PROFILEDATA_INSTRPROFWRITER_H
225

source code of llvm/include/llvm/ProfileData/InstrProfWriter.h