Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OMModule.cpp
Go to the documentation of this file.
1//===- OMModule.cpp - OM 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#include "circt-c/Dialect/HW.h"
11#include "circt-c/Dialect/OM.h"
12#include "mlir-c/BuiltinAttributes.h"
13#include "mlir-c/BuiltinTypes.h"
14#include "mlir-c/IR.h"
15#include "mlir/Bindings/Python/NanobindAdaptors.h"
16#include <nanobind/nanobind.h>
17#include <nanobind/stl/variant.h>
18#include <nanobind/stl/vector.h>
19namespace nb = nanobind;
20
21using namespace mlir;
22using namespace mlir::python;
23using namespace mlir::python::nanobind_adaptors;
24
25namespace {
26
27struct List;
28struct Object;
29struct Tuple;
30struct Map;
31struct BasePath;
32struct Path;
33
34/// These are the Python types that are represented by the different primitive
35/// OMEvaluatorValues as Attributes.
36using PythonPrimitive = std::variant<nb::int_, nb::float_, nb::str, nb::bool_,
37 nb::tuple, nb::list, nb::dict>;
38
39/// None is used to by nanobind when default initializing a PythonValue. The
40/// order of types in the variant matters here, and we want nanobind to try
41/// casting to the Python classes defined in this file first, before
42/// MlirAttribute and the upstream MLIR type casters. If the MlirAttribute
43/// is tried first, then we can hit an assert inside the MLIR codebase.
44struct None {};
45using PythonValue = std::variant<None, Object, List, Tuple, Map, BasePath, Path,
46 PythonPrimitive>;
47
48/// Map an opaque OMEvaluatorValue into a python value.
49PythonValue omEvaluatorValueToPythonValue(OMEvaluatorValue result);
50OMEvaluatorValue pythonValueToOMEvaluatorValue(PythonValue result,
51 MlirContext ctx);
52static PythonPrimitive omPrimitiveToPythonValue(MlirAttribute attr);
53static MlirAttribute omPythonValueToPrimitive(PythonPrimitive value,
54 MlirContext ctx);
55
56/// Provides a List class by simply wrapping the OMObject CAPI.
57struct List {
58 // Instantiate a List with a reference to the underlying OMEvaluatorValue.
59 List(OMEvaluatorValue value) : value(value) {}
60
61 /// Return the number of elements.
62 intptr_t getNumElements() { return omEvaluatorListGetNumElements(value); }
63
64 PythonValue getElement(intptr_t i);
65 OMEvaluatorValue getValue() const { return value; }
66
67private:
68 // The underlying CAPI value.
69 OMEvaluatorValue value;
70};
71
72struct Tuple {
73 // Instantiate a Tuple with a reference to the underlying OMEvaluatorValue.
74 Tuple(OMEvaluatorValue value) : value(value) {}
75
76 /// Return the number of elements.
77 intptr_t getNumElements() { return omEvaluatorTupleGetNumElements(value); }
78
79 PythonValue getElement(intptr_t i);
80 OMEvaluatorValue getValue() const { return value; }
81
82private:
83 // The underlying CAPI value.
84 OMEvaluatorValue value;
85};
86
87/// Provides a Map class by simply wrapping the OMObject CAPI.
88struct Map {
89 // Instantiate a Map with a reference to the underlying OMEvaluatorValue.
90 Map(OMEvaluatorValue value) : value(value) {}
91
92 /// Return the keys.
93 std::vector<nb::str> getKeys() {
94 auto attr = omEvaluatorMapGetKeys(value);
95 intptr_t numFieldNames = mlirArrayAttrGetNumElements(attr);
96
97 std::vector<nb::str> pyFieldNames;
98 for (intptr_t i = 0; i < numFieldNames; ++i) {
99 auto name = mlirStringAttrGetValue(mlirArrayAttrGetElement(attr, i));
100 pyFieldNames.emplace_back(nb::str(name.data, name.length));
101 }
102
103 return pyFieldNames;
104 }
105
106 /// Look up the value. A key is an integer, string or attribute.
107 PythonValue dunderGetItemAttr(MlirAttribute key);
108 PythonValue dunderGetItemNamed(const std::string &key);
109 PythonValue dunderGetItemIndexed(intptr_t key);
110 PythonValue
111 dunderGetItem(std::variant<intptr_t, std::string, MlirAttribute> key);
112
113 /// Return a context from an underlying value.
114 MlirContext getContext() const { return omEvaluatorValueGetContext(value); }
115
116 OMEvaluatorValue getValue() const { return value; }
117 MlirType getType() { return omEvaluatorMapGetType(value); }
118
119private:
120 // The underlying CAPI value.
121 OMEvaluatorValue value;
122};
123
124/// Provides a BasePath class by simply wrapping the OMObject CAPI.
125struct BasePath {
126 /// Instantiate a BasePath with a reference to the underlying
127 /// OMEvaluatorValue.
128 BasePath(OMEvaluatorValue value) : value(value) {}
129
130 static BasePath getEmpty(MlirContext context) {
131 return BasePath(omEvaluatorBasePathGetEmpty(context));
132 }
133
134 /// Return a context from an underlying value.
135 MlirContext getContext() const { return omEvaluatorValueGetContext(value); }
136
137 OMEvaluatorValue getValue() const { return value; }
138
139private:
140 // The underlying CAPI value.
141 OMEvaluatorValue value;
142};
143
144/// Provides a Path class by simply wrapping the OMObject CAPI.
145struct Path {
146 /// Instantiate a Path with a reference to the underlying OMEvaluatorValue.
147 Path(OMEvaluatorValue value) : value(value) {}
148
149 /// Return a context from an underlying value.
150 MlirContext getContext() const { return omEvaluatorValueGetContext(value); }
151
152 OMEvaluatorValue getValue() const { return value; }
153
154 std::string dunderStr() {
155 auto ref = mlirStringAttrGetValue(omEvaluatorPathGetAsString(getValue()));
156 return std::string(ref.data, ref.length);
157 }
158
159private:
160 // The underlying CAPI value.
161 OMEvaluatorValue value;
162};
163
164/// Provides an Object class by simply wrapping the OMObject CAPI.
165struct Object {
166 // Instantiate an Object with a reference to the underlying OMObject.
167 Object(OMEvaluatorValue value) : value(value) {}
168
169 /// Get the Type from an Object, which will be a ClassType.
170 MlirType getType() { return omEvaluatorObjectGetType(value); }
171
172 /// Get the Location from an Object, which will be an MlirLocation.
173 MlirLocation getLocation() { return omEvaluatorValueGetLoc(value); }
174
175 // Get the field location info.
176 MlirLocation getFieldLoc(const std::string &name) {
177 // Wrap the requested field name in an attribute.
178 MlirContext context = mlirTypeGetContext(omEvaluatorObjectGetType(value));
179 MlirStringRef cName = mlirStringRefCreateFromCString(name.c_str());
180 MlirAttribute nameAttr = mlirStringAttrGet(context, cName);
181
182 // Get the field's ObjectValue via the CAPI.
183 OMEvaluatorValue result = omEvaluatorObjectGetField(value, nameAttr);
184
185 return omEvaluatorValueGetLoc(result);
186 }
187
188 // Get a field from the Object, using nanobind's support for variant to return
189 // a Python object that is either an Object or Attribute.
190 PythonValue getField(const std::string &name) {
191 // Wrap the requested field name in an attribute.
192 MlirContext context = mlirTypeGetContext(omEvaluatorObjectGetType(value));
193 MlirStringRef cName = mlirStringRefCreateFromCString(name.c_str());
194 MlirAttribute nameAttr = mlirStringAttrGet(context, cName);
195
196 // Get the field's ObjectValue via the CAPI.
197 OMEvaluatorValue result = omEvaluatorObjectGetField(value, nameAttr);
198
199 return omEvaluatorValueToPythonValue(result);
200 }
201
202 // Get a list with the names of all the fields in the Object.
203 std::vector<std::string> getFieldNames() {
204 MlirAttribute fieldNames = omEvaluatorObjectGetFieldNames(value);
205 intptr_t numFieldNames = mlirArrayAttrGetNumElements(fieldNames);
206
207 std::vector<std::string> pyFieldNames;
208 for (intptr_t i = 0; i < numFieldNames; ++i) {
209 MlirAttribute fieldName = mlirArrayAttrGetElement(fieldNames, i);
210 MlirStringRef fieldNameStr = mlirStringAttrGetValue(fieldName);
211 pyFieldNames.emplace_back(fieldNameStr.data, fieldNameStr.length);
212 }
213
214 return pyFieldNames;
215 }
216
217 // Get the hash of the object
218 unsigned getHash() { return omEvaluatorObjectGetHash(value); }
219
220 // Check the equality of the underlying values.
221 bool eq(Object &other) { return omEvaluatorObjectIsEq(value, other.value); }
222
223 OMEvaluatorValue getValue() const { return value; }
224
225private:
226 // The underlying CAPI OMObject.
227 OMEvaluatorValue value;
228};
229
230/// Provides an Evaluator class by simply wrapping the OMEvaluator CAPI.
231struct Evaluator {
232 // Instantiate an Evaluator with a reference to the underlying OMEvaluator.
233 Evaluator(MlirModule mod) : evaluator(omEvaluatorNew(mod)) {}
234
235 // Instantiate an Object.
236 Object instantiate(MlirAttribute className,
237 std::vector<PythonValue> actualParams) {
238 std::vector<OMEvaluatorValue> values;
239 for (auto &param : actualParams)
240 values.push_back(pythonValueToOMEvaluatorValue(
241 param, mlirModuleGetContext(getModule())));
242
243 // Instantiate the Object via the CAPI.
245 evaluator, className, values.size(), values.data());
246
247 // If the Object is null, something failed. Diagnostic handling is
248 // implemented in pure Python, so nothing to do here besides throwing an
249 // error to halt execution.
250 if (omEvaluatorObjectIsNull(result))
251 throw nb::value_error(
252 "unable to instantiate object, see previous error(s)");
253
254 // Return a new Object.
255 return Object(result);
256 }
257
258 // Get the Module the Evaluator is built from.
259 MlirModule getModule() { return omEvaluatorGetModule(evaluator); }
260
261private:
262 // The underlying CAPI OMEvaluator.
263 OMEvaluator evaluator;
264};
265
266class PyListAttrIterator {
267public:
268 PyListAttrIterator(MlirAttribute attr) : attr(std::move(attr)) {}
269
270 PyListAttrIterator &dunderIter() { return *this; }
271
272 MlirAttribute dunderNext() {
273 if (nextIndex >= omListAttrGetNumElements(attr))
274 throw nb::stop_iteration();
275 return omListAttrGetElement(attr, nextIndex++);
276 }
277
278 static void bind(nb::module_ &m) {
279 nb::class_<PyListAttrIterator>(m, "ListAttributeIterator")
280 .def("__iter__", &PyListAttrIterator::dunderIter)
281 .def("__next__", &PyListAttrIterator::dunderNext);
282 }
283
284private:
285 MlirAttribute attr;
286 intptr_t nextIndex = 0;
287};
288
289PythonValue List::getElement(intptr_t i) {
290 return omEvaluatorValueToPythonValue(omEvaluatorListGetElement(value, i));
291}
292
293class PyMapAttrIterator {
294public:
295 PyMapAttrIterator(MlirAttribute attr) : attr(std::move(attr)) {}
296
297 PyMapAttrIterator &dunderIter() { return *this; }
298
299 nb::tuple dunderNext() {
300 if (nextIndex >= omMapAttrGetNumElements(attr))
301 throw nb::stop_iteration();
302
303 MlirIdentifier key = omMapAttrGetElementKey(attr, nextIndex);
304 PythonValue value =
305 omPrimitiveToPythonValue(omMapAttrGetElementValue(attr, nextIndex));
306 nextIndex++;
307
308 auto keyName = mlirIdentifierStr(key);
309 std::string keyStr(keyName.data, keyName.length);
310 return nb::make_tuple(keyStr, value);
311 }
312
313 static void bind(nb::module_ &m) {
314 nb::class_<PyMapAttrIterator>(m, "MapAttributeIterator")
315 .def("__iter__", &PyMapAttrIterator::dunderIter)
316 .def("__next__", &PyMapAttrIterator::dunderNext);
317 }
318
319private:
320 MlirAttribute attr;
321 intptr_t nextIndex = 0;
322};
323
324PythonValue Tuple::getElement(intptr_t i) {
325 if (i < 0 || i >= omEvaluatorTupleGetNumElements(value))
326 throw std::out_of_range("tuple index out of range");
327
328 return omEvaluatorValueToPythonValue(omEvaluatorTupleGetElement(value, i));
329}
330
331PythonValue Map::dunderGetItemNamed(const std::string &key) {
332 MlirType type = omMapTypeGetKeyType(omEvaluatorMapGetType(value));
333 if (!omTypeIsAStringType(type))
334 throw nanobind::key_error("key is not string");
335 MlirAttribute attr =
336 mlirStringAttrTypedGet(type, mlirStringRefCreateFromCString(key.c_str()));
337 return dunderGetItemAttr(attr);
338}
339
340PythonValue Map::dunderGetItemIndexed(intptr_t i) {
341 MlirType type = omMapTypeGetKeyType(omEvaluatorMapGetType(value));
342 if (!mlirTypeIsAInteger(type))
343 throw nanobind::key_error("key is not integer");
344 MlirAttribute attr = mlirIntegerAttrGet(type, i);
345 return dunderGetItemAttr(attr);
346}
347
348PythonValue Map::dunderGetItemAttr(MlirAttribute key) {
349 OMEvaluatorValue result = omEvaluatorMapGetElement(value, key);
350
351 if (omEvaluatorValueIsNull(result))
352 throw nanobind::key_error("key not found");
353
354 return omEvaluatorValueToPythonValue(result);
355}
356
357PythonValue
358Map::dunderGetItem(std::variant<intptr_t, std::string, MlirAttribute> key) {
359 if (auto *i = std::get_if<intptr_t>(&key))
360 return dunderGetItemIndexed(*i);
361 else if (auto *str = std::get_if<std::string>(&key))
362 return dunderGetItemNamed(*str);
363 return dunderGetItemAttr(std::get<MlirAttribute>(key));
364}
365
366// Convert a generic MLIR Attribute to a PythonValue. This is basically a C++
367// fast path of the parts of attribute_to_var that we use in the OM dialect.
368static PythonPrimitive omPrimitiveToPythonValue(MlirAttribute attr) {
369 if (omAttrIsAIntegerAttr(attr)) {
370 auto strRef = omIntegerAttrToString(attr);
371 return nb::int_(nb::str(strRef.data, strRef.length));
372 }
373
374 if (mlirAttributeIsAFloat(attr)) {
375 return nb::float_(mlirFloatAttrGetValueDouble(attr));
376 }
377
378 if (mlirAttributeIsAString(attr)) {
379 auto strRef = mlirStringAttrGetValue(attr);
380 return nb::str(strRef.data, strRef.length);
381 }
382
383 // BoolAttr's are IntegerAttr's, check this first.
384 if (mlirAttributeIsABool(attr)) {
385 return nb::bool_(mlirBoolAttrGetValue(attr));
386 }
387
388 if (mlirAttributeIsAInteger(attr)) {
389 MlirType type = mlirAttributeGetType(attr);
390 if (mlirTypeIsAIndex(type) || mlirIntegerTypeIsSignless(type))
391 return nb::int_(mlirIntegerAttrGetValueInt(attr));
392 if (mlirIntegerTypeIsSigned(type))
393 return nb::int_(mlirIntegerAttrGetValueSInt(attr));
394 return nb::int_(mlirIntegerAttrGetValueUInt(attr));
395 }
396
397 if (omAttrIsAReferenceAttr(attr)) {
398 auto innerRef = omReferenceAttrGetInnerRef(attr);
399 auto moduleStrRef =
400 mlirStringAttrGetValue(hwInnerRefAttrGetModule(innerRef));
401 auto nameStrRef = mlirStringAttrGetValue(hwInnerRefAttrGetName(innerRef));
402 auto moduleStr = nb::str(moduleStrRef.data, moduleStrRef.length);
403 auto nameStr = nb::str(nameStrRef.data, nameStrRef.length);
404 return nb::make_tuple(moduleStr, nameStr);
405 }
406
407 if (omAttrIsAListAttr(attr)) {
408 nb::list results;
409 for (intptr_t i = 0, e = omListAttrGetNumElements(attr); i < e; ++i)
410 results.append(omPrimitiveToPythonValue(omListAttrGetElement(attr, i)));
411 return results;
412 }
413
414 if (omAttrIsAMapAttr(attr)) {
415 nb::dict results;
416 for (intptr_t i = 0, e = omMapAttrGetNumElements(attr); i < e; ++i) {
417 auto keyStrRef = mlirIdentifierStr(omMapAttrGetElementKey(attr, i));
418 auto key = nb::str(keyStrRef.data, keyStrRef.length);
419 auto value = omPrimitiveToPythonValue(omMapAttrGetElementValue(attr, i));
420 results[key] = value;
421 }
422 return results;
423 }
424
425 mlirAttributeDump(attr);
426 throw nb::type_error("Unexpected OM primitive attribute");
427}
428
429// Convert a primitive PythonValue to a generic MLIR Attribute. This is
430// basically a C++ fast path of the parts of var_to_attribute that we use in the
431// OM dialect.
432static MlirAttribute omPythonValueToPrimitive(PythonPrimitive value,
433 MlirContext ctx) {
434 if (auto *intValue = std::get_if<nb::int_>(&value)) {
435 auto intType = mlirIntegerTypeGet(ctx, 64);
436 auto intAttr = mlirIntegerAttrGet(intType, nb::cast<int64_t>(*intValue));
437 return omIntegerAttrGet(intAttr);
438 }
439
440 if (auto *attr = std::get_if<nb::float_>(&value)) {
441 auto floatType = mlirF64TypeGet(ctx);
442 return mlirFloatAttrDoubleGet(ctx, floatType, nb::cast<double>(*attr));
443 }
444
445 if (auto *attr = std::get_if<nb::str>(&value)) {
446 auto str = nb::cast<std::string>(*attr);
447 auto strRef = mlirStringRefCreate(str.data(), str.length());
448 return mlirStringAttrGet(ctx, strRef);
449 }
450
451 if (auto *attr = std::get_if<nb::bool_>(&value)) {
452 return mlirBoolAttrGet(ctx, nb::cast<bool>(*attr));
453 }
454
455 throw nb::type_error("Unexpected OM primitive value");
456}
457
458PythonValue omEvaluatorValueToPythonValue(OMEvaluatorValue result) {
459 // If the result is null, something failed. Diagnostic handling is
460 // implemented in pure Python, so nothing to do here besides throwing an
461 // error to halt execution.
462 if (omEvaluatorValueIsNull(result))
463 throw nb::value_error("unable to get field, see previous error(s)");
464
465 // If the field was an Object, return a new Object.
466 if (omEvaluatorValueIsAObject(result))
467 return Object(result);
468
469 // If the field was a list, return a new List.
470 if (omEvaluatorValueIsAList(result))
471 return List(result);
472
473 // If the field was a tuple, return a new Tuple.
474 if (omEvaluatorValueIsATuple(result))
475 return Tuple(result);
476
477 // If the field was a map, return a new Map.
478 if (omEvaluatorValueIsAMap(result))
479 return Map(result);
480
481 // If the field was a base path, return a new BasePath.
482 if (omEvaluatorValueIsABasePath(result))
483 return BasePath(result);
484
485 // If the field was a path, return a new Path.
486 if (omEvaluatorValueIsAPath(result))
487 return Path(result);
488
490 return omEvaluatorValueToPythonValue(
492
493 // If the field was a primitive, return the Attribute.
495 return omPrimitiveToPythonValue(omEvaluatorValueGetPrimitive(result));
496}
497
498OMEvaluatorValue pythonValueToOMEvaluatorValue(PythonValue result,
499 MlirContext ctx) {
500 if (auto *list = std::get_if<List>(&result))
501 return list->getValue();
502
503 if (auto *tuple = std::get_if<Tuple>(&result))
504 return tuple->getValue();
505
506 if (auto *map = std::get_if<Map>(&result))
507 return map->getValue();
508
509 if (auto *basePath = std::get_if<BasePath>(&result))
510 return basePath->getValue();
511
512 if (auto *path = std::get_if<Path>(&result))
513 return path->getValue();
514
515 if (auto *object = std::get_if<Object>(&result))
516 return object->getValue();
517
518 auto primitive = std::get<PythonPrimitive>(result);
520 omPythonValueToPrimitive(primitive, ctx));
521}
522
523} // namespace
524
525/// Populate the OM Python module.
527 m.doc() = "OM dialect Python native extension";
528
529 // Add the Evaluator class definition.
530 nb::class_<Evaluator>(m, "Evaluator")
531 .def(nb::init<MlirModule>(), nb::arg("module"))
532 .def("instantiate", &Evaluator::instantiate, "Instantiate an Object",
533 nb::arg("class_name"), nb::arg("actual_params"))
534 .def_prop_ro("module", &Evaluator::getModule,
535 "The Module the Evaluator is built from");
536
537 // Add the List class definition.
538 nb::class_<List>(m, "List")
539 .def(nb::init<List>(), nb::arg("list"))
540 .def("__getitem__", &List::getElement)
541 .def("__len__", &List::getNumElements);
542
543 nb::class_<Tuple>(m, "Tuple")
544 .def(nb::init<Tuple>(), nb::arg("tuple"))
545 .def("__getitem__", &Tuple::getElement)
546 .def("__len__", &Tuple::getNumElements);
547
548 // Add the Map class definition.
549 nb::class_<Map>(m, "Map")
550 .def(nb::init<Map>(), nb::arg("map"))
551 .def("__getitem__", &Map::dunderGetItem)
552 .def("keys", &Map::getKeys)
553 .def_prop_ro("type", &Map::getType, "The Type of the Map");
554
555 // Add the BasePath class definition.
556 nb::class_<BasePath>(m, "BasePath")
557 .def(nb::init<BasePath>(), nb::arg("basepath"))
558 .def_static("get_empty", &BasePath::getEmpty,
559 nb::arg("context") = nb::none());
560
561 // Add the Path class definition.
562 nb::class_<Path>(m, "Path")
563 .def(nb::init<Path>(), nb::arg("path"))
564 .def("__str__", &Path::dunderStr);
565
566 // Add the Object class definition.
567 nb::class_<Object>(m, "Object")
568 .def(nb::init<Object>(), nb::arg("object"))
569 .def("__getattr__", &Object::getField, "Get a field from an Object",
570 nb::arg("name"))
571 .def("get_field_loc", &Object::getFieldLoc,
572 "Get the location of a field from an Object", nb::arg("name"))
573 .def_prop_ro("field_names", &Object::getFieldNames,
574 "Get field names from an Object")
575 .def_prop_ro("type", &Object::getType, "The Type of the Object")
576 .def_prop_ro("loc", &Object::getLocation, "The Location of the Object")
577 .def("__hash__", &Object::getHash, "Get object hash")
578 .def("__eq__", &Object::eq, "Check if two objects are same");
579
580 // Add the ReferenceAttr definition
581 mlir_attribute_subclass(m, "ReferenceAttr", omAttrIsAReferenceAttr)
582 .def_property_readonly("inner_ref", [](MlirAttribute self) {
583 return omReferenceAttrGetInnerRef(self);
584 });
585
586 // Add the IntegerAttr definition
587 mlir_attribute_subclass(m, "OMIntegerAttr", omAttrIsAIntegerAttr)
588 .def_classmethod("get",
589 [](nb::object cls, MlirAttribute intVal) {
590 return cls(omIntegerAttrGet(intVal));
591 })
592 .def_property_readonly(
593 "integer",
594 [](MlirAttribute self) { return omIntegerAttrGetInt(self); })
595 .def("__str__", [](MlirAttribute self) {
596 MlirStringRef str = omIntegerAttrToString(self);
597 return std::string(str.data, str.length);
598 });
599
600 // Add the OMListAttr definition
601 mlir_attribute_subclass(m, "ListAttr", omAttrIsAListAttr)
602 .def("__getitem__", &omListAttrGetElement)
603 .def("__len__", &omListAttrGetNumElements)
604 .def("__iter__",
605 [](MlirAttribute arr) { return PyListAttrIterator(arr); });
606 PyListAttrIterator::bind(m);
607
608 // Add the MapAttr definition
609 mlir_attribute_subclass(m, "MapAttr", omAttrIsAMapAttr)
610 .def("__iter__", [](MlirAttribute arr) { return PyMapAttrIterator(arr); })
611 .def("__len__", &omMapAttrGetNumElements);
612 PyMapAttrIterator::bind(m);
613
614 // Add the AnyType class definition.
615 mlir_type_subclass(m, "AnyType", omTypeIsAAnyType, omAnyTypeGetTypeID);
616
617 // Add the ClassType class definition.
618 mlir_type_subclass(m, "ClassType", omTypeIsAClassType, omClassTypeGetTypeID)
619 .def_property_readonly("name", [](MlirType type) {
620 MlirStringRef name = mlirIdentifierStr(omClassTypeGetName(type));
621 return std::string(name.data, name.length);
622 });
623
624 // Add the BasePathType class definition.
625 mlir_type_subclass(m, "BasePathType", omTypeIsAFrozenBasePathType,
627
628 // Add the ListType class definition.
629 mlir_type_subclass(m, "ListType", omTypeIsAListType, omListTypeGetTypeID)
630 .def_property_readonly("element_type", omListTypeGetElementType);
631
632 // Add the PathType class definition.
633 mlir_type_subclass(m, "PathType", omTypeIsAFrozenPathType,
635}
assert(baseType &&"element must be base type")
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetModule(MlirAttribute)
Definition HW.cpp:245
MLIR_CAPI_EXPORTED MlirAttribute hwInnerRefAttrGetName(MlirAttribute)
Definition HW.cpp:241
MLIR_CAPI_EXPORTED bool omEvaluatorObjectIsEq(OMEvaluatorValue object, OMEvaluatorValue other)
Check equality of two objects.
Definition OM.cpp:183
MLIR_CAPI_EXPORTED intptr_t omListAttrGetNumElements(MlirAttribute attr)
Definition OM.cpp:419
MLIR_CAPI_EXPORTED unsigned omEvaluatorObjectGetHash(OMEvaluatorValue object)
Get the object hash.
Definition OM.cpp:178
MLIR_CAPI_EXPORTED bool omTypeIsAFrozenBasePathType(MlirType type)
Is the Type a FrozenBasePathType.
Definition OM.cpp:50
MLIR_CAPI_EXPORTED MlirTypeID omListTypeGetTypeID(void)
Get the TypeID for a ListType.
Definition OM.cpp:73
MLIR_CAPI_EXPORTED bool omAttrIsAListAttr(MlirAttribute attr)
Definition OM.cpp:415
MLIR_CAPI_EXPORTED bool omTypeIsAListType(MlirType type)
Is the Type a ListType.
Definition OM.cpp:70
MLIR_CAPI_EXPORTED MlirTypeID omFrozenPathTypeGetTypeID(void)
Get the TypeID for a FrozenPathType.
Definition OM.cpp:65
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsABasePath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a BasePath.
Definition OM.cpp:328
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsNull(OMEvaluatorValue evaluatorValue)
Definition OM.cpp:234
MLIR_CAPI_EXPORTED MlirAttribute omMapAttrGetElementValue(MlirAttribute attr, intptr_t pos)
Definition OM.cpp:445
MLIR_CAPI_EXPORTED intptr_t omEvaluatorTupleGetNumElements(OMEvaluatorValue evaluatorValue)
Get the size of the tuple.
Definition OM.cpp:299
MLIR_CAPI_EXPORTED OMEvaluator omEvaluatorNew(MlirModule mod)
Construct an Evaluator with an IR module.
Definition OM.cpp:122
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorValueGetReferenceValue(OMEvaluatorValue evaluatorValue)
Dereference a Reference EvaluatorValue.
Definition OM.cpp:353
MLIR_CAPI_EXPORTED MlirLocation omEvaluatorValueGetLoc(OMEvaluatorValue evaluatorValue)
Definition OM.cpp:229
MLIR_CAPI_EXPORTED MlirAttribute omEvaluatorValueGetPrimitive(OMEvaluatorValue evaluatorValue)
Get the Primitive from an EvaluatorValue, which must contain a Primitive.
Definition OM.cpp:252
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorBasePathGetEmpty(MlirContext context)
Create an empty BasePath.
Definition OM.cpp:332
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAPath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Path.
Definition OM.cpp:336
MLIR_CAPI_EXPORTED bool omTypeIsAAnyType(MlirType type)
Is the Type an AnyType.
Definition OM.cpp:33
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAMap(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Map.
Definition OM.cpp:324
MLIR_CAPI_EXPORTED MlirTypeID omFrozenBasePathTypeGetTypeID(void)
Get the TypeID for a FrozenBasePathType.
Definition OM.cpp:55
MLIR_CAPI_EXPORTED MlirModule omEvaluatorGetModule(OMEvaluator evaluator)
Get the Module the Evaluator is built from.
Definition OM.cpp:157
MLIR_CAPI_EXPORTED MlirType omMapTypeGetKeyType(MlirType type)
Return a key type of a map.
Definition OM.cpp:91
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorInstantiate(OMEvaluator evaluator, MlirAttribute className, intptr_t nActualParams, OMEvaluatorValue *actualParams)
Use the Evaluator to Instantiate an Object from its class name and actual parameters.
Definition OM.cpp:129
MLIR_CAPI_EXPORTED MlirTypeID omClassTypeGetTypeID(void)
Get the TypeID for a ClassType.
Definition OM.cpp:42
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorMapGetElement(OMEvaluatorValue evaluatorValue, MlirAttribute attr)
Get an element of the map.
Definition OM.cpp:313
MLIR_CAPI_EXPORTED intptr_t omEvaluatorListGetNumElements(OMEvaluatorValue evaluatorValue)
Get the length of the list.
Definition OM.cpp:280
MLIR_CAPI_EXPORTED MlirIdentifier omClassTypeGetName(MlirType type)
Get the name for a ClassType.
Definition OM.cpp:45
MLIR_CAPI_EXPORTED bool omAttrIsAReferenceAttr(MlirAttribute attr)
Definition OM.cpp:374
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorTupleGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the tuple.
Definition OM.cpp:306
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAList(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is an Object.
Definition OM.cpp:267
MLIR_CAPI_EXPORTED MlirType omListTypeGetElementType(MlirType type)
Definition OM.cpp:76
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsATuple(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Tuple.
Definition OM.cpp:294
MLIR_CAPI_EXPORTED MlirType omEvaluatorMapGetType(OMEvaluatorValue evaluatorValue)
Get the Type from a Map, which will be a MapType.
Definition OM.cpp:193
MLIR_CAPI_EXPORTED bool omTypeIsAFrozenPathType(MlirType type)
Is the Type a FrozenPathType.
Definition OM.cpp:60
MLIR_CAPI_EXPORTED MlirType omEvaluatorObjectGetType(OMEvaluatorValue object)
Get the Type from an Object, which will be a ClassType.
Definition OM.cpp:173
MLIR_CAPI_EXPORTED bool omAttrIsAIntegerAttr(MlirAttribute attr)
Definition OM.cpp:387
MLIR_CAPI_EXPORTED bool omTypeIsAStringType(MlirType type)
Is the Type a StringType.
Definition OM.cpp:81
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorListGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the list.
Definition OM.cpp:287
MLIR_CAPI_EXPORTED bool omTypeIsAClassType(MlirType type)
Is the Type a ClassType.
Definition OM.cpp:39
MLIR_CAPI_EXPORTED bool omEvaluatorObjectIsNull(OMEvaluatorValue object)
Query if the Object is null.
Definition OM.cpp:167
MLIR_CAPI_EXPORTED intptr_t omMapAttrGetNumElements(MlirAttribute attr)
Definition OM.cpp:435
MLIR_CAPI_EXPORTED MlirStringRef omIntegerAttrToString(MlirAttribute attr)
Get a string representation of an om::IntegerAttr.
Definition OM.cpp:402
MLIR_CAPI_EXPORTED MlirIdentifier omMapAttrGetElementKey(MlirAttribute attr, intptr_t pos)
Definition OM.cpp:440
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorValueFromPrimitive(MlirAttribute primitive)
Get the EvaluatorValue from a Primitive value.
Definition OM.cpp:261
MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGet(MlirAttribute attr)
Get an om::IntegerAttr from mlir::IntegerAttr.
Definition OM.cpp:395
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAObject(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is an Object.
Definition OM.cpp:240
MLIR_CAPI_EXPORTED MlirAttribute omListAttrGetElement(MlirAttribute attr, intptr_t pos)
Definition OM.cpp:424
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAPrimitive(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Primitive.
Definition OM.cpp:246
MLIR_CAPI_EXPORTED OMEvaluatorValue omEvaluatorObjectGetField(OMEvaluatorValue object, MlirAttribute name)
Get a field from an Object, which must contain a field of that name.
Definition OM.cpp:203
MLIR_CAPI_EXPORTED MlirAttribute omEvaluatorMapGetKeys(OMEvaluatorValue object)
Get an ArrayAttr with the keys in a Map.
Definition OM.cpp:198
MLIR_CAPI_EXPORTED MlirContext omEvaluatorValueGetContext(OMEvaluatorValue evaluatorValue)
Definition OM.cpp:224
MLIR_CAPI_EXPORTED MlirAttribute omEvaluatorObjectGetFieldNames(OMEvaluatorValue object)
Get all the field names from an Object, can be empty if object has no fields.
Definition OM.cpp:189
MLIR_CAPI_EXPORTED bool omAttrIsAMapAttr(MlirAttribute attr)
Definition OM.cpp:433
MLIR_CAPI_EXPORTED bool omEvaluatorValueIsAReference(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Reference.
Definition OM.cpp:346
MLIR_CAPI_EXPORTED MlirTypeID omAnyTypeGetTypeID(void)
Get the TypeID for an AnyType.
Definition OM.cpp:36
MLIR_CAPI_EXPORTED MlirAttribute omEvaluatorPathGetAsString(OMEvaluatorValue evaluatorValue)
Get a string representation of a Path.
Definition OM.cpp:340
MLIR_CAPI_EXPORTED MlirAttribute omReferenceAttrGetInnerRef(MlirAttribute attr)
Definition OM.cpp:378
MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGetInt(MlirAttribute attr)
Given an om::IntegerAttr, return the mlir::IntegerAttr.
Definition OM.cpp:391
@ None
Don't preserve aggregate at all.
Definition Passes.h:33
evaluator::ObjectValue Object
Definition Evaluator.h:411
void populateDialectOMSubmodule(nanobind::module_ &m)
A value type for use in C APIs that just wraps a pointer to an Object.
Definition OM.h:90
A value type for use in C APIs that just wraps a pointer to an Evaluator.
Definition OM.h:79