Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 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 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/InstrProf.h" |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/StringExtras.h" |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Function.h" |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 19 | #include "llvm/IR/GlobalVariable.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
| 21 | #include "llvm/Support/Compression.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 23 | #include "llvm/Support/LEB128.h" |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 29 | class InstrProfErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 30 | const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 31 | std::string message(int IE) const override { |
Rafael Espindola | 92512e8 | 2014-06-03 05:12:33 +0000 | [diff] [blame] | 32 | instrprof_error E = static_cast<instrprof_error>(IE); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 33 | switch (E) { |
| 34 | case instrprof_error::success: |
| 35 | return "Success"; |
| 36 | case instrprof_error::eof: |
| 37 | return "End of File"; |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 38 | case instrprof_error::unrecognized_format: |
| 39 | return "Unrecognized instrumentation profile encoding format"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 40 | case instrprof_error::bad_magic: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 41 | return "Invalid instrumentation profile data (bad magic)"; |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 42 | case instrprof_error::bad_header: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 43 | return "Invalid instrumentation profile data (file header is corrupt)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 44 | case instrprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 45 | return "Unsupported instrumentation profile format version"; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 46 | case instrprof_error::unsupported_hash_type: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 47 | return "Unsupported instrumentation profile hash type"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 48 | case instrprof_error::too_large: |
| 49 | return "Too much profile data"; |
| 50 | case instrprof_error::truncated: |
| 51 | return "Truncated profile data"; |
| 52 | case instrprof_error::malformed: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 53 | return "Malformed instrumentation profile data"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 54 | case instrprof_error::unknown_function: |
| 55 | return "No profile data available for function"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 56 | case instrprof_error::hash_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 57 | return "Function control flow change detected (hash mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 58 | case instrprof_error::count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 59 | return "Function basic block count change detected (counter mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 60 | case instrprof_error::counter_overflow: |
| 61 | return "Counter overflow"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 62 | case instrprof_error::value_site_count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 63 | return "Function value site count change detected (counter mismatch)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 64 | } |
| 65 | llvm_unreachable("A value of instrprof_error has no message."); |
| 66 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 67 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 68 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 69 | |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 70 | static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; |
| 71 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 72 | const std::error_category &llvm::instrprof_category() { |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 73 | return *ErrorCategory; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 74 | } |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 75 | |
| 76 | namespace llvm { |
| 77 | |
| 78 | std::string getPGOFuncName(StringRef RawFuncName, |
| 79 | GlobalValue::LinkageTypes Linkage, |
Xinliang David Li | a86545b | 2015-12-11 20:23:22 +0000 | [diff] [blame] | 80 | StringRef FileName, |
| 81 | uint64_t Version LLVM_ATTRIBUTE_UNUSED) { |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 82 | |
| 83 | // Function names may be prefixed with a binary '1' to indicate |
| 84 | // that the backend should not modify the symbols due to any platform |
| 85 | // naming convention. Do not include that '1' in the PGO profile name. |
| 86 | if (RawFuncName[0] == '\1') |
| 87 | RawFuncName = RawFuncName.substr(1); |
| 88 | |
| 89 | std::string FuncName = RawFuncName; |
| 90 | if (llvm::GlobalValue::isLocalLinkage(Linkage)) { |
| 91 | // For local symbols, prepend the main file name to distinguish them. |
| 92 | // Do not include the full path in the file name since there's no guarantee |
| 93 | // that it will stay the same, e.g., if the files are checked out from |
| 94 | // version control in different locations. |
| 95 | if (FileName.empty()) |
Xinliang David Li | a86545b | 2015-12-11 20:23:22 +0000 | [diff] [blame] | 96 | FuncName = FuncName.insert(0, "<unknown>:"); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 97 | else |
Xinliang David Li | a86545b | 2015-12-11 20:23:22 +0000 | [diff] [blame] | 98 | FuncName = FuncName.insert(0, FileName.str() + ":"); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 99 | } |
| 100 | return FuncName; |
| 101 | } |
| 102 | |
Xinliang David Li | 307902e | 2015-12-05 05:16:36 +0000 | [diff] [blame] | 103 | std::string getPGOFuncName(const Function &F, uint64_t Version) { |
| 104 | return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(), |
| 105 | Version); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 108 | StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) { |
| 109 | if (FileName.empty()) |
| 110 | return PGOFuncName; |
| 111 | // Drop the file name including ':'. See also getPGOFuncName. |
| 112 | if (PGOFuncName.startswith(FileName)) |
| 113 | PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1); |
| 114 | return PGOFuncName; |
| 115 | } |
| 116 | |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 117 | // \p FuncName is the string used as profile lookup key for the function. A |
| 118 | // symbol is created to hold the name. Return the legalized symbol name. |
| 119 | static std::string getPGOFuncNameVarName(StringRef FuncName, |
| 120 | GlobalValue::LinkageTypes Linkage) { |
| 121 | std::string VarName = getInstrProfNameVarPrefix(); |
| 122 | VarName += FuncName; |
| 123 | |
| 124 | if (!GlobalValue::isLocalLinkage(Linkage)) |
| 125 | return VarName; |
| 126 | |
| 127 | // Now fix up illegal chars in local VarName that may upset the assembler. |
| 128 | const char *InvalidChars = "-:<>\"'"; |
| 129 | size_t found = VarName.find_first_of(InvalidChars); |
| 130 | while (found != std::string::npos) { |
| 131 | VarName[found] = '_'; |
| 132 | found = VarName.find_first_of(InvalidChars, found + 1); |
| 133 | } |
| 134 | return VarName; |
| 135 | } |
| 136 | |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 137 | GlobalVariable *createPGOFuncNameVar(Module &M, |
| 138 | GlobalValue::LinkageTypes Linkage, |
| 139 | StringRef FuncName) { |
| 140 | |
| 141 | // We generally want to match the function's linkage, but available_externally |
| 142 | // and extern_weak both have the wrong semantics, and anything that doesn't |
| 143 | // need to link across compilation units doesn't need to be visible at all. |
| 144 | if (Linkage == GlobalValue::ExternalWeakLinkage) |
| 145 | Linkage = GlobalValue::LinkOnceAnyLinkage; |
| 146 | else if (Linkage == GlobalValue::AvailableExternallyLinkage) |
| 147 | Linkage = GlobalValue::LinkOnceODRLinkage; |
| 148 | else if (Linkage == GlobalValue::InternalLinkage || |
| 149 | Linkage == GlobalValue::ExternalLinkage) |
| 150 | Linkage = GlobalValue::PrivateLinkage; |
| 151 | |
| 152 | auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false); |
| 153 | auto FuncNameVar = |
| 154 | new GlobalVariable(M, Value->getType(), true, Linkage, Value, |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 155 | getPGOFuncNameVarName(FuncName, Linkage)); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 156 | |
| 157 | // Hide the symbol so that we correctly get a copy for each executable. |
| 158 | if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage())) |
| 159 | FuncNameVar->setVisibility(GlobalValue::HiddenVisibility); |
| 160 | |
| 161 | return FuncNameVar; |
| 162 | } |
| 163 | |
| 164 | GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { |
| 165 | return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); |
| 166 | } |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 167 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 168 | int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs, |
| 169 | bool doCompression, std::string &Result) { |
| 170 | uint8_t Header[16], *P = Header; |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame^] | 171 | std::string UncompressedNameStrings = |
| 172 | join(NameStrs.begin(), NameStrs.end(), StringRef(" ")); |
Xinliang David Li | 13ea29b | 2016-01-04 20:26:05 +0000 | [diff] [blame] | 173 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 174 | unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); |
| 175 | P += EncLen; |
| 176 | if (!doCompression) { |
| 177 | EncLen = encodeULEB128(0, P); |
| 178 | P += EncLen; |
| 179 | Result.append(reinterpret_cast<char *>(&Header[0]), P - &Header[0]); |
| 180 | Result += UncompressedNameStrings; |
| 181 | return 0; |
| 182 | } |
| 183 | SmallVector<char, 128> CompressedNameStrings; |
| 184 | zlib::Status Success = |
| 185 | zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, |
| 186 | zlib::BestSizeCompression); |
| 187 | assert(Success == zlib::StatusOK); |
| 188 | if (Success != zlib::StatusOK) |
| 189 | return 1; |
| 190 | EncLen = encodeULEB128(CompressedNameStrings.size(), P); |
| 191 | P += EncLen; |
| 192 | Result.append(reinterpret_cast<char *>(&Header[0]), P - &Header[0]); |
| 193 | Result += |
| 194 | std::string(CompressedNameStrings.data(), CompressedNameStrings.size()); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Xinliang David Li | 37c1fa0 | 2016-01-03 04:38:13 +0000 | [diff] [blame] | 198 | StringRef getPGOFuncNameInitializer(GlobalVariable *NameVar) { |
| 199 | auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer()); |
| 200 | StringRef NameStr = |
| 201 | Arr->isCString() ? Arr->getAsCString() : Arr->getAsString(); |
| 202 | return NameStr; |
| 203 | } |
| 204 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 205 | int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars, |
| 206 | std::string &Result) { |
| 207 | std::vector<std::string> NameStrs; |
| 208 | for (auto *NameVar : NameVars) { |
Xinliang David Li | 37c1fa0 | 2016-01-03 04:38:13 +0000 | [diff] [blame] | 209 | NameStrs.push_back(getPGOFuncNameInitializer(NameVar)); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 210 | } |
| 211 | return collectPGOFuncNameStrings(NameStrs, zlib::isAvailable(), Result); |
| 212 | } |
| 213 | |
| 214 | int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { |
| 215 | const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data()); |
| 216 | const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() + |
| 217 | NameStrings.size()); |
| 218 | while (P < EndP) { |
| 219 | uint32_t N; |
| 220 | uint64_t UncompressedSize = decodeULEB128(P, &N); |
| 221 | P += N; |
| 222 | uint64_t CompressedSize = decodeULEB128(P, &N); |
| 223 | P += N; |
| 224 | bool isCompressed = (CompressedSize != 0); |
| 225 | SmallString<128> UncompressedNameStrings; |
| 226 | StringRef NameStrings; |
| 227 | if (isCompressed) { |
| 228 | StringRef CompressedNameStrings(reinterpret_cast<const char *>(P), |
| 229 | CompressedSize); |
| 230 | if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, |
| 231 | UncompressedSize) != zlib::StatusOK) |
| 232 | return 1; |
| 233 | P += CompressedSize; |
| 234 | NameStrings = StringRef(UncompressedNameStrings.data(), |
| 235 | UncompressedNameStrings.size()); |
| 236 | } else { |
| 237 | NameStrings = |
| 238 | StringRef(reinterpret_cast<const char *>(P), UncompressedSize); |
| 239 | P += UncompressedSize; |
| 240 | } |
| 241 | // Now parse the name strings. |
| 242 | size_t NameStart = 0; |
| 243 | bool isLast = false; |
| 244 | do { |
| 245 | size_t NameStop = NameStrings.find(' ', NameStart); |
| 246 | if (NameStop == StringRef::npos) |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame^] | 247 | NameStop = NameStrings.size(); |
| 248 | if (NameStop >= NameStrings.size() - 1) |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 249 | isLast = true; |
| 250 | StringRef Name = NameStrings.substr(NameStart, NameStop - NameStart); |
| 251 | Symtab.addFuncName(Name); |
| 252 | if (isLast) |
| 253 | break; |
| 254 | NameStart = NameStop + 1; |
| 255 | } while (true); |
| 256 | |
| 257 | while (P < EndP && *P == 0) |
| 258 | P++; |
| 259 | } |
| 260 | Symtab.finalizeSymtab(); |
| 261 | return 0; |
| 262 | } |
| 263 | |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 264 | instrprof_error |
| 265 | InstrProfValueSiteRecord::mergeValueData(InstrProfValueSiteRecord &Input, |
| 266 | uint64_t Weight) { |
| 267 | this->sortByTargetValues(); |
| 268 | Input.sortByTargetValues(); |
| 269 | auto I = ValueData.begin(); |
| 270 | auto IE = ValueData.end(); |
| 271 | instrprof_error Result = instrprof_error::success; |
| 272 | for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE; |
| 273 | ++J) { |
| 274 | while (I != IE && I->Value < J->Value) |
| 275 | ++I; |
| 276 | if (I != IE && I->Value == J->Value) { |
| 277 | uint64_t JCount = J->Count; |
| 278 | bool Overflowed; |
| 279 | if (Weight > 1) { |
| 280 | JCount = SaturatingMultiply(JCount, Weight, &Overflowed); |
| 281 | if (Overflowed) |
| 282 | Result = instrprof_error::counter_overflow; |
| 283 | } |
| 284 | I->Count = SaturatingAdd(I->Count, JCount, &Overflowed); |
| 285 | if (Overflowed) |
| 286 | Result = instrprof_error::counter_overflow; |
| 287 | ++I; |
| 288 | continue; |
| 289 | } |
| 290 | ValueData.insert(I, *J); |
| 291 | } |
| 292 | return Result; |
| 293 | } |
| 294 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 295 | // Merge Value Profile data from Src record to this record for ValueKind. |
| 296 | // Scale merged value counts by \p Weight. |
| 297 | instrprof_error InstrProfRecord::mergeValueProfData(uint32_t ValueKind, |
| 298 | InstrProfRecord &Src, |
| 299 | uint64_t Weight) { |
| 300 | uint32_t ThisNumValueSites = getNumValueSites(ValueKind); |
| 301 | uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind); |
| 302 | if (ThisNumValueSites != OtherNumValueSites) |
| 303 | return instrprof_error::value_site_count_mismatch; |
| 304 | std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = |
| 305 | getValueSitesForKind(ValueKind); |
| 306 | std::vector<InstrProfValueSiteRecord> &OtherSiteRecords = |
| 307 | Src.getValueSitesForKind(ValueKind); |
| 308 | instrprof_error Result = instrprof_error::success; |
| 309 | for (uint32_t I = 0; I < ThisNumValueSites; I++) |
| 310 | MergeResult(Result, |
| 311 | ThisSiteRecords[I].mergeValueData(OtherSiteRecords[I], Weight)); |
| 312 | return Result; |
| 313 | } |
| 314 | |
| 315 | instrprof_error InstrProfRecord::merge(InstrProfRecord &Other, |
| 316 | uint64_t Weight) { |
| 317 | // If the number of counters doesn't match we either have bad data |
| 318 | // or a hash collision. |
| 319 | if (Counts.size() != Other.Counts.size()) |
| 320 | return instrprof_error::count_mismatch; |
| 321 | |
| 322 | instrprof_error Result = instrprof_error::success; |
| 323 | |
| 324 | for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) { |
| 325 | bool Overflowed; |
| 326 | uint64_t OtherCount = Other.Counts[I]; |
| 327 | if (Weight > 1) { |
| 328 | OtherCount = SaturatingMultiply(OtherCount, Weight, &Overflowed); |
| 329 | if (Overflowed) |
| 330 | Result = instrprof_error::counter_overflow; |
| 331 | } |
| 332 | Counts[I] = SaturatingAdd(Counts[I], OtherCount, &Overflowed); |
| 333 | if (Overflowed) |
| 334 | Result = instrprof_error::counter_overflow; |
| 335 | } |
| 336 | |
| 337 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 338 | MergeResult(Result, mergeValueProfData(Kind, Other, Weight)); |
| 339 | |
| 340 | return Result; |
| 341 | } |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 342 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 343 | // Map indirect call target name hash to name string. |
| 344 | uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 345 | ValueMapType *ValueMap) { |
| 346 | if (!ValueMap) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 347 | return Value; |
| 348 | switch (ValueKind) { |
| 349 | case IPVK_IndirectCallTarget: { |
| 350 | auto Result = |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 351 | std::lower_bound(ValueMap->begin(), ValueMap->end(), Value, |
| 352 | [](const std::pair<uint64_t, uint64_t> &LHS, |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 353 | uint64_t RHS) { return LHS.first < RHS; }); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 354 | if (Result != ValueMap->end()) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 355 | Value = (uint64_t)Result->second; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | return Value; |
| 360 | } |
| 361 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 362 | void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site, |
| 363 | InstrProfValueData *VData, uint32_t N, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 364 | ValueMapType *ValueMap) { |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 365 | for (uint32_t I = 0; I < N; I++) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 366 | VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 367 | } |
| 368 | std::vector<InstrProfValueSiteRecord> &ValueSites = |
| 369 | getValueSitesForKind(ValueKind); |
| 370 | if (N == 0) |
| 371 | ValueSites.push_back(InstrProfValueSiteRecord()); |
| 372 | else |
| 373 | ValueSites.emplace_back(VData, VData + N); |
| 374 | } |
| 375 | |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 376 | #define INSTR_PROF_COMMON_API_IMPL |
| 377 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 378 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 379 | /*! |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 380 | * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 381 | * class. These C wrappers are used as adaptors so that C++ code can be |
| 382 | * invoked as callbacks. |
| 383 | */ |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 384 | uint32_t getNumValueKindsInstrProf(const void *Record) { |
| 385 | return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds(); |
| 386 | } |
| 387 | |
| 388 | uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) { |
| 389 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 390 | ->getNumValueSites(VKind); |
| 391 | } |
| 392 | |
| 393 | uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) { |
| 394 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 395 | ->getNumValueData(VKind); |
| 396 | } |
| 397 | |
| 398 | uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, |
| 399 | uint32_t S) { |
| 400 | return reinterpret_cast<const InstrProfRecord *>(R) |
| 401 | ->getNumValueDataForSite(VK, S); |
| 402 | } |
| 403 | |
| 404 | void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, |
| 405 | uint32_t K, uint32_t S, |
| 406 | uint64_t (*Mapper)(uint32_t, uint64_t)) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 407 | return reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite( |
| 408 | Dst, K, S, Mapper); |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 411 | ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) { |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 412 | ValueProfData *VD = |
| 413 | (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData()); |
| 414 | memset(VD, 0, TotalSizeInBytes); |
| 415 | return VD; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static ValueProfRecordClosure InstrProfRecordClosure = { |
| 419 | 0, |
| 420 | getNumValueKindsInstrProf, |
| 421 | getNumValueSitesInstrProf, |
| 422 | getNumValueDataInstrProf, |
| 423 | getNumValueDataForSiteInstrProf, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 424 | 0, |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 425 | getValueForSiteInstrProf, |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 426 | allocValueProfDataInstrProf}; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 427 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 428 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 429 | uint32_t ValueProfData::getSize(const InstrProfRecord &Record) { |
| 430 | InstrProfRecordClosure.Record = &Record; |
| 431 | return getValueProfDataSize(&InstrProfRecordClosure); |
| 432 | } |
| 433 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 434 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 435 | std::unique_ptr<ValueProfData> |
| 436 | ValueProfData::serializeFrom(const InstrProfRecord &Record) { |
| 437 | InstrProfRecordClosure.Record = &Record; |
| 438 | |
| 439 | std::unique_ptr<ValueProfData> VPD( |
Xinliang David Li | 0e6a36e | 2015-12-01 19:47:32 +0000 | [diff] [blame] | 440 | serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr)); |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 441 | return VPD; |
| 442 | } |
| 443 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 444 | void ValueProfRecord::deserializeTo(InstrProfRecord &Record, |
| 445 | InstrProfRecord::ValueMapType *VMap) { |
| 446 | Record.reserveSites(Kind, NumValueSites); |
| 447 | |
| 448 | InstrProfValueData *ValueData = getValueProfRecordValueData(this); |
| 449 | for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) { |
| 450 | uint8_t ValueDataCount = this->SiteCountArray[VSite]; |
| 451 | Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap); |
| 452 | ValueData += ValueDataCount; |
| 453 | } |
| 454 | } |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 455 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 456 | // For writing/serializing, Old is the host endianness, and New is |
| 457 | // byte order intended on disk. For Reading/deserialization, Old |
| 458 | // is the on-disk source endianness, and New is the host endianness. |
| 459 | void ValueProfRecord::swapBytes(support::endianness Old, |
| 460 | support::endianness New) { |
| 461 | using namespace support; |
| 462 | if (Old == New) |
| 463 | return; |
| 464 | |
| 465 | if (getHostEndianness() != Old) { |
| 466 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 467 | sys::swapByteOrder<uint32_t>(Kind); |
| 468 | } |
| 469 | uint32_t ND = getValueProfRecordNumValueData(this); |
| 470 | InstrProfValueData *VD = getValueProfRecordValueData(this); |
| 471 | |
| 472 | // No need to swap byte array: SiteCountArrray. |
| 473 | for (uint32_t I = 0; I < ND; I++) { |
| 474 | sys::swapByteOrder<uint64_t>(VD[I].Value); |
| 475 | sys::swapByteOrder<uint64_t>(VD[I].Count); |
| 476 | } |
| 477 | if (getHostEndianness() == Old) { |
| 478 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 479 | sys::swapByteOrder<uint32_t>(Kind); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void ValueProfData::deserializeTo(InstrProfRecord &Record, |
| 484 | InstrProfRecord::ValueMapType *VMap) { |
| 485 | if (NumValueKinds == 0) |
| 486 | return; |
| 487 | |
| 488 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 489 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 490 | VR->deserializeTo(Record, VMap); |
| 491 | VR = getValueProfRecordNext(VR); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | template <class T> |
| 496 | static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) { |
| 497 | using namespace support; |
| 498 | if (Orig == little) |
| 499 | return endian::readNext<T, little, unaligned>(D); |
| 500 | else |
| 501 | return endian::readNext<T, big, unaligned>(D); |
| 502 | } |
| 503 | |
| 504 | static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) { |
| 505 | return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) |
| 506 | ValueProfData()); |
| 507 | } |
| 508 | |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 509 | instrprof_error ValueProfData::checkIntegrity() { |
| 510 | if (NumValueKinds > IPVK_Last + 1) |
| 511 | return instrprof_error::malformed; |
| 512 | // Total size needs to be mulltiple of quadword size. |
| 513 | if (TotalSize % sizeof(uint64_t)) |
| 514 | return instrprof_error::malformed; |
| 515 | |
| 516 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 517 | for (uint32_t K = 0; K < this->NumValueKinds; K++) { |
| 518 | if (VR->Kind > IPVK_Last) |
| 519 | return instrprof_error::malformed; |
| 520 | VR = getValueProfRecordNext(VR); |
| 521 | if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) |
| 522 | return instrprof_error::malformed; |
| 523 | } |
| 524 | return instrprof_error::success; |
| 525 | } |
| 526 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 527 | ErrorOr<std::unique_ptr<ValueProfData>> |
| 528 | ValueProfData::getValueProfData(const unsigned char *D, |
| 529 | const unsigned char *const BufferEnd, |
| 530 | support::endianness Endianness) { |
| 531 | using namespace support; |
| 532 | if (D + sizeof(ValueProfData) > BufferEnd) |
| 533 | return instrprof_error::truncated; |
| 534 | |
Xinliang David Li | b8c3ad1 | 2015-11-17 03:47:21 +0000 | [diff] [blame] | 535 | const unsigned char *Header = D; |
| 536 | uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 537 | if (D + TotalSize > BufferEnd) |
| 538 | return instrprof_error::too_large; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 539 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 540 | std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 541 | memcpy(VPD.get(), D, TotalSize); |
| 542 | // Byte swap. |
| 543 | VPD->swapBytesToHost(Endianness); |
| 544 | |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 545 | instrprof_error EC = VPD->checkIntegrity(); |
| 546 | if (EC != instrprof_error::success) |
| 547 | return EC; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 548 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 549 | return std::move(VPD); |
| 550 | } |
| 551 | |
| 552 | void ValueProfData::swapBytesToHost(support::endianness Endianness) { |
| 553 | using namespace support; |
| 554 | if (Endianness == getHostEndianness()) |
| 555 | return; |
| 556 | |
| 557 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 558 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 559 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 560 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 561 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 562 | VR->swapBytes(Endianness, getHostEndianness()); |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 563 | VR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | void ValueProfData::swapBytesFromHost(support::endianness Endianness) { |
| 568 | using namespace support; |
| 569 | if (Endianness == getHostEndianness()) |
| 570 | return; |
| 571 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 572 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 573 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 574 | ValueProfRecord *NVR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 575 | VR->swapBytes(getHostEndianness(), Endianness); |
| 576 | VR = NVR; |
| 577 | } |
| 578 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 579 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 580 | } |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 581 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 582 | } |