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 "IRModule.h"
10 
11 #include "PybindUtils.h"
12 
13 #include "mlir-c/BuiltinAttributes.h"
14 #include "mlir-c/BuiltinTypes.h"
15 
16 namespace py = pybind11;
17 using namespace mlir;
18 using namespace mlir::python;
19 
20 using llvm::Optional;
21 using llvm::SmallVector;
22 using llvm::Twine;
23 
24 //------------------------------------------------------------------------------
25 // Docstrings (trivial, non-duplicated docstrings are included inline).
26 //------------------------------------------------------------------------------
27 
28 static const char kDenseElementsAttrGetDocstring[] =
29     R"(Gets a DenseElementsAttr from a Python buffer or array.
30 
31 When `type` is not provided, then some limited type inferencing is done based
32 on the buffer format. Support presently exists for 8/16/32/64 signed and
33 unsigned integers and float16/float32/float64. DenseElementsAttrs of these
34 types can also be converted back to a corresponding buffer.
35 
36 For conversions outside of these types, a `type=` must be explicitly provided
37 and the buffer contents must be bit-castable to the MLIR internal
38 representation:
39 
40   * Integer types (except for i1): the buffer must be byte aligned to the
41     next byte boundary.
42   * Floating point types: Must be bit-castable to the given floating point
43     size.
44   * i1 (bool): Bit packed into 8bit words where the bit pattern matches a
45     row major ordering. An arbitrary Numpy `bool_` array can be bit packed to
46     this specification with: `np.packbits(ary, axis=None, bitorder='little')`.
47 
48 If a single element buffer is passed (or for i1, a single byte with value 0
49 or 255), then a splat will be created.
50 
51 Args:
52   array: The array or buffer to convert.
53   signless: If inferring an appropriate MLIR type, use signless types for
54     integers (defaults True).
55   type: Skips inference of the MLIR element type and uses this instead. The
56     storage size must be consistent with the actual contents of the buffer.
57   shape: Overrides the shape of the buffer when constructing the MLIR
58     shaped type. This is needed when the physical and logical shape differ (as
59     for i1).
60   context: Explicit context, if not from context manager.
61 
62 Returns:
63   DenseElementsAttr on success.
64 
65 Raises:
66   ValueError: If the type of the buffer or array cannot be matched to an MLIR
67     type or if the buffer does not meet expectations.
68 )";
69 
70 namespace {
71 
72 static MlirStringRef toMlirStringRef(const std::string &s) {
73   return mlirStringRefCreate(s.data(), s.size());
74 }
75 
76 class PyAffineMapAttribute : public PyConcreteAttribute<PyAffineMapAttribute> {
77 public:
78   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAAffineMap;
79   static constexpr const char *pyClassName = "AffineMapAttr";
80   using PyConcreteAttribute::PyConcreteAttribute;
81 
82   static void bindDerived(ClassTy &c) {
83     c.def_static(
84         "get",
85         [](PyAffineMap &affineMap) {
86           MlirAttribute attr = mlirAffineMapAttrGet(affineMap.get());
87           return PyAffineMapAttribute(affineMap.getContext(), attr);
88         },
89         py::arg("affine_map"), "Gets an attribute wrapping an AffineMap.");
90   }
91 };
92 
93 template <typename T>
94 static T pyTryCast(py::handle object) {
95   try {
96     return object.cast<T>();
97   } catch (py::cast_error &err) {
98     std::string msg =
99         std::string(
100             "Invalid attribute when attempting to create an ArrayAttribute (") +
101         err.what() + ")";
102     throw py::cast_error(msg);
103   } catch (py::reference_cast_error &err) {
104     std::string msg = std::string("Invalid attribute (None?) when attempting "
105                                   "to create an ArrayAttribute (") +
106                       err.what() + ")";
107     throw py::cast_error(msg);
108   }
109 }
110 
111 class PyArrayAttribute : public PyConcreteAttribute<PyArrayAttribute> {
112 public:
113   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAArray;
114   static constexpr const char *pyClassName = "ArrayAttr";
115   using PyConcreteAttribute::PyConcreteAttribute;
116 
117   class PyArrayAttributeIterator {
118   public:
119     PyArrayAttributeIterator(PyAttribute attr) : attr(attr) {}
120 
121     PyArrayAttributeIterator &dunderIter() { return *this; }
122 
123     PyAttribute dunderNext() {
124       if (nextIndex >= mlirArrayAttrGetNumElements(attr.get())) {
125         throw py::stop_iteration();
126       }
127       return PyAttribute(attr.getContext(),
128                          mlirArrayAttrGetElement(attr.get(), nextIndex++));
129     }
130 
131     static void bind(py::module &m) {
132       py::class_<PyArrayAttributeIterator>(m, "ArrayAttributeIterator",
133                                            py::module_local())
134           .def("__iter__", &PyArrayAttributeIterator::dunderIter)
135           .def("__next__", &PyArrayAttributeIterator::dunderNext);
136     }
137 
138   private:
139     PyAttribute attr;
140     int nextIndex = 0;
141   };
142 
143   PyAttribute getItem(intptr_t i) {
144     return PyAttribute(getContext(), mlirArrayAttrGetElement(*this, i));
145   }
146 
147   static void bindDerived(ClassTy &c) {
148     c.def_static(
149         "get",
150         [](py::list attributes, DefaultingPyMlirContext context) {
151           SmallVector<MlirAttribute> mlirAttributes;
152           mlirAttributes.reserve(py::len(attributes));
153           for (auto attribute : attributes) {
154             mlirAttributes.push_back(pyTryCast<PyAttribute>(attribute));
155           }
156           MlirAttribute attr = mlirArrayAttrGet(
157               context->get(), mlirAttributes.size(), mlirAttributes.data());
158           return PyArrayAttribute(context->getRef(), attr);
159         },
160         py::arg("attributes"), py::arg("context") = py::none(),
161         "Gets a uniqued Array attribute");
162     c.def("__getitem__",
163           [](PyArrayAttribute &arr, intptr_t i) {
164             if (i >= mlirArrayAttrGetNumElements(arr))
165               throw py::index_error("ArrayAttribute index out of range");
166             return arr.getItem(i);
167           })
168         .def("__len__",
169              [](const PyArrayAttribute &arr) {
170                return mlirArrayAttrGetNumElements(arr);
171              })
172         .def("__iter__", [](const PyArrayAttribute &arr) {
173           return PyArrayAttributeIterator(arr);
174         });
175     c.def("__add__", [](PyArrayAttribute arr, py::list extras) {
176       std::vector<MlirAttribute> attributes;
177       intptr_t numOldElements = mlirArrayAttrGetNumElements(arr);
178       attributes.reserve(numOldElements + py::len(extras));
179       for (intptr_t i = 0; i < numOldElements; ++i)
180         attributes.push_back(arr.getItem(i));
181       for (py::handle attr : extras)
182         attributes.push_back(pyTryCast<PyAttribute>(attr));
183       MlirAttribute arrayAttr = mlirArrayAttrGet(
184           arr.getContext()->get(), attributes.size(), attributes.data());
185       return PyArrayAttribute(arr.getContext(), arrayAttr);
186     });
187   }
188 };
189 
190 /// Float Point Attribute subclass - FloatAttr.
191 class PyFloatAttribute : public PyConcreteAttribute<PyFloatAttribute> {
192 public:
193   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAFloat;
194   static constexpr const char *pyClassName = "FloatAttr";
195   using PyConcreteAttribute::PyConcreteAttribute;
196 
197   static void bindDerived(ClassTy &c) {
198     c.def_static(
199         "get",
200         [](PyType &type, double value, DefaultingPyLocation loc) {
201           MlirAttribute attr = mlirFloatAttrDoubleGetChecked(loc, type, value);
202           // TODO: Rework error reporting once diagnostic engine is exposed
203           // in C API.
204           if (mlirAttributeIsNull(attr)) {
205             throw SetPyError(PyExc_ValueError,
206                              Twine("invalid '") +
207                                  py::repr(py::cast(type)).cast<std::string>() +
208                                  "' and expected floating point type.");
209           }
210           return PyFloatAttribute(type.getContext(), attr);
211         },
212         py::arg("type"), py::arg("value"), py::arg("loc") = py::none(),
213         "Gets an uniqued float point attribute associated to a type");
214     c.def_static(
215         "get_f32",
216         [](double value, DefaultingPyMlirContext context) {
217           MlirAttribute attr = mlirFloatAttrDoubleGet(
218               context->get(), mlirF32TypeGet(context->get()), value);
219           return PyFloatAttribute(context->getRef(), attr);
220         },
221         py::arg("value"), py::arg("context") = py::none(),
222         "Gets an uniqued float point attribute associated to a f32 type");
223     c.def_static(
224         "get_f64",
225         [](double value, DefaultingPyMlirContext context) {
226           MlirAttribute attr = mlirFloatAttrDoubleGet(
227               context->get(), mlirF64TypeGet(context->get()), value);
228           return PyFloatAttribute(context->getRef(), attr);
229         },
230         py::arg("value"), py::arg("context") = py::none(),
231         "Gets an uniqued float point attribute associated to a f64 type");
232     c.def_property_readonly(
233         "value",
234         [](PyFloatAttribute &self) {
235           return mlirFloatAttrGetValueDouble(self);
236         },
237         "Returns the value of the float point attribute");
238   }
239 };
240 
241 /// Integer Attribute subclass - IntegerAttr.
242 class PyIntegerAttribute : public PyConcreteAttribute<PyIntegerAttribute> {
243 public:
244   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAInteger;
245   static constexpr const char *pyClassName = "IntegerAttr";
246   using PyConcreteAttribute::PyConcreteAttribute;
247 
248   static void bindDerived(ClassTy &c) {
249     c.def_static(
250         "get",
251         [](PyType &type, int64_t value) {
252           MlirAttribute attr = mlirIntegerAttrGet(type, value);
253           return PyIntegerAttribute(type.getContext(), attr);
254         },
255         py::arg("type"), py::arg("value"),
256         "Gets an uniqued integer attribute associated to a type");
257     c.def_property_readonly(
258         "value",
259         [](PyIntegerAttribute &self) {
260           return mlirIntegerAttrGetValueInt(self);
261         },
262         "Returns the value of the integer attribute");
263   }
264 };
265 
266 /// Bool Attribute subclass - BoolAttr.
267 class PyBoolAttribute : public PyConcreteAttribute<PyBoolAttribute> {
268 public:
269   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsABool;
270   static constexpr const char *pyClassName = "BoolAttr";
271   using PyConcreteAttribute::PyConcreteAttribute;
272 
273   static void bindDerived(ClassTy &c) {
274     c.def_static(
275         "get",
276         [](bool value, DefaultingPyMlirContext context) {
277           MlirAttribute attr = mlirBoolAttrGet(context->get(), value);
278           return PyBoolAttribute(context->getRef(), attr);
279         },
280         py::arg("value"), py::arg("context") = py::none(),
281         "Gets an uniqued bool attribute");
282     c.def_property_readonly(
283         "value",
284         [](PyBoolAttribute &self) { return mlirBoolAttrGetValue(self); },
285         "Returns the value of the bool attribute");
286   }
287 };
288 
289 class PyFlatSymbolRefAttribute
290     : public PyConcreteAttribute<PyFlatSymbolRefAttribute> {
291 public:
292   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAFlatSymbolRef;
293   static constexpr const char *pyClassName = "FlatSymbolRefAttr";
294   using PyConcreteAttribute::PyConcreteAttribute;
295 
296   static void bindDerived(ClassTy &c) {
297     c.def_static(
298         "get",
299         [](std::string value, DefaultingPyMlirContext context) {
300           MlirAttribute attr =
301               mlirFlatSymbolRefAttrGet(context->get(), toMlirStringRef(value));
302           return PyFlatSymbolRefAttribute(context->getRef(), attr);
303         },
304         py::arg("value"), py::arg("context") = py::none(),
305         "Gets a uniqued FlatSymbolRef attribute");
306     c.def_property_readonly(
307         "value",
308         [](PyFlatSymbolRefAttribute &self) {
309           MlirStringRef stringRef = mlirFlatSymbolRefAttrGetValue(self);
310           return py::str(stringRef.data, stringRef.length);
311         },
312         "Returns the value of the FlatSymbolRef attribute as a string");
313   }
314 };
315 
316 class PyStringAttribute : public PyConcreteAttribute<PyStringAttribute> {
317 public:
318   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAString;
319   static constexpr const char *pyClassName = "StringAttr";
320   using PyConcreteAttribute::PyConcreteAttribute;
321 
322   static void bindDerived(ClassTy &c) {
323     c.def_static(
324         "get",
325         [](std::string value, DefaultingPyMlirContext context) {
326           MlirAttribute attr =
327               mlirStringAttrGet(context->get(), toMlirStringRef(value));
328           return PyStringAttribute(context->getRef(), attr);
329         },
330         py::arg("value"), py::arg("context") = py::none(),
331         "Gets a uniqued string attribute");
332     c.def_static(
333         "get_typed",
334         [](PyType &type, std::string value) {
335           MlirAttribute attr =
336               mlirStringAttrTypedGet(type, toMlirStringRef(value));
337           return PyStringAttribute(type.getContext(), attr);
338         },
339         py::arg("type"), py::arg("value"),
340         "Gets a uniqued string attribute associated to a type");
341     c.def_property_readonly(
342         "value",
343         [](PyStringAttribute &self) {
344           MlirStringRef stringRef = mlirStringAttrGetValue(self);
345           return py::str(stringRef.data, stringRef.length);
346         },
347         "Returns the value of the string attribute");
348   }
349 };
350 
351 // TODO: Support construction of string elements.
352 class PyDenseElementsAttribute
353     : public PyConcreteAttribute<PyDenseElementsAttribute> {
354 public:
355   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsADenseElements;
356   static constexpr const char *pyClassName = "DenseElementsAttr";
357   using PyConcreteAttribute::PyConcreteAttribute;
358 
359   static PyDenseElementsAttribute
360   getFromBuffer(py::buffer array, bool signless, Optional<PyType> explicitType,
361                 Optional<std::vector<int64_t>> explicitShape,
362                 DefaultingPyMlirContext contextWrapper) {
363     // Request a contiguous view. In exotic cases, this will cause a copy.
364     int flags = PyBUF_C_CONTIGUOUS | PyBUF_FORMAT;
365     Py_buffer *view = new Py_buffer();
366     if (PyObject_GetBuffer(array.ptr(), view, flags) != 0) {
367       delete view;
368       throw py::error_already_set();
369     }
370     py::buffer_info arrayInfo(view);
371     SmallVector<int64_t> shape;
372     if (explicitShape) {
373       shape.append(explicitShape->begin(), explicitShape->end());
374     } else {
375       shape.append(arrayInfo.shape.begin(),
376                    arrayInfo.shape.begin() + arrayInfo.ndim);
377     }
378 
379     MlirAttribute encodingAttr = mlirAttributeGetNull();
380     MlirContext context = contextWrapper->get();
381 
382     // Detect format codes that are suitable for bulk loading. This includes
383     // all byte aligned integer and floating point types up to 8 bytes.
384     // Notably, this excludes, bool (which needs to be bit-packed) and
385     // other exotics which do not have a direct representation in the buffer
386     // protocol (i.e. complex, etc).
387     Optional<MlirType> bulkLoadElementType;
388     if (explicitType) {
389       bulkLoadElementType = *explicitType;
390     } else if (arrayInfo.format == "f") {
391       // f32
392       assert(arrayInfo.itemsize == 4 && "mismatched array itemsize");
393       bulkLoadElementType = mlirF32TypeGet(context);
394     } else if (arrayInfo.format == "d") {
395       // f64
396       assert(arrayInfo.itemsize == 8 && "mismatched array itemsize");
397       bulkLoadElementType = mlirF64TypeGet(context);
398     } else if (arrayInfo.format == "e") {
399       // f16
400       assert(arrayInfo.itemsize == 2 && "mismatched array itemsize");
401       bulkLoadElementType = mlirF16TypeGet(context);
402     } else if (isSignedIntegerFormat(arrayInfo.format)) {
403       if (arrayInfo.itemsize == 4) {
404         // i32
405         bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 32)
406                                        : mlirIntegerTypeSignedGet(context, 32);
407       } else if (arrayInfo.itemsize == 8) {
408         // i64
409         bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 64)
410                                        : mlirIntegerTypeSignedGet(context, 64);
411       } else if (arrayInfo.itemsize == 1) {
412         // i8
413         bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 8)
414                                        : mlirIntegerTypeSignedGet(context, 8);
415       } else if (arrayInfo.itemsize == 2) {
416         // i16
417         bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 16)
418                                        : mlirIntegerTypeSignedGet(context, 16);
419       }
420     } else if (isUnsignedIntegerFormat(arrayInfo.format)) {
421       if (arrayInfo.itemsize == 4) {
422         // unsigned i32
423         bulkLoadElementType = signless
424                                   ? mlirIntegerTypeGet(context, 32)
425                                   : mlirIntegerTypeUnsignedGet(context, 32);
426       } else if (arrayInfo.itemsize == 8) {
427         // unsigned i64
428         bulkLoadElementType = signless
429                                   ? mlirIntegerTypeGet(context, 64)
430                                   : mlirIntegerTypeUnsignedGet(context, 64);
431       } else if (arrayInfo.itemsize == 1) {
432         // i8
433         bulkLoadElementType = signless ? mlirIntegerTypeGet(context, 8)
434                                        : mlirIntegerTypeUnsignedGet(context, 8);
435       } else if (arrayInfo.itemsize == 2) {
436         // i16
437         bulkLoadElementType = signless
438                                   ? mlirIntegerTypeGet(context, 16)
439                                   : mlirIntegerTypeUnsignedGet(context, 16);
440       }
441     }
442     if (bulkLoadElementType) {
443       auto shapedType = mlirRankedTensorTypeGet(
444           shape.size(), shape.data(), *bulkLoadElementType, encodingAttr);
445       size_t rawBufferSize = arrayInfo.size * arrayInfo.itemsize;
446       MlirAttribute attr = mlirDenseElementsAttrRawBufferGet(
447           shapedType, rawBufferSize, arrayInfo.ptr);
448       if (mlirAttributeIsNull(attr)) {
449         throw std::invalid_argument(
450             "DenseElementsAttr could not be constructed from the given buffer. "
451             "This may mean that the Python buffer layout does not match that "
452             "MLIR expected layout and is a bug.");
453       }
454       return PyDenseElementsAttribute(contextWrapper->getRef(), attr);
455     }
456 
457     throw std::invalid_argument(
458         std::string("unimplemented array format conversion from format: ") +
459         arrayInfo.format);
460   }
461 
462   static PyDenseElementsAttribute getSplat(PyType shapedType,
463                                            PyAttribute &elementAttr) {
464     auto contextWrapper =
465         PyMlirContext::forContext(mlirTypeGetContext(shapedType));
466     if (!mlirAttributeIsAInteger(elementAttr) &&
467         !mlirAttributeIsAFloat(elementAttr)) {
468       std::string message = "Illegal element type for DenseElementsAttr: ";
469       message.append(py::repr(py::cast(elementAttr)));
470       throw SetPyError(PyExc_ValueError, message);
471     }
472     if (!mlirTypeIsAShaped(shapedType) ||
473         !mlirShapedTypeHasStaticShape(shapedType)) {
474       std::string message =
475           "Expected a static ShapedType for the shaped_type parameter: ";
476       message.append(py::repr(py::cast(shapedType)));
477       throw SetPyError(PyExc_ValueError, message);
478     }
479     MlirType shapedElementType = mlirShapedTypeGetElementType(shapedType);
480     MlirType attrType = mlirAttributeGetType(elementAttr);
481     if (!mlirTypeEqual(shapedElementType, attrType)) {
482       std::string message =
483           "Shaped element type and attribute type must be equal: shaped=";
484       message.append(py::repr(py::cast(shapedType)));
485       message.append(", element=");
486       message.append(py::repr(py::cast(elementAttr)));
487       throw SetPyError(PyExc_ValueError, message);
488     }
489 
490     MlirAttribute elements =
491         mlirDenseElementsAttrSplatGet(shapedType, elementAttr);
492     return PyDenseElementsAttribute(contextWrapper->getRef(), elements);
493   }
494 
495   intptr_t dunderLen() { return mlirElementsAttrGetNumElements(*this); }
496 
497   py::buffer_info accessBuffer() {
498     if (mlirDenseElementsAttrIsSplat(*this)) {
499       // TODO: Currently crashes the program.
500       // Reported as https://github.com/pybind/pybind11/issues/3336
501       throw std::invalid_argument(
502           "unsupported data type for conversion to Python buffer");
503     }
504 
505     MlirType shapedType = mlirAttributeGetType(*this);
506     MlirType elementType = mlirShapedTypeGetElementType(shapedType);
507     std::string format;
508 
509     if (mlirTypeIsAF32(elementType)) {
510       // f32
511       return bufferInfo<float>(shapedType);
512     }
513     if (mlirTypeIsAF64(elementType)) {
514       // f64
515       return bufferInfo<double>(shapedType);
516     } else if (mlirTypeIsAF16(elementType)) {
517       // f16
518       return bufferInfo<uint16_t>(shapedType, "e");
519     } else if (mlirTypeIsAInteger(elementType) &&
520                mlirIntegerTypeGetWidth(elementType) == 32) {
521       if (mlirIntegerTypeIsSignless(elementType) ||
522           mlirIntegerTypeIsSigned(elementType)) {
523         // i32
524         return bufferInfo<int32_t>(shapedType);
525       } else if (mlirIntegerTypeIsUnsigned(elementType)) {
526         // unsigned i32
527         return bufferInfo<uint32_t>(shapedType);
528       }
529     } else if (mlirTypeIsAInteger(elementType) &&
530                mlirIntegerTypeGetWidth(elementType) == 64) {
531       if (mlirIntegerTypeIsSignless(elementType) ||
532           mlirIntegerTypeIsSigned(elementType)) {
533         // i64
534         return bufferInfo<int64_t>(shapedType);
535       } else if (mlirIntegerTypeIsUnsigned(elementType)) {
536         // unsigned i64
537         return bufferInfo<uint64_t>(shapedType);
538       }
539     } else if (mlirTypeIsAInteger(elementType) &&
540                mlirIntegerTypeGetWidth(elementType) == 8) {
541       if (mlirIntegerTypeIsSignless(elementType) ||
542           mlirIntegerTypeIsSigned(elementType)) {
543         // i8
544         return bufferInfo<int8_t>(shapedType);
545       } else if (mlirIntegerTypeIsUnsigned(elementType)) {
546         // unsigned i8
547         return bufferInfo<uint8_t>(shapedType);
548       }
549     } else if (mlirTypeIsAInteger(elementType) &&
550                mlirIntegerTypeGetWidth(elementType) == 16) {
551       if (mlirIntegerTypeIsSignless(elementType) ||
552           mlirIntegerTypeIsSigned(elementType)) {
553         // i16
554         return bufferInfo<int16_t>(shapedType);
555       } else if (mlirIntegerTypeIsUnsigned(elementType)) {
556         // unsigned i16
557         return bufferInfo<uint16_t>(shapedType);
558       }
559     }
560 
561     // TODO: Currently crashes the program.
562     // Reported as https://github.com/pybind/pybind11/issues/3336
563     throw std::invalid_argument(
564         "unsupported data type for conversion to Python buffer");
565   }
566 
567   static void bindDerived(ClassTy &c) {
568     c.def("__len__", &PyDenseElementsAttribute::dunderLen)
569         .def_static("get", PyDenseElementsAttribute::getFromBuffer,
570                     py::arg("array"), py::arg("signless") = true,
571                     py::arg("type") = py::none(), py::arg("shape") = py::none(),
572                     py::arg("context") = py::none(),
573                     kDenseElementsAttrGetDocstring)
574         .def_static("get_splat", PyDenseElementsAttribute::getSplat,
575                     py::arg("shaped_type"), py::arg("element_attr"),
576                     "Gets a DenseElementsAttr where all values are the same")
577         .def_property_readonly("is_splat",
578                                [](PyDenseElementsAttribute &self) -> bool {
579                                  return mlirDenseElementsAttrIsSplat(self);
580                                })
581         .def_buffer(&PyDenseElementsAttribute::accessBuffer);
582   }
583 
584 private:
585   static bool isUnsignedIntegerFormat(const std::string &format) {
586     if (format.empty())
587       return false;
588     char code = format[0];
589     return code == 'I' || code == 'B' || code == 'H' || code == 'L' ||
590            code == 'Q';
591   }
592 
593   static bool isSignedIntegerFormat(const std::string &format) {
594     if (format.empty())
595       return false;
596     char code = format[0];
597     return code == 'i' || code == 'b' || code == 'h' || code == 'l' ||
598            code == 'q';
599   }
600 
601   template <typename Type>
602   py::buffer_info bufferInfo(MlirType shapedType,
603                              const char *explicitFormat = nullptr) {
604     intptr_t rank = mlirShapedTypeGetRank(shapedType);
605     // Prepare the data for the buffer_info.
606     // Buffer is configured for read-only access below.
607     Type *data = static_cast<Type *>(
608         const_cast<void *>(mlirDenseElementsAttrGetRawData(*this)));
609     // Prepare the shape for the buffer_info.
610     SmallVector<intptr_t, 4> shape;
611     for (intptr_t i = 0; i < rank; ++i)
612       shape.push_back(mlirShapedTypeGetDimSize(shapedType, i));
613     // Prepare the strides for the buffer_info.
614     SmallVector<intptr_t, 4> strides;
615     intptr_t strideFactor = 1;
616     for (intptr_t i = 1; i < rank; ++i) {
617       strideFactor = 1;
618       for (intptr_t j = i; j < rank; ++j) {
619         strideFactor *= mlirShapedTypeGetDimSize(shapedType, j);
620       }
621       strides.push_back(sizeof(Type) * strideFactor);
622     }
623     strides.push_back(sizeof(Type));
624     std::string format;
625     if (explicitFormat) {
626       format = explicitFormat;
627     } else {
628       format = py::format_descriptor<Type>::format();
629     }
630     return py::buffer_info(data, sizeof(Type), format, rank, shape, strides,
631                            /*readonly=*/true);
632   }
633 }; // namespace
634 
635 /// Refinement of the PyDenseElementsAttribute for attributes containing integer
636 /// (and boolean) values. Supports element access.
637 class PyDenseIntElementsAttribute
638     : public PyConcreteAttribute<PyDenseIntElementsAttribute,
639                                  PyDenseElementsAttribute> {
640 public:
641   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsADenseIntElements;
642   static constexpr const char *pyClassName = "DenseIntElementsAttr";
643   using PyConcreteAttribute::PyConcreteAttribute;
644 
645   /// Returns the element at the given linear position. Asserts if the index is
646   /// out of range.
647   py::int_ dunderGetItem(intptr_t pos) {
648     if (pos < 0 || pos >= dunderLen()) {
649       throw SetPyError(PyExc_IndexError,
650                        "attempt to access out of bounds element");
651     }
652 
653     MlirType type = mlirAttributeGetType(*this);
654     type = mlirShapedTypeGetElementType(type);
655     assert(mlirTypeIsAInteger(type) &&
656            "expected integer element type in dense int elements attribute");
657     // Dispatch element extraction to an appropriate C function based on the
658     // elemental type of the attribute. py::int_ is implicitly constructible
659     // from any C++ integral type and handles bitwidth correctly.
660     // TODO: consider caching the type properties in the constructor to avoid
661     // querying them on each element access.
662     unsigned width = mlirIntegerTypeGetWidth(type);
663     bool isUnsigned = mlirIntegerTypeIsUnsigned(type);
664     if (isUnsigned) {
665       if (width == 1) {
666         return mlirDenseElementsAttrGetBoolValue(*this, pos);
667       }
668       if (width == 32) {
669         return mlirDenseElementsAttrGetUInt32Value(*this, pos);
670       }
671       if (width == 64) {
672         return mlirDenseElementsAttrGetUInt64Value(*this, pos);
673       }
674     } else {
675       if (width == 1) {
676         return mlirDenseElementsAttrGetBoolValue(*this, pos);
677       }
678       if (width == 32) {
679         return mlirDenseElementsAttrGetInt32Value(*this, pos);
680       }
681       if (width == 64) {
682         return mlirDenseElementsAttrGetInt64Value(*this, pos);
683       }
684     }
685     throw SetPyError(PyExc_TypeError, "Unsupported integer type");
686   }
687 
688   static void bindDerived(ClassTy &c) {
689     c.def("__getitem__", &PyDenseIntElementsAttribute::dunderGetItem);
690   }
691 };
692 
693 class PyDictAttribute : public PyConcreteAttribute<PyDictAttribute> {
694 public:
695   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsADictionary;
696   static constexpr const char *pyClassName = "DictAttr";
697   using PyConcreteAttribute::PyConcreteAttribute;
698 
699   intptr_t dunderLen() { return mlirDictionaryAttrGetNumElements(*this); }
700 
701   bool dunderContains(const std::string &name) {
702     return !mlirAttributeIsNull(
703         mlirDictionaryAttrGetElementByName(*this, toMlirStringRef(name)));
704   }
705 
706   static void bindDerived(ClassTy &c) {
707     c.def("__contains__", &PyDictAttribute::dunderContains);
708     c.def("__len__", &PyDictAttribute::dunderLen);
709     c.def_static(
710         "get",
711         [](py::dict attributes, DefaultingPyMlirContext context) {
712           SmallVector<MlirNamedAttribute> mlirNamedAttributes;
713           mlirNamedAttributes.reserve(attributes.size());
714           for (auto &it : attributes) {
715             auto &mlirAttr = it.second.cast<PyAttribute &>();
716             auto name = it.first.cast<std::string>();
717             mlirNamedAttributes.push_back(mlirNamedAttributeGet(
718                 mlirIdentifierGet(mlirAttributeGetContext(mlirAttr),
719                                   toMlirStringRef(name)),
720                 mlirAttr));
721           }
722           MlirAttribute attr =
723               mlirDictionaryAttrGet(context->get(), mlirNamedAttributes.size(),
724                                     mlirNamedAttributes.data());
725           return PyDictAttribute(context->getRef(), attr);
726         },
727         py::arg("value") = py::dict(), py::arg("context") = py::none(),
728         "Gets an uniqued dict attribute");
729     c.def("__getitem__", [](PyDictAttribute &self, const std::string &name) {
730       MlirAttribute attr =
731           mlirDictionaryAttrGetElementByName(self, toMlirStringRef(name));
732       if (mlirAttributeIsNull(attr)) {
733         throw SetPyError(PyExc_KeyError,
734                          "attempt to access a non-existent attribute");
735       }
736       return PyAttribute(self.getContext(), attr);
737     });
738     c.def("__getitem__", [](PyDictAttribute &self, intptr_t index) {
739       if (index < 0 || index >= self.dunderLen()) {
740         throw SetPyError(PyExc_IndexError,
741                          "attempt to access out of bounds attribute");
742       }
743       MlirNamedAttribute namedAttr = mlirDictionaryAttrGetElement(self, index);
744       return PyNamedAttribute(
745           namedAttr.attribute,
746           std::string(mlirIdentifierStr(namedAttr.name).data));
747     });
748   }
749 };
750 
751 /// Refinement of PyDenseElementsAttribute for attributes containing
752 /// floating-point values. Supports element access.
753 class PyDenseFPElementsAttribute
754     : public PyConcreteAttribute<PyDenseFPElementsAttribute,
755                                  PyDenseElementsAttribute> {
756 public:
757   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsADenseFPElements;
758   static constexpr const char *pyClassName = "DenseFPElementsAttr";
759   using PyConcreteAttribute::PyConcreteAttribute;
760 
761   py::float_ dunderGetItem(intptr_t pos) {
762     if (pos < 0 || pos >= dunderLen()) {
763       throw SetPyError(PyExc_IndexError,
764                        "attempt to access out of bounds element");
765     }
766 
767     MlirType type = mlirAttributeGetType(*this);
768     type = mlirShapedTypeGetElementType(type);
769     // Dispatch element extraction to an appropriate C function based on the
770     // elemental type of the attribute. py::float_ is implicitly constructible
771     // from float and double.
772     // TODO: consider caching the type properties in the constructor to avoid
773     // querying them on each element access.
774     if (mlirTypeIsAF32(type)) {
775       return mlirDenseElementsAttrGetFloatValue(*this, pos);
776     }
777     if (mlirTypeIsAF64(type)) {
778       return mlirDenseElementsAttrGetDoubleValue(*this, pos);
779     }
780     throw SetPyError(PyExc_TypeError, "Unsupported floating-point type");
781   }
782 
783   static void bindDerived(ClassTy &c) {
784     c.def("__getitem__", &PyDenseFPElementsAttribute::dunderGetItem);
785   }
786 };
787 
788 class PyTypeAttribute : public PyConcreteAttribute<PyTypeAttribute> {
789 public:
790   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAType;
791   static constexpr const char *pyClassName = "TypeAttr";
792   using PyConcreteAttribute::PyConcreteAttribute;
793 
794   static void bindDerived(ClassTy &c) {
795     c.def_static(
796         "get",
797         [](PyType value, DefaultingPyMlirContext context) {
798           MlirAttribute attr = mlirTypeAttrGet(value.get());
799           return PyTypeAttribute(context->getRef(), attr);
800         },
801         py::arg("value"), py::arg("context") = py::none(),
802         "Gets a uniqued Type attribute");
803     c.def_property_readonly("value", [](PyTypeAttribute &self) {
804       return PyType(self.getContext()->getRef(),
805                     mlirTypeAttrGetValue(self.get()));
806     });
807   }
808 };
809 
810 /// Unit Attribute subclass. Unit attributes don't have values.
811 class PyUnitAttribute : public PyConcreteAttribute<PyUnitAttribute> {
812 public:
813   static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAUnit;
814   static constexpr const char *pyClassName = "UnitAttr";
815   using PyConcreteAttribute::PyConcreteAttribute;
816 
817   static void bindDerived(ClassTy &c) {
818     c.def_static(
819         "get",
820         [](DefaultingPyMlirContext context) {
821           return PyUnitAttribute(context->getRef(),
822                                  mlirUnitAttrGet(context->get()));
823         },
824         py::arg("context") = py::none(), "Create a Unit attribute.");
825   }
826 };
827 
828 } // namespace
829 
830 void mlir::python::populateIRAttributes(py::module &m) {
831   PyAffineMapAttribute::bind(m);
832   PyArrayAttribute::bind(m);
833   PyArrayAttribute::PyArrayAttributeIterator::bind(m);
834   PyBoolAttribute::bind(m);
835   PyDenseElementsAttribute::bind(m);
836   PyDenseFPElementsAttribute::bind(m);
837   PyDenseIntElementsAttribute::bind(m);
838   PyDictAttribute::bind(m);
839   PyFlatSymbolRefAttribute::bind(m);
840   PyFloatAttribute::bind(m);
841   PyIntegerAttribute::bind(m);
842   PyStringAttribute::bind(m);
843   PyTypeAttribute::bind(m);
844   PyUnitAttribute::bind(m);
845 }
846