2018-12-29 04:02:08 +08:00
|
|
|
//===- Operator.cpp - Operator class --------------------------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-12-29 04:02:08 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-12-29 04:02:08 +08:00
|
|
|
//
|
2019-01-08 02:09:34 +08:00
|
|
|
// Operator wrapper to simplify using TableGen Record defining a MLIR Op.
|
2018-12-29 04:02:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/TableGen/Operator.h"
|
2019-01-06 00:11:29 +08:00
|
|
|
#include "mlir/TableGen/Predicate.h"
|
2021-04-16 02:29:23 +08:00
|
|
|
#include "mlir/TableGen/Trait.h"
|
2019-01-09 09:19:22 +08:00
|
|
|
#include "mlir/TableGen/Type.h"
|
2020-05-27 23:45:55 +08:00
|
|
|
#include "llvm/ADT/EquivalenceClasses.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Sequence.h"
|
2019-12-20 08:43:35 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2020-09-15 04:01:07 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2020-04-15 05:53:50 +08:00
|
|
|
#include "llvm/ADT/TypeSwitch.h"
|
2020-03-01 09:11:12 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2021-10-20 22:08:36 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-12-29 04:02:08 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
2019-11-15 03:02:52 +08:00
|
|
|
#define DEBUG_TYPE "mlir-tblgen-operator"
|
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
using namespace mlir;
|
2020-08-12 08:47:07 +08:00
|
|
|
using namespace mlir::tblgen;
|
2019-01-09 09:19:37 +08:00
|
|
|
|
2018-12-29 04:02:08 +08:00
|
|
|
using llvm::DagInit;
|
|
|
|
using llvm::DefInit;
|
|
|
|
using llvm::Record;
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator::Operator(const llvm::Record &def)
|
2019-05-21 00:33:10 +08:00
|
|
|
: dialect(def.getValueAsDef("opDialect")), def(def) {
|
|
|
|
// The first `_` in the op's TableGen def name is treated as separating the
|
|
|
|
// dialect prefix and the op class name. The dialect prefix will be ignored if
|
|
|
|
// not empty. Otherwise, if def name starts with a `_`, the `_` is considered
|
|
|
|
// as part of the class name.
|
|
|
|
StringRef prefix;
|
|
|
|
std::tie(prefix, cppClassName) = def.getName().split('_');
|
|
|
|
if (prefix.empty()) {
|
|
|
|
// Class name with a leading underscore and without dialect prefix
|
2019-04-16 00:13:22 +08:00
|
|
|
cppClassName = def.getName();
|
|
|
|
} else if (cppClassName.empty()) {
|
2019-05-21 00:33:10 +08:00
|
|
|
// Class name without dialect prefix
|
|
|
|
cppClassName = prefix;
|
2019-04-16 00:13:22 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2021-05-11 05:30:22 +08:00
|
|
|
cppNamespace = def.getValueAsString("cppNamespace");
|
|
|
|
|
2019-04-16 00:13:22 +08:00
|
|
|
populateOpStructure();
|
2021-09-09 08:10:16 +08:00
|
|
|
assertInvariants();
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string Operator::getOperationName() const {
|
2019-05-21 00:33:10 +08:00
|
|
|
auto prefix = dialect.getName();
|
|
|
|
auto opName = def.getValueAsString("opName");
|
2019-04-30 00:24:09 +08:00
|
|
|
if (prefix.empty())
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(opName);
|
|
|
|
return std::string(llvm::formatv("{0}.{1}", prefix, opName));
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string Operator::getAdaptorName() const {
|
2020-06-15 21:01:31 +08:00
|
|
|
return std::string(llvm::formatv("{0}Adaptor", getCppClassName()));
|
2020-05-29 00:05:24 +08:00
|
|
|
}
|
|
|
|
|
2021-09-09 08:10:16 +08:00
|
|
|
void Operator::assertInvariants() const {
|
|
|
|
// Check that the name of arguments/results/regions/successors don't overlap.
|
|
|
|
DenseMap<StringRef, StringRef> existingNames;
|
|
|
|
auto checkName = [&](StringRef name, StringRef entity) {
|
|
|
|
if (name.empty())
|
|
|
|
return;
|
|
|
|
auto insertion = existingNames.insert({name, entity});
|
|
|
|
if (insertion.second)
|
|
|
|
return;
|
|
|
|
if (entity == insertion.first->second)
|
|
|
|
PrintFatalError(getLoc(), "op has a conflict with two " + entity +
|
|
|
|
" having the same name '" + name + "'");
|
|
|
|
PrintFatalError(getLoc(), "op has a conflict with " +
|
|
|
|
insertion.first->second + " and " + entity +
|
|
|
|
" both having an entry with the name '" +
|
|
|
|
name + "'");
|
|
|
|
};
|
|
|
|
// Check operands amongst themselves.
|
|
|
|
for (int i : llvm::seq<int>(0, getNumOperands()))
|
|
|
|
checkName(getOperand(i).name, "operands");
|
|
|
|
|
|
|
|
// Check results amongst themselves and against operands.
|
|
|
|
for (int i : llvm::seq<int>(0, getNumResults()))
|
|
|
|
checkName(getResult(i).name, "results");
|
|
|
|
|
|
|
|
// Check regions amongst themselves and against operands and results.
|
|
|
|
for (int i : llvm::seq<int>(0, getNumRegions()))
|
|
|
|
checkName(getRegion(i).name, "regions");
|
|
|
|
|
|
|
|
// Check successors amongst themselves and against operands, results, and
|
|
|
|
// regions.
|
|
|
|
for (int i : llvm::seq<int>(0, getNumSuccessors()))
|
|
|
|
checkName(getSuccessor(i).name, "successors");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getDialectName() const { return dialect.getName(); }
|
2019-04-16 00:13:22 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getCppClassName() const { return cppClassName; }
|
2019-05-21 00:33:10 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
std::string Operator::getQualCppClassName() const {
|
2021-05-11 05:30:22 +08:00
|
|
|
if (cppNamespace.empty())
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(cppClassName);
|
2021-05-11 05:30:22 +08:00
|
|
|
return std::string(llvm::formatv("{0}::{1}", cppNamespace, cppClassName));
|
2019-01-03 08:11:42 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2021-05-11 05:30:22 +08:00
|
|
|
StringRef Operator::getCppNamespace() const { return cppNamespace; }
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
int Operator::getNumResults() const {
|
2019-01-30 02:03:17 +08:00
|
|
|
DagInit *results = def.getValueAsDag("results");
|
|
|
|
return results->getNumArgs();
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getExtraClassDeclaration() const {
|
2019-04-30 14:12:40 +08:00
|
|
|
constexpr auto attr = "extraClassDeclaration";
|
|
|
|
if (def.isValueUnset(attr))
|
|
|
|
return {};
|
|
|
|
return def.getValueAsString(attr);
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
const llvm::Record &Operator::getDef() const { return def; }
|
2019-06-03 23:03:20 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::skipDefaultBuilders() const {
|
2019-07-05 17:27:39 +08:00
|
|
|
return def.getValueAsBit("skipDefaultBuilders");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::result_begin() -> value_iterator { return results.begin(); }
|
2019-06-04 20:03:42 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::result_end() -> value_iterator { return results.end(); }
|
2019-06-04 20:03:42 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getResults() -> value_range {
|
2019-06-04 20:03:42 +08:00
|
|
|
return {result_begin(), result_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
TypeConstraint Operator::getResultTypeConstraint(int index) const {
|
2019-02-21 02:50:26 +08:00
|
|
|
DagInit *results = def.getValueAsDag("results");
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
return TypeConstraint(cast<DefInit>(results->getArg(index)));
|
2019-01-30 02:03:17 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getResultName(int index) const {
|
2019-02-21 02:50:26 +08:00
|
|
|
DagInit *results = def.getValueAsDag("results");
|
|
|
|
return results->getArgNameStr(index);
|
2019-02-19 23:15:59 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getResultDecorators(int index) const -> var_decorator_range {
|
2020-03-07 05:55:36 +08:00
|
|
|
Record *result =
|
|
|
|
cast<DefInit>(def.getValueAsDag("results")->getArg(index))->getDef();
|
|
|
|
if (!result->isSubClassOf("OpVariable"))
|
|
|
|
return var_decorator_range(nullptr, nullptr);
|
|
|
|
return *result->getValueAsListInit("decorators");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumVariableLengthResults() const {
|
2020-04-11 05:11:45 +08:00
|
|
|
return llvm::count_if(results, [](const NamedTypeConstraint &c) {
|
|
|
|
return c.constraint.isVariableLength();
|
|
|
|
});
|
2019-01-30 02:03:17 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumVariableLengthOperands() const {
|
2020-04-11 05:11:45 +08:00
|
|
|
return llvm::count_if(operands, [](const NamedTypeConstraint &c) {
|
|
|
|
return c.constraint.isVariableLength();
|
|
|
|
});
|
2019-02-06 21:06:11 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::hasSingleVariadicArg() const {
|
|
|
|
return getNumArgs() == 1 && getArg(0).is<NamedTypeConstraint *>() &&
|
2020-08-08 05:02:19 +08:00
|
|
|
getOperand(0).isVariadic();
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator::arg_iterator Operator::arg_begin() const { return arguments.begin(); }
|
2019-10-22 11:47:49 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator::arg_iterator Operator::arg_end() const { return arguments.end(); }
|
2019-10-22 11:47:49 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
Operator::arg_range Operator::getArgs() const {
|
2019-10-22 11:47:49 +08:00
|
|
|
return {arg_begin(), arg_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getArgName(int index) const {
|
2018-12-29 04:02:08 +08:00
|
|
|
DagInit *argumentValues = def.getValueAsDag("arguments");
|
[mlir] Add basic support for attributes in ODS-generated Python bindings
In ODS, attributes of an operation can be provided as a part of the "arguments"
field, together with operands. Such attributes are accepted by the op builder
and have accessors generated.
Implement similar functionality for ODS-generated op-specific Python bindings:
the `__init__` method now accepts arguments together with operands, in the same
order as in the ODS `arguments` field; the instance properties are introduced
to OpView classes to access the attributes.
This initial implementation accepts and returns instances of the corresponding
attribute class, and not the underlying values since the mapping scheme of the
value types between C++, C and Python is not yet clear. Default-valued
attributes are not supported as that would require Python to be able to parse
C++ literals.
Since attributes in ODS are tightely related to the actual C++ type system,
provide a separate Tablegen file with the mapping between ODS storage type for
attributes (typically, the underlying C++ attribute class), and the
corresponding class name. So far, this might look unnecessary since all names
match exactly, but this is not necessarily the cases for non-standard,
out-of-tree attributes, which may also be placed in non-default namespaces or
Python modules. This also allows out-of-tree users to generate Python bindings
without having to modify the bindings generator itself. Storage type was
preferred over the Tablegen "def" of the attribute class because ODS
essentially encodes attribute _constraints_ rather than classes, e.g. there may
be many Tablegen "def"s in the ODS that correspond to the same attribute type
with additional constraints
The presence of the explicit mapping requires the change in the .td file
structure: instead of just calling the bindings generator directly on the main
ODS file of the dialect, it becomes necessary to create a new file that
includes the main ODS file of the dialect and provides the mapping for
attribute types. Arguably, this approach offers better separability of the
Python bindings in the build system as the main dialect no longer needs to know
that it is being processed by the bindings generator.
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D91542
2020-11-16 23:17:03 +08:00
|
|
|
return argumentValues->getArgNameStr(index);
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getArgDecorators(int index) const -> var_decorator_range {
|
2020-03-07 05:55:36 +08:00
|
|
|
Record *arg =
|
|
|
|
cast<DefInit>(def.getValueAsDag("arguments")->getArg(index))->getDef();
|
|
|
|
if (!arg->isSubClassOf("OpVariable"))
|
|
|
|
return var_decorator_range(nullptr, nullptr);
|
|
|
|
return *arg->getValueAsListInit("decorators");
|
|
|
|
}
|
|
|
|
|
2021-04-16 02:29:23 +08:00
|
|
|
const Trait *Operator::getTrait(StringRef trait) const {
|
2019-11-26 09:26:16 +08:00
|
|
|
for (const auto &t : traits) {
|
2021-04-16 02:29:23 +08:00
|
|
|
if (const auto *traitDef = dyn_cast<NativeTrait>(&t)) {
|
|
|
|
if (traitDef->getFullyQualifiedTraitName() == trait)
|
|
|
|
return traitDef;
|
|
|
|
} else if (const auto *traitDef = dyn_cast<InternalTrait>(&t)) {
|
|
|
|
if (traitDef->getFullyQualifiedTraitName() == trait)
|
|
|
|
return traitDef;
|
|
|
|
} else if (const auto *traitDef = dyn_cast<InterfaceTrait>(&t)) {
|
|
|
|
if (traitDef->getFullyQualifiedTraitName() == trait)
|
|
|
|
return traitDef;
|
2019-03-27 06:31:15 +08:00
|
|
|
}
|
2019-02-21 02:50:26 +08:00
|
|
|
}
|
2019-11-26 09:26:16 +08:00
|
|
|
return nullptr;
|
2019-02-15 02:54:50 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::region_begin() const -> const_region_iterator {
|
2020-04-05 16:03:24 +08:00
|
|
|
return regions.begin();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::region_end() const -> const_region_iterator {
|
2020-04-05 16:03:24 +08:00
|
|
|
return regions.end();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getRegions() const
|
2020-04-05 16:03:24 +08:00
|
|
|
-> llvm::iterator_range<const_region_iterator> {
|
|
|
|
return {region_begin(), region_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumRegions() const { return regions.size(); }
|
2019-05-31 07:50:16 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
const NamedRegion &Operator::getRegion(unsigned index) const {
|
2019-05-31 07:50:16 +08:00
|
|
|
return regions[index];
|
|
|
|
}
|
2019-05-28 23:03:46 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumVariadicRegions() const {
|
2020-04-05 16:03:24 +08:00
|
|
|
return llvm::count_if(regions,
|
|
|
|
[](const NamedRegion &c) { return c.isVariadic(); });
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::successor_begin() const -> const_successor_iterator {
|
2020-02-22 05:19:50 +08:00
|
|
|
return successors.begin();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::successor_end() const -> const_successor_iterator {
|
2020-02-22 05:19:50 +08:00
|
|
|
return successors.end();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getSuccessors() const
|
2020-02-22 05:19:50 +08:00
|
|
|
-> llvm::iterator_range<const_successor_iterator> {
|
|
|
|
return {successor_begin(), successor_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumSuccessors() const { return successors.size(); }
|
2020-02-22 05:19:50 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
const NamedSuccessor &Operator::getSuccessor(unsigned index) const {
|
2020-02-22 05:19:50 +08:00
|
|
|
return successors[index];
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
unsigned Operator::getNumVariadicSuccessors() const {
|
2020-02-22 05:19:50 +08:00
|
|
|
return llvm::count_if(successors,
|
|
|
|
[](const NamedSuccessor &c) { return c.isVariadic(); });
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::trait_begin() const -> const_trait_iterator {
|
2019-02-21 02:50:26 +08:00
|
|
|
return traits.begin();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::trait_end() const -> const_trait_iterator {
|
2019-02-21 02:50:26 +08:00
|
|
|
return traits.end();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getTraits() const -> llvm::iterator_range<const_trait_iterator> {
|
2019-02-21 02:50:26 +08:00
|
|
|
return {trait_begin(), trait_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::attribute_begin() const -> attribute_iterator {
|
2018-12-29 04:02:08 +08:00
|
|
|
return attributes.begin();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::attribute_end() const -> attribute_iterator {
|
2018-12-29 04:02:08 +08:00
|
|
|
return attributes.end();
|
|
|
|
}
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getAttributes() const
|
2019-01-09 09:19:37 +08:00
|
|
|
-> llvm::iterator_range<attribute_iterator> {
|
2018-12-29 04:02:08 +08:00
|
|
|
return {attribute_begin(), attribute_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::operand_begin() -> value_iterator { return operands.begin(); }
|
|
|
|
auto Operator::operand_end() -> value_iterator { return operands.end(); }
|
|
|
|
auto Operator::getOperands() -> value_range {
|
2018-12-29 04:02:08 +08:00
|
|
|
return {operand_begin(), operand_end()};
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getArg(int index) const -> Argument { return arguments[index]; }
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2020-05-27 23:45:55 +08:00
|
|
|
// Mapping from result index to combined argument and result index. Arguments
|
|
|
|
// are indexed to match getArg index, while the result indexes are mapped to
|
|
|
|
// avoid overlap.
|
|
|
|
static int resultIndex(int i) { return -1 - i; }
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::isVariadic() const {
|
2020-05-27 23:45:55 +08:00
|
|
|
return any_of(llvm::concat<const NamedTypeConstraint>(operands, results),
|
|
|
|
[](const NamedTypeConstraint &op) { return op.isVariadic(); });
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Operator::populateTypeInferenceInfo(
|
2020-05-27 23:45:55 +08:00
|
|
|
const llvm::StringMap<int> &argumentsAndResultsIndex) {
|
|
|
|
// If the type inference op interface is not registered, then do not attempt
|
|
|
|
// to determine if the result types an be inferred.
|
|
|
|
auto &recordKeeper = def.getRecords();
|
|
|
|
auto *inferTrait = recordKeeper.getDef(inferTypeOpInterface);
|
|
|
|
allResultsHaveKnownTypes = false;
|
|
|
|
if (!inferTrait)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If there are no results, the skip this else the build method generated
|
|
|
|
// overlaps with another autogenerated builder.
|
|
|
|
if (getNumResults() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip for ops with variadic operands/results.
|
|
|
|
// TODO: This can be relaxed.
|
|
|
|
if (isVariadic())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Skip cases currently being custom generated.
|
|
|
|
// TODO: Remove special cases.
|
2020-09-15 04:01:07 +08:00
|
|
|
if (getTrait("::mlir::OpTrait::SameOperandsAndResultType"))
|
2020-05-27 23:45:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We create equivalence classes of argument/result types where arguments
|
|
|
|
// and results are mapped into the same index space and indices corresponding
|
|
|
|
// to the same type are in the same equivalence class.
|
|
|
|
llvm::EquivalenceClasses<int> ecs;
|
|
|
|
resultTypeMapping.resize(getNumResults());
|
|
|
|
// Captures the argument whose type matches a given result type. Preference
|
|
|
|
// towards capturing operands first before attributes.
|
|
|
|
auto captureMapping = [&](int i) {
|
|
|
|
bool found = false;
|
|
|
|
ecs.insert(resultIndex(i));
|
|
|
|
auto mi = ecs.findLeader(resultIndex(i));
|
|
|
|
for (auto me = ecs.member_end(); mi != me; ++mi) {
|
|
|
|
if (*mi < 0) {
|
|
|
|
auto tc = getResultTypeConstraint(i);
|
|
|
|
if (tc.getBuilderCall().hasValue()) {
|
|
|
|
resultTypeMapping[i].emplace_back(tc);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:05:47 +08:00
|
|
|
if (getArg(*mi).is<NamedAttribute *>()) {
|
2020-05-27 23:45:55 +08:00
|
|
|
// TODO: Handle attributes.
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-21 03:45:05 +08:00
|
|
|
resultTypeMapping[i].emplace_back(*mi);
|
|
|
|
found = true;
|
2020-05-27 23:45:55 +08:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
};
|
|
|
|
|
2021-04-16 02:29:23 +08:00
|
|
|
for (const Trait &trait : traits) {
|
2020-05-27 23:45:55 +08:00
|
|
|
const llvm::Record &def = trait.getDef();
|
|
|
|
// If the infer type op interface was manually added, then treat it as
|
|
|
|
// intention that the op needs special handling.
|
|
|
|
// TODO: Reconsider whether to always generate, this is more conservative
|
|
|
|
// and keeps existing behavior so starting that way for now.
|
|
|
|
if (def.isSubClassOf(
|
|
|
|
llvm::formatv("{0}::Trait", inferTypeOpInterface).str()))
|
|
|
|
return;
|
2021-04-16 02:29:23 +08:00
|
|
|
if (const auto *traitDef = dyn_cast<InterfaceTrait>(&trait))
|
|
|
|
if (&traitDef->getDef() == inferTrait)
|
2020-05-27 23:45:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!def.isSubClassOf("AllTypesMatch"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto values = def.getValueAsListOfStrings("values");
|
|
|
|
auto root = argumentsAndResultsIndex.lookup(values.front());
|
|
|
|
for (StringRef str : values)
|
|
|
|
ecs.unionSets(argumentsAndResultsIndex.lookup(str), root);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies that all output types have a corresponding known input type
|
|
|
|
// and chooses matching operand or attribute (in that order) that
|
|
|
|
// matches it.
|
|
|
|
allResultsHaveKnownTypes =
|
|
|
|
all_of(llvm::seq<int>(0, getNumResults()), captureMapping);
|
|
|
|
|
|
|
|
// If the types could be computed, then add type inference trait.
|
|
|
|
if (allResultsHaveKnownTypes)
|
2021-04-16 02:29:23 +08:00
|
|
|
traits.push_back(Trait::create(inferTrait->getDefInit()));
|
2020-05-27 23:45:55 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Operator::populateOpStructure() {
|
2018-12-29 04:02:08 +08:00
|
|
|
auto &recordKeeper = def.getRecords();
|
2020-05-27 23:45:55 +08:00
|
|
|
auto *typeConstraintClass = recordKeeper.getClass("TypeConstraint");
|
|
|
|
auto *attrClass = recordKeeper.getClass("Attr");
|
|
|
|
auto *derivedAttrClass = recordKeeper.getClass("DerivedAttr");
|
|
|
|
auto *opVarClass = recordKeeper.getClass("OpVariable");
|
2019-03-06 17:23:41 +08:00
|
|
|
numNativeAttributes = 0;
|
2018-12-29 04:02:08 +08:00
|
|
|
|
|
|
|
DagInit *argumentValues = def.getValueAsDag("arguments");
|
2019-11-15 03:02:52 +08:00
|
|
|
unsigned numArgs = argumentValues->getNumArgs();
|
|
|
|
|
2020-05-27 23:45:55 +08:00
|
|
|
// Mapping from name of to argument or result index. Arguments are indexed
|
|
|
|
// to match getArg index, while the results are negatively indexed.
|
|
|
|
llvm::StringMap<int> argumentsAndResultsIndex;
|
|
|
|
|
2019-03-06 17:23:41 +08:00
|
|
|
// Handle operands and native attributes.
|
2019-11-15 03:02:52 +08:00
|
|
|
for (unsigned i = 0; i != numArgs; ++i) {
|
2020-05-27 23:45:55 +08:00
|
|
|
auto *arg = argumentValues->getArg(i);
|
2019-01-30 22:05:27 +08:00
|
|
|
auto givenName = argumentValues->getArgNameStr(i);
|
2020-05-27 23:45:55 +08:00
|
|
|
auto *argDefInit = dyn_cast<DefInit>(arg);
|
2018-12-29 04:02:08 +08:00
|
|
|
if (!argDefInit)
|
|
|
|
PrintFatalError(def.getLoc(),
|
2019-02-19 23:15:59 +08:00
|
|
|
Twine("undefined type for argument #") + Twine(i));
|
2018-12-29 04:02:08 +08:00
|
|
|
Record *argDef = argDefInit->getDef();
|
2020-03-07 05:55:36 +08:00
|
|
|
if (argDef->isSubClassOf(opVarClass))
|
|
|
|
argDef = argDef->getValueAsDef("constraint");
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-03-06 17:23:41 +08:00
|
|
|
if (argDef->isSubClassOf(typeConstraintClass)) {
|
2019-03-18 22:54:20 +08:00
|
|
|
operands.push_back(
|
2020-03-07 05:55:36 +08:00
|
|
|
NamedTypeConstraint{givenName, TypeConstraint(argDef)});
|
2019-03-06 17:23:41 +08:00
|
|
|
} else if (argDef->isSubClassOf(attrClass)) {
|
|
|
|
if (givenName.empty())
|
|
|
|
PrintFatalError(argDef->getLoc(), "attributes must be named");
|
|
|
|
if (argDef->isSubClassOf(derivedAttrClass))
|
|
|
|
PrintFatalError(argDef->getLoc(),
|
|
|
|
"derived attributes not allowed in argument list");
|
|
|
|
attributes.push_back({givenName, Attribute(argDef)});
|
|
|
|
++numNativeAttributes;
|
|
|
|
} else {
|
|
|
|
PrintFatalError(def.getLoc(), "unexpected def type; only defs deriving "
|
|
|
|
"from TypeConstraint or Attr are allowed");
|
|
|
|
}
|
2020-05-27 23:45:55 +08:00
|
|
|
if (!givenName.empty())
|
|
|
|
argumentsAndResultsIndex[givenName] = i;
|
2019-01-04 07:53:54 +08:00
|
|
|
}
|
2018-12-29 04:02:08 +08:00
|
|
|
|
2019-01-08 02:09:34 +08:00
|
|
|
// Handle derived attributes.
|
2019-01-04 07:53:54 +08:00
|
|
|
for (const auto &val : def.getValues()) {
|
|
|
|
if (auto *record = dyn_cast<llvm::RecordRecTy>(val.getType())) {
|
|
|
|
if (!record->isSubClassOf(attrClass))
|
|
|
|
continue;
|
|
|
|
if (!record->isSubClassOf(derivedAttrClass))
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
"unexpected Attr where only DerivedAttr is allowed");
|
|
|
|
|
|
|
|
if (record->getClasses().size() != 1) {
|
2018-12-29 04:02:08 +08:00
|
|
|
PrintFatalError(
|
|
|
|
def.getLoc(),
|
2019-01-04 07:53:54 +08:00
|
|
|
"unsupported attribute modelling, only single class expected");
|
|
|
|
}
|
2019-01-30 22:05:27 +08:00
|
|
|
attributes.push_back(
|
|
|
|
{cast<llvm::StringInit>(val.getNameInit())->getValue(),
|
|
|
|
Attribute(cast<DefInit>(val.getValue()))});
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 21:06:11 +08:00
|
|
|
|
2019-11-15 03:02:52 +08:00
|
|
|
// Populate `arguments`. This must happen after we've finalized `operands` and
|
|
|
|
// `attributes` because we will put their elements' pointers in `arguments`.
|
|
|
|
// SmallVector may perform re-allocation under the hood when adding new
|
|
|
|
// elements.
|
|
|
|
int operandIndex = 0, attrIndex = 0;
|
|
|
|
for (unsigned i = 0; i != numArgs; ++i) {
|
|
|
|
Record *argDef = dyn_cast<DefInit>(argumentValues->getArg(i))->getDef();
|
2020-03-07 05:55:36 +08:00
|
|
|
if (argDef->isSubClassOf(opVarClass))
|
|
|
|
argDef = argDef->getValueAsDef("constraint");
|
2019-11-15 03:02:52 +08:00
|
|
|
|
|
|
|
if (argDef->isSubClassOf(typeConstraintClass)) {
|
2020-06-30 07:40:52 +08:00
|
|
|
attrOrOperandMapping.push_back(
|
|
|
|
{OperandOrAttribute::Kind::Operand, operandIndex});
|
2019-11-15 03:02:52 +08:00
|
|
|
arguments.emplace_back(&operands[operandIndex++]);
|
|
|
|
} else {
|
|
|
|
assert(argDef->isSubClassOf(attrClass));
|
2020-06-30 07:40:52 +08:00
|
|
|
attrOrOperandMapping.push_back(
|
|
|
|
{OperandOrAttribute::Kind::Attribute, attrIndex});
|
2019-11-15 03:02:52 +08:00
|
|
|
arguments.emplace_back(&attributes[attrIndex++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:15:59 +08:00
|
|
|
auto *resultsDag = def.getValueAsDag("results");
|
|
|
|
auto *outsOp = dyn_cast<DefInit>(resultsDag->getOperator());
|
|
|
|
if (!outsOp || outsOp->getDef()->getName() != "outs") {
|
|
|
|
PrintFatalError(def.getLoc(), "'results' must have 'outs' directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle results.
|
|
|
|
for (unsigned i = 0, e = resultsDag->getNumArgs(); i < e; ++i) {
|
|
|
|
auto name = resultsDag->getArgNameStr(i);
|
2020-03-07 05:55:36 +08:00
|
|
|
auto *resultInit = dyn_cast<DefInit>(resultsDag->getArg(i));
|
|
|
|
if (!resultInit) {
|
2019-02-19 23:15:59 +08:00
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
Twine("undefined type for result #") + Twine(i));
|
|
|
|
}
|
2020-03-07 05:55:36 +08:00
|
|
|
auto *resultDef = resultInit->getDef();
|
|
|
|
if (resultDef->isSubClassOf(opVarClass))
|
|
|
|
resultDef = resultDef->getValueAsDef("constraint");
|
[TableGen] Consolidate constraint related concepts
Previously we have multiple mechanisms to specify op definition and match constraints:
TypeConstraint, AttributeConstraint, Type, Attr, mAttr, mAttrAnyOf, mPat. These variants
are not added because there are so many distinct cases we need to model; essentially,
they are all carrying a predicate. It's just an artifact of implementation.
It's quite confusing for users to grasp these variants and choose among them. Instead,
as the OpBase TableGen file, we need to strike to provide an unified mechanism. Each
dialect has the flexibility to define its own aliases if wanted.
This CL removes mAttr, mAttrAnyOf, mPat. A new base class, Constraint, is added. Now
TypeConstraint and AttrConstraint derive from Constraint. Type and Attr further derive
from TypeConstraint and AttrConstraint, respectively.
Comments are revised and examples are added to make it clear how to use constraints.
PiperOrigin-RevId: 240125076
2019-03-25 21:09:26 +08:00
|
|
|
results.push_back({name, TypeConstraint(resultDef)});
|
2020-05-27 23:45:55 +08:00
|
|
|
if (!name.empty())
|
|
|
|
argumentsAndResultsIndex[name] = resultIndex(i);
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
|
|
|
|
// We currently only support VariadicOfVariadic operands.
|
|
|
|
if (results.back().constraint.isVariadicOfVariadic()) {
|
|
|
|
PrintFatalError(
|
|
|
|
def.getLoc(),
|
|
|
|
"'VariadicOfVariadic' results are currently not supported");
|
|
|
|
}
|
2019-02-19 23:15:59 +08:00
|
|
|
}
|
|
|
|
|
2020-02-22 05:19:50 +08:00
|
|
|
// Handle successors
|
|
|
|
auto *successorsDag = def.getValueAsDag("successors");
|
|
|
|
auto *successorsOp = dyn_cast<DefInit>(successorsDag->getOperator());
|
|
|
|
if (!successorsOp || successorsOp->getDef()->getName() != "successor") {
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
"'successors' must have 'successor' directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = successorsDag->getNumArgs(); i < e; ++i) {
|
|
|
|
auto name = successorsDag->getArgNameStr(i);
|
|
|
|
auto *successorInit = dyn_cast<DefInit>(successorsDag->getArg(i));
|
|
|
|
if (!successorInit) {
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
Twine("undefined kind for successor #") + Twine(i));
|
|
|
|
}
|
|
|
|
Successor successor(successorInit->getDef());
|
|
|
|
|
|
|
|
// Only support variadic successors if it is the last one for now.
|
|
|
|
if (i != e - 1 && successor.isVariadic())
|
|
|
|
PrintFatalError(def.getLoc(), "only the last successor can be variadic");
|
|
|
|
successors.push_back({name, successor});
|
|
|
|
}
|
|
|
|
|
2019-12-20 08:43:35 +08:00
|
|
|
// Create list of traits, skipping over duplicates: appending to lists in
|
|
|
|
// tablegen is easy, making them unique less so, so dedupe here.
|
2020-05-27 23:45:55 +08:00
|
|
|
if (auto *traitList = def.getValueAsListInit("traits")) {
|
2019-12-20 08:43:35 +08:00
|
|
|
// This is uniquing based on pointers of the trait.
|
|
|
|
SmallPtrSet<const llvm::Init *, 32> traitSet;
|
|
|
|
traits.reserve(traitSet.size());
|
2021-07-21 01:44:48 +08:00
|
|
|
|
|
|
|
std::function<void(llvm::ListInit *)> insert;
|
|
|
|
insert = [&](llvm::ListInit *traitList) {
|
|
|
|
for (auto *traitInit : *traitList) {
|
|
|
|
auto *def = cast<DefInit>(traitInit)->getDef();
|
|
|
|
if (def->isSubClassOf("OpTraitList")) {
|
|
|
|
insert(def->getValueAsListInit("traits"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Keep traits in the same order while skipping over duplicates.
|
|
|
|
if (traitSet.insert(traitInit).second)
|
|
|
|
traits.push_back(Trait::create(traitInit));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
insert(traitList);
|
2019-12-20 08:43:35 +08:00
|
|
|
}
|
2019-05-28 23:03:46 +08:00
|
|
|
|
2020-05-27 23:45:55 +08:00
|
|
|
populateTypeInferenceInfo(argumentsAndResultsIndex);
|
|
|
|
|
2019-05-28 23:03:46 +08:00
|
|
|
// Handle regions
|
2019-05-31 07:50:16 +08:00
|
|
|
auto *regionsDag = def.getValueAsDag("regions");
|
|
|
|
auto *regionsOp = dyn_cast<DefInit>(regionsDag->getOperator());
|
|
|
|
if (!regionsOp || regionsOp->getDef()->getName() != "region") {
|
|
|
|
PrintFatalError(def.getLoc(), "'regions' must have 'region' directive");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = regionsDag->getNumArgs(); i < e; ++i) {
|
|
|
|
auto name = regionsDag->getArgNameStr(i);
|
|
|
|
auto *regionInit = dyn_cast<DefInit>(regionsDag->getArg(i));
|
|
|
|
if (!regionInit) {
|
|
|
|
PrintFatalError(def.getLoc(),
|
|
|
|
Twine("undefined kind for region #") + Twine(i));
|
|
|
|
}
|
2020-04-05 16:03:24 +08:00
|
|
|
Region region(regionInit->getDef());
|
|
|
|
if (region.isVariadic()) {
|
|
|
|
// Only support variadic regions if it is the last one for now.
|
|
|
|
if (i != e - 1)
|
|
|
|
PrintFatalError(def.getLoc(), "only the last region can be variadic");
|
|
|
|
if (name.empty())
|
|
|
|
PrintFatalError(def.getLoc(), "variadic regions must be named");
|
|
|
|
}
|
|
|
|
|
|
|
|
regions.push_back({name, region});
|
2019-05-31 07:50:16 +08:00
|
|
|
}
|
2019-11-15 03:02:52 +08:00
|
|
|
|
2021-01-12 03:54:51 +08:00
|
|
|
// Populate the builders.
|
|
|
|
auto *builderList =
|
|
|
|
dyn_cast_or_null<llvm::ListInit>(def.getValueInit("builders"));
|
|
|
|
if (builderList && !builderList->empty()) {
|
|
|
|
for (llvm::Init *init : builderList->getValues())
|
|
|
|
builders.emplace_back(cast<llvm::DefInit>(init)->getDef(), def.getLoc());
|
|
|
|
} else if (skipDefaultBuilders()) {
|
|
|
|
PrintFatalError(
|
|
|
|
def.getLoc(),
|
|
|
|
"default builders are skipped and no custom builders provided");
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:02:52 +08:00
|
|
|
LLVM_DEBUG(print(llvm::dbgs()));
|
2018-12-29 04:02:08 +08:00
|
|
|
}
|
2019-01-06 00:11:29 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getSameTypeAsResult(int index) const -> ArrayRef<ArgOrType> {
|
2020-05-27 23:45:55 +08:00
|
|
|
assert(allResultTypesKnown());
|
|
|
|
return resultTypeMapping[index];
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
ArrayRef<llvm::SMLoc> Operator::getLoc() const { return def.getLoc(); }
|
2019-02-06 04:02:53 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::hasDescription() const {
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-23 01:31:04 +08:00
|
|
|
return def.getValue("description") != nullptr;
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getDescription() const {
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-23 01:31:04 +08:00
|
|
|
return def.getValueAsString("description");
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::hasSummary() const { return def.getValue("summary") != nullptr; }
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-23 01:31:04 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getSummary() const {
|
Start doc generation pass.
Start doc generation pass that generates simple markdown output. The output is formatted simply[1] in markdown, but this allows seeing what info we have, where we can refine the op description (e.g., the inputs is probably redundant), what info is missing (e.g., the attributes could probably have a description).
The formatting of the description is still left up to whatever was in the op definition (which luckily, due to the uniformity in the .td file, turned out well but relying on the indentation there is fragile). The mechanism to autogenerate these post changes has not been added yet either. The output file could be run through a markdown formatter too to remove extra spaces.
[1]. This is not proposal for final style :) There could also be a discussion around single doc vs multiple (per dialect, per op), whether we want a TOC, whether operands/attributes should be headings or just formatted differently ...
PiperOrigin-RevId: 230354538
2019-01-23 01:31:04 +08:00
|
|
|
return def.getValueAsString("summary");
|
|
|
|
}
|
2019-11-15 03:02:52 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
bool Operator::hasAssemblyFormat() const {
|
2020-03-25 02:57:13 +08:00
|
|
|
auto *valueInit = def.getValueInit("assemblyFormat");
|
2020-11-25 02:09:02 +08:00
|
|
|
return isa<llvm::StringInit>(valueInit);
|
2020-03-25 02:57:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
StringRef Operator::getAssemblyFormat() const {
|
2020-03-25 02:57:13 +08:00
|
|
|
return TypeSwitch<llvm::Init *, StringRef>(def.getValueInit("assemblyFormat"))
|
[mlir] Add support for VariadicOfVariadic operands
This revision adds native ODS support for VariadicOfVariadic operand
groups. An example of this is the SwitchOp, which has a variadic number
of nested operand ranges for each of the case statements, where the
number of case statements is variadic. Builtin ODS support allows for
generating proper accessors for the nested operand ranges, builder
support, and declarative format support. VariadicOfVariadic operands
are supported by providing a segment attribute to use to store the
operand groups, mapping similarly to the AttrSizedOperand trait
(but with a user defined attribute name).
`build` methods for VariadicOfVariadic operand expect inputs of the
form `ArrayRef<ValueRange>`. Accessors for the variadic ranges
return a new `OperandRangeRange` type, which represents a
contiguous range of `OperandRange`. In the declarative assembly
format, VariadicOfVariadic operands and types are by default
formatted as a comma delimited list of value lists:
`(<value>, <value>), (), (<value>)`.
Differential Revision: https://reviews.llvm.org/D107774
2021-08-24 04:23:09 +08:00
|
|
|
.Case<llvm::StringInit>([&](auto *init) { return init->getValue(); });
|
2020-03-25 02:57:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
void Operator::print(llvm::raw_ostream &os) const {
|
2019-11-15 03:02:52 +08:00
|
|
|
os << "op '" << getOperationName() << "'\n";
|
|
|
|
for (Argument arg : arguments) {
|
|
|
|
if (auto *attr = arg.dyn_cast<NamedAttribute *>())
|
|
|
|
os << "[attribute] " << attr->name << '\n';
|
|
|
|
else
|
|
|
|
os << "[operand] " << arg.get<NamedTypeConstraint *>()->name << '\n';
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 05:55:36 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::VariableDecoratorIterator::unwrap(llvm::Init *init)
|
2020-03-07 05:55:36 +08:00
|
|
|
-> VariableDecorator {
|
|
|
|
return VariableDecorator(cast<llvm::DefInit>(init)->getDef());
|
|
|
|
}
|
2020-06-30 07:40:52 +08:00
|
|
|
|
2020-08-12 08:47:07 +08:00
|
|
|
auto Operator::getArgToOperandOrAttribute(int index) const
|
2020-06-30 07:40:52 +08:00
|
|
|
-> OperandOrAttribute {
|
|
|
|
return attrOrOperandMapping[index];
|
|
|
|
}
|
2021-10-20 22:08:36 +08:00
|
|
|
|
|
|
|
// Helper to return the names for accessor.
|
|
|
|
static SmallVector<std::string, 2>
|
|
|
|
getGetterOrSetterNames(bool isGetter, const Operator &op, StringRef name) {
|
|
|
|
Dialect::EmitPrefix prefixType = op.getDialect().getEmitAccessorPrefix();
|
|
|
|
std::string prefix;
|
|
|
|
if (prefixType != Dialect::EmitPrefix::Raw)
|
|
|
|
prefix = isGetter ? "get" : "set";
|
|
|
|
|
|
|
|
SmallVector<std::string, 2> names;
|
|
|
|
bool rawToo = prefixType == Dialect::EmitPrefix::Both;
|
|
|
|
|
2021-10-25 09:36:33 +08:00
|
|
|
// Whether to skip generating prefixed form for argument. This just does some
|
|
|
|
// basic checks.
|
|
|
|
//
|
|
|
|
// There are a little bit more invasive checks possible for cases where not
|
|
|
|
// all ops have the trait that would cause overlap. For many cases here,
|
|
|
|
// renaming would be better (e.g., we can only guard in limited manner against
|
|
|
|
// methods from traits and interfaces here, so avoiding these in op definition
|
|
|
|
// is safer).
|
2021-10-20 22:08:36 +08:00
|
|
|
auto skip = [&](StringRef newName) {
|
2021-10-25 09:36:33 +08:00
|
|
|
bool shouldSkip = newName == "getAttributeNames" ||
|
|
|
|
newName == "getAttributes" || newName == "getOperation" ||
|
|
|
|
newName == "getType";
|
|
|
|
if (newName == "getOperands") {
|
|
|
|
// To reduce noise, skip generating the prefixed form and the warning if
|
|
|
|
// $operands correspond to single variadic argument.
|
|
|
|
if (op.getNumOperands() == 1 && op.getNumVariableLengthOperands() == 1)
|
|
|
|
return true;
|
|
|
|
shouldSkip = true;
|
|
|
|
}
|
2021-10-27 08:35:16 +08:00
|
|
|
if (newName == "getRegions") {
|
|
|
|
if (op.getNumRegions() == 1 && op.getNumVariadicRegions() == 1)
|
|
|
|
return true;
|
|
|
|
shouldSkip = true;
|
|
|
|
}
|
2021-10-20 22:08:36 +08:00
|
|
|
if (!shouldSkip)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This note could be avoided where the final function generated would
|
|
|
|
// have been identical. But preferably in the op definition avoiding using
|
|
|
|
// the generic name and then getting a more specialize type is better.
|
|
|
|
PrintNote(op.getLoc(),
|
|
|
|
"Skipping generation of prefixed accessor `" + newName +
|
|
|
|
"` as it overlaps with default one; generating raw form (`" +
|
|
|
|
name + "`) still");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!prefix.empty()) {
|
|
|
|
names.push_back(
|
|
|
|
prefix + convertToCamelFromSnakeCase(name, /*capitalizeFirst=*/true));
|
|
|
|
// Skip cases which would overlap with default ones for now.
|
|
|
|
if (skip(names.back())) {
|
|
|
|
rawToo = true;
|
|
|
|
names.clear();
|
2021-10-25 09:36:33 +08:00
|
|
|
} else if (rawToo) {
|
2021-10-20 22:08:36 +08:00
|
|
|
LLVM_DEBUG(llvm::errs() << "WITH_GETTER(\"" << op.getQualCppClassName()
|
2021-10-25 09:36:33 +08:00
|
|
|
<< "::" << name << "\")\n"
|
2021-10-20 22:08:36 +08:00
|
|
|
<< "WITH_GETTER(\"" << op.getQualCppClassName()
|
2021-10-25 09:36:33 +08:00
|
|
|
<< "Adaptor::" << name << "\")\n";);
|
2021-10-20 22:08:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prefix.empty() || rawToo)
|
|
|
|
names.push_back(name.str());
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<std::string, 2> Operator::getGetterNames(StringRef name) const {
|
|
|
|
return getGetterOrSetterNames(/*isGetter=*/true, *this, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<std::string, 2> Operator::getSetterNames(StringRef name) const {
|
|
|
|
return getGetterOrSetterNames(/*isGetter=*/false, *this, name);
|
|
|
|
}
|