[ODS] Add support for TypeArrayAttr
authorLei Zhang <antiagainst@google.com>
Mon, 3 Jun 2019 21:52:56 +0000 (14:52 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Tue, 4 Jun 2019 02:27:27 +0000 (19:27 -0700)
PiperOrigin-RevId: 251314203

mlir/include/mlir/IR/OpBase.td
mlir/test/IR/attribute.mlir [new file with mode: 0644]
mlir/test/TestDialect/TestOps.td

index 81577bb..ff3da38 100644 (file)
@@ -618,6 +618,21 @@ class StringBasedAttr<Pred condition, string descr> : Attr<condition, descr> {
 def StrAttr : StringBasedAttr<CPred<"$_self.isa<StringAttr>()">,
                               "string attribute">;
 
+// Base class for attributes containing types. Example:
+//   def IntTypeAttr : TypeAttrBase<"IntegerType", "integer type attribute">
+// defines a type attribute containing an integer type.
+class TypeAttrBase<string retType, string description> :
+    Attr<And<[
+      CPred<"$_self.isa<TypeAttr>()">,
+      CPred<"$_self.cast<TypeAttr>().getValue().isa<" # retType # ">()">]>,
+    description> {
+  let storageType = [{ TypeAttr }];
+  let returnType = retType;
+  let convertFromStorage = "$_self.getValue().cast<" # retType # ">()";
+}
+
+def TypeAttr : TypeAttrBase<"Type", "any type attribute">;
+
 // An enum attribute case.
 class EnumAttrCase<string sym> : StringBasedAttr<
     CPred<"$_self.cast<StringAttr>().getValue() == \"" # sym # "\"">,
@@ -693,6 +708,9 @@ def F64ArrayAttr : TypedArrayAttrBase<F64Attr, "64-bit float array attribute"> {
 def StrArrayAttr : TypedArrayAttrBase<StrAttr, "string array attribute"> {
   let constBuilderCall = "$_builder.getStrArrayAttr($0)";
 }
+def TypeArrayAttr : TypedArrayAttrBase<TypeAttr, "type array attribute"> {
+  let constBuilderCall = ?;
+}
 
 // Attributes containing functions.
 def FunctionAttr : Attr<CPred<"$_self.isa<FunctionAttr>()">,
@@ -702,21 +720,6 @@ def FunctionAttr : Attr<CPred<"$_self.isa<FunctionAttr>()">,
   let constBuilderCall = "$_builder.getFunctionAttr($0)";
 }
 
-// Base class for attributes containing types. Example:
-//   def IntTypeAttr : TypeAttrBase<"IntegerType", "integer type attribute">
-// defines a type attribute containing an integer type.
-class TypeAttrBase<string retType, string description> :
-    Attr<And<[
-      CPred<"$_self.isa<TypeAttr>()">,
-      CPred<"$_self.cast<TypeAttr>().getValue().isa<" # retType # ">()">]>,
-    description> {
-  let storageType = [{ TypeAttr }];
-  let returnType = retType;
-  let convertFromStorage = "$_self.getValue().cast<" # retType # ">()";
-}
-
-def TypeAttr : TypeAttrBase<"Type", "any type attribute">;
-
 // DerivedAttr are attributes whose value is computed from properties
 // of the operation. They do not require additional storage and are
 // materialized as needed.
diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir
new file mode 100644 (file)
index 0000000..ec65cb9
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: mlir-test-opt %s -split-input-file -verify | FileCheck %s
+
+//===----------------------------------------------------------------------===//
+// Test TypeArrayAttr
+//===----------------------------------------------------------------------===//
+
+func @correct_type_array_attr_pass() {
+  // CHECK: test.type_array_attr
+  "test.type_array_attr"() {attr: [i32, f32]} : () -> ()
+  return
+}
+
+// -----
+
+func @non_type_in_type_array_attr_fail() {
+  // expected-error @+1 {{'attr' failed to satisfy constraint: type array attribute}}
+  "test.type_array_attr"() {attr: [i32, 5: i64]} : () -> ()
+  return
+}
index d694644..a721510 100644 (file)
@@ -62,6 +62,14 @@ def NestedTupleOp : TEST_Op<"nested_tuple_32_bit"> {
 
 
 //===----------------------------------------------------------------------===//
+// Test Attributes
+//===----------------------------------------------------------------------===//
+
+def TypeArrayAttrOp : TEST_Op<"type_array_attr"> {
+  let arguments = (ins TypeArrayAttr:$attr);
+}
+
+//===----------------------------------------------------------------------===//
 // Test Traits
 //===----------------------------------------------------------------------===//