1 //===- IRPrinting.cpp -----------------------------------------------------===// 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 9 #include "PassDetail.h" 10 #include "mlir/IR/Module.h" 11 #include "mlir/Pass/PassManager.h" 12 #include "llvm/Support/Format.h" 13 #include "llvm/Support/FormatVariadic.h" 14 #include "llvm/Support/SHA1.h" 15 16 using namespace mlir; 17 using namespace mlir::detail; 18 19 namespace { 20 //===----------------------------------------------------------------------===// 21 // OperationFingerPrint 22 //===----------------------------------------------------------------------===// 23 24 /// A unique fingerprint for a specific operation, and all of it's internal 25 /// operations. 26 class OperationFingerPrint { 27 public: 28 OperationFingerPrint(Operation *topOp) { 29 llvm::SHA1 hasher; 30 31 // Hash each of the operations based upon their mutable bits: 32 topOp->walk([&](Operation *op) { 33 // - Operation pointer 34 addDataToHash(hasher, op); 35 // - Attributes 36 addDataToHash(hasher, op->getMutableAttrDict()); 37 // - Blocks in Regions 38 for (Region ®ion : op->getRegions()) { 39 for (Block &block : region) { 40 addDataToHash(hasher, &block); 41 for (BlockArgument arg : block.getArguments()) 42 addDataToHash(hasher, arg); 43 } 44 } 45 // - Location 46 addDataToHash(hasher, op->getLoc().getAsOpaquePointer()); 47 // - Operands 48 for (Value operand : op->getOperands()) 49 addDataToHash(hasher, operand); 50 // - Successors 51 for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i) 52 addDataToHash(hasher, op->getSuccessor(i)); 53 }); 54 hash = hasher.result(); 55 } 56 57 bool operator==(const OperationFingerPrint &other) const { 58 return hash == other.hash; 59 } 60 bool operator!=(const OperationFingerPrint &other) const { 61 return !(*this == other); 62 } 63 64 private: 65 template <typename T> void addDataToHash(llvm::SHA1 &hasher, const T &data) { 66 hasher.update( 67 ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(&data), sizeof(T))); 68 } 69 70 SmallString<20> hash; 71 }; 72 73 //===----------------------------------------------------------------------===// 74 // IRPrinter 75 //===----------------------------------------------------------------------===// 76 77 class IRPrinterInstrumentation : public PassInstrumentation { 78 public: 79 IRPrinterInstrumentation(std::unique_ptr<PassManager::IRPrinterConfig> config) 80 : config(std::move(config)) {} 81 82 private: 83 /// Instrumentation hooks. 84 void runBeforePass(Pass *pass, Operation *op) override; 85 void runAfterPass(Pass *pass, Operation *op) override; 86 void runAfterPassFailed(Pass *pass, Operation *op) override; 87 88 /// Configuration to use. 89 std::unique_ptr<PassManager::IRPrinterConfig> config; 90 91 /// The following is a set of fingerprints for operations that are currently 92 /// being operated on in a pass. This field is only used when the 93 /// configuration asked for change detection. 94 DenseMap<Pass *, OperationFingerPrint> beforePassFingerPrints; 95 }; 96 } // end anonymous namespace 97 98 /// Returns true if the given pass is hidden from IR printing. 99 static bool isHiddenPass(Pass *pass) { 100 return isa<OpToOpPassAdaptor>(pass) || isa<VerifierPass>(pass); 101 } 102 103 static void printIR(Operation *op, bool printModuleScope, raw_ostream &out, 104 OpPrintingFlags flags) { 105 // Check to see if we are printing the top-level module. 106 auto module = dyn_cast<ModuleOp>(op); 107 if (module && !op->getBlock()) 108 return module.print(out << "\n", flags); 109 110 // Otherwise, check to see if we are not printing at module scope. 111 if (!printModuleScope) 112 return op->print(out << "\n", flags.useLocalScope()); 113 114 // Otherwise, we are printing at module scope. 115 out << " ('" << op->getName() << "' operation"; 116 if (auto symbolName = 117 op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName())) 118 out << ": @" << symbolName.getValue(); 119 out << ")\n"; 120 121 // Find the top-level module operation. 122 auto *topLevelOp = op; 123 while (auto *parentOp = topLevelOp->getParentOp()) 124 topLevelOp = parentOp; 125 126 // Check to see if the top-level operation is actually a module in the case of 127 // invalid-ir. 128 if (auto module = dyn_cast<ModuleOp>(topLevelOp)) 129 module.print(out, flags); 130 else 131 topLevelOp->print(out, flags); 132 } 133 134 /// Instrumentation hooks. 135 void IRPrinterInstrumentation::runBeforePass(Pass *pass, Operation *op) { 136 if (isHiddenPass(pass)) 137 return; 138 // If the config asked to detect changes, record the current fingerprint. 139 if (config->shouldPrintAfterOnlyOnChange()) 140 beforePassFingerPrints.try_emplace(pass, op); 141 142 config->printBeforeIfEnabled(pass, op, [&](raw_ostream &out) { 143 out << formatv("// *** IR Dump Before {0} ***", pass->getName()); 144 printIR(op, config->shouldPrintAtModuleScope(), out, OpPrintingFlags()); 145 out << "\n\n"; 146 }); 147 } 148 149 void IRPrinterInstrumentation::runAfterPass(Pass *pass, Operation *op) { 150 if (isHiddenPass(pass)) 151 return; 152 // If the config asked to detect changes, compare the current fingerprint with 153 // the previous. 154 if (config->shouldPrintAfterOnlyOnChange()) { 155 auto fingerPrintIt = beforePassFingerPrints.find(pass); 156 assert(fingerPrintIt != beforePassFingerPrints.end() && 157 "expected valid fingerprint"); 158 // If the fingerprints are the same, we don't print the IR. 159 if (fingerPrintIt->second == OperationFingerPrint(op)) { 160 beforePassFingerPrints.erase(fingerPrintIt); 161 return; 162 } 163 beforePassFingerPrints.erase(fingerPrintIt); 164 } 165 166 config->printAfterIfEnabled(pass, op, [&](raw_ostream &out) { 167 out << formatv("// *** IR Dump After {0} ***", pass->getName()); 168 printIR(op, config->shouldPrintAtModuleScope(), out, OpPrintingFlags()); 169 out << "\n\n"; 170 }); 171 } 172 173 void IRPrinterInstrumentation::runAfterPassFailed(Pass *pass, Operation *op) { 174 if (isa<OpToOpPassAdaptor>(pass)) 175 return; 176 if (config->shouldPrintAfterOnlyOnChange()) 177 beforePassFingerPrints.erase(pass); 178 179 config->printAfterIfEnabled(pass, op, [&](raw_ostream &out) { 180 out << formatv("// *** IR Dump After {0} Failed ***", pass->getName()); 181 printIR(op, config->shouldPrintAtModuleScope(), out, 182 OpPrintingFlags().printGenericOpForm()); 183 out << "\n\n"; 184 }); 185 } 186 187 //===----------------------------------------------------------------------===// 188 // IRPrinterConfig 189 //===----------------------------------------------------------------------===// 190 191 /// Initialize the configuration. 192 PassManager::IRPrinterConfig::IRPrinterConfig(bool printModuleScope, 193 bool printAfterOnlyOnChange) 194 : printModuleScope(printModuleScope), 195 printAfterOnlyOnChange(printAfterOnlyOnChange) {} 196 PassManager::IRPrinterConfig::~IRPrinterConfig() {} 197 198 /// A hook that may be overridden by a derived config that checks if the IR 199 /// of 'operation' should be dumped *before* the pass 'pass' has been 200 /// executed. If the IR should be dumped, 'printCallback' should be invoked 201 /// with the stream to dump into. 202 void PassManager::IRPrinterConfig::printBeforeIfEnabled( 203 Pass *pass, Operation *operation, PrintCallbackFn printCallback) { 204 // By default, never print. 205 } 206 207 /// A hook that may be overridden by a derived config that checks if the IR 208 /// of 'operation' should be dumped *after* the pass 'pass' has been 209 /// executed. If the IR should be dumped, 'printCallback' should be invoked 210 /// with the stream to dump into. 211 void PassManager::IRPrinterConfig::printAfterIfEnabled( 212 Pass *pass, Operation *operation, PrintCallbackFn printCallback) { 213 // By default, never print. 214 } 215 216 //===----------------------------------------------------------------------===// 217 // PassManager 218 //===----------------------------------------------------------------------===// 219 220 namespace { 221 /// Simple wrapper config that allows for the simpler interface defined above. 222 struct BasicIRPrinterConfig : public PassManager::IRPrinterConfig { 223 BasicIRPrinterConfig( 224 std::function<bool(Pass *, Operation *)> shouldPrintBeforePass, 225 std::function<bool(Pass *, Operation *)> shouldPrintAfterPass, 226 bool printModuleScope, bool printAfterOnlyOnChange, raw_ostream &out) 227 : IRPrinterConfig(printModuleScope, printAfterOnlyOnChange), 228 shouldPrintBeforePass(shouldPrintBeforePass), 229 shouldPrintAfterPass(shouldPrintAfterPass), out(out) { 230 assert((shouldPrintBeforePass || shouldPrintAfterPass) && 231 "expected at least one valid filter function"); 232 } 233 234 void printBeforeIfEnabled(Pass *pass, Operation *operation, 235 PrintCallbackFn printCallback) final { 236 if (shouldPrintBeforePass && shouldPrintBeforePass(pass, operation)) 237 printCallback(out); 238 } 239 240 void printAfterIfEnabled(Pass *pass, Operation *operation, 241 PrintCallbackFn printCallback) final { 242 if (shouldPrintAfterPass && shouldPrintAfterPass(pass, operation)) 243 printCallback(out); 244 } 245 246 /// Filter functions for before and after pass execution. 247 std::function<bool(Pass *, Operation *)> shouldPrintBeforePass; 248 std::function<bool(Pass *, Operation *)> shouldPrintAfterPass; 249 250 /// The stream to output to. 251 raw_ostream &out; 252 }; 253 } // end anonymous namespace 254 255 /// Add an instrumentation to print the IR before and after pass execution, 256 /// using the provided configuration. 257 void PassManager::enableIRPrinting(std::unique_ptr<IRPrinterConfig> config) { 258 if (config->shouldPrintAtModuleScope() && 259 getContext()->isMultithreadingEnabled()) 260 llvm::report_fatal_error("IR printing can't be setup on a pass-manager " 261 "without disabling multi-threading first."); 262 addInstrumentation( 263 std::make_unique<IRPrinterInstrumentation>(std::move(config))); 264 } 265 266 /// Add an instrumentation to print the IR before and after pass execution. 267 void PassManager::enableIRPrinting( 268 std::function<bool(Pass *, Operation *)> shouldPrintBeforePass, 269 std::function<bool(Pass *, Operation *)> shouldPrintAfterPass, 270 bool printModuleScope, bool printAfterOnlyOnChange, raw_ostream &out) { 271 enableIRPrinting(std::make_unique<BasicIRPrinterConfig>( 272 std::move(shouldPrintBeforePass), std::move(shouldPrintAfterPass), 273 printModuleScope, printAfterOnlyOnChange, out)); 274 } 275