5 ## Attribute / Type Constraints
7 When defining the arguments of an operation in TableGen, users can specify
8 either plain attributes/types or use attribute/type constraints to levy
9 additional requirements on the attribute value or operand type.
12 def My_Type1 : MyDialect_Type<"Type1", "type1"> { ... }
13 def My_Type2 : MyDialect_Type<"Type2", "type2"> { ... }
16 let arguments = (ins MyType1:$val);
18 let arguments = (ins AnyTypeOf<[MyType1, MyType2]>:$val);
21 `AnyTypeOf` is an example for a type constraints. Many useful type constraints
22 can be found in `mlir/IR/CommonTypeConstraints.td`. Additional verification
23 code is generated for type/attribute constraints. Type constraints can not only
24 be used when defining operation arguments, but also when defining type
27 Optionally, C++ functions can be generated, so that type constraints can be
28 checked from C++. The name of the C++ function must be specified in the
29 `cppFunctionName` field. If no function name is specified, no C++ function is
33 // Example: Element type constraint for VectorType
34 def Builtin_VectorTypeElementType : AnyTypeOf<[AnyInteger, Index, AnyFloat]> {
35 let cppFunctionName = "isValidVectorTypeElementType";
39 The above example tranlates into the following C++ code:
41 bool isValidVectorTypeElementType(::mlir::Type type) {
42 return (((::llvm::isa<::mlir::IntegerType>(type))) || ((::llvm::isa<::mlir::IndexType>(type))) || ((::llvm::isa<::mlir::FloatType>(type))));
46 An extra TableGen rule is needed to emit C++ code for type constraints. This
47 will generate only the declarations/definitions of the type constaraints that
48 are defined in the specified `.td` file, but not those that are in included
52 mlir_tablegen(<Your Dialect>TypeConstraints.h.inc -gen-type-constraint-decls)
53 mlir_tablegen(<Your Dialect>TypeConstraints.cpp.inc -gen-type-constraint-defs)
56 The generated `<Your Dialect>TypeConstraints.h.inc` will need to be included
57 whereever you are referencing the type constraint in C++. Note that no C++
58 namespace will be emitted by the code generator. The `#include` statements of
59 the `.h.inc`/`.cpp.inc` files should be wrapped in C++ namespaces by the user.