[Mlir-commits] [mlir] [mlir][ODS] Optionally generate public C++ functions for type constraints (PR #104577)
River Riddle
llvmlistbot at llvm.org
Tue Aug 20 11:17:34 PDT 2024
================
@@ -1204,3 +1204,52 @@ void MyDialect::initialize() {
>();
}
```
+
+### Attribute / Type Constraints
+
+When defining the arguments of an operation in TableGen, users can specify
+either plain attributes/types or use attribute/type constraints to levy
+additional requirements on the attribute value or operand type.
+
+```tablegen
+def My_Type1 : MyDialect_Type<"Type1", "type1"> { ... }
+def My_Type2 : MyDialect_Type<"Type2", "type2"> { ... }
+
+// Plain type
+let arguments = (ins MyType1:$val);
+// Type constraint
+let arguments = (ins AnyTypeOf<[MyType1, MyType2]>:$val);
+```
+
+`AnyTypeOf` is an example for a type constraints. Many useful type constraints
+can be found in `mlir/IR/CommonTypeConstraints.td`. Additional verification
+code is generated for type/attribute constraints. Type constraints can not only
+be used when defining operation arguments, but also when defining type
+parameters.
+
+Optionally, C++ functions can be generated, so that type constraints can be
+checked from C++. The name of the C++ function must be specified in the
+`cppFunctionName` field. If no function name is specified, no C++ function is
+emitted.
+
+```tablegen
+// Example: Element type constraint for VectorType
+def Builtin_VectorTypeElementType : AnyTypeOf<[AnyInteger, Index, AnyFloat]> {
+ let cppFunctionName = "isValidVectorTypeElementType";
+}
+```
----------------
River707 wrote:
Can you given an example of the C++ that is generated by this?
https://github.com/llvm/llvm-project/pull/104577
More information about the Mlir-commits
mailing list