CIRCT 23.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 //===--------------------------------------------------------------------===//
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 mlir_type_subclass(m, "MapType", rtgTypeIsAMap)
115 .def_classmethod(
116 "get",
117 [](nb::object cls, MlirType keyType, MlirType valueType) {
118 return cls(rtgMapTypeGet(keyType, valueType));
119 },
120 nb::arg("self"), nb::arg("key_type"), nb::arg("value_type"))
121 .def_property_readonly(
122 "key_type", [](MlirType self) { return rtgMapTypeGetKeyType(self); })
123 .def_property_readonly("value_type", [](MlirType self) {
124 return rtgMapTypeGetValueType(self);
125 });
126
127 mlir_type_subclass(m, "TupleType", rtgTypeIsATuple)
128 .def_classmethod(
129 "get",
130 [](nb::object cls, const std::vector<MlirType> &fieldTypes,
131 MlirContext ctxt) {
132 return cls(
133 rtgTupleTypeGet(ctxt, fieldTypes.size(), fieldTypes.data()));
134 },
135 nb::arg("self"), nb::arg("field_types") = std::vector<MlirType>(),
136 nb::arg("ctxt") = nullptr)
137 .def_property_readonly("fields", [](MlirType self) {
138 std::vector<MlirType> fields;
139 for (intptr_t i = 0; i < rtgTypeGetNumFields(self); ++i)
140 fields.push_back(rtgTupleTypeGetFieldType(self, i));
141 return fields;
142 });
143
144 mlir_type_subclass(m, "StringType", rtgTypeIsAString)
145 .def_classmethod(
146 "get",
147 [](nb::object cls, MlirContext ctxt) {
148 return cls(rtgStringTypeGet(ctxt));
149 },
150 nb::arg("self"), nb::arg("ctxt") = nullptr);
151
152 // Types for ISA targets
153 //===--------------------------------------------------------------------===//
154
155 mlir_type_subclass(m, "LabelType", rtgTypeIsALabel)
156 .def_classmethod(
157 "get",
158 [](nb::object cls, MlirContext ctxt) {
159 return cls(rtgLabelTypeGet(ctxt));
160 },
161 nb::arg("self"), nb::arg("ctxt") = nullptr);
162
163 mlir_type_subclass(m, "ImmediateType", rtgTypeIsAImmediate)
164 .def_classmethod(
165 "get",
166 [](nb::object cls, uint32_t width, MlirContext ctx) {
167 return cls(rtgImmediateTypeGet(ctx, width));
168 },
169 nb::arg("self"), nb::arg("width"), nb::arg("ctx") = nullptr)
170 .def_property_readonly("width", [](MlirType self) {
171 return rtgImmediateTypeGetWidth(self);
172 });
173
174 mlir_type_subclass(m, "MemoryBlockType", rtgTypeIsAMemoryBlock)
175 .def_classmethod(
176 "get",
177 [](nb::object cls, uint32_t addressWidth, MlirContext ctxt) {
178 return cls(rtgMemoryBlockTypeGet(ctxt, addressWidth));
179 },
180 nb::arg("self"), nb::arg("address_width"), nb::arg("ctxt") = nullptr)
181 .def_property_readonly("address_width", [](MlirType self) {
183 });
184
185 mlir_type_subclass(m, "MemoryType", rtgTypeIsAMemory)
186 .def_classmethod(
187 "get",
188 [](nb::object cls, uint32_t addressWidth, MlirContext ctxt) {
189 return cls(rtgMemoryTypeGet(ctxt, addressWidth));
190 },
191 nb::arg("self"), nb::arg("address_width"), nb::arg("ctxt") = nullptr)
192 .def_property_readonly("address_width", [](MlirType self) {
193 return rtgMemoryTypeGetAddressWidth(self);
194 });
195
196 //===--------------------------------------------------------------------===//
197 // Attributes
198 //===--------------------------------------------------------------------===//
199
200 mlir_attribute_subclass(m, "DefaultContextAttr", rtgAttrIsADefaultContextAttr)
201 .def_classmethod(
202 "get",
203 [](nb::object cls, MlirType type, MlirContext ctxt) {
204 return cls(rtgDefaultContextAttrGet(ctxt, type));
205 },
206 nb::arg("self"), nb::arg("type"), nb::arg("ctxt") = nullptr);
207
208 mlir_attribute_subclass(m, "AnyContextAttr", rtgAttrIsAAnyContextAttr)
209 .def_classmethod(
210 "get",
211 [](nb::object cls, MlirType type, MlirContext ctxt) {
212 return cls(rtgAnyContextAttrGet(ctxt, type));
213 },
214 nb::arg("self"), nb::arg("type"), nb::arg("ctxt") = nullptr);
215
216 mlir_attribute_subclass(m, "MapAttr", rtgAttrIsAMap)
217 .def_classmethod(
218 "get",
219 [](nb::object cls, MlirType mapType,
220 const std::vector<std::pair<MlirAttribute, MlirAttribute>>
221 &entries,
222 MlirContext ctxt) {
223 std::vector<MlirAttribute> keys;
224 std::vector<MlirAttribute> values;
225 for (auto entry : entries) {
226 keys.push_back(entry.first);
227 values.push_back(entry.second);
228 }
229 return cls(rtgMapAttrGet(ctxt, mapType, keys.size(), keys.data(),
230 values.data()));
231 },
232 nb::arg("self"), nb::arg("map_type"),
233 nb::arg("entries") =
234 std::vector<std::pair<MlirAttribute, MlirAttribute>>(),
235 nb::arg("ctxt") = nullptr)
236 .def(
237 "lookup",
238 [](MlirAttribute self, MlirAttribute key) {
239 auto val = rtgMapAttrLookup(self, key);
240 if (mlirAttributeIsNull(val))
241 return nb::none();
242 return nb::cast(val);
243 },
244 nb::arg("key"),
245 "Look up the value associated with the given key. Returns None if "
246 "the key is not found.");
247
248 // Attributes for ISA targets
249 //===--------------------------------------------------------------------===//
250
251 nb::enum_<RTGLabelVisibility>(m, "LabelVisibility")
252 .value("LOCAL", RTG_LABEL_VISIBILITY_LOCAL)
253 .value("GLOBAL", RTG_LABEL_VISIBILITY_GLOBAL)
254 .value("EXTERNAL", RTG_LABEL_VISIBILITY_EXTERNAL)
255 .export_values();
256
257 mlir_attribute_subclass(m, "LabelVisibilityAttr",
259 .def_classmethod(
260 "get",
261 [](nb::object cls, RTGLabelVisibility visibility, MlirContext ctxt) {
262 return cls(rtgLabelVisibilityAttrGet(ctxt, visibility));
263 },
264 nb::arg("self"), nb::arg("visibility"), nb::arg("ctxt") = nullptr)
265 .def_property_readonly("value", [](MlirAttribute self) {
267 });
268
269 mlir_attribute_subclass(m, "ImmediateAttr", rtgAttrIsAImmediate)
270 .def_classmethod(
271 "get",
272 [](nb::object cls, uint32_t width, uint64_t value, MlirContext ctx) {
273 return cls(rtgImmediateAttrGet(ctx, width, value));
274 },
275 nb::arg("self"), nb::arg("width"), nb::arg("value"),
276 nb::arg("ctx") = nullptr)
277 .def_property_readonly(
278 "width",
279 [](MlirAttribute self) { return rtgImmediateAttrGetWidth(self); })
280 .def_property_readonly("value", [](MlirAttribute self) {
281 return rtgImmediateAttrGetValue(self);
282 });
283
284 mlir_attribute_subclass(m, "VirtualRegisterConfigAttr",
286 .def_classmethod(
287 "get",
288 [](nb::object cls, const std::vector<MlirAttribute> &allowedRegs,
289 MlirContext ctxt) {
290 return cls(rtgVirtualRegisterConfigAttrGet(ctxt, allowedRegs.size(),
291 allowedRegs.data()));
292 },
293 nb::arg("self"), nb::arg("allowed_regs"), nb::arg("ctxt") = nullptr)
294 .def_property_readonly("regs", [](MlirAttribute self) {
295 std::vector<MlirAttribute> regs;
296 for (unsigned
297 i = 0,
299 i < numRegs; ++i)
300 regs.push_back(rtgVirtualRegisterConfigAttrGetRegister(self, i));
301 return regs;
302 });
303
304 mlir_attribute_subclass(m, "LabelAttr", rtgAttrIsALabel)
305 .def_classmethod(
306 "get",
307 [](nb::object cls, const std::string &name, MlirContext ctxt) {
308 MlirStringRef nameRef =
309 mlirStringRefCreate(name.data(), name.size());
310 return cls(rtgLabelAttrGet(ctxt, nameRef));
311 },
312 nb::arg("self"), nb::arg("name"), nb::arg("ctxt") = nullptr)
313 .def_property_readonly("name", [](MlirAttribute self) {
314 MlirStringRef name = rtgLabelAttrGetName(self);
315 return nb::str(name.data, name.length);
316 });
317}
MlirType elementType
Definition CHIRRTL.cpp:29
MLIR_CAPI_EXPORTED MlirAttribute rtgAnyContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG any context attribute in the context.
Definition RTG.cpp:307
MLIR_CAPI_EXPORTED bool rtgTypeIsAMemory(MlirType type)
If the type is an RTG memory.
Definition RTG.cpp:195
MLIR_CAPI_EXPORTED bool rtgAttrIsAAnyContextAttr(MlirAttribute attr)
If the attribute is an RTG any context attribute.
Definition RTG.cpp:303
MLIR_CAPI_EXPORTED MlirType rtgLabelTypeGet(MlirContext ctxt)
Creates an RTG mode type in the context.
Definition RTG.cpp:73
MLIR_CAPI_EXPORTED RTGLabelVisibility rtgLabelVisibilityAttrGetValue(MlirAttribute attr)
Get the RTG label visibility from the attribute.
Definition RTG.cpp:251
MLIR_CAPI_EXPORTED MlirAttribute rtgVirtualRegisterConfigAttrGet(MlirContext ctxt, intptr_t numRegs, MlirAttribute const *allowedRegs)
Creates an RTG virtual register config attribute in the context.
Definition RTG.cpp:319
MLIR_CAPI_EXPORTED uint32_t rtgImmediateAttrGetWidth(MlirAttribute attr)
Returns the width of the RTG immediate attribute.
Definition RTG.cpp:292
RTGLabelVisibility
Definition RTG.h:159
@ RTG_LABEL_VISIBILITY_EXTERNAL
Definition RTG.h:162
@ RTG_LABEL_VISIBILITY_GLOBAL
Definition RTG.h:161
@ RTG_LABEL_VISIBILITY_LOCAL
Definition RTG.h:160
MLIR_CAPI_EXPORTED MlirAttribute rtgVirtualRegisterConfigAttrGetRegister(MlirAttribute attr, intptr_t index)
Returns the allowed register at the given index in the RTG virtual register config attribute.
Definition RTG.cpp:331
MLIR_CAPI_EXPORTED MlirType rtgSequenceTypeGetElement(MlirType type, unsigned i)
The type of of the substitution element at the given index.
Definition RTG.cpp:53
MLIR_CAPI_EXPORTED MlirAttribute rtgDefaultContextAttrGet(MlirContext ctxt, MlirType type)
Creates an RTG default context attribute in the context.
Definition RTG.cpp:240
MLIR_CAPI_EXPORTED MlirAttribute rtgMapAttrGet(MlirContext ctx, MlirType mapType, intptr_t numEntries, MlirAttribute const *keys, MlirAttribute const *values)
Creates an RTG map attribute in the context with the given entries.
Definition RTG.cpp:360
MLIR_CAPI_EXPORTED MlirType rtgImmediateTypeGet(MlirContext ctx, uint32_t width)
Creates an RTG immediate type in the context.
Definition RTG.cpp:184
MLIR_CAPI_EXPORTED uint32_t rtgMemoryTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory type.
Definition RTG.cpp:201
MLIR_CAPI_EXPORTED bool rtgTypeIsAArray(MlirType type)
If the type is an RTG array.
Definition RTG.cpp:131
MLIR_CAPI_EXPORTED bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr)
If the attribute is an RTG label visibility.
Definition RTG.cpp:247
MLIR_CAPI_EXPORTED bool rtgAttrIsALabel(MlirAttribute attr)
Checks if the attribute is an RTG label attribute.
Definition RTG.cpp:341
MLIR_CAPI_EXPORTED intptr_t rtgTypeGetNumFields(MlirType type)
Returns the number of fields in the RTG tuple.
Definition RTG.cpp:169
MLIR_CAPI_EXPORTED MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility)
Creates an RTG label visibility attribute in the context.
Definition RTG.cpp:265
MLIR_CAPI_EXPORTED MlirAttribute rtgMapAttrLookup(MlirAttribute attr, MlirAttribute key)
Looks up the value associated with the given key in the RTG map attribute.
Definition RTG.cpp:371
MLIR_CAPI_EXPORTED MlirType rtgMapTypeGet(MlirType keyType, MlirType valueType)
Creates an RTG map type in the context.
Definition RTG.cpp:142
MLIR_CAPI_EXPORTED MlirType rtgArrayTypeGetElementType(MlirType type)
Returns the element type of the RTG array.
Definition RTG.cpp:133
MLIR_CAPI_EXPORTED bool rtgTypeIsATuple(MlirType type)
If the type is an RTG tuple.
Definition RTG.cpp:165
MLIR_CAPI_EXPORTED bool rtgTypeIsABag(MlirType type)
If the type is an RTG bag.
Definition RTG.cpp:94
MLIR_CAPI_EXPORTED MlirType rtgSequenceTypeGet(MlirContext ctxt, intptr_t numElements, MlirType const *elementTypes)
Creates an RTG sequence type in the context.
Definition RTG.cpp:41
MLIR_CAPI_EXPORTED MlirType rtgStringTypeGet(MlirContext ctxt)
Creates an RTG string type in the context.
Definition RTG.cpp:225
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:287
MLIR_CAPI_EXPORTED unsigned rtgSequenceTypeGetNumElements(MlirType type)
The number of substitution elements of the RTG sequence.
Definition RTG.cpp:49
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGet(MlirType elementType)
Creates an RTG set type in the context.
Definition RTG.cpp:82
MLIR_CAPI_EXPORTED bool rtgTypeIsASet(MlirType type)
If the type is an RTG set.
Definition RTG.cpp:80
MLIR_CAPI_EXPORTED MlirType rtgTupleTypeGet(MlirContext ctxt, intptr_t numFields, MlirType const *fieldTypes)
Creates an RTG tuple type in the context.
Definition RTG.cpp:157
MLIR_CAPI_EXPORTED MlirType rtgMemoryTypeGet(MlirContext ctx, uint32_t addressWidth)
Creates an RTG memory type in the context.
Definition RTG.cpp:197
MLIR_CAPI_EXPORTED MlirType rtgRandomizedSequenceTypeGet(MlirContext ctxt)
Creates an RTG randomized sequence type in the context.
Definition RTG.cpp:64
MLIR_CAPI_EXPORTED uint64_t rtgImmediateAttrGetValue(MlirAttribute attr)
Returns the value of the RTG immediate attribute.
Definition RTG.cpp:296
MLIR_CAPI_EXPORTED bool rtgTypeIsAMemoryBlock(MlirType type)
If the type is an RTG memory block.
Definition RTG.cpp:208
MLIR_CAPI_EXPORTED bool rtgAttrIsAImmediate(MlirAttribute attr)
Checks if the attribute is an RTG immediate attribute.
Definition RTG.cpp:283
MLIR_CAPI_EXPORTED bool rtgTypeIsARandomizedSequence(MlirType type)
If the type is an RTG randomized sequence.
Definition RTG.cpp:60
MLIR_CAPI_EXPORTED bool rtgTypeIsAMap(MlirType type)
If the type is an RTG map.
Definition RTG.cpp:140
MLIR_CAPI_EXPORTED bool rtgTypeIsADict(MlirType type)
If the type is an RTG dict.
Definition RTG.cpp:108
MLIR_CAPI_EXPORTED MlirType rtgTupleTypeGetFieldType(MlirType type, intptr_t idx)
Returns a field type of the RTG tuple.
Definition RTG.cpp:173
MLIR_CAPI_EXPORTED MlirStringRef rtgLabelAttrGetName(MlirAttribute attr)
Returns the name of the RTG label attribute.
Definition RTG.cpp:349
MLIR_CAPI_EXPORTED bool rtgAttrIsAVirtualRegisterConfig(MlirAttribute attr)
Checks if the attribute is an RTG virtual register config attribute.
Definition RTG.cpp:314
MLIR_CAPI_EXPORTED bool rtgTypeIsAImmediate(MlirType type)
If the type is an RTG immediate.
Definition RTG.cpp:180
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:110
MLIR_CAPI_EXPORTED bool rtgAttrIsAMap(MlirAttribute attr)
Checks if the attribute is an RTG map attribute.
Definition RTG.cpp:356
MLIR_CAPI_EXPORTED intptr_t rtgVirtualRegisterConfigAttrGetNumRegisters(MlirAttribute attr)
Returns the number of allowed registers in the RTG virtual register config attribute.
Definition RTG.cpp:327
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGet(MlirType elementType)
Creates an RTG bag type in the context.
Definition RTG.cpp:96
MLIR_CAPI_EXPORTED uint32_t rtgMemoryBlockTypeGetAddressWidth(MlirType type)
Returns the address with of an RTG memory block type.
Definition RTG.cpp:216
MLIR_CAPI_EXPORTED MlirType rtgArrayTypeGet(MlirType elementType)
Creates an RTG array type in the context.
Definition RTG.cpp:126
MLIR_CAPI_EXPORTED MlirType rtgBagTypeGetElementType(MlirType type)
Return the element type of the RTG bag.
Definition RTG.cpp:101
MLIR_CAPI_EXPORTED bool rtgTypeIsAString(MlirType type)
If the type is an RTG string.
Definition RTG.cpp:223
MLIR_CAPI_EXPORTED bool rtgTypeIsASequence(MlirType type)
If the type is an RTG sequence.
Definition RTG.cpp:37
MLIR_CAPI_EXPORTED MlirAttribute rtgLabelAttrGet(MlirContext ctx, MlirStringRef name)
Creates an RTG label attribute in the context with the given name.
Definition RTG.cpp:345
MLIR_CAPI_EXPORTED bool rtgTypeIsALabel(MlirType type)
If the type is an RTG label.
Definition RTG.cpp:71
MLIR_CAPI_EXPORTED uint32_t rtgImmediateTypeGetWidth(MlirType type)
Returns the width of the RTG immediate type.
Definition RTG.cpp:188
MLIR_CAPI_EXPORTED MlirType rtgMemoryBlockTypeGet(MlirContext ctx, uint32_t addressWidth)
Creates an RTG memory block type in the context.
Definition RTG.cpp:212
MLIR_CAPI_EXPORTED MlirType rtgMapTypeGetKeyType(MlirType type)
Return the key type of the RTG map.
Definition RTG.cpp:146
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGetElementType(MlirType type)
Return the element type of the RTG set.
Definition RTG.cpp:87
MLIR_CAPI_EXPORTED bool rtgAttrIsADefaultContextAttr(MlirAttribute attr)
If the attribute is an RTG default context.
Definition RTG.cpp:236
MLIR_CAPI_EXPORTED MlirType rtgMapTypeGetValueType(MlirType type)
Return the value type of the RTG map.
Definition RTG.cpp:150
void populateDialectRTGSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.