Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OM] Add C API and Python bindings for IntegerAttr to string. #6787

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading