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/DenseMap.h" 56 #include "llvm/IR/PassManager.h" 57 #include <type_traits> 58 #include <vector> 59 60 namespace llvm { 61 62 class PreservedAnalyses; 63 class StringRef; 64 65 /// This class manages callbacks registration, as well as provides a way for 66 /// PassInstrumentation to pass control to the registered callbacks. 67 class PassInstrumentationCallbacks { 68 public: 69 // Before/After callbacks accept IRUnits whenever appropriate, so they need 70 // to take them as constant pointers, wrapped with llvm::Any. 71 // For the case when IRUnit has been invalidated there is a different 72 // callback to use - AfterPassInvalidated. 73 // We call all BeforePassFuncs to determine if a pass should run or not. 74 // BeforeNonSkippedPassFuncs are called only if the pass should run. 75 // TODO: currently AfterPassInvalidated does not accept IRUnit, since passing 76 // already invalidated IRUnit is unsafe. There are ways to handle invalidated 77 // IRUnits in a safe way, and we might pursue that as soon as there is a 78 // useful instrumentation that needs it. 79 using BeforePassFunc = bool(StringRef, Any); 80 using BeforeSkippedPassFunc = void(StringRef, Any); 81 using BeforeNonSkippedPassFunc = void(StringRef, Any); 82 using AfterPassFunc = void(StringRef, Any, const PreservedAnalyses &); 83 using AfterPassInvalidatedFunc = void(StringRef, const PreservedAnalyses &); 84 using BeforeAnalysisFunc = void(StringRef, Any); 85 using AfterAnalysisFunc = void(StringRef, Any); 86 using AnalysisInvalidatedFunc = void(StringRef, Any); 87 using AnalysesClearedFunc = void(StringRef); 88 89 public: 90 PassInstrumentationCallbacks() = default; 91 92 /// Copying PassInstrumentationCallbacks is not intended. 93 PassInstrumentationCallbacks(const PassInstrumentationCallbacks &) = delete; 94 void operator=(const PassInstrumentationCallbacks &) = delete; 95 96 template <typename CallableT> registerShouldRunOptionalPassCallback(CallableT C)97 void registerShouldRunOptionalPassCallback(CallableT C) { 98 ShouldRunOptionalPassCallbacks.emplace_back(std::move(C)); 99 } 100 101 template <typename CallableT> registerBeforeSkippedPassCallback(CallableT C)102 void registerBeforeSkippedPassCallback(CallableT C) { 103 BeforeSkippedPassCallbacks.emplace_back(std::move(C)); 104 } 105 106 template <typename CallableT> registerBeforeNonSkippedPassCallback(CallableT C)107 void registerBeforeNonSkippedPassCallback(CallableT C) { 108 BeforeNonSkippedPassCallbacks.emplace_back(std::move(C)); 109 } 110 111 template <typename CallableT> 112 void registerAfterPassCallback(CallableT C, bool ToFront = false) { 113 if (ToFront) 114 AfterPassCallbacks.insert(AfterPassCallbacks.begin(), std::move(C)); 115 else 116 AfterPassCallbacks.emplace_back(std::move(C)); 117 } 118 119 template <typename CallableT> 120 void registerAfterPassInvalidatedCallback(CallableT C, bool ToFront = false) { 121 if (ToFront) 122 AfterPassInvalidatedCallbacks.insert( 123 AfterPassInvalidatedCallbacks.begin(), std::move(C)); 124 else 125 AfterPassInvalidatedCallbacks.emplace_back(std::move(C)); 126 } 127 128 template <typename CallableT> registerBeforeAnalysisCallback(CallableT C)129 void registerBeforeAnalysisCallback(CallableT C) { 130 BeforeAnalysisCallbacks.emplace_back(std::move(C)); 131 } 132 133 template <typename CallableT> 134 void registerAfterAnalysisCallback(CallableT C, bool ToFront = false) { 135 if (ToFront) 136 AfterAnalysisCallbacks.insert(AfterAnalysisCallbacks.begin(), 137 std::move(C)); 138 else 139 AfterAnalysisCallbacks.emplace_back(std::move(C)); 140 } 141 142 template <typename CallableT> registerAnalysisInvalidatedCallback(CallableT C)143 void registerAnalysisInvalidatedCallback(CallableT C) { 144 AnalysisInvalidatedCallbacks.emplace_back(std::move(C)); 145 } 146 147 template <typename CallableT> registerAnalysesClearedCallback(CallableT C)148 void registerAnalysesClearedCallback(CallableT C) { 149 AnalysesClearedCallbacks.emplace_back(std::move(C)); 150 } 151 152 template <typename CallableT> registerClassToPassNameCallback(CallableT C)153 void registerClassToPassNameCallback(CallableT C) { 154 ClassToPassNameCallbacks.emplace_back(std::move(C)); 155 } 156 157 /// Add a class name to pass name mapping for use by pass instrumentation. 158 void addClassToPassName(StringRef ClassName, StringRef PassName); 159 /// Get the pass name for a given pass class name. 160 StringRef getPassNameForClassName(StringRef ClassName); 161 162 private: 163 friend class PassInstrumentation; 164 165 /// These are only run on passes that are not required. They return false when 166 /// an optional pass should be skipped. 167 SmallVector<llvm::unique_function<BeforePassFunc>, 4> 168 ShouldRunOptionalPassCallbacks; 169 /// These are run on passes that are skipped. 170 SmallVector<llvm::unique_function<BeforeSkippedPassFunc>, 4> 171 BeforeSkippedPassCallbacks; 172 /// These are run on passes that are about to be run. 173 SmallVector<llvm::unique_function<BeforeNonSkippedPassFunc>, 4> 174 BeforeNonSkippedPassCallbacks; 175 /// These are run on passes that have just run. 176 SmallVector<llvm::unique_function<AfterPassFunc>, 4> AfterPassCallbacks; 177 /// These are run passes that have just run on invalidated IR. 178 SmallVector<llvm::unique_function<AfterPassInvalidatedFunc>, 4> 179 AfterPassInvalidatedCallbacks; 180 /// These are run on analyses that are about to be run. 181 SmallVector<llvm::unique_function<BeforeAnalysisFunc>, 4> 182 BeforeAnalysisCallbacks; 183 /// These are run on analyses that have been run. 184 SmallVector<llvm::unique_function<AfterAnalysisFunc>, 4> 185 AfterAnalysisCallbacks; 186 /// These are run on analyses that have been invalidated. 187 SmallVector<llvm::unique_function<AnalysisInvalidatedFunc>, 4> 188 AnalysisInvalidatedCallbacks; 189 /// These are run on analyses that have been cleared. 190 SmallVector<llvm::unique_function<AnalysesClearedFunc>, 4> 191 AnalysesClearedCallbacks; 192 193 SmallVector<llvm::unique_function<void ()>, 4> ClassToPassNameCallbacks; 194 DenseMap<StringRef, std::string> ClassToPassName; 195 }; 196 197 /// This class provides instrumentation entry points for the Pass Manager, 198 /// doing calls to callbacks registered in PassInstrumentationCallbacks. 199 class PassInstrumentation { 200 PassInstrumentationCallbacks *Callbacks; 201 202 // Template argument PassT of PassInstrumentation::runBeforePass could be two 203 // kinds: (1) a regular pass inherited from PassInfoMixin (happen when 204 // creating a adaptor pass for a regular pass); (2) a type-erased PassConcept 205 // created from (1). Here we want to make case (1) skippable unconditionally 206 // since they are regular passes. We call PassConcept::isRequired to decide 207 // for case (2). 208 template <typename PassT> 209 using has_required_t = decltype(std::declval<PassT &>().isRequired()); 210 211 template <typename PassT> 212 static std::enable_if_t<is_detected<has_required_t, PassT>::value, bool> isRequired(const PassT & Pass)213 isRequired(const PassT &Pass) { 214 return Pass.isRequired(); 215 } 216 template <typename PassT> 217 static std::enable_if_t<!is_detected<has_required_t, PassT>::value, bool> isRequired(const PassT & Pass)218 isRequired(const PassT &Pass) { 219 return false; 220 } 221 222 public: 223 /// Callbacks object is not owned by PassInstrumentation, its life-time 224 /// should at least match the life-time of corresponding 225 /// PassInstrumentationAnalysis (which usually is till the end of current 226 /// compilation). 227 PassInstrumentation(PassInstrumentationCallbacks *CB = nullptr) Callbacks(CB)228 : Callbacks(CB) {} 229 230 /// BeforePass instrumentation point - takes \p Pass instance to be executed 231 /// and constant reference to IR it operates on. \Returns true if pass is 232 /// allowed to be executed. These are only run on optional pass since required 233 /// passes must always be run. This allows these callbacks to print info when 234 /// they want to skip a pass. 235 template <typename IRUnitT, typename PassT> runBeforePass(const PassT & Pass,const IRUnitT & IR)236 bool runBeforePass(const PassT &Pass, const IRUnitT &IR) const { 237 if (!Callbacks) 238 return true; 239 240 bool ShouldRun = true; 241 if (!isRequired(Pass)) { 242 for (auto &C : Callbacks->ShouldRunOptionalPassCallbacks) 243 ShouldRun &= C(Pass.name(), llvm::Any(&IR)); 244 } 245 246 if (ShouldRun) { 247 for (auto &C : Callbacks->BeforeNonSkippedPassCallbacks) 248 C(Pass.name(), llvm::Any(&IR)); 249 } else { 250 for (auto &C : Callbacks->BeforeSkippedPassCallbacks) 251 C(Pass.name(), llvm::Any(&IR)); 252 } 253 254 return ShouldRun; 255 } 256 257 /// AfterPass instrumentation point - takes \p Pass instance that has 258 /// just been executed and constant reference to \p IR it operates on. 259 /// \p IR is guaranteed to be valid at this point. 260 template <typename IRUnitT, typename PassT> runAfterPass(const PassT & Pass,const IRUnitT & IR,const PreservedAnalyses & PA)261 void runAfterPass(const PassT &Pass, const IRUnitT &IR, 262 const PreservedAnalyses &PA) const { 263 if (Callbacks) 264 for (auto &C : Callbacks->AfterPassCallbacks) 265 C(Pass.name(), llvm::Any(&IR), PA); 266 } 267 268 /// AfterPassInvalidated instrumentation point - takes \p Pass instance 269 /// that has just been executed. For use when IR has been invalidated 270 /// by \p Pass execution. 271 template <typename IRUnitT, typename PassT> runAfterPassInvalidated(const PassT & Pass,const PreservedAnalyses & PA)272 void runAfterPassInvalidated(const PassT &Pass, 273 const PreservedAnalyses &PA) const { 274 if (Callbacks) 275 for (auto &C : Callbacks->AfterPassInvalidatedCallbacks) 276 C(Pass.name(), PA); 277 } 278 279 /// BeforeAnalysis instrumentation point - takes \p Analysis instance 280 /// to be executed and constant reference to IR it operates on. 281 template <typename IRUnitT, typename PassT> runBeforeAnalysis(const PassT & Analysis,const IRUnitT & IR)282 void runBeforeAnalysis(const PassT &Analysis, const IRUnitT &IR) const { 283 if (Callbacks) 284 for (auto &C : Callbacks->BeforeAnalysisCallbacks) 285 C(Analysis.name(), llvm::Any(&IR)); 286 } 287 288 /// AfterAnalysis instrumentation point - takes \p Analysis instance 289 /// that has just been executed and constant reference to IR it operated on. 290 template <typename IRUnitT, typename PassT> runAfterAnalysis(const PassT & Analysis,const IRUnitT & IR)291 void runAfterAnalysis(const PassT &Analysis, const IRUnitT &IR) const { 292 if (Callbacks) 293 for (auto &C : Callbacks->AfterAnalysisCallbacks) 294 C(Analysis.name(), llvm::Any(&IR)); 295 } 296 297 /// AnalysisInvalidated instrumentation point - takes \p Analysis instance 298 /// that has just been invalidated and constant reference to IR it operated 299 /// on. 300 template <typename IRUnitT, typename PassT> runAnalysisInvalidated(const PassT & Analysis,const IRUnitT & IR)301 void runAnalysisInvalidated(const PassT &Analysis, const IRUnitT &IR) const { 302 if (Callbacks) 303 for (auto &C : Callbacks->AnalysisInvalidatedCallbacks) 304 C(Analysis.name(), llvm::Any(&IR)); 305 } 306 307 /// AnalysesCleared instrumentation point - takes name of IR that analyses 308 /// operated on. runAnalysesCleared(StringRef Name)309 void runAnalysesCleared(StringRef Name) const { 310 if (Callbacks) 311 for (auto &C : Callbacks->AnalysesClearedCallbacks) 312 C(Name); 313 } 314 315 /// Handle invalidation from the pass manager when PassInstrumentation 316 /// is used as the result of PassInstrumentationAnalysis. 317 /// 318 /// On attempt to invalidate just return false. There is nothing to become 319 /// invalid here. 320 template <typename IRUnitT, typename... ExtraArgsT> invalidate(IRUnitT &,const class llvm::PreservedAnalyses &,ExtraArgsT...)321 bool invalidate(IRUnitT &, const class llvm::PreservedAnalyses &, 322 ExtraArgsT...) { 323 return false; 324 } 325 326 template <typename CallableT> pushBeforeNonSkippedPassCallback(CallableT C)327 void pushBeforeNonSkippedPassCallback(CallableT C) { 328 if (Callbacks) 329 Callbacks->BeforeNonSkippedPassCallbacks.emplace_back(std::move(C)); 330 } popBeforeNonSkippedPassCallback()331 void popBeforeNonSkippedPassCallback() { 332 if (Callbacks) 333 Callbacks->BeforeNonSkippedPassCallbacks.pop_back(); 334 } 335 336 /// Get the pass name for a given pass class name. getPassNameForClassName(StringRef ClassName)337 StringRef getPassNameForClassName(StringRef ClassName) const { 338 if (Callbacks) 339 return Callbacks->getPassNameForClassName(ClassName); 340 return {}; 341 } 342 }; 343 344 bool isSpecialPass(StringRef PassID, const std::vector<StringRef> &Specials); 345 346 /// Pseudo-analysis pass that exposes the \c PassInstrumentation to pass 347 /// managers. 348 class PassInstrumentationAnalysis 349 : public AnalysisInfoMixin<PassInstrumentationAnalysis> { 350 friend AnalysisInfoMixin<PassInstrumentationAnalysis>; 351 static AnalysisKey Key; 352 353 PassInstrumentationCallbacks *Callbacks; 354 355 public: 356 /// PassInstrumentationCallbacks object is shared, owned by something else, 357 /// not this analysis. 358 PassInstrumentationAnalysis(PassInstrumentationCallbacks *Callbacks = nullptr) Callbacks(Callbacks)359 : Callbacks(Callbacks) {} 360 361 using Result = PassInstrumentation; 362 363 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs> run(IRUnitT &,AnalysisManagerT &,ExtraArgTs &&...)364 Result run(IRUnitT &, AnalysisManagerT &, ExtraArgTs &&...) { 365 return PassInstrumentation(Callbacks); 366 } 367 }; 368 369 370 } // namespace llvm 371 372 #endif 373