-
Notifications
You must be signed in to change notification settings - Fork 303
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
Conversation
This solves the problem, but it's fairly hacky to do There is another hack, that I'm not sure about. To make the string, we are currently creating a new StringAttr, and returning a StringRef that points to the newly interned string. I've tried various mechanisms to return a string, or accept a string reference as an argument to overwrite, but I wasn't able to make those work. Is there a better way to return/assign a string in a C API? |
363c10d
to
7e4b8bd
Compare
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.
7e4b8bd
to
25886dd
Compare
Yeah seems like there is no CAPI for bridging APInt (there was an old thread in the forum https://discourse.llvm.org/t/llvm-support-types-in-c-api/1751 but no progress). I think the proper solution is to add CPI for APInt to MLIR upstream but |
Interesting, I hadn't noticed that one. I agree it would be best to handle upstream. Let me start an issue with my real ask: I want arbitrary width attributes that have widths > 64 to be able to be passed to Python. I'm curious what the community thinks. |
Seems like the general point upstream is we could take some time to think about the best way to do this for Python, but the easy thing of passing through a string is the most expedient way to get this done correctly. Any concern to merge this for now to get something working? I'm interested in designing the right thing here, but would also like to unblock for now. |
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 approach seems good enough, until we have a better API upstream.
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.