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] Python bindings for om integer #6042

Merged
merged 4 commits into from
Sep 19, 2023
Merged

Conversation

prithayan
Copy link
Contributor

@prithayan prithayan commented Sep 5, 2023

Implement the python bindings for the om.integer attribute. Also ensure all tests use om::IntegerAttr instead of mlir::IntegerAttr.

@prithayan prithayan marked this pull request as draft September 5, 2023 14:58
@prithayan prithayan force-pushed the dev/pbarua/om_int_evaluator branch 2 times, most recently from 8e4784a to c1ed204 Compare September 6, 2023 16:37
@prithayan prithayan marked this pull request as ready for review September 6, 2023 17:29
@prithayan prithayan changed the title [OM] Evaluator accept om.integer attribute [OM] Python bindings for om integer Sep 6, 2023
Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic bindings parts look good, just left some questions about a couple parts that I don't really understand the need for.


MLIR_CAPI_EXPORTED bool omAttrIsAIntegerAttr(MlirAttribute attr);

MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGetAttr(MlirAttribute attr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i think our convention so far is usually to just call it "blahTypeAttrGet", you don't need the final "Attr" in "omIntegerAttrGetAttr".

@@ -64,6 +65,21 @@ struct AttributeValue : EvaluatorValue {
Attribute attr;
};

struct IntegerValue : EvaluatorValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a new kind of EvaluatorValue? It seems like this could just be an AttributeValue. How is IntegerValue different from a normal AttributeValue, maybe I'm missing something, but I don't see it in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I think we don't need this.

@@ -171,15 +171,23 @@ MlirAttribute omEvaluatorValueGetPrimitive(OMEvaluatorValue evaluatorValue) {

/// Get the Primitive from an EvaluatorValue, which must contain a Primitive.
OMEvaluatorValue omEvaluatorValueFromPrimitive(MlirAttribute primitive) {
auto attr = unwrap(primitive);
// Create om::IntegerAttr for any mlir::IntegerAttr.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to do this conversion. It seems like this sort of logic shouldn't belong in the C API. What was the motivation for this part? We have conversion passes to make sure we're using the right types of data by the time we get to the evaluator.

Copy link
Contributor Author

@prithayan prithayan Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this conversion is required if we want to send integer constants from evaluator. Otherwise there will be type mismatch. The other alternative would be to create om.integer constants in the evaluator script, instead of
obj = evaluator.instantiate("Test", 42)
For example without this explicit conversion evaluator would error out,

 Evaluator (INFO) actual parameter: 42 : i64
 Evaluator (INFO) format parameter type: '!om.integer'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for clarifying. But, I still think the C API is not the right place for the conversion. I think we should generally avoid any logic in the C API layer besides converting to and from C++ objects. Could we do this in the Python code behind the instantiate API that wraps integers to always wrap them as om.integer attributes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Mike, that makes sense, I will try that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the right place would be to add the similar logic to dialects/om.py:unwrap_python_object.

@prithayan prithayan force-pushed the dev/pbarua/om_int_evaluator branch 2 times, most recently from e5aebd6 to 6186898 Compare September 14, 2023 13:59
@prithayan
Copy link
Contributor Author

@uenoku , @mikeurbach , Addressed the review comments, please take another look at this PR.

Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks for the iterations on this.

@@ -308,7 +308,7 @@ PythonValue omEvaluatorValueToPythonValue(OMEvaluatorValue result) {

OMEvaluatorValue pythonValueToOMEvaluatorValue(PythonValue result) {
if (auto *attr = std::get_if<MlirAttribute>(&result))
return omEvaluatorValueFromPrimitive(*attr);
return omEvaluatorValueFromPrimitive(omCastIntAttrIfValid(*attr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is needed here because we're still using the var_to_attribute from support.py right? I think this current implementation is fine--thanks for moving the cast to this point. Down the road, the OM evaluator might want to have it's own version of var_to_attribute, which always creates OM integer for Python integers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also wondering if we need something similar for the other primitive types, like strings, booleans, etc. If we do, then I think we should probably just define our own var_to_attribute for OM specifically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Mike. omCastIntAttrIfValid might be redundant if we tweaked om.py:unwrap_python_object/wrap_mlir_value.

Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the iterations, I think this is looking great.

(Attribute)unwrap(attr).cast<circt::om::IntegerAttr>().getValue());
}

MlirAttribute omCastIntAttrIfValid(MlirAttribute attr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be something like omIntegerAttrGet(MlirAttribute attr), and do a direct cast<mlir::IntegerAttr>(unwrap(attr))? I think the way you've refactored the Python, we just need a method to get an om::IntegerAttr from an mlir::IntegerAttr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mikeurbach , you are right, simplified the API.

lib/CAPI/Dialect/OM.cpp Outdated Show resolved Hide resolved
@@ -38,11 +38,16 @@ def wrap_mlir_object(value):
assert isinstance(value, BaseObject)
return Object(value)

def om_var_to_attribute(obj, none_on_fail: bool = False) -> ir.Attrbute:
if isinstance(obj, int):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need similar functionality for strings, bools, etc. right? That can come as follow ups.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for bools we are still using i1 , so mlir::BoolAttr should suffice.
Seems like StringAttr` also doesn't require an explicit cast.
Will add lit tests for string and bool, and the API if required.

@prithayan prithayan merged commit 10da65b into main Sep 19, 2023
5 checks passed
Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@prithayan prithayan deleted the dev/pbarua/om_int_evaluator branch April 11, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants