blob: 2608847fd103f0e4ae1d65238e92b6993e3ae108 [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,
Xinliang David Lic79283e2015-12-11 19:53:19 +000077 StringRef FileName, uint64_t Version) {
Xinliang David Li441959d2015-11-09 00:01:22 +000078
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);
Xinliang David Lic79283e2015-12-11 19:53:19 +000084 const char *Unknown = (Version <= 3 ? "<unknown>:" : "__unknown__");
85 const char *Sep = (Version <= 3 ? ":" : "__");
Xinliang David Li441959d2015-11-09 00:01:22 +000086
87 std::string FuncName = RawFuncName;
88 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
89 // For local symbols, prepend the main file name to distinguish them.
90 // Do not include the full path in the file name since there's no guarantee
91 // that it will stay the same, e.g., if the files are checked out from
92 // version control in different locations.
93 if (FileName.empty())
Xinliang David Lic79283e2015-12-11 19:53:19 +000094 FuncName = FuncName.insert(0, Unknown);
Xinliang David Li441959d2015-11-09 00:01:22 +000095 else
Xinliang David Lic79283e2015-12-11 19:53:19 +000096 FuncName = FuncName.insert(0, FileName.str() + Sep);
Xinliang David Li441959d2015-11-09 00:01:22 +000097 }
98 return FuncName;
99}
100
Xinliang David Li307902e2015-12-05 05:16:36 +0000101std::string getPGOFuncName(const Function &F, uint64_t Version) {
102 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(),
103 Version);
Xinliang David Li441959d2015-11-09 00:01:22 +0000104}
105
106GlobalVariable *createPGOFuncNameVar(Module &M,
107 GlobalValue::LinkageTypes Linkage,
108 StringRef FuncName) {
109
110 // We generally want to match the function's linkage, but available_externally
111 // and extern_weak both have the wrong semantics, and anything that doesn't
112 // need to link across compilation units doesn't need to be visible at all.
113 if (Linkage == GlobalValue::ExternalWeakLinkage)
114 Linkage = GlobalValue::LinkOnceAnyLinkage;
115 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
116 Linkage = GlobalValue::LinkOnceODRLinkage;
117 else if (Linkage == GlobalValue::InternalLinkage ||
118 Linkage == GlobalValue::ExternalLinkage)
119 Linkage = GlobalValue::PrivateLinkage;
120
121 auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
122 auto FuncNameVar =
123 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
124 Twine(getInstrProfNameVarPrefix()) + FuncName);
125
126 // Hide the symbol so that we correctly get a copy for each executable.
127 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
128 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
129
130 return FuncNameVar;
131}
132
133GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
134 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
135}
Xinliang David Liee415892015-11-10 00:24:45 +0000136
Xinliang David Lib75544a2015-11-28 19:07:09 +0000137#define INSTR_PROF_COMMON_API_IMPL
138#include "llvm/ProfileData/InstrProfData.inc"
Xinliang David Liee415892015-11-10 00:24:45 +0000139
Xinliang David Liee415892015-11-10 00:24:45 +0000140
Xinliang David Lib75544a2015-11-28 19:07:09 +0000141/*!
142 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
Xinliang David Lied966772015-11-25 23:31:18 +0000143 * class. These C wrappers are used as adaptors so that C++ code can be
144 * invoked as callbacks.
145 */
Xinliang David Lif47cf552015-11-25 06:23:38 +0000146uint32_t getNumValueKindsInstrProf(const void *Record) {
147 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
148}
149
150uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
151 return reinterpret_cast<const InstrProfRecord *>(Record)
152 ->getNumValueSites(VKind);
153}
154
155uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
156 return reinterpret_cast<const InstrProfRecord *>(Record)
157 ->getNumValueData(VKind);
158}
159
160uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
161 uint32_t S) {
162 return reinterpret_cast<const InstrProfRecord *>(R)
163 ->getNumValueDataForSite(VK, S);
164}
165
166void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
167 uint32_t K, uint32_t S,
168 uint64_t (*Mapper)(uint32_t, uint64_t)) {
169 return reinterpret_cast<const InstrProfRecord *>(R)
170 ->getValueForSite(Dst, K, S, Mapper);
171}
172
Xinliang David Lie8092312015-11-25 19:13:00 +0000173uint64_t stringToHash(uint32_t ValueKind, uint64_t Value) {
174 switch (ValueKind) {
175 case IPVK_IndirectCallTarget:
176 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType,
177 (const char *)Value);
178 break;
179 default:
180 llvm_unreachable("value kind not handled !");
181 }
182 return Value;
183}
184
Xinliang David Lif47cf552015-11-25 06:23:38 +0000185ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
186 return (ValueProfData *)(new (::operator new(TotalSizeInBytes))
187 ValueProfData());
188}
189
190static ValueProfRecordClosure InstrProfRecordClosure = {
191 0,
192 getNumValueKindsInstrProf,
193 getNumValueSitesInstrProf,
194 getNumValueDataInstrProf,
195 getNumValueDataForSiteInstrProf,
196 stringToHash,
197 getValueForSiteInstrProf,
Xinliang David Lie8092312015-11-25 19:13:00 +0000198 allocValueProfDataInstrProf
199};
Xinliang David Lif47cf552015-11-25 06:23:38 +0000200
Xinliang David Lie8092312015-11-25 19:13:00 +0000201// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000202uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
203 InstrProfRecordClosure.Record = &Record;
204 return getValueProfDataSize(&InstrProfRecordClosure);
205}
206
Xinliang David Lie8092312015-11-25 19:13:00 +0000207// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000208std::unique_ptr<ValueProfData>
209ValueProfData::serializeFrom(const InstrProfRecord &Record) {
210 InstrProfRecordClosure.Record = &Record;
211
212 std::unique_ptr<ValueProfData> VPD(
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000213 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
Xinliang David Lif47cf552015-11-25 06:23:38 +0000214 return VPD;
215}
216
Xinliang David Lie8092312015-11-25 19:13:00 +0000217void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
218 InstrProfRecord::ValueMapType *VMap) {
219 Record.reserveSites(Kind, NumValueSites);
220
221 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
222 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
223 uint8_t ValueDataCount = this->SiteCountArray[VSite];
224 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
225 ValueData += ValueDataCount;
226 }
227}
Xinliang David Lied966772015-11-25 23:31:18 +0000228
Xinliang David Lie8092312015-11-25 19:13:00 +0000229// For writing/serializing, Old is the host endianness, and New is
230// byte order intended on disk. For Reading/deserialization, Old
231// is the on-disk source endianness, and New is the host endianness.
232void ValueProfRecord::swapBytes(support::endianness Old,
233 support::endianness New) {
234 using namespace support;
235 if (Old == New)
236 return;
237
238 if (getHostEndianness() != Old) {
239 sys::swapByteOrder<uint32_t>(NumValueSites);
240 sys::swapByteOrder<uint32_t>(Kind);
241 }
242 uint32_t ND = getValueProfRecordNumValueData(this);
243 InstrProfValueData *VD = getValueProfRecordValueData(this);
244
245 // No need to swap byte array: SiteCountArrray.
246 for (uint32_t I = 0; I < ND; I++) {
247 sys::swapByteOrder<uint64_t>(VD[I].Value);
248 sys::swapByteOrder<uint64_t>(VD[I].Count);
249 }
250 if (getHostEndianness() == Old) {
251 sys::swapByteOrder<uint32_t>(NumValueSites);
252 sys::swapByteOrder<uint32_t>(Kind);
253 }
254}
255
256void ValueProfData::deserializeTo(InstrProfRecord &Record,
257 InstrProfRecord::ValueMapType *VMap) {
258 if (NumValueKinds == 0)
259 return;
260
261 ValueProfRecord *VR = getFirstValueProfRecord(this);
262 for (uint32_t K = 0; K < NumValueKinds; K++) {
263 VR->deserializeTo(Record, VMap);
264 VR = getValueProfRecordNext(VR);
265 }
266}
267
268template <class T>
269static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
270 using namespace support;
271 if (Orig == little)
272 return endian::readNext<T, little, unaligned>(D);
273 else
274 return endian::readNext<T, big, unaligned>(D);
275}
276
277static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
278 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
279 ValueProfData());
280}
281
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000282instrprof_error ValueProfData::checkIntegrity() {
283 if (NumValueKinds > IPVK_Last + 1)
284 return instrprof_error::malformed;
285 // Total size needs to be mulltiple of quadword size.
286 if (TotalSize % sizeof(uint64_t))
287 return instrprof_error::malformed;
288
289 ValueProfRecord *VR = getFirstValueProfRecord(this);
290 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
291 if (VR->Kind > IPVK_Last)
292 return instrprof_error::malformed;
293 VR = getValueProfRecordNext(VR);
294 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
295 return instrprof_error::malformed;
296 }
297 return instrprof_error::success;
298}
299
Xinliang David Liee415892015-11-10 00:24:45 +0000300ErrorOr<std::unique_ptr<ValueProfData>>
301ValueProfData::getValueProfData(const unsigned char *D,
302 const unsigned char *const BufferEnd,
303 support::endianness Endianness) {
304 using namespace support;
305 if (D + sizeof(ValueProfData) > BufferEnd)
306 return instrprof_error::truncated;
307
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000308 const unsigned char *Header = D;
309 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000310 if (D + TotalSize > BufferEnd)
311 return instrprof_error::too_large;
Xinliang David Liee415892015-11-10 00:24:45 +0000312
Xinliang David Lif47cf552015-11-25 06:23:38 +0000313 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000314 memcpy(VPD.get(), D, TotalSize);
315 // Byte swap.
316 VPD->swapBytesToHost(Endianness);
317
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000318 instrprof_error EC = VPD->checkIntegrity();
319 if (EC != instrprof_error::success)
320 return EC;
Xinliang David Liee415892015-11-10 00:24:45 +0000321
Xinliang David Liee415892015-11-10 00:24:45 +0000322 return std::move(VPD);
323}
324
325void ValueProfData::swapBytesToHost(support::endianness Endianness) {
326 using namespace support;
327 if (Endianness == getHostEndianness())
328 return;
329
330 sys::swapByteOrder<uint32_t>(TotalSize);
331 sys::swapByteOrder<uint32_t>(NumValueKinds);
332
Xinliang David Lif47cf552015-11-25 06:23:38 +0000333 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000334 for (uint32_t K = 0; K < NumValueKinds; K++) {
335 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000336 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000337 }
338}
339
340void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
341 using namespace support;
342 if (Endianness == getHostEndianness())
343 return;
344
Xinliang David Lif47cf552015-11-25 06:23:38 +0000345 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000346 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000347 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000348 VR->swapBytes(getHostEndianness(), Endianness);
349 VR = NVR;
350 }
351 sys::swapByteOrder<uint32_t>(TotalSize);
352 sys::swapByteOrder<uint32_t>(NumValueKinds);
353}
Xinliang David Lie8092312015-11-25 19:13:00 +0000354
Xinliang David Liee415892015-11-10 00:24:45 +0000355}