1 //===- Operator.cpp - Operator 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 // Operator wrapper to simplify using TableGen Record defining a MLIR Op.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/TableGen/Operator.h"
14 #include "mlir/TableGen/OpTrait.h"
15 #include "mlir/TableGen/Predicate.h"
16 #include "mlir/TableGen/Type.h"
17 #include "llvm/ADT/EquivalenceClasses.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/Sequence.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/TypeSwitch.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/FormatVariadic.h"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 
27 #define DEBUG_TYPE "mlir-tblgen-operator"
28 
29 using namespace mlir;
30 
31 using llvm::DagInit;
32 using llvm::DefInit;
33 using llvm::Record;
34 
35 tblgen::Operator::Operator(const llvm::Record &def)
36     : dialect(def.getValueAsDef("opDialect")), def(def) {
37   // The first `_` in the op's TableGen def name is treated as separating the
38   // dialect prefix and the op class name. The dialect prefix will be ignored if
39   // not empty. Otherwise, if def name starts with a `_`, the `_` is considered
40   // as part of the class name.
41   StringRef prefix;
42   std::tie(prefix, cppClassName) = def.getName().split('_');
43   if (prefix.empty()) {
44     // Class name with a leading underscore and without dialect prefix
45     cppClassName = def.getName();
46   } else if (cppClassName.empty()) {
47     // Class name without dialect prefix
48     cppClassName = prefix;
49   }
50 
51   populateOpStructure();
52 }
53 
54 std::string tblgen::Operator::getOperationName() const {
55   auto prefix = dialect.getName();
56   auto opName = def.getValueAsString("opName");
57   if (prefix.empty())
58     return std::string(opName);
59   return std::string(llvm::formatv("{0}.{1}", prefix, opName));
60 }
61 
62 std::string tblgen::Operator::getAdaptorName() const {
63   return std::string(llvm::formatv("{0}Adaptor", getCppClassName()));
64 }
65 
66 StringRef tblgen::Operator::getDialectName() const { return dialect.getName(); }
67 
68 StringRef tblgen::Operator::getCppClassName() const { return cppClassName; }
69 
70 std::string tblgen::Operator::getQualCppClassName() const {
71   auto prefix = dialect.getCppNamespace();
72   if (prefix.empty())
73     return std::string(cppClassName);
74   return std::string(llvm::formatv("{0}::{1}", prefix, cppClassName));
75 }
76 
77 int tblgen::Operator::getNumResults() const {
78   DagInit *results = def.getValueAsDag("results");
79   return results->getNumArgs();
80 }
81 
82 StringRef tblgen::Operator::getExtraClassDeclaration() const {
83   constexpr auto attr = "extraClassDeclaration";
84   if (def.isValueUnset(attr))
85     return {};
86   return def.getValueAsString(attr);
87 }
88 
89 const llvm::Record &tblgen::Operator::getDef() const { return def; }
90 
91 bool tblgen::Operator::skipDefaultBuilders() const {
92   return def.getValueAsBit("skipDefaultBuilders");
93 }
94 
95 auto tblgen::Operator::result_begin() -> value_iterator {
96   return results.begin();
97 }
98 
99 auto tblgen::Operator::result_end() -> value_iterator { return results.end(); }
100 
101 auto tblgen::Operator::getResults() -> value_range {
102   return {result_begin(), result_end()};
103 }
104 
105 tblgen::TypeConstraint
106 tblgen::Operator::getResultTypeConstraint(int index) const {
107   DagInit *results = def.getValueAsDag("results");
108   return TypeConstraint(cast<DefInit>(results->getArg(index)));
109 }
110 
111 StringRef tblgen::Operator::getResultName(int index) const {
112   DagInit *results = def.getValueAsDag("results");
113   return results->getArgNameStr(index);
114 }
115 
116 auto tblgen::Operator::getResultDecorators(int index) const
117     -> var_decorator_range {
118   Record *result =
119       cast<DefInit>(def.getValueAsDag("results")->getArg(index))->getDef();
120   if (!result->isSubClassOf("OpVariable"))
121     return var_decorator_range(nullptr, nullptr);
122   return *result->getValueAsListInit("decorators");
123 }
124 
125 unsigned tblgen::Operator::getNumVariableLengthResults() const {
126   return llvm::count_if(results, [](const NamedTypeConstraint &c) {
127     return c.constraint.isVariableLength();
128   });
129 }
130 
131 unsigned tblgen::Operator::getNumVariableLengthOperands() const {
132   return llvm::count_if(operands, [](const NamedTypeConstraint &c) {
133     return c.constraint.isVariableLength();
134   });
135 }
136 
137 tblgen::Operator::arg_iterator tblgen::Operator::arg_begin() const {
138   return arguments.begin();
139 }
140 
141 tblgen::Operator::arg_iterator tblgen::Operator::arg_end() const {
142   return arguments.end();
143 }
144 
145 tblgen::Operator::arg_range tblgen::Operator::getArgs() const {
146   return {arg_begin(), arg_end()};
147 }
148 
149 StringRef tblgen::Operator::getArgName(int index) const {
150   DagInit *argumentValues = def.getValueAsDag("arguments");
151   return argumentValues->getArgName(index)->getValue();
152 }
153 
154 auto tblgen::Operator::getArgDecorators(int index) const
155     -> var_decorator_range {
156   Record *arg =
157       cast<DefInit>(def.getValueAsDag("arguments")->getArg(index))->getDef();
158   if (!arg->isSubClassOf("OpVariable"))
159     return var_decorator_range(nullptr, nullptr);
160   return *arg->getValueAsListInit("decorators");
161 }
162 
163 const tblgen::OpTrait *tblgen::Operator::getTrait(StringRef trait) const {
164   for (const auto &t : traits) {
165     if (const auto *opTrait = dyn_cast<tblgen::NativeOpTrait>(&t)) {
166       if (opTrait->getTrait() == trait)
167         return opTrait;
168     } else if (const auto *opTrait = dyn_cast<tblgen::InternalOpTrait>(&t)) {
169       if (opTrait->getTrait() == trait)
170         return opTrait;
171     } else if (const auto *opTrait = dyn_cast<tblgen::InterfaceOpTrait>(&t)) {
172       if (opTrait->getTrait() == trait)
173         return opTrait;
174     }
175   }
176   return nullptr;
177 }
178 
179 auto tblgen::Operator::region_begin() const -> const_region_iterator {
180   return regions.begin();
181 }
182 auto tblgen::Operator::region_end() const -> const_region_iterator {
183   return regions.end();
184 }
185 auto tblgen::Operator::getRegions() const
186     -> llvm::iterator_range<const_region_iterator> {
187   return {region_begin(), region_end()};
188 }
189 
190 unsigned tblgen::Operator::getNumRegions() const { return regions.size(); }
191 
192 const tblgen::NamedRegion &tblgen::Operator::getRegion(unsigned index) const {
193   return regions[index];
194 }
195 
196 unsigned tblgen::Operator::getNumVariadicRegions() const {
197   return llvm::count_if(regions,
198                         [](const NamedRegion &c) { return c.isVariadic(); });
199 }
200 
201 auto tblgen::Operator::successor_begin() const -> const_successor_iterator {
202   return successors.begin();
203 }
204 auto tblgen::Operator::successor_end() const -> const_successor_iterator {
205   return successors.end();
206 }
207 auto tblgen::Operator::getSuccessors() const
208     -> llvm::iterator_range<const_successor_iterator> {
209   return {successor_begin(), successor_end()};
210 }
211 
212 unsigned tblgen::Operator::getNumSuccessors() const {
213   return successors.size();
214 }
215 
216 const tblgen::NamedSuccessor &
217 tblgen::Operator::getSuccessor(unsigned index) const {
218   return successors[index];
219 }
220 
221 unsigned tblgen::Operator::getNumVariadicSuccessors() const {
222   return llvm::count_if(successors,
223                         [](const NamedSuccessor &c) { return c.isVariadic(); });
224 }
225 
226 auto tblgen::Operator::trait_begin() const -> const_trait_iterator {
227   return traits.begin();
228 }
229 auto tblgen::Operator::trait_end() const -> const_trait_iterator {
230   return traits.end();
231 }
232 auto tblgen::Operator::getTraits() const
233     -> llvm::iterator_range<const_trait_iterator> {
234   return {trait_begin(), trait_end()};
235 }
236 
237 auto tblgen::Operator::attribute_begin() const -> attribute_iterator {
238   return attributes.begin();
239 }
240 auto tblgen::Operator::attribute_end() const -> attribute_iterator {
241   return attributes.end();
242 }
243 auto tblgen::Operator::getAttributes() const
244     -> llvm::iterator_range<attribute_iterator> {
245   return {attribute_begin(), attribute_end()};
246 }
247 
248 auto tblgen::Operator::operand_begin() -> value_iterator {
249   return operands.begin();
250 }
251 auto tblgen::Operator::operand_end() -> value_iterator {
252   return operands.end();
253 }
254 auto tblgen::Operator::getOperands() -> value_range {
255   return {operand_begin(), operand_end()};
256 }
257 
258 auto tblgen::Operator::getArg(int index) const -> Argument {
259   return arguments[index];
260 }
261 
262 // Mapping from result index to combined argument and result index. Arguments
263 // are indexed to match getArg index, while the result indexes are mapped to
264 // avoid overlap.
265 static int resultIndex(int i) { return -1 - i; }
266 
267 bool tblgen::Operator::isVariadic() const {
268   return any_of(llvm::concat<const NamedTypeConstraint>(operands, results),
269                 [](const NamedTypeConstraint &op) { return op.isVariadic(); });
270 }
271 
272 void tblgen::Operator::populateTypeInferenceInfo(
273     const llvm::StringMap<int> &argumentsAndResultsIndex) {
274   // If the type inference op interface is not registered, then do not attempt
275   // to determine if the result types an be inferred.
276   auto &recordKeeper = def.getRecords();
277   auto *inferTrait = recordKeeper.getDef(inferTypeOpInterface);
278   allResultsHaveKnownTypes = false;
279   if (!inferTrait)
280     return;
281 
282   // If there are no results, the skip this else the build method generated
283   // overlaps with another autogenerated builder.
284   if (getNumResults() == 0)
285     return;
286 
287   // Skip for ops with variadic operands/results.
288   // TODO: This can be relaxed.
289   if (isVariadic())
290     return;
291 
292   // Skip cases currently being custom generated.
293   // TODO: Remove special cases.
294   if (getTrait("OpTrait::SameOperandsAndResultType"))
295     return;
296 
297   // We create equivalence classes of argument/result types where arguments
298   // and results are mapped into the same index space and indices corresponding
299   // to the same type are in the same equivalence class.
300   llvm::EquivalenceClasses<int> ecs;
301   resultTypeMapping.resize(getNumResults());
302   // Captures the argument whose type matches a given result type. Preference
303   // towards capturing operands first before attributes.
304   auto captureMapping = [&](int i) {
305     bool found = false;
306     ecs.insert(resultIndex(i));
307     auto mi = ecs.findLeader(resultIndex(i));
308     for (auto me = ecs.member_end(); mi != me; ++mi) {
309       if (*mi < 0) {
310         auto tc = getResultTypeConstraint(i);
311         if (tc.getBuilderCall().hasValue()) {
312           resultTypeMapping[i].emplace_back(tc);
313           found = true;
314         }
315         continue;
316       }
317 
318       if (getArg(*mi).is<NamedAttribute *>()) {
319         // TODO: Handle attributes.
320         continue;
321       } else {
322         resultTypeMapping[i].emplace_back(*mi);
323         found = true;
324       }
325     }
326     return found;
327   };
328 
329   for (const OpTrait &trait : traits) {
330     const llvm::Record &def = trait.getDef();
331     // If the infer type op interface was manually added, then treat it as
332     // intention that the op needs special handling.
333     // TODO: Reconsider whether to always generate, this is more conservative
334     // and keeps existing behavior so starting that way for now.
335     if (def.isSubClassOf(
336             llvm::formatv("{0}::Trait", inferTypeOpInterface).str()))
337       return;
338     if (const auto *opTrait = dyn_cast<tblgen::InterfaceOpTrait>(&trait))
339       if (&opTrait->getDef() == inferTrait)
340         return;
341 
342     if (!def.isSubClassOf("AllTypesMatch"))
343       continue;
344 
345     auto values = def.getValueAsListOfStrings("values");
346     auto root = argumentsAndResultsIndex.lookup(values.front());
347     for (StringRef str : values)
348       ecs.unionSets(argumentsAndResultsIndex.lookup(str), root);
349   }
350 
351   // Verifies that all output types have a corresponding known input type
352   // and chooses matching operand or attribute (in that order) that
353   // matches it.
354   allResultsHaveKnownTypes =
355       all_of(llvm::seq<int>(0, getNumResults()), captureMapping);
356 
357   // If the types could be computed, then add type inference trait.
358   if (allResultsHaveKnownTypes)
359     traits.push_back(OpTrait::create(inferTrait->getDefInit()));
360 }
361 
362 void tblgen::Operator::populateOpStructure() {
363   auto &recordKeeper = def.getRecords();
364   auto *typeConstraintClass = recordKeeper.getClass("TypeConstraint");
365   auto *attrClass = recordKeeper.getClass("Attr");
366   auto *derivedAttrClass = recordKeeper.getClass("DerivedAttr");
367   auto *opVarClass = recordKeeper.getClass("OpVariable");
368   numNativeAttributes = 0;
369 
370   DagInit *argumentValues = def.getValueAsDag("arguments");
371   unsigned numArgs = argumentValues->getNumArgs();
372 
373   // Mapping from name of to argument or result index. Arguments are indexed
374   // to match getArg index, while the results are negatively indexed.
375   llvm::StringMap<int> argumentsAndResultsIndex;
376 
377   // Handle operands and native attributes.
378   for (unsigned i = 0; i != numArgs; ++i) {
379     auto *arg = argumentValues->getArg(i);
380     auto givenName = argumentValues->getArgNameStr(i);
381     auto *argDefInit = dyn_cast<DefInit>(arg);
382     if (!argDefInit)
383       PrintFatalError(def.getLoc(),
384                       Twine("undefined type for argument #") + Twine(i));
385     Record *argDef = argDefInit->getDef();
386     if (argDef->isSubClassOf(opVarClass))
387       argDef = argDef->getValueAsDef("constraint");
388 
389     if (argDef->isSubClassOf(typeConstraintClass)) {
390       operands.push_back(
391           NamedTypeConstraint{givenName, TypeConstraint(argDef)});
392     } else if (argDef->isSubClassOf(attrClass)) {
393       if (givenName.empty())
394         PrintFatalError(argDef->getLoc(), "attributes must be named");
395       if (argDef->isSubClassOf(derivedAttrClass))
396         PrintFatalError(argDef->getLoc(),
397                         "derived attributes not allowed in argument list");
398       attributes.push_back({givenName, Attribute(argDef)});
399       ++numNativeAttributes;
400     } else {
401       PrintFatalError(def.getLoc(), "unexpected def type; only defs deriving "
402                                     "from TypeConstraint or Attr are allowed");
403     }
404     if (!givenName.empty())
405       argumentsAndResultsIndex[givenName] = i;
406   }
407 
408   // Handle derived attributes.
409   for (const auto &val : def.getValues()) {
410     if (auto *record = dyn_cast<llvm::RecordRecTy>(val.getType())) {
411       if (!record->isSubClassOf(attrClass))
412         continue;
413       if (!record->isSubClassOf(derivedAttrClass))
414         PrintFatalError(def.getLoc(),
415                         "unexpected Attr where only DerivedAttr is allowed");
416 
417       if (record->getClasses().size() != 1) {
418         PrintFatalError(
419             def.getLoc(),
420             "unsupported attribute modelling, only single class expected");
421       }
422       attributes.push_back(
423           {cast<llvm::StringInit>(val.getNameInit())->getValue(),
424            Attribute(cast<DefInit>(val.getValue()))});
425     }
426   }
427 
428   // Populate `arguments`. This must happen after we've finalized `operands` and
429   // `attributes` because we will put their elements' pointers in `arguments`.
430   // SmallVector may perform re-allocation under the hood when adding new
431   // elements.
432   int operandIndex = 0, attrIndex = 0;
433   for (unsigned i = 0; i != numArgs; ++i) {
434     Record *argDef = dyn_cast<DefInit>(argumentValues->getArg(i))->getDef();
435     if (argDef->isSubClassOf(opVarClass))
436       argDef = argDef->getValueAsDef("constraint");
437 
438     if (argDef->isSubClassOf(typeConstraintClass)) {
439       attrOrOperandMapping.push_back(
440           {OperandOrAttribute::Kind::Operand, operandIndex});
441       arguments.emplace_back(&operands[operandIndex++]);
442     } else {
443       assert(argDef->isSubClassOf(attrClass));
444       attrOrOperandMapping.push_back(
445           {OperandOrAttribute::Kind::Attribute, attrIndex});
446       arguments.emplace_back(&attributes[attrIndex++]);
447     }
448   }
449 
450   auto *resultsDag = def.getValueAsDag("results");
451   auto *outsOp = dyn_cast<DefInit>(resultsDag->getOperator());
452   if (!outsOp || outsOp->getDef()->getName() != "outs") {
453     PrintFatalError(def.getLoc(), "'results' must have 'outs' directive");
454   }
455 
456   // Handle results.
457   for (unsigned i = 0, e = resultsDag->getNumArgs(); i < e; ++i) {
458     auto name = resultsDag->getArgNameStr(i);
459     auto *resultInit = dyn_cast<DefInit>(resultsDag->getArg(i));
460     if (!resultInit) {
461       PrintFatalError(def.getLoc(),
462                       Twine("undefined type for result #") + Twine(i));
463     }
464     auto *resultDef = resultInit->getDef();
465     if (resultDef->isSubClassOf(opVarClass))
466       resultDef = resultDef->getValueAsDef("constraint");
467     results.push_back({name, TypeConstraint(resultDef)});
468     if (!name.empty())
469       argumentsAndResultsIndex[name] = resultIndex(i);
470   }
471 
472   // Handle successors
473   auto *successorsDag = def.getValueAsDag("successors");
474   auto *successorsOp = dyn_cast<DefInit>(successorsDag->getOperator());
475   if (!successorsOp || successorsOp->getDef()->getName() != "successor") {
476     PrintFatalError(def.getLoc(),
477                     "'successors' must have 'successor' directive");
478   }
479 
480   for (unsigned i = 0, e = successorsDag->getNumArgs(); i < e; ++i) {
481     auto name = successorsDag->getArgNameStr(i);
482     auto *successorInit = dyn_cast<DefInit>(successorsDag->getArg(i));
483     if (!successorInit) {
484       PrintFatalError(def.getLoc(),
485                       Twine("undefined kind for successor #") + Twine(i));
486     }
487     Successor successor(successorInit->getDef());
488 
489     // Only support variadic successors if it is the last one for now.
490     if (i != e - 1 && successor.isVariadic())
491       PrintFatalError(def.getLoc(), "only the last successor can be variadic");
492     successors.push_back({name, successor});
493   }
494 
495   // Create list of traits, skipping over duplicates: appending to lists in
496   // tablegen is easy, making them unique less so, so dedupe here.
497   if (auto *traitList = def.getValueAsListInit("traits")) {
498     // This is uniquing based on pointers of the trait.
499     SmallPtrSet<const llvm::Init *, 32> traitSet;
500     traits.reserve(traitSet.size());
501     for (auto *traitInit : *traitList) {
502       // Keep traits in the same order while skipping over duplicates.
503       if (traitSet.insert(traitInit).second)
504         traits.push_back(OpTrait::create(traitInit));
505     }
506   }
507 
508   populateTypeInferenceInfo(argumentsAndResultsIndex);
509 
510   // Handle regions
511   auto *regionsDag = def.getValueAsDag("regions");
512   auto *regionsOp = dyn_cast<DefInit>(regionsDag->getOperator());
513   if (!regionsOp || regionsOp->getDef()->getName() != "region") {
514     PrintFatalError(def.getLoc(), "'regions' must have 'region' directive");
515   }
516 
517   for (unsigned i = 0, e = regionsDag->getNumArgs(); i < e; ++i) {
518     auto name = regionsDag->getArgNameStr(i);
519     auto *regionInit = dyn_cast<DefInit>(regionsDag->getArg(i));
520     if (!regionInit) {
521       PrintFatalError(def.getLoc(),
522                       Twine("undefined kind for region #") + Twine(i));
523     }
524     Region region(regionInit->getDef());
525     if (region.isVariadic()) {
526       // Only support variadic regions if it is the last one for now.
527       if (i != e - 1)
528         PrintFatalError(def.getLoc(), "only the last region can be variadic");
529       if (name.empty())
530         PrintFatalError(def.getLoc(), "variadic regions must be named");
531     }
532 
533     regions.push_back({name, region});
534   }
535 
536   LLVM_DEBUG(print(llvm::dbgs()));
537 }
538 
539 auto tblgen::Operator::getSameTypeAsResult(int index) const
540     -> ArrayRef<ArgOrType> {
541   assert(allResultTypesKnown());
542   return resultTypeMapping[index];
543 }
544 
545 ArrayRef<llvm::SMLoc> tblgen::Operator::getLoc() const { return def.getLoc(); }
546 
547 bool tblgen::Operator::hasDescription() const {
548   return def.getValue("description") != nullptr;
549 }
550 
551 StringRef tblgen::Operator::getDescription() const {
552   return def.getValueAsString("description");
553 }
554 
555 bool tblgen::Operator::hasSummary() const {
556   return def.getValue("summary") != nullptr;
557 }
558 
559 StringRef tblgen::Operator::getSummary() const {
560   return def.getValueAsString("summary");
561 }
562 
563 bool tblgen::Operator::hasAssemblyFormat() const {
564   auto *valueInit = def.getValueInit("assemblyFormat");
565   return isa<llvm::CodeInit, llvm::StringInit>(valueInit);
566 }
567 
568 StringRef tblgen::Operator::getAssemblyFormat() const {
569   return TypeSwitch<llvm::Init *, StringRef>(def.getValueInit("assemblyFormat"))
570       .Case<llvm::StringInit, llvm::CodeInit>(
571           [&](auto *init) { return init->getValue(); });
572 }
573 
574 void tblgen::Operator::print(llvm::raw_ostream &os) const {
575   os << "op '" << getOperationName() << "'\n";
576   for (Argument arg : arguments) {
577     if (auto *attr = arg.dyn_cast<NamedAttribute *>())
578       os << "[attribute] " << attr->name << '\n';
579     else
580       os << "[operand] " << arg.get<NamedTypeConstraint *>()->name << '\n';
581   }
582 }
583 
584 auto tblgen::Operator::VariableDecoratorIterator::unwrap(llvm::Init *init)
585     -> VariableDecorator {
586   return VariableDecorator(cast<llvm::DefInit>(init)->getDef());
587 }
588 
589 auto tblgen::Operator::getArgToOperandOrAttribute(int index) const
590     -> OperandOrAttribute {
591   return attrOrOperandMapping[index];
592 }
593