[Mlir-commits] [mlir] Handle large integers (> 64 bits) for the IntegerAttr C-API (PR #130539)

John Demme llvmlistbot at llvm.org
Wed Mar 19 13:50:28 PDT 2025


================
@@ -456,8 +456,30 @@ class PyIntegerAttribute : public PyConcreteAttribute<PyIntegerAttribute> {
   static void bindDerived(ClassTy &c) {
     c.def_static(
         "get",
-        [](PyType &type, int64_t value) {
-          MlirAttribute attr = mlirIntegerAttrGet(type, value);
+        [](PyType &type, py::int_ value) {
+          apint_interop_t interop;
+          if (mlirTypeIsAIndex(type))
+            interop.numbits = 64;
+          else
+            interop.numbits = mlirIntegerTypeGetWidth((MlirType)type);
+
+          py::object to_bytes = value.attr("to_bytes");
+          int numbytes = (interop.numbits + 7) / 8;
+          bool Signed =
+              mlirTypeIsAIndex(type) || mlirIntegerTypeIsSigned(type);
+          py::bytes bytes_obj =
+              to_bytes(numbytes, "little", py::arg("signed") = Signed);
----------------
teqdruid wrote:

Is it appropriate to check the signedness of the attribute type and use it here?

https://github.com/llvm/llvm-project/pull/130539


More information about the Mlir-commits mailing list