brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · 1acb410 Raw
106 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "mlir-c/Dialect/PDL.h"10#include "mlir-c/IR.h"11#include "mlir/Bindings/Python/Nanobind.h"12#include "mlir/Bindings/Python/NanobindAdaptors.h"13 14namespace nb = nanobind;15using namespace llvm;16using namespace mlir;17using namespace mlir::python;18using namespace mlir::python::nanobind_adaptors;19 20static void populateDialectPDLSubmodule(const nanobind::module_ &m) {21  //===-------------------------------------------------------------------===//22  // PDLType23  //===-------------------------------------------------------------------===//24 25  auto pdlType = mlir_type_subclass(m, "PDLType", mlirTypeIsAPDLType);26 27  //===-------------------------------------------------------------------===//28  // AttributeType29  //===-------------------------------------------------------------------===//30 31  auto attributeType =32      mlir_type_subclass(m, "AttributeType", mlirTypeIsAPDLAttributeType);33  attributeType.def_classmethod(34      "get",35      [](const 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") = nb::none());40 41  //===-------------------------------------------------------------------===//42  // OperationType43  //===-------------------------------------------------------------------===//44 45  auto operationType =46      mlir_type_subclass(m, "OperationType", mlirTypeIsAPDLOperationType);47  operationType.def_classmethod(48      "get",49      [](const 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") = nb::none());54 55  //===-------------------------------------------------------------------===//56  // RangeType57  //===-------------------------------------------------------------------===//58 59  auto rangeType = mlir_type_subclass(m, "RangeType", mlirTypeIsAPDLRangeType);60  rangeType.def_classmethod(61      "get",62      [](const 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      nb::sig(72          "def element_type(self) -> " MAKE_MLIR_PYTHON_QUALNAME("ir.Type")),73      "Get the element type.");74 75  //===-------------------------------------------------------------------===//76  // TypeType77  //===-------------------------------------------------------------------===//78 79  auto typeType = mlir_type_subclass(m, "TypeType", mlirTypeIsAPDLTypeType);80  typeType.def_classmethod(81      "get",82      [](const nb::object &cls, MlirContext ctx) {83        return cls(mlirPDLTypeTypeGet(ctx));84      },85      "Get an instance of TypeType in given context.", nb::arg("cls"),86      nb::arg("context") = nb::none());87 88  //===-------------------------------------------------------------------===//89  // ValueType90  //===-------------------------------------------------------------------===//91 92  auto valueType = mlir_type_subclass(m, "ValueType", mlirTypeIsAPDLValueType);93  valueType.def_classmethod(94      "get",95      [](const nb::object &cls, MlirContext ctx) {96        return cls(mlirPDLValueTypeGet(ctx));97      },98      "Get an instance of TypeType in given context.", nb::arg("cls"),99      nb::arg("context") = nb::none());100}101 102NB_MODULE(_mlirDialectsPDL, m) {103  m.doc() = "MLIR PDL dialect.";104  populateDialectPDLSubmodule(m);105}106