[mlir][irdl] Add `irdl.c_pred`
authorMathieu Fehr <mathieu.fehr@gmail.com>
Tue, 6 Jun 2023 19:07:01 +0000 (20:07 +0100)
committerMathieu Fehr <mathieu.fehr@gmail.com>
Thu, 8 Jun 2023 10:40:48 +0000 (11:40 +0100)
`irdl.c_pred` is an attribute constraint defined by a C++ predicate.
Contrary to the other constraints, this operation cannot be used in
dialects that are registered at runtime. Its principal use is to
share dialect definitions that are defined in C++ or ODS.

Reviewed By: Mogball

Differential Revision: https://reviews.llvm.org/D152301

mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
mlir/test/Dialect/IRDL/cpred.irdl.mlir [new file with mode: 0644]

index f5b4600062ea7e01ca4fcaa9310f6f0ee788828d..f451db321584672bf55ab0e53506a4ccafab5a8f 100644 (file)
@@ -426,4 +426,39 @@ def IRDL_AllOf : IRDL_ConstraintOp<"all_of",
   let assemblyFormat = [{ `(` $args `)` ` ` attr-dict }];
 }
 
+def IRDL_CPredOp : IRDL_Op<"c_pred"> {
+  let summary = "Constraints an attribute using a C++ predicate";
+  let description = [{
+    `irdl.c_pred` 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 (file)
index 0000000..129793e
--- /dev/null
@@ -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(%{{.*}})
+    }
+  }
+}