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/PybindAdaptors.h"
12#include <pybind11/cast.h>
13#include <pybind11/detail/common.h>
14#include <pybind11/pybind11.h>
15#include <pybind11/pytypes.h>
16
17namespace py = pybind11;
18using namespace llvm;
19using namespace mlir;
20using namespace mlir::python;
21using namespace mlir::python::adaptors;
22
23void populateDialectPDLSubmodule(const pybind11::module &m) {
24 //===-------------------------------------------------------------------===//
25 // PDLType
26 //===-------------------------------------------------------------------===//
27
28 auto pdlType = mlir_type_subclass(m, "PDLType", mlirTypeIsAPDLType);
29
30 //===-------------------------------------------------------------------===//
31 // AttributeType
32 //===-------------------------------------------------------------------===//
33
34 auto attributeType =
35 mlir_type_subclass(m, "AttributeType", mlirTypeIsAPDLAttributeType);
36 attributeType.def_classmethod(
37 "get",
38 [](py::object cls, MlirContext ctx) {
39 return cls(mlirPDLAttributeTypeGet(ctx));
40 },
41 "Get an instance of AttributeType in given context.", py::arg("cls"),
42 py::arg("context") = py::none());
43
44 //===-------------------------------------------------------------------===//
45 // OperationType
46 //===-------------------------------------------------------------------===//
47
48 auto operationType =
49 mlir_type_subclass(m, "OperationType", mlirTypeIsAPDLOperationType);
50 operationType.def_classmethod(
51 "get",
52 [](py::object cls, MlirContext ctx) {
53 return cls(mlirPDLOperationTypeGet(ctx));
54 },
55 "Get an instance of OperationType in given context.", py::arg("cls"),
56 py::arg("context") = py::none());
57
58 //===-------------------------------------------------------------------===//
59 // RangeType
60 //===-------------------------------------------------------------------===//
61
62 auto rangeType = mlir_type_subclass(m, "RangeType", mlirTypeIsAPDLRangeType);
63 rangeType.def_classmethod(
64 "get",
65 [](py::object cls, MlirType elementType) {
66 return cls(mlirPDLRangeTypeGet(elementType));
67 },
68 "Gets an instance of RangeType in the same context as the provided "
69 "element type.",
70 py::arg("cls"), py::arg("element_type"));
71 rangeType.def_property_readonly(
72 "element_type",
73 [](MlirType type) { return mlirPDLRangeTypeGetElementType(type); },
74 "Get the element type.");
75
76 //===-------------------------------------------------------------------===//
77 // TypeType
78 //===-------------------------------------------------------------------===//
79
80 auto typeType = mlir_type_subclass(m, "TypeType", mlirTypeIsAPDLTypeType);
81 typeType.def_classmethod(
82 "get",
83 [](py::object cls, MlirContext ctx) {
84 return cls(mlirPDLTypeTypeGet(ctx));
85 },
86 "Get an instance of TypeType in given context.", py::arg("cls"),
87 py::arg("context") = py::none());
88
89 //===-------------------------------------------------------------------===//
90 // ValueType
91 //===-------------------------------------------------------------------===//
92
93 auto valueType = mlir_type_subclass(m, "ValueType", mlirTypeIsAPDLValueType);
94 valueType.def_classmethod(
95 "get",
96 [](py::object cls, MlirContext ctx) {
97 return cls(mlirPDLValueTypeGet(ctx));
98 },
99 "Get an instance of TypeType in given context.", py::arg("cls"),
100 py::arg("context") = py::none());
101}
102
103PYBIND11_MODULE(_mlirDialectsPDL, m) {
104 m.doc() = "MLIR PDL dialect.";
105 populateDialectPDLSubmodule(m);
106}
107

source code of mlir/lib/Bindings/Python/DialectPDL.cpp