CIRCT 21.0.0git
Loading...
Searching...
No Matches
RTGModule.cpp
Go to the documentation of this file.
1//===- RTGModule.cpp - RTG API nanobind module ----------------------------===//
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 "CIRCTModules.h"
10
11#include "circt-c/Dialect/RTG.h"
12
13#include "mlir/Bindings/Python/NanobindAdaptors.h"
14
15#include <nanobind/nanobind.h>
16
17namespace nb = nanobind;
18
19using namespace circt;
20using namespace mlir::python::nanobind_adaptors;
21
22/// Populate the rtg python module.
24 m.doc() = "RTG dialect Python native extension";
25
26 mlir_type_subclass(m, "SequenceType", rtgTypeIsASequence)
27 .def_classmethod(
28 "get",
29 [](nb::object cls, std::vector<MlirType> &elementTypes,
30 MlirContext ctxt) {
31 return cls(rtgSequenceTypeGet(ctxt, elementTypes.size(),
32 elementTypes.data()));
33 },
34 nb::arg("self"), nb::arg("elementTypes") = std::vector<MlirType>(),
35 nb::arg("ctxt") = nullptr)
36 .def_property_readonly(
37 "num_elements",
38 [](MlirType self) { return rtgSequenceTypeGetNumElements(self); })
39 .def("get_element", [](MlirType self, unsigned i) {
40 return rtgSequenceTypeGetElement(self, i);
41 });
42
43 mlir_type_subclass(m, "RandomizedSequenceType", rtgTypeIsARandomizedSequence)
44 .def_classmethod(
45 "get",
46 [](nb::object cls, MlirContext ctxt) {
47 return cls(rtgRandomizedSequenceTypeGet(ctxt));
48 },
49 nb::arg("self"), nb::arg("ctxt") = nullptr);
50
51 mlir_type_subclass(m, "LabelType", rtgTypeIsALabel)
52 .def_classmethod(
53 "get",
54 [](nb::object cls, MlirContext ctxt) {
55 return cls(rtgLabelTypeGet(ctxt));
56 },
57 nb::arg("self"), nb::arg("ctxt") = nullptr);
58
59 mlir_type_subclass(m, "SetType", rtgTypeIsASet)
60 .def_classmethod(
61 "get",
62 [](nb::object cls, MlirType elementType) {
63 return cls(rtgSetTypeGet(elementType));
64 },
65 nb::arg("self"), nb::arg("element_type"))
66 .def_property_readonly("element_type", [](MlirType self) {
67 return rtgSetTypeGetElementType(self);
68 });
69
70 mlir_type_subclass(m, "BagType", rtgTypeIsABag)
71 .def_classmethod(
72 "get",
73 [](nb::object cls, MlirType elementType) {
74 return cls(rtgBagTypeGet(elementType));
75 },
76 nb::arg("self"), nb::arg("element_type"))
77 .def_property_readonly("element_type", [](MlirType self) {
78 return rtgBagTypeGetElementType(self);
79 });
80
81 mlir_type_subclass(m, "DictType", rtgTypeIsADict)
82 .def_classmethod(
83 "get",
84 [](nb::object cls,
85 const std::vector<std::pair<MlirAttribute, MlirType>> &entries,
86 MlirContext ctxt) {
87 std::vector<MlirAttribute> names;
88 std::vector<MlirType> types;
89 for (auto entry : entries) {
90 names.push_back(entry.first);
91 types.push_back(entry.second);
92 }
93 return cls(
94 rtgDictTypeGet(ctxt, types.size(), names.data(), types.data()));
95 },
96 nb::arg("self"),
97 nb::arg("entries") =
98 std::vector<std::pair<MlirAttribute, MlirType>>(),
99 nb::arg("ctxt") = nullptr);
100
101 nb::enum_<RTGLabelVisibility>(m, "LabelVisibility")
102 .value("LOCAL", RTG_LABEL_VISIBILITY_LOCAL)
103 .value("GLOBAL", RTG_LABEL_VISIBILITY_GLOBAL)
104 .value("EXTERNAL", RTG_LABEL_VISIBILITY_EXTERNAL)
105 .export_values();
106
107 mlir_attribute_subclass(m, "LabelVisibilityAttr",
109 .def_classmethod(
110 "get",
111 [](nb::object cls, RTGLabelVisibility visibility, MlirContext ctxt) {
112 return cls(rtgLabelVisibilityAttrGet(ctxt, visibility));
113 },
114 nb::arg("self"), nb::arg("visibility"), nb::arg("ctxt") = nullptr)
115 .def_property_readonly("value", [](MlirAttribute self) {
117 });
118
119 mlir_attribute_subclass(m, "DefaultContextAttr", rtgAttrIsADefaultContextAttr)
120 .def_classmethod(
121 "get",
122 [](nb::object cls, MlirType type, MlirContext ctxt) {
123 return cls(rtgDefaultContextAttrGet(ctxt, type));
124 },
125 nb::arg("self"), nb::arg("type"), nb::arg("ctxt") = nullptr);
126}
MlirType elementType
Definition CHIRRTL.cpp:29
MLIR_CAPI_EXPORTED MlirType rtgLabelTypeGet(MlirContext ctxt)
Creates an RTG mode type in the context.
Definition RTG.cpp:68
MLIR_CAPI_EXPORTED RTGLabelVisibility rtgLabelVisibilityAttrGetValue(MlirAttribute attr)
Get the RTG label visibility from the attribute.
Definition RTG.cpp:126
RTGLabelVisibility
Definition RTG.h:86
@ RTG_LABEL_VISIBILITY_EXTERNAL
Definition RTG.h:89
@ RTG_LABEL_VISIBILITY_GLOBAL
Definition RTG.h:88
@ RTG_LABEL_VISIBILITY_LOCAL
Definition RTG.h:87
MLIR_CAPI_EXPORTED MlirType rtgSequenceTypeGetElement(MlirType type, unsigned i)
The type of of the substitution element at the given index.
Definition RTG.cpp:48
MLIR_CAPI_EXPORTED MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG default context attribute in the context.
Definition RTG.cpp:159
MLIR_CAPI_EXPORTED bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr)
If the attribute is an RTG label visibility.
Definition RTG.cpp:122
MLIR_CAPI_EXPORTED MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility)
Creates an RTG label visibility attribute in the context.
Definition RTG.cpp:140
MLIR_CAPI_EXPORTED bool rtgTypeIsABag(MlirType type)
If the type is an RTG bag.
Definition RTG.cpp:89
MLIR_CAPI_EXPORTED MlirType rtgSequenceTypeGet(MlirContext ctxt, intptr_t numElements, MlirType const *elementTypes)
Creates an RTG sequence type in the context.
Definition RTG.cpp:36
MLIR_CAPI_EXPORTED unsigned rtgSequenceTypeGetNumElements(MlirType type)
The number of substitution elements of the RTG sequence.
Definition RTG.cpp:44
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGet(MlirType elementType)
Creates an RTG set type in the context.
Definition RTG.cpp:77
MLIR_CAPI_EXPORTED bool rtgTypeIsASet(MlirType type)
If the type is an RTG set.
Definition RTG.cpp:75
MLIR_CAPI_EXPORTED MlirType rtgRandomizedSequenceTypeGet(MlirContext ctxt)
Creates an RTG randomized sequence type in the context.
Definition RTG.cpp:59
MLIR_CAPI_EXPORTED bool rtgTypeIsARandomizedSequence(MlirType type)
If the type is an RTG randomized sequence.
Definition RTG.cpp:55
MLIR_CAPI_EXPORTED bool rtgTypeIsADict(MlirType type)
If the type is an RTG dict.
Definition RTG.cpp:103
MLIR_CAPI_EXPORTED MlirType rtgDictTypeGet(MlirContext ctxt, intptr_t numEntries, MlirAttribute const *entryNames, MlirType const *entryTypes)
Creates an RTG dict type in the context.
Definition RTG.cpp:105
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGet(MlirType elementType)
Creates an RTG bag type in the context.
Definition RTG.cpp:91
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGetElementType(MlirType type)
Return the element type of the RTG bag.
Definition RTG.cpp:96
MLIR_CAPI_EXPORTED bool rtgTypeIsASequence(MlirType type)
If the type is an RTG sequence.
Definition RTG.cpp:32
MLIR_CAPI_EXPORTED bool rtgTypeIsALabel(MlirType type)
If the type is an RTG label.
Definition RTG.cpp:66
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGetElementType(MlirType type)
Return the element type of the RTG set.
Definition RTG.cpp:82
MLIR_CAPI_EXPORTED bool rtgAttrIsADefaultContextAttr(MlirAttribute attr)
If the attribute is an RTG default context.
Definition RTG.cpp:155
void populateDialectRTGSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.