blob: 4f7328279e2396afdd47d2e9bfc7ee7da01649b1 [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";
35 case instrprof_error::bad_magic:
Justin Bogner367a9f22015-05-06 23:19:35 +000036 return "Invalid profile data (bad magic)";
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000037 case instrprof_error::bad_header:
Justin Bogner367a9f22015-05-06 23:19:35 +000038 return "Invalid profile data (file header is corrupt)";
Justin Bognerf8d79192014-03-21 17:24:48 +000039 case instrprof_error::unsupported_version:
Justin Bogner367a9f22015-05-06 23:19:35 +000040 return "Unsupported profiling format version";
Justin Bognerb7aa2632014-04-18 21:48:40 +000041 case instrprof_error::unsupported_hash_type:
Justin Bogner367a9f22015-05-06 23:19:35 +000042 return "Unsupported profiling hash";
Justin Bognerf8d79192014-03-21 17:24:48 +000043 case instrprof_error::too_large:
44 return "Too much profile data";
45 case instrprof_error::truncated:
46 return "Truncated profile data";
47 case instrprof_error::malformed:
48 return "Malformed profile data";
49 case instrprof_error::unknown_function:
50 return "No profile data available for function";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000051 case instrprof_error::hash_mismatch:
52 return "Function hash mismatch";
53 case instrprof_error::count_mismatch:
54 return "Function count mismatch";
55 case instrprof_error::counter_overflow:
56 return "Counter overflow";
Justin Bogner9e9a0572015-09-29 22:13:58 +000057 case instrprof_error::value_site_count_mismatch:
58 return "Function's value site counts mismatch";
Justin Bognerf8d79192014-03-21 17:24:48 +000059 }
60 llvm_unreachable("A value of instrprof_error has no message.");
61 }
Justin Bognerf8d79192014-03-21 17:24:48 +000062};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000063}
Justin Bognerf8d79192014-03-21 17:24:48 +000064
Chris Bieneman1efe8012014-09-19 23:19:24 +000065static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
66
Rafael Espindola25188c92014-06-12 01:45:43 +000067const std::error_category &llvm::instrprof_category() {
Chris Bieneman1efe8012014-09-19 23:19:24 +000068 return *ErrorCategory;
Justin Bognerf8d79192014-03-21 17:24:48 +000069}
Xinliang David Li441959d2015-11-09 00:01:22 +000070
71namespace llvm {
72
73std::string getPGOFuncName(StringRef RawFuncName,
74 GlobalValue::LinkageTypes Linkage,
75 StringRef FileName) {
76
77 // Function names may be prefixed with a binary '1' to indicate
78 // that the backend should not modify the symbols due to any platform
79 // naming convention. Do not include that '1' in the PGO profile name.
80 if (RawFuncName[0] == '\1')
81 RawFuncName = RawFuncName.substr(1);
82
83 std::string FuncName = RawFuncName;
84 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
85 // For local symbols, prepend the main file name to distinguish them.
86 // Do not include the full path in the file name since there's no guarantee
87 // that it will stay the same, e.g., if the files are checked out from
88 // version control in different locations.
89 if (FileName.empty())
90 FuncName = FuncName.insert(0, "<unknown>:");
91 else
92 FuncName = FuncName.insert(0, FileName.str() + ":");
93 }
94 return FuncName;
95}
96
97std::string getPGOFuncName(const Function &F) {
98 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName());
99}
100
101GlobalVariable *createPGOFuncNameVar(Module &M,
102 GlobalValue::LinkageTypes Linkage,
103 StringRef FuncName) {
104
105 // We generally want to match the function's linkage, but available_externally
106 // and extern_weak both have the wrong semantics, and anything that doesn't
107 // need to link across compilation units doesn't need to be visible at all.
108 if (Linkage == GlobalValue::ExternalWeakLinkage)
109 Linkage = GlobalValue::LinkOnceAnyLinkage;
110 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
111 Linkage = GlobalValue::LinkOnceODRLinkage;
112 else if (Linkage == GlobalValue::InternalLinkage ||
113 Linkage == GlobalValue::ExternalLinkage)
114 Linkage = GlobalValue::PrivateLinkage;
115
116 auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
117 auto FuncNameVar =
118 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
119 Twine(getInstrProfNameVarPrefix()) + FuncName);
120
121 // Hide the symbol so that we correctly get a copy for each executable.
122 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
123 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
124
125 return FuncNameVar;
126}
127
128GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
129 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
130}
Xinliang David Liee415892015-11-10 00:24:45 +0000131
132namespace IndexedInstrProf {
133
134uint32_t ValueProfRecord::getHeaderSize(uint32_t NumValueSites) {
135 uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
136 sizeof(uint8_t) * NumValueSites;
137 // Round the size to multiple of 8 bytes.
138 Size = (Size + 7) & ~7;
139 return Size;
140}
141
142uint32_t ValueProfRecord::getSize(uint32_t NumValueSites,
143 uint32_t NumValueData) {
144 return getHeaderSize(NumValueSites) +
145 sizeof(InstrProfValueData) * NumValueData;
146}
147
148void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
149 InstrProfRecord::ValueMapType *VMap) {
150 Record.reserveSites(Kind, NumValueSites);
151
152 InstrProfValueData *ValueData = this->getValueData();
153 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
154 uint8_t ValueDataCount = this->SiteCountArray[VSite];
155 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
156 ValueData += ValueDataCount;
157 }
158}
159
160void ValueProfRecord::serializeFrom(const InstrProfRecord &Record,
161 uint32_t ValueKind,
162 uint32_t NumValueSites) {
163 Kind = ValueKind;
164 this->NumValueSites = NumValueSites;
165 InstrProfValueData *DstVD = getValueData();
166 for (uint32_t S = 0; S < NumValueSites; S++) {
167 uint32_t ND = Record.getNumValueDataForSite(ValueKind, S);
168 SiteCountArray[S] = ND;
169 std::unique_ptr<InstrProfValueData[]> SrcVD =
170 Record.getValueForSite(ValueKind, S);
171 for (uint32_t I = 0; I < ND; I++) {
172 DstVD[I] = SrcVD[I];
173 switch (ValueKind) {
174 case IPVK_IndirectCallTarget:
175 DstVD[I].Value = ComputeHash(HashType, (const char *)DstVD[I].Value);
176 break;
177 default:
178 llvm_unreachable("value kind not handled !");
179 }
180 }
181 DstVD += ND;
182 }
183}
184
185template <class T> static T swapToHostOrder(T v, support::endianness Orig) {
186 if (Orig == getHostEndianness())
187 return v;
188 sys::swapByteOrder<T>(v);
189 return v;
190}
191
192// For writing/serializing, Old is the host endianness, and New is
193// byte order intended on disk. For Reading/deserialization, Old
194// is the on-disk source endianness, and New is the host endianness.
195void ValueProfRecord::swapBytes(support::endianness Old,
196 support::endianness New) {
197 using namespace support;
198 if (Old == New)
199 return;
200
201 if (getHostEndianness() != Old) {
202 sys::swapByteOrder<uint32_t>(NumValueSites);
203 sys::swapByteOrder<uint32_t>(Kind);
204 }
205 uint32_t ND = getNumValueData();
206 InstrProfValueData *VD = getValueData();
207
208 // No need to swap byte array: SiteCountArrray.
209 for (uint32_t I = 0; I < ND; I++) {
210 sys::swapByteOrder<uint64_t>(VD[I].Value);
211 sys::swapByteOrder<uint64_t>(VD[I].Count);
212 }
213 if (getHostEndianness() == Old) {
214 sys::swapByteOrder<uint32_t>(NumValueSites);
215 sys::swapByteOrder<uint32_t>(Kind);
216 }
217}
218
219uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
220 uint32_t TotalSize = sizeof(ValueProfData);
221 uint32_t NumValueKinds = Record.getNumValueKinds();
222 if (NumValueKinds == 0)
223 return TotalSize;
224
225 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
226 uint32_t NumValueSites = Record.getNumValueSites(Kind);
227 if (!NumValueSites)
228 continue;
229 TotalSize +=
230 ValueProfRecord::getSize(NumValueSites, Record.getNumValueData(Kind));
231 }
232 return TotalSize;
233}
234
235void ValueProfData::deserializeTo(InstrProfRecord &Record,
236 InstrProfRecord::ValueMapType *VMap) {
237 if (NumValueKinds == 0)
238 return;
239
240 ValueProfRecord *VR = getFirstValueProfRecord();
241 for (uint32_t K = 0; K < NumValueKinds; K++) {
242 VR->deserializeTo(Record, VMap);
243 VR = VR->getNext();
244 }
245}
246
247std::unique_ptr<ValueProfData>
248ValueProfData::serializeFrom(const InstrProfRecord &Record) {
249 uint32_t TotalSize = getSize(Record);
Xinliang David Li6021b752015-11-10 17:11:33 +0000250 void *RawMem = ::operator new(TotalSize);
251 ValueProfData *VPDMem = new (RawMem) ValueProfData();
252 std::unique_ptr<ValueProfData> VPD(VPDMem);
Xinliang David Liee415892015-11-10 00:24:45 +0000253
254 VPD->TotalSize = TotalSize;
255 VPD->NumValueKinds = Record.getNumValueKinds();
256 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
257 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
258 uint32_t NumValueSites = Record.getNumValueSites(Kind);
259 if (!NumValueSites)
260 continue;
261 VR->serializeFrom(Record, Kind, NumValueSites);
262 VR = VR->getNext();
263 }
264 return VPD;
265}
266
267ErrorOr<std::unique_ptr<ValueProfData>>
268ValueProfData::getValueProfData(const unsigned char *D,
269 const unsigned char *const BufferEnd,
270 support::endianness Endianness) {
271 using namespace support;
272 if (D + sizeof(ValueProfData) > BufferEnd)
273 return instrprof_error::truncated;
274
275 uint32_t TotalSize = swapToHostOrder<uint32_t>(
276 reinterpret_cast<const uint32_t *>(D)[0], Endianness);
277 uint32_t NumValueKinds = swapToHostOrder<uint32_t>(
278 reinterpret_cast<const uint32_t *>(D)[1], Endianness);
279
280 if (D + TotalSize > BufferEnd)
281 return instrprof_error::too_large;
282 if (NumValueKinds > IPVK_Last + 1)
283 return instrprof_error::malformed;
284 // Total size needs to be mulltiple of quadword size.
285 if (TotalSize % sizeof(uint64_t))
286 return instrprof_error::malformed;
287
288 std::unique_ptr<ValueProfData> VPD(
289 reinterpret_cast<ValueProfData *>(new char[TotalSize]));
290 memcpy(VPD.get(), D, TotalSize);
291 // Byte swap.
292 VPD->swapBytesToHost(Endianness);
293
294 // Data integrety check:
295 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
296 for (uint32_t K = 0; K < VPD->NumValueKinds; K++) {
297 if (VR->Kind > IPVK_Last)
298 return instrprof_error::malformed;
299 VR = VR->getNext();
Aaron Ballman470b5f12015-11-11 14:57:28 +0000300 if ((char *)VR - (char *)VPD.get() > (ptrdiff_t)TotalSize)
Xinliang David Liee415892015-11-10 00:24:45 +0000301 return instrprof_error::malformed;
302 }
303
304 D += TotalSize;
305 return std::move(VPD);
306}
307
308void ValueProfData::swapBytesToHost(support::endianness Endianness) {
309 using namespace support;
310 if (Endianness == getHostEndianness())
311 return;
312
313 sys::swapByteOrder<uint32_t>(TotalSize);
314 sys::swapByteOrder<uint32_t>(NumValueKinds);
315
316 ValueProfRecord *VR = getFirstValueProfRecord();
317 for (uint32_t K = 0; K < NumValueKinds; K++) {
318 VR->swapBytes(Endianness, getHostEndianness());
319 VR = VR->getNext();
320 }
321}
322
323void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
324 using namespace support;
325 if (Endianness == getHostEndianness())
326 return;
327
328 ValueProfRecord *VR = getFirstValueProfRecord();
329 for (uint32_t K = 0; K < NumValueKinds; K++) {
330 ValueProfRecord *NVR = VR->getNext();
331 VR->swapBytes(getHostEndianness(), Endianness);
332 VR = NVR;
333 }
334 sys::swapByteOrder<uint32_t>(TotalSize);
335 sys::swapByteOrder<uint32_t>(NumValueKinds);
336}
337
338ValueProfRecord *ValueProfData::getFirstValueProfRecord() {
339 return reinterpret_cast<ValueProfRecord *>((char *)this +
340 sizeof(ValueProfData));
341}
342
343uint32_t ValueProfRecord::getNumValueData() const {
344 uint32_t NumValueData = 0;
345 for (uint32_t I = 0; I < NumValueSites; I++)
346 NumValueData += SiteCountArray[I];
347 return NumValueData;
348}
349
350ValueProfRecord *ValueProfRecord::getNext() {
351 return reinterpret_cast<ValueProfRecord *>((char *)this + getSize());
352}
353
354InstrProfValueData *ValueProfRecord::getValueData() {
355 return reinterpret_cast<InstrProfValueData *>((char *)this +
356 getHeaderSize(NumValueSites));
357}
358
359} // End of IndexedInstrProf namespace.
Xinliang David Li441959d2015-11-09 00:01:22 +0000360}