[PATCH] D75411: [mlir][ods] Add query for derived attribute
Jacques Pienaar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 29 12:22:08 PST 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 reviewer: rriddle.
Herald added a project: LLVM.
For ODS generated operations, enable querying whether there is a derived
attribute with a given name.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D75411
Files:
mlir/include/mlir/IR/OperationSupport.h
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
@@ -391,6 +391,18 @@
emitAttrWithReturnType(name, attr);
}
}
+
+ // 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();
+ body << " return llvm::StringSwitch<bool>(name)";
+ for (auto namedAttr : op.getAttributes())
+ if (namedAttr.attr.isDerivedAttr())
+ body << "\n .Case(\"" << namedAttr.name << "\", true)";
+ body << "\n .Default(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
@@ -201,6 +201,19 @@
// DEF: odsState.addAttribute("str_attr", (*odsBuilder).getStringAttr(str_attr));
// DEF: odsState.addAttribute("dv_str_attr", (*odsBuilder).getStringAttr(dv_str_attr));
+// Test derived type attr.
+// ---
+def DerivedTypeAttrOp : NS_Op<"derived_type_attr_op", []> {
+ let results = (outs AnyTensor:$output);
+ DerivedTypeAttr element_dtype = DerivedTypeAttr<"return output().getType();">;
+}
+
+// DECL: bool isDerivedAttribute
+// DEF: bool DerivedTypeAttrOp::isDerivedAttribute(StringRef name) {
+// DEF: return llvm::StringSwitch<bool>(name)
+// DEF: .Case("element_dtype", true)
+// DEF: .Default(false);
+// DEF: }
// Test that only default valued attributes at the end of the arguments
// list get default values in the builder signature
Index: mlir/include/mlir/IR/OperationSupport.h
===================================================================
--- mlir/include/mlir/IR/OperationSupport.h
+++ mlir/include/mlir/IR/OperationSupport.h
@@ -21,6 +21,7 @@
#include "mlir/IR/Value.h"
#include "mlir/Support/LogicalResult.h"
#include "llvm/ADT/PointerUnion.h"
+#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/TrailingObjects.h"
#include <memory>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75411.247458.patch
Type: text/x-patch
Size: 2298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200229/0187d2d3/attachment.bin>
More information about the llvm-commits
mailing list