Skip to content

Commit

Permalink
[OM] Add C API and Python bindings for IntegerAttr to string. (#6787)
Browse files Browse the repository at this point in the history
Both the upstream MLIR IntegerAttr and OM IntegerAttr are backed by an
arbitrary precision integer. However, the upstream Python bindings
don't have any mechanism to return an integer larger than 64 bits back
to Python, even though Python ints are also arbitrary precision
integers.

To support this, we can handle this where we explicitly convert OM
IntegerAttrs to Python values. The simplest thing is to print a string
representation of the arbitrary precision integer, and parse that to a
Python int. This adds the necessary C API and Python binding for a "to
string" method, and uses it in the attribute_to_var function.

There are smarter ways we can handle the conversion, but the "to
string" API seems generally useful, so I'm using that in the
conversion for now.
  • Loading branch information
mikeurbach authored Mar 8, 2024
1 parent 993e491 commit 1cc1069
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/circt-c/Dialect/OM.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGetInt(MlirAttribute attr);
/// Get an om::IntegerAttr from mlir::IntegerAttr.
MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGet(MlirAttribute attr);

/// Get a string representation of an om::IntegerAttr.
MLIR_CAPI_EXPORTED MlirStringRef omIntegerAttrToString(MlirAttribute attr);

//===----------------------------------------------------------------------===//
// ListAttr API
//===----------------------------------------------------------------------===//
Expand Down
38 changes: 37 additions & 1 deletion integration_test/Bindings/Python/dialects/om.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import circt
from circt.dialects import om
from circt.ir import Context, InsertionPoint, Location, Module
from circt.ir import Context, InsertionPoint, Location, Module, IntegerAttr, IntegerType
from circt.support import var_to_attribute

from dataclasses import dataclass
Expand Down Expand Up @@ -259,3 +259,39 @@

# CHECK: 3
print(delayed.result)

with Context() as ctx:
circt.register_dialects(ctx)

# Signless
int_attr1 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signless(64), 42))
# CHECK: 42
print(str(int_attr1))

int_attr2 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signless(64), -42))
# CHECK: 18446744073709551574
print(str(int_attr2))

# Signed
int_attr3 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signed(64), 42))
# CHECK: 42
print(str(int_attr3))

int_attr4 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_signed(64), -42))
# CHECK: -42
print(str(int_attr4))

# Unsigned
int_attr5 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_unsigned(64), 42))
# CHECK: 42
print(str(int_attr5))

int_attr6 = om.OMIntegerAttr.get(
IntegerAttr.get(IntegerType.get_unsigned(64), -42))
# CHECK: 18446744073709551574
print(str(int_attr6))
8 changes: 6 additions & 2 deletions lib/Bindings/Python/OMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,12 @@ void circt::python::populateDialectOMSubmodule(py::module &m) {
[](py::object cls, MlirAttribute intVal) {
return cls(omIntegerAttrGet(intVal));
})
.def_property_readonly("integer", [](MlirAttribute self) {
return omIntegerAttrGetInt(self);
.def_property_readonly(
"integer",
[](MlirAttribute self) { return omIntegerAttrGetInt(self); })
.def("__str__", [](MlirAttribute self) {
MlirStringRef str = omIntegerAttrToString(self);
return std::string(str.data, str.length);
});

// Add the OMListAttr definition
Expand Down
2 changes: 1 addition & 1 deletion lib/Bindings/Python/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def attribute_to_var(attr):
except ValueError:
pass
try:
return attribute_to_var(om.OMIntegerAttr(attr).integer)
return int(str(om.OMIntegerAttr(attr)))
except ValueError:
pass
try:
Expand Down
10 changes: 10 additions & 0 deletions lib/CAPI/Dialect/OM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ MlirAttribute omIntegerAttrGet(MlirAttribute attr) {
circt::om::IntegerAttr::get(integerAttr.getContext(), integerAttr));
}

/// Get a string representation of an om::IntegerAttr.
MlirStringRef omIntegerAttrToString(MlirAttribute attr) {
mlir::IntegerAttr integerAttr =
cast<circt::om::IntegerAttr>(unwrap(attr)).getValue();
SmallVector<char> str;
integerAttr.getValue().toString(
str, /*Radix=*/10, /*Signed=*/integerAttr.getType().isSignedInteger());
return wrap(StringAttr::get(integerAttr.getContext(), str).getValue());
}

//===----------------------------------------------------------------------===//
// ListAttr API.
//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit 1cc1069

Please sign in to comment.