| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 1 | //===- Standard pass instrumentations handling ----------------*- C++ -*--===// |
| 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 |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// |
| 10 | /// This file defines IR-printing pass instrumentation callbacks as well as |
| 11 | /// StandardInstrumentations class that manages standard pass instrumentations. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Passes/StandardInstrumentations.h" |
| Arthur Eubanks | b13b858 | 2020-07-21 09:48:22 -0700 | [diff] [blame] | 16 | #include "llvm/ADT/Any.h" |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Optional.h" |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraphSCCPass.h" |
| 19 | #include "llvm/Analysis/LazyCallGraph.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | #include "llvm/IR/IRPrintingPasses.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/PassInstrumentation.h" |
| Arthur Eubanks | b13b858 | 2020-07-21 09:48:22 -0700 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/FormatVariadic.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| Arthur Eubanks | b13b858 | 2020-07-21 09:48:22 -0700 | [diff] [blame] | 32 | // TODO: remove once all required passes are marked as such. |
| 33 | static cl::opt<bool> |
| 34 | EnableOptnone("enable-npm-optnone", cl::init(false), |
| 35 | cl::desc("Enable skipping optional passes optnone functions " |
| 36 | "under new pass manager")); |
| 37 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 38 | namespace { |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 39 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 40 | /// Extracting Module out of \p IR unit. Also fills a textual description |
| 41 | /// of \p IR for use in header when printing. |
| 42 | Optional<std::pair<const Module *, std::string>> unwrapModule(Any IR) { |
| 43 | if (any_isa<const Module *>(IR)) |
| 44 | return std::make_pair(any_cast<const Module *>(IR), std::string()); |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 45 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 46 | if (any_isa<const Function *>(IR)) { |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 47 | const Function *F = any_cast<const Function *>(IR); |
| 48 | if (!llvm::isFunctionInPrintList(F->getName())) |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 49 | return None; |
| 50 | const Module *M = F->getParent(); |
| 51 | return std::make_pair(M, formatv(" (function: {0})", F->getName()).str()); |
| 52 | } |
| 53 | |
| 54 | if (any_isa<const LazyCallGraph::SCC *>(IR)) { |
| Fedor Sergeev | 0dc6ac9 | 2018-10-15 10:46:35 +0000 | [diff] [blame] | 55 | const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); |
| Fedor Sergeev | 0dc6ac9 | 2018-10-15 10:46:35 +0000 | [diff] [blame] | 56 | for (const LazyCallGraph::Node &N : *C) { |
| 57 | const Function &F = N.getFunction(); |
| 58 | if (!F.isDeclaration() && isFunctionInPrintList(F.getName())) { |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 59 | const Module *M = F.getParent(); |
| 60 | return std::make_pair(M, formatv(" (scc: {0})", C->getName()).str()); |
| Fedor Sergeev | 0dc6ac9 | 2018-10-15 10:46:35 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 63 | return None; |
| 64 | } |
| 65 | |
| 66 | if (any_isa<const Loop *>(IR)) { |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 67 | const Loop *L = any_cast<const Loop *>(IR); |
| 68 | const Function *F = L->getHeader()->getParent(); |
| 69 | if (!isFunctionInPrintList(F->getName())) |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 70 | return None; |
| 71 | const Module *M = F->getParent(); |
| 72 | std::string LoopName; |
| 73 | raw_string_ostream ss(LoopName); |
| 74 | L->getHeader()->printAsOperand(ss, false); |
| 75 | return std::make_pair(M, formatv(" (loop: {0})", ss.str()).str()); |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 76 | } |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 77 | |
| 78 | llvm_unreachable("Unknown IR unit"); |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 81 | void printIR(const Function *F, StringRef Banner, |
| 82 | StringRef Extra = StringRef()) { |
| 83 | if (!llvm::isFunctionInPrintList(F->getName())) |
| 84 | return; |
| 85 | dbgs() << Banner << Extra << "\n" << static_cast<const Value &>(*F); |
| 86 | } |
| Hongtao Yu | bae33a7 | 2020-02-23 15:13:27 -0800 | [diff] [blame] | 87 | |
| 88 | void printIR(const Module *M, StringRef Banner, StringRef Extra = StringRef()) { |
| 89 | if (llvm::isFunctionInPrintList("*") || llvm::forcePrintModuleIR()) { |
| 90 | dbgs() << Banner << Extra << "\n"; |
| 91 | M->print(dbgs(), nullptr, false); |
| 92 | } else { |
| 93 | for (const auto &F : M->functions()) { |
| 94 | printIR(&F, Banner, Extra); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 99 | void printIR(const LazyCallGraph::SCC *C, StringRef Banner, |
| 100 | StringRef Extra = StringRef()) { |
| 101 | bool BannerPrinted = false; |
| 102 | for (const LazyCallGraph::Node &N : *C) { |
| 103 | const Function &F = N.getFunction(); |
| 104 | if (!F.isDeclaration() && llvm::isFunctionInPrintList(F.getName())) { |
| 105 | if (!BannerPrinted) { |
| 106 | dbgs() << Banner << Extra << "\n"; |
| 107 | BannerPrinted = true; |
| 108 | } |
| 109 | F.print(dbgs()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | void printIR(const Loop *L, StringRef Banner) { |
| 114 | const Function *F = L->getHeader()->getParent(); |
| 115 | if (!llvm::isFunctionInPrintList(F->getName())) |
| 116 | return; |
| Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 117 | llvm::printLoop(const_cast<Loop &>(*L), dbgs(), std::string(Banner)); |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into |
| 121 | /// llvm::Any and does actual print job. |
| 122 | void unwrapAndPrint(Any IR, StringRef Banner, bool ForceModule = false) { |
| 123 | if (ForceModule) { |
| 124 | if (auto UnwrappedModule = unwrapModule(IR)) |
| 125 | printIR(UnwrappedModule->first, Banner, UnwrappedModule->second); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (any_isa<const Module *>(IR)) { |
| 130 | const Module *M = any_cast<const Module *>(IR); |
| 131 | assert(M && "module should be valid for printing"); |
| 132 | printIR(M, Banner); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (any_isa<const Function *>(IR)) { |
| 137 | const Function *F = any_cast<const Function *>(IR); |
| 138 | assert(F && "function should be valid for printing"); |
| 139 | printIR(F, Banner); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (any_isa<const LazyCallGraph::SCC *>(IR)) { |
| 144 | const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); |
| 145 | assert(C && "scc should be valid for printing"); |
| Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 146 | std::string Extra = std::string(formatv(" (scc: {0})", C->getName())); |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 147 | printIR(C, Banner, Extra); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | if (any_isa<const Loop *>(IR)) { |
| 152 | const Loop *L = any_cast<const Loop *>(IR); |
| 153 | assert(L && "Loop should be valid for printing"); |
| 154 | printIR(L, Banner); |
| 155 | return; |
| 156 | } |
| 157 | llvm_unreachable("Unknown wrapped IR type"); |
| 158 | } |
| 159 | |
| 160 | } // namespace |
| 161 | |
| 162 | PrintIRInstrumentation::~PrintIRInstrumentation() { |
| 163 | assert(ModuleDescStack.empty() && "ModuleDescStack is not empty at exit"); |
| 164 | } |
| 165 | |
| 166 | void PrintIRInstrumentation::pushModuleDesc(StringRef PassID, Any IR) { |
| 167 | assert(StoreModuleDesc); |
| 168 | const Module *M = nullptr; |
| 169 | std::string Extra; |
| 170 | if (auto UnwrappedModule = unwrapModule(IR)) |
| 171 | std::tie(M, Extra) = UnwrappedModule.getValue(); |
| 172 | ModuleDescStack.emplace_back(M, Extra, PassID); |
| 173 | } |
| 174 | |
| 175 | PrintIRInstrumentation::PrintModuleDesc |
| 176 | PrintIRInstrumentation::popModuleDesc(StringRef PassID) { |
| 177 | assert(!ModuleDescStack.empty() && "empty ModuleDescStack"); |
| 178 | PrintModuleDesc ModuleDesc = ModuleDescStack.pop_back_val(); |
| 179 | assert(std::get<2>(ModuleDesc).equals(PassID) && "malformed ModuleDescStack"); |
| 180 | return ModuleDesc; |
| 181 | } |
| 182 | |
| Yuanfang Chen | 7a2e112 | 2020-07-28 16:31:46 -0700 | [diff] [blame] | 183 | void PrintIRInstrumentation::printBeforePass(StringRef PassID, Any IR) { |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 184 | if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<")) |
| Yuanfang Chen | 7a2e112 | 2020-07-28 16:31:46 -0700 | [diff] [blame] | 185 | return; |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 186 | |
| 187 | // Saving Module for AfterPassInvalidated operations. |
| 188 | // Note: here we rely on a fact that we do not change modules while |
| 189 | // traversing the pipeline, so the latest captured module is good |
| 190 | // for all print operations that has not happen yet. |
| 191 | if (StoreModuleDesc && llvm::shouldPrintAfterPass(PassID)) |
| 192 | pushModuleDesc(PassID, IR); |
| 193 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 194 | if (!llvm::shouldPrintBeforePass(PassID)) |
| Yuanfang Chen | 7a2e112 | 2020-07-28 16:31:46 -0700 | [diff] [blame] | 195 | return; |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 196 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 197 | SmallString<20> Banner = formatv("*** IR Dump Before {0} ***", PassID); |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 198 | unwrapAndPrint(IR, Banner, llvm::forcePrintModuleIR()); |
| Yuanfang Chen | 7a2e112 | 2020-07-28 16:31:46 -0700 | [diff] [blame] | 199 | return; |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 202 | void PrintIRInstrumentation::printAfterPass(StringRef PassID, Any IR) { |
| 203 | if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<")) |
| 204 | return; |
| 205 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 206 | if (!llvm::shouldPrintAfterPass(PassID)) |
| 207 | return; |
| 208 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 209 | if (StoreModuleDesc) |
| 210 | popModuleDesc(PassID); |
| 211 | |
| 212 | SmallString<20> Banner = formatv("*** IR Dump After {0} ***", PassID); |
| 213 | unwrapAndPrint(IR, Banner, llvm::forcePrintModuleIR()); |
| 214 | } |
| 215 | |
| 216 | void PrintIRInstrumentation::printAfterPassInvalidated(StringRef PassID) { |
| 217 | if (!StoreModuleDesc || !llvm::shouldPrintAfterPass(PassID)) |
| 218 | return; |
| 219 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 220 | if (PassID.startswith("PassManager<") || PassID.contains("PassAdaptor<")) |
| 221 | return; |
| 222 | |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 223 | const Module *M; |
| 224 | std::string Extra; |
| 225 | StringRef StoredPassID; |
| 226 | std::tie(M, Extra, StoredPassID) = popModuleDesc(PassID); |
| 227 | // Additional filtering (e.g. -filter-print-func) can lead to module |
| 228 | // printing being skipped. |
| 229 | if (!M) |
| 230 | return; |
| 231 | |
| 232 | SmallString<20> Banner = |
| 233 | formatv("*** IR Dump After {0} *** invalidated: ", PassID); |
| 234 | printIR(M, Banner, Extra); |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 235 | } |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 236 | |
| 237 | void PrintIRInstrumentation::registerCallbacks( |
| 238 | PassInstrumentationCallbacks &PIC) { |
| 239 | // BeforePass callback is not just for printing, it also saves a Module |
| 240 | // for later use in AfterPassInvalidated. |
| 241 | StoreModuleDesc = llvm::forcePrintModuleIR() && llvm::shouldPrintAfterPass(); |
| 242 | if (llvm::shouldPrintBeforePass() || StoreModuleDesc) |
| Yuanfang Chen | 7a2e112 | 2020-07-28 16:31:46 -0700 | [diff] [blame] | 243 | PIC.registerBeforeNonSkippedPassCallback( |
| 244 | [this](StringRef P, Any IR) { this->printBeforePass(P, IR); }); |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 245 | |
| 246 | if (llvm::shouldPrintAfterPass()) { |
| 247 | PIC.registerAfterPassCallback( |
| 248 | [this](StringRef P, Any IR) { this->printAfterPass(P, IR); }); |
| 249 | PIC.registerAfterPassInvalidatedCallback( |
| 250 | [this](StringRef P) { this->printAfterPassInvalidated(P); }); |
| 251 | } |
| 252 | } |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 253 | |
| Arthur Eubanks | b13b858 | 2020-07-21 09:48:22 -0700 | [diff] [blame] | 254 | void OptNoneInstrumentation::registerCallbacks( |
| 255 | PassInstrumentationCallbacks &PIC) { |
| 256 | PIC.registerBeforePassCallback( |
| 257 | [this](StringRef P, Any IR) { return this->skip(P, IR); }); |
| 258 | } |
| 259 | |
| 260 | bool OptNoneInstrumentation::skip(StringRef PassID, Any IR) { |
| 261 | if (!EnableOptnone) |
| 262 | return true; |
| 263 | const Function *F = nullptr; |
| 264 | if (any_isa<const Function *>(IR)) { |
| 265 | F = any_cast<const Function *>(IR); |
| 266 | } else if (any_isa<const Loop *>(IR)) { |
| 267 | F = any_cast<const Loop *>(IR)->getHeader()->getParent(); |
| 268 | } |
| 269 | if (F && F->hasOptNone()) { |
| 270 | if (DebugLogging) |
| 271 | dbgs() << "Skipping pass: " << PassID << " (optnone)\n"; |
| 272 | return false; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 277 | void StandardInstrumentations::registerCallbacks( |
| 278 | PassInstrumentationCallbacks &PIC) { |
| Fedor Sergeev | 2d94c22 | 2018-12-21 11:49:05 +0000 | [diff] [blame] | 279 | PrintIR.registerCallbacks(PIC); |
| Fedor Sergeev | 884e52a | 2018-10-05 22:32:01 +0000 | [diff] [blame] | 280 | TimePasses.registerCallbacks(PIC); |
| Arthur Eubanks | b13b858 | 2020-07-21 09:48:22 -0700 | [diff] [blame] | 281 | OptNone.registerCallbacks(PIC); |
| Fedor Sergeev | 662e568 | 2018-09-24 16:08:15 +0000 | [diff] [blame] | 282 | } |