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