MLIR 23.0.0git
IRAttributes.cpp
Go to the documentation of this file.
1//===- IRAttributes.cpp - Exports builtin and standard attributes ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <algorithm>
10#include <cmath>
11#include <cstdint>
12#include <cstring>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
20#include "mlir-c/BuiltinTypes.h"
27
28namespace nb = nanobind;
29using namespace nanobind::literals;
30using namespace mlir;
32
33//------------------------------------------------------------------------------
34// Docstrings (trivial, non-duplicated docstrings are included inline).
35//------------------------------------------------------------------------------
36
37static const char kDenseElementsAttrGetDocstring[] =
38 R"(Gets a DenseElementsAttr from a Python buffer or array.
39
40When `type` is not provided, then some limited type inferencing is done based
41on the buffer format. Support presently exists for 8/16/32/64 signed and
42unsigned integers and float16/float32/float64. DenseElementsAttrs of these
43types can also be converted back to a corresponding buffer.
44
45For conversions outside of these types, a `type=` must be explicitly provided
46and the buffer contents must be bit-castable to the MLIR internal
47representation:
48
49 * Integer types: the buffer must be byte aligned to the next byte boundary.
50 * Floating point types: Must be bit-castable to the given floating point
51 size.
52 * i1 (bool): Each boolean value is stored as a single byte (0 or 1).
53
54If a single element buffer is passed, then a splat will be created.
55
56Args:
57 array: The array or buffer to convert.
58 signless: If inferring an appropriate MLIR type, use signless types for
59 integers (defaults True).
60 type: Skips inference of the MLIR element type and uses this instead. The
61 storage size must be consistent with the actual contents of the buffer.
62 shape: Overrides the shape of the buffer when constructing the MLIR
63 shaped type. This is needed when the physical and logical shape differ.
64 context: Explicit context, if not from context manager.
65
66Returns:
67 DenseElementsAttr on success.
68
69Raises:
70 ValueError: If the type of the buffer or array cannot be matched to an MLIR
71 type or if the buffer does not meet expectations.
72)";
73
75 R"(Gets a DenseElementsAttr from a Python list of attributes.
76
77Note that it can be expensive to construct attributes individually.
78For a large number of elements, consider using a Python buffer or array instead.
79
80Args:
81 attrs: A list of attributes.
82 type: The desired shape and type of the resulting DenseElementsAttr.
83 If not provided, the element type is determined based on the type
84 of the 0th attribute and the shape is `[len(attrs)]`.
85 context: Explicit context, if not from context manager.
86
87Returns:
88 DenseElementsAttr on success.
89
90Raises:
91 ValueError: If the type of the attributes does not match the type
92 specified by `shaped_type`.
93)";
94
96 R"(Gets a DenseResourceElementsAttr from a Python buffer or array.
97
98This function does minimal validation or massaging of the data, and it is
99up to the caller to ensure that the buffer meets the characteristics
100implied by the shape.
101
102The backing buffer and any user objects will be retained for the lifetime
103of the resource blob. This is typically bounded to the context but the
104resource can have a shorter lifespan depending on how it is used in
105subsequent processing.
106
107Args:
108 buffer: The array or buffer to convert.
109 name: Name to provide to the resource (may be changed upon collision).
110 type: The explicit ShapedType to construct the attribute with.
111 context: Explicit context, if not from context manager.
112
113Returns:
114 DenseResourceElementsAttr on success.
115
116Raises:
117 ValueError: If the type of the buffer or array cannot be matched to an MLIR
118 type or if the buffer does not meet expectations.
119)";
120
121namespace {
122/// Local helper adapted from llvm::scope_exit.
123template <typename Callable>
124class [[nodiscard]] scope_exit {
125 Callable ExitFunction;
126 bool Engaged = true; // False once moved-from or release()d.
127
128public:
129 template <typename Fp>
130 explicit scope_exit(Fp &&F) : ExitFunction(std::forward<Fp>(F)) {}
131
132 scope_exit(scope_exit &&Rhs)
133 : ExitFunction(std::move(Rhs.ExitFunction)), Engaged(Rhs.Engaged) {
134 Rhs.release();
135 }
136 scope_exit(const scope_exit &) = delete;
137 scope_exit &operator=(scope_exit &&) = delete;
138 scope_exit &operator=(const scope_exit &) = delete;
139
140 void release() { Engaged = false; }
141
142 ~scope_exit() {
143 if (Engaged)
144 ExitFunction();
145 }
146};
147
148template <typename Callable>
149scope_exit(Callable) -> scope_exit<Callable>;
150} // namespace
151
152namespace mlir {
153namespace python {
155
157 void *ptr, Py_ssize_t itemsize, const char *format, Py_ssize_t ndim,
158 std::vector<Py_ssize_t> shape_in, std::vector<Py_ssize_t> strides_in,
159 bool readonly,
160 std::unique_ptr<Py_buffer, void (*)(Py_buffer *)> owned_view_in)
162 shape(std::move(shape_in)), strides(std::move(strides_in)),
163 readonly(readonly), owned_view(std::move(owned_view_in)) {
164 size = 1;
165 for (Py_ssize_t i = 0; i < ndim; ++i) {
166 size *= shape[i];
167 }
168}
169
170nb_buffer_info nb_buffer::request() const {
171 int flags = PyBUF_STRIDES | PyBUF_FORMAT;
172 auto *view = new Py_buffer();
173 if (PyObject_GetBuffer(ptr(), view, flags) != 0) {
174 delete view;
175 throw nb::python_error();
176 }
177 return nb_buffer_info(view);
178}
179
180template <>
182 static const char *format() { return "?"; }
183};
184template <>
185struct nb_format_descriptor<int8_t> {
186 static const char *format() { return "b"; }
187};
188template <>
189struct nb_format_descriptor<uint8_t> {
190 static const char *format() { return "B"; }
191};
192template <>
193struct nb_format_descriptor<int16_t> {
194 static const char *format() { return "h"; }
195};
196template <>
197struct nb_format_descriptor<uint16_t> {
198 static const char *format() { return "H"; }
199};
200template <>
201struct nb_format_descriptor<int32_t> {
202 static const char *format() { return "i"; }
203};
204template <>
205struct nb_format_descriptor<uint32_t> {
206 static const char *format() { return "I"; }
207};
208template <>
210 static const char *format() { return "q"; }
211};
212template <>
213struct nb_format_descriptor<uint64_t> {
214 static const char *format() { return "Q"; }
215};
216template <>
217struct nb_format_descriptor<float> {
218 static const char *format() { return "f"; }
219};
220template <>
221struct nb_format_descriptor<double> {
222 static const char *format() { return "d"; }
223};
224
226 c.def_static(
227 "get",
228 [](PyAffineMap &affineMap) {
229 MlirAttribute attr = mlirAffineMapAttrGet(affineMap.get());
230 return PyAffineMapAttribute(affineMap.getContext(), attr);
231 },
232 nb::arg("affine_map"), "Gets an attribute wrapping an AffineMap.");
233 c.def_prop_ro(
234 "value",
235 [](PyAffineMapAttribute &self) {
237 },
238 "Returns the value of the AffineMap attribute");
239}
240
242 c.def_static(
243 "get",
244 [](PyIntegerSet &integerSet) {
245 MlirAttribute attr = mlirIntegerSetAttrGet(integerSet.get());
246 return PyIntegerSetAttribute(integerSet.getContext(), attr);
247 },
248 nb::arg("integer_set"), "Gets an attribute wrapping an IntegerSet.");
249}
250
251nb::typed<nb::object, PyAttribute>
253 // TODO: Throw is an inefficient way to stop iteration.
254 if (PyArrayAttribute::PyArrayAttributeIterator::nextIndex >=
256 PyArrayAttribute::PyArrayAttributeIterator::attr.get())) {
257 PyErr_SetNone(PyExc_StopIteration);
258 // python functions should return NULL after setting any exception
259 return nb::object();
260 }
261 return PyAttribute(
262 this->PyArrayAttribute::PyArrayAttributeIterator::attr
263 .getContext(),
265 PyArrayAttribute::PyArrayAttributeIterator::attr.get(),
266 PyArrayAttribute::PyArrayAttributeIterator::nextIndex++))
267 .maybeDownCast();
268}
269
271 nb::class_<PyArrayAttributeIterator>(m, "ArrayAttributeIterator")
272 .def("__iter__", &PyArrayAttributeIterator::dunderIter)
273 .def("__next__", &PyArrayAttributeIterator::dunderNext);
274}
275
276MlirAttribute PyArrayAttribute::getItem(intptr_t i) const {
277 return mlirArrayAttrGetElement(*this, i);
278}
279
281 c.def_static(
282 "get",
283 [](nb::typed<nb::sequence, PyAttribute> attributes,
284 DefaultingPyMlirContext context) {
285 std::vector<MlirAttribute> mlirAttributes;
286 mlirAttributes.reserve(nb::len(attributes));
287 for (auto attribute : attributes) {
288 mlirAttributes.push_back(pyTryCast<PyAttribute>(attribute));
289 }
290 MlirAttribute attr = mlirArrayAttrGet(
291 context->get(), mlirAttributes.size(), mlirAttributes.data());
292 return PyArrayAttribute(context->getRef(), attr);
293 },
294 nb::arg("attributes"), nb::arg("context") = nb::none(),
295 "Gets a uniqued Array attribute");
296 c.def("__getitem__",
297 [](PyArrayAttribute &arr,
298 intptr_t i) -> nb::typed<nb::object, PyAttribute> {
299 if (i >= mlirArrayAttrGetNumElements(arr))
300 throw nb::index_error("ArrayAttribute index out of range");
301 return PyAttribute(arr.getContext(), arr.getItem(i)).maybeDownCast();
302 })
303 .def("__len__",
304 [](const PyArrayAttribute &arr) {
305 return mlirArrayAttrGetNumElements(arr);
306 })
307 .def("__iter__", [](const PyArrayAttribute &arr) {
308 return PyArrayAttributeIterator(arr);
309 });
310 c.def("__add__", [](PyArrayAttribute arr,
311 nb::typed<nb::sequence, PyAttribute> extras) {
312 std::vector<MlirAttribute> attributes;
313 intptr_t numOldElements = mlirArrayAttrGetNumElements(arr);
314 attributes.reserve(numOldElements + nb::len(extras));
315 for (intptr_t i = 0; i < numOldElements; ++i)
316 attributes.push_back(arr.getItem(i));
317 for (nb::handle attr : extras)
318 attributes.push_back(pyTryCast<PyAttribute>(attr));
319 MlirAttribute arrayAttr = mlirArrayAttrGet(
320 arr.getContext()->get(), attributes.size(), attributes.data());
321 return PyArrayAttribute(arr.getContext(), arrayAttr);
322 });
323}
325 c.def_static(
326 "get",
327 [](PyType &type, double value, DefaultingPyLocation loc) {
328 PyMlirContext::ErrorCapture errors(loc->getContext());
329 MlirAttribute attr = mlirFloatAttrDoubleGetChecked(loc, type, value);
330 if (mlirAttributeIsNull(attr))
331 throw MLIRError("Invalid attribute", errors.take());
332 return PyFloatAttribute(type.getContext(), attr);
333 },
334 nb::arg("type"), nb::arg("value"), nb::arg("loc") = nb::none(),
335 "Gets an uniqued float point attribute associated to a type");
336 c.def_static(
337 "get_unchecked",
338 [](PyType &type, double value, DefaultingPyMlirContext context) {
339 PyMlirContext::ErrorCapture errors(context->getRef());
340 MlirAttribute attr =
341 mlirFloatAttrDoubleGet(context.get()->get(), type, value);
342 if (mlirAttributeIsNull(attr))
343 throw MLIRError("Invalid attribute", errors.take());
344 return PyFloatAttribute(type.getContext(), attr);
345 },
346 nb::arg("type"), nb::arg("value"), nb::arg("context") = nb::none(),
347 "Gets an uniqued float point attribute associated to a type");
348 c.def_static(
349 "get_f32",
350 [](double value, DefaultingPyMlirContext context) {
351 MlirAttribute attr = mlirFloatAttrDoubleGet(
352 context->get(), mlirF32TypeGet(context->get()), value);
353 return PyFloatAttribute(context->getRef(), attr);
354 },
355 nb::arg("value"), nb::arg("context") = nb::none(),
356 "Gets an uniqued float point attribute associated to a f32 type");
357 c.def_static(
358 "get_f64",
359 [](double value, DefaultingPyMlirContext context) {
360 MlirAttribute attr = mlirFloatAttrDoubleGet(
361 context->get(), mlirF64TypeGet(context->get()), value);
362 return PyFloatAttribute(context->getRef(), attr);
363 },
364 nb::arg("value"), nb::arg("context") = nb::none(),
365 "Gets an uniqued float point attribute associated to a f64 type");
366 c.def_prop_ro("value", mlirFloatAttrGetValueDouble,
367 "Returns the value of the float attribute");
368 c.def("__float__", mlirFloatAttrGetValueDouble,
369 "Converts the value of the float attribute to a Python float");
370}
371
373 c.def_static(
374 "get",
375 [](PyType &type, nb::object value) {
376 // Handle IndexType - it doesn't have a bit width or signedness.
377 if (mlirTypeIsAIndex(type)) {
378 int64_t intValue = nb::cast<int64_t>(value);
379 MlirAttribute attr = mlirIntegerAttrGet(type, intValue);
380 return PyIntegerAttribute(type.getContext(), attr);
381 }
382
383 // Get the bit width of the integer type.
384 unsigned bitWidth = mlirIntegerTypeGetWidth(type);
385
386 // Try to use the fast path for small integers.
387 if (bitWidth <= 64) {
388 int64_t intValue = nb::cast<int64_t>(value);
389 MlirAttribute attr = mlirIntegerAttrGet(type, intValue);
390 return PyIntegerAttribute(type.getContext(), attr);
391 }
392
393 // For larger integers, convert Python int to array of 64-bit words.
394 unsigned numWords = std::ceil(static_cast<double>(bitWidth) / 64);
395 std::vector<uint64_t> words(numWords, 0);
396
397 // Extract words from Python integer (little-endian order).
398 nb::object mask = nb::int_(0xFFFFFFFFFFFFFFFFULL);
399 nb::object shift = nb::int_(64);
400 nb::object current = value;
401
402 // Handle negative numbers for signed types by converting to two's
403 // complement representation.
404 if (mlirIntegerTypeIsSigned(type)) {
405 nb::object zero = nb::int_(0);
406 if (nb::cast<bool>(current < zero)) {
407 nb::object twoToTheBitWidth = nb::int_(1) << nb::int_(bitWidth);
408 current = current + twoToTheBitWidth;
409 }
410 }
411
412 for (unsigned i = 0; i < numWords; ++i) {
413 words[i] = nb::cast<uint64_t>(current & mask);
414 current = current >> shift;
415 }
416
417 MlirAttribute attr =
418 mlirIntegerAttrGetFromWords(type, numWords, words.data());
419 return PyIntegerAttribute(type.getContext(), attr);
420 },
421 nb::arg("type"), nb::arg("value"),
422 "Gets an uniqued integer attribute associated to a type");
423 c.def_prop_ro("value", toPyInt, "Returns the value of the integer attribute");
424 c.def("__int__", toPyInt,
425 "Converts the value of the integer attribute to a Python int");
426 c.def_prop_ro_static("static_typeid", [](nb::object & /*class*/) {
428 });
429}
430
431nb::int_ PyIntegerAttribute::toPyInt(PyIntegerAttribute &self) {
432 MlirType type = mlirAttributeGetType(self);
433 unsigned bitWidth = mlirIntegerAttrGetValueBitWidth(self);
434
435 // For integers that fit in 64 bits, use the fast path.
436 if (bitWidth <= 64) {
438 return nb::int_(mlirIntegerAttrGetValueInt(self));
439 if (mlirIntegerTypeIsSigned(type))
440 return nb::int_(mlirIntegerAttrGetValueSInt(self));
441 return nb::int_(mlirIntegerAttrGetValueUInt(self));
442 }
443
444 // For larger integers, reconstruct the value from raw words.
445 unsigned numWords = mlirIntegerAttrGetValueNumWords(self);
446 std::vector<uint64_t> words(numWords);
447 mlirIntegerAttrGetValueWords(self, words.data());
448
449 // Build the Python integer by shifting and ORing the words together.
450 // Words are in little-endian order (least significant first).
451 nb::object result = nb::int_(0);
452 nb::object shift = nb::int_(64);
453 for (unsigned i = numWords; i > 0; --i) {
454 result = result << shift;
455 result = result | nb::int_(words[i - 1]);
456 }
457
458 // Handle signed integers: if the sign bit is set, subtract 2^bitWidth.
459 if (mlirIntegerTypeIsSigned(type)) {
460 // Check if sign bit is set (most significant bit of the value).
461 bool signBitSet = (words[numWords - 1] >> ((bitWidth - 1) % 64)) & 1;
462 if (signBitSet) {
463 nb::object twoToTheBitWidth = nb::int_(1) << nb::int_(bitWidth);
464 result = result - twoToTheBitWidth;
465 }
466 }
467
468 return nb::cast<nb::int_>(result);
469}
470
472 c.def_static(
473 "get",
474 [](bool value, DefaultingPyMlirContext context) {
475 MlirAttribute attr = mlirBoolAttrGet(context->get(), value);
476 return PyBoolAttribute(context->getRef(), attr);
477 },
478 nb::arg("value"), nb::arg("context") = nb::none(),
479 "Gets an uniqued bool attribute");
480 c.def_prop_ro("value", mlirBoolAttrGetValue,
481 "Returns the value of the bool attribute");
482 c.def("__bool__", mlirBoolAttrGetValue,
483 "Converts the value of the bool attribute to a Python bool");
484}
485
487PySymbolRefAttribute::fromList(const std::vector<std::string> &symbols,
488 PyMlirContext &context) {
489 if (symbols.empty())
490 throw std::runtime_error("SymbolRefAttr must be composed of at least "
491 "one symbol.");
492 MlirStringRef rootSymbol = toMlirStringRef(symbols[0]);
493 std::vector<MlirAttribute> referenceAttrs;
494 for (size_t i = 1; i < symbols.size(); ++i) {
495 referenceAttrs.push_back(
496 mlirFlatSymbolRefAttrGet(context.get(), toMlirStringRef(symbols[i])));
497 }
498 return PySymbolRefAttribute(context.getRef(),
499 mlirSymbolRefAttrGet(context.get(), rootSymbol,
500 referenceAttrs.size(),
501 referenceAttrs.data()));
502}
503
505 c.def_static(
506 "get",
507 [](const std::vector<std::string> &symbols,
508 DefaultingPyMlirContext context) {
509 return PySymbolRefAttribute::fromList(symbols, context.resolve());
510 },
511 nb::arg("symbols"), nb::arg("context") = nb::none(),
512 "Gets a uniqued SymbolRef attribute from a list of symbol names");
513 c.def_prop_ro(
514 "value",
515 [](PySymbolRefAttribute &self) {
517 std::vector<MlirStringRef> symbols;
518 symbols.reserve(numNested + 1);
519 symbols.push_back(mlirSymbolRefAttrGetRootReference(self));
520 for (intptr_t i = 0; i < numNested; ++i) {
521 symbols.push_back(mlirSymbolRefAttrGetRootReference(
523 }
524 return symbols;
525 },
526 "Returns the value of the SymbolRef attribute as a list[str]");
527}
528
530 c.def_static(
531 "get",
532 [](const std::string &value, DefaultingPyMlirContext context) {
533 MlirAttribute attr =
534 mlirFlatSymbolRefAttrGet(context->get(), toMlirStringRef(value));
535 return PyFlatSymbolRefAttribute(context->getRef(), attr);
536 },
537 nb::arg("value"), nb::arg("context") = nb::none(),
538 "Gets a uniqued FlatSymbolRef attribute");
539 c.def_prop_ro(
540 "value",
541 [](PyFlatSymbolRefAttribute &self) {
543 return nb::str(stringRef.data, stringRef.length);
544 },
545 "Returns the value of the FlatSymbolRef attribute as a string");
546}
547
549 c.def_static(
550 "get",
551 [](const std::string &dialectNamespace, const nb_buffer &buffer,
552 PyType &type, DefaultingPyMlirContext context) {
553 const nb_buffer_info bufferInfo = buffer.request();
554 intptr_t bufferSize = bufferInfo.size;
555 MlirAttribute attr = mlirOpaqueAttrGet(
556 context->get(), toMlirStringRef(dialectNamespace), bufferSize,
557 static_cast<char *>(bufferInfo.ptr), type);
558 return PyOpaqueAttribute(context->getRef(), attr);
559 },
560 nb::arg("dialect_namespace"), nb::arg("buffer"), nb::arg("type"),
561 nb::arg("context") = nb::none(),
562 // clang-format off
563 nb::sig("def get(dialect_namespace: str, buffer: typing_extensions.Buffer, type: Type, context: Context | None = None) -> OpaqueAttr"),
564 // clang-format on
565 "Gets an Opaque attribute.");
566 c.def_prop_ro(
567 "dialect_namespace",
568 [](PyOpaqueAttribute &self) {
570 return nb::str(stringRef.data, stringRef.length);
571 },
572 "Returns the dialect namespace for the Opaque attribute as a string");
573 c.def_prop_ro(
574 "data",
575 [](PyOpaqueAttribute &self) {
576 MlirStringRef stringRef = mlirOpaqueAttrGetData(self);
577 return nb::bytes(stringRef.data, stringRef.length);
578 },
579 "Returns the data for the Opaqued attributes as `bytes`");
580}
581
583 const nb::typed<nb::sequence, PyAttribute> &attributes,
584 std::optional<PyType> explicitType,
585 DefaultingPyMlirContext contextWrapper) {
586 const size_t numAttributes = nb::len(attributes);
587 if (numAttributes == 0)
588 throw nb::value_error("Attributes list must be non-empty.");
589
590 MlirType shapedType;
591 if (explicitType) {
592 if ((!mlirTypeIsAShaped(*explicitType) ||
593 !mlirShapedTypeHasStaticShape(*explicitType))) {
594
595 std::string message = nanobind::detail::join(
596 "Expected a static ShapedType for the shaped_type parameter: ",
597 nb::cast<std::string>(nb::repr(nb::cast(*explicitType))));
598 throw nb::value_error(message.c_str());
599 }
600 shapedType = *explicitType;
601 } else {
602 std::vector<int64_t> shape = {static_cast<int64_t>(numAttributes)};
603 shapedType = mlirRankedTensorTypeGet(
604 shape.size(), shape.data(),
607 }
608
609 std::vector<MlirAttribute> mlirAttributes;
610 mlirAttributes.reserve(numAttributes);
611 for (const nb::handle &attribute : attributes) {
612 MlirAttribute mlirAttribute = pyTryCast<PyAttribute>(attribute);
613 MlirType attrType = mlirAttributeGetType(mlirAttribute);
614 mlirAttributes.push_back(mlirAttribute);
615
616 if (!mlirTypeEqual(mlirShapedTypeGetElementType(shapedType), attrType)) {
617 std::string message = nanobind::detail::join(
618 "All attributes must be of the same type and match the type "
619 "parameter: expected=",
620 nb::cast<std::string>(nb::repr(nb::cast(shapedType))),
621 ", but got=", nb::cast<std::string>(nb::repr(nb::cast(attrType))));
622 throw nb::value_error(message.c_str());
623 }
624 }
625
626 MlirAttribute elements = mlirDenseElementsAttrGet(
627 shapedType, mlirAttributes.size(), mlirAttributes.data());
628
629 return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
630}
631
633 const nb_buffer &array, bool signless,
634 const std::optional<PyType> &explicitType,
635 std::optional<std::vector<int64_t>> explicitShape,
636 DefaultingPyMlirContext contextWrapper) {
637 // Request a contiguous view. In exotic cases, this will cause a copy.
638 int flags = PyBUF_ND;
639 if (!explicitType) {
640 flags |= PyBUF_FORMAT;
641 }
642 Py_buffer view;
643 if (PyObject_GetBuffer(array.ptr(), &view, flags) != 0) {
644 throw nb::python_error();
645 }
646 scope_exit freeBuffer([&]() { PyBuffer_Release(&view); });
647
648 MlirContext context = contextWrapper->get();
649 MlirAttribute attr = getAttributeFromBuffer(
650 view, signless, explicitType, std::move(explicitShape), context);
651 if (mlirAttributeIsNull(attr)) {
652 throw std::invalid_argument(
653 "DenseElementsAttr could not be constructed from the given buffer. "
654 "This may mean that the Python buffer layout does not match that "
655 "MLIR expected layout and is a bug.");
656 }
657 return PyDenseElementsAttribute(contextWrapper->getRef(), attr);
658}
659
662 PyAttribute &elementAttr) {
663 auto contextWrapper =
665 if (!mlirAttributeIsAInteger(elementAttr) &&
666 !mlirAttributeIsAFloat(elementAttr)) {
667 std::string message = "Illegal element type for DenseElementsAttr: ";
668 message.append(nb::cast<std::string>(nb::repr(nb::cast(elementAttr))));
669 throw nb::value_error(message.c_str());
670 }
671 if (!mlirTypeIsAShaped(shapedType) ||
672 !mlirShapedTypeHasStaticShape(shapedType)) {
673 std::string message =
674 "Expected a static ShapedType for the shaped_type parameter: ";
675 message.append(nb::cast<std::string>(nb::repr(nb::cast(shapedType))));
676 throw nb::value_error(message.c_str());
677 }
678 MlirType shapedElementType = mlirShapedTypeGetElementType(shapedType);
679 MlirType attrType = mlirAttributeGetType(elementAttr);
680 if (!mlirTypeEqual(shapedElementType, attrType)) {
681 std::string message =
682 "Shaped element type and attribute type must be equal: shaped=";
683 message.append(nb::cast<std::string>(nb::repr(nb::cast(shapedType))));
684 message.append(", element=");
685 message.append(nb::cast<std::string>(nb::repr(nb::cast(elementAttr))));
686 throw nb::value_error(message.c_str());
687 }
688
689 MlirAttribute elements =
690 mlirDenseElementsAttrSplatGet(shapedType, elementAttr);
691 return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
692}
693
697
698std::unique_ptr<nb_buffer_info> PyDenseElementsAttribute::accessBuffer() {
699 MlirType shapedType = mlirAttributeGetType(*this);
700 MlirType elementType = mlirShapedTypeGetElementType(shapedType);
701 std::string format;
702
703 if (mlirTypeIsAF32(elementType)) {
704 // f32
705 return bufferInfo<float>(shapedType);
706 }
707 if (mlirTypeIsAF64(elementType)) {
708 // f64
709 return bufferInfo<double>(shapedType);
710 }
711 if (mlirTypeIsAF16(elementType)) {
712 // f16
713 return bufferInfo<uint16_t>(shapedType, "e");
714 }
715 if (mlirTypeIsAIndex(elementType)) {
716 // Same as IndexType::kInternalStorageBitWidth
717 return bufferInfo<int64_t>(shapedType);
718 }
719 if (mlirTypeIsAInteger(elementType) &&
720 mlirIntegerTypeGetWidth(elementType) == 32) {
721 if (mlirIntegerTypeIsSignless(elementType) ||
722 mlirIntegerTypeIsSigned(elementType)) {
723 // i32
724 return bufferInfo<int32_t>(shapedType);
725 }
726 if (mlirIntegerTypeIsUnsigned(elementType)) {
727 // unsigned i32
728 return bufferInfo<uint32_t>(shapedType);
729 }
730 } else if (mlirTypeIsAInteger(elementType) &&
731 mlirIntegerTypeGetWidth(elementType) == 64) {
732 if (mlirIntegerTypeIsSignless(elementType) ||
733 mlirIntegerTypeIsSigned(elementType)) {
734 // i64
735 return bufferInfo<int64_t>(shapedType);
736 }
737 if (mlirIntegerTypeIsUnsigned(elementType)) {
738 // unsigned i64
739 return bufferInfo<uint64_t>(shapedType);
740 }
741 } else if (mlirTypeIsAInteger(elementType) &&
742 mlirIntegerTypeGetWidth(elementType) == 8) {
743 if (mlirIntegerTypeIsSignless(elementType) ||
744 mlirIntegerTypeIsSigned(elementType)) {
745 // i8
746 return bufferInfo<int8_t>(shapedType);
747 }
748 if (mlirIntegerTypeIsUnsigned(elementType)) {
749 // unsigned i8
750 return bufferInfo<uint8_t>(shapedType);
751 }
752 } else if (mlirTypeIsAInteger(elementType) &&
753 mlirIntegerTypeGetWidth(elementType) == 16) {
754 if (mlirIntegerTypeIsSignless(elementType) ||
755 mlirIntegerTypeIsSigned(elementType)) {
756 // i16
757 return bufferInfo<int16_t>(shapedType);
758 }
759 if (mlirIntegerTypeIsUnsigned(elementType)) {
760 // unsigned i16
761 return bufferInfo<uint16_t>(shapedType);
762 }
763 } else if (mlirTypeIsAInteger(elementType) &&
764 mlirIntegerTypeGetWidth(elementType) == 1) {
765 // i1 / bool
766 return bufferInfo<bool>(shapedType);
767 }
768
769 // TODO: Currently crashes the program.
770 // Reported as https://github.com/pybind/pybind11/issues/3336
771 throw std::invalid_argument(
772 "unsupported data type for conversion to Python buffer");
773}
774
775template <typename ClassT>
777 const char *pyClassName) {
778 std::string getSig1 =
779 // clang-format off
780 "def get(array: typing_extensions.Buffer, signless: bool = True, type: Type | None = None, shape: Sequence[int] | None = None, context: Context | None = None) -> " +
781 // clang-format on
782 std::string(pyClassName);
783 std::string getSig2 =
784 // clang-format off
785 "def get(attrs: Sequence[Attribute], type: Type | None = None, context: Context | None = None) -> " +
786 // clang-format on
787 std::string(pyClassName);
788 std::string getSplatSig =
789 // clang-format off
790 "def get_splat(shaped_type: Type, element_attr: Attribute) -> " +
791 // clang-format on
792 std::string(pyClassName);
793
794 c.def_static("get", PyDenseElementsAttribute::getFromBuffer, nb::arg("array"),
795 nb::arg("signless") = true, nb::arg("type") = nb::none(),
796 nb::arg("shape") = nb::none(), nb::arg("context") = nb::none(),
797 nb::sig(getSig1.c_str()), kDenseElementsAttrGetDocstring)
798 .def_static("get", PyDenseElementsAttribute::getFromList,
799 nb::arg("attrs"), nb::arg("type") = nb::none(),
800 nb::arg("context") = nb::none(), nb::sig(getSig2.c_str()),
802 .def_static("get_splat", PyDenseElementsAttribute::getSplat,
803 nb::arg("shaped_type"), nb::arg("element_attr"),
804 nb::sig(getSplatSig.c_str()),
805 ("Gets a " + std::string(pyClassName) +
806 " where all values are the same")
807 .c_str());
808}
809
811 c.def("__len__", &PyDenseElementsAttribute::dunderLen);
813 c.def_prop_ro("is_splat",
814 [](PyDenseElementsAttribute &self) -> bool {
815 return mlirDenseElementsAttrIsSplat(self);
816 })
817 .def("get_splat_value",
819 -> nb::typed<nb::object, PyAttribute> {
821 throw nb::value_error(
822 "get_splat_value called on a non-splat attribute");
823 return PyAttribute(self.getContext(),
825 .maybeDownCast();
826 });
827}
828
829bool PyDenseElementsAttribute::isUnsignedIntegerFormat(
830 std::string_view format) {
831 if (format.empty())
832 return false;
833 char code = format[0];
834 return code == 'I' || code == 'B' || code == 'H' || code == 'L' ||
835 code == 'Q';
836}
837
838bool PyDenseElementsAttribute::isSignedIntegerFormat(std::string_view format) {
839 if (format.empty())
840 return false;
841 char code = format[0];
842 return code == 'i' || code == 'b' || code == 'h' || code == 'l' ||
843 code == 'q';
844}
845
846MlirType PyDenseElementsAttribute::getShapedType(
847 std::optional<MlirType> bulkLoadElementType,
848 std::optional<std::vector<int64_t>> explicitShape, Py_buffer &view) {
849 std::vector<int64_t> shape;
850 if (explicitShape) {
851 shape.insert(shape.end(), explicitShape->begin(), explicitShape->end());
852 } else {
853 shape.insert(shape.end(), view.shape, view.shape + view.ndim);
854 }
855
856 if (mlirTypeIsAShaped(*bulkLoadElementType)) {
857 if (explicitShape) {
858 throw std::invalid_argument("Shape can only be specified explicitly "
859 "when the type is not a shaped type.");
860 }
861 return *bulkLoadElementType;
862 }
863 MlirAttribute encodingAttr = mlirAttributeGetNull();
864 return mlirRankedTensorTypeGet(shape.size(), shape.data(),
865 *bulkLoadElementType, encodingAttr);
866}
867
868MlirAttribute PyDenseElementsAttribute::getAttributeFromBuffer(
869 Py_buffer &view, bool signless, std::optional<PyType> explicitType,
870 const std::optional<std::vector<int64_t>> &explicitShape,
871 MlirContext &context) {
872 // Detect format codes that are suitable for bulk loading. This includes
873 // all byte aligned integer and floating point types up to 8 bytes.
874 // Notably, this excludes exotics types which do not have a direct
875 // representation in the buffer protocol (i.e. complex, etc).
876 std::optional<MlirType> bulkLoadElementType;
877 if (explicitType) {
878 bulkLoadElementType = *explicitType;
879 } else {
880 std::string_view format(view.format);
881 if (format == "f") {
882 // f32
883 assert(view.itemsize == 4 && "mismatched array itemsize");
884 bulkLoadElementType = mlirF32TypeGet(context);
885 } else if (format == "d") {
886 // f64
887 assert(view.itemsize == 8 && "mismatched array itemsize");
888 bulkLoadElementType = mlirF64TypeGet(context);
889 } else if (format == "e") {
890 // f16
891 assert(view.itemsize == 2 && "mismatched array itemsize");
892 bulkLoadElementType = mlirF16TypeGet(context);
893 } else if (format == "?") {
894 // i1
895 bulkLoadElementType = mlirIntegerTypeGet(context, 1);
896 } else if (isSignedIntegerFormat(format)) {
897 if (view.itemsize == 4) {
898 // i32
899 bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 32)
900 : mlirIntegerTypeSignedGet(context, 32);
901 } else if (view.itemsize == 8) {
902 // i64
903 bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 64)
904 : mlirIntegerTypeSignedGet(context, 64);
905 } else if (view.itemsize == 1) {
906 // i8
907 bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 8)
908 : mlirIntegerTypeSignedGet(context, 8);
909 } else if (view.itemsize == 2) {
910 // i16
911 bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 16)
912 : mlirIntegerTypeSignedGet(context, 16);
913 }
914 } else if (isUnsignedIntegerFormat(format)) {
915 if (view.itemsize == 4) {
916 // unsigned i32
917 bulkLoadElementType = signless
918 ? mlirIntegerTypeGet(context, 32)
919 : mlirIntegerTypeUnsignedGet(context, 32);
920 } else if (view.itemsize == 8) {
921 // unsigned i64
922 bulkLoadElementType = signless
923 ? mlirIntegerTypeGet(context, 64)
924 : mlirIntegerTypeUnsignedGet(context, 64);
925 } else if (view.itemsize == 1) {
926 // i8
927 bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 8)
928 : mlirIntegerTypeUnsignedGet(context, 8);
929 } else if (view.itemsize == 2) {
930 // i16
931 bulkLoadElementType = signless
932 ? mlirIntegerTypeGet(context, 16)
933 : mlirIntegerTypeUnsignedGet(context, 16);
934 }
935 }
936 if (!bulkLoadElementType) {
937 throw std::invalid_argument(
938 std::string("unimplemented array format conversion from format: ") +
939 std::string(format));
940 }
941 }
942
943 MlirType type = getShapedType(bulkLoadElementType, explicitShape, view);
944 return mlirDenseElementsAttrRawBufferGet(type, view.len, view.buf);
945}
946
947PyType_Slot PyDenseElementsAttribute::slots[] = {
948 {Py_bf_getbuffer,
949 reinterpret_cast<void *>(PyDenseElementsAttribute::bf_getbuffer)},
950 {Py_bf_releasebuffer,
951 reinterpret_cast<void *>(PyDenseElementsAttribute::bf_releasebuffer)},
952 {0, nullptr},
953};
954
955/*static*/ int PyDenseElementsAttribute::bf_getbuffer(PyObject *obj,
956 Py_buffer *view,
957 int flags) {
958 view->obj = nullptr;
959 std::unique_ptr<nb_buffer_info> info;
960 try {
961 auto *attr = nb::cast<PyDenseElementsAttribute *>(nb::handle(obj));
962 info = attr->accessBuffer();
963 } catch (nb::python_error &e) {
964 e.restore();
965 nb::chain_error(PyExc_BufferError, "Error converting attribute to buffer");
966 return -1;
967 } catch (std::exception &e) {
968 nb::chain_error(PyExc_BufferError,
969 "Error converting attribute to buffer: %s", e.what());
970 return -1;
971 }
972 view->obj = obj;
973 view->ndim = 1;
974 view->buf = info->ptr;
975 view->itemsize = info->itemsize;
976 view->len = info->itemsize;
977 for (auto s : info->shape) {
978 view->len *= s;
979 }
980 view->readonly = info->readonly;
981 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
982 view->format = const_cast<char *>(info->format);
983 }
984 if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) {
985 view->ndim = static_cast<int>(info->ndim);
986 view->strides = info->strides.data();
987 view->shape = info->shape.data();
988 }
989 view->suboffsets = nullptr;
990 view->internal = info.release();
991 Py_INCREF(obj);
992 return 0;
993}
994
995/*static*/ void PyDenseElementsAttribute::bf_releasebuffer(PyObject *,
996 Py_buffer *view) {
997 delete reinterpret_cast<nb_buffer_info *>(view->internal);
998}
999
1001 if (pos < 0 || pos >= dunderLen()) {
1002 throw nb::index_error("attempt to access out of bounds element");
1003 }
1004
1005 MlirType type = mlirAttributeGetType(*this);
1006 type = mlirShapedTypeGetElementType(type);
1007 // Index type can also appear as a DenseIntElementsAttr and therefore can be
1008 // casted to integer.
1009 assert(mlirTypeIsAInteger(type) ||
1010 mlirTypeIsAIndex(type) && "expected integer/index element type in "
1011 "dense int elements attribute");
1012 // Dispatch element extraction to an appropriate C function based on the
1013 // elemental type of the attribute. nb::int_ is implicitly
1014 // constructible from any C++ integral type and handles bitwidth correctly.
1015 // TODO: consider caching the type properties in the constructor to avoid
1016 // querying them on each element access.
1017 if (mlirTypeIsAIndex(type)) {
1018 return nb::int_(mlirDenseElementsAttrGetIndexValue(*this, pos));
1019 }
1020 unsigned width = mlirIntegerTypeGetWidth(type);
1021 bool isUnsigned = mlirIntegerTypeIsUnsigned(type);
1022 if (isUnsigned) {
1023 if (width == 1) {
1024 return nb::int_(int(mlirDenseElementsAttrGetBoolValue(*this, pos)));
1025 }
1026 if (width == 8) {
1027 return nb::int_(mlirDenseElementsAttrGetUInt8Value(*this, pos));
1028 }
1029 if (width == 16) {
1030 return nb::int_(mlirDenseElementsAttrGetUInt16Value(*this, pos));
1031 }
1032 if (width == 32) {
1033 return nb::int_(mlirDenseElementsAttrGetUInt32Value(*this, pos));
1034 }
1035 if (width == 64) {
1036 return nb::int_(mlirDenseElementsAttrGetUInt64Value(*this, pos));
1037 }
1038 } else {
1039 if (width == 1) {
1040 return nb::int_(int(mlirDenseElementsAttrGetBoolValue(*this, pos)));
1041 }
1042 if (width == 8) {
1043 return nb::int_(mlirDenseElementsAttrGetInt8Value(*this, pos));
1044 }
1045 if (width == 16) {
1046 return nb::int_(mlirDenseElementsAttrGetInt16Value(*this, pos));
1047 }
1048 if (width == 32) {
1049 return nb::int_(mlirDenseElementsAttrGetInt32Value(*this, pos));
1050 }
1051 if (width == 64) {
1052 return nb::int_(mlirDenseElementsAttrGetInt64Value(*this, pos));
1053 }
1054 }
1055 throw nb::type_error("Unsupported integer type");
1056}
1057
1062
1063// Py_IsFinalizing is part of the stable ABI since 3.13. Before that, it was
1064// available as the private _Py_IsFinalizing, which is not part of the limited
1065// API.
1066#if defined(Py_LIMITED_API) && Py_LIMITED_API < 0x030d0000
1067// Under limited API targeting < 3.13, use sys.is_finalizing() via C API.
1068// PySys_GetObject avoids import machinery (safe during finalization).
1069static int Py_IsFinalizing(void) {
1070 // PySys_GetObject returns a borrowed reference; no Py_DECREF needed.
1071 PyObject *fn = PySys_GetObject("is_finalizing");
1072 if (!fn)
1073 return 0;
1074 PyObject *result = PyObject_CallNoArgs(fn);
1075 if (!result) {
1076 PyErr_Clear();
1077 return 0;
1078 }
1079 int val = PyObject_IsTrue(result);
1080 Py_DECREF(result);
1081 return val > 0 ? 1 : 0;
1082}
1083#elif PY_VERSION_HEX < 0x030d0000
1084#define Py_IsFinalizing _Py_IsFinalizing
1085#endif
1086
1089 const nb_buffer &buffer, const std::string &name, const PyType &type,
1090 std::optional<size_t> alignment, bool isMutable,
1091 DefaultingPyMlirContext contextWrapper) {
1092 if (!mlirTypeIsAShaped(type)) {
1093 throw std::invalid_argument(
1094 "Constructing a DenseResourceElementsAttr requires a ShapedType.");
1095 }
1096
1097 // Do not request any conversions as we must ensure to use caller
1098 // managed memory.
1099 int flags = PyBUF_STRIDES;
1100 std::unique_ptr<Py_buffer> view = std::make_unique<Py_buffer>();
1101 if (PyObject_GetBuffer(buffer.ptr(), view.get(), flags) != 0) {
1102 throw nb::python_error();
1103 }
1104
1105 // This scope releaser will only release if we haven't yet transferred
1106 // ownership.
1107 scope_exit freeBuffer([&]() {
1108 if (view)
1109 PyBuffer_Release(view.get());
1110 });
1111
1112 if (!PyBuffer_IsContiguous(view.get(), 'A')) {
1113 throw std::invalid_argument("Contiguous buffer is required.");
1114 }
1115
1116 // Infer alignment to be the stride of one element if not explicit.
1117 size_t inferredAlignment;
1118 if (alignment)
1119 inferredAlignment = *alignment;
1120 else if (view->ndim == 0)
1121 inferredAlignment = view->itemsize;
1122 else
1123 inferredAlignment = view->strides[view->ndim - 1];
1124
1125 // The userData is a Py_buffer* that the deleter owns.
1126 auto deleter = [](void *userData, const void *data, size_t size,
1127 size_t align) {
1128 if (Py_IsFinalizing())
1129 return;
1130 assert(Py_IsInitialized() && "expected interpreter to be initialized");
1131 Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
1132 nb::gil_scoped_acquire gil;
1133 PyBuffer_Release(ownedView);
1134 delete ownedView;
1135 };
1136
1137 size_t rawBufferSize = view->len;
1138 MlirAttribute attr = mlirUnmanagedDenseResourceElementsAttrGet(
1139 type, toMlirStringRef(name), view->buf, rawBufferSize, inferredAlignment,
1140 isMutable, deleter, static_cast<void *>(view.get()));
1141 if (mlirAttributeIsNull(attr)) {
1142 throw std::invalid_argument(
1143 "DenseResourceElementsAttr could not be constructed from the given "
1144 "buffer. "
1145 "This may mean that the Python buffer layout does not match that "
1146 "MLIR expected layout and is a bug.");
1147 }
1148 view.release();
1149 return PyDenseResourceElementsAttribute(contextWrapper->getRef(), attr);
1150}
1151
1153 c.def_static(
1155 nb::arg("array"), nb::arg("name"), nb::arg("type"),
1156 nb::arg("alignment") = nb::none(), nb::arg("is_mutable") = false,
1157 nb::arg("context") = nb::none(),
1158 // clang-format off
1159 nb::sig("def get_from_buffer(array: typing_extensions.Buffer, name: str, type: Type, alignment: int | None = None, is_mutable: bool = False, context: Context | None = None) -> DenseResourceElementsAttr"),
1160 // clang-format on
1162}
1163
1167
1168bool PyDictAttribute::dunderContains(const std::string &name) const {
1169 return !mlirAttributeIsNull(
1171}
1172
1174 c.def("__contains__", &PyDictAttribute::dunderContains);
1175 c.def("__len__", &PyDictAttribute::dunderLen);
1176 c.def_static(
1177 "get",
1178 [](const nb::typed<nb::dict, nb::str, PyAttribute> &attributes,
1179 DefaultingPyMlirContext context) {
1180 std::vector<MlirNamedAttribute> mlirNamedAttributes;
1181 mlirNamedAttributes.reserve(attributes.size());
1182 for (std::pair<nb::handle, nb::handle> it : attributes) {
1183 auto &mlirAttr = nb::cast<PyAttribute &>(it.second);
1184 auto name = nb::cast<std::string>(it.first);
1185 mlirNamedAttributes.push_back(mlirNamedAttributeGet(
1188 mlirAttr));
1189 }
1190 MlirAttribute attr =
1191 mlirDictionaryAttrGet(context->get(), mlirNamedAttributes.size(),
1192 mlirNamedAttributes.data());
1193 return PyDictAttribute(context->getRef(), attr);
1194 },
1195 nb::arg("value") = nb::dict(), nb::arg("context") = nb::none(),
1196 "Gets an uniqued dict attribute");
1197 c.def("__getitem__",
1198 [](PyDictAttribute &self,
1199 const std::string &name) -> nb::typed<nb::object, PyAttribute> {
1200 MlirAttribute attr =
1202 if (mlirAttributeIsNull(attr))
1203 throw nb::key_error("attempt to access a non-existent attribute");
1204 return PyAttribute(self.getContext(), attr).maybeDownCast();
1205 });
1206 c.def("__getitem__", [](PyDictAttribute &self, intptr_t index) {
1207 if (index < 0 || index >= self.dunderLen()) {
1208 throw nb::index_error("attempt to access out of bounds attribute");
1209 }
1211 return PyNamedAttribute(
1212 namedAttr.attribute,
1213 std::string(mlirIdentifierStr(namedAttr.name).data));
1214 });
1215}
1216
1218 if (pos < 0 || pos >= dunderLen()) {
1219 throw nb::index_error("attempt to access out of bounds element");
1220 }
1221
1222 MlirType type = mlirAttributeGetType(*this);
1223 type = mlirShapedTypeGetElementType(type);
1224 // Dispatch element extraction to an appropriate C function based on the
1225 // elemental type of the attribute. nb::float_ is implicitly
1226 // constructible from float and double.
1227 // TODO: consider caching the type properties in the constructor to avoid
1228 // querying them on each element access.
1229 if (mlirTypeIsAF32(type)) {
1230 return nb::float_(mlirDenseElementsAttrGetFloatValue(*this, pos));
1231 }
1232 if (mlirTypeIsAF64(type)) {
1233 return nb::float_(mlirDenseElementsAttrGetDoubleValue(*this, pos));
1234 }
1235 throw nb::type_error("Unsupported floating-point type");
1236}
1237
1242
1244 c.def_static(
1245 "get",
1246 [](const PyType &value, DefaultingPyMlirContext context) {
1247 MlirAttribute attr = mlirTypeAttrGet(value.get());
1248 return PyTypeAttribute(context->getRef(), attr);
1249 },
1250 nb::arg("value"), nb::arg("context") = nb::none(),
1251 "Gets a uniqued Type attribute");
1252 c.def_prop_ro(
1253 "value", [](PyTypeAttribute &self) -> nb::typed<nb::object, PyType> {
1254 return PyType(self.getContext(), mlirTypeAttrGetValue(self.get()))
1255 .maybeDownCast();
1256 });
1257}
1258
1260 c.def_static(
1261 "get",
1262 [](DefaultingPyMlirContext context) {
1263 return PyUnitAttribute(context->getRef(),
1264 mlirUnitAttrGet(context->get()));
1265 },
1266 nb::arg("context") = nb::none(), "Create a Unit attribute.");
1267}
1268
1270 c.def_static(
1271 "get",
1272 [](int64_t offset, const std::vector<int64_t> &strides,
1274 MlirAttribute attr = mlirStridedLayoutAttrGet(
1275 ctx->get(), offset, strides.size(), strides.data());
1276 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1277 },
1278 nb::arg("offset"), nb::arg("strides"), nb::arg("context") = nb::none(),
1279 "Gets a strided layout attribute.");
1280 c.def_static(
1281 "get_fully_dynamic",
1282 [](int64_t rank, DefaultingPyMlirContext ctx) {
1284 std::vector<int64_t> strides(rank);
1285 std::fill(strides.begin(), strides.end(), dynamic);
1286 MlirAttribute attr = mlirStridedLayoutAttrGet(
1287 ctx->get(), dynamic, strides.size(), strides.data());
1288 return PyStridedLayoutAttribute(ctx->getRef(), attr);
1289 },
1290 nb::arg("rank"), nb::arg("context") = nb::none(),
1291 "Gets a strided layout attribute with dynamic offset and strides of "
1292 "a "
1293 "given rank.");
1294 c.def_prop_ro(
1295 "offset",
1296 [](PyStridedLayoutAttribute &self) {
1297 return mlirStridedLayoutAttrGetOffset(self);
1298 },
1299 "Returns the value of the float point attribute");
1300 c.def_prop_ro(
1301 "strides",
1302 [](PyStridedLayoutAttribute &self) {
1304 std::vector<int64_t> strides(size);
1305 for (intptr_t i = 0; i < size; i++) {
1306 strides[i] = mlirStridedLayoutAttrGetStride(self, i);
1307 }
1308 return strides;
1309 },
1310 "Returns the value of the float point attribute");
1311}
1312
1313nb::object denseArrayAttributeCaster(PyAttribute &pyAttribute) {
1315 return nb::cast(PyDenseBoolArrayAttribute(pyAttribute));
1316 if (PyDenseI8ArrayAttribute::isaFunction(pyAttribute))
1317 return nb::cast(PyDenseI8ArrayAttribute(pyAttribute));
1319 return nb::cast(PyDenseI16ArrayAttribute(pyAttribute));
1321 return nb::cast(PyDenseI32ArrayAttribute(pyAttribute));
1323 return nb::cast(PyDenseI64ArrayAttribute(pyAttribute));
1325 return nb::cast(PyDenseF32ArrayAttribute(pyAttribute));
1327 return nb::cast(PyDenseF64ArrayAttribute(pyAttribute));
1328 std::string msg =
1329 std::string("Can't cast unknown element type DenseArrayAttr (") +
1330 nb::cast<std::string>(nb::repr(nb::cast(pyAttribute))) + ")";
1331 throw nb::type_error(msg.c_str());
1332}
1333
1336 return nb::cast(PyDenseFPElementsAttribute(pyAttribute));
1338 return nb::cast(PyDenseIntElementsAttribute(pyAttribute));
1339 std::string msg =
1340 std::string("Can't cast unknown element type DenseTypedElementsAttr (") +
1341 nb::cast<std::string>(nb::repr(nb::cast(pyAttribute))) + ")";
1342 throw nb::type_error(msg.c_str());
1343}
1344
1346 if (PyBoolAttribute::isaFunction(pyAttribute))
1347 return nb::cast(PyBoolAttribute(pyAttribute));
1348 if (PyIntegerAttribute::isaFunction(pyAttribute))
1349 return nb::cast(PyIntegerAttribute(pyAttribute));
1350 std::string msg = std::string("Can't cast unknown attribute type Attr (") +
1351 nb::cast<std::string>(nb::repr(nb::cast(pyAttribute))) +
1352 ")";
1353 throw nb::type_error(msg.c_str());
1354}
1355
1358 return nb::cast(PyFlatSymbolRefAttribute(pyAttribute));
1359 if (PySymbolRefAttribute::isaFunction(pyAttribute))
1360 return nb::cast(PySymbolRefAttribute(pyAttribute));
1361 std::string msg = std::string("Can't cast unknown SymbolRef attribute (") +
1362 nb::cast<std::string>(nb::repr(nb::cast(pyAttribute))) +
1363 ")";
1364 throw nb::type_error(msg.c_str());
1365}
1366
1368 c.def_static(
1369 "get",
1370 [](const std::string &value, DefaultingPyMlirContext context) {
1371 MlirAttribute attr =
1372 mlirStringAttrGet(context->get(), toMlirStringRef(value));
1373 return PyStringAttribute(context->getRef(), attr);
1374 },
1375 nb::arg("value"), nb::arg("context") = nb::none(),
1376 "Gets a uniqued string attribute");
1377 c.def_static(
1378 "get",
1379 [](const nb::bytes &value, DefaultingPyMlirContext context) {
1380 MlirAttribute attr =
1381 mlirStringAttrGet(context->get(), toMlirStringRef(value));
1382 return PyStringAttribute(context->getRef(), attr);
1383 },
1384 nb::arg("value"), nb::arg("context") = nb::none(),
1385 "Gets a uniqued string attribute");
1386 c.def_static(
1387 "get_typed",
1388 [](PyType &type, const std::string &value) {
1389 MlirAttribute attr =
1391 return PyStringAttribute(type.getContext(), attr);
1392 },
1393 nb::arg("type"), nb::arg("value"),
1394 "Gets a uniqued string attribute associated to a type");
1395 c.def_prop_ro(
1396 "value",
1397 [](PyStringAttribute &self) {
1398 MlirStringRef stringRef = mlirStringAttrGetValue(self);
1399 return nb::str(stringRef.data, stringRef.length);
1400 },
1401 "Returns the value of the string attribute");
1402 c.def_prop_ro(
1403 "value_bytes",
1404 [](PyStringAttribute &self) {
1405 MlirStringRef stringRef = mlirStringAttrGetValue(self);
1406 return nb::bytes(stringRef.data, stringRef.length);
1407 },
1408 "Returns the value of the string attribute as `bytes`");
1409}
1410
1411static MlirDynamicAttrDefinition
1412getDynamicAttrDef(const std::string &fullAttrName,
1413 DefaultingPyMlirContext context) {
1414 size_t dotPos = fullAttrName.find('.');
1415 if (dotPos == std::string::npos) {
1416 throw nb::value_error("Expected full attribute name to be in the format "
1417 "'<dialectName>.<attributeName>'.");
1418 }
1419
1420 std::string dialectName = fullAttrName.substr(0, dotPos);
1421 std::string attrName = fullAttrName.substr(dotPos + 1);
1422 PyDialects dialects(context->getRef());
1423 MlirDialect dialect = dialects.getDialectForKey(dialectName, false);
1424 if (!mlirDialectIsAExtensibleDialect(dialect))
1425 throw nb::value_error(
1426 ("Dialect '" + dialectName + "' is not an extensible dialect.")
1427 .c_str());
1428
1429 MlirDynamicAttrDefinition attrDef = mlirExtensibleDialectLookupAttrDefinition(
1430 dialect, toMlirStringRef(attrName));
1431 if (attrDef.ptr == nullptr) {
1432 throw nb::value_error(("Dialect '" + dialectName +
1433 "' does not contain an attribute named '" +
1434 attrName + "'.")
1435 .c_str());
1436 }
1437 return attrDef;
1438}
1439
1441 c.def_static(
1442 "get",
1443 [](const std::string &fullAttrName, const std::vector<PyAttribute> &attrs,
1444 DefaultingPyMlirContext context) {
1445 std::vector<MlirAttribute> mlirAttrs;
1446 mlirAttrs.reserve(attrs.size());
1447 for (const auto &attr : attrs)
1448 mlirAttrs.push_back(attr.get());
1449
1450 MlirDynamicAttrDefinition attrDef =
1451 getDynamicAttrDef(fullAttrName, context);
1452 MlirAttribute attr =
1453 mlirDynamicAttrGet(attrDef, mlirAttrs.data(), mlirAttrs.size());
1454 return PyDynamicAttribute(context->getRef(), attr);
1455 },
1456 nb::arg("full_attr_name"), nb::arg("attributes"),
1457 nb::arg("context") = nb::none(), "Create a dynamic attribute.");
1458 c.def_prop_ro(
1459 "params",
1460 [](PyDynamicAttribute &self) {
1461 size_t numParams = mlirDynamicAttrGetNumParams(self);
1462 std::vector<PyAttribute> params;
1463 params.reserve(numParams);
1464 for (size_t i = 0; i < numParams; ++i)
1465 params.emplace_back(self.getContext(),
1466 mlirDynamicAttrGetParam(self, i));
1467 return params;
1468 },
1469 "Returns the parameters of the dynamic attribute as a list of "
1470 "attributes.");
1471 c.def_prop_ro("attr_name", [](PyDynamicAttribute &self) {
1472 MlirDynamicAttrDefinition attrDef = mlirDynamicAttrGetAttrDef(self);
1474 MlirDialect dialect = mlirDynamicAttrDefinitionGetDialect(attrDef);
1475 MlirStringRef dialectNamespace = mlirDialectGetNamespace(dialect);
1476 return std::string(dialectNamespace.data, dialectNamespace.length) + "." +
1477 std::string(name.data, name.length);
1478 });
1479 c.def_static(
1480 "lookup_typeid",
1481 [](const std::string &fullAttrName, DefaultingPyMlirContext context) {
1482 MlirDynamicAttrDefinition attrDef =
1483 getDynamicAttrDef(fullAttrName, context);
1485 },
1486 nb::arg("full_attr_name"), nb::arg("context") = nb::none(),
1487 "Look up the TypeID for the given dynamic attribute name.");
1488}
1489
1490void populateIRAttributes(nb::module_ &m) {
1493 PyDenseBoolArrayAttribute::PyDenseArrayIterator::bind(m);
1495 PyDenseI8ArrayAttribute::PyDenseArrayIterator::bind(m);
1497 PyDenseI16ArrayAttribute::PyDenseArrayIterator::bind(m);
1499 PyDenseI32ArrayAttribute::PyDenseArrayIterator::bind(m);
1501 PyDenseI64ArrayAttribute::PyDenseArrayIterator::bind(m);
1503 PyDenseF32ArrayAttribute::PyDenseArrayIterator::bind(m);
1505 PyDenseF64ArrayAttribute::PyDenseArrayIterator::bind(m);
1508 nb::cast<nb::callable>(nb::cpp_function(denseArrayAttributeCaster)));
1509
1517 nb::cast<nb::callable>(nb::cpp_function(
1520
1525 nb::cast<nb::callable>(
1526 nb::cpp_function(symbolRefOrFlatSymbolRefAttributeCaster)));
1527
1537 nb::cast<nb::callable>(nb::cpp_function(integerOrBoolAttributeCaster)));
1539
1542}
1543} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
1544} // namespace python
1545} // namespace mlir
#define Py_IsFinalizing
static const char kDenseElementsAttrGetDocstring[]
static const char kDenseResourceElementsAttrGetFromBufferDocstring[]
static const char kDenseElementsAttrGetFromListDocstring[]
MlirContext mlirAttributeGetContext(MlirAttribute attribute)
Definition IR.cpp:1290
MlirType mlirAttributeGetType(MlirAttribute attribute)
Definition IR.cpp:1294
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
Definition IRCore.h:310
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:537
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:291
Wrapper around the generic MlirAttribute.
Definition IRCore.h:1014
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
Definition IRCore.h:1016
static void bind(nanobind::module_ &m, PyType_Slot *slots=nullptr)
Definition IRCore.h:1096
nanobind::class_< PyAffineMapAttribute, PyAttribute > ClassTy
Definition IRCore.h:1071
static void bindFactoryMethods(ClassT &c, const char *pyClassName)
Registers get/get_splat factory methods with the concrete return type in the nb::sig.
static PyDenseElementsAttribute getSplat(const PyType &shapedType, PyAttribute &elementAttr)
static PyDenseElementsAttribute getFromList(const nanobind::typed< nanobind::sequence, PyAttribute > &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.
Refinement of the PyDenseElementsAttribute for attributes containing integer (and boolean) values.
nanobind::int_ dunderGetItem(intptr_t pos) const
Returns the element at the given linear position.
static PyDenseResourceElementsAttribute getFromBuffer(const nb_buffer &buffer, const std::string &name, const PyType &type, std::optional< size_t > alignment, bool isMutable, DefaultingPyMlirContext contextWrapper)
User-level object for accessing dialects with dotted syntax such as: ctx.dialect.std.
Definition IRCore.h:486
MlirDialect getDialectForKey(const std::string &key, bool attrError)
Definition IRCore.cpp:794
Float Point Attribute subclass - FloatAttr.
static PyGlobals & get()
Most code should get the globals via this static accessor.
Definition Globals.cpp:59
void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster, bool replace=false)
Adds a user-friendly type caster.
Definition Globals.cpp:125
static PyMlirContextRef forContext(MlirContext context)
Returns a context reference for the singleton PyMlirContext wrapper for the given context.
Definition IRCore.cpp:460
MlirContext get()
Accesses the underlying MlirContext.
Definition IRCore.h:224
PyMlirContextRef getRef()
Gets a strong reference to this context, which will ensure it is kept alive for the life of the refer...
Definition IRCore.cpp:445
Represents a Python MlirNamedAttr, carrying an optional owned name.
Definition IRCore.h:1040
static PySymbolRefAttribute fromList(const std::vector< std::string > &symbols, PyMlirContext &context)
A TypeID provides an efficient and unique identifier for a specific C++ type.
Definition IRCore.h:913
Wrapper around the generic MlirType.
Definition IRCore.h:887
nanobind::typed< nanobind::object, PyType > maybeDownCast()
Definition IRCore.cpp:1917
Unit Attribute subclass. Unit attributes don't have values.
MLIR_CAPI_EXPORTED MlirAttribute mlirAffineMapAttrGet(MlirAffineMap map)
Creates an affine map attribute wrapping the given map.
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 uint8_t mlirDenseElementsAttrGetUInt8Value(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED int64_t mlirStridedLayoutAttrGetOffset(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 unsigned mlirIntegerAttrGetValueNumWords(MlirAttribute attr)
Returns the number of 64-bit words that make up the integer attribute's underlying APInt value.
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 uint64_t mlirDenseElementsAttrGetIndexValue(MlirAttribute attr, intptr_t pos)
MLIR_CAPI_EXPORTED MlirTypeID mlirIntegerAttrGetTypeID(void)
Returns the typeID of an Integer attribute.
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 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 MlirAttribute mlirIntegerAttrGet(MlirType type, int64_t value)
Creates an integer attribute of the given type with the given integer value.
MLIR_CAPI_EXPORTED bool mlirDenseElementsAttrGetBoolValue(MlirAttribute attr, intptr_t pos)
Returns the pos-th value (flat contiguous indexing) of a specific type contained by the given dense e...
MLIR_CAPI_EXPORTED MlirAttribute mlirDictionaryAttrGet(MlirContext ctx, intptr_t numElements, MlirNamedAttribute const *elements)
Creates a dictionary attribute containing the given list of elements in the provided context.
MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseResourceElementsAttrGet(MlirType shapedType, MlirStringRef name, void *data, size_t dataLength, size_t dataAlignment, bool dataIsMutable, void(*deleter)(void *userData, const void *data, size_t size, size_t align), void *userData)
Unlike the typed accessors below, constructs the attribute with a raw data buffer and no type/alignme...
MLIR_CAPI_EXPORTED MlirAttribute mlirSymbolRefAttrGetNestedReference(MlirAttribute attr, intptr_t pos)
Returns pos-th reference nested in the given symbol reference attribute.
MLIR_CAPI_EXPORTED void mlirIntegerAttrGetValueWords(MlirAttribute attr, uint64_t *words)
Copies the 64-bit words making up the integer attribute's APInt value into the provided buffer.
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 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 mlirDenseTypedElementsAttrGetTypeID(void)
Returns the typeID of a DenseTypedElements 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 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 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 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 float mlirDenseElementsAttrGetFloatValue(MlirAttribute attr, intptr_t pos)
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 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 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 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 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 MlirAttribute mlirDenseElementsAttrSplatGet(MlirType shapedType, MlirAttribute element)
Creates a dense elements attribute with the given Shaped type containing a single replicated element ...
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 unsigned mlirIntegerAttrGetValueBitWidth(MlirAttribute attr)
Returns the bit width of the integer attribute's underlying APInt value.
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 MlirAttribute mlirStringAttrTypedGet(MlirType type, MlirStringRef str)
Creates a string attribute in the given context containing the given string.
MLIR_CAPI_EXPORTED MlirAttribute mlirIntegerAttrGetFromWords(MlirType type, unsigned numWords, const uint64_t *words)
Creates an integer attribute of the given type from an array of 64-bit words.
MLIR_CAPI_EXPORTED MlirStringRef mlirFlatSymbolRefAttrGetValue(MlirAttribute attr)
Returns the referenced symbol as a string reference.
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 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 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.
MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGetParam(MlirAttribute attr, intptr_t index)
Get the parameter at the given index in the provided dynamic attribute.
MLIR_CAPI_EXPORTED MlirDialect mlirDynamicAttrDefinitionGetDialect(MlirDynamicAttrDefinition attrDef)
Get the dialect that the given dynamic attribute definition belongs to.
MLIR_CAPI_EXPORTED MlirAttribute mlirDynamicAttrGet(MlirDynamicAttrDefinition attrDef, MlirAttribute *attrs, intptr_t numAttrs)
Get a dynamic attribute by instantiating the given attribute definition with the provided attributes.
MLIR_CAPI_EXPORTED bool mlirDialectIsAExtensibleDialect(MlirDialect dialect)
Check if the given dialect is an extensible dialect.
MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition mlirDynamicAttrGetAttrDef(MlirAttribute attr)
Get the attribute definition of the given dynamic attribute.
MLIR_CAPI_EXPORTED MlirDynamicAttrDefinition mlirExtensibleDialectLookupAttrDefinition(MlirDialect dialect, MlirStringRef attrName)
Look up a registered attribute definition by attribute name in the given dialect.
MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicAttrDefinitionGetTypeID(MlirDynamicAttrDefinition attrDef)
Get the type ID of a dynamic attribute definition.
MLIR_CAPI_EXPORTED MlirStringRef mlirDynamicAttrDefinitionGetName(MlirDynamicAttrDefinition attrDef)
Get the name of the given dynamic attribute definition.
MLIR_CAPI_EXPORTED intptr_t mlirDynamicAttrGetNumParams(MlirAttribute attr)
Get the number of parameters in the given dynamic attribute.
MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect)
Returns the namespace of the given dialect.
Definition IR.cpp:136
MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, MlirAttribute attr)
Associates an attribute with the name. Takes ownership of neither.
Definition IR.cpp:1321
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Definition IR.cpp:1342
MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type)
Gets the context that a type was created with.
Definition IR.cpp:1259
MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition IR.cpp:1271
MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str)
Gets an identifier with the given string value.
Definition IR.cpp:1330
nb::object symbolRefOrFlatSymbolRefAttributeCaster(PyAttribute &pyAttribute)
nb::object integerOrBoolAttributeCaster(PyAttribute &pyAttribute)
MlirStringRef toMlirStringRef(const std::string &s)
Definition IRCore.h:1348
static T pyTryCast(nanobind::handle object)
nb::object denseTypedElementsAttributeCaster(PyAttribute &pyAttribute)
nb::object denseArrayAttributeCaster(PyAttribute &pyAttribute)
static MlirDynamicAttrDefinition getDynamicAttrDef(const std::string &fullAttrName, DefaultingPyMlirContext context)
MLIR_PYTHON_API_EXPORTED void populateIRAttributes(nanobind::module_ &m)
Include the generated interface declarations.
std::string join(const Ts &...args)
Helper function to concatenate arguments into a std::string.
Named MLIR attribute.
Definition IR.h:76
MlirAttribute attribute
Definition IR.h:78
MlirIdentifier name
Definition IR.h:77
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80
Custom exception that allows access to error diagnostic information.
Definition IRCore.h:1330
RAII object that captures any error diagnostics emitted to the provided context.
Definition IRCore.h:446
std::vector< PyDiagnostic::DiagnosticInfo > take()
Definition IRCore.h:456
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))