| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1 | //===- PGOInstrumentation.cpp - MST-based PGO Instrumentation -------------===// |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +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 |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements PGO instrumentation using a minimum spanning tree based |
| 10 | // on the following paper: |
| 11 | // [1] Donald E. Knuth, Francis R. Stevenson. Optimal measurement of points |
| 12 | // for program frequency counts. BIT Numerical Mathematics 1973, Volume 13, |
| 13 | // Issue 3, pp 313-322 |
| 14 | // The idea of the algorithm based on the fact that for each node (except for |
| 15 | // the entry and exit), the sum of incoming edge counts equals the sum of |
| 16 | // outgoing edge counts. The count of edge on spanning tree can be derived from |
| 17 | // those edges not on the spanning tree. Knuth proves this method instruments |
| 18 | // the minimum number of edges. |
| 19 | // |
| 20 | // The minimal spanning tree here is actually a maximum weight tree -- on-tree |
| 21 | // edges have higher frequencies (more likely to execute). The idea is to |
| 22 | // instrument those less frequently executed edges to reduce the runtime |
| 23 | // overhead of instrumented binaries. |
| 24 | // |
| 25 | // This file contains two passes: |
| 26 | // (1) Pass PGOInstrumentationGen which instruments the IR to generate edge |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 27 | // count profile, and generates the instrumentation for indirect call |
| 28 | // profiling. |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 29 | // (2) Pass PGOInstrumentationUse which reads the edge count profile and |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 30 | // annotates the branch weights. It also reads the indirect call value |
| 31 | // profiling records and annotate the indirect call instructions. |
| 32 | // |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 33 | // To get the precise counter information, These two passes need to invoke at |
| 34 | // the same compilation point (so they see the same IR). For pass |
| 35 | // PGOInstrumentationGen, the real work is done in instrumentOneFunc(). For |
| 36 | // pass PGOInstrumentationUse, the real work in done in class PGOUseFunc and |
| 37 | // the profile is opened in module level and passed to each PGOUseFunc instance. |
| 38 | // The shared code for PGOInstrumentationGen and PGOInstrumentationUse is put |
| 39 | // in class FuncPGOInstrumentation. |
| 40 | // |
| 41 | // Class PGOEdge represents a CFG edge and some auxiliary information. Class |
| 42 | // BBInfo contains auxiliary information for each BB. These two classes are used |
| 43 | // in pass PGOInstrumentationGen. Class PGOUseEdge and UseBBInfo are the derived |
| 44 | // class of PGOEdge and BBInfo, respectively. They contains extra data structure |
| 45 | // used in populating profile counters. |
| 46 | // The MST implementation is in Class CFGMST (CFGMST.h). |
| 47 | // |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 50 | #include "CFGMST.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/APInt.h" |
| 52 | #include "llvm/ADT/ArrayRef.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/STLExtras.h" |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/SmallVector.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/Statistic.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/StringRef.h" |
| Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Triple.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/Twine.h" |
| 59 | #include "llvm/ADT/iterator.h" |
| 60 | #include "llvm/ADT/iterator_range.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 61 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 62 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 63 | #include "llvm/Analysis/CFG.h" |
| Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 64 | #include "llvm/Analysis/IndirectCallVisitor.h" |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 65 | #include "llvm/Analysis/LoopInfo.h" |
| Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 66 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 67 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 68 | #include "llvm/IR/Attributes.h" |
| 69 | #include "llvm/IR/BasicBlock.h" |
| 70 | #include "llvm/IR/CFG.h" |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 71 | #include "llvm/IR/CallSite.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 72 | #include "llvm/IR/Comdat.h" |
| 73 | #include "llvm/IR/Constant.h" |
| 74 | #include "llvm/IR/Constants.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 75 | #include "llvm/IR/DiagnosticInfo.h" |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 76 | #include "llvm/IR/Dominators.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 77 | #include "llvm/IR/Function.h" |
| 78 | #include "llvm/IR/GlobalAlias.h" |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 79 | #include "llvm/IR/GlobalValue.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 80 | #include "llvm/IR/GlobalVariable.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 81 | #include "llvm/IR/IRBuilder.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 82 | #include "llvm/IR/InstVisitor.h" |
| 83 | #include "llvm/IR/InstrTypes.h" |
| 84 | #include "llvm/IR/Instruction.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 85 | #include "llvm/IR/Instructions.h" |
| 86 | #include "llvm/IR/IntrinsicInst.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 87 | #include "llvm/IR/Intrinsics.h" |
| 88 | #include "llvm/IR/LLVMContext.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 89 | #include "llvm/IR/MDBuilder.h" |
| 90 | #include "llvm/IR/Module.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 91 | #include "llvm/IR/PassManager.h" |
| 92 | #include "llvm/IR/ProfileSummary.h" |
| 93 | #include "llvm/IR/Type.h" |
| 94 | #include "llvm/IR/Value.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 95 | #include "llvm/Pass.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 96 | #include "llvm/ProfileData/InstrProf.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 97 | #include "llvm/ProfileData/InstrProfReader.h" |
| 98 | #include "llvm/Support/BranchProbability.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 99 | #include "llvm/Support/Casting.h" |
| 100 | #include "llvm/Support/CommandLine.h" |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 101 | #include "llvm/Support/DOTGraphTraits.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 102 | #include "llvm/Support/Debug.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 103 | #include "llvm/Support/Error.h" |
| 104 | #include "llvm/Support/ErrorHandling.h" |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 105 | #include "llvm/Support/GraphWriter.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 106 | #include "llvm/Support/JamCRC.h" |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 107 | #include "llvm/Support/raw_ostream.h" |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 108 | #include "llvm/Transforms/Instrumentation.h" |
| Jordan Rupprecht | 090683b | 2019-03-04 22:54:44 +0000 | [diff] [blame] | 109 | #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 110 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 111 | #include <algorithm> |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 112 | #include <cassert> |
| 113 | #include <cstdint> |
| 114 | #include <memory> |
| 115 | #include <numeric> |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 116 | #include <string> |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 117 | #include <unordered_map> |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 118 | #include <utility> |
| 119 | #include <vector> |
| 120 | |
| 121 | using namespace llvm; |
| Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 122 | using ProfileCount = Function::ProfileCount; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 123 | |
| 124 | #define DEBUG_TYPE "pgo-instrumentation" |
| 125 | |
| 126 | STATISTIC(NumOfPGOInstrument, "Number of edges instrumented."); |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 127 | STATISTIC(NumOfPGOSelectInsts, "Number of select instruction instrumented."); |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 128 | STATISTIC(NumOfPGOMemIntrinsics, "Number of mem intrinsics instrumented."); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 129 | STATISTIC(NumOfPGOEdge, "Number of edges."); |
| 130 | STATISTIC(NumOfPGOBB, "Number of basic-blocks."); |
| 131 | STATISTIC(NumOfPGOSplit, "Number of critical edge splits."); |
| 132 | STATISTIC(NumOfPGOFunc, "Number of functions having valid profile counts."); |
| 133 | STATISTIC(NumOfPGOMismatch, "Number of functions having mismatch profile."); |
| 134 | STATISTIC(NumOfPGOMissing, "Number of functions without profile."); |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 135 | STATISTIC(NumOfPGOICall, "Number of indirect call value instrumentations."); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 136 | STATISTIC(NumOfCSPGOInstrument, "Number of edges instrumented in CSPGO."); |
| 137 | STATISTIC(NumOfCSPGOSelectInsts, |
| 138 | "Number of select instruction instrumented in CSPGO."); |
| 139 | STATISTIC(NumOfCSPGOMemIntrinsics, |
| 140 | "Number of mem intrinsics instrumented in CSPGO."); |
| 141 | STATISTIC(NumOfCSPGOEdge, "Number of edges in CSPGO."); |
| 142 | STATISTIC(NumOfCSPGOBB, "Number of basic-blocks in CSPGO."); |
| 143 | STATISTIC(NumOfCSPGOSplit, "Number of critical edge splits in CSPGO."); |
| 144 | STATISTIC(NumOfCSPGOFunc, |
| 145 | "Number of functions having valid profile counts in CSPGO."); |
| 146 | STATISTIC(NumOfCSPGOMismatch, |
| 147 | "Number of functions having mismatch profile in CSPGO."); |
| 148 | STATISTIC(NumOfCSPGOMissing, "Number of functions without profile in CSPGO."); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 149 | |
| 150 | // Command line option to specify the file to read profile from. This is |
| 151 | // mainly used for testing. |
| 152 | static cl::opt<std::string> |
| 153 | PGOTestProfileFile("pgo-test-profile-file", cl::init(""), cl::Hidden, |
| 154 | cl::value_desc("filename"), |
| 155 | cl::desc("Specify the path of profile data file. This is" |
| 156 | "mainly for test purpose.")); |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 157 | static cl::opt<std::string> PGOTestProfileRemappingFile( |
| 158 | "pgo-test-profile-remapping-file", cl::init(""), cl::Hidden, |
| 159 | cl::value_desc("filename"), |
| 160 | cl::desc("Specify the path of profile remapping file. This is mainly for " |
| 161 | "test purpose.")); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 162 | |
| Rong Xu | ecdc98f | 2016-03-04 22:08:44 +0000 | [diff] [blame] | 163 | // Command line option to disable value profiling. The default is false: |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 164 | // i.e. value profiling is enabled by default. This is for debug purpose. |
| Rong Xu | 9e926e8 | 2016-02-29 19:16:04 +0000 | [diff] [blame] | 165 | static cl::opt<bool> DisableValueProfiling("disable-vp", cl::init(false), |
| 166 | cl::Hidden, |
| 167 | cl::desc("Disable Value Profiling")); |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 168 | |
| Rong Xu | ecdc98f | 2016-03-04 22:08:44 +0000 | [diff] [blame] | 169 | // Command line option to set the maximum number of VP annotations to write to |
| Rong Xu | 08afb05 | 2016-04-28 17:31:22 +0000 | [diff] [blame] | 170 | // the metadata for a single indirect call callsite. |
| 171 | static cl::opt<unsigned> MaxNumAnnotations( |
| 172 | "icp-max-annotations", cl::init(3), cl::Hidden, cl::ZeroOrMore, |
| 173 | cl::desc("Max number of annotations for a single indirect " |
| 174 | "call callsite")); |
| Rong Xu | ecdc98f | 2016-03-04 22:08:44 +0000 | [diff] [blame] | 175 | |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 176 | // Command line option to set the maximum number of value annotations |
| 177 | // to write to the metadata for a single memop intrinsic. |
| 178 | static cl::opt<unsigned> MaxNumMemOPAnnotations( |
| 179 | "memop-max-annotations", cl::init(4), cl::Hidden, cl::ZeroOrMore, |
| 180 | cl::desc("Max number of preicise value annotations for a single memop" |
| 181 | "intrinsic")); |
| 182 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 183 | // Command line option to control appending FunctionHash to the name of a COMDAT |
| 184 | // function. This is to avoid the hash mismatch caused by the preinliner. |
| 185 | static cl::opt<bool> DoComdatRenaming( |
| Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 186 | "do-comdat-renaming", cl::init(false), cl::Hidden, |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 187 | cl::desc("Append function hash to the name of COMDAT function to avoid " |
| 188 | "function hash mismatch due to the preinliner")); |
| 189 | |
| Rong Xu | 0698de9 | 2016-05-13 17:26:06 +0000 | [diff] [blame] | 190 | // Command line option to enable/disable the warning about missing profile |
| 191 | // information. |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 192 | static cl::opt<bool> |
| 193 | PGOWarnMissing("pgo-warn-missing-function", cl::init(false), cl::Hidden, |
| 194 | cl::desc("Use this option to turn on/off " |
| 195 | "warnings about missing profile data for " |
| 196 | "functions.")); |
| Rong Xu | 0698de9 | 2016-05-13 17:26:06 +0000 | [diff] [blame] | 197 | |
| 198 | // Command line option to enable/disable the warning about a hash mismatch in |
| 199 | // the profile data. |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 200 | static cl::opt<bool> |
| 201 | NoPGOWarnMismatch("no-pgo-warn-mismatch", cl::init(false), cl::Hidden, |
| 202 | cl::desc("Use this option to turn off/on " |
| 203 | "warnings about profile cfg mismatch.")); |
| Rong Xu | 0698de9 | 2016-05-13 17:26:06 +0000 | [diff] [blame] | 204 | |
| Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 205 | // Command line option to enable/disable the warning about a hash mismatch in |
| 206 | // the profile data for Comdat functions, which often turns out to be false |
| 207 | // positive due to the pre-instrumentation inline. |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 208 | static cl::opt<bool> |
| 209 | NoPGOWarnMismatchComdat("no-pgo-warn-mismatch-comdat", cl::init(true), |
| 210 | cl::Hidden, |
| 211 | cl::desc("The option is used to turn on/off " |
| 212 | "warnings about hash mismatch for comdat " |
| 213 | "functions.")); |
| Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 214 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 215 | // Command line option to enable/disable select instruction instrumentation. |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 216 | static cl::opt<bool> |
| 217 | PGOInstrSelect("pgo-instr-select", cl::init(true), cl::Hidden, |
| 218 | cl::desc("Use this option to turn on/off SELECT " |
| 219 | "instruction instrumentation. ")); |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 220 | |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 221 | // Command line option to turn on CFG dot or text dump of raw profile counts |
| 222 | static cl::opt<PGOViewCountsType> PGOViewRawCounts( |
| 223 | "pgo-view-raw-counts", cl::Hidden, |
| 224 | cl::desc("A boolean option to show CFG dag or text " |
| 225 | "with raw profile counts from " |
| 226 | "profile data. See also option " |
| 227 | "-pgo-view-counts. To limit graph " |
| 228 | "display to only one function, use " |
| 229 | "filtering option -view-bfi-func-name."), |
| 230 | cl::values(clEnumValN(PGOVCT_None, "none", "do not show."), |
| 231 | clEnumValN(PGOVCT_Graph, "graph", "show a graph."), |
| 232 | clEnumValN(PGOVCT_Text, "text", "show in text."))); |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 233 | |
| Rong Xu | 8e06e80 | 2017-03-17 20:51:44 +0000 | [diff] [blame] | 234 | // Command line option to enable/disable memop intrinsic call.size profiling. |
| 235 | static cl::opt<bool> |
| 236 | PGOInstrMemOP("pgo-instr-memop", cl::init(true), cl::Hidden, |
| 237 | cl::desc("Use this option to turn on/off " |
| Teresa Johnson | cd2aa0d | 2017-05-24 17:55:25 +0000 | [diff] [blame] | 238 | "memory intrinsic size profiling.")); |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 239 | |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 240 | // Emit branch probability as optimization remarks. |
| 241 | static cl::opt<bool> |
| 242 | EmitBranchProbability("pgo-emit-branch-prob", cl::init(false), cl::Hidden, |
| 243 | cl::desc("When this option is on, the annotated " |
| 244 | "branch probability will be emitted as " |
| Rong Xu | 662f38b | 2018-03-27 18:55:56 +0000 | [diff] [blame] | 245 | "optimization remarks: -{Rpass|" |
| 246 | "pass-remarks}=pgo-instrumentation")); |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 247 | |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 248 | // Command line option to turn on CFG dot dump after profile annotation. |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 249 | // Defined in Analysis/BlockFrequencyInfo.cpp: -pgo-view-counts |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 250 | extern cl::opt<PGOViewCountsType> PGOViewCounts; |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 251 | |
| Xinliang David Li | 58fcc9b | 2017-02-02 21:29:17 +0000 | [diff] [blame] | 252 | // Command line option to specify the name of the function for CFG dump |
| 253 | // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name= |
| 254 | extern cl::opt<std::string> ViewBlockFreqFuncName; |
| 255 | |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 256 | // Return a string describing the branch condition that can be |
| 257 | // used in static branch probability heuristics: |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 258 | static std::string getBranchCondString(Instruction *TI) { |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 259 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
| 260 | if (!BI || !BI->isConditional()) |
| 261 | return std::string(); |
| 262 | |
| 263 | Value *Cond = BI->getCondition(); |
| 264 | ICmpInst *CI = dyn_cast<ICmpInst>(Cond); |
| 265 | if (!CI) |
| 266 | return std::string(); |
| 267 | |
| 268 | std::string result; |
| 269 | raw_string_ostream OS(result); |
| 270 | OS << CmpInst::getPredicateName(CI->getPredicate()) << "_"; |
| 271 | CI->getOperand(0)->getType()->print(OS, true); |
| 272 | |
| 273 | Value *RHS = CI->getOperand(1); |
| 274 | ConstantInt *CV = dyn_cast<ConstantInt>(RHS); |
| 275 | if (CV) { |
| 276 | if (CV->isZero()) |
| 277 | OS << "_Zero"; |
| 278 | else if (CV->isOne()) |
| 279 | OS << "_One"; |
| Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 280 | else if (CV->isMinusOne()) |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 281 | OS << "_MinusOne"; |
| 282 | else |
| 283 | OS << "_Const"; |
| 284 | } |
| 285 | OS.flush(); |
| 286 | return result; |
| 287 | } |
| 288 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 289 | namespace { |
| 290 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 291 | /// The select instruction visitor plays three roles specified |
| 292 | /// by the mode. In \c VM_counting mode, it simply counts the number of |
| 293 | /// select instructions. In \c VM_instrument mode, it inserts code to count |
| 294 | /// the number times TrueValue of select is taken. In \c VM_annotate mode, |
| 295 | /// it reads the profile data and annotate the select instruction with metadata. |
| 296 | enum VisitMode { VM_counting, VM_instrument, VM_annotate }; |
| 297 | class PGOUseFunc; |
| 298 | |
| 299 | /// Instruction Visitor class to visit select instructions. |
| 300 | struct SelectInstVisitor : public InstVisitor<SelectInstVisitor> { |
| 301 | Function &F; |
| 302 | unsigned NSIs = 0; // Number of select instructions instrumented. |
| 303 | VisitMode Mode = VM_counting; // Visiting mode. |
| 304 | unsigned *CurCtrIdx = nullptr; // Pointer to current counter index. |
| 305 | unsigned TotalNumCtrs = 0; // Total number of counters |
| 306 | GlobalVariable *FuncNameVar = nullptr; |
| 307 | uint64_t FuncHash = 0; |
| 308 | PGOUseFunc *UseFunc = nullptr; |
| 309 | |
| 310 | SelectInstVisitor(Function &Func) : F(Func) {} |
| 311 | |
| 312 | void countSelects(Function &Func) { |
| Vitaly Buka | ca6ecd2 | 2017-03-15 23:07:41 +0000 | [diff] [blame] | 313 | NSIs = 0; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 314 | Mode = VM_counting; |
| 315 | visit(Func); |
| 316 | } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 317 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 318 | // Visit the IR stream and instrument all select instructions. \p |
| 319 | // Ind is a pointer to the counter index variable; \p TotalNC |
| 320 | // is the total number of counters; \p FNV is the pointer to the |
| 321 | // PGO function name var; \p FHash is the function hash. |
| 322 | void instrumentSelects(Function &Func, unsigned *Ind, unsigned TotalNC, |
| 323 | GlobalVariable *FNV, uint64_t FHash) { |
| 324 | Mode = VM_instrument; |
| 325 | CurCtrIdx = Ind; |
| 326 | TotalNumCtrs = TotalNC; |
| 327 | FuncHash = FHash; |
| 328 | FuncNameVar = FNV; |
| 329 | visit(Func); |
| 330 | } |
| 331 | |
| 332 | // Visit the IR stream and annotate all select instructions. |
| 333 | void annotateSelects(Function &Func, PGOUseFunc *UF, unsigned *Ind) { |
| 334 | Mode = VM_annotate; |
| 335 | UseFunc = UF; |
| 336 | CurCtrIdx = Ind; |
| 337 | visit(Func); |
| 338 | } |
| 339 | |
| 340 | void instrumentOneSelectInst(SelectInst &SI); |
| 341 | void annotateOneSelectInst(SelectInst &SI); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 342 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 343 | // Visit \p SI instruction and perform tasks according to visit mode. |
| 344 | void visitSelectInst(SelectInst &SI); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 345 | |
| Vitaly Buka | ca6ecd2 | 2017-03-15 23:07:41 +0000 | [diff] [blame] | 346 | // Return the number of select instructions. This needs be called after |
| 347 | // countSelects(). |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 348 | unsigned getNumOfSelectInsts() const { return NSIs; } |
| 349 | }; |
| 350 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 351 | /// Instruction Visitor class to visit memory intrinsic calls. |
| 352 | struct MemIntrinsicVisitor : public InstVisitor<MemIntrinsicVisitor> { |
| 353 | Function &F; |
| 354 | unsigned NMemIs = 0; // Number of memIntrinsics instrumented. |
| 355 | VisitMode Mode = VM_counting; // Visiting mode. |
| 356 | unsigned CurCtrId = 0; // Current counter index. |
| 357 | unsigned TotalNumCtrs = 0; // Total number of counters |
| 358 | GlobalVariable *FuncNameVar = nullptr; |
| 359 | uint64_t FuncHash = 0; |
| 360 | PGOUseFunc *UseFunc = nullptr; |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 361 | std::vector<Instruction *> Candidates; |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 362 | |
| 363 | MemIntrinsicVisitor(Function &Func) : F(Func) {} |
| 364 | |
| 365 | void countMemIntrinsics(Function &Func) { |
| 366 | NMemIs = 0; |
| 367 | Mode = VM_counting; |
| 368 | visit(Func); |
| 369 | } |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 370 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 371 | void instrumentMemIntrinsics(Function &Func, unsigned TotalNC, |
| 372 | GlobalVariable *FNV, uint64_t FHash) { |
| 373 | Mode = VM_instrument; |
| 374 | TotalNumCtrs = TotalNC; |
| 375 | FuncHash = FHash; |
| 376 | FuncNameVar = FNV; |
| 377 | visit(Func); |
| 378 | } |
| 379 | |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 380 | std::vector<Instruction *> findMemIntrinsics(Function &Func) { |
| 381 | Candidates.clear(); |
| 382 | Mode = VM_annotate; |
| 383 | visit(Func); |
| 384 | return Candidates; |
| 385 | } |
| 386 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 387 | // Visit the IR stream and annotate all mem intrinsic call instructions. |
| 388 | void instrumentOneMemIntrinsic(MemIntrinsic &MI); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 389 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 390 | // Visit \p MI instruction and perform tasks according to visit mode. |
| 391 | void visitMemIntrinsic(MemIntrinsic &SI); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 392 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 393 | unsigned getNumOfMemIntrinsics() const { return NMemIs; } |
| 394 | }; |
| 395 | |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 396 | class PGOInstrumentationGenLegacyPass : public ModulePass { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 397 | public: |
| 398 | static char ID; |
| 399 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 400 | PGOInstrumentationGenLegacyPass(bool IsCS = false) |
| 401 | : ModulePass(ID), IsCS(IsCS) { |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 402 | initializePGOInstrumentationGenLegacyPassPass( |
| 403 | *PassRegistry::getPassRegistry()); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 406 | StringRef getPassName() const override { return "PGOInstrumentationGenPass"; } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 407 | |
| 408 | private: |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 409 | // Is this is context-sensitive instrumentation. |
| 410 | bool IsCS; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 411 | bool runOnModule(Module &M) override; |
| 412 | |
| 413 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 414 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 415 | } |
| 416 | }; |
| 417 | |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 418 | class PGOInstrumentationUseLegacyPass : public ModulePass { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 419 | public: |
| 420 | static char ID; |
| 421 | |
| 422 | // Provide the profile filename as the parameter. |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 423 | PGOInstrumentationUseLegacyPass(std::string Filename = "", bool IsCS = false) |
| 424 | : ModulePass(ID), ProfileFileName(std::move(Filename)), IsCS(IsCS) { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 425 | if (!PGOTestProfileFile.empty()) |
| 426 | ProfileFileName = PGOTestProfileFile; |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 427 | initializePGOInstrumentationUseLegacyPassPass( |
| 428 | *PassRegistry::getPassRegistry()); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 431 | StringRef getPassName() const override { return "PGOInstrumentationUsePass"; } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 432 | |
| 433 | private: |
| 434 | std::string ProfileFileName; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 435 | // Is this is context-sensitive instrumentation use. |
| 436 | bool IsCS; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 437 | |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 438 | bool runOnModule(Module &M) override; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 439 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 440 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 441 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 442 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 443 | } |
| 444 | }; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 445 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 446 | class PGOInstrumentationGenCreateVarLegacyPass : public ModulePass { |
| 447 | public: |
| 448 | static char ID; |
| 449 | StringRef getPassName() const override { |
| 450 | return "PGOInstrumentationGenCreateVarPass"; |
| 451 | } |
| 452 | PGOInstrumentationGenCreateVarLegacyPass(std::string CSInstrName = "") |
| 453 | : ModulePass(ID), InstrProfileOutput(CSInstrName) { |
| 454 | initializePGOInstrumentationGenCreateVarLegacyPassPass( |
| 455 | *PassRegistry::getPassRegistry()); |
| 456 | } |
| 457 | |
| 458 | private: |
| 459 | bool runOnModule(Module &M) override { |
| 460 | createProfileFileNameVar(M, InstrProfileOutput); |
| 461 | createIRLevelProfileFlagVar(M, true); |
| 462 | return false; |
| 463 | } |
| 464 | std::string InstrProfileOutput; |
| 465 | }; |
| 466 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 467 | } // end anonymous namespace |
| 468 | |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 469 | char PGOInstrumentationGenLegacyPass::ID = 0; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 470 | |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 471 | INITIALIZE_PASS_BEGIN(PGOInstrumentationGenLegacyPass, "pgo-instr-gen", |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 472 | "PGO instrumentation.", false, false) |
| 473 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 474 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 475 | INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen", |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 476 | "PGO instrumentation.", false, false) |
| 477 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 478 | ModulePass *llvm::createPGOInstrumentationGenLegacyPass(bool IsCS) { |
| 479 | return new PGOInstrumentationGenLegacyPass(IsCS); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 482 | char PGOInstrumentationUseLegacyPass::ID = 0; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 483 | |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 484 | INITIALIZE_PASS_BEGIN(PGOInstrumentationUseLegacyPass, "pgo-instr-use", |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 485 | "Read PGO instrumentation profile.", false, false) |
| 486 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 487 | INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass) |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 488 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 489 | INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use", |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 490 | "Read PGO instrumentation profile.", false, false) |
| 491 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 492 | ModulePass *llvm::createPGOInstrumentationUseLegacyPass(StringRef Filename, |
| 493 | bool IsCS) { |
| 494 | return new PGOInstrumentationUseLegacyPass(Filename.str(), IsCS); |
| 495 | } |
| 496 | |
| 497 | char PGOInstrumentationGenCreateVarLegacyPass::ID = 0; |
| 498 | |
| 499 | INITIALIZE_PASS(PGOInstrumentationGenCreateVarLegacyPass, |
| 500 | "pgo-instr-gen-create-var", |
| 501 | "Create PGO instrumentation version variable for CSPGO.", false, |
| 502 | false) |
| 503 | |
| 504 | ModulePass * |
| 505 | llvm::createPGOInstrumentationGenCreateVarLegacyPass(StringRef CSInstrName) { |
| 506 | return new PGOInstrumentationGenCreateVarLegacyPass(CSInstrName); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | namespace { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 510 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 511 | /// An MST based instrumentation for PGO |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 512 | /// |
| 513 | /// Implements a Minimum Spanning Tree (MST) based instrumentation for PGO |
| 514 | /// in the function level. |
| 515 | struct PGOEdge { |
| 516 | // This class implements the CFG edges. Note the CFG can be a multi-graph. |
| 517 | // So there might be multiple edges with same SrcBB and DestBB. |
| 518 | const BasicBlock *SrcBB; |
| 519 | const BasicBlock *DestBB; |
| 520 | uint64_t Weight; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 521 | bool InMST = false; |
| 522 | bool Removed = false; |
| 523 | bool IsCritical = false; |
| 524 | |
| Xinliang David Li | fa3f1a1 | 2017-12-10 07:39:53 +0000 | [diff] [blame] | 525 | PGOEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1) |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 526 | : SrcBB(Src), DestBB(Dest), Weight(W) {} |
| 527 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 528 | // Return the information string of an edge. |
| 529 | const std::string infoString() const { |
| 530 | return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") + |
| 531 | (IsCritical ? "c" : " ") + " W=" + Twine(Weight)).str(); |
| 532 | } |
| 533 | }; |
| 534 | |
| 535 | // This class stores the auxiliary information for each BB. |
| 536 | struct BBInfo { |
| 537 | BBInfo *Group; |
| 538 | uint32_t Index; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 539 | uint32_t Rank = 0; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 540 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 541 | BBInfo(unsigned IX) : Group(this), Index(IX) {} |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 542 | |
| 543 | // Return the information string of this object. |
| 544 | const std::string infoString() const { |
| 545 | return (Twine("Index=") + Twine(Index)).str(); |
| 546 | } |
| 547 | }; |
| 548 | |
| 549 | // This class implements the CFG edges. Note the CFG can be a multi-graph. |
| 550 | template <class Edge, class BBInfo> class FuncPGOInstrumentation { |
| 551 | private: |
| 552 | Function &F; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 553 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 554 | // Is this is context-sensitive instrumentation. |
| 555 | bool IsCS; |
| 556 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 557 | // A map that stores the Comdat group in function F. |
| 558 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 559 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 560 | void computeCFGHash(); |
| 561 | void renameComdatFunction(); |
| 562 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 563 | public: |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 564 | std::vector<std::vector<Instruction *>> ValueSites; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 565 | SelectInstVisitor SIVisitor; |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 566 | MemIntrinsicVisitor MIVisitor; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 567 | std::string FuncName; |
| 568 | GlobalVariable *FuncNameVar; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 569 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 570 | // CFG hash value for this function. |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 571 | uint64_t FunctionHash = 0; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 572 | |
| 573 | // The Minimum Spanning Tree of function CFG. |
| 574 | CFGMST<Edge, BBInfo> MST; |
| 575 | |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 576 | // Collect all the BBs that will be instrumented, and store them in |
| 577 | // InstrumentBBs. |
| 578 | void getInstrumentBBs(std::vector<BasicBlock *> &InstrumentBBs); |
| 579 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 580 | // Give an edge, find the BB that will be instrumented. |
| 581 | // Return nullptr if there is no BB to be instrumented. |
| 582 | BasicBlock *getInstrBB(Edge *E); |
| 583 | |
| 584 | // Return the auxiliary BB information. |
| 585 | BBInfo &getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); } |
| 586 | |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 587 | // Return the auxiliary BB information if available. |
| 588 | BBInfo *findBBInfo(const BasicBlock *BB) const { return MST.findBBInfo(BB); } |
| 589 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 590 | // Dump edges and BB information. |
| 591 | void dumpInfo(std::string Str = "") const { |
| 592 | MST.dumpEdges(dbgs(), Twine("Dump Function ") + FuncName + " Hash: " + |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 593 | Twine(FunctionHash) + "\t" + Str); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 596 | FuncPGOInstrumentation( |
| 597 | Function &Func, |
| 598 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers, |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 599 | bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 600 | BlockFrequencyInfo *BFI = nullptr, bool IsCS = false) |
| 601 | : F(Func), IsCS(IsCS), ComdatMembers(ComdatMembers), |
| 602 | ValueSites(IPVK_Last + 1), SIVisitor(Func), MIVisitor(Func), |
| 603 | MST(F, BPI, BFI) { |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 604 | // This should be done before CFG hash computation. |
| 605 | SIVisitor.countSelects(Func); |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 606 | MIVisitor.countMemIntrinsics(Func); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 607 | if (!IsCS) { |
| 608 | NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts(); |
| 609 | NumOfPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics(); |
| 610 | NumOfPGOBB += MST.BBInfos.size(); |
| 611 | ValueSites[IPVK_IndirectCallTarget] = findIndirectCalls(Func); |
| 612 | } else { |
| 613 | NumOfCSPGOSelectInsts += SIVisitor.getNumOfSelectInsts(); |
| 614 | NumOfCSPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics(); |
| 615 | NumOfCSPGOBB += MST.BBInfos.size(); |
| 616 | } |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 617 | ValueSites[IPVK_MemOPSize] = MIVisitor.findMemIntrinsics(Func); |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 618 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 619 | FuncName = getPGOFuncName(F); |
| 620 | computeCFGHash(); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 621 | if (!ComdatMembers.empty()) |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 622 | renameComdatFunction(); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 623 | LLVM_DEBUG(dumpInfo("after CFGMST")); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 624 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 625 | for (auto &E : MST.AllEdges) { |
| 626 | if (E->Removed) |
| 627 | continue; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 628 | IsCS ? NumOfCSPGOEdge++ : NumOfPGOEdge++; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 629 | if (!E->InMST) |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 630 | IsCS ? NumOfCSPGOInstrument++ : NumOfPGOInstrument++; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | if (CreateGlobalVar) |
| 634 | FuncNameVar = createPGOFuncNameVar(F, FuncName); |
| Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 635 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 636 | }; |
| 637 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 638 | } // end anonymous namespace |
| 639 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 640 | // Compute Hash value for the CFG: the lower 32 bits are CRC32 of the index |
| 641 | // value of each BB in the CFG. The higher 32 bits record the number of edges. |
| 642 | template <class Edge, class BBInfo> |
| 643 | void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() { |
| 644 | std::vector<char> Indexes; |
| 645 | JamCRC JC; |
| 646 | for (auto &BB : F) { |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 647 | const Instruction *TI = BB.getTerminator(); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 648 | for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) { |
| 649 | BasicBlock *Succ = TI->getSuccessor(I); |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 650 | auto BI = findBBInfo(Succ); |
| 651 | if (BI == nullptr) |
| 652 | continue; |
| 653 | uint32_t Index = BI->Index; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 654 | for (int J = 0; J < 4; J++) |
| 655 | Indexes.push_back((char)(Index >> (J * 8))); |
| 656 | } |
| 657 | } |
| 658 | JC.update(Indexes); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 659 | |
| 660 | // Hash format for context sensitive profile. Reserve 4 bits for other |
| 661 | // information. |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 662 | FunctionHash = (uint64_t)SIVisitor.getNumOfSelectInsts() << 56 | |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 663 | (uint64_t)ValueSites[IPVK_IndirectCallTarget].size() << 48 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 664 | //(uint64_t)ValueSites[IPVK_MemOPSize].size() << 40 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 665 | (uint64_t)MST.AllEdges.size() << 32 | JC.getCRC(); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 666 | // Reserve bit 60-63 for other information purpose. |
| 667 | FunctionHash &= 0x0FFFFFFFFFFFFFFF; |
| 668 | if (IsCS) |
| 669 | NamedInstrProfRecord::setCSFlagInHash(FunctionHash); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 670 | LLVM_DEBUG(dbgs() << "Function Hash Computation for " << F.getName() << ":\n" |
| 671 | << " CRC = " << JC.getCRC() |
| 672 | << ", Selects = " << SIVisitor.getNumOfSelectInsts() |
| 673 | << ", Edges = " << MST.AllEdges.size() << ", ICSites = " |
| 674 | << ValueSites[IPVK_IndirectCallTarget].size() |
| 675 | << ", Hash = " << FunctionHash << "\n";); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | // Check if we can safely rename this Comdat function. |
| 679 | static bool canRenameComdat( |
| 680 | Function &F, |
| 681 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers) { |
| Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 682 | if (!DoComdatRenaming || !canRenameComdatFunc(F, true)) |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 683 | return false; |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 684 | |
| 685 | // FIXME: Current only handle those Comdat groups that only containing one |
| 686 | // function and function aliases. |
| 687 | // (1) For a Comdat group containing multiple functions, we need to have a |
| 688 | // unique postfix based on the hashes for each function. There is a |
| 689 | // non-trivial code refactoring to do this efficiently. |
| 690 | // (2) Variables can not be renamed, so we can not rename Comdat function in a |
| 691 | // group including global vars. |
| 692 | Comdat *C = F.getComdat(); |
| 693 | for (auto &&CM : make_range(ComdatMembers.equal_range(C))) { |
| 694 | if (dyn_cast<GlobalAlias>(CM.second)) |
| 695 | continue; |
| 696 | Function *FM = dyn_cast<Function>(CM.second); |
| 697 | if (FM != &F) |
| 698 | return false; |
| 699 | } |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | // Append the CFGHash to the Comdat function name. |
| 704 | template <class Edge, class BBInfo> |
| 705 | void FuncPGOInstrumentation<Edge, BBInfo>::renameComdatFunction() { |
| 706 | if (!canRenameComdat(F, ComdatMembers)) |
| 707 | return; |
| Rong Xu | 0e79f7d | 2016-10-06 20:38:13 +0000 | [diff] [blame] | 708 | std::string OrigName = F.getName().str(); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 709 | std::string NewFuncName = |
| 710 | Twine(F.getName() + "." + Twine(FunctionHash)).str(); |
| 711 | F.setName(Twine(NewFuncName)); |
| Rong Xu | 0e79f7d | 2016-10-06 20:38:13 +0000 | [diff] [blame] | 712 | GlobalAlias::create(GlobalValue::WeakAnyLinkage, OrigName, &F); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 713 | FuncName = Twine(FuncName + "." + Twine(FunctionHash)).str(); |
| 714 | Comdat *NewComdat; |
| 715 | Module *M = F.getParent(); |
| 716 | // For AvailableExternallyLinkage functions, change the linkage to |
| 717 | // LinkOnceODR and put them into comdat. This is because after renaming, there |
| 718 | // is no backup external copy available for the function. |
| 719 | if (!F.hasComdat()) { |
| 720 | assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage); |
| 721 | NewComdat = M->getOrInsertComdat(StringRef(NewFuncName)); |
| 722 | F.setLinkage(GlobalValue::LinkOnceODRLinkage); |
| 723 | F.setComdat(NewComdat); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | // This function belongs to a single function Comdat group. |
| 728 | Comdat *OrigComdat = F.getComdat(); |
| 729 | std::string NewComdatName = |
| 730 | Twine(OrigComdat->getName() + "." + Twine(FunctionHash)).str(); |
| 731 | NewComdat = M->getOrInsertComdat(StringRef(NewComdatName)); |
| 732 | NewComdat->setSelectionKind(OrigComdat->getSelectionKind()); |
| 733 | |
| 734 | for (auto &&CM : make_range(ComdatMembers.equal_range(OrigComdat))) { |
| 735 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(CM.second)) { |
| 736 | // For aliases, change the name directly. |
| 737 | assert(dyn_cast<Function>(GA->getAliasee()->stripPointerCasts()) == &F); |
| Rong Xu | 0e79f7d | 2016-10-06 20:38:13 +0000 | [diff] [blame] | 738 | std::string OrigGAName = GA->getName().str(); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 739 | GA->setName(Twine(GA->getName() + "." + Twine(FunctionHash))); |
| Rong Xu | 0e79f7d | 2016-10-06 20:38:13 +0000 | [diff] [blame] | 740 | GlobalAlias::create(GlobalValue::WeakAnyLinkage, OrigGAName, GA); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 741 | continue; |
| 742 | } |
| 743 | // Must be a function. |
| 744 | Function *CF = dyn_cast<Function>(CM.second); |
| 745 | assert(CF); |
| 746 | CF->setComdat(NewComdat); |
| 747 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 750 | // Collect all the BBs that will be instruments and return them in |
| 751 | // InstrumentBBs. |
| 752 | template <class Edge, class BBInfo> |
| 753 | void FuncPGOInstrumentation<Edge, BBInfo>::getInstrumentBBs( |
| 754 | std::vector<BasicBlock *> &InstrumentBBs) { |
| 755 | // Use a worklist as we will update the vector during the iteration. |
| 756 | std::vector<Edge *> EdgeList; |
| 757 | EdgeList.reserve(MST.AllEdges.size()); |
| 758 | for (auto &E : MST.AllEdges) |
| 759 | EdgeList.push_back(E.get()); |
| 760 | |
| 761 | for (auto &E : EdgeList) { |
| 762 | BasicBlock *InstrBB = getInstrBB(E); |
| 763 | if (InstrBB) |
| 764 | InstrumentBBs.push_back(InstrBB); |
| 765 | } |
| 766 | } |
| 767 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 768 | // Given a CFG E to be instrumented, find which BB to place the instrumented |
| 769 | // code. The function will split the critical edge if necessary. |
| 770 | template <class Edge, class BBInfo> |
| 771 | BasicBlock *FuncPGOInstrumentation<Edge, BBInfo>::getInstrBB(Edge *E) { |
| 772 | if (E->InMST || E->Removed) |
| 773 | return nullptr; |
| 774 | |
| 775 | BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB); |
| 776 | BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB); |
| 777 | // For a fake edge, instrument the real BB. |
| 778 | if (SrcBB == nullptr) |
| 779 | return DestBB; |
| 780 | if (DestBB == nullptr) |
| 781 | return SrcBB; |
| 782 | |
| 783 | // Instrument the SrcBB if it has a single successor, |
| 784 | // otherwise, the DestBB if this is not a critical edge. |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 785 | Instruction *TI = SrcBB->getTerminator(); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 786 | if (TI->getNumSuccessors() <= 1) |
| 787 | return SrcBB; |
| 788 | if (!E->IsCritical) |
| 789 | return DestBB; |
| 790 | |
| 791 | // For a critical edge, we have to split. Instrument the newly |
| 792 | // created BB. |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 793 | IsCS ? NumOfCSPGOSplit++ : NumOfPGOSplit++; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 794 | LLVM_DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB).Index |
| 795 | << " --> " << getBBInfo(DestBB).Index << "\n"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 796 | unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB); |
| 797 | BasicBlock *InstrBB = SplitCriticalEdge(TI, SuccNum); |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 798 | if (!InstrBB) { |
| 799 | LLVM_DEBUG( |
| 800 | dbgs() << "Fail to split critical edge: not instrument this edge.\n"); |
| 801 | return nullptr; |
| 802 | } |
| 803 | // Need to add two new edges. First one: Add new edge of SrcBB->InstrBB. |
| 804 | MST.addEdge(SrcBB, InstrBB, 0); |
| 805 | // Second one: Add new edge of InstrBB->DestBB. |
| 806 | Edge &NewEdge1 = MST.addEdge(InstrBB, DestBB, 0); |
| 807 | NewEdge1.InMST = true; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 808 | E->Removed = true; |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 809 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 810 | return InstrBB; |
| 811 | } |
| 812 | |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 813 | // Visit all edge and instrument the edges not in MST, and do value profiling. |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 814 | // Critical edges will be split. |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 815 | static void instrumentOneFunc( |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 816 | Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 817 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers, |
| 818 | bool IsCS) { |
| Hiroshi Yamauchi | f3bda1d | 2017-12-12 19:07:43 +0000 | [diff] [blame] | 819 | // Split indirectbr critical edges here before computing the MST rather than |
| 820 | // later in getInstrBB() to avoid invalidating it. |
| 821 | SplitIndirectBrCriticalEdges(F, BPI, BFI); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 822 | |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 823 | FuncPGOInstrumentation<PGOEdge, BBInfo> FuncInfo(F, ComdatMembers, true, BPI, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 824 | BFI, IsCS); |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 825 | std::vector<BasicBlock *> InstrumentBBs; |
| 826 | FuncInfo.getInstrumentBBs(InstrumentBBs); |
| 827 | unsigned NumCounters = |
| 828 | InstrumentBBs.size() + FuncInfo.SIVisitor.getNumOfSelectInsts(); |
| Xinliang David Li | d119761 | 2016-08-01 20:25:06 +0000 | [diff] [blame] | 829 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 830 | uint32_t I = 0; |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 831 | Type *I8PtrTy = Type::getInt8PtrTy(M->getContext()); |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 832 | for (auto *InstrBB : InstrumentBBs) { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 833 | IRBuilder<> Builder(InstrBB, InstrBB->getFirstInsertionPt()); |
| 834 | assert(Builder.GetInsertPoint() != InstrBB->end() && |
| 835 | "Cannot get the Instrumentation point"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 836 | Builder.CreateCall( |
| 837 | Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment), |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 838 | {ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy), |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 839 | Builder.getInt64(FuncInfo.FunctionHash), Builder.getInt32(NumCounters), |
| 840 | Builder.getInt32(I++)}); |
| 841 | } |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 842 | |
| 843 | // Now instrument select instructions: |
| 844 | FuncInfo.SIVisitor.instrumentSelects(F, &I, NumCounters, FuncInfo.FuncNameVar, |
| 845 | FuncInfo.FunctionHash); |
| Xinliang David Li | d119761 | 2016-08-01 20:25:06 +0000 | [diff] [blame] | 846 | assert(I == NumCounters); |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 847 | |
| 848 | if (DisableValueProfiling) |
| 849 | return; |
| 850 | |
| Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 851 | unsigned NumIndirectCalls = 0; |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 852 | for (auto &I : FuncInfo.ValueSites[IPVK_IndirectCallTarget]) { |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 853 | CallSite CS(I); |
| 854 | Value *Callee = CS.getCalledValue(); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 855 | LLVM_DEBUG(dbgs() << "Instrument one indirect call: CallSite Index = " |
| Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 856 | << NumIndirectCalls << "\n"); |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 857 | IRBuilder<> Builder(I); |
| 858 | assert(Builder.GetInsertPoint() != I->getParent()->end() && |
| 859 | "Cannot get the Instrumentation point"); |
| 860 | Builder.CreateCall( |
| 861 | Intrinsic::getDeclaration(M, Intrinsic::instrprof_value_profile), |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 862 | {ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy), |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 863 | Builder.getInt64(FuncInfo.FunctionHash), |
| 864 | Builder.CreatePtrToInt(Callee, Builder.getInt64Ty()), |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 865 | Builder.getInt32(IPVK_IndirectCallTarget), |
| Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 866 | Builder.getInt32(NumIndirectCalls++)}); |
| Rong Xu | ed9fec7 | 2016-01-21 18:11:44 +0000 | [diff] [blame] | 867 | } |
| Chandler Carruth | 57578aa | 2019-01-07 07:15:51 +0000 | [diff] [blame] | 868 | NumOfPGOICall += NumIndirectCalls; |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 869 | |
| 870 | // Now instrument memop intrinsic calls. |
| 871 | FuncInfo.MIVisitor.instrumentMemIntrinsics( |
| 872 | F, NumCounters, FuncInfo.FuncNameVar, FuncInfo.FunctionHash); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 875 | namespace { |
| 876 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 877 | // This class represents a CFG edge in profile use compilation. |
| 878 | struct PGOUseEdge : public PGOEdge { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 879 | bool CountValid = false; |
| 880 | uint64_t CountValue = 0; |
| 881 | |
| Xinliang David Li | fa3f1a1 | 2017-12-10 07:39:53 +0000 | [diff] [blame] | 882 | PGOUseEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1) |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 883 | : PGOEdge(Src, Dest, W) {} |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 884 | |
| 885 | // Set edge count value |
| 886 | void setEdgeCount(uint64_t Value) { |
| 887 | CountValue = Value; |
| 888 | CountValid = true; |
| 889 | } |
| 890 | |
| 891 | // Return the information string for this object. |
| 892 | const std::string infoString() const { |
| 893 | if (!CountValid) |
| 894 | return PGOEdge::infoString(); |
| Rong Xu | 9e926e8 | 2016-02-29 19:16:04 +0000 | [diff] [blame] | 895 | return (Twine(PGOEdge::infoString()) + " Count=" + Twine(CountValue)) |
| 896 | .str(); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 897 | } |
| 898 | }; |
| 899 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 900 | using DirectEdges = SmallVector<PGOUseEdge *, 2>; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 901 | |
| 902 | // This class stores the auxiliary information for each BB. |
| 903 | struct UseBBInfo : public BBInfo { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 904 | uint64_t CountValue = 0; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 905 | bool CountValid; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 906 | int32_t UnknownCountInEdge = 0; |
| 907 | int32_t UnknownCountOutEdge = 0; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 908 | DirectEdges InEdges; |
| 909 | DirectEdges OutEdges; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 910 | |
| 911 | UseBBInfo(unsigned IX) : BBInfo(IX), CountValid(false) {} |
| 912 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 913 | UseBBInfo(unsigned IX, uint64_t C) |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 914 | : BBInfo(IX), CountValue(C), CountValid(true) {} |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 915 | |
| 916 | // Set the profile count value for this BB. |
| 917 | void setBBInfoCount(uint64_t Value) { |
| 918 | CountValue = Value; |
| 919 | CountValid = true; |
| 920 | } |
| 921 | |
| 922 | // Return the information string of this object. |
| 923 | const std::string infoString() const { |
| 924 | if (!CountValid) |
| 925 | return BBInfo::infoString(); |
| 926 | return (Twine(BBInfo::infoString()) + " Count=" + Twine(CountValue)).str(); |
| 927 | } |
| 928 | }; |
| 929 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 930 | } // end anonymous namespace |
| 931 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 932 | // Sum up the count values for all the edges. |
| 933 | static uint64_t sumEdgeCount(const ArrayRef<PGOUseEdge *> Edges) { |
| 934 | uint64_t Total = 0; |
| 935 | for (auto &E : Edges) { |
| 936 | if (E->Removed) |
| 937 | continue; |
| 938 | Total += E->CountValue; |
| 939 | } |
| 940 | return Total; |
| 941 | } |
| 942 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 943 | namespace { |
| 944 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 945 | class PGOUseFunc { |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 946 | public: |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 947 | PGOUseFunc(Function &Func, Module *Modu, |
| 948 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers, |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 949 | BranchProbabilityInfo *BPI = nullptr, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 950 | BlockFrequencyInfo *BFIin = nullptr, bool IsCS = false) |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 951 | : F(Func), M(Modu), BFI(BFIin), |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 952 | FuncInfo(Func, ComdatMembers, false, BPI, BFIin, IsCS), |
| 953 | FreqAttr(FFA_Normal), IsCS(IsCS) {} |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 954 | |
| 955 | // Read counts for the instrumented BB from profile. |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 956 | bool readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros); |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 957 | |
| 958 | // Populate the counts for all BBs. |
| 959 | void populateCounters(); |
| 960 | |
| 961 | // Set the branch weights based on the count values. |
| 962 | void setBranchWeights(); |
| 963 | |
| Hiroshi Inoue | ae17900 | 2018-04-14 08:59:00 +0000 | [diff] [blame] | 964 | // Annotate the value profile call sites for all value kind. |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 965 | void annotateValueSites(); |
| 966 | |
| 967 | // Annotate the value profile call sites for one value kind. |
| 968 | void annotateValueSites(uint32_t Kind); |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 969 | |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 970 | // Annotate the irreducible loop header weights. |
| 971 | void annotateIrrLoopHeaderWeights(); |
| 972 | |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 973 | // The hotness of the function from the profile count. |
| 974 | enum FuncFreqAttr { FFA_Normal, FFA_Cold, FFA_Hot }; |
| 975 | |
| 976 | // Return the function hotness from the profile. |
| 977 | FuncFreqAttr getFuncFreqAttr() const { return FreqAttr; } |
| 978 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 979 | // Return the function hash. |
| 980 | uint64_t getFuncHash() const { return FuncInfo.FunctionHash; } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 981 | |
| Easwaran Raman | 5fe04a1 | 2016-05-26 22:57:11 +0000 | [diff] [blame] | 982 | // Return the profile record for this function; |
| 983 | InstrProfRecord &getProfileRecord() { return ProfileRecord; } |
| 984 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 985 | // Return the auxiliary BB information. |
| 986 | UseBBInfo &getBBInfo(const BasicBlock *BB) const { |
| 987 | return FuncInfo.getBBInfo(BB); |
| 988 | } |
| 989 | |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 990 | // Return the auxiliary BB information if available. |
| 991 | UseBBInfo *findBBInfo(const BasicBlock *BB) const { |
| 992 | return FuncInfo.findBBInfo(BB); |
| 993 | } |
| 994 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 995 | Function &getFunc() const { return F; } |
| 996 | |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 997 | void dumpInfo(std::string Str = "") const { |
| 998 | FuncInfo.dumpInfo(Str); |
| 999 | } |
| 1000 | |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 1001 | uint64_t getProgramMaxCount() const { return ProgramMaxCount; } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1002 | private: |
| 1003 | Function &F; |
| 1004 | Module *M; |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1005 | BlockFrequencyInfo *BFI; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1006 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1007 | // This member stores the shared information with class PGOGenFunc. |
| 1008 | FuncPGOInstrumentation<PGOUseEdge, UseBBInfo> FuncInfo; |
| 1009 | |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1010 | // The maximum count value in the profile. This is only used in PGO use |
| 1011 | // compilation. |
| 1012 | uint64_t ProgramMaxCount; |
| 1013 | |
| Rong Xu | 33308f9 | 2016-10-25 21:47:24 +0000 | [diff] [blame] | 1014 | // Position of counter that remains to be read. |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1015 | uint32_t CountPosition = 0; |
| Rong Xu | 33308f9 | 2016-10-25 21:47:24 +0000 | [diff] [blame] | 1016 | |
| 1017 | // Total size of the profile count for this function. |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1018 | uint32_t ProfileCountSize = 0; |
| Rong Xu | 33308f9 | 2016-10-25 21:47:24 +0000 | [diff] [blame] | 1019 | |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1020 | // ProfileRecord for this function. |
| 1021 | InstrProfRecord ProfileRecord; |
| 1022 | |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1023 | // Function hotness info derived from profile. |
| 1024 | FuncFreqAttr FreqAttr; |
| 1025 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1026 | // Is to use the context sensitive profile. |
| 1027 | bool IsCS; |
| 1028 | |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1029 | // Find the Instrumented BB and set the value. Return false on error. |
| 1030 | bool setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Set the edge counter value for the unknown edge -- there should be only |
| 1033 | // one unknown edge. |
| 1034 | void setEdgeCount(DirectEdges &Edges, uint64_t Value); |
| 1035 | |
| 1036 | // Return FuncName string; |
| 1037 | const std::string getFuncName() const { return FuncInfo.FuncName; } |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1038 | |
| 1039 | // Set the hot/cold inline hints based on the count values. |
| 1040 | // FIXME: This function should be removed once the functionality in |
| 1041 | // the inliner is implemented. |
| 1042 | void markFunctionAttributes(uint64_t EntryCount, uint64_t MaxCount) { |
| 1043 | if (ProgramMaxCount == 0) |
| 1044 | return; |
| 1045 | // Threshold of the hot functions. |
| 1046 | const BranchProbability HotFunctionThreshold(1, 100); |
| 1047 | // Threshold of the cold functions. |
| 1048 | const BranchProbability ColdFunctionThreshold(2, 10000); |
| 1049 | if (EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount)) |
| 1050 | FreqAttr = FFA_Hot; |
| 1051 | else if (MaxCount <= ColdFunctionThreshold.scale(ProgramMaxCount)) |
| 1052 | FreqAttr = FFA_Cold; |
| 1053 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1054 | }; |
| 1055 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1056 | } // end anonymous namespace |
| 1057 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1058 | // Visit all the edges and assign the count value for the instrumented |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1059 | // edges and the BB. Return false on error. |
| 1060 | bool PGOUseFunc::setInstrumentedCounts( |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1061 | const std::vector<uint64_t> &CountFromProfile) { |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 1062 | |
| 1063 | std::vector<BasicBlock *> InstrumentBBs; |
| 1064 | FuncInfo.getInstrumentBBs(InstrumentBBs); |
| 1065 | unsigned NumCounters = |
| 1066 | InstrumentBBs.size() + FuncInfo.SIVisitor.getNumOfSelectInsts(); |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1067 | // The number of counters here should match the number of counters |
| 1068 | // in profile. Return if they mismatch. |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 1069 | if (NumCounters != CountFromProfile.size()) { |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1072 | uint32_t I = 0; |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 1073 | for (BasicBlock *InstrBB : InstrumentBBs) { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1074 | uint64_t CountValue = CountFromProfile[I++]; |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 1075 | UseBBInfo &Info = getBBInfo(InstrBB); |
| 1076 | Info.setBBInfoCount(CountValue); |
| 1077 | // If only one in-edge, the edge profile count should be the same as BB |
| 1078 | // profile count. |
| 1079 | if (Info.InEdges.size() == 1) { |
| 1080 | Info.InEdges[0]->setEdgeCount(CountValue); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1081 | } |
| Rong Xu | e88173a | 2019-05-28 21:45:56 +0000 | [diff] [blame] | 1082 | // If only one out-edge, the edge profile count should be the same as BB |
| 1083 | // profile count. |
| 1084 | if (Info.OutEdges.size() == 1) { |
| 1085 | Info.OutEdges[0]->setEdgeCount(CountValue); |
| 1086 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1087 | } |
| Rong Xu | 0a2a131 | 2017-03-09 19:08:55 +0000 | [diff] [blame] | 1088 | ProfileCountSize = CountFromProfile.size(); |
| Rong Xu | 33308f9 | 2016-10-25 21:47:24 +0000 | [diff] [blame] | 1089 | CountPosition = I; |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1090 | return true; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | // Set the count value for the unknown edge. There should be one and only one |
| 1094 | // unknown edge in Edges vector. |
| 1095 | void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) { |
| 1096 | for (auto &E : Edges) { |
| 1097 | if (E->CountValid) |
| 1098 | continue; |
| 1099 | E->setEdgeCount(Value); |
| 1100 | |
| 1101 | getBBInfo(E->SrcBB).UnknownCountOutEdge--; |
| 1102 | getBBInfo(E->DestBB).UnknownCountInEdge--; |
| 1103 | return; |
| 1104 | } |
| 1105 | llvm_unreachable("Cannot find the unknown count edge"); |
| 1106 | } |
| 1107 | |
| 1108 | // Read the profile from ProfileFileName and assign the value to the |
| 1109 | // instrumented BB and the edges. This function also updates ProgramMaxCount. |
| 1110 | // Return true if the profile are successfully read, and false on errors. |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 1111 | bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros) { |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1112 | auto &Ctx = M->getContext(); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1113 | Expected<InstrProfRecord> Result = |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1114 | PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1115 | if (Error E = Result.takeError()) { |
| 1116 | handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { |
| 1117 | auto Err = IPE.get(); |
| 1118 | bool SkipWarning = false; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1119 | LLVM_DEBUG(dbgs() << "Error in reading profile for Func " |
| 1120 | << FuncInfo.FuncName << ": "); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1121 | if (Err == instrprof_error::unknown_function) { |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1122 | IsCS ? NumOfCSPGOMissing++ : NumOfPGOMissing++; |
| Xinliang David Li | 76a0108 | 2016-08-11 05:09:30 +0000 | [diff] [blame] | 1123 | SkipWarning = !PGOWarnMissing; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1124 | LLVM_DEBUG(dbgs() << "unknown function"); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1125 | } else if (Err == instrprof_error::hash_mismatch || |
| 1126 | Err == instrprof_error::malformed) { |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1127 | IsCS ? NumOfCSPGOMismatch++ : NumOfPGOMismatch++; |
| Rong Xu | 20f5df1 | 2017-01-11 20:19:41 +0000 | [diff] [blame] | 1128 | SkipWarning = |
| 1129 | NoPGOWarnMismatch || |
| 1130 | (NoPGOWarnMismatchComdat && |
| 1131 | (F.hasComdat() || |
| 1132 | F.getLinkage() == GlobalValue::AvailableExternallyLinkage)); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1133 | LLVM_DEBUG(dbgs() << "hash mismatch (skip=" << SkipWarning << ")"); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1134 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1135 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1136 | LLVM_DEBUG(dbgs() << " IsCS=" << IsCS << "\n"); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1137 | if (SkipWarning) |
| 1138 | return; |
| 1139 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1140 | std::string Msg = IPE.message() + std::string(" ") + F.getName().str() + |
| 1141 | std::string(" Hash = ") + |
| 1142 | std::to_string(FuncInfo.FunctionHash); |
| 1143 | |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1144 | Ctx.diagnose( |
| 1145 | DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning)); |
| 1146 | }); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1147 | return false; |
| 1148 | } |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1149 | ProfileRecord = std::move(Result.get()); |
| 1150 | std::vector<uint64_t> &CountFromProfile = ProfileRecord.Counts; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1151 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1152 | IsCS ? NumOfCSPGOFunc++ : NumOfPGOFunc++; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1153 | LLVM_DEBUG(dbgs() << CountFromProfile.size() << " counts\n"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1154 | uint64_t ValueSum = 0; |
| 1155 | for (unsigned I = 0, S = CountFromProfile.size(); I < S; I++) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1156 | LLVM_DEBUG(dbgs() << " " << I << ": " << CountFromProfile[I] << "\n"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1157 | ValueSum += CountFromProfile[I]; |
| 1158 | } |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 1159 | AllZeros = (ValueSum == 0); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1160 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1161 | LLVM_DEBUG(dbgs() << "SUM = " << ValueSum << "\n"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1162 | |
| 1163 | getBBInfo(nullptr).UnknownCountOutEdge = 2; |
| 1164 | getBBInfo(nullptr).UnknownCountInEdge = 2; |
| 1165 | |
| Rong Xu | 959ef16 | 2019-04-11 20:54:17 +0000 | [diff] [blame] | 1166 | if (!setInstrumentedCounts(CountFromProfile)) { |
| 1167 | LLVM_DEBUG( |
| 1168 | dbgs() << "Inconsistent number of counts, skipping this function"); |
| 1169 | Ctx.diagnose(DiagnosticInfoPGOProfile( |
| 1170 | M->getName().data(), |
| 1171 | Twine("Inconsistent number of counts in ") + F.getName().str() |
| 1172 | + Twine(": the profile may be stale or there is a function name collision."), |
| 1173 | DS_Warning)); |
| 1174 | return false; |
| 1175 | } |
| Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 1176 | ProgramMaxCount = PGOReader->getMaximumFunctionCount(IsCS); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | // Populate the counters from instrumented BBs to all BBs. |
| 1181 | // In the end of this operation, all BBs should have a valid count value. |
| 1182 | void PGOUseFunc::populateCounters() { |
| 1183 | // First set up Count variable for all BBs. |
| 1184 | for (auto &E : FuncInfo.MST.AllEdges) { |
| 1185 | if (E->Removed) |
| 1186 | continue; |
| 1187 | |
| 1188 | const BasicBlock *SrcBB = E->SrcBB; |
| 1189 | const BasicBlock *DestBB = E->DestBB; |
| 1190 | UseBBInfo &SrcInfo = getBBInfo(SrcBB); |
| 1191 | UseBBInfo &DestInfo = getBBInfo(DestBB); |
| 1192 | SrcInfo.OutEdges.push_back(E.get()); |
| 1193 | DestInfo.InEdges.push_back(E.get()); |
| 1194 | SrcInfo.UnknownCountOutEdge++; |
| 1195 | DestInfo.UnknownCountInEdge++; |
| 1196 | |
| 1197 | if (!E->CountValid) |
| 1198 | continue; |
| 1199 | DestInfo.UnknownCountInEdge--; |
| 1200 | SrcInfo.UnknownCountOutEdge--; |
| 1201 | } |
| 1202 | |
| 1203 | bool Changes = true; |
| 1204 | unsigned NumPasses = 0; |
| 1205 | while (Changes) { |
| 1206 | NumPasses++; |
| 1207 | Changes = false; |
| 1208 | |
| 1209 | // For efficient traversal, it's better to start from the end as most |
| 1210 | // of the instrumented edges are at the end. |
| 1211 | for (auto &BB : reverse(F)) { |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1212 | UseBBInfo *Count = findBBInfo(&BB); |
| 1213 | if (Count == nullptr) |
| 1214 | continue; |
| 1215 | if (!Count->CountValid) { |
| 1216 | if (Count->UnknownCountOutEdge == 0) { |
| 1217 | Count->CountValue = sumEdgeCount(Count->OutEdges); |
| 1218 | Count->CountValid = true; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1219 | Changes = true; |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1220 | } else if (Count->UnknownCountInEdge == 0) { |
| 1221 | Count->CountValue = sumEdgeCount(Count->InEdges); |
| 1222 | Count->CountValid = true; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1223 | Changes = true; |
| 1224 | } |
| 1225 | } |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1226 | if (Count->CountValid) { |
| 1227 | if (Count->UnknownCountOutEdge == 1) { |
| Rong Xu | 51a1e3c | 2016-12-13 06:41:14 +0000 | [diff] [blame] | 1228 | uint64_t Total = 0; |
| 1229 | uint64_t OutSum = sumEdgeCount(Count->OutEdges); |
| 1230 | // If the one of the successor block can early terminate (no-return), |
| 1231 | // we can end up with situation where out edge sum count is larger as |
| 1232 | // the source BB's count is collected by a post-dominated block. |
| 1233 | if (Count->CountValue > OutSum) |
| 1234 | Total = Count->CountValue - OutSum; |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1235 | setEdgeCount(Count->OutEdges, Total); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1236 | Changes = true; |
| 1237 | } |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1238 | if (Count->UnknownCountInEdge == 1) { |
| Rong Xu | 51a1e3c | 2016-12-13 06:41:14 +0000 | [diff] [blame] | 1239 | uint64_t Total = 0; |
| 1240 | uint64_t InSum = sumEdgeCount(Count->InEdges); |
| 1241 | if (Count->CountValue > InSum) |
| 1242 | Total = Count->CountValue - InSum; |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1243 | setEdgeCount(Count->InEdges, Total); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1244 | Changes = true; |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1250 | LLVM_DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n"); |
| Sean Silva | 8c7e121 | 2016-05-28 04:19:45 +0000 | [diff] [blame] | 1251 | #ifndef NDEBUG |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1252 | // Assert every BB has a valid counter. |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1253 | for (auto &BB : F) { |
| 1254 | auto BI = findBBInfo(&BB); |
| 1255 | if (BI == nullptr) |
| 1256 | continue; |
| 1257 | assert(BI->CountValid && "BB count is not valid"); |
| 1258 | } |
| Sean Silva | 8c7e121 | 2016-05-28 04:19:45 +0000 | [diff] [blame] | 1259 | #endif |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1260 | uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue; |
| Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 1261 | F.setEntryCount(ProfileCount(FuncEntryCount, Function::PCT_Real)); |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1262 | uint64_t FuncMaxCount = FuncEntryCount; |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1263 | for (auto &BB : F) { |
| 1264 | auto BI = findBBInfo(&BB); |
| 1265 | if (BI == nullptr) |
| 1266 | continue; |
| 1267 | FuncMaxCount = std::max(FuncMaxCount, BI->CountValue); |
| 1268 | } |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1269 | markFunctionAttributes(FuncEntryCount, FuncMaxCount); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1270 | |
| Rong Xu | 33308f9 | 2016-10-25 21:47:24 +0000 | [diff] [blame] | 1271 | // Now annotate select instructions |
| 1272 | FuncInfo.SIVisitor.annotateSelects(F, this, &CountPosition); |
| 1273 | assert(CountPosition == ProfileCountSize); |
| 1274 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1275 | LLVM_DEBUG(FuncInfo.dumpInfo("after reading profile.")); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | // Assign the scaled count values to the BB with multiple out edges. |
| 1279 | void PGOUseFunc::setBranchWeights() { |
| 1280 | // Generate MD_prof metadata for every branch instruction. |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1281 | LLVM_DEBUG(dbgs() << "\nSetting branch weights for func " << F.getName() |
| 1282 | << " IsCS=" << IsCS << "\n"); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1283 | for (auto &BB : F) { |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1284 | Instruction *TI = BB.getTerminator(); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1285 | if (TI->getNumSuccessors() < 2) |
| 1286 | continue; |
| Rong Xu | 15848e5 | 2017-08-23 21:36:02 +0000 | [diff] [blame] | 1287 | if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || |
| 1288 | isa<IndirectBrInst>(TI))) |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1289 | continue; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1290 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1291 | if (getBBInfo(&BB).CountValue == 0) |
| 1292 | continue; |
| 1293 | |
| 1294 | // We have a non-zero Branch BB. |
| 1295 | const UseBBInfo &BBCountInfo = getBBInfo(&BB); |
| 1296 | unsigned Size = BBCountInfo.OutEdges.size(); |
| Xinliang David Li | 63248ab | 2016-08-19 06:31:45 +0000 | [diff] [blame] | 1297 | SmallVector<uint64_t, 2> EdgeCounts(Size, 0); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1298 | uint64_t MaxCount = 0; |
| 1299 | for (unsigned s = 0; s < Size; s++) { |
| 1300 | const PGOUseEdge *E = BBCountInfo.OutEdges[s]; |
| 1301 | const BasicBlock *SrcBB = E->SrcBB; |
| 1302 | const BasicBlock *DestBB = E->DestBB; |
| Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 1303 | if (DestBB == nullptr) |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1304 | continue; |
| 1305 | unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB); |
| 1306 | uint64_t EdgeCount = E->CountValue; |
| 1307 | if (EdgeCount > MaxCount) |
| 1308 | MaxCount = EdgeCount; |
| 1309 | EdgeCounts[SuccNum] = EdgeCount; |
| 1310 | } |
| Xinliang David Li | 2c93368 | 2016-08-19 05:31:33 +0000 | [diff] [blame] | 1311 | setProfMetadata(M, TI, EdgeCounts, MaxCount); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1314 | |
| Hiroshi Yamauchi | c94d4d7 | 2017-11-20 21:03:38 +0000 | [diff] [blame] | 1315 | static bool isIndirectBrTarget(BasicBlock *BB) { |
| 1316 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 1317 | if (isa<IndirectBrInst>((*PI)->getTerminator())) |
| 1318 | return true; |
| 1319 | } |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1323 | void PGOUseFunc::annotateIrrLoopHeaderWeights() { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1324 | LLVM_DEBUG(dbgs() << "\nAnnotating irreducible loop header weights.\n"); |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1325 | // Find irr loop headers |
| 1326 | for (auto &BB : F) { |
| Hiroshi Yamauchi | c94d4d7 | 2017-11-20 21:03:38 +0000 | [diff] [blame] | 1327 | // As a heuristic also annotate indrectbr targets as they have a high chance |
| 1328 | // to become an irreducible loop header after the indirectbr tail |
| 1329 | // duplication. |
| 1330 | if (BFI->isIrrLoopHeader(&BB) || isIndirectBrTarget(&BB)) { |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1331 | Instruction *TI = BB.getTerminator(); |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1332 | const UseBBInfo &BBCountInfo = getBBInfo(&BB); |
| 1333 | setIrrLoopHeaderMetadata(M, TI, BBCountInfo.CountValue); |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1338 | void SelectInstVisitor::instrumentOneSelectInst(SelectInst &SI) { |
| 1339 | Module *M = F.getParent(); |
| 1340 | IRBuilder<> Builder(&SI); |
| 1341 | Type *Int64Ty = Builder.getInt64Ty(); |
| 1342 | Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 1343 | auto *Step = Builder.CreateZExt(SI.getCondition(), Int64Ty); |
| 1344 | Builder.CreateCall( |
| 1345 | Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step), |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1346 | {ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), |
| Rong Xu | 0a2a131 | 2017-03-09 19:08:55 +0000 | [diff] [blame] | 1347 | Builder.getInt64(FuncHash), Builder.getInt32(TotalNumCtrs), |
| 1348 | Builder.getInt32(*CurCtrIdx), Step}); |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1349 | ++(*CurCtrIdx); |
| 1350 | } |
| 1351 | |
| 1352 | void SelectInstVisitor::annotateOneSelectInst(SelectInst &SI) { |
| 1353 | std::vector<uint64_t> &CountFromProfile = UseFunc->getProfileRecord().Counts; |
| 1354 | assert(*CurCtrIdx < CountFromProfile.size() && |
| 1355 | "Out of bound access of counters"); |
| 1356 | uint64_t SCounts[2]; |
| 1357 | SCounts[0] = CountFromProfile[*CurCtrIdx]; // True count |
| 1358 | ++(*CurCtrIdx); |
| Rong Xu | a5b5745 | 2016-12-02 19:10:29 +0000 | [diff] [blame] | 1359 | uint64_t TotalCount = 0; |
| 1360 | auto BI = UseFunc->findBBInfo(SI.getParent()); |
| 1361 | if (BI != nullptr) |
| 1362 | TotalCount = BI->CountValue; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1363 | // False Count |
| 1364 | SCounts[1] = (TotalCount > SCounts[0] ? TotalCount - SCounts[0] : 0); |
| 1365 | uint64_t MaxCount = std::max(SCounts[0], SCounts[1]); |
| Xinliang David Li | c736828 | 2016-09-20 20:20:01 +0000 | [diff] [blame] | 1366 | if (MaxCount) |
| 1367 | setProfMetadata(F.getParent(), &SI, SCounts, MaxCount); |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | void SelectInstVisitor::visitSelectInst(SelectInst &SI) { |
| 1371 | if (!PGOInstrSelect) |
| 1372 | return; |
| 1373 | // FIXME: do not handle this yet. |
| 1374 | if (SI.getCondition()->getType()->isVectorTy()) |
| 1375 | return; |
| 1376 | |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1377 | switch (Mode) { |
| 1378 | case VM_counting: |
| Vitaly Buka | ca6ecd2 | 2017-03-15 23:07:41 +0000 | [diff] [blame] | 1379 | NSIs++; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | case VM_instrument: |
| 1382 | instrumentOneSelectInst(SI); |
| Simon Pilgrim | f33a6b7 | 2016-09-18 21:08:35 +0000 | [diff] [blame] | 1383 | return; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1384 | case VM_annotate: |
| 1385 | annotateOneSelectInst(SI); |
| Simon Pilgrim | f33a6b7 | 2016-09-18 21:08:35 +0000 | [diff] [blame] | 1386 | return; |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1387 | } |
| Simon Pilgrim | f33a6b7 | 2016-09-18 21:08:35 +0000 | [diff] [blame] | 1388 | |
| 1389 | llvm_unreachable("Unknown visiting mode"); |
| Xinliang David Li | 4ca1733 | 2016-09-18 18:34:07 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 1392 | void MemIntrinsicVisitor::instrumentOneMemIntrinsic(MemIntrinsic &MI) { |
| 1393 | Module *M = F.getParent(); |
| 1394 | IRBuilder<> Builder(&MI); |
| 1395 | Type *Int64Ty = Builder.getInt64Ty(); |
| 1396 | Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 1397 | Value *Length = MI.getLength(); |
| Fangrui Song | 2c5c12c | 2019-04-05 16:16:23 +0000 | [diff] [blame] | 1398 | assert(!isa<ConstantInt>(Length)); |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 1399 | Builder.CreateCall( |
| 1400 | Intrinsic::getDeclaration(M, Intrinsic::instrprof_value_profile), |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1401 | {ConstantExpr::getBitCast(FuncNameVar, I8PtrTy), |
| Ana Pazos | f731bde | 2017-06-19 20:04:33 +0000 | [diff] [blame] | 1402 | Builder.getInt64(FuncHash), Builder.CreateZExtOrTrunc(Length, Int64Ty), |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 1403 | Builder.getInt32(IPVK_MemOPSize), Builder.getInt32(CurCtrId)}); |
| 1404 | ++CurCtrId; |
| 1405 | } |
| 1406 | |
| 1407 | void MemIntrinsicVisitor::visitMemIntrinsic(MemIntrinsic &MI) { |
| 1408 | if (!PGOInstrMemOP) |
| 1409 | return; |
| 1410 | Value *Length = MI.getLength(); |
| 1411 | // Not instrument constant length calls. |
| 1412 | if (dyn_cast<ConstantInt>(Length)) |
| 1413 | return; |
| 1414 | |
| 1415 | switch (Mode) { |
| 1416 | case VM_counting: |
| 1417 | NMemIs++; |
| 1418 | return; |
| 1419 | case VM_instrument: |
| 1420 | instrumentOneMemIntrinsic(MI); |
| 1421 | return; |
| 1422 | case VM_annotate: |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 1423 | Candidates.push_back(&MI); |
| 1424 | return; |
| Rong Xu | 60faea1 | 2017-03-16 21:15:48 +0000 | [diff] [blame] | 1425 | } |
| 1426 | llvm_unreachable("Unknown visiting mode"); |
| 1427 | } |
| 1428 | |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1429 | // Traverse all valuesites and annotate the instructions for all value kind. |
| 1430 | void PGOUseFunc::annotateValueSites() { |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1431 | if (DisableValueProfiling) |
| 1432 | return; |
| 1433 | |
| Rong Xu | 8e8fe85 | 2016-04-01 16:43:30 +0000 | [diff] [blame] | 1434 | // Create the PGOFuncName meta data. |
| Rong Xu | f8f051c | 2016-04-22 21:00:17 +0000 | [diff] [blame] | 1435 | createPGOFuncNameMetadata(F, FuncInfo.FuncName); |
| Rong Xu | b534166 | 2016-03-30 18:37:52 +0000 | [diff] [blame] | 1436 | |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1437 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 1438 | annotateValueSites(Kind); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| Dmitry Mikulin | 312b5f8 | 2019-04-23 22:26:55 +0000 | [diff] [blame] | 1441 | static const char *ValueProfKindDescr[] = { |
| 1442 | #define VALUE_PROF_KIND(Enumerator, Value, Descr) Descr, |
| 1443 | #include "llvm/ProfileData/InstrProfData.inc" |
| 1444 | }; |
| 1445 | |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1446 | // Annotate the instructions for a specific value kind. |
| 1447 | void PGOUseFunc::annotateValueSites(uint32_t Kind) { |
| Dmitry Mikulin | 312b5f8 | 2019-04-23 22:26:55 +0000 | [diff] [blame] | 1448 | assert(Kind <= IPVK_Last); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1449 | unsigned ValueSiteIndex = 0; |
| 1450 | auto &ValueSites = FuncInfo.ValueSites[Kind]; |
| 1451 | unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind); |
| 1452 | if (NumValueSites != ValueSites.size()) { |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1453 | auto &Ctx = M->getContext(); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1454 | Ctx.diagnose(DiagnosticInfoPGOProfile( |
| 1455 | M->getName().data(), |
| Dmitry Mikulin | 312b5f8 | 2019-04-23 22:26:55 +0000 | [diff] [blame] | 1456 | Twine("Inconsistent number of value sites for ") + |
| 1457 | Twine(ValueProfKindDescr[Kind]) + |
| 1458 | Twine(" profiling in \"") + F.getName().str() + |
| 1459 | Twine("\", possibly due to the use of a stale profile."), |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1460 | DS_Warning)); |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1464 | for (auto &I : ValueSites) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1465 | LLVM_DEBUG(dbgs() << "Read one value site profile (kind = " << Kind |
| 1466 | << "): Index = " << ValueSiteIndex << " out of " |
| 1467 | << NumValueSites << "\n"); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1468 | annotateValueSite(*M, *I, ProfileRecord, |
| 1469 | static_cast<InstrProfValueKind>(Kind), ValueSiteIndex, |
| Rong Xu | e60343d | 2017-03-17 18:07:26 +0000 | [diff] [blame] | 1470 | Kind == IPVK_MemOPSize ? MaxNumMemOPAnnotations |
| 1471 | : MaxNumAnnotations); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1472 | ValueSiteIndex++; |
| Rong Xu | 13b01dc | 2016-02-10 18:24:45 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1475 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 1476 | // Collect the set of members for each Comdat in module M and store |
| 1477 | // in ComdatMembers. |
| 1478 | static void collectComdatMembers( |
| 1479 | Module &M, |
| 1480 | std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers) { |
| 1481 | if (!DoComdatRenaming) |
| 1482 | return; |
| 1483 | for (Function &F : M) |
| 1484 | if (Comdat *C = F.getComdat()) |
| 1485 | ComdatMembers.insert(std::make_pair(C, &F)); |
| 1486 | for (GlobalVariable &GV : M.globals()) |
| 1487 | if (Comdat *C = GV.getComdat()) |
| 1488 | ComdatMembers.insert(std::make_pair(C, &GV)); |
| 1489 | for (GlobalAlias &GA : M.aliases()) |
| 1490 | if (Comdat *C = GA.getComdat()) |
| 1491 | ComdatMembers.insert(std::make_pair(C, &GA)); |
| 1492 | } |
| 1493 | |
| Xinliang David Li | 5ad7c82 | 2016-05-02 20:33:59 +0000 | [diff] [blame] | 1494 | static bool InstrumentAllFunctions( |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1495 | Module &M, function_ref<BranchProbabilityInfo *(Function &)> LookupBPI, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1496 | function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, bool IsCS) { |
| 1497 | // For the context-sensitve instrumentation, we should have a separated pass |
| 1498 | // (before LTO/ThinLTO linking) to create these variables. |
| 1499 | if (!IsCS) |
| 1500 | createIRLevelProfileFlagVar(M, /* IsCS */ false); |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 1501 | std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers; |
| 1502 | collectComdatMembers(M, ComdatMembers); |
| 1503 | |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1504 | for (auto &F : M) { |
| 1505 | if (F.isDeclaration()) |
| 1506 | continue; |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1507 | auto *BPI = LookupBPI(F); |
| Xinliang David Li | dfa21c3 | 2016-05-09 21:37:12 +0000 | [diff] [blame] | 1508 | auto *BFI = LookupBFI(F); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1509 | instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers, IsCS); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1510 | } |
| 1511 | return true; |
| 1512 | } |
| 1513 | |
| Jordan Rupprecht | 090683b | 2019-03-04 22:54:44 +0000 | [diff] [blame] | 1514 | PreservedAnalyses |
| 1515 | PGOInstrumentationGenCreateVar::run(Module &M, ModuleAnalysisManager &AM) { |
| 1516 | createProfileFileNameVar(M, CSInstrName); |
| 1517 | createIRLevelProfileFlagVar(M, /* IsCS */ true); |
| 1518 | return PreservedAnalyses::all(); |
| 1519 | } |
| 1520 | |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1521 | bool PGOInstrumentationGenLegacyPass::runOnModule(Module &M) { |
| Xinliang David Li | 5ad7c82 | 2016-05-02 20:33:59 +0000 | [diff] [blame] | 1522 | if (skipModule(M)) |
| 1523 | return false; |
| 1524 | |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1525 | auto LookupBPI = [this](Function &F) { |
| 1526 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 1527 | }; |
| Xinliang David Li | dfa21c3 | 2016-05-09 21:37:12 +0000 | [diff] [blame] | 1528 | auto LookupBFI = [this](Function &F) { |
| 1529 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| Xinliang David Li | 5ad7c82 | 2016-05-02 20:33:59 +0000 | [diff] [blame] | 1530 | }; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1531 | return InstrumentAllFunctions(M, LookupBPI, LookupBFI, IsCS); |
| Xinliang David Li | 5ad7c82 | 2016-05-02 20:33:59 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1534 | PreservedAnalyses PGOInstrumentationGen::run(Module &M, |
| Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 1535 | ModuleAnalysisManager &AM) { |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1536 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1537 | auto LookupBPI = [&FAM](Function &F) { |
| 1538 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 1539 | }; |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1540 | |
| Xinliang David Li | dfa21c3 | 2016-05-09 21:37:12 +0000 | [diff] [blame] | 1541 | auto LookupBFI = [&FAM](Function &F) { |
| 1542 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1543 | }; |
| 1544 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1545 | if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI, IsCS)) |
| Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 1546 | return PreservedAnalyses::all(); |
| 1547 | |
| 1548 | return PreservedAnalyses::none(); |
| 1549 | } |
| 1550 | |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1551 | static bool annotateAllFunctions( |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1552 | Module &M, StringRef ProfileFileName, StringRef ProfileRemappingFileName, |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1553 | function_ref<BranchProbabilityInfo *(Function &)> LookupBPI, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1554 | function_ref<BlockFrequencyInfo *(Function &)> LookupBFI, bool IsCS) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1555 | LLVM_DEBUG(dbgs() << "Read in profile counters: "); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1556 | auto &Ctx = M.getContext(); |
| 1557 | // Read the counter array from file. |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1558 | auto ReaderOrErr = |
| 1559 | IndexedInstrProfReader::create(ProfileFileName, ProfileRemappingFileName); |
| Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 1560 | if (Error E = ReaderOrErr.takeError()) { |
| 1561 | handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { |
| 1562 | Ctx.diagnose( |
| 1563 | DiagnosticInfoPGOProfile(ProfileFileName.data(), EI.message())); |
| 1564 | }); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1565 | return false; |
| 1566 | } |
| 1567 | |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1568 | std::unique_ptr<IndexedInstrProfReader> PGOReader = |
| 1569 | std::move(ReaderOrErr.get()); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1570 | if (!PGOReader) { |
| 1571 | Ctx.diagnose(DiagnosticInfoPGOProfile(ProfileFileName.data(), |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1572 | StringRef("Cannot get PGOReader"))); |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1573 | return false; |
| 1574 | } |
| Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 1575 | if (!PGOReader->hasCSIRLevelProfile() && IsCS) |
| 1576 | return false; |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1577 | |
| Rong Xu | 33c76c0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 1578 | // TODO: might need to change the warning once the clang option is finalized. |
| 1579 | if (!PGOReader->isIRLevelProfile()) { |
| 1580 | Ctx.diagnose(DiagnosticInfoPGOProfile( |
| 1581 | ProfileFileName.data(), "Not an IR level instrumentation profile")); |
| 1582 | return false; |
| 1583 | } |
| 1584 | |
| Rong Xu | 705f777 | 2016-07-25 18:45:37 +0000 | [diff] [blame] | 1585 | std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers; |
| 1586 | collectComdatMembers(M, ComdatMembers); |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 1587 | std::vector<Function *> HotFunctions; |
| 1588 | std::vector<Function *> ColdFunctions; |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1589 | for (auto &F : M) { |
| 1590 | if (F.isDeclaration()) |
| 1591 | continue; |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1592 | auto *BPI = LookupBPI(F); |
| Xinliang David Li | dfa21c3 | 2016-05-09 21:37:12 +0000 | [diff] [blame] | 1593 | auto *BFI = LookupBFI(F); |
| Hiroshi Yamauchi | f3bda1d | 2017-12-12 19:07:43 +0000 | [diff] [blame] | 1594 | // Split indirectbr critical edges here before computing the MST rather than |
| 1595 | // later in getInstrBB() to avoid invalidating it. |
| 1596 | SplitIndirectBrCriticalEdges(F, BPI, BFI); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1597 | PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI, IsCS); |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 1598 | bool AllZeros = false; |
| 1599 | if (!Func.readCounters(PGOReader.get(), AllZeros)) |
| Sean Silva | 2e8f095 | 2016-05-28 04:19:40 +0000 | [diff] [blame] | 1600 | continue; |
| Rong Xu | fb4bcc4 | 2018-11-07 23:51:20 +0000 | [diff] [blame] | 1601 | if (AllZeros) { |
| 1602 | F.setEntryCount(ProfileCount(0, Function::PCT_Real)); |
| 1603 | if (Func.getProgramMaxCount() != 0) |
| 1604 | ColdFunctions.push_back(&F); |
| 1605 | continue; |
| 1606 | } |
| Sean Silva | 2e8f095 | 2016-05-28 04:19:40 +0000 | [diff] [blame] | 1607 | Func.populateCounters(); |
| 1608 | Func.setBranchWeights(); |
| Rong Xu | a3bbf96 | 2017-03-15 18:23:39 +0000 | [diff] [blame] | 1609 | Func.annotateValueSites(); |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1610 | Func.annotateIrrLoopHeaderWeights(); |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1611 | PGOUseFunc::FuncFreqAttr FreqAttr = Func.getFuncFreqAttr(); |
| 1612 | if (FreqAttr == PGOUseFunc::FFA_Cold) |
| Sean Silva | 2a73019 | 2016-05-28 03:02:50 +0000 | [diff] [blame] | 1613 | ColdFunctions.push_back(&F); |
| Sean Silva | 9dd4b5c | 2016-05-28 03:56:25 +0000 | [diff] [blame] | 1614 | else if (FreqAttr == PGOUseFunc::FFA_Hot) |
| 1615 | HotFunctions.push_back(&F); |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 1616 | if (PGOViewCounts != PGOVCT_None && |
| 1617 | (ViewBlockFreqFuncName.empty() || |
| 1618 | F.getName().equals(ViewBlockFreqFuncName))) { |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 1619 | LoopInfo LI{DominatorTree(F)}; |
| 1620 | std::unique_ptr<BranchProbabilityInfo> NewBPI = |
| 1621 | llvm::make_unique<BranchProbabilityInfo>(F, LI); |
| 1622 | std::unique_ptr<BlockFrequencyInfo> NewBFI = |
| 1623 | llvm::make_unique<BlockFrequencyInfo>(F, *NewBPI, LI); |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 1624 | if (PGOViewCounts == PGOVCT_Graph) |
| 1625 | NewBFI->view(); |
| 1626 | else if (PGOViewCounts == PGOVCT_Text) { |
| 1627 | dbgs() << "pgo-view-counts: " << Func.getFunc().getName() << "\n"; |
| 1628 | NewBFI->print(dbgs()); |
| 1629 | } |
| Xinliang David Li | cb253ce | 2017-01-23 18:58:24 +0000 | [diff] [blame] | 1630 | } |
| Hiroshi Yamauchi | a43913c | 2017-09-13 17:20:38 +0000 | [diff] [blame] | 1631 | if (PGOViewRawCounts != PGOVCT_None && |
| 1632 | (ViewBlockFreqFuncName.empty() || |
| 1633 | F.getName().equals(ViewBlockFreqFuncName))) { |
| 1634 | if (PGOViewRawCounts == PGOVCT_Graph) |
| 1635 | if (ViewBlockFreqFuncName.empty()) |
| 1636 | WriteGraph(&Func, Twine("PGORawCounts_") + Func.getFunc().getName()); |
| 1637 | else |
| 1638 | ViewGraph(&Func, Twine("PGORawCounts_") + Func.getFunc().getName()); |
| 1639 | else if (PGOViewRawCounts == PGOVCT_Text) { |
| 1640 | dbgs() << "pgo-view-raw-counts: " << Func.getFunc().getName() << "\n"; |
| 1641 | Func.dumpInfo(); |
| 1642 | } |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1643 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1644 | } |
| Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 1645 | M.setProfileSummary(PGOReader->getSummary(IsCS).getMD(M.getContext()), |
| 1646 | IsCS ? ProfileSummary::PSK_CSInstr |
| 1647 | : ProfileSummary::PSK_Instr); |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1648 | |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 1649 | // Set function hotness attribute from the profile. |
| Sean Silva | 42cc342 | 2016-05-28 04:24:39 +0000 | [diff] [blame] | 1650 | // We have to apply these attributes at the end because their presence |
| 1651 | // can affect the BranchProbabilityInfo of any callers, resulting in an |
| 1652 | // inconsistent MST between prof-gen and prof-use. |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 1653 | for (auto &F : HotFunctions) { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1654 | F->addFnAttr(Attribute::InlineHint); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1655 | LLVM_DEBUG(dbgs() << "Set inline attribute to function: " << F->getName() |
| 1656 | << "\n"); |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 1657 | } |
| 1658 | for (auto &F : ColdFunctions) { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1659 | F->addFnAttr(Attribute::Cold); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1660 | LLVM_DEBUG(dbgs() << "Set cold attribute to function: " << F->getName() |
| 1661 | << "\n"); |
| Rong Xu | 6090afd | 2016-03-28 17:08:56 +0000 | [diff] [blame] | 1662 | } |
| Rong Xu | f430ae4 | 2015-12-09 18:08:16 +0000 | [diff] [blame] | 1663 | return true; |
| 1664 | } |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 1665 | |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1666 | PGOInstrumentationUse::PGOInstrumentationUse(std::string Filename, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1667 | std::string RemappingFilename, |
| 1668 | bool IsCS) |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1669 | : ProfileFileName(std::move(Filename)), |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1670 | ProfileRemappingFileName(std::move(RemappingFilename)), IsCS(IsCS) { |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1671 | if (!PGOTestProfileFile.empty()) |
| 1672 | ProfileFileName = PGOTestProfileFile; |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1673 | if (!PGOTestProfileRemappingFile.empty()) |
| 1674 | ProfileRemappingFileName = PGOTestProfileRemappingFile; |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | PreservedAnalyses PGOInstrumentationUse::run(Module &M, |
| Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 1678 | ModuleAnalysisManager &AM) { |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1679 | |
| 1680 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1681 | auto LookupBPI = [&FAM](Function &F) { |
| 1682 | return &FAM.getResult<BranchProbabilityAnalysis>(F); |
| 1683 | }; |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1684 | |
| 1685 | auto LookupBFI = [&FAM](Function &F) { |
| 1686 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 1687 | }; |
| 1688 | |
| Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 1689 | if (!annotateAllFunctions(M, ProfileFileName, ProfileRemappingFileName, |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1690 | LookupBPI, LookupBFI, IsCS)) |
| Xinliang David Li | da19558 | 2016-05-10 21:59:52 +0000 | [diff] [blame] | 1691 | return PreservedAnalyses::all(); |
| 1692 | |
| 1693 | return PreservedAnalyses::none(); |
| 1694 | } |
| 1695 | |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 1696 | bool PGOInstrumentationUseLegacyPass::runOnModule(Module &M) { |
| 1697 | if (skipModule(M)) |
| 1698 | return false; |
| 1699 | |
| Xinliang David Li | d91057b | 2017-12-08 19:38:07 +0000 | [diff] [blame] | 1700 | auto LookupBPI = [this](Function &F) { |
| 1701 | return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI(); |
| 1702 | }; |
| Xinliang David Li | dfa21c3 | 2016-05-09 21:37:12 +0000 | [diff] [blame] | 1703 | auto LookupBFI = [this](Function &F) { |
| 1704 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 1705 | }; |
| 1706 | |
| Rong Xu | 6cdf3d8 | 2019-02-27 17:24:33 +0000 | [diff] [blame] | 1707 | return annotateAllFunctions(M, ProfileFileName, "", LookupBPI, LookupBFI, |
| 1708 | IsCS); |
| Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 1709 | } |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1710 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1711 | static std::string getSimpleNodeName(const BasicBlock *Node) { |
| 1712 | if (!Node->getName().empty()) |
| 1713 | return Node->getName(); |
| 1714 | |
| 1715 | std::string SimpleNodeName; |
| 1716 | raw_string_ostream OS(SimpleNodeName); |
| 1717 | Node->printAsOperand(OS, false); |
| 1718 | return OS.str(); |
| 1719 | } |
| 1720 | |
| 1721 | void llvm::setProfMetadata(Module *M, Instruction *TI, |
| 1722 | ArrayRef<uint64_t> EdgeCounts, |
| 1723 | uint64_t MaxCount) { |
| Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 1724 | MDBuilder MDB(M->getContext()); |
| 1725 | assert(MaxCount > 0 && "Bad max count"); |
| 1726 | uint64_t Scale = calculateCountScale(MaxCount); |
| 1727 | SmallVector<unsigned, 4> Weights; |
| 1728 | for (const auto &ECI : EdgeCounts) |
| 1729 | Weights.push_back(scaleBranchCount(ECI, Scale)); |
| 1730 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1731 | LLVM_DEBUG(dbgs() << "Weight is: "; for (const auto &W |
| 1732 | : Weights) { |
| 1733 | dbgs() << W << " "; |
| 1734 | } dbgs() << "\n";); |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1735 | TI->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 1736 | if (EmitBranchProbability) { |
| 1737 | std::string BrCondStr = getBranchCondString(TI); |
| 1738 | if (BrCondStr.empty()) |
| 1739 | return; |
| 1740 | |
| Rong Xu | 662f38b | 2018-03-27 18:55:56 +0000 | [diff] [blame] | 1741 | uint64_t WSum = |
| 1742 | std::accumulate(Weights.begin(), Weights.end(), (uint64_t)0, |
| 1743 | [](uint64_t w1, uint64_t w2) { return w1 + w2; }); |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 1744 | uint64_t TotalCount = |
| Rong Xu | 662f38b | 2018-03-27 18:55:56 +0000 | [diff] [blame] | 1745 | std::accumulate(EdgeCounts.begin(), EdgeCounts.end(), (uint64_t)0, |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 1746 | [](uint64_t c1, uint64_t c2) { return c1 + c2; }); |
| Rong Xu | 662f38b | 2018-03-27 18:55:56 +0000 | [diff] [blame] | 1747 | Scale = calculateCountScale(WSum); |
| 1748 | BranchProbability BP(scaleBranchCount(Weights[0], Scale), |
| 1749 | scaleBranchCount(WSum, Scale)); |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 1750 | std::string BranchProbStr; |
| 1751 | raw_string_ostream OS(BranchProbStr); |
| 1752 | OS << BP; |
| 1753 | OS << " (total count : " << TotalCount << ")"; |
| 1754 | OS.flush(); |
| 1755 | Function *F = TI->getParent()->getParent(); |
| Davide Italiano | 0c8d26c | 2017-07-20 20:43:05 +0000 | [diff] [blame] | 1756 | OptimizationRemarkEmitter ORE(F); |
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 1757 | ORE.emit([&]() { |
| 1758 | return OptimizationRemark(DEBUG_TYPE, "pgo-instrumentation", TI) |
| 1759 | << BrCondStr << " is true with probability : " << BranchProbStr; |
| 1760 | }); |
| Xinliang David Li | 0a0acbc | 2017-06-01 18:58:50 +0000 | [diff] [blame] | 1761 | } |
| Rong Xu | 48596b6 | 2017-04-04 16:42:20 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1764 | namespace llvm { |
| 1765 | |
| Hiroshi Yamauchi | dce9def | 2017-11-02 22:26:51 +0000 | [diff] [blame] | 1766 | void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, uint64_t Count) { |
| 1767 | MDBuilder MDB(M->getContext()); |
| 1768 | TI->setMetadata(llvm::LLVMContext::MD_irr_loop, |
| 1769 | MDB.createIrrLoopHeaderWeight(Count)); |
| 1770 | } |
| 1771 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1772 | template <> struct GraphTraits<PGOUseFunc *> { |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1773 | using NodeRef = const BasicBlock *; |
| 1774 | using ChildIteratorType = succ_const_iterator; |
| 1775 | using nodes_iterator = pointer_iterator<Function::const_iterator>; |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1776 | |
| 1777 | static NodeRef getEntryNode(const PGOUseFunc *G) { |
| 1778 | return &G->getFunc().front(); |
| 1779 | } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1780 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1781 | static ChildIteratorType child_begin(const NodeRef N) { |
| 1782 | return succ_begin(N); |
| 1783 | } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1784 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1785 | static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1786 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1787 | static nodes_iterator nodes_begin(const PGOUseFunc *G) { |
| 1788 | return nodes_iterator(G->getFunc().begin()); |
| 1789 | } |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1790 | |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1791 | static nodes_iterator nodes_end(const PGOUseFunc *G) { |
| 1792 | return nodes_iterator(G->getFunc().end()); |
| 1793 | } |
| 1794 | }; |
| 1795 | |
| 1796 | template <> struct DOTGraphTraits<PGOUseFunc *> : DefaultDOTGraphTraits { |
| 1797 | explicit DOTGraphTraits(bool isSimple = false) |
| 1798 | : DefaultDOTGraphTraits(isSimple) {} |
| 1799 | |
| 1800 | static std::string getGraphName(const PGOUseFunc *G) { |
| 1801 | return G->getFunc().getName(); |
| 1802 | } |
| 1803 | |
| 1804 | std::string getNodeLabel(const BasicBlock *Node, const PGOUseFunc *Graph) { |
| 1805 | std::string Result; |
| 1806 | raw_string_ostream OS(Result); |
| Xinliang David Li | 6144a59 | 2017-02-03 21:57:51 +0000 | [diff] [blame] | 1807 | |
| 1808 | OS << getSimpleNodeName(Node) << ":\\l"; |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1809 | UseBBInfo *BI = Graph->findBBInfo(Node); |
| Xinliang David Li | 6144a59 | 2017-02-03 21:57:51 +0000 | [diff] [blame] | 1810 | OS << "Count : "; |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1811 | if (BI && BI->CountValid) |
| Xinliang David Li | 6144a59 | 2017-02-03 21:57:51 +0000 | [diff] [blame] | 1812 | OS << BI->CountValue << "\\l"; |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1813 | else |
| Xinliang David Li | 6144a59 | 2017-02-03 21:57:51 +0000 | [diff] [blame] | 1814 | OS << "Unknown\\l"; |
| 1815 | |
| 1816 | if (!PGOInstrSelect) |
| 1817 | return Result; |
| 1818 | |
| 1819 | for (auto BI = Node->begin(); BI != Node->end(); ++BI) { |
| 1820 | auto *I = &*BI; |
| 1821 | if (!isa<SelectInst>(I)) |
| 1822 | continue; |
| 1823 | // Display scaled counts for SELECT instruction: |
| 1824 | OS << "SELECT : { T = "; |
| 1825 | uint64_t TC, FC; |
| Xinliang David Li | c7db0d0 | 2017-02-04 07:40:43 +0000 | [diff] [blame] | 1826 | bool HasProf = I->extractProfMetadata(TC, FC); |
| 1827 | if (!HasProf) |
| Xinliang David Li | 6144a59 | 2017-02-03 21:57:51 +0000 | [diff] [blame] | 1828 | OS << "Unknown, F = Unknown }\\l"; |
| 1829 | else |
| 1830 | OS << TC << ", F = " << FC << " }\\l"; |
| 1831 | } |
| Xinliang David Li | d289e45 | 2017-01-27 19:06:25 +0000 | [diff] [blame] | 1832 | return Result; |
| 1833 | } |
| 1834 | }; |
| Eugene Zelenko | fce4357 | 2017-10-21 00:57:46 +0000 | [diff] [blame] | 1835 | |
| 1836 | } // end namespace llvm |