[mlir] [clang] [llvm] [clang-tools-extra] [mlir][irdl] Add `irdl.base` op (PR #76400)

2024-01-17 Thread Fehr Mathieu via cfe-commits

https://github.com/math-fehr updated 
https://github.com/llvm/llvm-project/pull/76400

>From 4363403ffcff10844c304426cb92bc559cf0d95c Mon Sep 17 00:00:00 2001
From: Mathieu Fehr 
Date: Sat, 23 Dec 2023 17:11:46 +
Subject: [PATCH 1/2] [mlir][irdl] Add irdl.base operation

---
 mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td  | 41 
 .../include/mlir/Dialect/IRDL/IRDLVerifiers.h | 42 +
 mlir/lib/Dialect/IRDL/IR/IRDL.cpp | 33 ++
 mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp  | 54 
 mlir/lib/Dialect/IRDL/IRDLVerifiers.cpp   | 33 ++
 mlir/test/Dialect/IRDL/invalid.irdl.mlir  | 43 +
 mlir/test/Dialect/IRDL/testd.irdl.mlir| 48 +++---
 mlir/test/Dialect/IRDL/testd.mlir | 63 ---
 8 files changed, 340 insertions(+), 17 deletions(-)
 create mode 100644 mlir/test/Dialect/IRDL/invalid.irdl.mlir

diff --git a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td 
b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
index 681425f8174426a..c63a3a70f6703f6 100644
--- a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
+++ b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
@@ -451,6 +451,47 @@ def IRDL_IsOp : IRDL_ConstraintOp<"is",
   let assemblyFormat = " $expected ` ` attr-dict ";
 }
 
