-
Notifications
You must be signed in to change notification settings - Fork 299
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
Conversation
8e4784a
to
c1ed204
Compare
There was a problem hiding this 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.
include/circt-c/Dialect/OM.h
Outdated
|
||
MLIR_CAPI_EXPORTED bool omAttrIsAIntegerAttr(MlirAttribute attr); | ||
|
||
MLIR_CAPI_EXPORTED MlirAttribute omIntegerAttrGetAttr(MlirAttribute attr); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
lib/CAPI/Dialect/OM.cpp
Outdated
@@ -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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
e5aebd6
to
6186898
Compare
@uenoku , @mikeurbach , Addressed the review comments, please take another look at this PR. |
There was a problem hiding this 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.
lib/Bindings/Python/OMModule.cpp
Outdated
@@ -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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Cast mlir::IntegerAttr to om::IntegerAttr
6186898
to
c7a3ee1
Compare
There was a problem hiding this 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.
lib/CAPI/Dialect/OM.cpp
Outdated
(Attribute)unwrap(attr).cast<circt::om::IntegerAttr>().getValue()); | ||
} | ||
|
||
MlirAttribute omCastIntAttrIfValid(MlirAttribute attr) { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
@@ -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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
e7aa58b
to
f0ce742
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
Implement the python bindings for the
om.integer
attribute. Also ensure all tests useom::IntegerAttr
instead ofmlir::IntegerAttr
.