[PATCH] D76144: [mlir] Automatically add DerivedAttribute op interface

Jacques Pienaar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 13 10:12:02 PDT 2020


jpienaar created this revision.
jpienaar added a reviewer: mehdi_amini.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, rriddle.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76144

Files:
  mlir/test/mlir-tblgen/op-attribute.td
  mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp


Index: mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
===================================================================
--- mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -401,16 +401,19 @@
   // 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() {
Index: mlir/test/mlir-tblgen/op-attribute.td
===================================================================
--- mlir/test/mlir-tblgen/op-attribute.td
+++ mlir/test/mlir-tblgen/op-attribute.td
@@ -208,7 +208,9 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76144.250250.patch
Type: text/x-patch
Size: 2053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200313/60c6ae69/attachment.bin>


More information about the llvm-commits mailing list