diff --git a/mlir/include/mlir/IR/CommonAttrConstraints.td b/mlir/include/mlir/IR/CommonAttrConstraints.td index 17ca82c510f8a..599f5ecba5803 100644 --- a/mlir/include/mlir/IR/CommonAttrConstraints.td +++ b/mlir/include/mlir/IR/CommonAttrConstraints.td @@ -334,17 +334,19 @@ class StringBasedAttr : Attr { let valueType = NoneType; } -def StrAttr : StringBasedAttr($_self)">, - "string attribute">; +def StrAttrPred : CPred<"::llvm::isa<::mlir::StringAttr>($_self)">; + +def StrAttr : StringBasedAttr; // A string attribute that represents the name of a symbol. -def SymbolNameAttr : StringBasedAttr($_self)">, - "string attribute">; +def SymbolNameAttr : StringBasedAttr; // String attribute that has a specific value type. class TypedStrAttr - : StringBasedAttr($_self)">, - "string attribute"> { + : StringBasedAttr($_self).getType()", + ty.predicate>]>, + "string attribute of " # ty.summary> { let valueType = ty; } diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir index 0085d64ae82b6..5a005a393d8ac 100644 --- a/mlir/test/IR/attribute.mlir +++ b/mlir/test/IR/attribute.mlir @@ -416,10 +416,29 @@ func.func @non_type_in_type_array_attr_fail() { // Test StringAttr with custom type //===----------------------------------------------------------------------===// -// CHECK-LABEL: func @string_attr_custom_type -func.func @string_attr_custom_type() { - // CHECK: "string_data" : !foo.string - test.string_attr_with_type "string_data" : !foo.string +// CHECK-LABEL: func @string_attr_custom_type_valid +func.func @string_attr_custom_type_valid() { + // CHECK: "string_data" : i64 + test.string_attr_with_type "string_data" : i64 + return +} + +// ----- + +func.func @string_attr_custom_type_invalid() { + // expected-error @+1 {{'attr' failed to satisfy constraint: string attribute of integer}} + test.string_attr_with_type "string_data" : f32 + return +} + +// ----- + +// CHECK-LABEL: func @string_attr_custom_mixed_type +func.func @string_attr_custom_mixed_type() { + // CHECK: "string_data" : i64 + test.string_attr_with_mixed_type "string_data" : i64 + // CHECK: 42 : i64 + test.string_attr_with_mixed_type 42 : i64 return } diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td index f37573c1351ce..79840094686e1 100644 --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -193,8 +193,15 @@ def TypeArrayAttrWithDefaultOp : TEST_Op<"type_array_attr_with_default"> { let arguments = (ins DefaultValuedAttr:$attr); } -def TypeStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> { - let arguments = (ins TypedStrAttr:$attr); +def TypedStringAttrWithTypeOp : TEST_Op<"string_attr_with_type"> { + let arguments = (ins TypedStrAttr:$attr); + let assemblyFormat = "$attr attr-dict"; +} + +def TypedStringAttrWithMixedTypeOp : TEST_Op<"string_attr_with_mixed_type"> { + let arguments = (ins + AnyAttrOf<[TypedStrAttr, I64Attr]>:$attr + ); let assemblyFormat = "$attr attr-dict"; }