1 //===- RewriterGen.cpp - MLIR pattern rewriter generator ------------===// 2 // 3 // Copyright 2019 The MLIR Authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // ============================================================================= 17 // 18 // RewriterGen uses pattern rewrite definitions to generate rewriter matchers. 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "mlir/TableGen/GenInfo.h" 23 #include "mlir/TableGen/Operator.h" 24 #include "mlir/TableGen/Predicate.h" 25 #include "mlir/TableGen/Type.h" 26 #include "llvm/ADT/StringExtras.h" 27 #include "llvm/ADT/StringSet.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Support/FormatVariadic.h" 30 #include "llvm/Support/PrettyStackTrace.h" 31 #include "llvm/Support/Signals.h" 32 #include "llvm/TableGen/Error.h" 33 #include "llvm/TableGen/Main.h" 34 #include "llvm/TableGen/Record.h" 35 #include "llvm/TableGen/TableGenBackend.h" 36 37 using namespace llvm; 38 using namespace mlir; 39 40 using mlir::tblgen::Attribute; 41 using mlir::tblgen::Operator; 42 using mlir::tblgen::Type; 43 44 namespace { 45 46 // Wrapper around dag argument. 47 struct DagArg { 48 DagArg(Init *init) : init(init) {} 49 bool isAttr(); 50 51 Init *init; 52 }; 53 54 } // end namespace 55 56 bool DagArg::isAttr() { 57 if (auto defInit = dyn_cast<DefInit>(init)) 58 return defInit->getDef()->isSubClassOf("Attr"); 59 return false; 60 } 61 62 namespace { 63 class Pattern { 64 public: 65 static void emit(StringRef rewriteName, Record *p, raw_ostream &os); 66 67 private: 68 Pattern(Record *pattern, raw_ostream &os) : pattern(pattern), os(os) {} 69 70 // Emit the rewrite pattern named `rewriteName`. 71 void emit(StringRef rewriteName); 72 73 // Emit the matcher. 74 void emitMatcher(DagInit *tree); 75 76 // Emits the value of constant attribute to `os`. 77 void emitAttributeValue(Record *constAttr); 78 79 // Collect bound arguments. 80 void collectBoundArguments(DagInit *tree); 81 82 // Map from bound argument name to DagArg. 83 StringMap<DagArg> boundArguments; 84 85 // Number of the operations in the input pattern. 86 int numberOfOpsMatched = 0; 87 88 Record *pattern; 89 raw_ostream &os; 90 }; 91 } // end namespace 92 93 void Pattern::emitAttributeValue(Record *constAttr) { 94 Attribute attr(constAttr->getValueAsDef("attr")); 95 auto value = constAttr->getValue("value"); 96 Type type = attr.getType(); 97 auto storageType = attr.getStorageType(); 98 99 // For attributes stored as strings we do not need to query builder etc. 100 if (storageType == "StringAttr") { 101 os << formatv("rewriter.getStringAttr({0})", 102 value->getValue()->getAsString()); 103 return; 104 } 105 106 auto builder = type.getBuilderCall(); 107 if (builder.empty()) 108 PrintFatalError(pattern->getLoc(), 109 "no builder specified for " + type.getTableGenDefName()); 110 111 // Construct the attribute based on storage type and builder. 112 // TODO(jpienaar): Verify the constants here 113 os << formatv("{0}::get(rewriter.{1}, {2})", storageType, builder, 114 value->getValue()->getAsUnquotedString()); 115 } 116 117 void Pattern::collectBoundArguments(DagInit *tree) { 118 ++numberOfOpsMatched; 119 // TODO(jpienaar): Expand to multiple matches. 120 for (int i = 0, e = tree->getNumArgs(); i != e; ++i) { 121 auto arg = tree->getArg(i); 122 if (auto argTree = dyn_cast<DagInit>(arg)) { 123 collectBoundArguments(argTree); 124 continue; 125 } 126 auto name = tree->getArgNameStr(i); 127 if (name.empty()) 128 continue; 129 boundArguments.try_emplace(name, arg); 130 } 131 } 132 133 // Helper function to match patterns. 134 static void matchOp(Record *pattern, DagInit *tree, int depth, 135 raw_ostream &os) { 136 Operator op(cast<DefInit>(tree->getOperator())->getDef()); 137 int indent = 4 + 2 * depth; 138 // Skip the operand matching at depth 0 as the pattern rewriter already does. 139 if (depth != 0) { 140 // Skip if there is no defining instruction (e.g., arguments to function). 141 os.indent(indent) << formatv("if (!op{0}) return matchFailure();\n", depth); 142 os.indent(indent) << formatv( 143 "if (!op{0}->isa<{1}>()) return matchFailure();\n", depth, 144 op.qualifiedCppClassName()); 145 } 146 if (tree->getNumArgs() != op.getNumArgs()) 147 PrintFatalError(pattern->getLoc(), 148 Twine("mismatch in number of arguments to op '") + 149 op.getOperationName() + 150 "' in pattern and op's definition"); 151 for (int i = 0, e = tree->getNumArgs(); i != e; ++i) { 152 auto arg = tree->getArg(i); 153 auto opArg = op.getArg(i); 154 155 if (auto argTree = dyn_cast<DagInit>(arg)) { 156 os.indent(indent) << "{\n"; 157 os.indent(indent + 2) << formatv( 158 "auto op{0} = op{1}->getOperand({2})->getDefiningInst();\n", 159 depth + 1, depth, i); 160 matchOp(pattern, argTree, depth + 1, os); 161 os.indent(indent) << "}\n"; 162 continue; 163 } 164 165 // Verify arguments. 166 if (auto defInit = dyn_cast<DefInit>(arg)) { 167 // Verify operands. 168 if (auto *operand = opArg.dyn_cast<Operator::Operand *>()) { 169 // Skip verification where not needed due to definition of op. 170 if (operand->defInit == defInit) 171 goto StateCapture; 172 173 if (!defInit->getDef()->isSubClassOf("Type")) 174 PrintFatalError(pattern->getLoc(), 175 "type argument required for operand"); 176 177 auto constraint = tblgen::TypeConstraint(*defInit); 178 os.indent(indent) 179 << "if (!(" 180 << formatv(constraint.getConditionTemplate().str().c_str(), 181 formatv("op{0}->getOperand({1})->getType()", depth, i)) 182 << ")) return matchFailure();\n"; 183 } 184 185 // TODO(jpienaar): Verify attributes. 186 if (auto *attr = opArg.dyn_cast<Operator::NamedAttribute *>()) { 187 } 188 } 189 190 StateCapture: 191 auto name = tree->getArgNameStr(i); 192 if (name.empty()) 193 continue; 194 if (opArg.is<Operator::Operand *>()) 195 os.indent(indent) << "state->" << name << " = op" << depth 196 << "->getOperand(" << i << ");\n"; 197 if (auto namedAttr = opArg.dyn_cast<Operator::NamedAttribute *>()) { 198 os.indent(indent) << "state->" << name << " = op" << depth 199 << "->getAttrOfType<" 200 << namedAttr->attr.getStorageType() << ">(\"" 201 << namedAttr->getName() << "\");\n"; 202 } 203 } 204 } 205 206 void Pattern::emitMatcher(DagInit *tree) { 207 // Emit the heading. 208 os << R"( 209 PatternMatchResult match(OperationInst *op0) const override { 210 // TODO: This just handle 1 result 211 if (op0->getNumResults() != 1) return matchFailure(); 212 auto state = std::make_unique<MatchedState>();)" 213 << "\n"; 214 matchOp(pattern, tree, 0, os); 215 os.indent(4) << "return matchSuccess(std::move(state));\n }\n"; 216 } 217 218 void Pattern::emit(StringRef rewriteName) { 219 DagInit *tree = pattern->getValueAsDag("PatternToMatch"); 220 // Collect bound arguments and compute number of ops matched. 221 // TODO(jpienaar): the benefit metric is simply number of ops matched at the 222 // moment, revise. 223 collectBoundArguments(tree); 224 225 // Emit RewritePattern for Pattern. 226 DefInit *root = cast<DefInit>(tree->getOperator()); 227 auto *rootName = cast<StringInit>(root->getDef()->getValueInit("opName")); 228 os << formatv(R"(struct {0} : public RewritePattern { 229 {0}(MLIRContext *context) : RewritePattern({1}, {2}, context) {{})", 230 rewriteName, rootName->getAsString(), numberOfOpsMatched) 231 << "\n"; 232 233 // Emit matched state. 234 os << " struct MatchedState : public PatternState {\n"; 235 for (auto &arg : boundArguments) { 236 if (arg.second.isAttr()) { 237 DefInit *defInit = cast<DefInit>(arg.second.init); 238 os.indent(4) << Attribute(defInit).getStorageType() << " " << arg.first() 239 << ";\n"; 240 } else { 241 os.indent(4) << "Value* " << arg.first() << ";\n"; 242 } 243 } 244 os << " };\n"; 245 246 emitMatcher(tree); 247 ListInit *resultOps = pattern->getValueAsListInit("ResultOps"); 248 if (resultOps->size() != 1) 249 PrintFatalError("only single result rules supported"); 250 DagInit *resultTree = cast<DagInit>(resultOps->getElement(0)); 251 252 // TODO(jpienaar): Expand to multiple results. 253 for (auto result : resultTree->getArgs()) { 254 if (isa<DagInit>(result)) 255 PrintFatalError(pattern->getLoc(), "only single op result supported"); 256 } 257 258 DefInit *resultRoot = cast<DefInit>(resultTree->getOperator()); 259 Operator resultOp(*resultRoot->getDef()); 260 auto resultOperands = resultRoot->getDef()->getValueAsDag("arguments"); 261 262 os << formatv(R"( 263 void rewrite(OperationInst *op, std::unique_ptr<PatternState> state, 264 PatternRewriter &rewriter) const override { 265 auto& s = *static_cast<MatchedState *>(state.get()); 266 rewriter.replaceOpWithNewOp<{0}>(op, op->getResult(0)->getType())", 267 resultOp.cppClassName()); 268 if (resultOperands->getNumArgs() != resultTree->getNumArgs()) { 269 PrintFatalError(pattern->getLoc(), 270 Twine("mismatch between arguments of resultant op (") + 271 Twine(resultOperands->getNumArgs()) + 272 ") and arguments provided for rewrite (" + 273 Twine(resultTree->getNumArgs()) + Twine(')')); 274 } 275 276 // Create the builder call for the result. 277 // Add operands. 278 int i = 0; 279 for (auto operand : resultOp.getOperands()) { 280 // Start each operand on its own line. 281 (os << ",\n").indent(6); 282 283 auto name = resultTree->getArgNameStr(i); 284 if (boundArguments.find(name) == boundArguments.end()) 285 PrintFatalError(pattern->getLoc(), 286 Twine("referencing unbound variable '") + name + "'"); 287 if (operand.name) 288 os << "/*" << operand.name->getAsUnquotedString() << "=*/"; 289 os << "s." << name; 290 // TODO(jpienaar): verify types 291 ++i; 292 } 293 294 // Add attributes. 295 for (int e = resultTree->getNumArgs(); i != e; ++i) { 296 // Start each attribute on its own line. 297 (os << ",\n").indent(6); 298 299 // The argument in the result DAG pattern. 300 auto name = resultTree->getArgNameStr(i); 301 auto opName = resultOp.getArgName(i); 302 auto defInit = dyn_cast<DefInit>(resultTree->getArg(i)); 303 auto *value = defInit ? defInit->getDef()->getValue("value") : nullptr; 304 if (!value) { 305 if (boundArguments.find(name) == boundArguments.end()) 306 PrintFatalError(pattern->getLoc(), 307 Twine("referencing unbound variable '") + name + "'"); 308 os << "/*" << opName << "=*/" 309 << "s." << name; 310 continue; 311 } 312 313 // TODO(jpienaar): Refactor out into map to avoid recomputing these. 314 auto argument = resultOp.getArg(i); 315 if (!argument.is<Operator::NamedAttribute *>()) 316 PrintFatalError(pattern->getLoc(), 317 Twine("expected attribute ") + Twine(i)); 318 319 if (!name.empty()) 320 os << "/*" << name << "=*/"; 321 emitAttributeValue(defInit->getDef()); 322 // TODO(jpienaar): verify types 323 } 324 os << "\n );\n }\n};\n"; 325 } 326 327 void Pattern::emit(StringRef rewriteName, Record *p, raw_ostream &os) { 328 Pattern pattern(p, os); 329 pattern.emit(rewriteName); 330 } 331 332 static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) { 333 emitSourceFileHeader("Rewriters", os); 334 const auto &patterns = recordKeeper.getAllDerivedDefinitions("Pattern"); 335 336 // Ensure unique patterns simply by appending unique suffix. 337 std::string baseRewriteName = "GeneratedConvert"; 338 int rewritePatternCount = 0; 339 for (Record *p : patterns) { 340 Pattern::emit(baseRewriteName + llvm::utostr(rewritePatternCount++), p, os); 341 } 342 343 // Emit function to add the generated matchers to the pattern list. 344 os << "void populateWithGenerated(MLIRContext *context, " 345 << "OwningRewritePatternList *patterns) {\n"; 346 for (unsigned i = 0; i != rewritePatternCount; ++i) { 347 os.indent(2) << "patterns->push_back(std::make_unique<" << baseRewriteName 348 << i << ">(context));\n"; 349 } 350 os << "}\n"; 351 } 352 353 mlir::GenRegistration 354 genRewriters("gen-rewriters", "Generate pattern rewriters", 355 [](const RecordKeeper &records, raw_ostream &os) { 356 emitRewriters(records, os); 357 return false; 358 }); 359