9#ifndef MLIR_BINDINGS_PYTHON_IRATTRIBUTES_H
10#define MLIR_BINDINGS_PYTHON_IRATTRIBUTES_H
41 std::vector<Py_ssize_t> shape_in, std::vector<Py_ssize_t> strides_in,
43 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)> owned_view_in =
44 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)>(
nullptr,
nullptr));
48 {view->shape, view->shape + view->ndim},
50 {view->strides, view->strides + view->ndim},
52 std::unique_ptr<Py_buffer, void (*)(Py_buffer *)>(
53 view, PyBuffer_Release)) {}
61 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)> owned_view;
65 NB_OBJECT_DEFAULT(
nb_buffer,
object,
"Buffer", PyObject_CheckBuffer);
102 return nanobind::cast<T>(
object);
103 }
catch (nanobind::cast_error &err) {
104 std::string msg = std::string(
"Invalid attribute when attempting to "
105 "create an ArrayAttribute (") +
107 throw std::runtime_error(msg.c_str());
108 }
catch (std::runtime_error &err) {
109 std::string msg = std::string(
"Invalid attribute (None?) when attempting "
110 "to create an ArrayAttribute (") +
112 throw std::runtime_error(msg.c_str());
118template <
typename EltTy,
typename DerivedT>
136 throw nanobind::stop_iteration();
137 return DerivedT::getElement(attr.get(), nextIndex++);
141 static void bind(nanobind::module_ &m) {
142 nanobind::class_<PyDenseArrayIterator>(m, DerivedT::pyIteratorName)
160 if constexpr (std::is_same_v<EltTy, bool>) {
164 std::vector<bool> values;
165 for (nanobind::handle py_value : py_values) {
166 int is_true = PyObject_IsTrue(py_value.ptr());
168 throw nanobind::python_error();
170 values.push_back(is_true);
172 return getAttribute(values, ctx->getRef());
174 nanobind::arg(
"values"), nanobind::arg(
"context") = nanobind::none(),
175 "Gets a uniqued dense array attribute");
180 return getAttribute(values, ctx->getRef());
182 nanobind::arg(
"values"), nanobind::arg(
"context") = nanobind::none(),
183 "Gets a uniqued dense array attribute");
186 c.def(
"__getitem__", [](DerivedT &arr,
intptr_t i) {
188 throw nanobind::index_error(
"DenseArray index out of range");
189 return arr.getItem(i);
191 c.def(
"__len__", [](
const DerivedT &arr) {
196 c.def(
"__add__", [](DerivedT &arr,
const nanobind::list &extras) {
197 std::vector<EltTy> values;
199 values.reserve(numOldElements + nanobind::len(extras));
200 for (
intptr_t i = 0; i < numOldElements; ++i)
201 values.push_back(arr.getItem(i));
202 for (nanobind::handle attr : extras)
204 return getAttribute(values, arr.getContext());
209 static DerivedT getAttribute(
const std::vector<EltTy> &values,
211 if constexpr (std::is_same_v<EltTy, bool>) {
212 std::vector<int> intValues(values.begin(), values.end());
213 MlirAttribute attr = DerivedT::getAttribute(ctx->
get(), intValues.size(),
215 return DerivedT(ctx, attr);
218 DerivedT::getAttribute(ctx->
get(), values.size(), values.data());
219 return DerivedT(ctx, attr);
232 using PyDenseArrayAttribute::PyDenseArrayAttribute;
241 using PyDenseArrayAttribute::PyDenseArrayAttribute;
250 using PyDenseArrayAttribute::PyDenseArrayAttribute;
259 using PyDenseArrayAttribute::PyDenseArrayAttribute;
268 using PyDenseArrayAttribute::PyDenseArrayAttribute;
277 using PyDenseArrayAttribute::PyDenseArrayAttribute;
286 using PyDenseArrayAttribute::PyDenseArrayAttribute;
305 nanobind::typed<nanobind::object, PyAttribute> dunderNext();
307 static void bind(nanobind::module_ &m);
314 MlirAttribute getItem(
intptr_t i)
const;
316 static void bindDerived(ClassTy &c);
407 std::optional<PyType> explicitType,
412 const std::optional<PyType> &explicitType,
413 std::optional<std::vector<int64_t>> explicitShape,
428 static int bf_getbuffer(PyObject *exporter, Py_buffer *view,
int flags);
429 static void bf_releasebuffer(PyObject *, Py_buffer *buffer);
431 static bool isUnsignedIntegerFormat(std::string_view format);
433 static bool isSignedIntegerFormat(std::string_view format);
436 getShapedType(std::optional<MlirType> bulkLoadElementType,
437 std::optional<std::vector<int64_t>> explicitShape,
440 static MlirAttribute getAttributeFromBuffer(
441 Py_buffer &view,
bool signless, std::optional<PyType> explicitType,
442 const std::optional<std::vector<int64_t>> &explicitShape,
443 MlirContext &context);
445 template <
typename Type>
446 std::unique_ptr<nb_buffer_info>
447 bufferInfo(MlirType shapedType,
const char *explicitFormat =
nullptr) {
454 std::vector<Py_ssize_t>
shape;
458 std::vector<Py_ssize_t> strides;
461 strides.assign(rank, 0);
463 for (
intptr_t i = 1; i < rank; ++i) {
467 strides.push_back(
sizeof(
Type) * strideFactor);
469 strides.push_back(
sizeof(
Type));
472 if (explicitFormat) {
473 format = explicitFormat;
475 format = nb_format_descriptor<Type>::format();
477 return std::make_unique<nb_buffer_info>(
478 data,
sizeof(Type), format, rank, std::move(shape), std::move(strides),
487 PyDenseElementsAttribute> {
490 static constexpr const char *
pyClassName =
"DenseIntElementsAttr";
505 static constexpr const char *
pyClassName =
"DenseResourceElementsAttr";
512 const PyType &type, std::optional<size_t> alignment,
539 PyDenseElementsAttribute> {
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...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Used in function arguments when None should resolve to the current context manager set instance.
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
static constexpr const char * pyClassName
static constexpr GetTypeIDFunctionTy getTypeIdFunction
PyConcreteAttribute()=default
static const MlirStringRef name
PyArrayAttributeIterator(PyAttribute attr)
PyArrayAttributeIterator & dunderIter()
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static const MlirStringRef name
PyConcreteAttribute()=default
Wrapper around the generic MlirAttribute.
Bool Attribute subclass - BoolAttr.
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
PyConcreteAttribute()=default
static void bind(nanobind::module_ &m, PyType_Slot *slots=nullptr)
nanobind::class_< PyAffineMapAttribute, PyAttribute > ClassTy
PyConcreteAttribute()=default
bool(*)(MlirAttribute) IsAFunctionTy
MlirTypeID(*)() GetTypeIDFunctionTy
Iterator over the integer elements of a dense array.
static void bind(nanobind::module_ &m)
Bind the iterator class.
PyDenseArrayIterator(PyAttribute attr)
EltTy dunderNext()
Return the next element.
PyDenseArrayIterator dunderIter()
Return a copy of the iterator.
A python-wrapped dense array attribute with an element type and a derived implementation class.
static void bindDerived(typename PyConcreteAttribute< DerivedT >::ClassTy &c)
Bind the attribute class.
EltTy getItem(intptr_t i)
Get the element at the given index.
static constexpr IsAFunctionTy isaFunction
static PyDenseElementsAttribute getSplat(const PyType &shapedType, PyAttribute &elementAttr)
std::unique_ptr< nb_buffer_info > accessBuffer()
PyConcreteAttribute()=default
static constexpr const char * pyClassName
intptr_t dunderLen() const
static PyType_Slot slots[]
static void bindDerived(ClassTy &c)
static PyDenseElementsAttribute getFromList(const nanobind::list &attributes, std::optional< PyType > explicitType, DefaultingPyMlirContext contextWrapper)
static PyDenseElementsAttribute getFromBuffer(const nb_buffer &array, bool signless, const std::optional< PyType > &explicitType, std::optional< std::vector< int64_t > > explicitShape, DefaultingPyMlirContext contextWrapper)
Refinement of PyDenseElementsAttribute for attributes containing floating-point values.
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
nanobind::float_ dunderGetItem(intptr_t pos) const
PyConcreteAttribute()=default
static constexpr const char * pyClassName
Refinement of the PyDenseElementsAttribute for attributes containing integer (and boolean) values.
static void bindDerived(ClassTy &c)
nanobind::int_ dunderGetItem(intptr_t pos) const
Returns the element at the given linear position.
static constexpr IsAFunctionTy isaFunction
PyConcreteAttribute()=default
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyClassName
static PyDenseResourceElementsAttribute getFromBuffer(const nb_buffer &buffer, const std::string &name, const PyType &type, std::optional< size_t > alignment, bool isMutable, DefaultingPyMlirContext contextWrapper)
static void bindDerived(ClassTy &c)
PyConcreteAttribute()=default
static const MlirStringRef name
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
static constexpr const char * pyClassName
intptr_t dunderLen() const
bool dunderContains(const std::string &name) const
static const MlirStringRef name
PyConcreteAttribute()=default
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static const MlirStringRef name
static void bindDerived(ClassTy &c)
PyConcreteAttribute()=default
Float Point Attribute subclass - FloatAttr.
static constexpr IsAFunctionTy isaFunction
static const MlirStringRef name
static void bindDerived(ClassTy &c)
PyConcreteAttribute()=default
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
Integer Attribute subclass - IntegerAttr.
static constexpr const char * pyClassName
static const MlirStringRef name
static constexpr IsAFunctionTy isaFunction
PyConcreteAttribute()=default
static void bindDerived(ClassTy &c)
static constexpr const char * pyClassName
static const MlirStringRef name
static constexpr IsAFunctionTy isaFunction
PyConcreteAttribute()=default
static void bindDerived(ClassTy &c)
static constexpr GetTypeIDFunctionTy getTypeIdFunction
MlirContext get()
Accesses the underlying MlirContext.
static const MlirStringRef name
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
PyConcreteAttribute()=default
static constexpr IsAFunctionTy isaFunction
static void bindDerived(ClassTy &c)
Strided layout attribute subclass.
static constexpr IsAFunctionTy isaFunction
static const MlirStringRef name
static void bindDerived(ClassTy &c)
static constexpr GetTypeIDFunctionTy getTypeIdFunction
PyConcreteAttribute()=default
static constexpr const char * pyClassName
static const MlirStringRef name
static constexpr const char * pyClassName
static void bindDerived(ClassTy &c)
static PySymbolRefAttribute fromList(const std::vector< std::string > &symbols, PyMlirContext &context)
PyConcreteAttribute()=default
static constexpr IsAFunctionTy isaFunction
static constexpr IsAFunctionTy isaFunction
static const MlirStringRef name
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr const char * pyClassName
PyConcreteAttribute()=default
static void bindDerived(ClassTy &c)
Wrapper around the generic MlirType.
Unit Attribute subclass. Unit attributes don't have values.
static const MlirStringRef name
static constexpr GetTypeIDFunctionTy getTypeIdFunction
PyConcreteAttribute()=default
static void bindDerived(ClassTy &c)
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseFPElements(MlirAttribute attr)
MLIR_CAPI_EXPORTED int16_t mlirDenseI16ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAStridedLayout(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI64Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirStringRef mlirStridedLayoutAttrGetName(void)
MLIR_CAPI_EXPORTED MlirStringRef mlirTypeAttrGetName(void)
MLIR_CAPI_EXPORTED MlirStringRef mlirIntegerAttrGetName(void)
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 MlirStringRef mlirIntegerSetAttrGetName(void)
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 MlirStringRef mlirUnitAttrGetName(void)
MLIR_CAPI_EXPORTED MlirTypeID mlirStridedLayoutAttrGetTypeID(void)
Returns the typeID of a StridedLayout attribute.
MLIR_CAPI_EXPORTED const void * mlirDenseElementsAttrGetRawData(MlirAttribute attr)
Returns the raw data of the given dense elements attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseResourceElements(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAType(MlirAttribute attr)
Checks whether the given attribute is a type 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 MlirStringRef mlirArrayAttrGetName(void)
MLIR_CAPI_EXPORTED MlirStringRef mlirDictionaryAttrGetName(void)
MLIR_CAPI_EXPORTED int64_t mlirDenseI64ArrayGetElement(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirTypeID mlirIntegerSetAttrGetTypeID(void)
Returns the typeID of an IntegerSet attribute.
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 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 MlirStringRef mlirFloatAttrGetName(void)
MLIR_CAPI_EXPORTED bool mlirDenseElementsAttrIsSplat(MlirAttribute attr)
Checks whether the given dense elements attribute contains a single replicated value (splat).
MLIR_CAPI_EXPORTED MlirStringRef mlirFlatSymbolRefAttrGetName(void)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseBoolArray(MlirAttribute attr)
Checks whether the given attribute is a dense array attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirFloatAttrGetTypeID(void)
Returns the typeID of a Float attribute.
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 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 MlirStringRef mlirAffineMapAttrGetName(void)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseF32Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsASymbolRef(MlirAttribute attr)
Checks whether the given attribute is a symbol reference attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADictionary(MlirAttribute attr)
Checks whether the given attribute is a dictionary attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI16ArrayGet(MlirContext ctx, intptr_t size, int16_t const *values)
MLIR_CAPI_EXPORTED MlirStringRef mlirOpaqueAttrGetName(void)
MLIR_CAPI_EXPORTED MlirTypeID mlirOpaqueAttrGetTypeID(void)
Returns the typeID of an Opaque attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI8ArrayGet(MlirContext ctx, intptr_t size, int8_t const *values)
MLIR_CAPI_EXPORTED MlirStringRef mlirDenseResourceElementsAttrGetName(void)
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 MlirTypeID mlirTypeAttrGetTypeID(void)
Returns the typeID of a Type attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirDictionaryAttrGetTypeID(void)
Returns the typeID of a Dictionary attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI32Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI8Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsAFlatSymbolRef(MlirAttribute attr)
Checks whether the given attribute is a flat symbol reference attribute.
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseI16Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool mlirAttributeIsADenseF64Array(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolRefAttrGetName(void)
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 int64_t mlirShapedTypeGetRank(MlirType type)
Returns the rank of the given ranked shaped type.
#define MLIR_PYTHON_API_EXPORTED
PyObjectRef< PyMlirContext > PyMlirContextRef
Wrapper around MlirContext.
static T pyTryCast(nanobind::handle object)
MLIR_PYTHON_API_EXPORTED void populateIRAttributes(nanobind::module_ &m)
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Instantiate the python dense array classes.
static constexpr auto getAttribute
static constexpr const char * pyClassName
static constexpr auto getElement
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyIteratorName
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyIteratorName
static constexpr const char * pyClassName
static constexpr auto getElement
static constexpr auto getAttribute
static constexpr const char * pyClassName
static constexpr IsAFunctionTy isaFunction
static constexpr auto getElement
static constexpr const char * pyIteratorName
static constexpr auto getAttribute
static constexpr auto getAttribute
static constexpr const char * pyIteratorName
static constexpr IsAFunctionTy isaFunction
static constexpr auto getElement
static constexpr const char * pyClassName
static constexpr auto getAttribute
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyIteratorName
static constexpr const char * pyClassName
static constexpr auto getElement
static constexpr auto getAttribute
static constexpr const char * pyIteratorName
static constexpr const char * pyClassName
static constexpr auto getElement
static constexpr IsAFunctionTy isaFunction
static constexpr IsAFunctionTy isaFunction
static constexpr const char * pyIteratorName
static constexpr auto getAttribute
static constexpr auto getElement
static constexpr const char * pyClassName
nb_buffer_info & operator=(nb_buffer_info &&)=default
nb_buffer_info(nb_buffer_info &&)=default
nb_buffer_info(void *ptr, Py_ssize_t itemsize, const char *format, Py_ssize_t ndim, std::vector< Py_ssize_t > shape_in, std::vector< Py_ssize_t > strides_in, bool readonly=false, std::unique_ptr< Py_buffer, void(*)(Py_buffer *)> owned_view_in=std::unique_ptr< Py_buffer, void(*)(Py_buffer *)>(nullptr, nullptr))
nb_buffer_info(Py_buffer *view)
std::vector< Py_ssize_t > shape
std::vector< Py_ssize_t > strides
nb_buffer_info & operator=(const nb_buffer_info &)=delete
nb_buffer_info(const nb_buffer_info &)=delete
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.