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