[Mlir-commits] [mlir] b2bb8b6 - [mlir] Automatically add DerivedAttribute op interface
Jacques Pienaar
llvmlistbot at llvm.org
Fri Mar 13 14:26:46 PDT 2020
Author: Jacques Pienaar
Date: 2020-03-13T14:26:30-07:00
New Revision: b2bb8b6cd6402d5495c0b20b27697df34de6c1c8
URL: https://github.com/llvm/llvm-project/commit/b2bb8b6cd6402d5495c0b20b27697df34de6c1c8
DIFF: https://github.com/llvm/llvm-project/commit/b2bb8b6cd6402d5495c0b20b27697df34de6c1c8.diff
LOG: [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
Added:
Modified:
mlir/test/mlir-tblgen/op-attribute.td
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Removed:
################################################################################
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() {
More information about the Mlir-commits
mailing list