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