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