MLIR 24.0.0git
Operator.cpp
Go to the documentation of this file.
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
16#include "mlir/TableGen/Trait.h"
17#include "mlir/TableGen/Type.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/Sequence.h"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/TypeSwitch.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/FormatVariadic.h"
26#include "llvm/TableGen/Error.h"
27#include "llvm/TableGen/Record.h"
28
29#define DEBUG_TYPE "mlir-tblgen-operator"
30
31using namespace mlir;
32using namespace mlir::tblgen;
33
34using llvm::DagInit;
35using llvm::DefInit;
36using llvm::Init;
37using llvm::ListInit;
38using llvm::Record;
39using llvm::StringInit;
40
41Operator::Operator(const Record &def)
42 : dialect(def.getValueAsDef("opDialect")), def(def) {
43 // The first `_` in the op's TableGen def name is treated as separating the
44 // dialect prefix and the op class name. The dialect prefix will be ignored if
45 // not empty. Otherwise, if def name starts with a `_`, the `_` is considered
46 // as part of the class name.
47 StringRef prefix;
48 std::tie(prefix, cppClassName) = def.getName().split('_');
49 if (prefix.empty()) {
50 // Class name with a leading underscore and without dialect prefix
51 cppClassName = def.getName();
52 } else if (cppClassName.empty()) {
53 // Class name without dialect prefix
54 cppClassName = prefix;
55 }
56
57 cppNamespace = def.getValueAsString("cppNamespace");
58
59 populateOpStructure();
60 assertInvariants();
61}
62
63std::string Operator::getOperationName() const {
64 auto prefix = dialect.getName();
65 auto opName = def.getValueAsString("opName");
66 if (prefix.empty())
67 return std::string(opName);
68 return std::string(llvm::formatv("{0}.{1}", prefix, opName));
69}
70
71std::string Operator::getAdaptorName() const {
72 return std::string(llvm::formatv("{0}Adaptor", getCppClassName()));
73}
74
76 return std::string(llvm::formatv("{0}GenericAdaptor", getCppClassName()));
77}
78
79/// Assert the invariants of accessors generated for the given name.
80static void assertAccessorInvariants(const Operator &op, StringRef name) {
81 std::string accessorName =
82 convertToCamelFromSnakeCase(name, /*capitalizeFirst=*/true);
83
84 // Functor used to detect when an accessor will cause an overlap with an
85 // operation API.
86 //
87 // There are a little bit more invasive checks possible for cases where not
88 // all ops have the trait that would cause overlap. For many cases here,
89 // renaming would be better (e.g., we can only guard in limited manner
90 // against methods from traits and interfaces here, so avoiding these in op
91 // definition is safer).
92 auto nameOverlapsWithOpAPI = [&](StringRef newName) {
93 if (newName == "AttributeNames" || newName == "Attributes" ||
94 newName == "Operation")
95 return true;
96 if (newName == "Operands")
97 return op.getNumOperands() != 1 || op.getNumVariableLengthOperands() != 1;
98 if (newName == "Regions")
99 return op.getNumRegions() != 1 || op.getNumVariadicRegions() != 1;
100 if (newName == "Type")
101 return op.getNumResults() != 1;
102 return false;
103 };
104 if (nameOverlapsWithOpAPI(accessorName)) {
105 // This error could be avoided in situations where the final function is
106 // identical, but preferably the op definition should avoid using generic
107 // names.
108 PrintFatalError(op.getLoc(), "generated accessor for `" + name +
109 "` overlaps with a default one; please "
110 "rename to avoid overlap");
111 }
112}
113
115 // Check that the name of arguments/results/regions/successors don't overlap.
116 DenseMap<StringRef, StringRef> existingNames;
117 auto checkName = [&](StringRef name, StringRef entity) {
118 if (name.empty())
119 return;
120 auto insertion = existingNames.insert({name, entity});
121 if (insertion.second) {
122 // Assert invariants for accessors generated for this name.
123 assertAccessorInvariants(*this, name);
124 return;
125 }
126 if (entity == insertion.first->second)
127 PrintFatalError(getLoc(), "op has a conflict with two " + entity +
128 " having the same name '" + name + "'");
129 PrintFatalError(getLoc(), "op has a conflict with " +
130 insertion.first->second + " and " + entity +
131 " both having an entry with the name '" +
132 name + "'");
133 };
134 // Check operands amongst themselves.
135 for (int i : llvm::seq<int>(0, getNumOperands()))
136 checkName(getOperand(i).name, "operands");
137
138 // Check results amongst themselves and against operands.
139 for (int i : llvm::seq<int>(0, getNumResults()))
140 checkName(getResult(i).name, "results");
141
142 // Check regions amongst themselves and against operands and results.
143 for (int i : llvm::seq<int>(0, getNumRegions()))
144 checkName(getRegion(i).name, "regions");
145
146 // Check successors amongst themselves and against operands, results, and
147 // regions.
148 for (int i : llvm::seq<int>(0, getNumSuccessors()))
149 checkName(getSuccessor(i).name, "successors");
150}
151
152StringRef Operator::getDialectName() const { return dialect.getName(); }
153
154StringRef Operator::getCppClassName() const { return cppClassName; }
155
156std::string Operator::getQualCppClassName() const {
157 if (cppNamespace.empty())
158 return std::string(cppClassName);
159 return std::string(llvm::formatv("{0}::{1}", cppNamespace, cppClassName));
160}
161
162StringRef Operator::getCppNamespace() const { return cppNamespace; }
163
165 const DagInit *results = def.getValueAsDag("results");
166 return results->getNumArgs();
167}
168
170 constexpr auto attr = "extraClassDeclaration";
171 if (def.isValueUnset(attr))
172 return {};
173 return def.getValueAsString(attr);
174}
175
177 constexpr auto attr = "extraClassDefinition";
178 if (def.isValueUnset(attr))
179 return {};
180 return def.getValueAsString(attr);
181}
182
183const Record &Operator::getDef() const { return def; }
184
186 return def.getValueAsBit("skipDefaultBuilders");
187}
188
190 return def.getValueAsBit("hasCustomPropertiesPrinter");
191}
192
194 return results.begin();
195}
196
198 return results.end();
199}
200
202 return {result_begin(), result_end()};
203}
204
206 const DagInit *results = def.getValueAsDag("results");
207 return TypeConstraint(cast<DefInit>(results->getArg(index)));
208}
209
210StringRef Operator::getResultName(int index) const {
211 const DagInit *results = def.getValueAsDag("results");
212 return results->getArgNameStr(index);
213}
214
216 const Record *result =
217 cast<DefInit>(def.getValueAsDag("results")->getArg(index))->getDef();
218 if (!result->isSubClassOf("OpVariable"))
219 return var_decorator_range(nullptr, nullptr);
220 return *result->getValueAsListInit("decorators");
221}
222
224 return llvm::count_if(results, [](const NamedTypeConstraint &c) {
225 return c.constraint.isVariableLength();
226 });
227}
228
230 return llvm::count_if(operands, [](const NamedTypeConstraint &c) {
231 return c.constraint.isVariableLength();
232 });
233}
234
236 return getNumArgs() == 1 && isa<NamedTypeConstraint *>(getArg(0)) &&
238}
239
240Operator::arg_iterator Operator::arg_begin() const { return arguments.begin(); }
241
242Operator::arg_iterator Operator::arg_end() const { return arguments.end(); }
243
245 return {arg_begin(), arg_end()};
246}
247
248StringRef Operator::getArgName(int index) const {
249 const DagInit *argumentValues = def.getValueAsDag("arguments");
250 return argumentValues->getArgNameStr(index);
251}
252
254 const Record *arg =
255 cast<DefInit>(def.getValueAsDag("arguments")->getArg(index))->getDef();
256 if (!arg->isSubClassOf("OpVariable"))
257 return var_decorator_range(nullptr, nullptr);
258 return *arg->getValueAsListInit("decorators");
259}
260
261const Trait *Operator::getTrait(StringRef trait) const {
262 for (const auto &t : traits) {
263 if (const auto *traitDef = dyn_cast<NativeTrait>(&t)) {
264 if (traitDef->getFullyQualifiedTraitName() == trait)
265 return traitDef;
266 } else if (const auto *traitDef = dyn_cast<InternalTrait>(&t)) {
267 if (traitDef->getFullyQualifiedTraitName() == trait)
268 return traitDef;
269 } else if (const auto *traitDef = dyn_cast<InterfaceTrait>(&t)) {
270 if (traitDef->getFullyQualifiedTraitName() == trait)
271 return traitDef;
272 }
273 }
274 return nullptr;
275}
276
278 if (!properties.empty())
279 return true;
280 if (getTrait("::mlir::OpTrait::AttrSizedOperandSegments") ||
281 getTrait("::mlir::OpTrait::AttrSizedResultSegments"))
282 return true;
283 return llvm::any_of(attributes, [](const NamedAttribute &attr) {
284 return !attr.attr.isDerivedAttr();
285 });
286}
287
290 for (const NamedAttribute &attr : attributes)
291 if (!attr.attr.isDerivedAttr())
292 names.push_back(attr.name);
293 for (const NamedProperty &property : properties)
294 names.push_back(property.name);
295 if (getTrait("::mlir::OpTrait::AttrSizedOperandSegments")) {
296 names.push_back(operandSegmentAttrName);
297 names.push_back(legacyOperandSegmentAttrName);
298 }
299 if (getTrait("::mlir::OpTrait::AttrSizedResultSegments")) {
300 names.push_back(resultSegmentAttrName);
301 names.push_back(legacyResultSegmentAttrName);
302 }
303 return names;
304}
305
307 return regions.begin();
308}
310 return regions.end();
311}
316
317unsigned Operator::getNumRegions() const { return regions.size(); }
318
319const NamedRegion &Operator::getRegion(unsigned index) const {
320 return regions[index];
321}
322
324 return llvm::count_if(regions,
325 [](const NamedRegion &c) { return c.isVariadic(); });
326}
327
329 return successors.begin();
330}
332 return successors.end();
333}
338
339unsigned Operator::getNumSuccessors() const { return successors.size(); }
340
342 return successors[index];
343}
344
346 return llvm::count_if(successors,
347 [](const NamedSuccessor &c) { return c.isVariadic(); });
348}
349
351 return traits.begin();
352}
354 return traits.end();
355}
359
361 return attributes.begin();
362}
364 return attributes.end();
365}
371 return attributes.begin();
372}
374 return attributes.end();
375}
379
381 return operands.begin();
382}
384 return operands.end();
385}
387 return {operand_begin(), operand_end()};
388}
389
390auto Operator::getArg(int index) const -> Argument { return arguments[index]; }
391
393 return any_of(llvm::concat<const NamedTypeConstraint>(operands, results),
394 [](const NamedTypeConstraint &op) { return op.isVariadic(); });
395}
396
397void Operator::populateTypeInferenceInfo(
398 const llvm::StringMap<int> &argumentsAndResultsIndex) {
399 // If the type inference op interface is not registered, then do not attempt
400 // to determine if the result types an be inferred.
401 auto &recordKeeper = def.getRecords();
402 auto *inferTrait = recordKeeper.getDef(inferTypeOpInterface);
403 allResultsHaveKnownTypes = false;
404 if (!inferTrait)
405 return;
406
407 // If there are no results, the skip this else the build method generated
408 // overlaps with another autogenerated builder.
409 if (getNumResults() == 0)
410 return;
411
412 // Skip ops with variadic or optional results.
414 return;
415
416 // Skip cases currently being custom generated.
417 // TODO: Remove special cases.
418 if (getTrait("::mlir::OpTrait::SameOperandsAndResultType")) {
419 // Check for a non-variable length operand to use as the type anchor.
420 auto *operandI = llvm::find_if(arguments, [](const Argument &arg) {
421 NamedTypeConstraint *operand =
422 llvm::dyn_cast_if_present<NamedTypeConstraint *>(arg);
423 return operand && !operand->isVariableLength();
424 });
425 if (operandI == arguments.end())
426 return;
427
428 // All result types are inferred from the operand type.
429 int operandIdx = operandI - arguments.begin();
430 for (int i = 0; i < getNumResults(); ++i)
431 resultTypeMapping.emplace_back(operandIdx, "$_self");
432
433 allResultsHaveKnownTypes = true;
434 traits.push_back(Trait::create(inferTrait->getDefInit()));
435 return;
436 }
437
438 /// This struct represents a node in this operation's result type inferenece
439 /// graph. Each node has a list of incoming type inference edges `sources`.
440 /// Each edge represents a "source" from which the result type can be
441 /// inferred, either an operand (leaf) or another result (node). When a node
442 /// is known to have a fully-inferred type, `inferred` is set to true.
443 struct ResultTypeInference {
444 /// The list of incoming type inference edges.
446 /// This flag is set to true when the result type is known to be inferrable.
447 bool inferred = false;
448 };
449
450 // This vector represents the type inference graph, with one node for each
451 // operation result. The nth element is the node for the nth result.
452 SmallVector<ResultTypeInference> inference(getNumResults(), {});
453
454 // For all results whose types are buildable, initialize their type inference
455 // nodes with an edge to themselves. Mark those nodes are fully-inferred.
456 for (auto [idx, infer] : llvm::enumerate(inference)) {
457 if (getResult(idx).constraint.getBuilderCall()) {
458 infer.sources.emplace_back(InferredResultType::mapResultIndex(idx),
459 "$_self");
460 infer.inferred = true;
461 }
462 }
463
464 // Use `AllTypesMatch` and `TypesMatchWith` operation traits to build the
465 // result type inference graph.
466 for (const Trait &trait : traits) {
467 const Record &def = trait.getDef();
468
469 // If the infer type op interface was manually added, then treat it as
470 // intention that the op needs special handling.
471 // TODO: Reconsider whether to always generate, this is more conservative
472 // and keeps existing behavior so starting that way for now.
473 if (def.isSubClassOf(
474 llvm::formatv("{0}::Trait", inferTypeOpInterface).str()))
475 return;
476 if (const auto *traitDef = dyn_cast<InterfaceTrait>(&trait))
477 if (&traitDef->getDef() == inferTrait)
478 return;
479
480 // The `TypesMatchWith` trait represents a 1 -> 1 type inference edge with a
481 // type transformer.
482 if (def.isSubClassOf("TypesMatchWith")) {
483 int target = argumentsAndResultsIndex.lookup(def.getValueAsString("rhs"));
484 // Ignore operand type inference.
486 continue;
488 ResultTypeInference &infer = inference[resultIndex];
489 // If the type of the result has already been inferred, do nothing.
490 if (infer.inferred)
491 continue;
492 int sourceIndex =
493 argumentsAndResultsIndex.lookup(def.getValueAsString("lhs"));
494 infer.sources.emplace_back(sourceIndex,
495 def.getValueAsString("transformer").str());
496 // Locally propagate inferredness.
497 infer.inferred =
498 InferredResultType::isArgIndex(sourceIndex) ||
499 inference[InferredResultType::unmapResultIndex(sourceIndex)].inferred;
500 continue;
501 }
502
503 // The `ShapedTypeMatchesElementCountAndTypes` trait represents a 1 -> 1
504 // type inference edge where a shaped type matches element count and types
505 // of variadic elements.
506 if (def.isSubClassOf("ShapedTypeMatchesElementCountAndTypes")) {
507 StringRef shapedArg = def.getValueAsString("shaped");
508 StringRef elementsArg = def.getValueAsString("elements");
509
510 int shapedIndex = argumentsAndResultsIndex.lookup(shapedArg);
511 int elementsIndex = argumentsAndResultsIndex.lookup(elementsArg);
512
513 // Handle result type inference from shaped type to variadic elements.
514 if (InferredResultType::isResultIndex(elementsIndex) &&
515 InferredResultType::isArgIndex(shapedIndex)) {
516 int resultIndex = InferredResultType::unmapResultIndex(elementsIndex);
517 ResultTypeInference &infer = inference[resultIndex];
518 if (!infer.inferred) {
519 infer.sources.emplace_back(
520 shapedIndex,
521 "::llvm::SmallVector<::mlir::Type>(::llvm::cast<::mlir::"
522 "ShapedType>($_self).getNumElements(), "
523 "::llvm::cast<::mlir::ShapedType>($_self).getElementType())");
524 infer.inferred = true;
525 }
526 }
527
528 // Type inference in the opposite direction is not possible as the actual
529 // shaped type can't be inferred from the variadic elements.
530
531 continue;
532 }
533
534 if (!def.isSubClassOf("AllTypesMatch"))
535 continue;
536
537 auto values = def.getValueAsListOfStrings("values");
538 // The `AllTypesMatch` trait represents an N <-> N fanin and fanout. That
539 // is, every result type has an edge from every other type. However, if any
540 // one of the values refers to an operand or a result with a fully-inferred
541 // type, we can infer all other types from that value. Try to find a
542 // fully-inferred type in the list.
543 std::optional<int> fullyInferredIndex;
544 SmallVector<int> resultIndices;
545 for (StringRef name : values) {
546 int index = argumentsAndResultsIndex.lookup(name);
548 resultIndices.push_back(InferredResultType::unmapResultIndex(index));
550 inference[InferredResultType::unmapResultIndex(index)].inferred)
551 fullyInferredIndex = index;
552 }
553 if (fullyInferredIndex) {
554 // Make the fully-inferred type the only source for all results that
555 // aren't already inferred -- a 1 -> N fanout.
556 for (int resultIndex : resultIndices) {
557 ResultTypeInference &infer = inference[resultIndex];
558 if (!infer.inferred) {
559 infer.sources.assign(1, {*fullyInferredIndex, "$_self"});
560 infer.inferred = true;
561 }
562 }
563 } else {
564 // Add an edge between every result and every other type; N <-> N.
565 for (int resultIndex : resultIndices) {
566 for (int otherResultIndex : resultIndices) {
567 if (resultIndex == otherResultIndex)
568 continue;
569 inference[resultIndex].sources.emplace_back(
570 InferredResultType::unmapResultIndex(otherResultIndex), "$_self");
571 }
572 }
573 }
574 }
575
576 // Propagate inferredness until a fixed point.
577 std::vector<ResultTypeInference *> worklist;
578 for (ResultTypeInference &infer : inference)
579 if (!infer.inferred)
580 worklist.push_back(&infer);
581 bool changed;
582 do {
583 changed = false;
584 for (auto cur = worklist.begin(); cur != worklist.end();) {
585 ResultTypeInference &infer = **cur;
586
587 InferredResultType *iter =
588 llvm::find_if(infer.sources, [&](const InferredResultType &source) {
589 assert(InferredResultType::isResultIndex(source.getIndex()));
590 return inference[InferredResultType::unmapResultIndex(
591 source.getIndex())]
592 .inferred;
593 });
594 if (iter == infer.sources.end()) {
595 ++cur;
596 continue;
597 }
598
599 changed = true;
600 infer.inferred = true;
601 // Make this the only source for the result. This breaks any cycles.
602 infer.sources.assign(1, *iter);
603 cur = worklist.erase(cur);
604 }
605 } while (changed);
606
607 allResultsHaveKnownTypes = worklist.empty();
608
609 // If the types could be computed, then add type inference trait.
610 if (allResultsHaveKnownTypes) {
611 traits.push_back(Trait::create(inferTrait->getDefInit()));
612 for (const ResultTypeInference &infer : inference)
613 resultTypeMapping.push_back(infer.sources.front());
614 }
615}
616
617void Operator::populateOpStructure() {
618 auto &recordKeeper = def.getRecords();
619 auto *typeConstraintClass = recordKeeper.getClass("TypeConstraint");
620 auto *attrClass = recordKeeper.getClass("Attr");
621 auto *propertyClass = recordKeeper.getClass("Property");
622 auto *derivedAttrClass = recordKeeper.getClass("DerivedAttr");
623 auto *opVarClass = recordKeeper.getClass("OpVariable");
624 numNativeAttributes = 0;
625
626 const DagInit *argumentValues = def.getValueAsDag("arguments");
627 unsigned numArgs = argumentValues->getNumArgs();
628
629 // Mapping from name of to argument or result index. Arguments are indexed
630 // to match getArg index, while the results are negatively indexed.
631 llvm::StringMap<int> argumentsAndResultsIndex;
632
633 // Handle operands and native attributes.
634 for (unsigned i = 0; i != numArgs; ++i) {
635 auto *arg = argumentValues->getArg(i);
636 auto givenName = argumentValues->getArgNameStr(i);
637 auto *argDefInit = dyn_cast<DefInit>(arg);
638 if (!argDefInit)
639 PrintFatalError(def.getLoc(),
640 Twine("undefined type for argument #") + Twine(i));
641 const Record *argDef = argDefInit->getDef();
642 if (argDef->isSubClassOf(opVarClass))
643 argDef = argDef->getValueAsDef("constraint");
644
645 if (argDef->isSubClassOf(typeConstraintClass)) {
646 operands.push_back(
647 NamedTypeConstraint{givenName, TypeConstraint(argDef)});
648 } else if (argDef->isSubClassOf(attrClass)) {
649 if (givenName.empty())
650 PrintFatalError(argDef->getLoc(), "attributes must be named");
651 if (argDef->isSubClassOf(derivedAttrClass))
652 PrintFatalError(argDef->getLoc(),
653 "derived attributes not allowed in argument list");
654 attributes.push_back({givenName, Attribute(argDef)});
655 ++numNativeAttributes;
656 } else if (argDef->isSubClassOf(propertyClass)) {
657 if (givenName.empty())
658 PrintFatalError(argDef->getLoc(), "properties must be named");
659 properties.push_back({givenName, Property(argDef)});
660 } else {
661 PrintFatalError(def.getLoc(),
662 "unexpected def type; only defs deriving "
663 "from TypeConstraint or Attr or Property are allowed");
664 }
665 if (!givenName.empty())
666 argumentsAndResultsIndex[givenName] = i;
667 }
668
669 // Handle derived attributes.
670 for (const auto &val : def.getValues()) {
671 if (auto *record = dyn_cast<llvm::RecordRecTy>(val.getType())) {
672 if (!record->isSubClassOf(attrClass))
673 continue;
674 if (!record->isSubClassOf(derivedAttrClass))
675 PrintFatalError(def.getLoc(),
676 "unexpected Attr where only DerivedAttr is allowed");
677
678 if (record->getClasses().size() != 1) {
679 PrintFatalError(
680 def.getLoc(),
681 "unsupported attribute modelling, only single class expected");
682 }
683 attributes.push_back({cast<StringInit>(val.getNameInit())->getValue(),
684 Attribute(cast<DefInit>(val.getValue()))});
685 }
686 }
687
688 // Populate `arguments`. This must happen after we've finalized `operands` and
689 // `attributes` because we will put their elements' pointers in `arguments`.
690 // SmallVector may perform re-allocation under the hood when adding new
691 // elements.
692 int operandIndex = 0, attrIndex = 0, propIndex = 0;
693 for (unsigned i = 0; i != numArgs; ++i) {
694 const Record *argDef =
695 dyn_cast<DefInit>(argumentValues->getArg(i))->getDef();
696 if (argDef->isSubClassOf(opVarClass))
697 argDef = argDef->getValueAsDef("constraint");
698
699 if (argDef->isSubClassOf(typeConstraintClass)) {
700 attrPropOrOperandMapping.push_back(
701 {OperandAttrOrProp::Kind::Operand, operandIndex});
702 arguments.emplace_back(&operands[operandIndex++]);
703 } else if (argDef->isSubClassOf(attrClass)) {
704 attrPropOrOperandMapping.push_back(
706 arguments.emplace_back(&attributes[attrIndex++]);
707 } else {
708 assert(argDef->isSubClassOf(propertyClass));
709 attrPropOrOperandMapping.push_back(
711 arguments.emplace_back(&properties[propIndex++]);
712 }
713 }
714
715 auto *resultsDag = def.getValueAsDag("results");
716 auto *outsOp = dyn_cast<DefInit>(resultsDag->getOperator());
717 if (!outsOp || outsOp->getDef()->getName() != "outs") {
718 PrintFatalError(def.getLoc(), "'results' must have 'outs' directive");
719 }
720
721 // Handle results.
722 for (unsigned i = 0, e = resultsDag->getNumArgs(); i < e; ++i) {
723 auto name = resultsDag->getArgNameStr(i);
724 auto *resultInit = dyn_cast<DefInit>(resultsDag->getArg(i));
725 if (!resultInit) {
726 PrintFatalError(def.getLoc(),
727 Twine("undefined type for result #") + Twine(i));
728 }
729 auto *resultDef = resultInit->getDef();
730 if (resultDef->isSubClassOf(opVarClass))
731 resultDef = resultDef->getValueAsDef("constraint");
732 results.push_back({name, TypeConstraint(resultDef)});
733 if (!name.empty())
734 argumentsAndResultsIndex[name] = InferredResultType::mapResultIndex(i);
735
736 // We currently only support VariadicOfVariadic operands.
737 if (results.back().constraint.isVariadicOfVariadic()) {
738 PrintFatalError(
739 def.getLoc(),
740 "'VariadicOfVariadic' results are currently not supported");
741 }
742 }
743
744 // Handle successors
745 auto *successorsDag = def.getValueAsDag("successors");
746 auto *successorsOp = dyn_cast<DefInit>(successorsDag->getOperator());
747 if (!successorsOp || successorsOp->getDef()->getName() != "successor") {
748 PrintFatalError(def.getLoc(),
749 "'successors' must have 'successor' directive");
750 }
751
752 for (unsigned i = 0, e = successorsDag->getNumArgs(); i < e; ++i) {
753 auto name = successorsDag->getArgNameStr(i);
754 auto *successorInit = dyn_cast<DefInit>(successorsDag->getArg(i));
755 if (!successorInit) {
756 PrintFatalError(def.getLoc(),
757 Twine("undefined kind for successor #") + Twine(i));
758 }
759 Successor successor(successorInit->getDef());
760
761 // Only support variadic successors if it is the last one for now.
762 if (i != e - 1 && successor.isVariadic())
763 PrintFatalError(def.getLoc(), "only the last successor can be variadic");
764 successors.push_back({name, successor});
765 }
766
767 // Create list of traits, skipping over duplicates: appending to lists in
768 // tablegen is easy, making them unique less so, so dedupe here.
769 if (auto *traitList = def.getValueAsListInit("traits")) {
770 // This is uniquing based on pointers of the trait.
771 SmallPtrSet<const Init *, 32> traitSet;
772 traits.reserve(traitSet.size());
773
774 // The declaration order of traits imply the verification order of traits.
775 // Some traits may require other traits to be verified first then they can
776 // do further verification based on those verified facts. If you see this
777 // error, fix the traits declaration order by checking the `dependentTraits`
778 // field.
779 auto verifyTraitValidity = [&](const Record *trait) {
780 auto *dependentTraits = trait->getValueAsListInit("dependentTraits");
781 for (auto *traitInit : *dependentTraits)
782 if (!traitSet.contains(traitInit))
783 PrintFatalError(
784 def.getLoc(),
785 trait->getValueAsString("trait") + " requires " +
786 cast<DefInit>(traitInit)->getDef()->getValueAsString(
787 "trait") +
788 " to precede it in traits list");
789 };
790
791 std::function<void(const ListInit *)> insert;
792 insert = [&](const ListInit *traitList) {
793 for (auto *traitInit : *traitList) {
794 auto *def = cast<DefInit>(traitInit)->getDef();
795 if (def->isSubClassOf("TraitList")) {
796 insert(def->getValueAsListInit("traits"));
797 continue;
798 }
799
800 // Ignore duplicates.
801 if (!traitSet.insert(traitInit).second)
802 continue;
803
804 // If this is an interface with base classes, add the bases to the
805 // trait list.
806 if (def->isSubClassOf("Interface"))
807 insert(def->getValueAsListInit("baseInterfaces"));
808
809 // Verify if the trait has all the dependent traits declared before
810 // itself.
811 verifyTraitValidity(def);
812 traits.push_back(Trait::create(traitInit));
813 }
814 };
815 insert(traitList);
816 }
817
818 populateTypeInferenceInfo(argumentsAndResultsIndex);
819
820 // Handle regions
821 auto *regionsDag = def.getValueAsDag("regions");
822 auto *regionsOp = dyn_cast<DefInit>(regionsDag->getOperator());
823 if (!regionsOp || regionsOp->getDef()->getName() != "region") {
824 PrintFatalError(def.getLoc(), "'regions' must have 'region' directive");
825 }
826
827 for (unsigned i = 0, e = regionsDag->getNumArgs(); i < e; ++i) {
828 auto name = regionsDag->getArgNameStr(i);
829 auto *regionInit = dyn_cast<DefInit>(regionsDag->getArg(i));
830 if (!regionInit) {
831 PrintFatalError(def.getLoc(),
832 Twine("undefined kind for region #") + Twine(i));
833 }
834 Region region(regionInit->getDef());
835 if (region.isVariadic()) {
836 // Only support variadic regions if it is the last one for now.
837 if (i != e - 1)
838 PrintFatalError(def.getLoc(), "only the last region can be variadic");
839 if (name.empty())
840 PrintFatalError(def.getLoc(), "variadic regions must be named");
841 }
842
843 regions.push_back({name, region});
844 }
845
846 // Populate the builders.
847 auto *builderList = dyn_cast_or_null<ListInit>(def.getValueInit("builders"));
848 if (builderList && !builderList->empty()) {
849 for (const Init *init : builderList->getElements())
850 builders.emplace_back(cast<DefInit>(init)->getDef(), def.getLoc());
851 } else if (skipDefaultBuilders()) {
852 PrintFatalError(
853 def.getLoc(),
854 "default builders are skipped and no custom builders provided");
855 }
856
857 LLVM_DEBUG(print(llvm::dbgs()));
858}
859
861 assert(allResultTypesKnown());
862 return resultTypeMapping[index];
863}
864
865ArrayRef<SMLoc> Operator::getLoc() const { return def.getLoc(); }
866
868 return !getDescription().trim().empty();
869}
870
871StringRef Operator::getDescription() const {
872 return def.getValueAsString("description");
873}
874
875bool Operator::hasSummary() const { return !getSummary().trim().empty(); }
876
877StringRef Operator::getSummary() const {
878 return def.getValueAsString("summary");
879}
880
882 auto *valueInit = def.getValueInit("assemblyFormat");
883 return isa<StringInit>(valueInit);
884}
885
887 return TypeSwitch<const Init *, StringRef>(def.getValueInit("assemblyFormat"))
888 .Case([&](const StringInit *init) { return init->getValue(); });
889}
890
891void Operator::print(llvm::raw_ostream &os) const {
892 os << "op '" << getOperationName() << "'\n";
893 for (Argument arg : arguments) {
894 if (auto *attr = llvm::dyn_cast_if_present<NamedAttribute *>(arg))
895 os << "[attribute] " << attr->name << '\n';
896 else
897 os << "[operand] " << cast<NamedTypeConstraint *>(arg)->name << '\n';
898 }
899}
900
903 return VariableDecorator(cast<DefInit>(init)->getDef());
904}
905
907 return attrPropOrOperandMapping[index];
908}
909
910std::string Operator::getGetterName(StringRef name) const {
911 return "get" + convertToCamelFromSnakeCase(name, /*capitalizeFirst=*/true);
912}
913
914std::string Operator::getSetterName(StringRef name) const {
915 return "set" + convertToCamelFromSnakeCase(name, /*capitalizeFirst=*/true);
916}
917
918std::string Operator::getRemoverName(StringRef name) const {
919 return "remove" + convertToCamelFromSnakeCase(name, /*capitalizeFirst=*/true);
920}
921
922bool Operator::hasFolder() const { return def.getValueAsBit("hasFolder"); }
923
925 return def.getValueAsBit("useCustomPropertiesEncoding");
926}
static void assertAccessorInvariants(const Operator &op, StringRef name)
Assert the invariants of accessors generated for the given name.
Definition Operator.cpp:80
bool isDerivedAttr() const
Definition Attribute.cpp:44
This class represents an inferred result type.
Definition Operator.h:52
static int mapResultIndex(int i)
Definition Operator.h:67
static int unmapResultIndex(int i)
Definition Operator.h:68
static bool isResultIndex(int i)
Definition Operator.h:69
static bool isArgIndex(int i)
Definition Operator.h:70
Wrapper class that contains a MLIR op's information (e.g., operands, attributes) defined in TableGen ...
Definition Operator.h:85
std::string getQualCppClassName() const
Returns this op's C++ class name prefixed with namespaces.
Definition Operator.cpp:156
unsigned getNumSuccessors() const
Returns the number of successors.
Definition Operator.cpp:339
const NamedRegion & getRegion(unsigned index) const
Returns the index-th region.
Definition Operator.cpp:319
TypeConstraint getResultTypeConstraint(int index) const
Returns the index-th result's type constraint.
Definition Operator.cpp:205
ArrayRef< SMLoc > getLoc() const
Definition Operator.cpp:865
Operator(const llvm::Record &def)
const NamedTypeConstraint * const_value_iterator
Definition Operator.h:144
llvm::iterator_range< const_region_iterator > getRegions() const
Definition Operator.cpp:312
StringRef getCppNamespace() const
Returns this op's C++ namespace.
Definition Operator.cpp:162
const_attribute_iterator attribute_begin() const
Definition Operator.cpp:360
std::string getGetterName(StringRef name) const
Returns the getter name for the accessor of name.
Definition Operator.cpp:910
const_successor_iterator successor_end() const
Definition Operator.cpp:331
int getNumOperands() const
Definition Operator.h:233
StringRef getDescription() const
Definition Operator.cpp:871
const_value_range getResults() const
Definition Operator.cpp:201
arg_range getArgs() const
Definition Operator.cpp:244
const NamedAttribute * const_attribute_iterator
Op attribute iterators.
Definition Operator.h:182
const_value_range getOperands() const
Definition Operator.cpp:386
const_region_iterator region_begin() const
Definition Operator.cpp:306
bool useCustomPropertiesEncoding() const
Whether to generate the readProperty/writeProperty methods for bytecode emission.
Definition Operator.cpp:924
const NamedRegion * const_region_iterator
Regions.
Definition Operator.h:273
NamedTypeConstraint & getOperand(int index)
Definition Operator.h:234
StringRef getResultName(int index) const
Returns the index-th result's name.
Definition Operator.cpp:210
var_decorator_range getArgDecorators(int index) const
Definition Operator.cpp:253
const Argument * arg_iterator
Definition Operator.h:256
unsigned getNumVariableLengthOperands() const
Returns the number of variadic operands in this operation.
Definition Operator.cpp:229
OperandAttrOrProp getArgToOperandAttrOrProp(int index) const
Returns the OperandAttrOrProp corresponding to the index.
Definition Operator.cpp:906
var_decorator_range getResultDecorators(int index) const
Returns the index-th result's decorators.
Definition Operator.cpp:215
std::string getGenericAdaptorName() const
Returns the name of op's generic adaptor C++ class.
Definition Operator.cpp:75
StringRef getExtraClassDefinition() const
Returns this op's extra class definition code.
Definition Operator.cpp:176
const_value_iterator result_begin() const
Op result iterators.
Definition Operator.cpp:193
const Trait * const_trait_iterator
Trait.
Definition Operator.h:301
const_attribute_iterator attribute_end() const
Definition Operator.cpp:363
const_trait_iterator trait_end() const
Definition Operator.cpp:353
llvm::iterator_range< VariableDecoratorIterator > var_decorator_range
Definition Operator.h:141
std::string getAdaptorName() const
Returns the name of op's adaptor C++ class.
Definition Operator.cpp:71
bool hasNonEmptyProperties() const
Returns whether this operation has any non-empty properties.
Definition Operator.cpp:277
int getNumResults() const
Returns the number of results this op produces.
Definition Operator.cpp:164
llvm::iterator_range< const_attribute_iterator > getAttributes() const
Definition Operator.cpp:366
llvm::iterator_range< const_value_iterator > const_value_range
Definition Operator.h:146
bool hasFolder() const
Definition Operator.cpp:922
const_value_iterator operand_end() const
Definition Operator.cpp:383
arg_iterator arg_end() const
Definition Operator.cpp:242
int getNumArgs() const
Returns the total number of arguments.
Definition Operator.h:243
NamedTypeConstraint & getResult(int index)
Returns the op result at the given index.
Definition Operator.h:166
llvm::iterator_range< arg_iterator > arg_range
Definition Operator.h:257
const_value_iterator operand_begin() const
Op operand iterators.
Definition Operator.cpp:380
void assertInvariants() const
Check invariants (like no duplicated or conflicted names) and abort the process if any invariant is b...
Definition Operator.cpp:114
StringRef getArgName(int index) const
Definition Operator.cpp:248
StringRef getDialectName() const
Returns this op's dialect name.
Definition Operator.cpp:152
const_region_iterator region_end() const
Definition Operator.cpp:309
unsigned getNumVariableLengthResults() const
Returns the number of variable length results in this operation.
Definition Operator.cpp:223
bool hasSingleVariadicArg() const
Returns true of the operation has a single variadic arg.
Definition Operator.cpp:235
const NamedSuccessor * const_successor_iterator
Successors.
Definition Operator.h:287
unsigned getNumVariadicSuccessors() const
Returns the number of variadic successors in this operation.
Definition Operator.cpp:345
StringRef getSummary() const
Definition Operator.cpp:877
bool isVariadic() const
Returns true if this op has variable length operands or results.
Definition Operator.cpp:392
llvm::iterator_range< const_trait_iterator > getTraits() const
Definition Operator.cpp:356
bool hasCustomPropertiesPrinter() const
Returns true if the operation provides a custom properties printer.
Definition Operator.cpp:189
const Trait * getTrait(llvm::StringRef trait) const
Returns the trait wrapper for the given MLIR C++ trait.
Definition Operator.cpp:261
SmallVector< StringRef > getInherentAttrNames() const
Returns all accepted attribute spellings for this operation's inherent attributes and properties,...
Definition Operator.cpp:288
llvm::iterator_range< const_successor_iterator > getSuccessors() const
Definition Operator.cpp:334
bool hasSummary() const
Definition Operator.cpp:875
const_successor_iterator successor_begin() const
Definition Operator.cpp:328
void print(llvm::raw_ostream &os) const
Prints the contents in this operator to the given os.
Definition Operator.cpp:891
unsigned getNumRegions() const
Returns the number of regions.
Definition Operator.cpp:317
const_trait_iterator trait_begin() const
Definition Operator.cpp:350
NamedAttribute * attribute_iterator
Definition Operator.h:186
StringRef getExtraClassDeclaration() const
Returns this op's extra class declaration code.
Definition Operator.cpp:169
StringRef getAssemblyFormat() const
Definition Operator.cpp:886
std::string getSetterName(StringRef name) const
Returns the setter name for the accessor of name.
Definition Operator.cpp:914
std::string getOperationName() const
Returns the operation name.
Definition Operator.cpp:63
const NamedSuccessor & getSuccessor(unsigned index) const
Returns the index-th successor.
Definition Operator.cpp:341
StringRef getCppClassName() const
Returns this op's C++ class name.
Definition Operator.cpp:154
bool allResultTypesKnown() const
Return whether all the result types are known.
Definition Operator.h:338
bool hasAssemblyFormat() const
Query functions for the assembly format of the operator.
Definition Operator.cpp:881
unsigned getNumVariadicRegions() const
Returns the number of variadic regions in this operation.
Definition Operator.cpp:323
bool skipDefaultBuilders() const
Returns true if default builders should not be generated.
Definition Operator.cpp:185
arg_iterator arg_begin() const
Op argument (attribute or operand) iterators.
Definition Operator.cpp:240
const InferredResultType & getInferredResultType(int index) const
Return all arguments or type constraints with same type as result[index].
Definition Operator.cpp:860
const llvm::Record & getDef() const
Returns the Tablegen definition this operator was constructed from.
Definition Operator.cpp:183
const_value_iterator result_end() const
Definition Operator.cpp:197
std::string getRemoverName(StringRef name) const
Returns the remove name for the accessor of name.
Definition Operator.cpp:918
Argument getArg(int index) const
Op argument (attribute or operand) accessors.
Definition Operator.cpp:390
bool hasDescription() const
Query functions for the documentation of the operator.
Definition Operator.cpp:867
static Trait create(const llvm::Init *init)
Definition Trait.cpp:26
bool isVariableLength() const
Definition Type.h:53
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:227
constexpr StringLiteral legacyResultSegmentAttrName
Definition Operator.h:46
const char * inferTypeOpInterface
constexpr StringLiteral legacyOperandSegmentAttrName
Definition Operator.h:44
constexpr StringLiteral operandSegmentAttrName
The canonical and legacy names of the implicit segment-size properties.
Definition Operator.h:42
llvm::PointerUnion< NamedAttribute *, NamedProperty *, NamedTypeConstraint * > Argument
Definition Argument.h:63
constexpr StringLiteral resultSegmentAttrName
Definition Operator.h:43
Include the generated interface declarations.
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:139
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:120
bool isVariadic() const
Definition Region.h:33
Pair consisting kind of argument and index into operands, attributes, or properties.
Definition Operator.h:346
static VariableDecorator unwrap(const llvm::Init *init)
Definition Operator.cpp:901
A class used to represent the decorators of an operator variable, i.e.
Definition Operator.h:118