1 //===- Pattern.cpp - Pattern wrapper class --------------------------------===//
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 // Pattern wrapper class to simplify using TableGen Record defining a MLIR
10 // Pattern.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/TableGen/Pattern.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/FormatVariadic.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 
22 #define DEBUG_TYPE "mlir-tblgen-pattern"
23 
24 using namespace mlir;
25 
26 using llvm::formatv;
27 using mlir::tblgen::Operator;
28 
29 //===----------------------------------------------------------------------===//
30 // DagLeaf
31 //===----------------------------------------------------------------------===//
32 
33 bool tblgen::DagLeaf::isUnspecified() const {
34   return dyn_cast_or_null<llvm::UnsetInit>(def);
35 }
36 
37 bool tblgen::DagLeaf::isOperandMatcher() const {
38   // Operand matchers specify a type constraint.
39   return isSubClassOf("TypeConstraint");
40 }
41 
42 bool tblgen::DagLeaf::isAttrMatcher() const {
43   // Attribute matchers specify an attribute constraint.
44   return isSubClassOf("AttrConstraint");
45 }
46 
47 bool tblgen::DagLeaf::isNativeCodeCall() const {
48   return isSubClassOf("NativeCodeCall");
49 }
50 
51 bool tblgen::DagLeaf::isConstantAttr() const {
52   return isSubClassOf("ConstantAttr");
53 }
54 
55 bool tblgen::DagLeaf::isEnumAttrCase() const {
56   return isSubClassOf("EnumAttrCaseInfo");
57 }
58 
59 tblgen::Constraint tblgen::DagLeaf::getAsConstraint() const {
60   assert((isOperandMatcher() || isAttrMatcher()) &&
61          "the DAG leaf must be operand or attribute");
62   return Constraint(cast<llvm::DefInit>(def)->getDef());
63 }
64 
65 tblgen::ConstantAttr tblgen::DagLeaf::getAsConstantAttr() const {
66   assert(isConstantAttr() && "the DAG leaf must be constant attribute");
67   return ConstantAttr(cast<llvm::DefInit>(def));
68 }
69 
70 tblgen::EnumAttrCase tblgen::DagLeaf::getAsEnumAttrCase() const {
71   assert(isEnumAttrCase() && "the DAG leaf must be an enum attribute case");
72   return EnumAttrCase(cast<llvm::DefInit>(def));
73 }
74 
75 std::string tblgen::DagLeaf::getConditionTemplate() const {
76   return getAsConstraint().getConditionTemplate();
77 }
78 
79 llvm::StringRef tblgen::DagLeaf::getNativeCodeTemplate() const {
80   assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
81   return cast<llvm::DefInit>(def)->getDef()->getValueAsString("expression");
82 }
83 
84 bool tblgen::DagLeaf::isSubClassOf(StringRef superclass) const {
85   if (auto *defInit = dyn_cast_or_null<llvm::DefInit>(def))
86     return defInit->getDef()->isSubClassOf(superclass);
87   return false;
88 }
89 
90 void tblgen::DagLeaf::print(raw_ostream &os) const {
91   if (def)
92     def->print(os);
93 }
94 
95 //===----------------------------------------------------------------------===//
96 // DagNode
97 //===----------------------------------------------------------------------===//
98 
99 bool tblgen::DagNode::isNativeCodeCall() const {
100   if (auto *defInit = dyn_cast_or_null<llvm::DefInit>(node->getOperator()))
101     return defInit->getDef()->isSubClassOf("NativeCodeCall");
102   return false;
103 }
104 
105 bool tblgen::DagNode::isOperation() const {
106   return !(isNativeCodeCall() || isReplaceWithValue());
107 }
108 
109 llvm::StringRef tblgen::DagNode::getNativeCodeTemplate() const {
110   assert(isNativeCodeCall() && "the DAG leaf must be NativeCodeCall");
111   return cast<llvm::DefInit>(node->getOperator())
112       ->getDef()
113       ->getValueAsString("expression");
114 }
115 
116 llvm::StringRef tblgen::DagNode::getSymbol() const {
117   return node->getNameStr();
118 }
119 
120 Operator &tblgen::DagNode::getDialectOp(RecordOperatorMap *mapper) const {
121   llvm::Record *opDef = cast<llvm::DefInit>(node->getOperator())->getDef();
122   auto it = mapper->find(opDef);
123   if (it != mapper->end())
124     return *it->second;
125   return *mapper->try_emplace(opDef, std::make_unique<Operator>(opDef))
126               .first->second;
127 }
128 
129 int tblgen::DagNode::getNumOps() const {
130   int count = isReplaceWithValue() ? 0 : 1;
131   for (int i = 0, e = getNumArgs(); i != e; ++i) {
132     if (auto child = getArgAsNestedDag(i))
133       count += child.getNumOps();
134   }
135   return count;
136 }
137 
138 int tblgen::DagNode::getNumArgs() const { return node->getNumArgs(); }
139 
140 bool tblgen::DagNode::isNestedDagArg(unsigned index) const {
141   return isa<llvm::DagInit>(node->getArg(index));
142 }
143 
144 tblgen::DagNode tblgen::DagNode::getArgAsNestedDag(unsigned index) const {
145   return DagNode(dyn_cast_or_null<llvm::DagInit>(node->getArg(index)));
146 }
147 
148 tblgen::DagLeaf tblgen::DagNode::getArgAsLeaf(unsigned index) const {
149   assert(!isNestedDagArg(index));
150   return DagLeaf(node->getArg(index));
151 }
152 
153 StringRef tblgen::DagNode::getArgName(unsigned index) const {
154   return node->getArgNameStr(index);
155 }
156 
157 bool tblgen::DagNode::isReplaceWithValue() const {
158   auto *dagOpDef = cast<llvm::DefInit>(node->getOperator())->getDef();
159   return dagOpDef->getName() == "replaceWithValue";
160 }
161 
162 void tblgen::DagNode::print(raw_ostream &os) const {
163   if (node)
164     node->print(os);
165 }
166 
167 //===----------------------------------------------------------------------===//
168 // SymbolInfoMap
169 //===----------------------------------------------------------------------===//
170 
171 StringRef tblgen::SymbolInfoMap::getValuePackName(StringRef symbol,
172                                                   int *index) {
173   StringRef name, indexStr;
174   int idx = -1;
175   std::tie(name, indexStr) = symbol.rsplit("__");
176 
177   if (indexStr.consumeInteger(10, idx)) {
178     // The second part is not an index; we return the whole symbol as-is.
179     return symbol;
180   }
181   if (index) {
182     *index = idx;
183   }
184   return name;
185 }
186 
187 tblgen::SymbolInfoMap::SymbolInfo::SymbolInfo(const Operator *op,
188                                               SymbolInfo::Kind kind,
189                                               Optional<int> index)
190     : op(op), kind(kind), argIndex(index) {}
191 
192 int tblgen::SymbolInfoMap::SymbolInfo::getStaticValueCount() const {
193   switch (kind) {
194   case Kind::Attr:
195   case Kind::Operand:
196   case Kind::Value:
197     return 1;
198   case Kind::Result:
199     return op->getNumResults();
200   }
201   llvm_unreachable("unknown kind");
202 }
203 
204 std::string
205 tblgen::SymbolInfoMap::SymbolInfo::getVarDecl(StringRef name) const {
206   LLVM_DEBUG(llvm::dbgs() << "getVarDecl for '" << name << "': ");
207   switch (kind) {
208   case Kind::Attr: {
209     auto type =
210         op->getArg(*argIndex).get<NamedAttribute *>()->attr.getStorageType();
211     return std::string(formatv("{0} {1};\n", type, name));
212   }
213   case Kind::Operand: {
214     // Use operand range for captured operands (to support potential variadic
215     // operands).
216     return std::string(
217         formatv("Operation::operand_range {0}(op0->getOperands());\n", name));
218   }
219   case Kind::Value: {
220     return std::string(formatv("ArrayRef<Value> {0};\n", name));
221   }
222   case Kind::Result: {
223     // Use the op itself for captured results.
224     return std::string(formatv("{0} {1};\n", op->getQualCppClassName(), name));
225   }
226   }
227   llvm_unreachable("unknown kind");
228 }
229 
230 std::string tblgen::SymbolInfoMap::SymbolInfo::getValueAndRangeUse(
231     StringRef name, int index, const char *fmt, const char *separator) const {
232   LLVM_DEBUG(llvm::dbgs() << "getValueAndRangeUse for '" << name << "': ");
233   switch (kind) {
234   case Kind::Attr: {
235     assert(index < 0);
236     auto repl = formatv(fmt, name);
237     LLVM_DEBUG(llvm::dbgs() << repl << " (Attr)\n");
238     return std::string(repl);
239   }
240   case Kind::Operand: {
241     assert(index < 0);
242     auto *operand = op->getArg(*argIndex).get<NamedTypeConstraint *>();
243     // If this operand is variadic, then return a range. Otherwise, return the
244     // value itself.
245     if (operand->isVariadic()) {
246       auto repl = formatv(fmt, name);
247       LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicOperand)\n");
248       return std::string(repl);
249     }
250     auto repl = formatv(fmt, formatv("(*{0}.begin())", name));
251     LLVM_DEBUG(llvm::dbgs() << repl << " (SingleOperand)\n");
252     return std::string(repl);
253   }
254   case Kind::Result: {
255     // If `index` is greater than zero, then we are referencing a specific
256     // result of a multi-result op. The result can still be variadic.
257     if (index >= 0) {
258       std::string v =
259           std::string(formatv("{0}.getODSResults({1})", name, index));
260       if (!op->getResult(index).isVariadic())
261         v = std::string(formatv("(*{0}.begin())", v));
262       auto repl = formatv(fmt, v);
263       LLVM_DEBUG(llvm::dbgs() << repl << " (SingleResult)\n");
264       return std::string(repl);
265     }
266 
267     // If this op has no result at all but still we bind a symbol to it, it
268     // means we want to capture the op itself.
269     if (op->getNumResults() == 0) {
270       LLVM_DEBUG(llvm::dbgs() << name << " (Op)\n");
271       return std::string(name);
272     }
273 
274     // We are referencing all results of the multi-result op. A specific result
275     // can either be a value or a range. Then join them with `separator`.
276     SmallVector<std::string, 4> values;
277     values.reserve(op->getNumResults());
278 
279     for (int i = 0, e = op->getNumResults(); i < e; ++i) {
280       std::string v = std::string(formatv("{0}.getODSResults({1})", name, i));
281       if (!op->getResult(i).isVariadic()) {
282         v = std::string(formatv("(*{0}.begin())", v));
283       }
284       values.push_back(std::string(formatv(fmt, v)));
285     }
286     auto repl = llvm::join(values, separator);
287     LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicResult)\n");
288     return repl;
289   }
290   case Kind::Value: {
291     assert(index < 0);
292     assert(op == nullptr);
293     auto repl = formatv(fmt, name);
294     LLVM_DEBUG(llvm::dbgs() << repl << " (Value)\n");
295     return std::string(repl);
296   }
297   }
298   llvm_unreachable("unknown kind");
299 }
300 
301 std::string tblgen::SymbolInfoMap::SymbolInfo::getAllRangeUse(
302     StringRef name, int index, const char *fmt, const char *separator) const {
303   LLVM_DEBUG(llvm::dbgs() << "getAllRangeUse for '" << name << "': ");
304   switch (kind) {
305   case Kind::Attr:
306   case Kind::Operand: {
307     assert(index < 0 && "only allowed for symbol bound to result");
308     auto repl = formatv(fmt, name);
309     LLVM_DEBUG(llvm::dbgs() << repl << " (Operand/Attr)\n");
310     return std::string(repl);
311   }
312   case Kind::Result: {
313     if (index >= 0) {
314       auto repl = formatv(fmt, formatv("{0}.getODSResults({1})", name, index));
315       LLVM_DEBUG(llvm::dbgs() << repl << " (SingleResult)\n");
316       return std::string(repl);
317     }
318 
319     // We are referencing all results of the multi-result op. Each result should
320     // have a value range, and then join them with `separator`.
321     SmallVector<std::string, 4> values;
322     values.reserve(op->getNumResults());
323 
324     for (int i = 0, e = op->getNumResults(); i < e; ++i) {
325       values.push_back(std::string(
326           formatv(fmt, formatv("{0}.getODSResults({1})", name, i))));
327     }
328     auto repl = llvm::join(values, separator);
329     LLVM_DEBUG(llvm::dbgs() << repl << " (VariadicResult)\n");
330     return repl;
331   }
332   case Kind::Value: {
333     assert(index < 0 && "only allowed for symbol bound to result");
334     assert(op == nullptr);
335     auto repl = formatv(fmt, formatv("{{{0}}", name));
336     LLVM_DEBUG(llvm::dbgs() << repl << " (Value)\n");
337     return std::string(repl);
338   }
339   }
340   llvm_unreachable("unknown kind");
341 }
342 
343 bool tblgen::SymbolInfoMap::bindOpArgument(StringRef symbol, const Operator &op,
344                                            int argIndex) {
345   StringRef name = getValuePackName(symbol);
346   if (name != symbol) {
347     auto error = formatv(
348         "symbol '{0}' with trailing index cannot bind to op argument", symbol);
349     PrintFatalError(loc, error);
350   }
351 
352   auto symInfo = op.getArg(argIndex).is<NamedAttribute *>()
353                      ? SymbolInfo::getAttr(&op, argIndex)
354                      : SymbolInfo::getOperand(&op, argIndex);
355 
356   return symbolInfoMap.insert({symbol, symInfo}).second;
357 }
358 
359 bool tblgen::SymbolInfoMap::bindOpResult(StringRef symbol, const Operator &op) {
360   StringRef name = getValuePackName(symbol);
361   return symbolInfoMap.insert({name, SymbolInfo::getResult(&op)}).second;
362 }
363 
364 bool tblgen::SymbolInfoMap::bindValue(StringRef symbol) {
365   return symbolInfoMap.insert({symbol, SymbolInfo::getValue()}).second;
366 }
367 
368 bool tblgen::SymbolInfoMap::contains(StringRef symbol) const {
369   return find(symbol) != symbolInfoMap.end();
370 }
371 
372 tblgen::SymbolInfoMap::const_iterator
373 tblgen::SymbolInfoMap::find(StringRef key) const {
374   StringRef name = getValuePackName(key);
375   return symbolInfoMap.find(name);
376 }
377 
378 int tblgen::SymbolInfoMap::getStaticValueCount(StringRef symbol) const {
379   StringRef name = getValuePackName(symbol);
380   if (name != symbol) {
381     // If there is a trailing index inside symbol, it references just one
382     // static value.
383     return 1;
384   }
385   // Otherwise, find how many it represents by querying the symbol's info.
386   return find(name)->getValue().getStaticValueCount();
387 }
388 
389 std::string
390 tblgen::SymbolInfoMap::getValueAndRangeUse(StringRef symbol, const char *fmt,
391                                            const char *separator) const {
392   int index = -1;
393   StringRef name = getValuePackName(symbol, &index);
394 
395   auto it = symbolInfoMap.find(name);
396   if (it == symbolInfoMap.end()) {
397     auto error = formatv("referencing unbound symbol '{0}'", symbol);
398     PrintFatalError(loc, error);
399   }
400 
401   return it->getValue().getValueAndRangeUse(name, index, fmt, separator);
402 }
403 
404 std::string tblgen::SymbolInfoMap::getAllRangeUse(StringRef symbol,
405                                                   const char *fmt,
406                                                   const char *separator) const {
407   int index = -1;
408   StringRef name = getValuePackName(symbol, &index);
409 
410   auto it = symbolInfoMap.find(name);
411   if (it == symbolInfoMap.end()) {
412     auto error = formatv("referencing unbound symbol '{0}'", symbol);
413     PrintFatalError(loc, error);
414   }
415 
416   return it->getValue().getAllRangeUse(name, index, fmt, separator);
417 }
418 
419 //===----------------------------------------------------------------------===//
420 // Pattern
421 //==----------------------------------------------------------------------===//
422 
423 tblgen::Pattern::Pattern(const llvm::Record *def, RecordOperatorMap *mapper)
424     : def(*def), recordOpMap(mapper) {}
425 
426 tblgen::DagNode tblgen::Pattern::getSourcePattern() const {
427   return tblgen::DagNode(def.getValueAsDag("sourcePattern"));
428 }
429 
430 int tblgen::Pattern::getNumResultPatterns() const {
431   auto *results = def.getValueAsListInit("resultPatterns");
432   return results->size();
433 }
434 
435 tblgen::DagNode tblgen::Pattern::getResultPattern(unsigned index) const {
436   auto *results = def.getValueAsListInit("resultPatterns");
437   return tblgen::DagNode(cast<llvm::DagInit>(results->getElement(index)));
438 }
439 
440 void tblgen::Pattern::collectSourcePatternBoundSymbols(
441     tblgen::SymbolInfoMap &infoMap) {
442   LLVM_DEBUG(llvm::dbgs() << "start collecting source pattern bound symbols\n");
443   collectBoundSymbols(getSourcePattern(), infoMap, /*isSrcPattern=*/true);
444   LLVM_DEBUG(llvm::dbgs() << "done collecting source pattern bound symbols\n");
445 }
446 
447 void tblgen::Pattern::collectResultPatternBoundSymbols(
448     tblgen::SymbolInfoMap &infoMap) {
449   LLVM_DEBUG(llvm::dbgs() << "start collecting result pattern bound symbols\n");
450   for (int i = 0, e = getNumResultPatterns(); i < e; ++i) {
451     auto pattern = getResultPattern(i);
452     collectBoundSymbols(pattern, infoMap, /*isSrcPattern=*/false);
453   }
454   LLVM_DEBUG(llvm::dbgs() << "done collecting result pattern bound symbols\n");
455 }
456 
457 const tblgen::Operator &tblgen::Pattern::getSourceRootOp() {
458   return getSourcePattern().getDialectOp(recordOpMap);
459 }
460 
461 tblgen::Operator &tblgen::Pattern::getDialectOp(DagNode node) {
462   return node.getDialectOp(recordOpMap);
463 }
464 
465 std::vector<tblgen::AppliedConstraint> tblgen::Pattern::getConstraints() const {
466   auto *listInit = def.getValueAsListInit("constraints");
467   std::vector<tblgen::AppliedConstraint> ret;
468   ret.reserve(listInit->size());
469 
470   for (auto it : *listInit) {
471     auto *dagInit = dyn_cast<llvm::DagInit>(it);
472     if (!dagInit)
473       PrintFatalError(def.getLoc(), "all elements in Pattern multi-entity "
474                                     "constraints should be DAG nodes");
475 
476     std::vector<std::string> entities;
477     entities.reserve(dagInit->arg_size());
478     for (auto *argName : dagInit->getArgNames()) {
479       if (!argName) {
480         PrintFatalError(
481             def.getLoc(),
482             "operands to additional constraints can only be symbol references");
483       }
484       entities.push_back(std::string(argName->getValue()));
485     }
486 
487     ret.emplace_back(cast<llvm::DefInit>(dagInit->getOperator())->getDef(),
488                      dagInit->getNameStr(), std::move(entities));
489   }
490   return ret;
491 }
492 
493 int tblgen::Pattern::getBenefit() const {
494   // The initial benefit value is a heuristic with number of ops in the source
495   // pattern.
496   int initBenefit = getSourcePattern().getNumOps();
497   llvm::DagInit *delta = def.getValueAsDag("benefitDelta");
498   if (delta->getNumArgs() != 1 || !isa<llvm::IntInit>(delta->getArg(0))) {
499     PrintFatalError(def.getLoc(),
500                     "The 'addBenefit' takes and only takes one integer value");
501   }
502   return initBenefit + dyn_cast<llvm::IntInit>(delta->getArg(0))->getValue();
503 }
504 
505 std::vector<tblgen::Pattern::IdentifierLine>
506 tblgen::Pattern::getLocation() const {
507   std::vector<std::pair<StringRef, unsigned>> result;
508   result.reserve(def.getLoc().size());
509   for (auto loc : def.getLoc()) {
510     unsigned buf = llvm::SrcMgr.FindBufferContainingLoc(loc);
511     assert(buf && "invalid source location");
512     result.emplace_back(
513         llvm::SrcMgr.getBufferInfo(buf).Buffer->getBufferIdentifier(),
514         llvm::SrcMgr.getLineAndColumn(loc, buf).first);
515   }
516   return result;
517 }
518 
519 void tblgen::Pattern::collectBoundSymbols(DagNode tree, SymbolInfoMap &infoMap,
520                                           bool isSrcPattern) {
521   auto treeName = tree.getSymbol();
522   if (!tree.isOperation()) {
523     if (!treeName.empty()) {
524       PrintFatalError(
525           def.getLoc(),
526           formatv("binding symbol '{0}' to non-operation unsupported right now",
527                   treeName));
528     }
529     return;
530   }
531 
532   auto &op = getDialectOp(tree);
533   auto numOpArgs = op.getNumArgs();
534   auto numTreeArgs = tree.getNumArgs();
535 
536   if (numOpArgs != numTreeArgs) {
537     auto err = formatv("op '{0}' argument number mismatch: "
538                        "{1} in pattern vs. {2} in definition",
539                        op.getOperationName(), numTreeArgs, numOpArgs);
540     PrintFatalError(def.getLoc(), err);
541   }
542 
543   // The name attached to the DAG node's operator is for representing the
544   // results generated from this op. It should be remembered as bound results.
545   if (!treeName.empty()) {
546     LLVM_DEBUG(llvm::dbgs()
547                << "found symbol bound to op result: " << treeName << '\n');
548     if (!infoMap.bindOpResult(treeName, op))
549       PrintFatalError(def.getLoc(),
550                       formatv("symbol '{0}' bound more than once", treeName));
551   }
552 
553   for (int i = 0; i != numTreeArgs; ++i) {
554     if (auto treeArg = tree.getArgAsNestedDag(i)) {
555       // This DAG node argument is a DAG node itself. Go inside recursively.
556       collectBoundSymbols(treeArg, infoMap, isSrcPattern);
557     } else if (isSrcPattern) {
558       // We can only bind symbols to op arguments in source pattern. Those
559       // symbols are referenced in result patterns.
560       auto treeArgName = tree.getArgName(i);
561       // `$_` is a special symbol meaning ignore the current argument.
562       if (!treeArgName.empty() && treeArgName != "_") {
563         LLVM_DEBUG(llvm::dbgs() << "found symbol bound to op argument: "
564                                 << treeArgName << '\n');
565         if (!infoMap.bindOpArgument(treeArgName, op, i)) {
566           auto err = formatv("symbol '{0}' bound more than once", treeArgName);
567           PrintFatalError(def.getLoc(), err);
568         }
569       }
570     }
571   }
572 }
573