[mlir] Automatically add DerivedAttribute op interface

Summary: When an operation has derived attributes, add the DerivedAttribute op interface.

Differential Revision: https://reviews.llvm.org/D76144
This commit is contained in:
Jacques Pienaar 2020-03-13 10:09:02 -07:00
parent e890453d6d
commit b2bb8b6cd6
2 changed files with 12 additions and 7 deletions

View File

@ -208,7 +208,9 @@ def DerivedTypeAttrOp : NS_Op<"derived_type_attr_op", []> {
DerivedTypeAttr element_dtype = DerivedTypeAttr<"return output().getType();">;
}
// DECL: bool isDerivedAttribute
// DECL: class DerivedTypeAttrOp : public Op
// DECL-SAME: DerivedAttributeOpInterface::Trait
// DECL: static bool isDerivedAttribute
// DEF: bool DerivedTypeAttrOp::isDerivedAttribute(StringRef name) {
// DEF: if (name == "element_dtype") return true;
// DEF: return false;

View File

@ -401,16 +401,19 @@ void OpEmitter::genAttrGetters() {
// Generate helper method to query whether a named attribute is a derived
// attribute. This enables, for example, avoiding adding an attribute that
// overlaps with a derived attribute.
auto &method =
opClass.newMethod("bool", "isDerivedAttribute", "StringRef name");
auto &body = method.body();
auto derivedAttr = make_filter_range(op.getAttributes(),
[](const NamedAttribute &namedAttr) {
return namedAttr.attr.isDerivedAttr();
});
for (auto namedAttr : derivedAttr)
body << " if (name == \"" << namedAttr.name << "\") return true;\n";
body << " return false;";
if (!derivedAttr.empty()) {
opClass.addTrait("DerivedAttributeOpInterface::Trait");
auto &method = opClass.newMethod("bool", "isDerivedAttribute",
"StringRef name", OpMethod::MP_Static);
auto &body = method.body();
for (auto namedAttr : derivedAttr)
body << " if (name == \"" << namedAttr.name << "\") return true;\n";
body << " return false;";
}
}
void OpEmitter::genAttrSetters() {