[OM] Add initial CAPI for OM dialect Evaluator. #5248
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds the usual CAPI structure and dialect registration boilerplate, as well as CAPIs around the Evaluator library.
The APIs are intended to be as minimal and straightforward as possible, simply wrapping and unwrapping the C++ structures when possible.
One slight divergence is in the ObjectValue type, which is a std::variant between an Object shared pointer or an Attribute. The normal approach of casting to and from a void pointer does not work with std::variant, so a different representation of ObjectValue is used instead. The discriminated union is simply represented as a struct which only over has one field set. It might be possible to save some space using a struct with a C union and a flag, but the simplicity of the current approach seemed reasonable.
Another minor detail worth mentioning is that we must take some care to ensure the shared pointers to Objects have their reference count kept up to date. In the CAPI for the instantiate method, if we simply return the shared pointer, the reference will be lost as the Object pointer travels to C as a void pointer, so we allocate a new shared pointer in the CAPI, which ensures the reference count accurately reflect that we have handed out another reference.