aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/DialectPDL.cpp
blob: bcc6ff406c9529a32160b87f3932cdd9a37a952b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//===- DialectPDL.cpp - 'pdl' dialect submodule ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir-c/Dialect/PDL.h"
#include "mlir-c/IR.h"
#include "mlir/Bindings/Python/NanobindAdaptors.h"
#include "mlir/Bindings/Python/Nanobind.h"

namespace nb = nanobind;
using namespace llvm;
using namespace mlir;
using namespace mlir::python;
using namespace mlir::python::nanobind_adaptors;

void populateDialectPDLSubmodule(const nanobind::module_ &m) {
  //===-------------------------------------------------------------------===//
  // PDLType
  //===-------------------------------------------------------------------===//

  auto pdlType = mlir_type_subclass(m, "PDLType", mlirTypeIsAPDLType);

  //===-------------------------------------------------------------------===//
  // AttributeType
  //===-------------------------------------------------------------------===//

  auto attributeType =
      mlir_type_subclass(m, "AttributeType", mlirTypeIsAPDLAttributeType);
  attributeType.def_classmethod(
      "get",
      [](nb::object cls, MlirContext ctx) {
        return cls(mlirPDLAttributeTypeGet(ctx));
      },
      "Get an instance of AttributeType in given context.", nb::arg("cls"),
      nb::arg("context").none() = nb::none());

  //===-------------------------------------------------------------------===//
  // OperationType
  //===-------------------------------------------------------------------===//

  auto operationType =
      mlir_type_subclass(m, "OperationType", mlirTypeIsAPDLOperationType);
  operationType.def_classmethod(
      "get",
      [](nb::object cls, MlirContext ctx) {
        return cls(mlirPDLOperationTypeGet(ctx));
      },
      "Get an instance of OperationType in given context.", nb::arg("cls"),
      nb::arg("context").none() = nb::none());

  //===-------------------------------------------------------------------===//
  // RangeType
  //===-------------------------------------------------------------------===//

  auto rangeType = mlir_type_subclass(m, "RangeType", mlirTypeIsAPDLRangeType);
  rangeType.def_classmethod(
      "get",
      [](nb::object cls, MlirType elementType) {
        return cls(mlirPDLRangeTypeGet(elementType));
      },
      "Gets an instance of RangeType in the same context as the provided "
      "element type.",
      nb::arg("cls"), nb::arg("element_type"));
  rangeType.def_property_readonly(
      "element_type",
      [](MlirType type) { return mlirPDLRangeTypeGetElementType(type); },
      "Get the element type.");

  //===-------------------------------------------------------------------===//
  // TypeType
  //===-------------------------------------------------------------------===//

  auto typeType = mlir_type_subclass(m, "TypeType", mlirTypeIsAPDLTypeType);
  typeType.def_classmethod(
      "get",
      [](nb::object cls, MlirContext ctx) {
        return cls(mlirPDLTypeTypeGet(ctx));
      },
      "Get an instance of TypeType in given context.", nb::arg("cls"),
      nb::arg("context").none() = nb::none());

  //===-------------------------------------------------------------------===//
  // ValueType
  //===-------------------------------------------------------------------===//

  auto valueType = mlir_type_subclass(m, "ValueType", mlirTypeIsAPDLValueType);
  valueType.def_classmethod(
      "get",
      [](nb::object cls, MlirContext ctx) {
        return cls(mlirPDLValueTypeGet(ctx));
      },
      "Get an instance of TypeType in given context.", nb::arg("cls"),
      nb::arg("context").none() = nb::none());
}

NB_MODULE(_mlirDialectsPDL, m) {
  m.doc() = "MLIR PDL dialect.";
  populateDialectPDLSubmodule(m);
}