Skip to content

Commit 0f01954

Browse files
address comments
1 parent 79eb577 commit 0f01954

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

mlir/docs/DefiningDialects/AttributesAndTypes.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,3 +1204,52 @@ void MyDialect::initialize() {
12041204
>();
12051205
}
12061206
```
1207+
1208+
### Attribute / Type Constraints
1209+
1210+
When defining the arguments of an operation in TableGen, users can specify
1211+
either plain attributes/types or use attribute/type constraints to levy
1212+
additional requirements on the attribute value or operand type.
1213+
1214+
```tablegen
1215+
def My_Type1 : MyDialect_Type<"Type1", "type1"> { ... }
1216+
def My_Type2 : MyDialect_Type<"Type2", "type2"> { ... }
1217+
1218+
// Plain type
1219+
let arguments = (ins MyType1:$val);
1220+
// Type constraint
1221+
let arguments = (ins AnyTypeOf<[MyType1, MyType2]>:$val);
1222+
```
1223+
1224+
`AnyTypeOf` is an example for a type constraints. Many useful type constraints
1225+
can be found in `mlir/IR/CommonTypeConstraints.td`. Additional verification
1226+
code is generated for type/attribute constraints. Type constraints can not only
1227+
be used when defining operation arguments, but also when defining type
1228+
parameters.
1229+
1230+
Optionally, C++ functions can be generated, so that type constraints can be
1231+
checked from C++. The name of the C++ function must be specified in the
1232+
`cppFunctionName` field. If no function name is specified, no C++ function is
1233+
emitted.
1234+
1235+
```tablegen
1236+
// Example: Element type constraint for VectorType
1237+
def Builtin_VectorTypeElementType : AnyTypeOf<[AnyInteger, Index, AnyFloat]> {
1238+
let cppFunctionName = "isValidVectorTypeElementType";
1239+
}
1240+
```
1241+
1242+
An extra TableGen rule is needed to emit C++ code for type constraints. This
1243+
will generate only the declarations/definitions of the type constaraints that
1244+
are defined in the specified `.td` file, but not those that are in included
1245+
`.td` files.
1246+
1247+
```cmake
1248+
mlir_tablegen(<Your Dialect>TypeConstraints.h.inc -gen-type-constraint-decls)
1249+
mlir_tablegen(<Your Dialect>TypeConstraints.cpp.inc -gen-type-constraint-defs)
1250+
```
1251+
1252+
The generated `<Your Dialect>TypeConstraints.h.inc` will need to be included
1253+
whereever you are referencing the type constraint in C++. Note that no C++
1254+
namespace will be emitted by the code generator. The `#include` statements of
1255+
the `.h.inc`/`.cpp.inc` files should be wrapped in C++ namespaces by the user.

mlir/lib/IR/BuiltinTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ LogicalResult OpaqueType::verify(function_ref<InFlightDiagnostic()> emitError,
235235
//===----------------------------------------------------------------------===//
236236

237237
bool VectorType::isValidElementType(Type t) {
238-
return succeeded(isValidVectorTypeElementType(t));
238+
return isValidVectorTypeElementType(t);
239239
}
240240

241241
LogicalResult VectorType::verify(function_ref<InFlightDiagnostic()> emitError,

mlir/test/mlir-tblgen/type-constraints.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def DummyConstraint : AnyTypeOf<[AnyInteger, Index, AnyFloat]> {
77
let cppFunctionName = "isValidDummy";
88
}
99

10-
// DECL: ::llvm::LogicalResult isValidDummy(::mlir::Type type);
10+
// DECL: bool isValidDummy(::mlir::Type type);
1111

12-
// DEF: ::llvm::LogicalResult isValidDummy(::mlir::Type type) {
13-
// DEF: return ::llvm::success((((::llvm::isa<::mlir::IntegerType>(type))) || ((::llvm::isa<::mlir::IndexType>(type))) || ((::llvm::isa<::mlir::FloatType>(type)))));
12+
// DEF: bool isValidDummy(::mlir::Type type) {
13+
// DEF: return (((::llvm::isa<::mlir::IntegerType>(type))) || ((::llvm::isa<::mlir::IndexType>(type))) || ((::llvm::isa<::mlir::FloatType>(type))));
1414
// DEF: }

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,12 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
10281028
//===----------------------------------------------------------------------===//
10291029

10301030
static const char *const typeConstraintDecl = R"(
1031-
::llvm::LogicalResult {0}(::mlir::Type type);
1031+
bool {0}(::mlir::Type type);
10321032
)";
10331033

10341034
static const char *const typeConstraintDef = R"(
1035-
::llvm::LogicalResult {0}(::mlir::Type type) {
1036-
return ::llvm::success(({1}));
1035+
bool {0}(::mlir::Type type) {
1036+
return ({1});
10371037
}
10381038
)";
10391039

0 commit comments

Comments
 (0)