#ifndef MLIR_C_BINDINGS_PYTHON_INTEROP_H
#define MLIR_C_BINDINGS_PYTHON_INTEROP_H
#if !defined(_MSC_VER)
#include <Python.h>
#endif
#include "mlir-c/AffineExpr.h"
#include "mlir-c/AffineMap.h"
#include "mlir-c/ExecutionEngine.h"
#include "mlir-c/IR.h"
#include "mlir-c/IntegerSet.h"
#include "mlir-c/Pass.h"
#include "mlir-c/Rewrite.h"
#ifndef MLIR_PYTHON_PACKAGE_PREFIX
#define MLIR_PYTHON_PACKAGE_PREFIX mlir.
#endif
#define MLIR_PYTHON_STRINGIZE(s) #s
#define MLIR_PYTHON_STRINGIZE_ARG(arg) MLIR_PYTHON_STRINGIZE(arg)
#define MAKE_MLIR_PYTHON_QUALNAME(local) \
MLIR_PYTHON_STRINGIZE_ARG(MLIR_PYTHON_PACKAGE_PREFIX) local
#define MLIR_PYTHON_CAPSULE_AFFINE_EXPR \
MAKE_MLIR_PYTHON_QUALNAME("ir.AffineExpr._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_AFFINE_MAP \
MAKE_MLIR_PYTHON_QUALNAME("ir.AffineMap._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_ATTRIBUTE \
MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_BLOCK MAKE_MLIR_PYTHON_QUALNAME("ir.Block._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_CONTEXT \
MAKE_MLIR_PYTHON_QUALNAME("ir.Context._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY \
MAKE_MLIR_PYTHON_QUALNAME("ir.DialectRegistry._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE \
MAKE_MLIR_PYTHON_QUALNAME("execution_engine.ExecutionEngine._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_INTEGER_SET \
MAKE_MLIR_PYTHON_QUALNAME("ir.IntegerSet._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_LOCATION \
MAKE_MLIR_PYTHON_QUALNAME("ir.Location._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_MODULE \
MAKE_MLIR_PYTHON_QUALNAME("ir.Module._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_OPERATION \
MAKE_MLIR_PYTHON_QUALNAME("ir.Operation._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_TYPE MAKE_MLIR_PYTHON_QUALNAME("ir.Type._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_PASS_MANAGER \
MAKE_MLIR_PYTHON_QUALNAME("passmanager.PassManager._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_VALUE MAKE_MLIR_PYTHON_QUALNAME("ir.Value._CAPIPtr")
#define MLIR_PYTHON_CAPSULE_TYPEID \
MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID._CAPIPtr")
* This will be a type-specific capsule created as per one of the helpers
* below.
*
* Ownership is not transferred by acquiring a capsule in this way: the
* validity of the pointer wrapped by the capsule will be bounded by the
* lifetime of the Python object that produced it. Only the name and pointer
* of the capsule are set. The caller is free to set a destructor and context
* as needed to manage anything further. */
#define MLIR_PYTHON_CAPI_PTR_ATTR "_CAPIPtr"
* constructing the corresponding Python object from a type-specific
* capsule wrapping the C-API pointer. The signature of the function is:
* def _CAPICreate(capsule) -> object
* Calling such a function implies a transfer of ownership of the object the
* capsule wraps: after such a call, the capsule should be considered invalid,
* and its wrapped pointer must not be destroyed.
*
* Only a very small number of Python objects can be created in such a fashion
* (i.e. top-level types such as Context where the lifetime can be cleanly
* delineated). */
#define MLIR_PYTHON_CAPI_FACTORY_ATTR "_CAPICreate"
* corresponding Python object to a subclass if the object is in fact a subclass
* (Concrete or mlir_type_subclass) of ir.Type. The signature of the function
* is: def maybe_downcast(self) -> object where the resulting object will
* (possibly) be an instance of the subclass.
*/
#define MLIR_PYTHON_MAYBE_DOWNCAST_ATTR "maybe_downcast"
* type caster registration binding. The signature of the function is:
* def register_type_caster(MlirTypeID mlirTypeID, *, bool replace)
* which then takes a typeCaster (register_type_caster is meant to be used as a
* decorator from python), and where replace indicates the typeCaster should
* replace any existing registered type casters (such as those for upstream
* ConcreteTypes). The interface of the typeCaster is: def type_caster(ir.Type)
* -> SubClassTypeT where SubClassTypeT indicates the result should be a
* subclass (inherit from) ir.Type.
*/
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR "register_type_caster"
* value caster registration binding. The signature of the function is:
* def register_value_caster(MlirTypeID mlirTypeID, *, bool replace)
* which then takes a valueCaster (register_value_caster is meant to be used as
* a decorator, from python), and where replace indicates the valueCaster should
* replace any existing registered value casters. The interface of the
* valueCaster is: def value_caster(ir.Value) -> SubClassValueT where
* SubClassValueT indicates the result should be a subclass (inherit from)
* ir.Value.
*/
#define MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR "register_value_caster"
#ifdef __cplusplus
#define MLIR_PYTHON_GET_WRAPPED_POINTER(object) \
(const_cast<void *>((object).ptr))
#else
#define MLIR_PYTHON_GET_WRAPPED_POINTER(object) (void *)(object.ptr)
#endif
#ifdef __cplusplus
extern "C" {
#endif
* returned capsule does not extend or affect ownership of any Python objects
* that reference the expression in any way.
*/
static inline PyObject *mlirPythonAffineExprToCapsule(MlirAffineExpr expr) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(expr),
MLIR_PYTHON_CAPSULE_AFFINE_EXPR, NULL);
}
* mlirPythonAffineExprToCapsule. If the capsule is not of the right type, then
* a null expression is returned (as checked via mlirAffineExprIsNull). In such
* a case, the Python APIs will have already set an error. */
static inline MlirAffineExpr mlirPythonCapsuleToAffineExpr(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_AFFINE_EXPR);
MlirAffineExpr expr = {ptr};
return expr;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the attribute in any way.
*/
static inline PyObject *mlirPythonAttributeToCapsule(MlirAttribute attribute) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(attribute),
MLIR_PYTHON_CAPSULE_ATTRIBUTE, NULL);
}
* mlirPythonAttributeToCapsule. If the capsule is not of the right type, then
* a null attribute is returned (as checked via mlirAttributeIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_ATTRIBUTE);
MlirAttribute attr = {ptr};
return attr;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the module in any way. */
static inline PyObject *mlirPythonBlockToCapsule(MlirBlock block) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(block),
MLIR_PYTHON_CAPSULE_BLOCK, NULL);
}
* mlirPythonBlockToCapsule. If the capsule is not of the right type, then
* a null pass manager is returned (as checked via mlirBlockIsNull). */
static inline MlirBlock mlirPythonCapsuleToBlock(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_BLOCK);
MlirBlock block = {ptr};
return block;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the context in any way.
*/
static inline PyObject *mlirPythonContextToCapsule(MlirContext context) {
return PyCapsule_New(context.ptr, MLIR_PYTHON_CAPSULE_CONTEXT, NULL);
}
* mlirPythonContextToCapsule. If the capsule is not of the right type, then
* a null context is returned (as checked via mlirContextIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirContext mlirPythonCapsuleToContext(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_CONTEXT);
MlirContext context = {ptr};
return context;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the context in any way.
*/
static inline PyObject *
mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry) {
return PyCapsule_New(registry.ptr, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY,
NULL);
}
* mlirPythonDialectRegistryToCapsule. If the capsule is not of the right type,
* then a null context is returned (as checked via mlirContextIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirDialectRegistry
mlirPythonCapsuleToDialectRegistry(PyObject *capsule) {
void *ptr =
PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY);
MlirDialectRegistry registry = {ptr};
return registry;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the location in any way. */
static inline PyObject *mlirPythonLocationToCapsule(MlirLocation loc) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(loc),
MLIR_PYTHON_CAPSULE_LOCATION, NULL);
}
* mlirPythonLocationToCapsule. If the capsule is not of the right type, then
* a null module is returned (as checked via mlirLocationIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_LOCATION);
MlirLocation loc = {ptr};
return loc;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the module in any way. */
static inline PyObject *mlirPythonModuleToCapsule(MlirModule module) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(module),
MLIR_PYTHON_CAPSULE_MODULE, NULL);
}
* mlirPythonModuleToCapsule. If the capsule is not of the right type, then
* a null module is returned (as checked via mlirModuleIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirModule mlirPythonCapsuleToModule(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_MODULE);
MlirModule module = {ptr};
return module;
}
* MlirFrozenRewritePatternSet.
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the module in any way. */
static inline PyObject *
mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(pm),
MLIR_PYTHON_CAPSULE_PASS_MANAGER, NULL);
}
* mlirPythonFrozenRewritePatternSetToCapsule. If the capsule is not of the
* right type, then a null module is returned. */
static inline MlirFrozenRewritePatternSet
mlirPythonCapsuleToFrozenRewritePatternSet(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_PASS_MANAGER);
MlirFrozenRewritePatternSet pm = {ptr};
return pm;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the module in any way. */
static inline PyObject *mlirPythonPassManagerToCapsule(MlirPassManager pm) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(pm),
MLIR_PYTHON_CAPSULE_PASS_MANAGER, NULL);
}
* mlirPythonPassManagerToCapsule. If the capsule is not of the right type, then
* a null pass manager is returned (as checked via mlirPassManagerIsNull). */
static inline MlirPassManager
mlirPythonCapsuleToPassManager(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_PASS_MANAGER);
MlirPassManager pm = {ptr};
return pm;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the operation in any way.
*/
static inline PyObject *mlirPythonOperationToCapsule(MlirOperation operation) {
return PyCapsule_New(operation.ptr, MLIR_PYTHON_CAPSULE_OPERATION, NULL);
}
* mlirPythonOperationToCapsule. If the capsule is not of the right type, then
* a null type is returned (as checked via mlirOperationIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_OPERATION);
MlirOperation op = {ptr};
return op;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the type in any way.
*/
static inline PyObject *mlirPythonTypeIDToCapsule(MlirTypeID typeID) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(typeID),
MLIR_PYTHON_CAPSULE_TYPEID, NULL);
}
* mlirPythonTypeIDToCapsule. If the capsule is not of the right type, then
* a null type is returned (as checked via mlirTypeIDIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_TYPEID);
MlirTypeID typeID = {ptr};
return typeID;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the type in any way.
*/
static inline PyObject *mlirPythonTypeToCapsule(MlirType type) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(type),
MLIR_PYTHON_CAPSULE_TYPE, NULL);
}
* mlirPythonTypeToCapsule. If the capsule is not of the right type, then
* a null type is returned (as checked via mlirTypeIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirType mlirPythonCapsuleToType(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_TYPE);
MlirType type = {ptr};
return type;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the type in any way.
*/
static inline PyObject *mlirPythonAffineMapToCapsule(MlirAffineMap affineMap) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(affineMap),
MLIR_PYTHON_CAPSULE_AFFINE_MAP, NULL);
}
* mlirPythonAffineMapToCapsule. If the capsule is not of the right type, then
* a null type is returned (as checked via mlirAffineMapIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirAffineMap mlirPythonCapsuleToAffineMap(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_AFFINE_MAP);
MlirAffineMap affineMap = {ptr};
return affineMap;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the set in any way. */
static inline PyObject *
mlirPythonIntegerSetToCapsule(MlirIntegerSet integerSet) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(integerSet),
MLIR_PYTHON_CAPSULE_INTEGER_SET, NULL);
}
* mlirPythonIntegerSetToCapsule. If the capsule is not of the right type, then
* a null set is returned (as checked via mlirIntegerSetIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirIntegerSet mlirPythonCapsuleToIntegerSet(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_INTEGER_SET);
MlirIntegerSet integerSet = {ptr};
return integerSet;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the set in any way. */
static inline PyObject *
mlirPythonExecutionEngineToCapsule(MlirExecutionEngine jit) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(jit),
MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE, NULL);
}
* mlirPythonIntegerSetToCapsule. If the capsule is not of the right type, then
* a null set is returned (as checked via mlirExecutionEngineIsNull). In such a
* case, the Python APIs will have already set an error. */
static inline MlirExecutionEngine
mlirPythonCapsuleToExecutionEngine(PyObject *capsule) {
void *ptr =
PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE);
MlirExecutionEngine jit = {ptr};
return jit;
}
* The returned capsule does not extend or affect ownership of any Python
* objects that reference the operation in any way.
*/
static inline PyObject *mlirPythonValueToCapsule(MlirValue value) {
return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(value),
MLIR_PYTHON_CAPSULE_VALUE, NULL);
}
* mlirPythonValueToCapsule. If the capsule is not of the right type, then a
* null type is returned (as checked via mlirValueIsNull). In such a case, the
* Python APIs will have already set an error. */
static inline MlirValue mlirPythonCapsuleToValue(PyObject *capsule) {
void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_VALUE);
MlirValue value = {ptr};
return value;
}
#ifdef __cplusplus
}
#endif
#endif