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