1 //===- llvm/IR/PassInstrumentation.h ----------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// 10 /// This file defines the Pass Instrumentation classes that provide 11 /// instrumentation points into the pass execution by PassManager. 12 /// 13 /// There are two main classes: 14 /// - PassInstrumentation provides a set of instrumentation points for 15 /// pass managers to call on. 16 /// 17 /// - PassInstrumentationCallbacks registers callbacks and provides access 18 /// to them for PassInstrumentation. 19 /// 20 /// PassInstrumentation object is being used as a result of 21 /// PassInstrumentationAnalysis (so it is intended to be easily copyable). 22 /// 23 /// Intended scheme of use for Pass Instrumentation is as follows: 24 /// - register instrumentation callbacks in PassInstrumentationCallbacks 25 /// instance. PassBuilder provides helper for that. 26 /// 27 /// - register PassInstrumentationAnalysis with all the PassManagers. 28 /// PassBuilder handles that automatically when registering analyses. 29 /// 30 /// - Pass Manager requests PassInstrumentationAnalysis from analysis manager 31 /// and gets PassInstrumentation as its result. 32 /// 33 /// - Pass Manager invokes PassInstrumentation entry points appropriately, 34 /// passing StringRef identification ("name") of the pass currently being 35 /// executed and IRUnit it works on. There can be different schemes of 36 /// providing names in future, currently it is just a name() of the pass. 37 /// 38 /// - PassInstrumentation wraps address of IRUnit into llvm::Any and passes 39 /// control to all the registered callbacks. Note that we specifically wrap 40 /// 'const IRUnitT*' so as to avoid any accidental changes to IR in 41 /// instrumenting callbacks. 42 /// 43 /// - Some instrumentation points (BeforePass) allow to control execution 44 /// of a pass. For those callbacks returning false means pass will not be 45 /// executed. 46 /// 47 //===----------------------------------------------------------------------===// 48 49 #ifndef LLVM_IR_PASSINSTRUMENTATION_H 50 #define LLVM_IR_PASSINSTRUMENTATION_H 51 52 #include "llvm/ADT/Any.h" 53 #include "llvm/ADT/FunctionExtras.h" 54 #include "llvm/ADT/SmallVector.h" 55 #include "llvm/ADT/StringMap.h" 56 #include <type_traits> 57 58 namespace llvm { 59 60 class PreservedAnalyses; 61 class StringRef; 62 63 /// This class manages callbacks registration, as well as provides a way for 64 /// PassInstrumentation to pass control to the registered callbacks. 65 class PassInstrumentationCallbacks { 66 public: 67 // Before/After callbacks accept IRUnits whenever appropriate, so they need 68 // to take them as constant pointers, wrapped with llvm::Any. 69 // For the case when IRUnit has been invalidated there is a different 70 // callback to use - AfterPassInvalidated. 71 // We call all BeforePassFuncs to determine if a pass should run or not. 72 // BeforeNonSkippedPassFuncs are called only if the pass should run. 73 // TODO: currently AfterPassInvalidated does not accept IRUnit, since passing 74 // already invalidated IRUnit is unsafe. There are ways to handle invalidated 75 // IRUnits in a safe way, and we might pursue that as soon as there is a 76 // useful instrumentation that needs it. 77 using BeforePassFunc = bool(StringRef, Any); 78 using BeforeSkippedPassFunc = void(StringRef, Any); 79 using BeforeNonSkippedPassFunc = void(StringRef, Any); 80 using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &); 81 using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &); 82 using BeforeAnalysisFunc = void(StringRef, Any); 83 using AfterAnalysisFunc = void(StringRef, Any); 84 85 public: PassInstrumentationCallbacks()86 PassInstrumentationCallbacks() {} 87 88 /// Copying PassInstrumentationCallbacks is not intended. 89 PassInstrumentationCallbacks(const PassInstrumentationCallbacks &) = delete; 90 void operator=(const PassInstrumentationCallbacks &) = delete; 91 92 template <typename CallableT> registerShouldRunOptionalPassCallback(CallableT C)93 void registerShouldRunOptionalPassCallback(CallableT C) { 94 ShouldRunOptionalPassCallbacks.emplace_back(std::move(C)); 95 } 96 97 template <typename CallableT> registerBeforeSkippedPassCallback(CallableT C)98 void registerBeforeSkippedPassCallback(CallableT C) { 99 BeforeSkippedPassCallbacks.emplace_back(std::move(C)); 100 } 101 102 template <typename CallableT> registerBeforeNonSkippedPassCallback(CallableT C)103 void registerBeforeNonSkippedPassCallback(CallableT C) { 104 BeforeNonSkippedPassCallbacks.emplace_back(std::move(C)); 105 } 106 registerAfterPassCallback(CallableT C)107 template <typename CallableT> void registerAfterPassCallback(CallableT C) { 108 AfterPassCallbacks.emplace_back(std::move(C)); 109 } 110 111 template <typename CallableT> registerAfterPassInvalidatedCallback(CallableT C)112 void registerAfterPassInvalidatedCallback(CallableT C) { 113 AfterPassInvalidatedCallbacks.emplace_back(std::move(C)); 114 } 115 116 template <typename CallableT> registerBeforeAnalysisCallback(CallableT C)117 void registerBeforeAnalysisCallback(CallableT C) { 118 BeforeAnalysisCallbacks.emplace_back(std::move(C)); 119 } 120 121 template <typename CallableT> registerAfterAnalysisCallback(CallableT C)122 void registerAfterAnalysisCallback(CallableT C) { 123 AfterAnalysisCallbacks.emplace_back(std::move(C)); 124 } 125 126 /// Add a class name to pass name mapping for use by pass instrumentation. 127 void addClassToPassName(StringRef ClassName, StringRef PassName); 128 /// Get the pass name for a given pass class name. 129 StringRef getPassNameForClassName(StringRef ClassName); 130 /// Whether or not the class to pass name map contains the pass name. 131 bool hasPassName(StringRef PassName); 132 133 private: 134 friend class PassInstrumentation; 135 136 /// These are only run on passes that are not required. They return false when 137 /// an optional pass should be skipped. 138 SmallVector<llvm::unique_function<BeforePassFunc>, 4> 139 ShouldRunOptionalPassCallbacks; 140 /// These are run on passes that are skipped. 141 SmallVector<llvm::unique_function<BeforeSkippedPassFunc>, 4> 142 BeforeSkippedPassCallbacks; 143 /// These are run on passes that are about to be run. 144 SmallVector<llvm::unique_function<BeforeNonSkippedPassFunc>, 4> 145 BeforeNonSkippedPassCallbacks; 146 /// These are run on passes that have just run. 147 SmallVector<llvm::unique_function<AfterPassFunc>, 4> AfterPassCallbacks; 148 /// These are run passes that have just run on invalidated IR. 149 SmallVector<llvm::unique_function<AfterPassInvalidatedFunc>, 4> 150 AfterPassInvalidatedCallbacks; 151 /// These are run on analyses that are about to be run. 152 SmallVector<llvm::unique_function<BeforeAnalysisFunc>, 4> 153 BeforeAnalysisCallbacks; 154 /// These are run on analyses that have been run. 155 SmallVector<llvm::unique_function<AfterAnalysisFunc>, 4> 156 AfterAnalysisCallbacks; 157 158 StringMap<std::string> ClassToPassName; 159 }; 160 161 /// This class provides instrumentation entry points for the Pass Manager, 162 /// doing calls to callbacks registered in PassInstrumentationCallbacks. 163 class PassInstrumentation { 164 PassInstrumentationCallbacks *Callbacks; 165 166 // Template argument PassT of PassInstrumentation::runBeforePass could be two 167 // kinds: (1) a regular pass inherited from PassInfoMixin (happen when 168 // creating a adaptor pass for a regular pass); (2) a type-erased PassConcept 169 // created from (1). Here we want to make case (1) skippable unconditionally 170 // since they are regular passes. We call PassConcept::isRequired to decide 171 // for case (2). 172 template <typename PassT> 173 using has_required_t = decltype(std::declval<PassT &>().isRequired()); 174 175 template <typename PassT> 176 static std::enable_if_t<is_detected<has_required_t, PassT>::value, bool> isRequired(const PassT & Pass)177 isRequired(const PassT &Pass) { 178 return Pass.isRequired(); 179 } 180 template <typename PassT> 181 static std::enable_if_t<!is_detected<has_required_t, PassT>::value, bool> isRequired(const PassT & Pass)182 isRequired(const PassT &Pass) { 183 return false; 184 } 185 186 public: 187 /// Callbacks object is not owned by PassInstrumentation, its life-time 188 /// should at least match the life-time of corresponding 189 /// PassInstrumentationAnalysis (which usually is till the end of current 190 /// compilation). 191 PassInstrumentation(PassInstrumentationCallbacks *CB = nullptr) Callbacks(CB)192 : Callbacks(CB) {} 193 194 /// BeforePass instrumentation point - takes \p Pass instance to be executed 195 /// and constant reference to IR it operates on. \Returns true if pass is 196 /// allowed to be executed. These are only run on optional pass since required 197 /// passes must always be run. This allows these callbacks to print info when 198 /// they want to skip a pass. 199 template <typename IRUnitT, typename PassT> runBeforePass(const PassT & Pass,const IRUnitT & IR)200 bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const { 201 if (!Callbacks) 202 return true; 203 204 bool ShouldRun = true; 205 if (!isRequired(Pass)) { 206 for (auto &C : Callbacks->ShouldRunOptionalPassCallbacks) 207 ShouldRun &= C(Pass.name(), llvm::Any(&IR)); 208 } 209 210 if (ShouldRun) { 211 for (auto &C : Callbacks->BeforeNonSkippedPassCallbacks) 212 C(Pass.name(), llvm::Any(&IR)); 213 } else { 214 for (auto &C : Callbacks->BeforeSkippedPassCallbacks) 215 C(Pass.name(), llvm::Any(&IR)); 216 } 217 218 return ShouldRun; 219 } 220 221 /// AfterPass instrumentation point - takes \p Pass instance that has 222 /// just been executed and constant reference to \p IR it operates on. 223 /// \p IR is guaranteed to be valid at this point. 224 template <typename IRUnitT, typename PassT> runAfterPass(const PassT & Pass,const IRUnitT & IR,const PreservedAnalyses & PA)225 void runAfterPass(const PassT &Pass, const IRUnitT &IR, 226 const PreservedAnalyses &PA) const { 227 if (Callbacks) 228 for (auto &C : Callbacks->AfterPassCallbacks) 229 C(Pass.name(), llvm::Any(&IR), PA); 230 } 231 232 /// AfterPassInvalidated instrumentation point - takes \p Pass instance 233 /// that has just been executed. For use when IR has been invalidated 234 /// by \p Pass execution. 235 template <typename IRUnitT, typename PassT> runAfterPassInvalidated(const PassT & Pass,const PreservedAnalyses & PA)236 void runAfterPassInvalidated(const PassT &Pass, 237 const PreservedAnalyses &PA) const { 238 if (Callbacks) 239 for (auto &C : Callbacks->AfterPassInvalidatedCallbacks) 240 C(Pass.name(), PA); 241 } 242 243 /// BeforeAnalysis instrumentation point - takes \p Analysis instance 244 /// to be executed and constant reference to IR it operates on. 245 template <typename IRUnitT, typename PassT> runBeforeAnalysis(const PassT & Analysis,const IRUnitT & IR)246 void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const { 247 if (Callbacks) 248 for (auto &C : Callbacks->BeforeAnalysisCallbacks) 249 C(Analysis.name(), llvm::Any(&IR)); 250 } 251 252 /// AfterAnalysis instrumentation point - takes \p Analysis instance 253 /// that has just been executed and constant reference to IR it operated on. 254 template <typename IRUnitT, typename PassT> runAfterAnalysis(const PassT & Analysis,const IRUnitT & IR)255 void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const { 256 if (Callbacks) 257 for (auto &C : Callbacks->AfterAnalysisCallbacks) 258 C(Analysis.name(), llvm::Any(&IR)); 259 } 260 261 /// Handle invalidation from the pass manager when PassInstrumentation 262 /// is used as the result of PassInstrumentationAnalysis. 263 /// 264 /// On attempt to invalidate just return false. There is nothing to become 265 /// invalid here. 266 template <typename IRUnitT, typename... ExtraArgsT> invalidate(IRUnitT &,const class llvm::PreservedAnalyses &,ExtraArgsT...)267 bool invalidate(IRUnitT &, const class llvm::PreservedAnalyses &, 268 ExtraArgsT...) { 269 return false; 270 } 271 272 template <typename CallableT> pushBeforeNonSkippedPassCallback(CallableT C)273 void pushBeforeNonSkippedPassCallback(CallableT C) { 274 if (Callbacks) 275 Callbacks->BeforeNonSkippedPassCallbacks.emplace_back(std::move(C)); 276 } popBeforeNonSkippedPassCallback()277 void popBeforeNonSkippedPassCallback() { 278 if (Callbacks) 279 Callbacks->BeforeNonSkippedPassCallbacks.pop_back(); 280 } 281 }; 282 283 bool isSpecialPass(StringRef PassID, const std::vector<StringRef> &Specials); 284 285 } // namespace llvm 286 287 #endif 288