1 //=-- 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 15 #include "llvm/IR/Constants.h" 16 #include "llvm/IR/Function.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/IR/GlobalVariable.h" 19 #include "llvm/ProfileData/InstrProf.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/ManagedStatic.h" 22 23 using namespace llvm; 24 25 namespace { 26 class InstrProfErrorCategoryType : public std::error_category { 27 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } 28 std::string message(int IE) const override { 29 instrprof_error E = static_cast<instrprof_error>(IE); 30 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: 36 return "Invalid profile data (bad magic)"; 37 case instrprof_error::bad_header: 38 return "Invalid profile data (file header is corrupt)"; 39 case instrprof_error::unsupported_version: 40 return "Unsupported profiling format version"; 41 case instrprof_error::unsupported_hash_type: 42 return "Unsupported profiling hash"; 43 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"; 51 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"; 57 case instrprof_error::value_site_count_mismatch: 58 return "Function's value site counts mismatch"; 59 } 60 llvm_unreachable("A value of instrprof_error has no message."); 61 } 62 }; 63 } 64 65 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; 66 67 const std::error_category &llvm::instrprof_category() { 68 return *ErrorCategory; 69 } 70 71 namespace llvm { 72 73 std::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 97 std::string getPGOFuncName(const Function &F) { 98 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName()); 99 } 100 101 GlobalVariable *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 128 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { 129 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); 130 } 131 132 namespace IndexedInstrProf { 133 134 uint32_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 142 uint32_t ValueProfRecord::getSize(uint32_t NumValueSites, 143 uint32_t NumValueData) { 144 return getHeaderSize(NumValueSites) + 145 sizeof(InstrProfValueData) * NumValueData; 146 } 147 148 void 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 160 void 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 185 template <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. 195 void 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 219 uint32_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 235 void 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 247 static std::unique_ptr<ValueProfData> AllocValueProfData(uint32_t TotalSize) { 248 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) 249 ValueProfData()); 250 } 251 252 std::unique_ptr<ValueProfData> 253 ValueProfData::serializeFrom(const InstrProfRecord &Record) { 254 uint32_t TotalSize = getSize(Record); 255 256 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize); 257 258 VPD->TotalSize = TotalSize; 259 VPD->NumValueKinds = Record.getNumValueKinds(); 260 ValueProfRecord *VR = VPD->getFirstValueProfRecord(); 261 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) { 262 uint32_t NumValueSites = Record.getNumValueSites(Kind); 263 if (!NumValueSites) 264 continue; 265 VR->serializeFrom(Record, Kind, NumValueSites); 266 VR = VR->getNext(); 267 } 268 return VPD; 269 } 270 271 ErrorOr<std::unique_ptr<ValueProfData>> 272 ValueProfData::getValueProfData(const unsigned char *D, 273 const unsigned char *const BufferEnd, 274 support::endianness Endianness) { 275 using namespace support; 276 if (D + sizeof(ValueProfData) > BufferEnd) 277 return instrprof_error::truncated; 278 279 uint32_t TotalSize = swapToHostOrder<uint32_t>( 280 reinterpret_cast<const uint32_t *>(D)[0], Endianness); 281 uint32_t NumValueKinds = swapToHostOrder<uint32_t>( 282 reinterpret_cast<const uint32_t *>(D)[1], Endianness); 283 284 if (D + TotalSize > BufferEnd) 285 return instrprof_error::too_large; 286 if (NumValueKinds > IPVK_Last + 1) 287 return instrprof_error::malformed; 288 // Total size needs to be mulltiple of quadword size. 289 if (TotalSize % sizeof(uint64_t)) 290 return instrprof_error::malformed; 291 292 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize); 293 294 memcpy(VPD.get(), D, TotalSize); 295 // Byte swap. 296 VPD->swapBytesToHost(Endianness); 297 298 // Data integrety check: 299 ValueProfRecord *VR = VPD->getFirstValueProfRecord(); 300 for (uint32_t K = 0; K < VPD->NumValueKinds; K++) { 301 if (VR->Kind > IPVK_Last) 302 return instrprof_error::malformed; 303 VR = VR->getNext(); 304 if ((char *)VR - (char *)VPD.get() > (ptrdiff_t)TotalSize) 305 return instrprof_error::malformed; 306 } 307 308 D += TotalSize; 309 return std::move(VPD); 310 } 311 312 void ValueProfData::swapBytesToHost(support::endianness Endianness) { 313 using namespace support; 314 if (Endianness == getHostEndianness()) 315 return; 316 317 sys::swapByteOrder<uint32_t>(TotalSize); 318 sys::swapByteOrder<uint32_t>(NumValueKinds); 319 320 ValueProfRecord *VR = getFirstValueProfRecord(); 321 for (uint32_t K = 0; K < NumValueKinds; K++) { 322 VR->swapBytes(Endianness, getHostEndianness()); 323 VR = VR->getNext(); 324 } 325 } 326 327 void ValueProfData::swapBytesFromHost(support::endianness Endianness) { 328 using namespace support; 329 if (Endianness == getHostEndianness()) 330 return; 331 332 ValueProfRecord *VR = getFirstValueProfRecord(); 333 for (uint32_t K = 0; K < NumValueKinds; K++) { 334 ValueProfRecord *NVR = VR->getNext(); 335 VR->swapBytes(getHostEndianness(), Endianness); 336 VR = NVR; 337 } 338 sys::swapByteOrder<uint32_t>(TotalSize); 339 sys::swapByteOrder<uint32_t>(NumValueKinds); 340 } 341 342 ValueProfRecord *ValueProfData::getFirstValueProfRecord() { 343 return reinterpret_cast<ValueProfRecord *>((char *)this + 344 sizeof(ValueProfData)); 345 } 346 347 uint32_t ValueProfRecord::getNumValueData() const { 348 uint32_t NumValueData = 0; 349 for (uint32_t I = 0; I < NumValueSites; I++) 350 NumValueData += SiteCountArray[I]; 351 return NumValueData; 352 } 353 354 ValueProfRecord *ValueProfRecord::getNext() { 355 return reinterpret_cast<ValueProfRecord *>((char *)this + getSize()); 356 } 357 358 InstrProfValueData *ValueProfRecord::getValueData() { 359 return reinterpret_cast<InstrProfValueData *>((char *)this + 360 getHeaderSize(NumValueSites)); 361 } 362 363 } // End of IndexedInstrProf namespace. 364 } 365