18 #ifndef MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
19 #define MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H
21 #include <pybind11/pybind11.h>
22 #include <pybind11/pytypes.h>
23 #include <pybind11/stl.h>
28 #include "llvm/ADT/Twine.h"
47 if (PyCapsule_CheckExact(apiObject.ptr()))
48 return py::reinterpret_borrow<py::object>(apiObject);
50 auto repr = py::repr(apiObject).cast<std::string>();
52 (llvm::Twine(
"Expected an MLIR object (got ") + repr +
").").str());
64 struct type_caster<MlirAffineMap> {
66 bool load(handle src,
bool) {
74 static handle
cast(MlirAffineMap v, return_value_policy, handle) {
86 struct type_caster<MlirAttribute> {
88 bool load(handle src,
bool) {
93 static handle
cast(MlirAttribute v, return_value_policy, handle) {
105 struct type_caster<MlirContext> {
125 struct type_caster<MlirDialectRegistry> {
132 static handle
cast(MlirDialectRegistry v, return_value_policy, handle) {
133 py::object capsule = py::reinterpret_steal<py::object>(
136 .attr(
"DialectRegistry")
144 struct type_caster<MlirLocation> {
157 static handle
cast(MlirLocation v, return_value_policy, handle) {
169 struct type_caster<MlirModule> {
176 static handle
cast(MlirModule v, return_value_policy, handle) {
188 struct type_caster<MlirOperation> {
195 static handle
cast(MlirOperation v, return_value_policy, handle) {
196 if (v.ptr ==
nullptr)
209 struct type_caster<MlirValue> {
216 static handle
cast(MlirValue v, return_value_policy, handle) {
217 if (v.ptr ==
nullptr)
230 struct type_caster<MlirPassManager> {
241 struct type_caster<MlirType> {
248 static handle
cast(MlirType t, return_value_policy, handle) {
281 py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
286 metaclass(derivedClassName, py::make_tuple(
superClass), attributes);
287 scope.attr(derivedClassName) =
thisClass;
290 template <
typename Func,
typename... Extra>
293 std::forward<Func>(f), py::name(name), py::is_method(
thisClass),
294 py::sibling(py::getattr(
thisClass, name, py::none())), extra...);
299 template <
typename Func,
typename... Extra>
301 const Extra &...extra) {
303 std::forward<Func>(f), py::name(name), py::is_method(
thisClass),
304 py::sibling(py::getattr(
thisClass, name, py::none())), extra...);
305 auto builtinProperty =
306 py::reinterpret_borrow<py::object>((PyObject *)&PyProperty_Type);
307 thisClass.attr(name) = builtinProperty(cf);
311 template <
typename Func,
typename... Extra>
313 const Extra &...extra) {
314 static_assert(!std::is_member_function_pointer<Func>::value,
315 "def_staticmethod(...) called with a non-static member "
318 std::forward<Func>(f), py::name(name), py::scope(
thisClass),
319 py::sibling(py::getattr(
thisClass, name, py::none())), extra...);
320 thisClass.attr(cf.name()) = py::staticmethod(cf);
324 template <
typename Func,
typename... Extra>
326 const Extra &...extra) {
327 static_assert(!std::is_member_function_pointer<Func>::value,
328 "def_classmethod(...) called with a non-static member "
331 std::forward<Func>(f), py::name(name), py::scope(
thisClass),
332 py::sibling(py::getattr(
thisClass, name, py::none())), extra...);
334 py::reinterpret_borrow<py::object>(PyClassMethod_New(cf.ptr()));
355 scope, attrClassName, isaFunction,
357 .attr(
"Attribute")) {}
374 std::string captureTypeName(
376 py::cpp_function newCf(
377 [superCls, isaFunction, captureTypeName](py::object cls,
378 py::object otherAttribute) {
379 MlirAttribute rawAttribute = py::cast<MlirAttribute>(otherAttribute);
380 if (!isaFunction(rawAttribute)) {
381 auto origRepr = py::repr(otherAttribute).cast<std::string>();
382 throw std::invalid_argument(
383 (llvm::Twine(
"Cannot cast attribute to ") + captureTypeName +
384 " (from " + origRepr +
")")
387 py::object
self = superCls.attr(
"__new__")(cls, otherAttribute);
390 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_attr"));
396 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
397 py::arg(
"other_attribute"));
411 scope, typeClassName, isaFunction,
429 std::string captureTypeName(
431 py::cpp_function newCf(
432 [superCls, isaFunction, captureTypeName](py::object cls,
433 py::object otherType) {
434 MlirType rawType = py::cast<MlirType>(otherType);
435 if (!isaFunction(rawType)) {
436 auto origRepr = py::repr(otherType).cast<std::string>();
437 throw std::invalid_argument((llvm::Twine(
"Cannot cast type to ") +
438 captureTypeName +
" (from " +
442 py::object
self = superCls.attr(
"__new__")(cls, otherType);
445 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_type"));
451 [isaFunction](MlirType other) {
return isaFunction(other); },
452 py::arg(
"other_type"));
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
static MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule)
Extracts an MlirOperations from a capsule as produced from mlirPythonOperationToCapsule.
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
static MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule)
Extracts an MlirAttribute from a capsule as produced from mlirPythonAttributeToCapsule.
static PyObject * mlirPythonAttributeToCapsule(MlirAttribute attribute)
Creates a capsule object encapsulating the raw C-API MlirAttribute.
static PyObject * mlirPythonLocationToCapsule(MlirLocation loc)
Creates a capsule object encapsulating the raw C-API MlirLocation.
static MlirAffineMap mlirPythonCapsuleToAffineMap(PyObject *capsule)
Extracts an MlirAffineMap from a capsule as produced from mlirPythonAffineMapToCapsule.
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
static MlirModule mlirPythonCapsuleToModule(PyObject *capsule)
Extracts an MlirModule from a capsule as produced from mlirPythonModuleToCapsule.
static MlirContext mlirPythonCapsuleToContext(PyObject *capsule)
Extracts a MlirContext from a capsule as produced from mlirPythonContextToCapsule.
static PyObject * mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry)
Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
static PyObject * mlirPythonTypeToCapsule(MlirType type)
Creates a capsule object encapsulating the raw C-API MlirType.
static MlirDialectRegistry mlirPythonCapsuleToDialectRegistry(PyObject *capsule)
Extracts an MlirDialectRegistry from a capsule as produced from mlirPythonDialectRegistryToCapsule.
#define MAKE_MLIR_PYTHON_QUALNAME(local)
static MlirType mlirPythonCapsuleToType(PyObject *capsule)
Extracts an MlirType from a capsule as produced from mlirPythonTypeToCapsule.
static MlirValue mlirPythonCapsuleToValue(PyObject *capsule)
Extracts an MlirValue from a capsule as produced from mlirPythonValueToCapsule.
static PyObject * mlirPythonAffineMapToCapsule(MlirAffineMap affineMap)
Creates a capsule object encapsulating the raw C-API MlirAffineMap.
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
static PyObject * mlirPythonOperationToCapsule(MlirOperation operation)
Creates a capsule object encapsulating the raw C-API MlirOperation.
static MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule)
Extracts an MlirLocation from a capsule as produced from mlirPythonLocationToCapsule.
static PyObject * mlirPythonValueToCapsule(MlirValue value)
Creates a capsule object encapsulating the raw C-API MlirValue.
Creates a custom subclass of mlir.ir.Attribute, implementing a casting constructor and type checking ...
bool(*)(MlirAttribute) IsAFunctionTy
mlir_attribute_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Attribute super-class.
mlir_attribute_subclass(py::handle scope, const char *attrClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
Creates a custom subclass of mlir.ir.Type, implementing a casting constructor and type checking metho...
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Type super-class.
bool(*)(MlirType) IsAFunctionTy
Provides a facility like py::class_ for defining a new class in a scope, but this allows extension of...
pure_subclass & def_classmethod(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def(const char *name, Func &&f, const Extra &...extra)
py::object get_class() const
pure_subclass(py::handle scope, const char *derivedClassName, const py::object &superClass)
pure_subclass & def_property_readonly(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def_staticmethod(const char *name, Func &&f, const Extra &...extra)
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
static bool mlirAffineMapIsNull(MlirAffineMap affineMap)
Checks whether an affine map is null.
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
static bool mlirModuleIsNull(MlirModule module)
Checks whether a module is null.
static bool mlirValueIsNull(MlirValue value)
Returns whether the value is null.
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
static bool mlirContextIsNull(MlirContext context)
Checks whether a context is null.
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
static bool mlirDialectRegistryIsNull(MlirDialectRegistry registry)
Checks if the dialect registry is null.
static bool mlirOperationIsNull(MlirOperation op)
Checks whether the underlying operation is null.
Include the generated interface declarations.
static py::object mlirApiObjectToCapsule(py::handle apiObject)
Helper to convert a presumed MLIR API object to a capsule, accepting either an explicit Capsule (whic...
PYBIND11_TYPE_CASTER(MlirAffineMap, _("MlirAffineMap"))
static handle cast(MlirAffineMap v, return_value_policy, handle)
bool load(handle src, bool)
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirAttribute, _("MlirAttribute"))
static handle cast(MlirAttribute v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirContext, _("MlirContext"))
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirDialectRegistry, _("MlirDialectRegistry"))
static handle cast(MlirDialectRegistry v, return_value_policy, handle)
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirLocation, _("MlirLocation"))
bool load(handle src, bool)
static handle cast(MlirLocation v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirModule, _("MlirModule"))
static handle cast(MlirModule v, return_value_policy, handle)
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirOperation, _("MlirOperation"))
bool load(handle src, bool)
static handle cast(MlirOperation v, return_value_policy, handle)
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirPassManager, _("MlirPassManager"))
static handle cast(MlirType t, return_value_policy, handle)
bool load(handle src, bool)
PYBIND11_TYPE_CASTER(MlirType, _("MlirType"))
bool load(handle src, bool)
static handle cast(MlirValue v, return_value_policy, handle)
PYBIND11_TYPE_CASTER(MlirValue, _("MlirValue"))