[Mlir-commits] [mlir] [mlir][python] Support Arbitrary Precision Integers in MLIR C API and Python Bindings (PR #177733)
Maksim Levental
llvmlistbot at llvm.org
Sat Jan 24 09:46:30 PST 2026
================
@@ -343,8 +343,50 @@ void PyFloatAttribute::bindDerived(ClassTy &c) {
void PyIntegerAttribute::bindDerived(ClassTy &c) {
c.def_static(
"get",
- [](PyType &type, int64_t value) {
- MlirAttribute attr = mlirIntegerAttrGet(type, value);
+ [](PyType &type, nb::object value) {
+ // Handle IndexType - it doesn't have a bit width or signedness.
+ if (mlirTypeIsAIndex(type)) {
+ int64_t intValue = nb::cast<int64_t>(value);
+ MlirAttribute attr = mlirIntegerAttrGet(type, intValue);
+ return PyIntegerAttribute(type.getContext(), attr);
+ }
+
+ // Get the bit width of the integer type.
+ unsigned bitWidth = mlirIntegerTypeGetWidth(type);
+
+ // Try to use the fast path for small integers.
+ if (bitWidth <= 64) {
+ int64_t intValue = nb::cast<int64_t>(value);
+ MlirAttribute attr = mlirIntegerAttrGet(type, intValue);
+ return PyIntegerAttribute(type.getContext(), attr);
+ }
+
+ // For larger integers, convert Python int to array of 64-bit words.
+ unsigned numWords = (bitWidth + 63) / 64;
----------------
makslevental wrote:
nit:
```suggestion
unsigned numWords = std::ceil(bitWidth / 64);
```
you might need to do `#include <cmath>`...
https://github.com/llvm/llvm-project/pull/177733
More information about the Mlir-commits
mailing list