diff --git a/mlir/test/mlir-tblgen/op-attribute.td b/mlir/test/mlir-tblgen/op-attribute.td index cb85149d885e..c38d59cc246a 100644 --- a/mlir/test/mlir-tblgen/op-attribute.td +++ b/mlir/test/mlir-tblgen/op-attribute.td @@ -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; diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 8953bcbc54b9..47eef13ad586 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -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() {