+def IRDL_BaseOp : IRDL_ConstraintOp<"base",
+[ParentOneOf<["TypeOp", "AttributeOp", "OperationOp"]>,
+ DeclareOpInterfaceMethods]> {
+  let summary = "Constraints an attribute/type base";
+  let description = [{
+`irdl.base` defines a constraint that only accepts a single type
+or attribute base, e.g. an `IntegerType`. The attribute base is defined
+either by a symbolic reference to the corresponding IRDL definition,
+or by the name of the base. Named bases are prefixed with `!` or `#`
+respectively for types and attributes.
+
+Example:
+
+```mlir
+irdl.dialect @cmath {
+  irdl.type @complex {
+%0 = irdl.base "!builtin.integer"
+irdl.parameters(%0)
+  }
+
+  irdl.type @complex_wrapper {
+%0 = irdl.base @complex
+irdl.parameters(%0)
+  }
+}
+```
+
+The above program defines a `cmath.complex` type that expects a single
+parameter, which is a type with base name `builtin.integer`, which is the
+name of an `IntegerType` type.
+It also defines a `cmath.complex_wrapper` type that expects a single
+parameter, which is a type of base type `cmath.complex`.
+  }];
+
+  let arguments = (ins OptionalAttr:$base_ref,
+   OptionalAttr:$base_name);
+  let results = (outs IRDL_AttributeType:$output);
+  let assemblyFormat = " ($base_ref^)? ($base_name^)? ` ` attr-dict";
+  let hasVerifier = 1;
+}
+
 def IRDL_ParametricOp : IRDL_ConstraintOp<"parametric",
 [ParentOneOf<["TypeOp", "AttributeOp", "OperationOp"]>, Pure]> {
   let summary = "Constraints an attribute/type base and its parameters";
diff --git a/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h 
b/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
index f8ce77cbc50e9ed..9ecb7c0107d7f8a 100644
--- a/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
+++ b/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
@@ -99,6 +99,48 @@ class IsConstraint : public Constraint {
   Attribute expectedAttribute;
 };
 
+/// A constraint that checks that an attribute is of a given attribute base
+/// (e.g. IntegerAttr).
+class BaseAttrConstraint : public Constraint {
+public:
+  BaseAttrConstraint(TypeID baseTypeID, StringRef baseName)
+  : baseTypeID(baseTypeID), baseName(baseName) {}
+
+  virtual ~BaseAttrConstraint() = default;
+
+  LogicalResult verify(function_ref emitError,
+   Attribute attr,
+   ConstraintVerifier &context) const override;
+
+private:
+  /// The expected base attribute typeID.
+  TypeID baseTypeID;
+
+  /// The base attribute name, only used for error reporting.
+  StringRef baseName;
+};
+
+/// A constraint that checks that a type is of a given type base (e.g.
+/// IntegerType).
+class BaseTypeConstraint : public Constraint {
+public:
+  BaseTypeConstraint(TypeID baseTypeID, StringRef baseName)
+  : baseTypeID(baseTypeID), baseName(baseName) {}
+
+  virtual ~BaseTypeConstraint() = default;
+
+  LogicalResult verify(function_ref emitError,
+   Attribute attr,
+   ConstraintVerifier &context) const override;
+
+private:
+  /// The expected base type typeID.
+  TypeID baseTypeID;
+
+  /// The base type name, only used for error reporting.
+  StringRef baseName;
+};
+
 /// A constraint that checks that an attribute is of a
 /// specific dynamic attribute definition, and that all of its parameters
 /// satisfy the given constraints.
diff --git a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp 
b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
index 33c6bb869a643f3..4eae2b03024c248 100644
--- a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
+++ b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
@@ -117,6 +117,39 @@ LogicalResult Attri

[libcxx] [mlir] [compiler-rt] [clang-tools-extra] [clang] [libc] [lld] [llvm] [flang] [mlir][irdl] Add `irdl.base` op (PR #76400)

2024-01-18 Thread Fehr Mathieu via cfe-commits

https://github.com/math-fehr updated 
https://github.com/llvm/llvm-project/pull/76400

>From 4363403ffcff10844c304426cb92bc559cf0d95c Mon Sep 17 00:00:00 2001
From: Mathieu Fehr 
Date: Sat, 23 Dec 2023 17:11:46 +
Subject: [PATCH 1/2] [mlir][irdl] Add irdl.base operation

---
 mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td  | 41 
 .../include/mlir/Dialect/IRDL/IRDLVerifiers.h | 42 +
 mlir/lib/Dialect/IRDL/IR/IRDL.cpp | 33 ++
 mlir/lib/Dialect/IRDL/IR/IRDLOps.cpp  | 54 
 mlir/lib/Dialect/IRDL/IRDLVerifiers.cpp   | 33 ++
 mlir/test/Dialect/IRDL/invalid.irdl.mlir  | 43 +
 mlir/test/Dialect/IRDL/testd.irdl.mlir| 48 +++---
 mlir/test/Dialect/IRDL/testd.mlir | 63 ---
 8 files changed, 340 insertions(+), 17 deletions(-)
 create mode 100644 mlir/test/Dialect/IRDL/invalid.irdl.mlir

diff --git a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td 
b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
index 681425f8174426a..c63a3a70f6703f6 100644
--- a/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
+++ b/mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
@@ -451,6 +451,47 @@ def IRDL_IsOp : IRDL_ConstraintOp<"is",
   let assemblyFormat = " $expected ` ` attr-dict ";
 }
 
+def IRDL_BaseOp : IRDL_ConstraintOp<"base",
+[ParentOneOf<["TypeOp", "AttributeOp", "OperationOp"]>,
+ DeclareOpInterfaceMethods]> {
+  let summary = "Constraints an attribute/type base";
+  let description = [{
+`irdl.base` defines a constraint that only accepts a single type
+or attribute base, e.g. an `IntegerType`. The attribute base is defined
+either by a symbolic reference to the corresponding IRDL definition,
+or by the name of the base. Named bases are prefixed with `!` or `#`
+respectively for types and attributes.
+
+Example:
+
+```mlir
+irdl.dialect @cmath {
+  irdl.type @complex {
+%0 = irdl.base "!builtin.integer"
+irdl.parameters(%0)
+  }
+
+  irdl.type @complex_wrapper {
+%0 = irdl.base @complex
+irdl.parameters(%0)
+  }
+}
+```
+
+The above program defines a `cmath.complex` type that expects a single
+parameter, which is a type with base name `builtin.integer`, which is the
+name of an `IntegerType` type.
+It also defines a `cmath.complex_wrapper` type that expects a single
+parameter, which is a type of base type `cmath.complex`.
+  }];
+
+  let arguments = (ins OptionalAttr:$base_ref,
+   OptionalAttr:$base_name);
+  let results = (outs IRDL_AttributeType:$output);
+  let assemblyFormat = " ($base_ref^)? ($base_name^)? ` ` attr-dict";
+  let hasVerifier = 1;
+}
+
 def IRDL_ParametricOp : IRDL_ConstraintOp<"parametric",
 [ParentOneOf<["TypeOp", "AttributeOp", "OperationOp"]>, Pure]> {
   let summary = "Constraints an attribute/type base and its parameters";
diff --git a/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h 
b/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
index f8ce77cbc50e9ed..9ecb7c0107d7f8a 100644
--- a/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
+++ b/mlir/include/mlir/Dialect/IRDL/IRDLVerifiers.h
@@ -99,6 +99,48 @@ class IsConstraint : public Constraint {
   Attribute expectedAttribute;
 };
 
+/// A constraint that checks that an attribute is of a given attribute base
+/// (e.g. IntegerAttr).
+class BaseAttrConstraint : public Constraint {
+public:
+  BaseAttrConstraint(TypeID baseTypeID, StringRef baseName)
+  : baseTypeID(baseTypeID), baseName(baseName) {}
+
+  virtual ~BaseAttrConstraint() = default;
+
+  LogicalResult verify(function_ref emitError,
+   Attribute attr,
+   ConstraintVerifier &context) const override;
+
+private:
+  /// The expected base attribute typeID.
+  TypeID baseTypeID;
+
+  /// The base attribute name, only used for error reporting.
+  StringRef baseName;
+};
+
+/// A constraint that checks that a type is of a given type base (e.g.
+/// IntegerType).
+class BaseTypeConstraint : public Constraint {
+public:
+  BaseTypeConstraint(TypeID baseTypeID, StringRef baseName)
+  : baseTypeID(baseTypeID), baseName(baseName) {}
+
+  virtual ~BaseTypeConstraint() = default;
+
+  LogicalResult verify(function_ref emitError,
+   Attribute attr,
+   ConstraintVerifier &context) const override;
+
+private:
+  /// The expected base type typeID.
+  TypeID baseTypeID;
+
+  /// The base type name, only used for error reporting.
+  StringRef baseName;
+};
+
 /// A constraint that checks that an attribute is of a
 /// specific dynamic attribute definition, and that all of its parameters
 /// satisfy the given constraints.
diff --git a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp 
b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
index 33c6bb869a643f3..4eae2b03024c248 100644
--- a/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
+++ b/mlir/lib/Dialect/IRDL/IR/IRDL.cpp
@@ -117,6 +117,39 @@ LogicalResult Attri

[clang-tools-extra] [mlir] [libc] [llvm] [clang] [libcxx] [flang] [lld] [compiler-rt] [mlir][irdl] Add `irdl.base` op (PR #76400)

2024-01-18 Thread Fehr Mathieu via cfe-commits

https://github.com/math-fehr closed 
https://github.com/llvm/llvm-project/pull/76400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits