diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -934,6 +934,19 @@ let constBuilderCall = "$0"; } +// Any attribute from the given list +class AnyAttrOf allowedAttrs, string summary = "", + string cppClassName = "::mlir::Attribute", + string fromStorage = "$_self"> : Attr< + // Satisfy any of the allowed attribute's condition + Or, + !if(!eq(summary, ""), + !interleave(!foreach(t, allowedAttrs, t.summary), " or "), + summary)> { + let returnType = cppClassName; + let convertFromStorage = fromStorage; +} + def BoolAttr : Attr()">, "bool attribute"> { let storageType = [{ ::mlir::BoolAttr }]; let returnType = [{ bool }]; diff --git a/mlir/test/IR/attribute.mlir b/mlir/test/IR/attribute.mlir --- a/mlir/test/IR/attribute.mlir +++ b/mlir/test/IR/attribute.mlir @@ -1,5 +1,36 @@ // RUN: mlir-opt %s -split-input-file -allow-unregistered-dialect -verify-diagnostics | FileCheck %s +//===----------------------------------------------------------------------===// +// Test AnyAttrOf attributes +//===----------------------------------------------------------------------===// + +func @any_attr_of_pass() { + "test.any_attr_of_i32_str"() { + // CHECK: attr = 3 : i32 + attr = 3 : i32 + } : () -> () + + "test.any_attr_of_i32_str"() { + // CHECK: attr = "string_data" + attr = "string_data" + } : () -> () + + return +} + +// ----- + +func @any_attr_of_fail() { + // expected-error @+1 {{'test.any_attr_of_i32_str' op attribute 'attr' failed to satisfy constraint: 32-bit signless integer attribute or string attribute}} + "test.any_attr_of_i32_str"() { + attr = 3 : i64 + } : () -> () + + return +} + +// ----- + //===----------------------------------------------------------------------===// // Test integer attributes //===----------------------------------------------------------------------===// diff --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td --- a/mlir/test/lib/Dialect/Test/TestOps.td +++ b/mlir/test/lib/Dialect/Test/TestOps.td @@ -187,6 +187,10 @@ // Test Attributes //===----------------------------------------------------------------------===// +def AnyAttrOfOp : TEST_Op<"any_attr_of_i32_str"> { + let arguments = (ins AnyAttrOf<[I32Attr, StrAttr]>:$attr); +} + def NonNegIntAttrOp : TEST_Op<"non_negative_int_attr"> { let arguments = (ins Confined:$i32attr,