1c7994bd9SMehdi Amini //===-- mlir-c/Interop.h - Constants for Python/C-API interop -----*- C -*-===//
2c7994bd9SMehdi Amini //
3c7994bd9SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4c7994bd9SMehdi Amini // Exceptions.
5c7994bd9SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
6c7994bd9SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7c7994bd9SMehdi Amini //
8c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
9c7994bd9SMehdi Amini //
10c7994bd9SMehdi Amini // This header declares constants and helpers necessary for C-level
11c7994bd9SMehdi Amini // interop with the MLIR Python extension module. Since the Python bindings
12c7994bd9SMehdi Amini // are a thin wrapper around the MLIR C-API, a further C-API is not provided
13c7994bd9SMehdi Amini // specifically for the Python extension. Instead, simple facilities are
14c7994bd9SMehdi Amini // provided for translating between Python types and corresponding MLIR C-API
15c7994bd9SMehdi Amini // types.
16c7994bd9SMehdi Amini //
17c7994bd9SMehdi Amini // This header is standalone, requiring nothing beyond normal linking against
18c7994bd9SMehdi Amini // the Python implementation.
19c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
20543922cdSStella Laurenzo
21543922cdSStella Laurenzo #ifndef MLIR_C_BINDINGS_PYTHON_INTEROP_H
22543922cdSStella Laurenzo #define MLIR_C_BINDINGS_PYTHON_INTEROP_H
23543922cdSStella Laurenzo
24784a5bccSStella Stamenova // We *should*, in theory, include Python.h here in order to import the correct
25784a5bccSStella Stamenova // definitions for what we need below, however, importing Python.h directly on
26784a5bccSStella Stamenova // Windows results in the enforcement of either pythonX.lib or pythonX_d.lib
27784a5bccSStella Stamenova // depending on the build flavor. Instead, we rely on the fact that this file
28784a5bccSStella Stamenova // (Interop.h) is always included AFTER pybind11 and will therefore have access
29784a5bccSStella Stamenova // to the definitions from Python.h in addition to having a workaround applied
30784a5bccSStella Stamenova // through the pybind11 headers that allows us to control which python library
31784a5bccSStella Stamenova // is used.
32784a5bccSStella Stamenova #if !defined(_MSC_VER)
33543922cdSStella Laurenzo #include <Python.h>
34784a5bccSStella Stamenova #endif
35543922cdSStella Laurenzo
3674628c43SAlex Zinenko #include "mlir-c/AffineExpr.h"
37abb4cd3eSzhanghb97 #include "mlir-c/AffineMap.h"
3813cb4317SMehdi Amini #include "mlir-c/ExecutionEngine.h"
39543922cdSStella Laurenzo #include "mlir-c/IR.h"
40b208e5bcSAlex Zinenko #include "mlir-c/IntegerSet.h"
415fef6ce0SStella Laurenzo #include "mlir-c/Pass.h"
42*18cf1cd9SJacques Pienaar #include "mlir-c/Rewrite.h"
43543922cdSStella Laurenzo
44e78b745cSStella Laurenzo // The 'mlir' Python package is relocatable and supports co-existing in multiple
45e78b745cSStella Laurenzo // projects. Each project must define its outer package prefix with this define
46e78b745cSStella Laurenzo // in order to provide proper isolation and local name resolution.
47e78b745cSStella Laurenzo // The default is for the upstream "import mlir" package layout.
48e78b745cSStella Laurenzo // Note that this prefix is internally stringified, allowing it to be passed
49e78b745cSStella Laurenzo // unquoted on the compiler command line without shell quote escaping issues.
50e78b745cSStella Laurenzo #ifndef MLIR_PYTHON_PACKAGE_PREFIX
51e78b745cSStella Laurenzo #define MLIR_PYTHON_PACKAGE_PREFIX mlir.
52e78b745cSStella Laurenzo #endif
53e78b745cSStella Laurenzo
54e78b745cSStella Laurenzo // Makes a fully-qualified name relative to the MLIR python package.
55e78b745cSStella Laurenzo #define MLIR_PYTHON_STRINGIZE(s) #s
56e78b745cSStella Laurenzo #define MLIR_PYTHON_STRINGIZE_ARG(arg) MLIR_PYTHON_STRINGIZE(arg)
57e78b745cSStella Laurenzo #define MAKE_MLIR_PYTHON_QUALNAME(local) \
58e78b745cSStella Laurenzo MLIR_PYTHON_STRINGIZE_ARG(MLIR_PYTHON_PACKAGE_PREFIX) local
59e78b745cSStella Laurenzo
60e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_AFFINE_EXPR \
61e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.AffineExpr._CAPIPtr")
62e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_AFFINE_MAP \
63e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.AffineMap._CAPIPtr")
64e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_ATTRIBUTE \
65e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute._CAPIPtr")
66c83318e3SAdam Paszke #define MLIR_PYTHON_CAPSULE_BLOCK MAKE_MLIR_PYTHON_QUALNAME("ir.Block._CAPIPtr")
67e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_CONTEXT \
68e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.Context._CAPIPtr")
695e83a5b4SStella Laurenzo #define MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY \
705e83a5b4SStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.DialectRegistry._CAPIPtr")
7113cb4317SMehdi Amini #define MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE \
72e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("execution_engine.ExecutionEngine._CAPIPtr")
73e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_INTEGER_SET \
74e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.IntegerSet._CAPIPtr")
75e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_LOCATION \
76e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.Location._CAPIPtr")
77e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_MODULE \
78e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.Module._CAPIPtr")
79e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_OPERATION \
80e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("ir.Operation._CAPIPtr")
81e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_TYPE MAKE_MLIR_PYTHON_QUALNAME("ir.Type._CAPIPtr")
82e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_PASS_MANAGER \
83e78b745cSStella Laurenzo MAKE_MLIR_PYTHON_QUALNAME("passmanager.PassManager._CAPIPtr")
84e78b745cSStella Laurenzo #define MLIR_PYTHON_CAPSULE_VALUE MAKE_MLIR_PYTHON_QUALNAME("ir.Value._CAPIPtr")
85d39a7844Smax #define MLIR_PYTHON_CAPSULE_TYPEID \
86d39a7844Smax MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID._CAPIPtr")
87543922cdSStella Laurenzo
88543922cdSStella Laurenzo /** Attribute on MLIR Python objects that expose their C-API pointer.
89543922cdSStella Laurenzo * This will be a type-specific capsule created as per one of the helpers
90543922cdSStella Laurenzo * below.
91543922cdSStella Laurenzo *
92543922cdSStella Laurenzo * Ownership is not transferred by acquiring a capsule in this way: the
93543922cdSStella Laurenzo * validity of the pointer wrapped by the capsule will be bounded by the
94543922cdSStella Laurenzo * lifetime of the Python object that produced it. Only the name and pointer
95543922cdSStella Laurenzo * of the capsule are set. The caller is free to set a destructor and context
96543922cdSStella Laurenzo * as needed to manage anything further. */
97543922cdSStella Laurenzo #define MLIR_PYTHON_CAPI_PTR_ATTR "_CAPIPtr"
98543922cdSStella Laurenzo
99543922cdSStella Laurenzo /** Attribute on MLIR Python objects that exposes a factory function for
100543922cdSStella Laurenzo * constructing the corresponding Python object from a type-specific
101543922cdSStella Laurenzo * capsule wrapping the C-API pointer. The signature of the function is:
102543922cdSStella Laurenzo * def _CAPICreate(capsule) -> object
103543922cdSStella Laurenzo * Calling such a function implies a transfer of ownership of the object the
104543922cdSStella Laurenzo * capsule wraps: after such a call, the capsule should be considered invalid,
105543922cdSStella Laurenzo * and its wrapped pointer must not be destroyed.
106543922cdSStella Laurenzo *
107543922cdSStella Laurenzo * Only a very small number of Python objects can be created in such a fashion
108543922cdSStella Laurenzo * (i.e. top-level types such as Context where the lifetime can be cleanly
109543922cdSStella Laurenzo * delineated). */
110543922cdSStella Laurenzo #define MLIR_PYTHON_CAPI_FACTORY_ATTR "_CAPICreate"
111543922cdSStella Laurenzo
112bfb1ba75Smax /** Attribute on MLIR Python objects that expose a function for downcasting the
113bfb1ba75Smax * corresponding Python object to a subclass if the object is in fact a subclass
114bfb1ba75Smax * (Concrete or mlir_type_subclass) of ir.Type. The signature of the function
115bfb1ba75Smax * is: def maybe_downcast(self) -> object where the resulting object will
116bfb1ba75Smax * (possibly) be an instance of the subclass.
117bfb1ba75Smax */
118bfb1ba75Smax #define MLIR_PYTHON_MAYBE_DOWNCAST_ATTR "maybe_downcast"
119bfb1ba75Smax
120bfb1ba75Smax /** Attribute on main C extension module (_mlir) that corresponds to the
121bfb1ba75Smax * type caster registration binding. The signature of the function is:
1227c850867SMaksim Levental * def register_type_caster(MlirTypeID mlirTypeID, *, bool replace)
1237c850867SMaksim Levental * which then takes a typeCaster (register_type_caster is meant to be used as a
1247c850867SMaksim Levental * decorator from python), and where replace indicates the typeCaster should
1257c850867SMaksim Levental * replace any existing registered type casters (such as those for upstream
1267c850867SMaksim Levental * ConcreteTypes). The interface of the typeCaster is: def type_caster(ir.Type)
1277c850867SMaksim Levental * -> SubClassTypeT where SubClassTypeT indicates the result should be a
1287c850867SMaksim Levental * subclass (inherit from) ir.Type.
129bfb1ba75Smax */
130bfb1ba75Smax #define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR "register_type_caster"
131bfb1ba75Smax
1327c850867SMaksim Levental /** Attribute on main C extension module (_mlir) that corresponds to the
1337c850867SMaksim Levental * value caster registration binding. The signature of the function is:
1347c850867SMaksim Levental * def register_value_caster(MlirTypeID mlirTypeID, *, bool replace)
1357c850867SMaksim Levental * which then takes a valueCaster (register_value_caster is meant to be used as
1367c850867SMaksim Levental * a decorator, from python), and where replace indicates the valueCaster should
1377c850867SMaksim Levental * replace any existing registered value casters. The interface of the
1387c850867SMaksim Levental * valueCaster is: def value_caster(ir.Value) -> SubClassValueT where
1397c850867SMaksim Levental * SubClassValueT indicates the result should be a subclass (inherit from)
1407c850867SMaksim Levental * ir.Value.
1417c850867SMaksim Levental */
1427c850867SMaksim Levental #define MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR "register_value_caster"
1437c850867SMaksim Levental
1445fef6ce0SStella Laurenzo /// Gets a void* from a wrapped struct. Needed because const cast is different
1455fef6ce0SStella Laurenzo /// between C/C++.
1465fef6ce0SStella Laurenzo #ifdef __cplusplus
14744bdcb88SMehdi Amini #define MLIR_PYTHON_GET_WRAPPED_POINTER(object) \
14844bdcb88SMehdi Amini (const_cast<void *>((object).ptr))
1495fef6ce0SStella Laurenzo #else
1505fef6ce0SStella Laurenzo #define MLIR_PYTHON_GET_WRAPPED_POINTER(object) (void *)(object.ptr)
1515fef6ce0SStella Laurenzo #endif
1525fef6ce0SStella Laurenzo
153543922cdSStella Laurenzo #ifdef __cplusplus
154543922cdSStella Laurenzo extern "C" {
155543922cdSStella Laurenzo #endif
156543922cdSStella Laurenzo
15774628c43SAlex Zinenko /** Creates a capsule object encapsulating the raw C-API MlirAffineExpr. The
15874628c43SAlex Zinenko * returned capsule does not extend or affect ownership of any Python objects
15974628c43SAlex Zinenko * that reference the expression in any way.
16074628c43SAlex Zinenko */
mlirPythonAffineExprToCapsule(MlirAffineExpr expr)16174628c43SAlex Zinenko static inline PyObject *mlirPythonAffineExprToCapsule(MlirAffineExpr expr) {
16274628c43SAlex Zinenko return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(expr),
16374628c43SAlex Zinenko MLIR_PYTHON_CAPSULE_AFFINE_EXPR, NULL);
16474628c43SAlex Zinenko }
16574628c43SAlex Zinenko
16674628c43SAlex Zinenko /** Extracts an MlirAffineExpr from a capsule as produced from
16774628c43SAlex Zinenko * mlirPythonAffineExprToCapsule. If the capsule is not of the right type, then
16874628c43SAlex Zinenko * a null expression is returned (as checked via mlirAffineExprIsNull). In such
16974628c43SAlex Zinenko * a case, the Python APIs will have already set an error. */
mlirPythonCapsuleToAffineExpr(PyObject * capsule)17074628c43SAlex Zinenko static inline MlirAffineExpr mlirPythonCapsuleToAffineExpr(PyObject *capsule) {
17174628c43SAlex Zinenko void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_AFFINE_EXPR);
17274628c43SAlex Zinenko MlirAffineExpr expr = {ptr};
17374628c43SAlex Zinenko return expr;
17474628c43SAlex Zinenko }
17574628c43SAlex Zinenko
1762fb5f4a1SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirAttribute.
1772fb5f4a1SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
1782fb5f4a1SStella Laurenzo * objects that reference the attribute in any way.
1792fb5f4a1SStella Laurenzo */
mlirPythonAttributeToCapsule(MlirAttribute attribute)1802fb5f4a1SStella Laurenzo static inline PyObject *mlirPythonAttributeToCapsule(MlirAttribute attribute) {
1812fb5f4a1SStella Laurenzo return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(attribute),
1822fb5f4a1SStella Laurenzo MLIR_PYTHON_CAPSULE_ATTRIBUTE, NULL);
1832fb5f4a1SStella Laurenzo }
1842fb5f4a1SStella Laurenzo
1852fb5f4a1SStella Laurenzo /** Extracts an MlirAttribute from a capsule as produced from
1862fb5f4a1SStella Laurenzo * mlirPythonAttributeToCapsule. If the capsule is not of the right type, then
1872fb5f4a1SStella Laurenzo * a null attribute is returned (as checked via mlirAttributeIsNull). In such a
1882fb5f4a1SStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToAttribute(PyObject * capsule)1892fb5f4a1SStella Laurenzo static inline MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule) {
1902fb5f4a1SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_ATTRIBUTE);
1912fb5f4a1SStella Laurenzo MlirAttribute attr = {ptr};
1922fb5f4a1SStella Laurenzo return attr;
1932fb5f4a1SStella Laurenzo }
1942fb5f4a1SStella Laurenzo
195c83318e3SAdam Paszke /** Creates a capsule object encapsulating the raw C-API MlirBlock.
196c83318e3SAdam Paszke * The returned capsule does not extend or affect ownership of any Python
197c83318e3SAdam Paszke * objects that reference the module in any way. */
mlirPythonBlockToCapsule(MlirBlock block)198c83318e3SAdam Paszke static inline PyObject *mlirPythonBlockToCapsule(MlirBlock block) {
199c83318e3SAdam Paszke return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(block),
200c83318e3SAdam Paszke MLIR_PYTHON_CAPSULE_BLOCK, NULL);
201c83318e3SAdam Paszke }
202c83318e3SAdam Paszke
203c83318e3SAdam Paszke /** Extracts an MlirBlock from a capsule as produced from
204c83318e3SAdam Paszke * mlirPythonBlockToCapsule. If the capsule is not of the right type, then
205c83318e3SAdam Paszke * a null pass manager is returned (as checked via mlirBlockIsNull). */
mlirPythonCapsuleToBlock(PyObject * capsule)206c83318e3SAdam Paszke static inline MlirBlock mlirPythonCapsuleToBlock(PyObject *capsule) {
207c83318e3SAdam Paszke void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_BLOCK);
208c83318e3SAdam Paszke MlirBlock block = {ptr};
209c83318e3SAdam Paszke return block;
210c83318e3SAdam Paszke }
211c83318e3SAdam Paszke
212543922cdSStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirContext.
213543922cdSStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
214543922cdSStella Laurenzo * objects that reference the context in any way.
215543922cdSStella Laurenzo */
mlirPythonContextToCapsule(MlirContext context)2165fef6ce0SStella Laurenzo static inline PyObject *mlirPythonContextToCapsule(MlirContext context) {
217543922cdSStella Laurenzo return PyCapsule_New(context.ptr, MLIR_PYTHON_CAPSULE_CONTEXT, NULL);
218543922cdSStella Laurenzo }
219543922cdSStella Laurenzo
220543922cdSStella Laurenzo /** Extracts a MlirContext from a capsule as produced from
221543922cdSStella Laurenzo * mlirPythonContextToCapsule. If the capsule is not of the right type, then
222543922cdSStella Laurenzo * a null context is returned (as checked via mlirContextIsNull). In such a
223543922cdSStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToContext(PyObject * capsule)2245fef6ce0SStella Laurenzo static inline MlirContext mlirPythonCapsuleToContext(PyObject *capsule) {
225543922cdSStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_CONTEXT);
226543922cdSStella Laurenzo MlirContext context = {ptr};
227543922cdSStella Laurenzo return context;
228543922cdSStella Laurenzo }
229543922cdSStella Laurenzo
2305e83a5b4SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
2315e83a5b4SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
2325e83a5b4SStella Laurenzo * objects that reference the context in any way.
2335e83a5b4SStella Laurenzo */
2345e83a5b4SStella Laurenzo static inline PyObject *
mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry)2355e83a5b4SStella Laurenzo mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry) {
2365e83a5b4SStella Laurenzo return PyCapsule_New(registry.ptr, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY,
2375e83a5b4SStella Laurenzo NULL);
2385e83a5b4SStella Laurenzo }
2395e83a5b4SStella Laurenzo
2405e83a5b4SStella Laurenzo /** Extracts an MlirDialectRegistry from a capsule as produced from
2415e83a5b4SStella Laurenzo * mlirPythonDialectRegistryToCapsule. If the capsule is not of the right type,
2425e83a5b4SStella Laurenzo * then a null context is returned (as checked via mlirContextIsNull). In such a
2435e83a5b4SStella Laurenzo * case, the Python APIs will have already set an error. */
2445e83a5b4SStella Laurenzo static inline MlirDialectRegistry
mlirPythonCapsuleToDialectRegistry(PyObject * capsule)2455e83a5b4SStella Laurenzo mlirPythonCapsuleToDialectRegistry(PyObject *capsule) {
2465e83a5b4SStella Laurenzo void *ptr =
2475e83a5b4SStella Laurenzo PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_DIALECT_REGISTRY);
2485e83a5b4SStella Laurenzo MlirDialectRegistry registry = {ptr};
2495e83a5b4SStella Laurenzo return registry;
2505e83a5b4SStella Laurenzo }
2515e83a5b4SStella Laurenzo
252bd2083c2SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirLocation.
253bd2083c2SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
254bd2083c2SStella Laurenzo * objects that reference the location in any way. */
mlirPythonLocationToCapsule(MlirLocation loc)255bd2083c2SStella Laurenzo static inline PyObject *mlirPythonLocationToCapsule(MlirLocation loc) {
256bd2083c2SStella Laurenzo return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(loc),
257bd2083c2SStella Laurenzo MLIR_PYTHON_CAPSULE_LOCATION, NULL);
258bd2083c2SStella Laurenzo }
259bd2083c2SStella Laurenzo
260bd2083c2SStella Laurenzo /** Extracts an MlirLocation from a capsule as produced from
261bd2083c2SStella Laurenzo * mlirPythonLocationToCapsule. If the capsule is not of the right type, then
262bd2083c2SStella Laurenzo * a null module is returned (as checked via mlirLocationIsNull). In such a
263bd2083c2SStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToLocation(PyObject * capsule)264bd2083c2SStella Laurenzo static inline MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule) {
265bd2083c2SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_LOCATION);
266bd2083c2SStella Laurenzo MlirLocation loc = {ptr};
267bd2083c2SStella Laurenzo return loc;
268bd2083c2SStella Laurenzo }
269bd2083c2SStella Laurenzo
270543922cdSStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirModule.
271543922cdSStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
272543922cdSStella Laurenzo * objects that reference the module in any way. */
mlirPythonModuleToCapsule(MlirModule module)2735fef6ce0SStella Laurenzo static inline PyObject *mlirPythonModuleToCapsule(MlirModule module) {
2745fef6ce0SStella Laurenzo return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(module),
2755fef6ce0SStella Laurenzo MLIR_PYTHON_CAPSULE_MODULE, NULL);
276543922cdSStella Laurenzo }
277543922cdSStella Laurenzo
278ad958f64SStella Laurenzo /** Extracts an MlirModule from a capsule as produced from
279ad958f64SStella Laurenzo * mlirPythonModuleToCapsule. If the capsule is not of the right type, then
280ad958f64SStella Laurenzo * a null module is returned (as checked via mlirModuleIsNull). In such a
281ad958f64SStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToModule(PyObject * capsule)2825fef6ce0SStella Laurenzo static inline MlirModule mlirPythonCapsuleToModule(PyObject *capsule) {
283ad958f64SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_MODULE);
284ad958f64SStella Laurenzo MlirModule module = {ptr};
285ad958f64SStella Laurenzo return module;
286ad958f64SStella Laurenzo }
287ad958f64SStella Laurenzo
288*18cf1cd9SJacques Pienaar /** Creates a capsule object encapsulating the raw C-API
289*18cf1cd9SJacques Pienaar * MlirFrozenRewritePatternSet.
290*18cf1cd9SJacques Pienaar * The returned capsule does not extend or affect ownership of any Python
291*18cf1cd9SJacques Pienaar * objects that reference the module in any way. */
292*18cf1cd9SJacques Pienaar static inline PyObject *
mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm)293*18cf1cd9SJacques Pienaar mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm) {
294*18cf1cd9SJacques Pienaar return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(pm),
295*18cf1cd9SJacques Pienaar MLIR_PYTHON_CAPSULE_PASS_MANAGER, NULL);
296*18cf1cd9SJacques Pienaar }
297*18cf1cd9SJacques Pienaar
298*18cf1cd9SJacques Pienaar /** Extracts an MlirFrozenRewritePatternSet from a capsule as produced from
299*18cf1cd9SJacques Pienaar * mlirPythonFrozenRewritePatternSetToCapsule. If the capsule is not of the
300*18cf1cd9SJacques Pienaar * right type, then a null module is returned. */
301*18cf1cd9SJacques Pienaar static inline MlirFrozenRewritePatternSet
mlirPythonCapsuleToFrozenRewritePatternSet(PyObject * capsule)302*18cf1cd9SJacques Pienaar mlirPythonCapsuleToFrozenRewritePatternSet(PyObject *capsule) {
303*18cf1cd9SJacques Pienaar void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_PASS_MANAGER);
304*18cf1cd9SJacques Pienaar MlirFrozenRewritePatternSet pm = {ptr};
305*18cf1cd9SJacques Pienaar return pm;
306*18cf1cd9SJacques Pienaar }
307*18cf1cd9SJacques Pienaar
3085fef6ce0SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirPassManager.
3095fef6ce0SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
3105fef6ce0SStella Laurenzo * objects that reference the module in any way. */
mlirPythonPassManagerToCapsule(MlirPassManager pm)3115fef6ce0SStella Laurenzo static inline PyObject *mlirPythonPassManagerToCapsule(MlirPassManager pm) {
3125fef6ce0SStella Laurenzo return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(pm),
3135fef6ce0SStella Laurenzo MLIR_PYTHON_CAPSULE_PASS_MANAGER, NULL);
3145fef6ce0SStella Laurenzo }
3155fef6ce0SStella Laurenzo
3165fef6ce0SStella Laurenzo /** Extracts an MlirPassManager from a capsule as produced from
3175fef6ce0SStella Laurenzo * mlirPythonPassManagerToCapsule. If the capsule is not of the right type, then
3185fef6ce0SStella Laurenzo * a null pass manager is returned (as checked via mlirPassManagerIsNull). */
3195fef6ce0SStella Laurenzo static inline MlirPassManager
mlirPythonCapsuleToPassManager(PyObject * capsule)3205fef6ce0SStella Laurenzo mlirPythonCapsuleToPassManager(PyObject *capsule) {
3215fef6ce0SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_PASS_MANAGER);
3225fef6ce0SStella Laurenzo MlirPassManager pm = {ptr};
3235fef6ce0SStella Laurenzo return pm;
3245fef6ce0SStella Laurenzo }
3255fef6ce0SStella Laurenzo
3262fb5f4a1SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirOperation.
3272fb5f4a1SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
3282fb5f4a1SStella Laurenzo * objects that reference the operation in any way.
3292fb5f4a1SStella Laurenzo */
mlirPythonOperationToCapsule(MlirOperation operation)3302fb5f4a1SStella Laurenzo static inline PyObject *mlirPythonOperationToCapsule(MlirOperation operation) {
3312fb5f4a1SStella Laurenzo return PyCapsule_New(operation.ptr, MLIR_PYTHON_CAPSULE_OPERATION, NULL);
3322fb5f4a1SStella Laurenzo }
3332fb5f4a1SStella Laurenzo
3342fb5f4a1SStella Laurenzo /** Extracts an MlirOperations from a capsule as produced from
3352fb5f4a1SStella Laurenzo * mlirPythonOperationToCapsule. If the capsule is not of the right type, then
3362fb5f4a1SStella Laurenzo * a null type is returned (as checked via mlirOperationIsNull). In such a
3372fb5f4a1SStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToOperation(PyObject * capsule)3382fb5f4a1SStella Laurenzo static inline MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule) {
3392fb5f4a1SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_OPERATION);
3402fb5f4a1SStella Laurenzo MlirOperation op = {ptr};
3412fb5f4a1SStella Laurenzo return op;
3422fb5f4a1SStella Laurenzo }
3432fb5f4a1SStella Laurenzo
344d39a7844Smax /** Creates a capsule object encapsulating the raw C-API MlirTypeID.
345d39a7844Smax * The returned capsule does not extend or affect ownership of any Python
346d39a7844Smax * objects that reference the type in any way.
347d39a7844Smax */
mlirPythonTypeIDToCapsule(MlirTypeID typeID)348d39a7844Smax static inline PyObject *mlirPythonTypeIDToCapsule(MlirTypeID typeID) {
349d39a7844Smax return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(typeID),
350d39a7844Smax MLIR_PYTHON_CAPSULE_TYPEID, NULL);
351d39a7844Smax }
352d39a7844Smax
353d39a7844Smax /** Extracts an MlirTypeID from a capsule as produced from
354d39a7844Smax * mlirPythonTypeIDToCapsule. If the capsule is not of the right type, then
355d39a7844Smax * a null type is returned (as checked via mlirTypeIDIsNull). In such a
356d39a7844Smax * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToTypeID(PyObject * capsule)357d39a7844Smax static inline MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule) {
358d39a7844Smax void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_TYPEID);
359d39a7844Smax MlirTypeID typeID = {ptr};
360d39a7844Smax return typeID;
361d39a7844Smax }
362d39a7844Smax
3632fb5f4a1SStella Laurenzo /** Creates a capsule object encapsulating the raw C-API MlirType.
3642fb5f4a1SStella Laurenzo * The returned capsule does not extend or affect ownership of any Python
3652fb5f4a1SStella Laurenzo * objects that reference the type in any way.
3662fb5f4a1SStella Laurenzo */
mlirPythonTypeToCapsule(MlirType type)3672fb5f4a1SStella Laurenzo static inline PyObject *mlirPythonTypeToCapsule(MlirType type) {
3682fb5f4a1SStella Laurenzo return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(type),
3692fb5f4a1SStella Laurenzo MLIR_PYTHON_CAPSULE_TYPE, NULL);
3702fb5f4a1SStella Laurenzo }
3712fb5f4a1SStella Laurenzo
3722fb5f4a1SStella Laurenzo /** Extracts an MlirType from a capsule as produced from
3732fb5f4a1SStella Laurenzo * mlirPythonTypeToCapsule. If the capsule is not of the right type, then
3742fb5f4a1SStella Laurenzo * a null type is returned (as checked via mlirTypeIsNull). In such a
3752fb5f4a1SStella Laurenzo * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToType(PyObject * capsule)3762fb5f4a1SStella Laurenzo static inline MlirType mlirPythonCapsuleToType(PyObject *capsule) {
3772fb5f4a1SStella Laurenzo void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_TYPE);
3782fb5f4a1SStella Laurenzo MlirType type = {ptr};
3792fb5f4a1SStella Laurenzo return type;
3802fb5f4a1SStella Laurenzo }
3812fb5f4a1SStella Laurenzo
382abb4cd3eSzhanghb97 /** Creates a capsule object encapsulating the raw C-API MlirAffineMap.
383abb4cd3eSzhanghb97 * The returned capsule does not extend or affect ownership of any Python
384abb4cd3eSzhanghb97 * objects that reference the type in any way.
385abb4cd3eSzhanghb97 */
mlirPythonAffineMapToCapsule(MlirAffineMap affineMap)386abb4cd3eSzhanghb97 static inline PyObject *mlirPythonAffineMapToCapsule(MlirAffineMap affineMap) {
387abb4cd3eSzhanghb97 return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(affineMap),
388abb4cd3eSzhanghb97 MLIR_PYTHON_CAPSULE_AFFINE_MAP, NULL);
389abb4cd3eSzhanghb97 }
390abb4cd3eSzhanghb97
391abb4cd3eSzhanghb97 /** Extracts an MlirAffineMap from a capsule as produced from
392abb4cd3eSzhanghb97 * mlirPythonAffineMapToCapsule. If the capsule is not of the right type, then
393abb4cd3eSzhanghb97 * a null type is returned (as checked via mlirAffineMapIsNull). In such a
394abb4cd3eSzhanghb97 * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToAffineMap(PyObject * capsule)395abb4cd3eSzhanghb97 static inline MlirAffineMap mlirPythonCapsuleToAffineMap(PyObject *capsule) {
396abb4cd3eSzhanghb97 void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_AFFINE_MAP);
397abb4cd3eSzhanghb97 MlirAffineMap affineMap = {ptr};
398abb4cd3eSzhanghb97 return affineMap;
399abb4cd3eSzhanghb97 }
400abb4cd3eSzhanghb97
401b208e5bcSAlex Zinenko /** Creates a capsule object encapsulating the raw C-API MlirIntegerSet.
402b208e5bcSAlex Zinenko * The returned capsule does not extend or affect ownership of any Python
403b208e5bcSAlex Zinenko * objects that reference the set in any way. */
404b208e5bcSAlex Zinenko static inline PyObject *
mlirPythonIntegerSetToCapsule(MlirIntegerSet integerSet)405b208e5bcSAlex Zinenko mlirPythonIntegerSetToCapsule(MlirIntegerSet integerSet) {
406b208e5bcSAlex Zinenko return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(integerSet),
407b208e5bcSAlex Zinenko MLIR_PYTHON_CAPSULE_INTEGER_SET, NULL);
408b208e5bcSAlex Zinenko }
409b208e5bcSAlex Zinenko
410b208e5bcSAlex Zinenko /** Extracts an MlirIntegerSet from a capsule as produced from
411b208e5bcSAlex Zinenko * mlirPythonIntegerSetToCapsule. If the capsule is not of the right type, then
412b208e5bcSAlex Zinenko * a null set is returned (as checked via mlirIntegerSetIsNull). In such a
413b208e5bcSAlex Zinenko * case, the Python APIs will have already set an error. */
mlirPythonCapsuleToIntegerSet(PyObject * capsule)414b208e5bcSAlex Zinenko static inline MlirIntegerSet mlirPythonCapsuleToIntegerSet(PyObject *capsule) {
415b208e5bcSAlex Zinenko void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_INTEGER_SET);
416b208e5bcSAlex Zinenko MlirIntegerSet integerSet = {ptr};
417b208e5bcSAlex Zinenko return integerSet;
418b208e5bcSAlex Zinenko }
419b208e5bcSAlex Zinenko
42013cb4317SMehdi Amini /** Creates a capsule object encapsulating the raw C-API MlirExecutionEngine.
42113cb4317SMehdi Amini * The returned capsule does not extend or affect ownership of any Python
42213cb4317SMehdi Amini * objects that reference the set in any way. */
42313cb4317SMehdi Amini static inline PyObject *
mlirPythonExecutionEngineToCapsule(MlirExecutionEngine jit)42413cb4317SMehdi Amini mlirPythonExecutionEngineToCapsule(MlirExecutionEngine jit) {
42513cb4317SMehdi Amini return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(jit),
42613cb4317SMehdi Amini MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE, NULL);
42713cb4317SMehdi Amini }
42813cb4317SMehdi Amini
42913cb4317SMehdi Amini /** Extracts an MlirExecutionEngine from a capsule as produced from
43013cb4317SMehdi Amini * mlirPythonIntegerSetToCapsule. If the capsule is not of the right type, then
43113cb4317SMehdi Amini * a null set is returned (as checked via mlirExecutionEngineIsNull). In such a
43213cb4317SMehdi Amini * case, the Python APIs will have already set an error. */
43313cb4317SMehdi Amini static inline MlirExecutionEngine
mlirPythonCapsuleToExecutionEngine(PyObject * capsule)43413cb4317SMehdi Amini mlirPythonCapsuleToExecutionEngine(PyObject *capsule) {
43513cb4317SMehdi Amini void *ptr =
43613cb4317SMehdi Amini PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_EXECUTION_ENGINE);
43713cb4317SMehdi Amini MlirExecutionEngine jit = {ptr};
43813cb4317SMehdi Amini return jit;
43913cb4317SMehdi Amini }
44013cb4317SMehdi Amini
4413f3d1c90SMike Urbach /** Creates a capsule object encapsulating the raw C-API MlirValue.
4423f3d1c90SMike Urbach * The returned capsule does not extend or affect ownership of any Python
4433f3d1c90SMike Urbach * objects that reference the operation in any way.
4443f3d1c90SMike Urbach */
mlirPythonValueToCapsule(MlirValue value)4453f3d1c90SMike Urbach static inline PyObject *mlirPythonValueToCapsule(MlirValue value) {
4463f3d1c90SMike Urbach return PyCapsule_New(MLIR_PYTHON_GET_WRAPPED_POINTER(value),
4473f3d1c90SMike Urbach MLIR_PYTHON_CAPSULE_VALUE, NULL);
4483f3d1c90SMike Urbach }
4493f3d1c90SMike Urbach
4503f3d1c90SMike Urbach /** Extracts an MlirValue from a capsule as produced from
4513f3d1c90SMike Urbach * mlirPythonValueToCapsule. If the capsule is not of the right type, then a
4523f3d1c90SMike Urbach * null type is returned (as checked via mlirValueIsNull). In such a case, the
4533f3d1c90SMike Urbach * Python APIs will have already set an error. */
mlirPythonCapsuleToValue(PyObject * capsule)4543f3d1c90SMike Urbach static inline MlirValue mlirPythonCapsuleToValue(PyObject *capsule) {
4553f3d1c90SMike Urbach void *ptr = PyCapsule_GetPointer(capsule, MLIR_PYTHON_CAPSULE_VALUE);
4563f3d1c90SMike Urbach MlirValue value = {ptr};
4573f3d1c90SMike Urbach return value;
4583f3d1c90SMike Urbach }
4593f3d1c90SMike Urbach
460543922cdSStella Laurenzo #ifdef __cplusplus
461543922cdSStella Laurenzo }
462543922cdSStella Laurenzo #endif
463543922cdSStella Laurenzo
464543922cdSStella Laurenzo #endif // MLIR_C_BINDINGS_PYTHON_INTEROP_H
465