Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 //===--------------------------------------------------------------------===//
27 // Types
28 //===--------------------------------------------------------------------===//
29
30 // Sequence Types
31 //===--------------------------------------------------------------------===//
32
33 mlir_type_subclass(m, "SequenceType", rtgTypeIsASequence)
34 .def_classmethod(
35 "get",
36 [](nb::object cls, std::vector<MlirType> &elementTypes,
37 MlirContext ctxt) {
38 return cls(rtgSequenceTypeGet(ctxt, elementTypes.size(),
39 elementTypes.data()));
40 },
41 nb::arg("self"), nb::arg("elementTypes") = std::vector<MlirType>(),
42 nb::arg("ctxt") = nullptr)
43 .def_property_readonly(
44 "num_elements",
45 [](MlirType self) { return rtgSequenceTypeGetNumElements(self); })
46 .def("get_element", [](MlirType self, unsigned i) {
47 return rtgSequenceTypeGetElement(self, i);
48 });
49
50 mlir_type_subclass(m, "RandomizedSequenceType", rtgTypeIsARandomizedSequence)
51 .def_classmethod(
52 "get",
53 [](nb::object cls, MlirContext ctxt) {
54 return cls(rtgRandomizedSequenceTypeGet(ctxt));
55 },
56 nb::arg("self"), nb::arg("ctxt") = nullptr);
57
58 // Common Datastructure Types
59 //===--------------------------------------------------------------------===//
60
61 mlir_type_subclass(m, "SetType", rtgTypeIsASet)
62 .def_classmethod(
63 "get",
64 [](nb::object cls, MlirType elementType) {
65 return cls(rtgSetTypeGet(elementType));
66 },
67 nb::arg("self"), nb::arg("element_type"))
68 .def_property_readonly("element_type", [](MlirType self) {
69 return rtgSetTypeGetElementType(self);
70 });
71
72 mlir_type_subclass(m, "BagType", rtgTypeIsABag)
73 .def_classmethod(
74 "get",
75 [](nb::object cls, MlirType elementType) {
76 return cls(rtgBagTypeGet(elementType));
77 },
78 nb::arg("self"), nb::arg("element_type"))
79 .def_property_readonly("element_type", [](MlirType self) {
80 return rtgBagTypeGetElementType(self);
81 });
82
83 mlir_type_subclass(m, "DictType", rtgTypeIsADict)
84 .def_classmethod(
85 "get",
86 [](nb::object cls,
87 const std::vector<std::pair<MlirAttribute, MlirType>> &entries,
88 MlirContext ctxt) {
89 std::vector<MlirAttribute> names;
90 std::vector<MlirType> types;
91 for (auto entry : entries) {
92 names.push_back(entry.first);
93 types.push_back(entry.second);
94 }
95 return cls(
96 rtgDictTypeGet(ctxt, types.size(), names.data(), types.data()));
97 },
98 nb::arg("self"),
99 nb::arg("entries") =
100 std::vector<std::pair<MlirAttribute, MlirType>>(),
101 nb::arg("ctxt") = nullptr);
102
103 mlir_type_subclass(m, "ArrayType", rtgTypeIsAArray)
104 .def_classmethod(
105 "get",
106 [](nb::object cls, MlirType elementType, MlirContext ctxt) {
107 return cls(rtgArrayTypeGet(elementType));
108 },
109 nb::arg("self"), nb::arg("element_type"), nb::arg("ctxt") = nullptr)
110 .def_property_readonly("element_type", [](MlirType self) {
111 return rtgArrayTypeGetElementType(self);
112 });
113
114 // Types for ISA targets
115 //===--------------------------------------------------------------------===//
116
117 mlir_type_subclass(m, "LabelType", rtgTypeIsALabel)
118 .def_classmethod(
119 "get",
120 [](nb::object cls, MlirContext ctxt) {
121 return cls(rtgLabelTypeGet(ctxt));
122 },
123 nb::arg("self"), nb::arg("ctxt") = nullptr);
124
125 mlir_type_subclass(m, "ImmediateType", rtgTypeIsAImmediate)
126 .def_classmethod(
127 "get",
128 [](nb::object cls, uint32_t width, MlirContext ctx) {
129 return cls(rtgImmediateTypeGet(ctx, width));
130 },
131 nb::arg("self"), nb::arg("width"), nb::arg("ctx") = nullptr)
132 .def_property_readonly("width", [](MlirType self) {
133 return rtgImmediateTypeGetWidth(self);
134 });
135
136 //===--------------------------------------------------------------------===//
137 // Attributes
138 //===--------------------------------------------------------------------===//
139
140 mlir_attribute_subclass(m, "DefaultContextAttr", rtgAttrIsADefaultContextAttr)
141 .def_classmethod(
142 "get",
143 [](nb::object cls, MlirType type, MlirContext ctxt) {
144 return cls(rtgDefaultContextAttrGet(ctxt, type));
145 },
146 nb::arg("self"), nb::arg("type"), nb::arg("ctxt") = nullptr);
147
148 // Attributes for ISA targets
149 //===--------------------------------------------------------------------===//
150
151 nb::enum_<RTGLabelVisibility>(m, "LabelVisibility")
152 .value("LOCAL", RTG_LABEL_VISIBILITY_LOCAL)
153 .value("GLOBAL", RTG_LABEL_VISIBILITY_GLOBAL)
154 .value("EXTERNAL", RTG_LABEL_VISIBILITY_EXTERNAL)
155 .export_values();
156
157 mlir_attribute_subclass(m, "LabelVisibilityAttr",
159 .def_classmethod(
160 "get",
161 [](nb::object cls, RTGLabelVisibility visibility, MlirContext ctxt) {
162 return cls(rtgLabelVisibilityAttrGet(ctxt, visibility));
163 },
164 nb::arg("self"), nb::arg("visibility"), nb::arg("ctxt") = nullptr)
165 .def_property_readonly("value", [](MlirAttribute self) {
167 });
168
169 mlir_attribute_subclass(m, "ImmediateAttr", rtgAttrIsAImmediate)
170 .def_classmethod(
171 "get",
172 [](nb::object cls, uint32_t width, uint64_t value, MlirContext ctx) {
173 return cls(rtgImmediateAttrGet(ctx, width, value));
174 },
175 nb::arg("self"), nb::arg("width"), nb::arg("value"),
176 nb::arg("ctx") = nullptr)
177 .def_property_readonly(
178 "width",
179 [](MlirAttribute self) { return rtgImmediateAttrGetWidth(self); })
180 .def_property_readonly("value", [](MlirAttribute self) {
181 return rtgImmediateAttrGetValue(self);
182 });
183}
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:169
MLIR_CAPI_EXPORTED uint32_t rtgImmediateAttrGetWidth(MlirAttribute attr)
Returns the width of the RTG immediate attribute.
Definition RTG.cpp:210
RTGLabelVisibility
Definition RTG.h:105
@ RTG_LABEL_VISIBILITY_EXTERNAL
Definition RTG.h:108
@ RTG_LABEL_VISIBILITY_GLOBAL
Definition RTG.h:107
@ RTG_LABEL_VISIBILITY_LOCAL
Definition RTG.h:106
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:158
MLIR_CAPI_EXPORTED MlirType rtgImmediateTypeGet(MlirContext ctx, uint32_t width)
Creates an RTG immediate type in the context.
Definition RTG.cpp:139
MLIR_CAPI_EXPORTED bool rtgTypeIsAArray(MlirType type)
If the type is an RTG array.
Definition RTG.cpp:126
MLIR_CAPI_EXPORTED bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr)
If the attribute is an RTG label visibility.
Definition RTG.cpp:165
MLIR_CAPI_EXPORTED MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility)
Creates an RTG label visibility attribute in the context.
Definition RTG.cpp:183
MLIR_CAPI_EXPORTED MlirType rtgArrayTypeGetElementType(MlirType type)
Returns the element type of the RTG array.
Definition RTG.cpp:128
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 MlirAttribute rtgImmediateAttrGet(MlirContext ctx, uint32_t width, uint64_t value)
Creates an RTG immediate attribute in the context with the given width and value.
Definition RTG.cpp:205
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 uint64_t rtgImmediateAttrGetValue(MlirAttribute attr)
Returns the value of the RTG immediate attribute.
Definition RTG.cpp:214
MLIR_CAPI_EXPORTED bool rtgAttrIsAImmediate(MlirAttribute attr)
Checks if the attribute is an RTG immediate attribute.
Definition RTG.cpp:201
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 bool rtgTypeIsAImmediate(MlirType type)
If the type is an RTG immediate.
Definition RTG.cpp:135
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 rtgArrayTypeGet(MlirType elementType)
Creates an RTG array type in the context.
Definition RTG.cpp:121
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 uint32_t rtgImmediateTypeGetWidth(MlirType type)
Returns the width of the RTG immediate type.
Definition RTG.cpp:143
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:154
void populateDialectRTGSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.