10 #include <string_view>
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/Support/raw_ostream.h"
35 R
"(Gets a DenseElementsAttr from a Python buffer or array.
37 When `type` is not provided, then some limited type inferencing is done based
38 on the buffer format. Support presently exists for 8/16/32/64 signed and
39 unsigned integers and float16/float32/float64. DenseElementsAttrs of these
40 types can also be converted back to a corresponding buffer.
42 For conversions outside of these types, a `type=` must be explicitly provided
43 and the buffer contents must be bit-castable to the MLIR internal
46 * Integer types (except for i1): the buffer must be byte aligned to the
48 * Floating point types: Must be bit-castable to the given floating point
50 * i1 (bool): Bit packed into 8bit words where the bit pattern matches a
51 row major ordering. An arbitrary Numpy `bool_` array can be bit packed to
52 this specification with: `np.packbits(ary, axis=None, bitorder='little')`.
54 If a single element buffer is passed (or for i1, a single byte with value 0
55 or 255), then a splat will be created.
58 array: The array or buffer to convert.
59 signless: If inferring an appropriate MLIR type, use signless types for
60 integers (defaults True).
61 type: Skips inference of the MLIR element type and uses this instead. The
62 storage size must be consistent with the actual contents of the buffer.
63 shape: Overrides the shape of the buffer when constructing the MLIR
64 shaped type. This is needed when the physical and logical shape differ (as
66 context: Explicit context, if not from context manager.
69 DenseElementsAttr on success.
72 ValueError: If the type of the buffer or array cannot be matched to an MLIR
73 type or if the buffer does not meet expectations.
77 R
"(Gets a DenseElementsAttr from a Python list of attributes.
79 Note that it can be expensive to construct attributes individually.
80 For a large number of elements, consider using a Python buffer or array instead.
83 attrs: A list of attributes.
84 type: The desired shape and type of the resulting DenseElementsAttr.
85 If not provided, the element type is determined based on the type
86 of the 0th attribute and the shape is `[len(attrs)]`.
87 context: Explicit context, if not from context manager.
90 DenseElementsAttr on success.
93 ValueError: If the type of the attributes does not match the type
94 specified by `shaped_type`.
98 R
"(Gets a DenseResourceElementsAttr from a Python buffer or array.
100 This function does minimal validation or massaging of the data, and it is
101 up to the caller to ensure that the buffer meets the characteristics
102 implied by the shape.
104 The backing buffer and any user objects will be retained for the lifetime
105 of the resource blob. This is typically bounded to the context but the
106 resource can have a shorter lifespan depending on how it is used in
107 subsequent processing.
110 buffer: The array or buffer to convert.
111 name: Name to provide to the resource (may be changed upon collision).
112 type: The explicit ShapedType to construct the attribute with.
113 context: Explicit context, if not from context manager.
116 DenseResourceElementsAttr on success.
119 ValueError: If the type of the buffer or array cannot be matched to an MLIR
120 type or if the buffer does not meet expectations.
132 static constexpr
const char *pyClassName =
"AffineMapAttr";
134 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
137 static void bindDerived(ClassTy &c) {
142 return PyAffineMapAttribute(affineMap.
getContext(), attr);
144 py::arg(
"affine_map"),
"Gets an attribute wrapping an AffineMap.");
146 "Returns the value of the AffineMap attribute");
150 class PyIntegerSetAttribute
154 static constexpr
const char *pyClassName =
"IntegerSetAttr";
156 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
159 static void bindDerived(ClassTy &c) {
164 return PyIntegerSetAttribute(integerSet.
getContext(), attr);
166 py::arg(
"integer_set"),
"Gets an attribute wrapping an IntegerSet.");
170 template <
typename T>
171 static T pyTryCast(py::handle
object) {
173 return object.cast<T>();
174 }
catch (py::cast_error &err) {
177 "Invalid attribute when attempting to create an ArrayAttribute (") +
179 throw py::cast_error(msg);
180 }
catch (py::reference_cast_error &err) {
181 std::string msg = std::string(
"Invalid attribute (None?) when attempting "
182 "to create an ArrayAttribute (") +
184 throw py::cast_error(msg);
190 template <
typename EltTy,
typename DerivedT>
196 class PyDenseArrayIterator {
198 PyDenseArrayIterator(
PyAttribute attr) : attr(std::move(attr)) {}
201 PyDenseArrayIterator dunderIter() {
return *
this; }
207 throw py::stop_iteration();
212 static void bind(py::module &m) {
213 py::class_<PyDenseArrayIterator>(m, DerivedT::pyIteratorName,
215 .def(
"__iter__", &PyDenseArrayIterator::dunderIter)
216 .def(
"__next__", &PyDenseArrayIterator::dunderNext);
227 EltTy getItem(intptr_t i) {
return DerivedT::getElement(*
this, i); }
235 return getAttribute(values, ctx->getRef());
237 py::arg(
"values"), py::arg(
"context") = py::none(),
238 "Gets a uniqued dense array attribute");
240 c.def(
"__getitem__", [](DerivedT &arr, intptr_t i) {
242 throw py::index_error(
"DenseArray index out of range");
243 return arr.getItem(i);
245 c.def(
"__len__", [](
const DerivedT &arr) {
249 [](
const DerivedT &arr) {
return PyDenseArrayIterator(arr); });
250 c.def(
"__add__", [](DerivedT &arr,
const py::list &extras) {
251 std::vector<EltTy> values;
253 values.reserve(numOldElements + py::len(extras));
254 for (intptr_t i = 0; i < numOldElements; ++i)
255 values.push_back(arr.getItem(i));
256 for (py::handle attr : extras)
257 values.push_back(pyTryCast<EltTy>(attr));
258 return getAttribute(values, arr.getContext());
263 static DerivedT getAttribute(
const std::vector<EltTy> &values,
265 if constexpr (std::is_same_v<EltTy, bool>) {
266 std::vector<int> intValues(values.begin(), values.end());
267 MlirAttribute attr = DerivedT::getAttribute(ctx->
get(), intValues.size(),
269 return DerivedT(ctx, attr);
272 DerivedT::getAttribute(ctx->
get(), values.size(), values.data());
273 return DerivedT(ctx, attr);
279 struct PyDenseBoolArrayAttribute
280 :
public PyDenseArrayAttribute<bool, PyDenseBoolArrayAttribute> {
284 static constexpr
const char *pyClassName =
"DenseBoolArrayAttr";
285 static constexpr
const char *pyIteratorName =
"DenseBoolArrayIterator";
286 using PyDenseArrayAttribute::PyDenseArrayAttribute;
288 struct PyDenseI8ArrayAttribute
289 :
public PyDenseArrayAttribute<int8_t, PyDenseI8ArrayAttribute> {
293 static constexpr
const char *pyClassName =
"DenseI8ArrayAttr";
294 static constexpr
const char *pyIteratorName =
"DenseI8ArrayIterator";
295 using PyDenseArrayAttribute::PyDenseArrayAttribute;
297 struct PyDenseI16ArrayAttribute
298 :
public PyDenseArrayAttribute<int16_t, PyDenseI16ArrayAttribute> {
302 static constexpr
const char *pyClassName =
"DenseI16ArrayAttr";
303 static constexpr
const char *pyIteratorName =
"DenseI16ArrayIterator";
304 using PyDenseArrayAttribute::PyDenseArrayAttribute;
306 struct PyDenseI32ArrayAttribute
307 :
public PyDenseArrayAttribute<int32_t, PyDenseI32ArrayAttribute> {
311 static constexpr
const char *pyClassName =
"DenseI32ArrayAttr";
312 static constexpr
const char *pyIteratorName =
"DenseI32ArrayIterator";
313 using PyDenseArrayAttribute::PyDenseArrayAttribute;
315 struct PyDenseI64ArrayAttribute
316 :
public PyDenseArrayAttribute<int64_t, PyDenseI64ArrayAttribute> {
320 static constexpr
const char *pyClassName =
"DenseI64ArrayAttr";
321 static constexpr
const char *pyIteratorName =
"DenseI64ArrayIterator";
322 using PyDenseArrayAttribute::PyDenseArrayAttribute;
324 struct PyDenseF32ArrayAttribute
325 :
public PyDenseArrayAttribute<float, PyDenseF32ArrayAttribute> {
329 static constexpr
const char *pyClassName =
"DenseF32ArrayAttr";
330 static constexpr
const char *pyIteratorName =
"DenseF32ArrayIterator";
331 using PyDenseArrayAttribute::PyDenseArrayAttribute;
333 struct PyDenseF64ArrayAttribute
334 :
public PyDenseArrayAttribute<double, PyDenseF64ArrayAttribute> {
338 static constexpr
const char *pyClassName =
"DenseF64ArrayAttr";
339 static constexpr
const char *pyIteratorName =
"DenseF64ArrayIterator";
340 using PyDenseArrayAttribute::PyDenseArrayAttribute;
346 static constexpr
const char *pyClassName =
"ArrayAttr";
348 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
351 class PyArrayAttributeIterator {
353 PyArrayAttributeIterator(
PyAttribute attr) : attr(std::move(attr)) {}
355 PyArrayAttributeIterator &dunderIter() {
return *
this; }
357 MlirAttribute dunderNext() {
360 throw py::stop_iteration();
364 static void bind(py::module &m) {
365 py::class_<PyArrayAttributeIterator>(m,
"ArrayAttributeIterator",
367 .def(
"__iter__", &PyArrayAttributeIterator::dunderIter)
368 .def(
"__next__", &PyArrayAttributeIterator::dunderNext);
376 MlirAttribute getItem(intptr_t i) {
380 static void bindDerived(ClassTy &c) {
385 mlirAttributes.reserve(py::len(attributes));
386 for (
auto attribute : attributes) {
387 mlirAttributes.push_back(pyTryCast<PyAttribute>(attribute));
390 context->
get(), mlirAttributes.size(), mlirAttributes.data());
391 return PyArrayAttribute(context->getRef(), attr);
393 py::arg(
"attributes"), py::arg(
"context") = py::none(),
394 "Gets a uniqued Array attribute");
396 [](PyArrayAttribute &arr, intptr_t i) {
398 throw py::index_error(
"ArrayAttribute index out of range");
399 return arr.getItem(i);
402 [](
const PyArrayAttribute &arr) {
405 .def(
"__iter__", [](
const PyArrayAttribute &arr) {
406 return PyArrayAttributeIterator(arr);
408 c.def(
"__add__", [](PyArrayAttribute arr, py::list extras) {
409 std::vector<MlirAttribute> attributes;
411 attributes.reserve(numOldElements + py::len(extras));
412 for (intptr_t i = 0; i < numOldElements; ++i)
413 attributes.push_back(arr.getItem(i));
414 for (py::handle attr : extras)
415 attributes.push_back(pyTryCast<PyAttribute>(attr));
417 arr.getContext()->get(), attributes.size(), attributes.data());
418 return PyArrayAttribute(arr.getContext(), arrayAttr);
427 static constexpr
const char *pyClassName =
"FloatAttr";
429 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
432 static void bindDerived(ClassTy &c) {
440 return PyFloatAttribute(type.
getContext(), attr);
442 py::arg(
"type"), py::arg(
"value"), py::arg(
"loc") = py::none(),
443 "Gets an uniqued float point attribute associated to a type");
449 return PyFloatAttribute(context->getRef(), attr);
451 py::arg(
"value"), py::arg(
"context") = py::none(),
452 "Gets an uniqued float point attribute associated to a f32 type");
458 return PyFloatAttribute(context->getRef(), attr);
460 py::arg(
"value"), py::arg(
"context") = py::none(),
461 "Gets an uniqued float point attribute associated to a f64 type");
463 "Returns the value of the float attribute");
465 "Converts the value of the float attribute to a Python float");
473 static constexpr
const char *pyClassName =
"IntegerAttr";
476 static void bindDerived(ClassTy &c) {
479 [](
PyType &type, int64_t value) {
481 return PyIntegerAttribute(type.
getContext(), attr);
483 py::arg(
"type"), py::arg(
"value"),
484 "Gets an uniqued integer attribute associated to a type");
485 c.def_property_readonly(
"value", toPyInt,
486 "Returns the value of the integer attribute");
487 c.def(
"__int__", toPyInt,
488 "Converts the value of the integer attribute to a Python int");
489 c.def_property_readonly_static(
"static_typeid",
490 [](py::object & ) -> MlirTypeID {
496 static py::int_ toPyInt(PyIntegerAttribute &
self) {
510 static constexpr
const char *pyClassName =
"BoolAttr";
513 static void bindDerived(ClassTy &c) {
518 return PyBoolAttribute(context->getRef(), attr);
520 py::arg(
"value"), py::arg(
"context") = py::none(),
521 "Gets an uniqued bool attribute");
523 "Returns the value of the bool attribute");
525 "Converts the value of the bool attribute to a Python bool");
532 static constexpr
const char *pyClassName =
"SymbolRefAttr";
535 static MlirAttribute fromList(
const std::vector<std::string> &symbols,
538 throw std::runtime_error(
"SymbolRefAttr must be composed of at least "
542 for (
size_t i = 1; i < symbols.size(); ++i) {
543 referenceAttrs.push_back(
547 referenceAttrs.size(), referenceAttrs.data());
550 static void bindDerived(ClassTy &c) {
553 [](
const std::vector<std::string> &symbols,
555 return PySymbolRefAttribute::fromList(symbols, context.
resolve());
557 py::arg(
"symbols"), py::arg(
"context") = py::none(),
558 "Gets a uniqued SymbolRef attribute from a list of symbol names");
559 c.def_property_readonly(
561 [](PySymbolRefAttribute &
self) {
562 std::vector<std::string> symbols = {
572 "Returns the value of the SymbolRef attribute as a list[str]");
576 class PyFlatSymbolRefAttribute
580 static constexpr
const char *pyClassName =
"FlatSymbolRefAttr";
583 static void bindDerived(ClassTy &c) {
589 return PyFlatSymbolRefAttribute(context->getRef(), attr);
591 py::arg(
"value"), py::arg(
"context") = py::none(),
592 "Gets a uniqued FlatSymbolRef attribute");
593 c.def_property_readonly(
595 [](PyFlatSymbolRefAttribute &
self) {
597 return py::str(stringRef.
data, stringRef.
length);
599 "Returns the value of the FlatSymbolRef attribute as a string");
606 static constexpr
const char *pyClassName =
"OpaqueAttr";
608 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
611 static void bindDerived(ClassTy &c) {
614 [](std::string dialectNamespace, py::buffer buffer,
PyType &type,
616 const py::buffer_info bufferInfo = buffer.request();
617 intptr_t bufferSize = bufferInfo.size;
620 static_cast<char *
>(bufferInfo.ptr), type);
621 return PyOpaqueAttribute(context->getRef(), attr);
623 py::arg(
"dialect_namespace"), py::arg(
"buffer"), py::arg(
"type"),
624 py::arg(
"context") = py::none(),
"Gets an Opaque attribute.");
625 c.def_property_readonly(
627 [](PyOpaqueAttribute &
self) {
629 return py::str(stringRef.
data, stringRef.
length);
631 "Returns the dialect namespace for the Opaque attribute as a string");
632 c.def_property_readonly(
634 [](PyOpaqueAttribute &
self) {
636 return py::bytes(stringRef.
data, stringRef.
length);
638 "Returns the data for the Opaqued attributes as `bytes`");
645 static constexpr
const char *pyClassName =
"StringAttr";
647 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
650 static void bindDerived(ClassTy &c) {
656 return PyStringAttribute(context->getRef(), attr);
658 py::arg(
"value"), py::arg(
"context") = py::none(),
659 "Gets a uniqued string attribute");
662 [](
PyType &type, std::string value) {
665 return PyStringAttribute(type.
getContext(), attr);
667 py::arg(
"type"), py::arg(
"value"),
668 "Gets a uniqued string attribute associated to a type");
669 c.def_property_readonly(
671 [](PyStringAttribute &
self) {
673 return py::str(stringRef.
data, stringRef.
length);
675 "Returns the value of the string attribute");
676 c.def_property_readonly(
678 [](PyStringAttribute &
self) {
680 return py::bytes(stringRef.
data, stringRef.
length);
682 "Returns the value of the string attribute as `bytes`");
687 class PyDenseElementsAttribute
691 static constexpr
const char *pyClassName =
"DenseElementsAttr";
694 static PyDenseElementsAttribute
695 getFromList(py::list attributes, std::optional<PyType> explicitType,
698 const size_t numAttributes = py::len(attributes);
699 if (numAttributes == 0)
700 throw py::value_error(
"Attributes list must be non-empty.");
708 llvm::raw_string_ostream os(message);
709 os <<
"Expected a static ShapedType for the shaped_type parameter: "
710 << py::repr(py::cast(*explicitType));
711 throw py::value_error(message);
713 shapedType = *explicitType;
717 shape.size(), shape.data(),
723 mlirAttributes.reserve(numAttributes);
724 for (
const py::handle &attribute : attributes) {
725 MlirAttribute mlirAttribute = pyTryCast<PyAttribute>(attribute);
727 mlirAttributes.push_back(mlirAttribute);
731 llvm::raw_string_ostream os(message);
732 os <<
"All attributes must be of the same type and match "
733 <<
"the type parameter: expected=" << py::repr(py::cast(shapedType))
734 <<
", but got=" << py::repr(py::cast(attrType));
735 throw py::value_error(message);
740 shapedType, mlirAttributes.size(), mlirAttributes.data());
742 return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
745 static PyDenseElementsAttribute
746 getFromBuffer(py::buffer array,
bool signless,
747 std::optional<PyType> explicitType,
748 std::optional<std::vector<int64_t>> explicitShape,
751 int flags = PyBUF_ND;
753 flags |= PyBUF_FORMAT;
756 if (PyObject_GetBuffer(array.ptr(), &view, flags) != 0) {
757 throw py::error_already_set();
759 auto freeBuffer = llvm::make_scope_exit([&]() { PyBuffer_Release(&view); });
762 shape.append(explicitShape->begin(), explicitShape->end());
764 shape.append(view.shape, view.shape + view.ndim);
768 MlirContext context = contextWrapper->
get();
775 std::optional<MlirType> bulkLoadElementType;
777 bulkLoadElementType = *explicitType;
779 std::string_view format(view.format);
782 assert(view.itemsize == 4 &&
"mismatched array itemsize");
784 }
else if (format ==
"d") {
786 assert(view.itemsize == 8 &&
"mismatched array itemsize");
788 }
else if (format ==
"e") {
790 assert(view.itemsize == 2 &&
"mismatched array itemsize");
792 }
else if (isSignedIntegerFormat(format)) {
793 if (view.itemsize == 4) {
795 bulkLoadElementType = signless
798 }
else if (view.itemsize == 8) {
800 bulkLoadElementType = signless
803 }
else if (view.itemsize == 1) {
807 }
else if (view.itemsize == 2) {
809 bulkLoadElementType = signless
813 }
else if (isUnsignedIntegerFormat(format)) {
814 if (view.itemsize == 4) {
816 bulkLoadElementType = signless
819 }
else if (view.itemsize == 8) {
821 bulkLoadElementType = signless
824 }
else if (view.itemsize == 1) {
826 bulkLoadElementType = signless
829 }
else if (view.itemsize == 2) {
831 bulkLoadElementType = signless
836 if (!bulkLoadElementType) {
837 throw std::invalid_argument(
838 std::string(
"unimplemented array format conversion from format: ") +
839 std::string(format));
846 throw std::invalid_argument(
"Shape can only be specified explicitly "
847 "when the type is not a shaped type.");
849 shapedType = *bulkLoadElementType;
852 *bulkLoadElementType, encodingAttr);
854 size_t rawBufferSize = view.len;
858 throw std::invalid_argument(
859 "DenseElementsAttr could not be constructed from the given buffer. "
860 "This may mean that the Python buffer layout does not match that "
861 "MLIR expected layout and is a bug.");
863 return PyDenseElementsAttribute(contextWrapper->getRef(), attr);
866 static PyDenseElementsAttribute getSplat(
const PyType &shapedType,
868 auto contextWrapper =
872 std::string message =
"Illegal element type for DenseElementsAttr: ";
873 message.append(py::repr(py::cast(elementAttr)));
874 throw py::value_error(message);
878 std::string message =
879 "Expected a static ShapedType for the shaped_type parameter: ";
880 message.append(py::repr(py::cast(shapedType)));
881 throw py::value_error(message);
886 std::string message =
887 "Shaped element type and attribute type must be equal: shaped=";
888 message.append(py::repr(py::cast(shapedType)));
889 message.append(
", element=");
890 message.append(py::repr(py::cast(elementAttr)));
891 throw py::value_error(message);
894 MlirAttribute elements =
896 return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
901 py::buffer_info accessBuffer() {
908 return bufferInfo<float>(shapedType);
912 return bufferInfo<double>(shapedType);
916 return bufferInfo<uint16_t>(shapedType,
"e");
920 return bufferInfo<int64_t>(shapedType);
927 return bufferInfo<int32_t>(shapedType);
931 return bufferInfo<uint32_t>(shapedType);
938 return bufferInfo<int64_t>(shapedType);
942 return bufferInfo<uint64_t>(shapedType);
949 return bufferInfo<int8_t>(shapedType);
953 return bufferInfo<uint8_t>(shapedType);
960 return bufferInfo<int16_t>(shapedType);
964 return bufferInfo<uint16_t>(shapedType);
970 throw std::invalid_argument(
971 "unsupported data type for conversion to Python buffer");
974 static void bindDerived(ClassTy &c) {
975 c.def(
"__len__", &PyDenseElementsAttribute::dunderLen)
976 .def_static(
"get", PyDenseElementsAttribute::getFromBuffer,
977 py::arg(
"array"), py::arg(
"signless") =
true,
978 py::arg(
"type") = py::none(), py::arg(
"shape") = py::none(),
979 py::arg(
"context") = py::none(),
981 .def_static(
"get", PyDenseElementsAttribute::getFromList,
982 py::arg(
"attrs"), py::arg(
"type") = py::none(),
983 py::arg(
"context") = py::none(),
985 .def_static(
"get_splat", PyDenseElementsAttribute::getSplat,
986 py::arg(
"shaped_type"), py::arg(
"element_attr"),
987 "Gets a DenseElementsAttr where all values are the same")
988 .def_property_readonly(
"is_splat",
989 [](PyDenseElementsAttribute &
self) ->
bool {
992 .def(
"get_splat_value",
993 [](PyDenseElementsAttribute &
self) {
995 throw py::value_error(
996 "get_splat_value called on a non-splat attribute");
999 .def_buffer(&PyDenseElementsAttribute::accessBuffer);
1003 static bool isUnsignedIntegerFormat(std::string_view format) {
1006 char code = format[0];
1007 return code ==
'I' || code ==
'B' || code ==
'H' || code ==
'L' ||
1011 static bool isSignedIntegerFormat(std::string_view format) {
1014 char code = format[0];
1015 return code ==
'i' || code ==
'b' || code ==
'h' || code ==
'l' ||
1019 template <
typename Type>
1020 py::buffer_info bufferInfo(MlirType shapedType,
1021 const char *explicitFormat =
nullptr) {
1029 for (intptr_t i = 0; i < rank; ++i)
1035 strides.assign(rank, 0);
1037 for (intptr_t i = 1; i < rank; ++i) {
1038 intptr_t strideFactor = 1;
1039 for (intptr_t
j = i;
j < rank; ++
j)
1041 strides.push_back(
sizeof(
Type) * strideFactor);
1043 strides.push_back(
sizeof(
Type));
1046 if (explicitFormat) {
1047 format = explicitFormat;
1049 format = py::format_descriptor<Type>::format();
1051 return py::buffer_info(data,
sizeof(
Type), format, rank, shape, strides,
1058 class PyDenseIntElementsAttribute
1060 PyDenseElementsAttribute> {
1063 static constexpr
const char *pyClassName =
"DenseIntElementsAttr";
1068 py::int_ dunderGetItem(intptr_t pos) {
1069 if (pos < 0 || pos >= dunderLen()) {
1070 throw py::index_error(
"attempt to access out of bounds element");
1076 "expected integer element type in dense int elements attribute");
1117 throw py::type_error(
"Unsupported integer type");
1120 static void bindDerived(ClassTy &c) {
1121 c.def(
"__getitem__", &PyDenseIntElementsAttribute::dunderGetItem);
1125 class PyDenseResourceElementsAttribute
1128 static constexpr IsAFunctionTy isaFunction =
1130 static constexpr
const char *pyClassName =
"DenseResourceElementsAttr";
1133 static PyDenseResourceElementsAttribute
1134 getFromBuffer(py::buffer buffer,
const std::string &name,
const PyType &type,
1135 std::optional<size_t> alignment,
bool isMutable,
1138 throw std::invalid_argument(
1139 "Constructing a DenseResourceElementsAttr requires a ShapedType.");
1144 int flags = PyBUF_STRIDES;
1145 std::unique_ptr<Py_buffer> view = std::make_unique<Py_buffer>();
1146 if (PyObject_GetBuffer(buffer.ptr(), view.get(), flags) != 0) {
1147 throw py::error_already_set();
1152 auto freeBuffer = llvm::make_scope_exit([&]() {
1154 PyBuffer_Release(view.get());
1157 if (!PyBuffer_IsContiguous(view.get(),
'A')) {
1158 throw std::invalid_argument(
"Contiguous buffer is required.");
1162 size_t inferredAlignment;
1164 inferredAlignment = *alignment;
1166 inferredAlignment = view->strides[view->ndim - 1];
1169 auto deleter = [](
void *userData,
const void *data,
size_t size,
1171 Py_buffer *ownedView =
static_cast<Py_buffer *
>(userData);
1172 PyBuffer_Release(ownedView);
1176 size_t rawBufferSize = view->len;
1179 inferredAlignment, isMutable, deleter,
static_cast<void *
>(view.get()));
1181 throw std::invalid_argument(
1182 "DenseResourceElementsAttr could not be constructed from the given "
1184 "This may mean that the Python buffer layout does not match that "
1185 "MLIR expected layout and is a bug.");
1188 return PyDenseResourceElementsAttribute(contextWrapper->getRef(), attr);
1191 static void bindDerived(ClassTy &c) {
1192 c.def_static(
"get_from_buffer",
1193 PyDenseResourceElementsAttribute::getFromBuffer,
1194 py::arg(
"array"), py::arg(
"name"), py::arg(
"type"),
1195 py::arg(
"alignment") = py::none(),
1196 py::arg(
"is_mutable") =
false, py::arg(
"context") = py::none(),
1204 static constexpr
const char *pyClassName =
"DictAttr";
1206 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1211 bool dunderContains(
const std::string &name) {
1216 static void bindDerived(ClassTy &c) {
1217 c.def(
"__contains__", &PyDictAttribute::dunderContains);
1218 c.def(
"__len__", &PyDictAttribute::dunderLen);
1223 mlirNamedAttributes.reserve(attributes.size());
1224 for (
auto &it : attributes) {
1226 auto name = it.first.cast<std::string>();
1232 MlirAttribute attr =
1234 mlirNamedAttributes.data());
1235 return PyDictAttribute(context->getRef(), attr);
1237 py::arg(
"value") = py::dict(), py::arg(
"context") = py::none(),
1238 "Gets an uniqued dict attribute");
1239 c.def(
"__getitem__", [](PyDictAttribute &
self,
const std::string &name) {
1240 MlirAttribute attr =
1243 throw py::key_error(
"attempt to access a non-existent attribute");
1246 c.def(
"__getitem__", [](PyDictAttribute &
self, intptr_t index) {
1247 if (index < 0 || index >=
self.dunderLen()) {
1248 throw py::index_error(
"attempt to access out of bounds attribute");
1260 class PyDenseFPElementsAttribute
1262 PyDenseElementsAttribute> {
1265 static constexpr
const char *pyClassName =
"DenseFPElementsAttr";
1268 py::float_ dunderGetItem(intptr_t pos) {
1269 if (pos < 0 || pos >= dunderLen()) {
1270 throw py::index_error(
"attempt to access out of bounds element");
1286 throw py::type_error(
"Unsupported floating-point type");
1289 static void bindDerived(ClassTy &c) {
1290 c.def(
"__getitem__", &PyDenseFPElementsAttribute::dunderGetItem);
1297 static constexpr
const char *pyClassName =
"TypeAttr";
1299 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1302 static void bindDerived(ClassTy &c) {
1307 return PyTypeAttribute(context->getRef(), attr);
1309 py::arg(
"value"), py::arg(
"context") = py::none(),
1310 "Gets a uniqued Type attribute");
1311 c.def_property_readonly(
"value", [](PyTypeAttribute &
self) {
1321 static constexpr
const char *pyClassName =
"UnitAttr";
1323 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1326 static void bindDerived(ClassTy &c) {
1330 return PyUnitAttribute(context->getRef(),
1333 py::arg(
"context") = py::none(),
"Create a Unit attribute.");
1338 class PyStridedLayoutAttribute
1342 static constexpr
const char *pyClassName =
"StridedLayoutAttr";
1344 static constexpr GetTypeIDFunctionTy getTypeIdFunction =
1347 static void bindDerived(ClassTy &c) {
1350 [](int64_t offset,
const std::vector<int64_t> strides,
1353 ctx->
get(), offset, strides.size(), strides.data());
1354 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1356 py::arg(
"offset"), py::arg(
"strides"), py::arg(
"context") = py::none(),
1357 "Gets a strided layout attribute.");
1359 "get_fully_dynamic",
1362 std::vector<int64_t> strides(rank);
1363 std::fill(strides.begin(), strides.end(), dynamic);
1365 ctx->
get(), dynamic, strides.size(), strides.data());
1366 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1368 py::arg(
"rank"), py::arg(
"context") = py::none(),
1369 "Gets a strided layout attribute with dynamic offset and strides of a "
1371 c.def_property_readonly(
1373 [](PyStridedLayoutAttribute &
self) {
1376 "Returns the value of the float point attribute");
1377 c.def_property_readonly(
1379 [](PyStridedLayoutAttribute &
self) {
1381 std::vector<int64_t> strides(size);
1382 for (intptr_t i = 0; i < size; i++) {
1387 "Returns the value of the float point attribute");
1391 py::object denseArrayAttributeCaster(
PyAttribute &pyAttribute) {
1392 if (PyDenseBoolArrayAttribute::isaFunction(pyAttribute))
1393 return py::cast(PyDenseBoolArrayAttribute(pyAttribute));
1394 if (PyDenseI8ArrayAttribute::isaFunction(pyAttribute))
1395 return py::cast(PyDenseI8ArrayAttribute(pyAttribute));
1396 if (PyDenseI16ArrayAttribute::isaFunction(pyAttribute))
1397 return py::cast(PyDenseI16ArrayAttribute(pyAttribute));
1398 if (PyDenseI32ArrayAttribute::isaFunction(pyAttribute))
1399 return py::cast(PyDenseI32ArrayAttribute(pyAttribute));
1400 if (PyDenseI64ArrayAttribute::isaFunction(pyAttribute))
1401 return py::cast(PyDenseI64ArrayAttribute(pyAttribute));
1402 if (PyDenseF32ArrayAttribute::isaFunction(pyAttribute))
1403 return py::cast(PyDenseF32ArrayAttribute(pyAttribute));
1404 if (PyDenseF64ArrayAttribute::isaFunction(pyAttribute))
1405 return py::cast(PyDenseF64ArrayAttribute(pyAttribute));
1407 std::string(
"Can't cast unknown element type DenseArrayAttr (") +
1408 std::string(py::repr(py::cast(pyAttribute))) +
")";
1409 throw py::cast_error(msg);
1412 py::object denseIntOrFPElementsAttributeCaster(
PyAttribute &pyAttribute) {
1413 if (PyDenseFPElementsAttribute::isaFunction(pyAttribute))
1414 return py::cast(PyDenseFPElementsAttribute(pyAttribute));
1415 if (PyDenseIntElementsAttribute::isaFunction(pyAttribute))
1416 return py::cast(PyDenseIntElementsAttribute(pyAttribute));
1419 "Can't cast unknown element type DenseIntOrFPElementsAttr (") +
1420 std::string(py::repr(py::cast(pyAttribute))) +
")";
1421 throw py::cast_error(msg);
1424 py::object integerOrBoolAttributeCaster(
PyAttribute &pyAttribute) {
1425 if (PyBoolAttribute::isaFunction(pyAttribute))
1426 return py::cast(PyBoolAttribute(pyAttribute));
1427 if (PyIntegerAttribute::isaFunction(pyAttribute))
1428 return py::cast(PyIntegerAttribute(pyAttribute));
1430 std::string(
"Can't cast unknown element type DenseArrayAttr (") +
1431 std::string(py::repr(py::cast(pyAttribute))) +
")";
1432 throw py::cast_error(msg);
1435 py::object symbolRefOrFlatSymbolRefAttributeCaster(
PyAttribute &pyAttribute) {
1436 if (PyFlatSymbolRefAttribute::isaFunction(pyAttribute))
1437 return py::cast(PyFlatSymbolRefAttribute(pyAttribute));
1438 if (PySymbolRefAttribute::isaFunction(pyAttribute))
1439 return py::cast(PySymbolRefAttribute(pyAttribute));
1440 std::string msg = std::string(
"Can't cast unknown SymbolRef attribute (") +
1441 std::string(py::repr(py::cast(pyAttribute))) +
")";
1442 throw py::cast_error(msg);
1448 PyAffineMapAttribute::bind(m);
1449 PyDenseBoolArrayAttribute::bind(m);
1450 PyDenseBoolArrayAttribute::PyDenseArrayIterator::bind(m);
1451 PyDenseI8ArrayAttribute::bind(m);
1452 PyDenseI8ArrayAttribute::PyDenseArrayIterator::bind(m);
1453 PyDenseI16ArrayAttribute::bind(m);
1454 PyDenseI16ArrayAttribute::PyDenseArrayIterator::bind(m);
1455 PyDenseI32ArrayAttribute::bind(m);
1456 PyDenseI32ArrayAttribute::PyDenseArrayIterator::bind(m);
1457 PyDenseI64ArrayAttribute::bind(m);
1458 PyDenseI64ArrayAttribute::PyDenseArrayIterator::bind(m);
1459 PyDenseF32ArrayAttribute::bind(m);
1460 PyDenseF32ArrayAttribute::PyDenseArrayIterator::bind(m);
1461 PyDenseF64ArrayAttribute::bind(m);
1462 PyDenseF64ArrayAttribute::PyDenseArrayIterator::bind(m);
1465 pybind11::cpp_function(denseArrayAttributeCaster));
1467 PyArrayAttribute::bind(m);
1468 PyArrayAttribute::PyArrayAttributeIterator::bind(m);
1469 PyBoolAttribute::bind(m);
1470 PyDenseElementsAttribute::bind(m);
1471 PyDenseFPElementsAttribute::bind(m);
1472 PyDenseIntElementsAttribute::bind(m);
1475 pybind11::cpp_function(denseIntOrFPElementsAttributeCaster));
1476 PyDenseResourceElementsAttribute::bind(m);
1478 PyDictAttribute::bind(m);
1479 PySymbolRefAttribute::bind(m);
1482 pybind11::cpp_function(symbolRefOrFlatSymbolRefAttributeCaster));
1484 PyFlatSymbolRefAttribute::bind(m);
1485 PyOpaqueAttribute::bind(m);
1486 PyFloatAttribute::bind(m);
1487 PyIntegerAttribute::bind(m);
1488 PyIntegerSetAttribute::bind(m);
1489 PyStringAttribute::bind(m);
1490 PyTypeAttribute::bind(m);
1493 pybind11::cpp_function(integerOrBoolAttributeCaster));
1494 PyUnitAttribute::bind(m);
1496 PyStridedLayoutAttribute::bind(m);
static const char kDenseElementsAttrGetDocstring[]
static const char kDenseResourceElementsAttrGetFromBufferDocstring[]
static const char kDenseElementsAttrGetFromListDocstring[]
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.
MlirIntegerSet get() const
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 MlirAffineMap mlirAffineMapAttrGetValue(MlirAttribute attr)
Returns the affine map wrapped in the given affine map attribute.
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 mlirAttributeIsAIntegerSet(MlirAttribute attr)
Checks whether the given attribute is an integer set 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 MlirAttribute mlirIntegerSetAttrGet(MlirIntegerSet set)
Creates an integer set attribute wrapping the given set.
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 MlirTypeID mlirIntegerSetAttrGetTypeID(void)
Returns the typeID of an IntegerSet attribute.
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 MlirAttribute mlirDenseElementsAttrGet(MlirType shapedType, intptr_t numElements, MlirAttribute const *elements)
Creates a dense elements attribute with the given Shaped type and elements in the same context as the...
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.