Skip to content

[MLIR] Support returning IntegerAttr larger than 64 bits to Python #84190

Open
@mikeurbach

Description

@mikeurbach

I'm using the MLIR Python bindings in CIRCT to represent some large integers, and while both IntegerAttr and Python can support arbitrary precision integers, the bindings currently use these methods that force the value into an int64_t or uint64_t:

static py::int_ toPyInt(PyIntegerAttribute &self) {
MlirType type = mlirAttributeGetType(self);
if (mlirTypeIsAIndex(type) || mlirIntegerTypeIsSignless(type))
return mlirIntegerAttrGetValueInt(self);
if (mlirIntegerTypeIsSigned(type))
return mlirIntegerAttrGetValueSInt(self);
return mlirIntegerAttrGetValueUInt(self);
}

I'm currently handling this downstream, by calling int(str(myattr)), with a custom __str__ that uses a C API to call APInt's toString. This feels like a hack, so I'm wondering if there is interest in returning large bitwidth IntegerAttrs to Python upstream.

@uenoku pointed me to an old Discourse thread discussing C APIs for APInt here: https://discourse.llvm.org/t/llvm-support-types-in-c-api/1751. That discussion seemed to fizzle out. Is there interest in providing some sort of C API to support this Python binding use case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions