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