10 #include <string_view>
17 #include "llvm/ADT/ScopeExit.h"
34 R
"(Gets a DenseElementsAttr from a Python buffer or array.
36 When `type` is not provided, then some limited type inferencing is done based
37 on the buffer format. Support presently exists for 8/16/32/64 signed and
38 unsigned integers and float16/float32/float64. DenseElementsAttrs of these
39 types can also be converted back to a corresponding buffer.
41 For conversions outside of these types, a `type=` must be explicitly provided
42 and the buffer contents must be bit-castable to the MLIR internal
45 * Integer types (except for i1): the buffer must be byte aligned to the
47 * Floating point types: Must be bit-castable to the given floating point
49 * i1 (bool): Bit packed into 8bit words where the bit pattern matches a
50 row major ordering. An arbitrary Numpy `bool_` array can be bit packed to
51 this specification with: `np.packbits(ary, axis=None, bitorder='little')`.
53 If a single element buffer is passed (or for i1, a single byte with value 0
54 or 255), then a splat will be created.
57 array: The array or buffer to convert.
58 signless: If inferring an appropriate MLIR type, use signless types for
59 integers (defaults True).
60 type: Skips inference of the MLIR element type and uses this instead. The
61 storage size must be consistent with the actual contents of the buffer.
62 shape: Overrides the shape of the buffer when constructing the MLIR
63 shaped type. This is needed when the physical and logical shape differ (as
65 context: Explicit context, if not from context manager.
68 DenseElementsAttr on success.
71 ValueError: If the type of the buffer or array cannot be matched to an MLIR
72 type or if the buffer does not meet expectations.
76 R
"(Gets a DenseResourceElementsAttr from a Python buffer or array.
78 This function does minimal validation or massaging of the data, and it is
79 up to the caller to ensure that the buffer meets the characteristics
82 The backing buffer and any user objects will be retained for the lifetime
83 of the resource blob. This is typically bounded to the context but the
84 resource can have a shorter lifespan depending on how it is used in
85 subsequent processing.
88 buffer: The array or buffer to convert.
89 name: Name to provide to the resource (may be changed upon collision).
90 type: The explicit ShapedType to construct the attribute with.
91 context: Explicit context, if not from context manager.
94 DenseResourceElementsAttr on success.
97 ValueError: If the type of the buffer or array cannot be matched to an MLIR
98 type or if the buffer does not meet expectations.
110 static constexpr
const char *pyClassName =
"AffineMapAttr";
112 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
115 static void bindDerived(ClassTy &c) {
120 return PyAffineMapAttribute(affineMap.
getContext(), attr);
122 py::arg(
"affine_map"),
"Gets an attribute wrapping an AffineMap.");
126 template <
typename T>
127 static T pyTryCast(py::handle
object) {
129 return object.cast<T>();
130 }
catch (py::cast_error &err) {
133 "Invalid attribute when attempting to create an ArrayAttribute (") +
135 throw py::cast_error(msg);
136 }
catch (py::reference_cast_error &err) {
137 std::string msg = std::string(
"Invalid attribute (None?) when attempting "
138 "to create an ArrayAttribute (") +
140 throw py::cast_error(msg);
146 template <
typename EltTy,
typename DerivedT>
152 class PyDenseArrayIterator {
154 PyDenseArrayIterator(
PyAttribute attr) : attr(std::move(attr)) {}
157 PyDenseArrayIterator dunderIter() {
return *
this; }
163 throw py::stop_iteration();
168 static void bind(py::module &m) {
169 py::class_<PyDenseArrayIterator>(m, DerivedT::pyIteratorName,
171 .def(
"__iter__", &PyDenseArrayIterator::dunderIter)
172 .def(
"__next__", &PyDenseArrayIterator::dunderNext);
183 EltTy getItem(intptr_t i) {
return DerivedT::getElement(*
this, i); }
191 return getAttribute(values, ctx->getRef());
193 py::arg(
"values"), py::arg(
"context") = py::none(),
194 "Gets a uniqued dense array attribute");
196 c.def(
"__getitem__", [](DerivedT &arr, intptr_t i) {
198 throw py::index_error(
"DenseArray index out of range");
199 return arr.getItem(i);
201 c.def(
"__len__", [](
const DerivedT &arr) {
205 [](
const DerivedT &arr) {
return PyDenseArrayIterator(arr); });
206 c.def(
"__add__", [](DerivedT &arr,
const py::list &extras) {
207 std::vector<EltTy> values;
209 values.reserve(numOldElements + py::len(extras));
210 for (intptr_t i = 0; i < numOldElements; ++i)
211 values.push_back(arr.getItem(i));
212 for (py::handle attr : extras)
213 values.push_back(pyTryCast<EltTy>(attr));
214 return getAttribute(values, arr.getContext());
219 static DerivedT getAttribute(
const std::vector<EltTy> &values,
221 if constexpr (std::is_same_v<EltTy, bool>) {
222 std::vector<int> intValues(values.begin(), values.end());
223 MlirAttribute attr = DerivedT::getAttribute(ctx->
get(), intValues.size(),
225 return DerivedT(ctx, attr);
228 DerivedT::getAttribute(ctx->
get(), values.size(), values.data());
229 return DerivedT(ctx, attr);
235 struct PyDenseBoolArrayAttribute
236 :
public PyDenseArrayAttribute<bool, PyDenseBoolArrayAttribute> {
240 static constexpr
const char *pyClassName =
"DenseBoolArrayAttr";
241 static constexpr
const char *pyIteratorName =
"DenseBoolArrayIterator";
242 using PyDenseArrayAttribute::PyDenseArrayAttribute;
244 struct PyDenseI8ArrayAttribute
245 :
public PyDenseArrayAttribute<int8_t, PyDenseI8ArrayAttribute> {
249 static constexpr
const char *pyClassName =
"DenseI8ArrayAttr";
250 static constexpr
const char *pyIteratorName =
"DenseI8ArrayIterator";
251 using PyDenseArrayAttribute::PyDenseArrayAttribute;
253 struct PyDenseI16ArrayAttribute
254 :
public PyDenseArrayAttribute<int16_t, PyDenseI16ArrayAttribute> {
258 static constexpr
const char *pyClassName =
"DenseI16ArrayAttr";
259 static constexpr
const char *pyIteratorName =
"DenseI16ArrayIterator";
260 using PyDenseArrayAttribute::PyDenseArrayAttribute;
262 struct PyDenseI32ArrayAttribute
263 :
public PyDenseArrayAttribute<int32_t, PyDenseI32ArrayAttribute> {
267 static constexpr
const char *pyClassName =
"DenseI32ArrayAttr";
268 static constexpr
const char *pyIteratorName =
"DenseI32ArrayIterator";
269 using PyDenseArrayAttribute::PyDenseArrayAttribute;
271 struct PyDenseI64ArrayAttribute
272 :
public PyDenseArrayAttribute<int64_t, PyDenseI64ArrayAttribute> {
276 static constexpr
const char *pyClassName =
"DenseI64ArrayAttr";
277 static constexpr
const char *pyIteratorName =
"DenseI64ArrayIterator";
278 using PyDenseArrayAttribute::PyDenseArrayAttribute;
280 struct PyDenseF32ArrayAttribute
281 :
public PyDenseArrayAttribute<float, PyDenseF32ArrayAttribute> {
285 static constexpr
const char *pyClassName =
"DenseF32ArrayAttr";
286 static constexpr
const char *pyIteratorName =
"DenseF32ArrayIterator";
287 using PyDenseArrayAttribute::PyDenseArrayAttribute;
289 struct PyDenseF64ArrayAttribute
290 :
public PyDenseArrayAttribute<double, PyDenseF64ArrayAttribute> {
294 static constexpr
const char *pyClassName =
"DenseF64ArrayAttr";
295 static constexpr
const char *pyIteratorName =
"DenseF64ArrayIterator";
296 using PyDenseArrayAttribute::PyDenseArrayAttribute;
302 static constexpr
const char *pyClassName =
"ArrayAttr";
304 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
307 class PyArrayAttributeIterator {
309 PyArrayAttributeIterator(
PyAttribute attr) : attr(std::move(attr)) {}
311 PyArrayAttributeIterator &dunderIter() {
return *
this; }
313 MlirAttribute dunderNext() {
316 throw py::stop_iteration();
320 static void bind(py::module &m) {
321 py::class_<PyArrayAttributeIterator>(m,
"ArrayAttributeIterator",
323 .def(
"__iter__", &PyArrayAttributeIterator::dunderIter)
324 .def(
"__next__", &PyArrayAttributeIterator::dunderNext);
332 MlirAttribute getItem(intptr_t i) {
336 static void bindDerived(ClassTy &c) {
341 mlirAttributes.reserve(py::len(attributes));
342 for (
auto attribute : attributes) {
343 mlirAttributes.push_back(pyTryCast<PyAttribute>(attribute));
346 context->
get(), mlirAttributes.size(), mlirAttributes.data());
347 return PyArrayAttribute(context->getRef(), attr);
349 py::arg(
"attributes"), py::arg(
"context") = py::none(),
350 "Gets a uniqued Array attribute");
352 [](PyArrayAttribute &arr, intptr_t i) {
354 throw py::index_error(
"ArrayAttribute index out of range");
355 return arr.getItem(i);
358 [](
const PyArrayAttribute &arr) {
361 .def(
"__iter__", [](
const PyArrayAttribute &arr) {
362 return PyArrayAttributeIterator(arr);
364 c.def(
"__add__", [](PyArrayAttribute arr, py::list extras) {
365 std::vector<MlirAttribute> attributes;
367 attributes.reserve(numOldElements + py::len(extras));
368 for (intptr_t i = 0; i < numOldElements; ++i)
369 attributes.push_back(arr.getItem(i));
370 for (py::handle attr : extras)
371 attributes.push_back(pyTryCast<PyAttribute>(attr));
373 arr.getContext()->get(), attributes.size(), attributes.data());
374 return PyArrayAttribute(arr.getContext(), arrayAttr);
383 static constexpr
const char *pyClassName =
"FloatAttr";
385 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
388 static void bindDerived(ClassTy &c) {
396 return PyFloatAttribute(type.
getContext(), attr);
398 py::arg(
"type"), py::arg(
"value"), py::arg(
"loc") = py::none(),
399 "Gets an uniqued float point attribute associated to a type");
405 return PyFloatAttribute(context->getRef(), attr);
407 py::arg(
"value"), py::arg(
"context") = py::none(),
408 "Gets an uniqued float point attribute associated to a f32 type");
414 return PyFloatAttribute(context->getRef(), attr);
416 py::arg(
"value"), py::arg(
"context") = py::none(),
417 "Gets an uniqued float point attribute associated to a f64 type");
419 "Returns the value of the float attribute");
421 "Converts the value of the float attribute to a Python float");
429 static constexpr
const char *pyClassName =
"IntegerAttr";
432 static void bindDerived(ClassTy &c) {
435 [](
PyType &type, int64_t value) {
437 return PyIntegerAttribute(type.
getContext(), attr);
439 py::arg(
"type"), py::arg(
"value"),
440 "Gets an uniqued integer attribute associated to a type");
441 c.def_property_readonly(
"value", toPyInt,
442 "Returns the value of the integer attribute");
443 c.def(
"__int__", toPyInt,
444 "Converts the value of the integer attribute to a Python int");
445 c.def_property_readonly_static(
"static_typeid",
446 [](py::object & ) -> MlirTypeID {
452 static py::int_ toPyInt(PyIntegerAttribute &
self) {
466 static constexpr
const char *pyClassName =
"BoolAttr";
469 static void bindDerived(ClassTy &c) {
474 return PyBoolAttribute(context->getRef(), attr);
476 py::arg(
"value"), py::arg(
"context") = py::none(),
477 "Gets an uniqued bool attribute");
479 "Returns the value of the bool attribute");
481 "Converts the value of the bool attribute to a Python bool");
488 static constexpr
const char *pyClassName =
"SymbolRefAttr";
491 static MlirAttribute fromList(
const std::vector<std::string> &symbols,
494 throw std::runtime_error(
"SymbolRefAttr must be composed of at least "
498 for (
size_t i = 1; i < symbols.size(); ++i) {
499 referenceAttrs.push_back(
503 referenceAttrs.size(), referenceAttrs.data());
506 static void bindDerived(ClassTy &c) {
509 [](
const std::vector<std::string> &symbols,
511 return PySymbolRefAttribute::fromList(symbols, context.
resolve());
513 py::arg(
"symbols"), py::arg(
"context") = py::none(),
514 "Gets a uniqued SymbolRef attribute from a list of symbol names");
515 c.def_property_readonly(
517 [](PySymbolRefAttribute &
self) {
518 std::vector<std::string> symbols = {
528 "Returns the value of the SymbolRef attribute as a list[str]");
532 class PyFlatSymbolRefAttribute
536 static constexpr
const char *pyClassName =
"FlatSymbolRefAttr";
539 static void bindDerived(ClassTy &c) {
545 return PyFlatSymbolRefAttribute(context->getRef(), attr);
547 py::arg(
"value"), py::arg(
"context") = py::none(),
548 "Gets a uniqued FlatSymbolRef attribute");
549 c.def_property_readonly(
551 [](PyFlatSymbolRefAttribute &
self) {
553 return py::str(stringRef.
data, stringRef.
length);
555 "Returns the value of the FlatSymbolRef attribute as a string");
562 static constexpr
const char *pyClassName =
"OpaqueAttr";
564 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
567 static void bindDerived(ClassTy &c) {
570 [](std::string dialectNamespace, py::buffer buffer,
PyType &type,
572 const py::buffer_info bufferInfo = buffer.request();
573 intptr_t bufferSize = bufferInfo.size;
576 static_cast<char *
>(bufferInfo.ptr), type);
577 return PyOpaqueAttribute(context->getRef(), attr);
579 py::arg(
"dialect_namespace"), py::arg(
"buffer"), py::arg(
"type"),
580 py::arg(
"context") = py::none(),
"Gets an Opaque attribute.");
581 c.def_property_readonly(
583 [](PyOpaqueAttribute &
self) {
585 return py::str(stringRef.
data, stringRef.
length);
587 "Returns the dialect namespace for the Opaque attribute as a string");
588 c.def_property_readonly(
590 [](PyOpaqueAttribute &
self) {
592 return py::bytes(stringRef.
data, stringRef.
length);
594 "Returns the data for the Opaqued attributes as `bytes`");
601 static constexpr
const char *pyClassName =
"StringAttr";
603 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
606 static void bindDerived(ClassTy &c) {
612 return PyStringAttribute(context->getRef(), attr);
614 py::arg(
"value"), py::arg(
"context") = py::none(),
615 "Gets a uniqued string attribute");
618 [](
PyType &type, std::string value) {
621 return PyStringAttribute(type.
getContext(), attr);
623 py::arg(
"type"), py::arg(
"value"),
624 "Gets a uniqued string attribute associated to a type");
625 c.def_property_readonly(
627 [](PyStringAttribute &
self) {
629 return py::str(stringRef.
data, stringRef.
length);
631 "Returns the value of the string attribute");
632 c.def_property_readonly(
634 [](PyStringAttribute &
self) {
636 return py::bytes(stringRef.
data, stringRef.
length);
638 "Returns the value of the string attribute as `bytes`");
643 class PyDenseElementsAttribute
647 static constexpr
const char *pyClassName =
"DenseElementsAttr";
650 static PyDenseElementsAttribute
651 getFromBuffer(py::buffer array,
bool signless,
652 std::optional<PyType> explicitType,
653 std::optional<std::vector<int64_t>> explicitShape,
656 int flags = PyBUF_ND;
658 flags |= PyBUF_FORMAT;
661 if (PyObject_GetBuffer(array.ptr(), &view, flags) != 0) {
662 throw py::error_already_set();
664 auto freeBuffer = llvm::make_scope_exit([&]() { PyBuffer_Release(&view); });
667 shape.append(explicitShape->begin(), explicitShape->end());
669 shape.append(view.shape, view.shape + view.ndim);
673 MlirContext context = contextWrapper->
get();
680 std::optional<MlirType> bulkLoadElementType;
682 bulkLoadElementType = *explicitType;
684 std::string_view format(view.format);
687 assert(view.itemsize == 4 &&
"mismatched array itemsize");
689 }
else if (format ==
"d") {
691 assert(view.itemsize == 8 &&
"mismatched array itemsize");
693 }
else if (format ==
"e") {
695 assert(view.itemsize == 2 &&
"mismatched array itemsize");
697 }
else if (isSignedIntegerFormat(format)) {
698 if (view.itemsize == 4) {
700 bulkLoadElementType = signless
703 }
else if (view.itemsize == 8) {
705 bulkLoadElementType = signless
708 }
else if (view.itemsize == 1) {
712 }
else if (view.itemsize == 2) {
714 bulkLoadElementType = signless
718 }
else if (isUnsignedIntegerFormat(format)) {
719 if (view.itemsize == 4) {
721 bulkLoadElementType = signless
724 }
else if (view.itemsize == 8) {
726 bulkLoadElementType = signless
729 }
else if (view.itemsize == 1) {
731 bulkLoadElementType = signless
734 }
else if (view.itemsize == 2) {
736 bulkLoadElementType = signless
741 if (!bulkLoadElementType) {
742 throw std::invalid_argument(
743 std::string(
"unimplemented array format conversion from format: ") +
744 std::string(format));
751 throw std::invalid_argument(
"Shape can only be specified explicitly "
752 "when the type is not a shaped type.");
754 shapedType = *bulkLoadElementType;
757 *bulkLoadElementType, encodingAttr);
759 size_t rawBufferSize = view.len;
763 throw std::invalid_argument(
764 "DenseElementsAttr could not be constructed from the given buffer. "
765 "This may mean that the Python buffer layout does not match that "
766 "MLIR expected layout and is a bug.");
768 return PyDenseElementsAttribute(contextWrapper->getRef(), attr);
771 static PyDenseElementsAttribute getSplat(
const PyType &shapedType,
773 auto contextWrapper =
777 std::string message =
"Illegal element type for DenseElementsAttr: ";
778 message.append(py::repr(py::cast(elementAttr)));
779 throw py::value_error(message);
783 std::string message =
784 "Expected a static ShapedType for the shaped_type parameter: ";
785 message.append(py::repr(py::cast(shapedType)));
786 throw py::value_error(message);
791 std::string message =
792 "Shaped element type and attribute type must be equal: shaped=";
793 message.append(py::repr(py::cast(shapedType)));
794 message.append(
", element=");
795 message.append(py::repr(py::cast(elementAttr)));
796 throw py::value_error(message);
799 MlirAttribute elements =
801 return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
806 py::buffer_info accessBuffer() {
813 return bufferInfo<float>(shapedType);
817 return bufferInfo<double>(shapedType);
821 return bufferInfo<uint16_t>(shapedType,
"e");
825 return bufferInfo<int64_t>(shapedType);
832 return bufferInfo<int32_t>(shapedType);
836 return bufferInfo<uint32_t>(shapedType);
843 return bufferInfo<int64_t>(shapedType);
847 return bufferInfo<uint64_t>(shapedType);
854 return bufferInfo<int8_t>(shapedType);
858 return bufferInfo<uint8_t>(shapedType);
865 return bufferInfo<int16_t>(shapedType);
869 return bufferInfo<uint16_t>(shapedType);
875 throw std::invalid_argument(
876 "unsupported data type for conversion to Python buffer");
879 static void bindDerived(ClassTy &c) {
880 c.def(
"__len__", &PyDenseElementsAttribute::dunderLen)
881 .def_static(
"get", PyDenseElementsAttribute::getFromBuffer,
882 py::arg(
"array"), py::arg(
"signless") =
true,
883 py::arg(
"type") = py::none(), py::arg(
"shape") = py::none(),
884 py::arg(
"context") = py::none(),
886 .def_static(
"get_splat", PyDenseElementsAttribute::getSplat,
887 py::arg(
"shaped_type"), py::arg(
"element_attr"),
888 "Gets a DenseElementsAttr where all values are the same")
889 .def_property_readonly(
"is_splat",
890 [](PyDenseElementsAttribute &
self) ->
bool {
893 .def(
"get_splat_value",
894 [](PyDenseElementsAttribute &
self) {
896 throw py::value_error(
897 "get_splat_value called on a non-splat attribute");
900 .def_buffer(&PyDenseElementsAttribute::accessBuffer);
904 static bool isUnsignedIntegerFormat(std::string_view format) {
907 char code = format[0];
908 return code ==
'I' || code ==
'B' || code ==
'H' || code ==
'L' ||
912 static bool isSignedIntegerFormat(std::string_view format) {
915 char code = format[0];
916 return code ==
'i' || code ==
'b' || code ==
'h' || code ==
'l' ||
920 template <
typename Type>
921 py::buffer_info bufferInfo(MlirType shapedType,
922 const char *explicitFormat =
nullptr) {
930 for (intptr_t i = 0; i < rank; ++i)
936 strides.assign(rank, 0);
938 for (intptr_t i = 1; i < rank; ++i) {
939 intptr_t strideFactor = 1;
940 for (intptr_t
j = i;
j < rank; ++
j)
942 strides.push_back(
sizeof(
Type) * strideFactor);
944 strides.push_back(
sizeof(
Type));
947 if (explicitFormat) {
948 format = explicitFormat;
950 format = py::format_descriptor<Type>::format();
952 return py::buffer_info(data,
sizeof(
Type), format, rank, shape, strides,
959 class PyDenseIntElementsAttribute
961 PyDenseElementsAttribute> {
964 static constexpr
const char *pyClassName =
"DenseIntElementsAttr";
969 py::int_ dunderGetItem(intptr_t pos) {
970 if (pos < 0 || pos >= dunderLen()) {
971 throw py::index_error(
"attempt to access out of bounds element");
977 "expected integer element type in dense int elements attribute");
1018 throw py::type_error(
"Unsupported integer type");
1021 static void bindDerived(ClassTy &c) {
1022 c.def(
"__getitem__", &PyDenseIntElementsAttribute::dunderGetItem);
1026 class PyDenseResourceElementsAttribute
1029 static constexpr IsAFunctionTy isaFunction =
1031 static constexpr
const char *pyClassName =
"DenseResourceElementsAttr";
1034 static PyDenseResourceElementsAttribute
1035 getFromBuffer(py::buffer buffer,
const std::string &name,
const PyType &type,
1036 std::optional<size_t> alignment,
bool isMutable,
1039 throw std::invalid_argument(
1040 "Constructing a DenseResourceElementsAttr requires a ShapedType.");
1045 int flags = PyBUF_STRIDES;
1046 std::unique_ptr<Py_buffer> view = std::make_unique<Py_buffer>();
1047 if (PyObject_GetBuffer(buffer.ptr(), view.get(), flags) != 0) {
1048 throw py::error_already_set();
1053 auto freeBuffer = llvm::make_scope_exit([&]() {
1055 PyBuffer_Release(view.get());
1058 if (!PyBuffer_IsContiguous(view.get(),
'A')) {
1059 throw std::invalid_argument(
"Contiguous buffer is required.");
1063 size_t inferredAlignment;
1065 inferredAlignment = *alignment;
1067 inferredAlignment = view->strides[view->ndim - 1];
1070 auto deleter = [](
void *userData,
const void *data,
size_t size,
1072 Py_buffer *ownedView =
static_cast<Py_buffer *
>(userData);
1073 PyBuffer_Release(ownedView);
1077 size_t rawBufferSize = view->len;
1080 inferredAlignment, isMutable, deleter,
static_cast<void *
>(view.get()));
1082 throw std::invalid_argument(
1083 "DenseResourceElementsAttr could not be constructed from the given "
1085 "This may mean that the Python buffer layout does not match that "
1086 "MLIR expected layout and is a bug.");
1089 return PyDenseResourceElementsAttribute(contextWrapper->getRef(), attr);
1092 static void bindDerived(ClassTy &c) {
1093 c.def_static(
"get_from_buffer",
1094 PyDenseResourceElementsAttribute::getFromBuffer,
1095 py::arg(
"array"), py::arg(
"name"), py::arg(
"type"),
1096 py::arg(
"alignment") = py::none(),
1097 py::arg(
"is_mutable") =
false, py::arg(
"context") = py::none(),
1105 static constexpr
const char *pyClassName =
"DictAttr";
1107 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1112 bool dunderContains(
const std::string &name) {
1117 static void bindDerived(ClassTy &c) {
1118 c.def(
"__contains__", &PyDictAttribute::dunderContains);
1119 c.def(
"__len__", &PyDictAttribute::dunderLen);
1124 mlirNamedAttributes.reserve(attributes.size());
1125 for (
auto &it : attributes) {
1127 auto name = it.first.cast<std::string>();
1133 MlirAttribute attr =
1135 mlirNamedAttributes.data());
1136 return PyDictAttribute(context->getRef(), attr);
1138 py::arg(
"value") = py::dict(), py::arg(
"context") = py::none(),
1139 "Gets an uniqued dict attribute");
1140 c.def(
"__getitem__", [](PyDictAttribute &
self,
const std::string &name) {
1141 MlirAttribute attr =
1144 throw py::key_error(
"attempt to access a non-existent attribute");
1147 c.def(
"__getitem__", [](PyDictAttribute &
self, intptr_t index) {
1148 if (index < 0 || index >=
self.dunderLen()) {
1149 throw py::index_error(
"attempt to access out of bounds attribute");
1161 class PyDenseFPElementsAttribute
1163 PyDenseElementsAttribute> {
1166 static constexpr
const char *pyClassName =
"DenseFPElementsAttr";
1169 py::float_ dunderGetItem(intptr_t pos) {
1170 if (pos < 0 || pos >= dunderLen()) {
1171 throw py::index_error(
"attempt to access out of bounds element");
1187 throw py::type_error(
"Unsupported floating-point type");
1190 static void bindDerived(ClassTy &c) {
1191 c.def(
"__getitem__", &PyDenseFPElementsAttribute::dunderGetItem);
1198 static constexpr
const char *pyClassName =
"TypeAttr";
1200 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1203 static void bindDerived(ClassTy &c) {
1208 return PyTypeAttribute(context->getRef(), attr);
1210 py::arg(
"value"), py::arg(
"context") = py::none(),
1211 "Gets a uniqued Type attribute");
1212 c.def_property_readonly(
"value", [](PyTypeAttribute &
self) {
1222 static constexpr
const char *pyClassName =
"UnitAttr";
1224 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1227 static void bindDerived(ClassTy &c) {
1231 return PyUnitAttribute(context->getRef(),
1234 py::arg(
"context") = py::none(),
"Create a Unit attribute.");
1239 class PyStridedLayoutAttribute
1243 static constexpr
const char *pyClassName =
"StridedLayoutAttr";
1245 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1248 static void bindDerived(ClassTy &c) {
1251 [](int64_t offset,
const std::vector<int64_t> strides,
1254 ctx->
get(), offset, strides.size(), strides.data());
1255 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1257 py::arg(
"offset"), py::arg(
"strides"), py::arg(
"context") = py::none(),
1258 "Gets a strided layout attribute.");
1260 "get_fully_dynamic",
1263 std::vector<int64_t> strides(rank);
1264 std::fill(strides.begin(), strides.end(), dynamic);
1266 ctx->
get(), dynamic, strides.size(), strides.data());
1267 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1269 py::arg(
"rank"), py::arg(
"context") = py::none(),
1270 "Gets a strided layout attribute with dynamic offset and strides of a "
1272 c.def_property_readonly(
1274 [](PyStridedLayoutAttribute &
self) {
1277 "Returns the value of the float point attribute");
1278 c.def_property_readonly(
1280 [](PyStridedLayoutAttribute &
self) {
1282 std::vector<int64_t> strides(size);
1283 for (intptr_t i = 0; i < size; i++) {
1288 "Returns the value of the float point attribute");
1292 py::object denseArrayAttributeCaster(
PyAttribute &pyAttribute) {
1293 if (PyDenseBoolArrayAttribute::isaFunction(pyAttribute))
1294 return py::cast(PyDenseBoolArrayAttribute(pyAttribute));
1295 if (PyDenseI8ArrayAttribute::isaFunction(pyAttribute))
1296 return py::cast(PyDenseI8ArrayAttribute(pyAttribute));
1297 if (PyDenseI16ArrayAttribute::isaFunction(pyAttribute))
1298 return py::cast(PyDenseI16ArrayAttribute(pyAttribute));
1299 if (PyDenseI32ArrayAttribute::isaFunction(pyAttribute))
1300 return py::cast(PyDenseI32ArrayAttribute(pyAttribute));
1301 if (PyDenseI64ArrayAttribute::isaFunction(pyAttribute))
1302 return py::cast(PyDenseI64ArrayAttribute(pyAttribute));
1303 if (PyDenseF32ArrayAttribute::isaFunction(pyAttribute))
1304 return py::cast(PyDenseF32ArrayAttribute(pyAttribute));
1305 if (PyDenseF64ArrayAttribute::isaFunction(pyAttribute))
1306 return py::cast(PyDenseF64ArrayAttribute(pyAttribute));
1308 std::string(
"Can't cast unknown element type DenseArrayAttr (") +
1309 std::string(py::repr(py::cast(pyAttribute))) +
")";
1310 throw py::cast_error(msg);
1313 py::object denseIntOrFPElementsAttributeCaster(
PyAttribute &pyAttribute) {
1314 if (PyDenseFPElementsAttribute::isaFunction(pyAttribute))
1315 return py::cast(PyDenseFPElementsAttribute(pyAttribute));
1316 if (PyDenseIntElementsAttribute::isaFunction(pyAttribute))
1317 return py::cast(PyDenseIntElementsAttribute(pyAttribute));
1320 "Can't cast unknown element type DenseIntOrFPElementsAttr (") +
1321 std::string(py::repr(py::cast(pyAttribute))) +
")";
1322 throw py::cast_error(msg);
1325 py::object integerOrBoolAttributeCaster(
PyAttribute &pyAttribute) {
1326 if (PyBoolAttribute::isaFunction(pyAttribute))
1327 return py::cast(PyBoolAttribute(pyAttribute));
1328 if (PyIntegerAttribute::isaFunction(pyAttribute))
1329 return py::cast(PyIntegerAttribute(pyAttribute));
1331 std::string(
"Can't cast unknown element type DenseArrayAttr (") +
1332 std::string(py::repr(py::cast(pyAttribute))) +
")";
1333 throw py::cast_error(msg);
1336 py::object symbolRefOrFlatSymbolRefAttributeCaster(
PyAttribute &pyAttribute) {
1337 if (PyFlatSymbolRefAttribute::isaFunction(pyAttribute))
1338 return py::cast(PyFlatSymbolRefAttribute(pyAttribute));
1339 if (PySymbolRefAttribute::isaFunction(pyAttribute))
1340 return py::cast(PySymbolRefAttribute(pyAttribute));
1341 std::string msg = std::string(
"Can't cast unknown SymbolRef attribute (") +
1342 std::string(py::repr(py::cast(pyAttribute))) +
")";
1343 throw py::cast_error(msg);
1349 PyAffineMapAttribute::bind(m);
1351 PyDenseBoolArrayAttribute::bind(m);
1352 PyDenseBoolArrayAttribute::PyDenseArrayIterator::bind(m);
1353 PyDenseI8ArrayAttribute::bind(m);
1354 PyDenseI8ArrayAttribute::PyDenseArrayIterator::bind(m);
1355 PyDenseI16ArrayAttribute::bind(m);
1356 PyDenseI16ArrayAttribute::PyDenseArrayIterator::bind(m);
1357 PyDenseI32ArrayAttribute::bind(m);
1358 PyDenseI32ArrayAttribute::PyDenseArrayIterator::bind(m);
1359 PyDenseI64ArrayAttribute::bind(m);
1360 PyDenseI64ArrayAttribute::PyDenseArrayIterator::bind(m);
1361 PyDenseF32ArrayAttribute::bind(m);
1362 PyDenseF32ArrayAttribute::PyDenseArrayIterator::bind(m);
1363 PyDenseF64ArrayAttribute::bind(m);
1364 PyDenseF64ArrayAttribute::PyDenseArrayIterator::bind(m);
1367 pybind11::cpp_function(denseArrayAttributeCaster));
1369 PyArrayAttribute::bind(m);
1370 PyArrayAttribute::PyArrayAttributeIterator::bind(m);
1371 PyBoolAttribute::bind(m);
1372 PyDenseElementsAttribute::bind(m);
1373 PyDenseFPElementsAttribute::bind(m);
1374 PyDenseIntElementsAttribute::bind(m);
1377 pybind11::cpp_function(denseIntOrFPElementsAttributeCaster));
1378 PyDenseResourceElementsAttribute::bind(m);
1380 PyDictAttribute::bind(m);
1381 PySymbolRefAttribute::bind(m);
1384 pybind11::cpp_function(symbolRefOrFlatSymbolRefAttributeCaster));
1386 PyFlatSymbolRefAttribute::bind(m);
1387 PyOpaqueAttribute::bind(m);
1388 PyFloatAttribute::bind(m);
1389 PyIntegerAttribute::bind(m);
1390 PyStringAttribute::bind(m);
1391 PyTypeAttribute::bind(m);
1394 pybind11::cpp_function(integerOrBoolAttributeCaster));
1395 PyUnitAttribute::bind(m);
1397 PyStridedLayoutAttribute::bind(m);
static const char kDenseElementsAttrGetDocstring[]
static const char kDenseResourceElementsAttrGetFromBufferDocstring[]
static MlirStringRef toMlirStringRef(const std::string &s)
static LogicalResult nextIndex(ArrayRef< int64_t > shape, MutableArrayRef< int64_t > index)
Walks over the indices of the elements of a tensor of a given shape by updating index in place to the...
std::string str() const
Converts the diagnostic to a string.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
PyMlirContextRef & getContext()
Accesses the context reference.
Used in function arguments when None should resolve to the current context manager set instance.
Used in function arguments when None should resolve to the current context manager set instance.
static PyMlirContext & resolve()
ReferrentTy * get() const
MlirAffineMap get() const
Wrapper around the generic MlirAttribute.
MlirAttribute get() const
CRTP base classes for Python attributes that subclass Attribute and should be castable from it (i....
pybind11::class_< DerivedTy, BaseTy > ClassTy
PyConcreteAttribute()=default
void registerTypeCaster(MlirTypeID mlirTypeID, pybind11::function typeCaster, bool replace=false)
Adds a user-friendly type caster.
static PyGlobals & get()
Most code should get the globals via this static accessor.
MlirContext get()
Accesses the underlying MlirContext.
static PyMlirContextRef forContext(MlirContext context)
Returns a context reference for the singleton PyMlirContext wrapper for the given context.
Represents a Python MlirNamedAttr, carrying an optional owned name.
Wrapper around the generic MlirType.
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
MLIR_CAPI_EXPORTED MlirAttribute mlirAffineMapAttrGet(MlirAffineMap map)
Creates an affine map attribute wrapping the given map.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseFPElements(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirAttribute mlirOpaqueAttrGet(MlirContext ctx, MlirStringRef dialectNamespace, intptr_t dataLength, const char *data, MlirType type)
Creates an opaque attribute in the given context associated with the dialect identified by its namesp...
MLIR_CAPI_EXPORTED MlirAttribute mlirFloatAttrDoubleGetChecked(MlirLocation loc, MlirType type, double value)
Same as "mlirFloatAttrDoubleGet", but if the type is not valid for a construction of a FloatAttr,...
MLIR_CAPI_EXPORTED int16_t mlirDenseI16ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAStridedLayout(MlirAttribute attr)
MLIR_CAPI_EXPORTED uint8_t mlirDenseElementsAttrGetUInt8Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED int64_t mlirStridedLayoutAttrGetOffset(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI64Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED int64_t mlirStridedLayoutAttrGetStride(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED int8_t mlirDenseElementsAttrGetInt8Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirAttribute mlirStridedLayoutAttrGet(MlirContext ctx, int64_t offset, intptr_t numStrides, const int64_t *strides)
MLIR_CAPI_EXPORTED MlirTypeID mlirStringAttrGetTypeID(void)
Returns the typeID of a String attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAUnit(MlirAttribute attr)
Checks whether the given attribute is a unit attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseElements(MlirAttribute attr)
Checks whether the given attribute is a dense elements attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAAffineMap(MlirAttribute attr)
Checks whether the given attribute is an affine map attribute.
MLIR_CAPI_EXPORTED int32_t mlirDenseI32ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED double mlirDenseF64ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirAttribute mlirFlatSymbolRefAttrGet(MlirContext ctx, MlirStringRef symbol)
Creates a flat symbol reference attribute in the given context referencing a symbol identified by the...
MLIR_CAPI_EXPORTED MlirTypeID mlirStridedLayoutAttrGetTypeID(void)
Returns the typeID of a StridedLayout attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirIntegerAttrGetTypeID(void)
Returns the typeID of an Integer attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseResourceElements(MlirAttribute attr)
MLIR_CAPI_EXPORTED int16_t mlirDenseElementsAttrGetInt16Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolRefAttrGetRootReference(MlirAttribute attr)
Returns the string reference to the root referenced symbol.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAType(MlirAttribute attr)
Checks whether the given attribute is a type attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirDenseIntOrFPElementsAttrGetTypeID(void)
Returns the typeID of an DenseIntOrFPElements attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseIntElements(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAArray(MlirAttribute attr)
Checks whether the given attribute is an array attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAInteger(MlirAttribute attr)
Checks whether the given attribute is an integer attribute.
MLIR_CAPI_EXPORTED intptr_t mlirDictionaryAttrGetNumElements(MlirAttribute attr)
Returns the number of attributes contained in a dictionary attribute.
MLIR_CAPI_EXPORTED uint16_t mlirDenseElementsAttrGetUInt16Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED uint64_t mlirDenseElementsAttrGetUInt64Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED bool mlirBoolAttrGetValue(MlirAttribute attr)
Returns the value stored in the given bool attribute.
MLIR_CAPI_EXPORTED int64_t mlirDenseI64ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirAttribute mlirIntegerAttrGet(MlirType type, int64_t value)
Creates an integer attribute of the given type with the given integer value.
MLIR_CAPI_EXPORTED bool mlirDenseElementsAttrGetBoolValue(MlirAttribute attr, intptr_t pos)
Returns the pos-th value (flat contiguous indexing) of a specific type contained by the given dense e...
MLIR_CAPI_EXPORTED MlirAttribute mlirDictionaryAttrGet(MlirContext ctx, intptr_t numElements, MlirNamedAttribute const *elements)
Creates a dictionary attribute containing the given list of elements in the provided context.
MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseResourceElementsAttrGet(MlirType shapedType, MlirStringRef name, void *data, size_t dataLength, size_t dataAlignment, bool dataIsMutable, void(*deleter)(void *userData, const void *data, size_t size, size_t align), void *userData)
Unlike the typed accessors below, constructs the attribute with a raw data buffer and no type/alignme...
MLIR_CAPI_EXPORTED bool mlirAttributeIsABool(MlirAttribute attr)
Checks whether the given attribute is a bool attribute.
MLIR_CAPI_EXPORTED bool mlirDenseBoolArrayGetElement(MlirAttribute attr, intptr_t pos)
Get an element of a dense array.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI64ArrayGet(MlirContext ctx, intptr_t size, int64_t const *values)
MLIR_CAPI_EXPORTED MlirTypeID mlirAffineMapAttrGetTypeID(void)
Returns the typeID of an AffineMap attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirSymbolRefAttrGetNestedReference(MlirAttribute attr, intptr_t pos)
Returns pos-th reference nested in the given symbol reference attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirArrayAttrGetTypeID(void)
Returns the typeID of an Array attribute.
MLIR_CAPI_EXPORTED float mlirDenseF32ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseF64ArrayGet(MlirContext ctx, intptr_t size, double const *values)
MLIR_CAPI_EXPORTED int64_t mlirIntegerAttrGetValueInt(MlirAttribute attr)
Returns the value stored in the given integer attribute, assuming the value is of signless type and f...
MLIR_CAPI_EXPORTED intptr_t mlirSymbolRefAttrGetNumNestedReferences(MlirAttribute attr)
Returns the number of references nested in the given symbol reference attribute.
MLIR_CAPI_EXPORTED MlirType mlirTypeAttrGetValue(MlirAttribute attr)
Returns the type stored in the given type attribute.
MLIR_CAPI_EXPORTED bool mlirDenseElementsAttrIsSplat(MlirAttribute attr)
Checks whether the given dense elements attribute contains a single replicated value (splat).
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseBoolArray(MlirAttribute attr)
Checks whether the given attribute is a dense array attribute.
MLIR_CAPI_EXPORTED MlirStringRef mlirOpaqueAttrGetData(MlirAttribute attr)
Returns the raw data as a string reference.
MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeGetNull(void)
Returns an empty attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirBoolAttrGet(MlirContext ctx, int value)
Creates a bool attribute in the given context with the given value.
MLIR_CAPI_EXPORTED MlirTypeID mlirFloatAttrGetTypeID(void)
Returns the typeID of a Float attribute.
MLIR_CAPI_EXPORTED int64_t mlirIntegerAttrGetValueSInt(MlirAttribute attr)
Returns the value stored in the given integer attribute, assuming the value is of signed type and fit...
MLIR_CAPI_EXPORTED int8_t mlirDenseI8ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI32ArrayGet(MlirContext ctx, intptr_t size, int32_t const *values)
MLIR_CAPI_EXPORTED int64_t mlirDenseElementsAttrGetInt64Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirNamedAttribute mlirDictionaryAttrGetElement(MlirAttribute attr, intptr_t pos)
Returns pos-th element of the given dictionary attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirUnitAttrGetTypeID(void)
Returns the typeID of a Unit attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseF32ArrayGet(MlirContext ctx, intptr_t size, float const *values)
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseBoolArrayGet(MlirContext ctx, intptr_t size, int const *values)
Create a dense array attribute with the given elements.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAOpaque(MlirAttribute attr)
Checks whether the given attribute is an opaque attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirArrayAttrGetElement(MlirAttribute attr, intptr_t pos)
Returns pos-th element stored in the given array attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDictionaryAttrGetElementByName(MlirAttribute attr, MlirStringRef name)
Returns the dictionary attribute element with the given name or NULL if the given name does not exist...
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseF32Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirTypeID mlirSymbolRefAttrGetTypeID(void)
Returns the typeID of an SymbolRef attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirDenseArrayAttrGetTypeID(void)
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrGetSplatValue(MlirAttribute attr)
Returns the single replicated value (splat) of a specific type contained by the given dense elements ...
MLIR_CAPI_EXPORTED bool mlirAttributeIsASymbolRef(MlirAttribute attr)
Checks whether the given attribute is a symbol reference attribute.
MLIR_CAPI_EXPORTED float mlirDenseElementsAttrGetFloatValue(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAString(MlirAttribute attr)
Checks whether the given attribute is a string attribute.
MLIR_CAPI_EXPORTED int64_t mlirElementsAttrGetNumElements(MlirAttribute attr)
Gets the total number of elements in the given elements attribute.
MLIR_CAPI_EXPORTED MlirStringRef mlirOpaqueAttrGetDialectNamespace(MlirAttribute attr)
Returns the namespace of the dialect with which the given opaque attribute is associated.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADictionary(MlirAttribute attr)
Checks whether the given attribute is a dictionary attribute.
MLIR_CAPI_EXPORTED int32_t mlirDenseElementsAttrGetInt32Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirStringRef mlirStringAttrGetValue(MlirAttribute attr)
Returns the attribute values as a string reference.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI16ArrayGet(MlirContext ctx, intptr_t size, int16_t const *values)
MLIR_CAPI_EXPORTED MlirTypeID mlirOpaqueAttrGetTypeID(void)
Returns the typeID of an Opaque attribute.
MLIR_CAPI_EXPORTED double mlirFloatAttrGetValueDouble(MlirAttribute attr)
Returns the value stored in the given floating point attribute, interpreting the value as double.
MLIR_CAPI_EXPORTED uint64_t mlirIntegerAttrGetValueUInt(MlirAttribute attr)
Returns the value stored in the given integer attribute, assuming the value is of unsigned type and f...
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI8ArrayGet(MlirContext ctx, intptr_t size, int8_t const *values)
MLIR_CAPI_EXPORTED MlirAttribute mlirUnitAttrGet(MlirContext ctx)
Creates a unit attribute in the given context.
MLIR_CAPI_EXPORTED double mlirDenseElementsAttrGetDoubleValue(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED intptr_t mlirStridedLayoutAttrGetNumStrides(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirAttribute mlirFloatAttrDoubleGet(MlirContext ctx, MlirType type, double value)
Creates a floating point attribute in the given context with the given double value and double-precis...
MLIR_CAPI_EXPORTED MlirAttribute mlirArrayAttrGet(MlirContext ctx, intptr_t numElements, MlirAttribute const *elements)
Creates an array element containing the given list of elements in the given context.
MLIR_CAPI_EXPORTED const void * mlirDenseElementsAttrGetRawData(MlirAttribute attr)
Returns the raw data of the given dense elements attribute.
MLIR_CAPI_EXPORTED intptr_t mlirDenseArrayGetNumElements(MlirAttribute attr)
Get the size of a dense array.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAFloat(MlirAttribute attr)
Checks whether the given attribute is a floating point attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirSymbolRefAttrGet(MlirContext ctx, MlirStringRef symbol, intptr_t numReferences, MlirAttribute const *references)
Creates a symbol reference attribute in the given context referencing a symbol identified by the give...
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeAttrGetTypeID(void)
Returns the typeID of a Type attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrSplatGet(MlirType shapedType, MlirAttribute element)
Creates a dense elements attribute with the given Shaped type containing a single replicated element ...
MLIR_CAPI_EXPORTED MlirTypeID mlirDictionaryAttrGetTypeID(void)
Returns the typeID of a Dictionary attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirStringAttrGet(MlirContext ctx, MlirStringRef str)
Creates a string attribute in the given context containing the given string.
MLIR_CAPI_EXPORTED MlirAttribute mlirTypeAttrGet(MlirType type)
Creates a type attribute wrapping the given type in the same context as the type.
MLIR_CAPI_EXPORTED intptr_t mlirArrayAttrGetNumElements(MlirAttribute attr)
Returns the number of elements stored in the given array attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrRawBufferGet(MlirType shapedType, size_t rawBufferSize, const void *rawBuffer)
Creates a dense elements attribute with the given Shaped type and elements populated from a packed,...
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI32Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI8Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirAttribute mlirStringAttrTypedGet(MlirType type, MlirStringRef str)
Creates a string attribute in the given context containing the given string.
MLIR_CAPI_EXPORTED bool mlirAttributeIsAFlatSymbolRef(MlirAttribute attr)
Checks whether the given attribute is a flat symbol reference attribute.
MLIR_CAPI_EXPORTED MlirStringRef mlirFlatSymbolRefAttrGetValue(MlirAttribute attr)
Returns the referenced symbol as a string reference.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI16Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseF64Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED uint32_t mlirDenseElementsAttrGetUInt32Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirType mlirRankedTensorTypeGet(intptr_t rank, const int64_t *shape, MlirType elementType, MlirAttribute encoding)
Creates a tensor type of a fixed rank with the given shape, element type, and optional encoding in th...
MLIR_CAPI_EXPORTED bool mlirIntegerTypeIsSignless(MlirType type)
Checks whether the given integer type is signless.
MLIR_CAPI_EXPORTED bool mlirTypeIsAInteger(MlirType type)
Checks whether the given type is an integer type.
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDimSize(MlirType type, intptr_t dim)
Returns the dim-th dimension of the given ranked shaped type.
MLIR_CAPI_EXPORTED MlirType mlirIntegerTypeGet(MlirContext ctx, unsigned bitwidth)
Creates a signless integer type of the given bitwidth in the context.
MLIR_CAPI_EXPORTED bool mlirIntegerTypeIsUnsigned(MlirType type)
Checks whether the given integer type is unsigned.
MLIR_CAPI_EXPORTED unsigned mlirIntegerTypeGetWidth(MlirType type)
Returns the bitwidth of an integer type.
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetRank(MlirType type)
Returns the rank of the given ranked shaped type.
MLIR_CAPI_EXPORTED MlirType mlirF64TypeGet(MlirContext ctx)
Creates a f64 type in the given context.
MLIR_CAPI_EXPORTED MlirType mlirIntegerTypeSignedGet(MlirContext ctx, unsigned bitwidth)
Creates a signed integer type of the given bitwidth in the context.
MLIR_CAPI_EXPORTED MlirType mlirF16TypeGet(MlirContext ctx)
Creates an f16 type in the given context.
MLIR_CAPI_EXPORTED bool mlirTypeIsAF64(MlirType type)
Checks whether the given type is an f64 type.
MLIR_CAPI_EXPORTED bool mlirTypeIsAF16(MlirType type)
Checks whether the given type is an f16 type.
MLIR_CAPI_EXPORTED bool mlirIntegerTypeIsSigned(MlirType type)
Checks whether the given integer type is signed.
MLIR_CAPI_EXPORTED MlirType mlirShapedTypeGetElementType(MlirType type)
Returns the element type of the shaped type.
MLIR_CAPI_EXPORTED bool mlirShapedTypeHasStaticShape(MlirType type)
Checks whether the given shaped type has a static shape.
MLIR_CAPI_EXPORTED MlirType mlirF32TypeGet(MlirContext ctx)
Creates an f32 type in the given context.
MLIR_CAPI_EXPORTED bool mlirTypeIsAShaped(MlirType type)
Checks whether the given type is a Shaped type.
MLIR_CAPI_EXPORTED MlirType mlirIntegerTypeUnsignedGet(MlirContext ctx, unsigned bitwidth)
Creates an unsigned integer type of the given bitwidth in the context.
MLIR_CAPI_EXPORTED bool mlirTypeIsAF32(MlirType type)
Checks whether the given type is an f32 type.
MLIR_CAPI_EXPORTED bool mlirTypeIsAIndex(MlirType type)
Checks whether the given type is an index type.
MLIR_CAPI_EXPORTED int64_t mlirShapedTypeGetDynamicStrideOrOffset(void)
Returns the value indicating a dynamic stride or offset in a shaped type.
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, MlirAttribute attr)
Associates an attribute with the name. Takes ownership of neither.
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute)
Gets the type of this attribute.
MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type)
Gets the context that a type was created with.
MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute)
Gets the context that an attribute was created with.
MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str)
Gets an identifier with the given string value.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
void populateIRAttributes(pybind11::module &m)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
A pointer to a sized fragment of a string, not necessarily null-terminated.
const char * data
Pointer to the first symbol.
size_t length
Length of the fragment.
Custom exception that allows access to error diagnostic information.
RAII object that captures any error diagnostics emitted to the provided context.
std::vector< PyDiagnostic::DiagnosticInfo > take()
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.