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 | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 20 | #include "llvm/IR/MDBuilder.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/Compression.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LEB128.h" |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 30 | std::string getInstrProfErrString(instrprof_error Err) { |
| 31 | switch (Err) { |
| 32 | case instrprof_error::success: |
| 33 | return "Success"; |
| 34 | case instrprof_error::eof: |
| 35 | return "End of File"; |
| 36 | case instrprof_error::unrecognized_format: |
| 37 | return "Unrecognized instrumentation profile encoding format"; |
| 38 | case instrprof_error::bad_magic: |
| 39 | return "Invalid instrumentation profile data (bad magic)"; |
| 40 | case instrprof_error::bad_header: |
| 41 | return "Invalid instrumentation profile data (file header is corrupt)"; |
| 42 | case instrprof_error::unsupported_version: |
| 43 | return "Unsupported instrumentation profile format version"; |
| 44 | case instrprof_error::unsupported_hash_type: |
| 45 | return "Unsupported instrumentation profile hash type"; |
| 46 | case instrprof_error::too_large: |
| 47 | return "Too much profile data"; |
| 48 | case instrprof_error::truncated: |
| 49 | return "Truncated profile data"; |
| 50 | case instrprof_error::malformed: |
| 51 | return "Malformed instrumentation profile data"; |
| 52 | case instrprof_error::unknown_function: |
| 53 | return "No profile data available for function"; |
| 54 | case instrprof_error::hash_mismatch: |
| 55 | return "Function control flow change detected (hash mismatch)"; |
| 56 | case instrprof_error::count_mismatch: |
| 57 | return "Function basic block count change detected (counter mismatch)"; |
| 58 | case instrprof_error::counter_overflow: |
| 59 | return "Counter overflow"; |
| 60 | case instrprof_error::value_site_count_mismatch: |
| 61 | return "Function value site count change detected (counter mismatch)"; |
| 62 | case instrprof_error::compress_failed: |
| 63 | return "Failed to compress data (zlib)"; |
| 64 | case instrprof_error::uncompress_failed: |
| 65 | return "Failed to uncompress data (zlib)"; |
| 66 | } |
| 67 | llvm_unreachable("A value of instrprof_error has no message."); |
| 68 | } |
| 69 | |
Peter Collingbourne | 4718f8b | 2016-05-24 20:13:46 +0000 | [diff] [blame] | 70 | // FIXME: This class is only here to support the transition to llvm::Error. It |
| 71 | // will be removed once this transition is complete. Clients should prefer to |
| 72 | // deal with the Error value directly, rather than converting to error_code. |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 73 | class InstrProfErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 74 | const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 75 | std::string message(int IE) const override { |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 76 | return getInstrProfErrString(static_cast<instrprof_error>(IE)); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 77 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 78 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 79 | } // end anonymous namespace |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 80 | |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 81 | static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; |
| 82 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 83 | const std::error_category &llvm::instrprof_category() { |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 84 | return *ErrorCategory; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 85 | } |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 86 | |
| 87 | namespace llvm { |
| 88 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 89 | void SoftInstrProfErrors::addError(instrprof_error IE) { |
| 90 | if (IE == instrprof_error::success) |
| 91 | return; |
| 92 | |
| 93 | if (FirstError == instrprof_error::success) |
| 94 | FirstError = IE; |
| 95 | |
| 96 | switch (IE) { |
| 97 | case instrprof_error::hash_mismatch: |
| 98 | ++NumHashMismatches; |
| 99 | break; |
| 100 | case instrprof_error::count_mismatch: |
| 101 | ++NumCountMismatches; |
| 102 | break; |
| 103 | case instrprof_error::counter_overflow: |
| 104 | ++NumCounterOverflows; |
| 105 | break; |
| 106 | case instrprof_error::value_site_count_mismatch: |
| 107 | ++NumValueSiteCountMismatches; |
| 108 | break; |
| 109 | default: |
| 110 | llvm_unreachable("Not a soft error"); |
| 111 | } |
| 112 | } |
| 113 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 114 | std::string InstrProfError::message() const { |
| 115 | return getInstrProfErrString(Err); |
| 116 | } |
| 117 | |
| 118 | char InstrProfError::ID = 0; |
| 119 | |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 120 | std::string getPGOFuncName(StringRef RawFuncName, |
| 121 | GlobalValue::LinkageTypes Linkage, |
Xinliang David Li | a86545b | 2015-12-11 20:23:22 +0000 | [diff] [blame] | 122 | StringRef FileName, |
| 123 | uint64_t Version LLVM_ATTRIBUTE_UNUSED) { |
Teresa Johnson | b43027d | 2016-03-15 02:13:19 +0000 | [diff] [blame] | 124 | return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Rong Xu | b534166 | 2016-03-30 18:37:52 +0000 | [diff] [blame] | 127 | // Return the PGOFuncName. This function has some special handling when called |
| 128 | // in LTO optimization. The following only applies when calling in LTO passes |
| 129 | // (when \c InLTO is true): LTO's internalization privatizes many global linkage |
| 130 | // symbols. This happens after value profile annotation, but those internal |
| 131 | // linkage functions should not have a source prefix. |
| 132 | // To differentiate compiler generated internal symbols from original ones, |
| 133 | // PGOFuncName meta data are created and attached to the original internal |
| 134 | // symbols in the value profile annotation step |
| 135 | // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta |
| 136 | // data, its original linkage must be non-internal. |
| 137 | std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) { |
| 138 | if (!InLTO) |
| 139 | return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(), |
| 140 | Version); |
| 141 | |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 142 | // In LTO mode (when InLTO is true), first check if there is a meta data. |
| 143 | if (MDNode *MD = getPGOFuncNameMetadata(F)) { |
Rong Xu | b534166 | 2016-03-30 18:37:52 +0000 | [diff] [blame] | 144 | StringRef S = cast<MDString>(MD->getOperand(0))->getString(); |
| 145 | return S.str(); |
| 146 | } |
| 147 | |
| 148 | // If there is no meta data, the function must be a global before the value |
| 149 | // profile annotation pass. Its current linkage may be internal if it is |
| 150 | // internalized in LTO mode. |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 151 | return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, ""); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 154 | StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) { |
| 155 | if (FileName.empty()) |
Vedant Kumar | 43a8565 | 2016-03-28 15:49:08 +0000 | [diff] [blame] | 156 | return PGOFuncName; |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 157 | // Drop the file name including ':'. See also getPGOFuncName. |
| 158 | if (PGOFuncName.startswith(FileName)) |
| 159 | PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1); |
| 160 | return PGOFuncName; |
| 161 | } |
| 162 | |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 163 | // \p FuncName is the string used as profile lookup key for the function. A |
| 164 | // symbol is created to hold the name. Return the legalized symbol name. |
Vedant Kumar | aa0cae6 | 2016-03-16 20:49:26 +0000 | [diff] [blame] | 165 | std::string getPGOFuncNameVarName(StringRef FuncName, |
| 166 | GlobalValue::LinkageTypes Linkage) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 167 | std::string VarName = getInstrProfNameVarPrefix(); |
| 168 | VarName += FuncName; |
| 169 | |
| 170 | if (!GlobalValue::isLocalLinkage(Linkage)) |
| 171 | return VarName; |
| 172 | |
| 173 | // Now fix up illegal chars in local VarName that may upset the assembler. |
| 174 | const char *InvalidChars = "-:<>\"'"; |
| 175 | size_t found = VarName.find_first_of(InvalidChars); |
| 176 | while (found != std::string::npos) { |
| 177 | VarName[found] = '_'; |
| 178 | found = VarName.find_first_of(InvalidChars, found + 1); |
| 179 | } |
| 180 | return VarName; |
| 181 | } |
| 182 | |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 183 | GlobalVariable *createPGOFuncNameVar(Module &M, |
| 184 | GlobalValue::LinkageTypes Linkage, |
Xinliang David Li | 897d292 | 2016-03-16 22:13:41 +0000 | [diff] [blame] | 185 | StringRef PGOFuncName) { |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 186 | |
| 187 | // We generally want to match the function's linkage, but available_externally |
| 188 | // and extern_weak both have the wrong semantics, and anything that doesn't |
| 189 | // need to link across compilation units doesn't need to be visible at all. |
| 190 | if (Linkage == GlobalValue::ExternalWeakLinkage) |
| 191 | Linkage = GlobalValue::LinkOnceAnyLinkage; |
| 192 | else if (Linkage == GlobalValue::AvailableExternallyLinkage) |
| 193 | Linkage = GlobalValue::LinkOnceODRLinkage; |
| 194 | else if (Linkage == GlobalValue::InternalLinkage || |
| 195 | Linkage == GlobalValue::ExternalLinkage) |
| 196 | Linkage = GlobalValue::PrivateLinkage; |
| 197 | |
Xinliang David Li | 897d292 | 2016-03-16 22:13:41 +0000 | [diff] [blame] | 198 | auto *Value = |
| 199 | ConstantDataArray::getString(M.getContext(), PGOFuncName, false); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 200 | auto FuncNameVar = |
| 201 | new GlobalVariable(M, Value->getType(), true, Linkage, Value, |
Xinliang David Li | 897d292 | 2016-03-16 22:13:41 +0000 | [diff] [blame] | 202 | getPGOFuncNameVarName(PGOFuncName, Linkage)); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 203 | |
| 204 | // Hide the symbol so that we correctly get a copy for each executable. |
| 205 | if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage())) |
| 206 | FuncNameVar->setVisibility(GlobalValue::HiddenVisibility); |
| 207 | |
| 208 | return FuncNameVar; |
| 209 | } |
| 210 | |
Xinliang David Li | 897d292 | 2016-03-16 22:13:41 +0000 | [diff] [blame] | 211 | GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) { |
| 212 | return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 213 | } |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 214 | |
Rong Xu | b534166 | 2016-03-30 18:37:52 +0000 | [diff] [blame] | 215 | void InstrProfSymtab::create(Module &M, bool InLTO) { |
| 216 | for (Function &F : M) { |
| 217 | // Function may not have a name: like using asm("") to overwrite the name. |
| 218 | // Ignore in this case. |
| 219 | if (!F.hasName()) |
| 220 | continue; |
| 221 | const std::string &PGOFuncName = getPGOFuncName(F, InLTO); |
| 222 | addFuncName(PGOFuncName); |
Rong Xu | d5a57b5 | 2016-03-31 17:39:33 +0000 | [diff] [blame] | 223 | MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F); |
Rong Xu | b534166 | 2016-03-30 18:37:52 +0000 | [diff] [blame] | 224 | } |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 225 | |
| 226 | finalizeSymtab(); |
| 227 | } |
| 228 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 229 | Error collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs, |
| 230 | bool doCompression, std::string &Result) { |
Vedant Kumar | 86705ba | 2016-03-28 21:06:42 +0000 | [diff] [blame] | 231 | assert(NameStrs.size() && "No name data to emit"); |
| 232 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 233 | uint8_t Header[16], *P = Header; |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame] | 234 | std::string UncompressedNameStrings = |
Vedant Kumar | 86705ba | 2016-03-28 21:06:42 +0000 | [diff] [blame] | 235 | join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator()); |
| 236 | |
| 237 | assert(StringRef(UncompressedNameStrings) |
| 238 | .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) && |
| 239 | "PGO name is invalid (contains separator token)"); |
Xinliang David Li | 13ea29b | 2016-01-04 20:26:05 +0000 | [diff] [blame] | 240 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 241 | unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); |
| 242 | P += EncLen; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 243 | |
| 244 | auto WriteStringToResult = [&](size_t CompressedLen, |
| 245 | const std::string &InputStr) { |
| 246 | EncLen = encodeULEB128(CompressedLen, P); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 247 | P += EncLen; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 248 | char *HeaderStr = reinterpret_cast<char *>(&Header[0]); |
| 249 | unsigned HeaderLen = P - &Header[0]; |
| 250 | Result.append(HeaderStr, HeaderLen); |
| 251 | Result += InputStr; |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 252 | return Error::success(); |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 255 | if (!doCompression) { |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 256 | return WriteStringToResult(0, UncompressedNameStrings); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 257 | } |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 258 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 259 | SmallVector<char, 128> CompressedNameStrings; |
| 260 | zlib::Status Success = |
| 261 | zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, |
| 262 | zlib::BestSizeCompression); |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 263 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 264 | if (Success != zlib::StatusOK) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 265 | return make_error<InstrProfError>(instrprof_error::compress_failed); |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 266 | |
| 267 | return WriteStringToResult( |
| 268 | CompressedNameStrings.size(), |
| 269 | std::string(CompressedNameStrings.data(), CompressedNameStrings.size())); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Xinliang David Li | eb7d7f8 | 2016-02-04 23:59:09 +0000 | [diff] [blame] | 272 | StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) { |
Xinliang David Li | 37c1fa0 | 2016-01-03 04:38:13 +0000 | [diff] [blame] | 273 | auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer()); |
| 274 | StringRef NameStr = |
| 275 | Arr->isCString() ? Arr->getAsCString() : Arr->getAsString(); |
| 276 | return NameStr; |
| 277 | } |
| 278 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 279 | Error collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars, |
| 280 | std::string &Result, bool doCompression) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 281 | std::vector<std::string> NameStrs; |
| 282 | for (auto *NameVar : NameVars) { |
Xinliang David Li | eb7d7f8 | 2016-02-04 23:59:09 +0000 | [diff] [blame] | 283 | NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar)); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 284 | } |
Xinliang David Li | 7316375 | 2016-01-26 23:13:00 +0000 | [diff] [blame] | 285 | return collectPGOFuncNameStrings( |
| 286 | NameStrs, zlib::isAvailable() && doCompression, Result); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 289 | Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 290 | const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data()); |
| 291 | const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() + |
| 292 | NameStrings.size()); |
| 293 | while (P < EndP) { |
| 294 | uint32_t N; |
| 295 | uint64_t UncompressedSize = decodeULEB128(P, &N); |
| 296 | P += N; |
| 297 | uint64_t CompressedSize = decodeULEB128(P, &N); |
| 298 | P += N; |
| 299 | bool isCompressed = (CompressedSize != 0); |
| 300 | SmallString<128> UncompressedNameStrings; |
| 301 | StringRef NameStrings; |
| 302 | if (isCompressed) { |
| 303 | StringRef CompressedNameStrings(reinterpret_cast<const char *>(P), |
| 304 | CompressedSize); |
| 305 | if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, |
| 306 | UncompressedSize) != zlib::StatusOK) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 307 | return make_error<InstrProfError>(instrprof_error::uncompress_failed); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 308 | P += CompressedSize; |
| 309 | NameStrings = StringRef(UncompressedNameStrings.data(), |
| 310 | UncompressedNameStrings.size()); |
| 311 | } else { |
| 312 | NameStrings = |
| 313 | StringRef(reinterpret_cast<const char *>(P), UncompressedSize); |
| 314 | P += UncompressedSize; |
| 315 | } |
| 316 | // Now parse the name strings. |
Xinliang David Li | 204efe2 | 2016-01-04 22:09:26 +0000 | [diff] [blame] | 317 | SmallVector<StringRef, 0> Names; |
Vedant Kumar | 86705ba | 2016-03-28 21:06:42 +0000 | [diff] [blame] | 318 | NameStrings.split(Names, getInstrProfNameSeparator()); |
Xinliang David Li | 204efe2 | 2016-01-04 22:09:26 +0000 | [diff] [blame] | 319 | for (StringRef &Name : Names) |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 320 | Symtab.addFuncName(Name); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 321 | |
| 322 | while (P < EndP && *P == 0) |
| 323 | P++; |
| 324 | } |
| 325 | Symtab.finalizeSymtab(); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 326 | return Error::success(); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 329 | void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE, |
| 330 | InstrProfValueSiteRecord &Input, |
| 331 | uint64_t Weight) { |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 332 | this->sortByTargetValues(); |
| 333 | Input.sortByTargetValues(); |
| 334 | auto I = ValueData.begin(); |
| 335 | auto IE = ValueData.end(); |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 336 | for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE; |
| 337 | ++J) { |
| 338 | while (I != IE && I->Value < J->Value) |
| 339 | ++I; |
| 340 | if (I != IE && I->Value == J->Value) { |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 341 | bool Overflowed; |
Nathan Slingerland | 7bee316 | 2016-01-12 22:34:00 +0000 | [diff] [blame] | 342 | I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed); |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 343 | if (Overflowed) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 344 | SIPE.addError(instrprof_error::counter_overflow); |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 345 | ++I; |
| 346 | continue; |
| 347 | } |
| 348 | ValueData.insert(I, *J); |
| 349 | } |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 352 | void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE, |
| 353 | uint64_t Weight) { |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 354 | for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) { |
| 355 | bool Overflowed; |
| 356 | I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed); |
| 357 | if (Overflowed) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 358 | SIPE.addError(instrprof_error::counter_overflow); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 359 | } |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 362 | // Merge Value Profile data from Src record to this record for ValueKind. |
| 363 | // Scale merged value counts by \p Weight. |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 364 | void InstrProfRecord::mergeValueProfData(uint32_t ValueKind, |
| 365 | InstrProfRecord &Src, |
| 366 | uint64_t Weight) { |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 367 | uint32_t ThisNumValueSites = getNumValueSites(ValueKind); |
| 368 | uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind); |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 369 | if (ThisNumValueSites != OtherNumValueSites) { |
| 370 | SIPE.addError(instrprof_error::value_site_count_mismatch); |
| 371 | return; |
| 372 | } |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 373 | std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = |
| 374 | getValueSitesForKind(ValueKind); |
| 375 | std::vector<InstrProfValueSiteRecord> &OtherSiteRecords = |
| 376 | Src.getValueSitesForKind(ValueKind); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 377 | for (uint32_t I = 0; I < ThisNumValueSites; I++) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 378 | ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 381 | void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) { |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 382 | // If the number of counters doesn't match we either have bad data |
| 383 | // or a hash collision. |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 384 | if (Counts.size() != Other.Counts.size()) { |
| 385 | SIPE.addError(instrprof_error::count_mismatch); |
| 386 | return; |
| 387 | } |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 388 | |
| 389 | for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) { |
| 390 | bool Overflowed; |
Nathan Slingerland | 7bee316 | 2016-01-12 22:34:00 +0000 | [diff] [blame] | 391 | Counts[I] = |
| 392 | SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 393 | if (Overflowed) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 394 | SIPE.addError(instrprof_error::counter_overflow); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 398 | mergeValueProfData(Kind, Other, Weight); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 399 | } |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 400 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 401 | void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) { |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 402 | uint32_t ThisNumValueSites = getNumValueSites(ValueKind); |
| 403 | std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = |
| 404 | getValueSitesForKind(ValueKind); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 405 | for (uint32_t I = 0; I < ThisNumValueSites; I++) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 406 | ThisSiteRecords[I].scale(SIPE, Weight); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 409 | void InstrProfRecord::scale(uint64_t Weight) { |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 410 | for (auto &Count : this->Counts) { |
| 411 | bool Overflowed; |
| 412 | Count = SaturatingMultiply(Count, Weight, &Overflowed); |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 413 | if (Overflowed) |
| 414 | SIPE.addError(instrprof_error::counter_overflow); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 415 | } |
| 416 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
Vedant Kumar | 42369db | 2016-05-11 19:42:19 +0000 | [diff] [blame] | 417 | scaleValueProfData(Kind, Weight); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 420 | // Map indirect call target name hash to name string. |
| 421 | uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 422 | ValueMapType *ValueMap) { |
| 423 | if (!ValueMap) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 424 | return Value; |
| 425 | switch (ValueKind) { |
| 426 | case IPVK_IndirectCallTarget: { |
| 427 | auto Result = |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 428 | std::lower_bound(ValueMap->begin(), ValueMap->end(), Value, |
| 429 | [](const std::pair<uint64_t, uint64_t> &LHS, |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 430 | uint64_t RHS) { return LHS.first < RHS; }); |
Xinliang David Li | 8dd4ca8 | 2016-04-11 17:13:08 +0000 | [diff] [blame] | 431 | // Raw function pointer collected by value profiler may be from |
| 432 | // external functions that are not instrumented. They won't have |
| 433 | // mapping data to be used by the deserializer. Force the value to |
| 434 | // be 0 in this case. |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 435 | if (Result != ValueMap->end() && Result->first == Value) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 436 | Value = (uint64_t)Result->second; |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 437 | else |
| 438 | Value = 0; |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | return Value; |
| 443 | } |
| 444 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 445 | void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site, |
| 446 | InstrProfValueData *VData, uint32_t N, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 447 | ValueMapType *ValueMap) { |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 448 | for (uint32_t I = 0; I < N; I++) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 449 | VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 450 | } |
| 451 | std::vector<InstrProfValueSiteRecord> &ValueSites = |
| 452 | getValueSitesForKind(ValueKind); |
| 453 | if (N == 0) |
Vedant Kumar | 6b22ba6 | 2016-05-11 16:03:02 +0000 | [diff] [blame] | 454 | ValueSites.emplace_back(); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 455 | else |
| 456 | ValueSites.emplace_back(VData, VData + N); |
| 457 | } |
| 458 | |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 459 | #define INSTR_PROF_COMMON_API_IMPL |
| 460 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 461 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 462 | /*! |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 463 | * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 464 | * class. These C wrappers are used as adaptors so that C++ code can be |
| 465 | * invoked as callbacks. |
| 466 | */ |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 467 | uint32_t getNumValueKindsInstrProf(const void *Record) { |
| 468 | return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds(); |
| 469 | } |
| 470 | |
| 471 | uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) { |
| 472 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 473 | ->getNumValueSites(VKind); |
| 474 | } |
| 475 | |
| 476 | uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) { |
| 477 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 478 | ->getNumValueData(VKind); |
| 479 | } |
| 480 | |
| 481 | uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, |
| 482 | uint32_t S) { |
| 483 | return reinterpret_cast<const InstrProfRecord *>(R) |
| 484 | ->getNumValueDataForSite(VK, S); |
| 485 | } |
| 486 | |
| 487 | void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 488 | uint32_t K, uint32_t S) { |
| 489 | reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S); |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 492 | ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) { |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 493 | ValueProfData *VD = |
| 494 | (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData()); |
| 495 | memset(VD, 0, TotalSizeInBytes); |
| 496 | return VD; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static ValueProfRecordClosure InstrProfRecordClosure = { |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 500 | nullptr, |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 501 | getNumValueKindsInstrProf, |
| 502 | getNumValueSitesInstrProf, |
| 503 | getNumValueDataInstrProf, |
| 504 | getNumValueDataForSiteInstrProf, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 505 | nullptr, |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 506 | getValueForSiteInstrProf, |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 507 | allocValueProfDataInstrProf}; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 508 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 509 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 510 | uint32_t ValueProfData::getSize(const InstrProfRecord &Record) { |
| 511 | InstrProfRecordClosure.Record = &Record; |
| 512 | return getValueProfDataSize(&InstrProfRecordClosure); |
| 513 | } |
| 514 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 515 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 516 | std::unique_ptr<ValueProfData> |
| 517 | ValueProfData::serializeFrom(const InstrProfRecord &Record) { |
| 518 | InstrProfRecordClosure.Record = &Record; |
| 519 | |
| 520 | std::unique_ptr<ValueProfData> VPD( |
Xinliang David Li | 0e6a36e | 2015-12-01 19:47:32 +0000 | [diff] [blame] | 521 | serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr)); |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 522 | return VPD; |
| 523 | } |
| 524 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 525 | void ValueProfRecord::deserializeTo(InstrProfRecord &Record, |
| 526 | InstrProfRecord::ValueMapType *VMap) { |
| 527 | Record.reserveSites(Kind, NumValueSites); |
| 528 | |
| 529 | InstrProfValueData *ValueData = getValueProfRecordValueData(this); |
| 530 | for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) { |
| 531 | uint8_t ValueDataCount = this->SiteCountArray[VSite]; |
| 532 | Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap); |
| 533 | ValueData += ValueDataCount; |
| 534 | } |
| 535 | } |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 536 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 537 | // For writing/serializing, Old is the host endianness, and New is |
| 538 | // byte order intended on disk. For Reading/deserialization, Old |
| 539 | // is the on-disk source endianness, and New is the host endianness. |
| 540 | void ValueProfRecord::swapBytes(support::endianness Old, |
| 541 | support::endianness New) { |
| 542 | using namespace support; |
| 543 | if (Old == New) |
| 544 | return; |
| 545 | |
| 546 | if (getHostEndianness() != Old) { |
| 547 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 548 | sys::swapByteOrder<uint32_t>(Kind); |
| 549 | } |
| 550 | uint32_t ND = getValueProfRecordNumValueData(this); |
| 551 | InstrProfValueData *VD = getValueProfRecordValueData(this); |
| 552 | |
| 553 | // No need to swap byte array: SiteCountArrray. |
| 554 | for (uint32_t I = 0; I < ND; I++) { |
| 555 | sys::swapByteOrder<uint64_t>(VD[I].Value); |
| 556 | sys::swapByteOrder<uint64_t>(VD[I].Count); |
| 557 | } |
| 558 | if (getHostEndianness() == Old) { |
| 559 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 560 | sys::swapByteOrder<uint32_t>(Kind); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void ValueProfData::deserializeTo(InstrProfRecord &Record, |
| 565 | InstrProfRecord::ValueMapType *VMap) { |
| 566 | if (NumValueKinds == 0) |
| 567 | return; |
| 568 | |
| 569 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 570 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 571 | VR->deserializeTo(Record, VMap); |
| 572 | VR = getValueProfRecordNext(VR); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | template <class T> |
| 577 | static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) { |
| 578 | using namespace support; |
| 579 | if (Orig == little) |
| 580 | return endian::readNext<T, little, unaligned>(D); |
| 581 | else |
| 582 | return endian::readNext<T, big, unaligned>(D); |
| 583 | } |
| 584 | |
| 585 | static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) { |
| 586 | return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) |
| 587 | ValueProfData()); |
| 588 | } |
| 589 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 590 | Error ValueProfData::checkIntegrity() { |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 591 | if (NumValueKinds > IPVK_Last + 1) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 592 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 593 | // Total size needs to be mulltiple of quadword size. |
| 594 | if (TotalSize % sizeof(uint64_t)) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 595 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 596 | |
| 597 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 598 | for (uint32_t K = 0; K < this->NumValueKinds; K++) { |
| 599 | if (VR->Kind > IPVK_Last) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 600 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 601 | VR = getValueProfRecordNext(VR); |
| 602 | if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 603 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 604 | } |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 605 | return Error::success(); |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 608 | Expected<std::unique_ptr<ValueProfData>> |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 609 | ValueProfData::getValueProfData(const unsigned char *D, |
| 610 | const unsigned char *const BufferEnd, |
| 611 | support::endianness Endianness) { |
| 612 | using namespace support; |
| 613 | if (D + sizeof(ValueProfData) > BufferEnd) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 614 | return make_error<InstrProfError>(instrprof_error::truncated); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 615 | |
Xinliang David Li | b8c3ad1 | 2015-11-17 03:47:21 +0000 | [diff] [blame] | 616 | const unsigned char *Header = D; |
| 617 | uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 618 | if (D + TotalSize > BufferEnd) |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 619 | return make_error<InstrProfError>(instrprof_error::too_large); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 620 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 621 | std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 622 | memcpy(VPD.get(), D, TotalSize); |
| 623 | // Byte swap. |
| 624 | VPD->swapBytesToHost(Endianness); |
| 625 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 626 | Error E = VPD->checkIntegrity(); |
| 627 | if (E) |
| 628 | return std::move(E); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 629 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 630 | return std::move(VPD); |
| 631 | } |
| 632 | |
| 633 | void ValueProfData::swapBytesToHost(support::endianness Endianness) { |
| 634 | using namespace support; |
| 635 | if (Endianness == getHostEndianness()) |
| 636 | return; |
| 637 | |
| 638 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 639 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 640 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 641 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 642 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 643 | VR->swapBytes(Endianness, getHostEndianness()); |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 644 | VR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | void ValueProfData::swapBytesFromHost(support::endianness Endianness) { |
| 649 | using namespace support; |
| 650 | if (Endianness == getHostEndianness()) |
| 651 | return; |
| 652 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 653 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 654 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 655 | ValueProfRecord *NVR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 656 | VR->swapBytes(getHostEndianness(), Endianness); |
| 657 | VR = NVR; |
| 658 | } |
| 659 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 660 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 661 | } |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 662 | |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 663 | void annotateValueSite(Module &M, Instruction &Inst, |
| 664 | const InstrProfRecord &InstrProfR, |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 665 | InstrProfValueKind ValueKind, uint32_t SiteIdx, |
| 666 | uint32_t MaxMDCount) { |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 667 | uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx); |
Betul Buyukkurt | 4f1e8c9 | 2016-04-14 16:25:45 +0000 | [diff] [blame] | 668 | if (!NV) |
| 669 | return; |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 670 | |
| 671 | uint64_t Sum = 0; |
| 672 | std::unique_ptr<InstrProfValueData[]> VD = |
| 673 | InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum); |
| 674 | |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 675 | ArrayRef<InstrProfValueData> VDs(VD.get(), NV); |
| 676 | annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount); |
Rong Xu | bb49490 | 2016-02-12 21:36:17 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void annotateValueSite(Module &M, Instruction &Inst, |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 680 | ArrayRef<InstrProfValueData> VDs, |
Rong Xu | bb49490 | 2016-02-12 21:36:17 +0000 | [diff] [blame] | 681 | uint64_t Sum, InstrProfValueKind ValueKind, |
| 682 | uint32_t MaxMDCount) { |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 683 | LLVMContext &Ctx = M.getContext(); |
| 684 | MDBuilder MDHelper(Ctx); |
| 685 | SmallVector<Metadata *, 3> Vals; |
| 686 | // Tag |
| 687 | Vals.push_back(MDHelper.createString("VP")); |
| 688 | // Value Kind |
| 689 | Vals.push_back(MDHelper.createConstant( |
| 690 | ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind))); |
| 691 | // Total Count |
| 692 | Vals.push_back( |
| 693 | MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum))); |
| 694 | |
| 695 | // Value Profile Data |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 696 | uint32_t MDCount = MaxMDCount; |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 697 | for (auto &VD : VDs) { |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 698 | Vals.push_back(MDHelper.createConstant( |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 699 | ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value))); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 700 | Vals.push_back(MDHelper.createConstant( |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 701 | ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count))); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 702 | if (--MDCount == 0) |
| 703 | break; |
| 704 | } |
| 705 | Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals)); |
| 706 | } |
| 707 | |
| 708 | bool getValueProfDataFromInst(const Instruction &Inst, |
| 709 | InstrProfValueKind ValueKind, |
| 710 | uint32_t MaxNumValueData, |
| 711 | InstrProfValueData ValueData[], |
| 712 | uint32_t &ActualNumValueData, uint64_t &TotalC) { |
| 713 | MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof); |
| 714 | if (!MD) |
| 715 | return false; |
| 716 | |
| 717 | unsigned NOps = MD->getNumOperands(); |
| 718 | |
| 719 | if (NOps < 5) |
| 720 | return false; |
| 721 | |
| 722 | // Operand 0 is a string tag "VP": |
| 723 | MDString *Tag = cast<MDString>(MD->getOperand(0)); |
| 724 | if (!Tag) |
| 725 | return false; |
| 726 | |
| 727 | if (!Tag->getString().equals("VP")) |
| 728 | return false; |
| 729 | |
| 730 | // Now check kind: |
| 731 | ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); |
| 732 | if (!KindInt) |
| 733 | return false; |
| 734 | if (KindInt->getZExtValue() != ValueKind) |
| 735 | return false; |
| 736 | |
| 737 | // Get total count |
| 738 | ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2)); |
| 739 | if (!TotalCInt) |
| 740 | return false; |
| 741 | TotalC = TotalCInt->getZExtValue(); |
| 742 | |
| 743 | ActualNumValueData = 0; |
| 744 | |
| 745 | for (unsigned I = 3; I < NOps; I += 2) { |
| 746 | if (ActualNumValueData >= MaxNumValueData) |
| 747 | break; |
| 748 | ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I)); |
| 749 | ConstantInt *Count = |
| 750 | mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1)); |
| 751 | if (!Value || !Count) |
| 752 | return false; |
| 753 | ValueData[ActualNumValueData].Value = Value->getZExtValue(); |
| 754 | ValueData[ActualNumValueData].Count = Count->getZExtValue(); |
| 755 | ActualNumValueData++; |
| 756 | } |
| 757 | return true; |
| 758 | } |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 759 | |
Rong Xu | 92c2eae | 2016-04-01 20:15:04 +0000 | [diff] [blame] | 760 | MDNode *getPGOFuncNameMetadata(const Function &F) { |
| 761 | return F.getMetadata(getPGOFuncNameMetadataName()); |
| 762 | } |
| 763 | |
Rong Xu | f8f051c | 2016-04-22 21:00:17 +0000 | [diff] [blame] | 764 | void createPGOFuncNameMetadata(Function &F, const std::string &PGOFuncName) { |
| 765 | // Only for internal linkage functions. |
| 766 | if (PGOFuncName == F.getName()) |
| 767 | return; |
| 768 | // Don't create duplicated meta-data. |
| 769 | if (getPGOFuncNameMetadata(F)) |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 770 | return; |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 771 | LLVMContext &C = F.getContext(); |
Rong Xu | f8f051c | 2016-04-22 21:00:17 +0000 | [diff] [blame] | 772 | MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName.c_str())); |
Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 773 | F.setMetadata(getPGOFuncNameMetadataName(), N); |
| 774 | } |
| 775 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 776 | } // end namespace llvm |