9#ifndef MLIR_BINDINGS_PYTHON_IRATTRIBUTES_H
10#define MLIR_BINDINGS_PYTHON_IRATTRIBUTES_H
42 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)> owned_view_in =
43 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)>(
nullptr,
nullptr));
47 {view->shape, view->shape + view->ndim},
49 {view->strides, view->strides + view->ndim},
51 std::unique_ptr<Py_buffer, void (*)(Py_buffer *)>(
52 view, PyBuffer_Release)) {}
60 std::unique_ptr<Py_buffer,
void (*)(Py_buffer *)> owned_view;
64 NB_OBJECT_DEFAULT(
nb_buffer,
object,
"Buffer", PyObject_CheckBuffer);
101 return nanobind::cast<T>(
object);
102 }
catch (nanobind::cast_error &err) {
103 std::string msg = std::string(
"Invalid attribute when attempting to "
104 "create an ArrayAttribute (") +
106 throw std::runtime_error(msg.c_str());
107 }
catch (std::runtime_error &err) {
108 std::string msg = std::string(
"Invalid attribute (None?) when attempting "
109 "to create an ArrayAttribute (") +
111 throw std::runtime_error(msg.c_str());
117template <
typename EltTy,
typename DerivedT>
135 throw nanobind::stop_iteration();
136 return DerivedT::getElement(attr.get(), nextIndex++);
140 static void bind(nanobind::module_ &m) {
141 nanobind::class_<PyDenseArrayIterator>(m, DerivedT::pyIteratorName)
159 if constexpr (std::is_same_v<EltTy, bool>) {
163 std::vector<bool> values;
164 for (nanobind::handle py_value : py_values) {
165 int is_true = PyObject_IsTrue(py_value.ptr());
167 throw nanobind::python_error();
169 values.push_back(is_true);
171 return getAttribute(values, ctx->getRef());
173 nanobind::arg(
"values"), nanobind::arg(
"context") = nanobind::none(),
174 "Gets a uniqued dense array attribute");
179 return getAttribute(values, ctx->getRef());
181 nanobind::arg(
"values"), nanobind::arg(
"context") = nanobind::none(),
182 "Gets a uniqued dense array attribute");
185 c.def(
"__getitem__", [](DerivedT &arr,
intptr_t i) {
187 throw nanobind::index_error(
"DenseArray index out of range");
188 return arr.getItem(i);
190 c.def(
"__len__", [](
const DerivedT &arr) {
195 c.def(
"__add__", [](DerivedT &arr,
const nanobind::list &extras) {
196 std::vector<EltTy> values;
198 values.reserve(numOldElements + nanobind::len(extras));
199 for (
intptr_t i = 0; i < numOldElements; ++i)
200 values.push_back(arr.getItem(i));
201 for (nanobind::handle attr : extras)
203 return getAttribute(values, arr.getContext());
208 static DerivedT getAttribute(
const std::vector<EltTy> &values,
210 if constexpr (std::is_same_v<EltTy, bool>) {
211 std::vector<int> intValues(values.begin(), values.end());
212 MlirAttribute attr = DerivedT::getAttribute(ctx->
get(), intValues.size(),
214 return DerivedT(ctx, attr);
217 DerivedT::getAttribute(ctx->
get(), values.size(), values.data());
218 return DerivedT(ctx, attr);
231 using PyDenseArrayAttribute::PyDenseArrayAttribute;
240 using PyDenseArrayAttribute::PyDenseArrayAttribute;
249 using PyDenseArrayAttribute::PyDenseArrayAttribute;
258 using PyDenseArrayAttribute::PyDenseArrayAttribute;
267 using PyDenseArrayAttribute::PyDenseArrayAttribute;
276 using PyDenseArrayAttribute::PyDenseArrayAttribute;
285 using PyDenseArrayAttribute::PyDenseArrayAttribute;
304 nanobind::typed<nanobind::object, PyAttribute> dunderNext();
306 static void bind(nanobind::module_ &m);
313 MlirAttribute getItem(
intptr_t i)
const;
315 static void bindDerived(ClassTy &c);
405 std::optional<PyType> explicitType,
410 const std::optional<PyType> &explicitType,
411 std::optional<std::vector<int64_t>> explicitShape,
426 static int bf_getbuffer(PyObject *exporter, Py_buffer *view,
int flags);
427 static void bf_releasebuffer(PyObject *, Py_buffer *buffer);
429 static bool isUnsignedIntegerFormat(std::string_view format);
431 static bool isSignedIntegerFormat(std::string_view format);
434 getShapedType(std::optional<MlirType> bulkLoadElementType,
435 std::optional<std::vector<int64_t>> explicitShape,
438 static MlirAttribute getAttributeFromBuffer(
439 Py_buffer &view,
bool signless, std::optional<PyType> explicitType,
440 const std::optional<std::vector<int64_t>> &explicitShape,
441 MlirContext &context);
446 static MlirAttribute getBitpackedAttributeFromBooleanBuffer(
447 Py_buffer &view, std::optional<std::vector<int64_t>> explicitShape,
448 MlirContext &context);
452 std::unique_ptr<nb_buffer_info>
453 getBooleanBufferFromBitpackedAttribute()
const;
455 template <
typename Type>
456 std::unique_ptr<nb_buffer_info>
457 bufferInfo(MlirType shapedType,
const char *explicitFormat =
nullptr) {
471 strides.assign(rank, 0);
473 for (
intptr_t i = 1; i < rank; ++i) {
477 strides.push_back(
sizeof(
Type) * strideFactor);
479 strides.push_back(
sizeof(
Type));
482 if (explicitFormat) {
483 format = explicitFormat;
485 format = nb_format_descriptor<Type>::format();
487 return std::make_unique<nb_buffer_info>(
488 data,
sizeof(Type), format, rank, std::move(shape), std::move(strides),
497 PyDenseElementsAttribute> {
500 static constexpr const char *
pyClassName =
"DenseIntElementsAttr";
515 static constexpr const char *
pyClassName =
"DenseResourceElementsAttr";
522 const PyType &type, std::optional<size_t> alignment,
549 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 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 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
SmallVector< ssize_t, 4 > strides
nb_buffer_info & operator=(nb_buffer_info &&)=default
nb_buffer_info(nb_buffer_info &&)=default
nb_buffer_info(Py_buffer *view)
nb_buffer_info(void *ptr, ssize_t itemsize, const char *format, ssize_t ndim, SmallVector< ssize_t, 4 > shape_in, SmallVector< ssize_t, 4 > 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))
SmallVector< ssize_t, 4 > shape
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.