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