blob: 530be8ac044a90c7df24d90c16feb9a5e9dd38bb [file] [log] [blame]
Justin Bognerf8d79192014-03-21 17:24:48 +00001//=-- InstrProf.cpp - Instrumented profiling format support -----------------=//
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 clang's instrumentation based PGO and
11// coverage.
12//
13//===----------------------------------------------------------------------===//
14
Xinliang David Li441959d2015-11-09 00:01:22 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/GlobalVariable.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000019#include "llvm/ProfileData/InstrProf.h"
20#include "llvm/Support/ErrorHandling.h"
Chris Bieneman1efe8012014-09-19 23:19:24 +000021#include "llvm/Support/ManagedStatic.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000022
23using namespace llvm;
24
25namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +000026class InstrProfErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +000027 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
Justin Bognerf8d79192014-03-21 17:24:48 +000028 std::string message(int IE) const override {
Rafael Espindola92512e82014-06-03 05:12:33 +000029 instrprof_error E = static_cast<instrprof_error>(IE);
Justin Bognerf8d79192014-03-21 17:24:48 +000030 switch (E) {
31 case instrprof_error::success:
32 return "Success";
33 case instrprof_error::eof:
34 return "End of File";
Nathan Slingerland4f823662015-11-13 03:47:58 +000035 case instrprof_error::unrecognized_format:
36 return "Unrecognized instrumentation profile encoding format";
Justin Bognerf8d79192014-03-21 17:24:48 +000037 case instrprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000038 return "Invalid instrumentation profile data (bad magic)";
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000039 case instrprof_error::bad_header:
Nathan Slingerland4f823662015-11-13 03:47:58 +000040 return "Invalid instrumentation profile data (file header is corrupt)";
Justin Bognerf8d79192014-03-21 17:24:48 +000041 case instrprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000042 return "Unsupported instrumentation profile format version";
Justin Bognerb7aa2632014-04-18 21:48:40 +000043 case instrprof_error::unsupported_hash_type:
Nathan Slingerland4f823662015-11-13 03:47:58 +000044 return "Unsupported instrumentation profile hash type";
Justin Bognerf8d79192014-03-21 17:24:48 +000045 case instrprof_error::too_large:
46 return "Too much profile data";
47 case instrprof_error::truncated:
48 return "Truncated profile data";
49 case instrprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Malformed instrumentation profile data";
Justin Bognerf8d79192014-03-21 17:24:48 +000051 case instrprof_error::unknown_function:
52 return "No profile data available for function";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000053 case instrprof_error::hash_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000054 return "Function control flow change detected (hash mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000055 case instrprof_error::count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000056 return "Function basic block count change detected (counter mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000057 case instrprof_error::counter_overflow:
58 return "Counter overflow";
Justin Bogner9e9a0572015-09-29 22:13:58 +000059 case instrprof_error::value_site_count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000060 return "Function value site count change detected (counter mismatch)";
Justin Bognerf8d79192014-03-21 17:24:48 +000061 }
62 llvm_unreachable("A value of instrprof_error has no message.");
63 }
Justin Bognerf8d79192014-03-21 17:24:48 +000064};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000065}
Justin Bognerf8d79192014-03-21 17:24:48 +000066
Chris Bieneman1efe8012014-09-19 23:19:24 +000067static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
68
Rafael Espindola25188c92014-06-12 01:45:43 +000069const std::error_category &llvm::instrprof_category() {
Chris Bieneman1efe8012014-09-19 23:19:24 +000070 return *ErrorCategory;
Justin Bognerf8d79192014-03-21 17:24:48 +000071}
Xinliang David Li441959d2015-11-09 00:01:22 +000072
73namespace llvm {
74
75std::string getPGOFuncName(StringRef RawFuncName,
76 GlobalValue::LinkageTypes Linkage,
77 StringRef FileName) {
78
79 // Function names may be prefixed with a binary '1' to indicate
80 // that the backend should not modify the symbols due to any platform
81 // naming convention. Do not include that '1' in the PGO profile name.
82 if (RawFuncName[0] == '\1')
83 RawFuncName = RawFuncName.substr(1);
84
85 std::string FuncName = RawFuncName;
86 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
87 // For local symbols, prepend the main file name to distinguish them.
88 // Do not include the full path in the file name since there's no guarantee
89 // that it will stay the same, e.g., if the files are checked out from
90 // version control in different locations.
91 if (FileName.empty())
92 FuncName = FuncName.insert(0, "<unknown>:");
93 else
94 FuncName = FuncName.insert(0, FileName.str() + ":");
95 }
96 return FuncName;
97}
98
99std::string getPGOFuncName(const Function &F) {
100 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName());
101}
102
103GlobalVariable *createPGOFuncNameVar(Module &M,
104 GlobalValue::LinkageTypes Linkage,
105 StringRef FuncName) {
106
107 // We generally want to match the function's linkage, but available_externally
108 // and extern_weak both have the wrong semantics, and anything that doesn't
109 // need to link across compilation units doesn't need to be visible at all.
110 if (Linkage == GlobalValue::ExternalWeakLinkage)
111 Linkage = GlobalValue::LinkOnceAnyLinkage;
112 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
113 Linkage = GlobalValue::LinkOnceODRLinkage;
114 else if (Linkage == GlobalValue::InternalLinkage ||
115 Linkage == GlobalValue::ExternalLinkage)
116 Linkage = GlobalValue::PrivateLinkage;
117
118 auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
119 auto FuncNameVar =
120 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
121 Twine(getInstrProfNameVarPrefix()) + FuncName);
122
123 // Hide the symbol so that we correctly get a copy for each executable.
124 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
125 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
126
127 return FuncNameVar;
128}
129
130GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
131 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
132}
Xinliang David Liee415892015-11-10 00:24:45 +0000133
Xinliang David Lib75544a2015-11-28 19:07:09 +0000134#define INSTR_PROF_COMMON_API_IMPL
135#include "llvm/ProfileData/InstrProfData.inc"
Xinliang David Liee415892015-11-10 00:24:45 +0000136
Xinliang David Liee415892015-11-10 00:24:45 +0000137
Xinliang David Lib75544a2015-11-28 19:07:09 +0000138/*!
139 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
Xinliang David Lied966772015-11-25 23:31:18 +0000140 * class. These C wrappers are used as adaptors so that C++ code can be
141 * invoked as callbacks.
142 */
Xinliang David Lif47cf552015-11-25 06:23:38 +0000143uint32_t getNumValueKindsInstrProf(const void *Record) {
144 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
145}
146
147uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
148 return reinterpret_cast<const InstrProfRecord *>(Record)
149 ->getNumValueSites(VKind);
150}
151
152uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
153 return reinterpret_cast<const InstrProfRecord *>(Record)
154 ->getNumValueData(VKind);
155}
156
157uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
158 uint32_t S) {
159 return reinterpret_cast<const InstrProfRecord *>(R)
160 ->getNumValueDataForSite(VK, S);
161}
162
163void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
164 uint32_t K, uint32_t S,
165 uint64_t (*Mapper)(uint32_t, uint64_t)) {
166 return reinterpret_cast<const InstrProfRecord *>(R)
167 ->getValueForSite(Dst, K, S, Mapper);
168}
169
Xinliang David Lie8092312015-11-25 19:13:00 +0000170uint64_t stringToHash(uint32_t ValueKind, uint64_t Value) {
171 switch (ValueKind) {
172 case IPVK_IndirectCallTarget:
173 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType,
174 (const char *)Value);
175 break;
176 default:
177 llvm_unreachable("value kind not handled !");
178 }
179 return Value;
180}
181
Xinliang David Lif47cf552015-11-25 06:23:38 +0000182ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
183 return (ValueProfData *)(new (::operator new(TotalSizeInBytes))
184 ValueProfData());
185}
186
187static ValueProfRecordClosure InstrProfRecordClosure = {
188 0,
189 getNumValueKindsInstrProf,
190 getNumValueSitesInstrProf,
191 getNumValueDataInstrProf,
192 getNumValueDataForSiteInstrProf,
193 stringToHash,
194 getValueForSiteInstrProf,
Xinliang David Lie8092312015-11-25 19:13:00 +0000195 allocValueProfDataInstrProf
196};
Xinliang David Lif47cf552015-11-25 06:23:38 +0000197
Xinliang David Lie8092312015-11-25 19:13:00 +0000198// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000199uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
200 InstrProfRecordClosure.Record = &Record;
201 return getValueProfDataSize(&InstrProfRecordClosure);
202}
203
Xinliang David Lie8092312015-11-25 19:13:00 +0000204// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000205std::unique_ptr<ValueProfData>
206ValueProfData::serializeFrom(const InstrProfRecord &Record) {
207 InstrProfRecordClosure.Record = &Record;
208
209 std::unique_ptr<ValueProfData> VPD(
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000210 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
Xinliang David Lif47cf552015-11-25 06:23:38 +0000211 return VPD;
212}
213
Xinliang David Lie8092312015-11-25 19:13:00 +0000214void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
215 InstrProfRecord::ValueMapType *VMap) {
216 Record.reserveSites(Kind, NumValueSites);
217
218 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
219 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
220 uint8_t ValueDataCount = this->SiteCountArray[VSite];
221 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
222 ValueData += ValueDataCount;
223 }
224}
Xinliang David Lied966772015-11-25 23:31:18 +0000225
Xinliang David Lie8092312015-11-25 19:13:00 +0000226// For writing/serializing, Old is the host endianness, and New is
227// byte order intended on disk. For Reading/deserialization, Old
228// is the on-disk source endianness, and New is the host endianness.
229void ValueProfRecord::swapBytes(support::endianness Old,
230 support::endianness New) {
231 using namespace support;
232 if (Old == New)
233 return;
234
235 if (getHostEndianness() != Old) {
236 sys::swapByteOrder<uint32_t>(NumValueSites);
237 sys::swapByteOrder<uint32_t>(Kind);
238 }
239 uint32_t ND = getValueProfRecordNumValueData(this);
240 InstrProfValueData *VD = getValueProfRecordValueData(this);
241
242 // No need to swap byte array: SiteCountArrray.
243 for (uint32_t I = 0; I < ND; I++) {
244 sys::swapByteOrder<uint64_t>(VD[I].Value);
245 sys::swapByteOrder<uint64_t>(VD[I].Count);
246 }
247 if (getHostEndianness() == Old) {
248 sys::swapByteOrder<uint32_t>(NumValueSites);
249 sys::swapByteOrder<uint32_t>(Kind);
250 }
251}
252
253void ValueProfData::deserializeTo(InstrProfRecord &Record,
254 InstrProfRecord::ValueMapType *VMap) {
255 if (NumValueKinds == 0)
256 return;
257
258 ValueProfRecord *VR = getFirstValueProfRecord(this);
259 for (uint32_t K = 0; K < NumValueKinds; K++) {
260 VR->deserializeTo(Record, VMap);
261 VR = getValueProfRecordNext(VR);
262 }
263}
264
265template <class T>
266static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
267 using namespace support;
268 if (Orig == little)
269 return endian::readNext<T, little, unaligned>(D);
270 else
271 return endian::readNext<T, big, unaligned>(D);
272}
273
274static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
275 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
276 ValueProfData());
277}
278
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000279instrprof_error ValueProfData::checkIntegrity() {
280 if (NumValueKinds > IPVK_Last + 1)
281 return instrprof_error::malformed;
282 // Total size needs to be mulltiple of quadword size.
283 if (TotalSize % sizeof(uint64_t))
284 return instrprof_error::malformed;
285
286 ValueProfRecord *VR = getFirstValueProfRecord(this);
287 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
288 if (VR->Kind > IPVK_Last)
289 return instrprof_error::malformed;
290 VR = getValueProfRecordNext(VR);
291 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
292 return instrprof_error::malformed;
293 }
294 return instrprof_error::success;
295}
296
Xinliang David Liee415892015-11-10 00:24:45 +0000297ErrorOr<std::unique_ptr<ValueProfData>>
298ValueProfData::getValueProfData(const unsigned char *D,
299 const unsigned char *const BufferEnd,
300 support::endianness Endianness) {
301 using namespace support;
302 if (D + sizeof(ValueProfData) > BufferEnd)
303 return instrprof_error::truncated;
304
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000305 const unsigned char *Header = D;
306 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000307 if (D + TotalSize > BufferEnd)
308 return instrprof_error::too_large;
Xinliang David Liee415892015-11-10 00:24:45 +0000309
Xinliang David Lif47cf552015-11-25 06:23:38 +0000310 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000311 memcpy(VPD.get(), D, TotalSize);
312 // Byte swap.
313 VPD->swapBytesToHost(Endianness);
314
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000315 instrprof_error EC = VPD->checkIntegrity();
316 if (EC != instrprof_error::success)
317 return EC;
Xinliang David Liee415892015-11-10 00:24:45 +0000318
Xinliang David Liee415892015-11-10 00:24:45 +0000319 return std::move(VPD);
320}
321
322void ValueProfData::swapBytesToHost(support::endianness Endianness) {
323 using namespace support;
324 if (Endianness == getHostEndianness())
325 return;
326
327 sys::swapByteOrder<uint32_t>(TotalSize);
328 sys::swapByteOrder<uint32_t>(NumValueKinds);
329
Xinliang David Lif47cf552015-11-25 06:23:38 +0000330 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000331 for (uint32_t K = 0; K < NumValueKinds; K++) {
332 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000333 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000334 }
335}
336
337void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
338 using namespace support;
339 if (Endianness == getHostEndianness())
340 return;
341
Xinliang David Lif47cf552015-11-25 06:23:38 +0000342 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000343 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000344 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000345 VR->swapBytes(getHostEndianness(), Endianness);
346 VR = NVR;
347 }
348 sys::swapByteOrder<uint32_t>(TotalSize);
349 sys::swapByteOrder<uint32_t>(NumValueKinds);
350}
Xinliang David Lie8092312015-11-25 19:13:00 +0000351
Xinliang David Liee415892015-11-10 00:24:45 +0000352}