diff --git a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td --- a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td +++ b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td @@ -426,4 +426,39 @@ let assemblyFormat = [{ `(` $args `)` ` ` attr-dict }]; } +def IRDL_Cpp : IRDL_Op<"c_pred"> { + let summary = "Constraints an attribute using a C++ predicate"; + let description = [{ + `irdl.inline_cpp` defines a constraint that is written in C++. + + Dialects using this operation cannot be registered at runtime, as it relies + on C++ code. + + Special placeholders can be used to refer to entities in the context where + this predicate is used. They serve as "hooks" to the enclosing environment. + The following special placeholders are supported in constraints for an op: + + * `$_builder` will be replaced by a mlir::Builder instance. + * `$_op` will be replaced by the current operation. + * `$_self` will be replaced with the entity this predicate is attached to. + Compared to ODS, `$_self` is always of type `mlir::Attribute`, and types + are manipulated as `TypeAttr` attributes. + + Example: + ```mlir + irdl.type @op_with_attr { + %0 = irdl.c_pred "::llvm::isa<::mlir::IntegerAttr>($_self)" + irdl.parameters(%0) + } + ``` + + In this example, @op_with_attr is defined as a type with a single + parameter, which is an `IntegerAttr`, as constrained by the C++ predicate. + }]; + + let arguments = (ins StrAttr:$pred); + let results = (outs IRDL_AttributeType:$output); + let assemblyFormat = [{ $pred ` ` attr-dict }]; +} + #endif // MLIR_DIALECT_IRDL_IR_IRDLOPS diff --git a/mlir/test/Dialect/IRDL/cpred.irdl.mlir b/mlir/test/Dialect/IRDL/cpred.irdl.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/IRDL/cpred.irdl.mlir @@ -0,0 +1,14 @@ +// RUN: mlir-opt %s | mlir-opt | FileCheck %s + +module { + // CHECK-LABEL: irdl.dialect @dialect { + irdl.dialect @dialect { + // CHECK-LABEL: irdl.type @type { + irdl.type @type { + %0 = irdl.c_pred "::llvm::isa<::mlir::IntegerAttr>($_self)" + // CHECK: %{{.*}} = irdl.c_pred "::llvm::isa<::mlir::IntegerAttr>($_self)" + irdl.parameters(%0) + // CHECK: irdl.parameters(%{{.*}}) + } + } +}