xref: /llvm-project/mlir/lib/Bindings/Python/DialectPDL.cpp (revision 5cd427477218d8bdb659c6c53a7758f741c3990a)
1 //===- DialectPDL.cpp - 'pdl' dialect submodule ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir-c/Dialect/PDL.h"
10 #include "mlir-c/IR.h"
11 #include "mlir/Bindings/Python/NanobindAdaptors.h"
12 #include "mlir/Bindings/Python/Nanobind.h"
13 
14 namespace nb = nanobind;
15 using namespace llvm;
16 using namespace mlir;
17 using namespace mlir::python;
18 using namespace mlir::python::nanobind_adaptors;
19 
20 void populateDialectPDLSubmodule(const nanobind::module_ &m) {
21   //===-------------------------------------------------------------------===//
22   // PDLType
23   //===-------------------------------------------------------------------===//
24 
25   auto pdlType = mlir_type_subclass(m, "PDLType", mlirTypeIsAPDLType);
26 
27   //===-------------------------------------------------------------------===//
28   // AttributeType
29   //===-------------------------------------------------------------------===//
30 
31   auto attributeType =
32       mlir_type_subclass(m, "AttributeType", mlirTypeIsAPDLAttributeType);
33   attributeType.def_classmethod(
34       "get",
35       [](nb::object cls, MlirContext ctx) {
36         return cls(mlirPDLAttributeTypeGet(ctx));
37       },
38       "Get an instance of AttributeType in given context.", nb::arg("cls"),
39       nb::arg("context").none() = nb::none());
40 
41   //===-------------------------------------------------------------------===//
42   // OperationType
43   //===-------------------------------------------------------------------===//
44 
45   auto operationType =
46       mlir_type_subclass(m, "OperationType", mlirTypeIsAPDLOperationType);
47   operationType.def_classmethod(
48       "get",
49       [](nb::object cls, MlirContext ctx) {
50         return cls(mlirPDLOperationTypeGet(ctx));
51       },
52       "Get an instance of OperationType in given context.", nb::arg("cls"),
53       nb::arg("context").none() = nb::none());
54 
55   //===-------------------------------------------------------------------===//
56   // RangeType
57   //===-------------------------------------------------------------------===//
58 
59   auto rangeType = mlir_type_subclass(m, "RangeType", mlirTypeIsAPDLRangeType);
60   rangeType.def_classmethod(
61       "get",
62       [](nb::object cls, MlirType elementType) {
63         return cls(mlirPDLRangeTypeGet(elementType));
64       },
65       "Gets an instance of RangeType in the same context as the provided "
66       "element type.",
67       nb::arg("cls"), nb::arg("element_type"));
68   rangeType.def_property_readonly(
69       "element_type",
70       [](MlirType type) { return mlirPDLRangeTypeGetElementType(type); },
71       "Get the element type.");
72 
73   //===-------------------------------------------------------------------===//
74   // TypeType
75   //===-------------------------------------------------------------------===//
76 
77   auto typeType = mlir_type_subclass(m, "TypeType", mlirTypeIsAPDLTypeType);
78   typeType.def_classmethod(
79       "get",
80       [](nb::object cls, MlirContext ctx) {
81         return cls(mlirPDLTypeTypeGet(ctx));
82       },
83       "Get an instance of TypeType in given context.", nb::arg("cls"),
84       nb::arg("context").none() = nb::none());
85 
86   //===-------------------------------------------------------------------===//
87   // ValueType
88   //===-------------------------------------------------------------------===//
89 
90   auto valueType = mlir_type_subclass(m, "ValueType", mlirTypeIsAPDLValueType);
91   valueType.def_classmethod(
92       "get",
93       [](nb::object cls, MlirContext ctx) {
94         return cls(mlirPDLValueTypeGet(ctx));
95       },
96       "Get an instance of TypeType in given context.", nb::arg("cls"),
97       nb::arg("context").none() = nb::none());
98 }
99 
100 NB_MODULE(_mlirDialectsPDL, m) {
101   m.doc() = "MLIR PDL dialect.";
102   populateDialectPDLSubmodule(m);
103 }
104