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"
31 using namespace py::literals;
48 if (PyCapsule_CheckExact(apiObject.ptr()))
49 return py::reinterpret_borrow<py::object>(apiObject);
51 auto repr = py::repr(apiObject).cast<std::string>();
53 (llvm::Twine(
"Expected an MLIR object (got ") + repr +
").").str());
65 struct type_caster<MlirAffineMap> {
67 bool load(handle src,
bool) {
75 static handle
cast(MlirAffineMap v, return_value_policy, handle) {
87 struct type_caster<MlirAttribute> {
89 bool load(handle src,
bool) {
94 static handle
cast(MlirAttribute v, return_value_policy, handle) {
107 struct type_caster<MlirBlock> {
118 struct type_caster<MlirContext> {
138 struct type_caster<MlirDialectRegistry> {
145 static handle
cast(MlirDialectRegistry v, return_value_policy, handle) {
146 py::object capsule = py::reinterpret_steal<py::object>(
149 .attr(
"DialectRegistry")
157 struct type_caster<MlirLocation> {
170 static handle
cast(MlirLocation v, return_value_policy, handle) {
182 struct type_caster<MlirModule> {
189 static handle
cast(MlirModule v, return_value_policy, handle) {
201 struct type_caster<MlirOperation> {
208 static handle
cast(MlirOperation v, return_value_policy, handle) {
209 if (v.ptr ==
nullptr)
222 struct type_caster<MlirValue> {
229 static handle
cast(MlirValue v, return_value_policy, handle) {
230 if (v.ptr ==
nullptr)
244 struct type_caster<MlirPassManager> {
255 struct type_caster<MlirTypeID> {
262 static handle
cast(MlirTypeID v, return_value_policy, handle) {
263 if (v.ptr ==
nullptr)
276 struct type_caster<MlirType> {
283 static handle
cast(MlirType t, return_value_policy, handle) {
315 const py::object &superClass) {
317 py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
318 py::object metaclass = pyType(superClass);
322 metaclass(derivedClassName, py::make_tuple(superClass), attributes);
323 scope.attr(derivedClassName) = thisClass;
326 template <
typename Func,
typename... Extra>
329 std::forward<Func>(f), py::name(name), py::is_method(thisClass),
330 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
331 thisClass.attr(cf.name()) = cf;
335 template <
typename Func,
typename... Extra>
337 const Extra &...extra) {
339 std::forward<Func>(f), py::name(name), py::is_method(thisClass),
340 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
341 auto builtinProperty =
342 py::reinterpret_borrow<py::object>((PyObject *)&PyProperty_Type);
343 thisClass.attr(name) = builtinProperty(cf);
347 template <
typename Func,
typename... Extra>
349 const Extra &...extra) {
350 static_assert(!std::is_member_function_pointer<Func>::value,
351 "def_staticmethod(...) called with a non-static member "
354 std::forward<Func>(f), py::name(name), py::scope(thisClass),
355 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
356 thisClass.attr(cf.name()) = py::staticmethod(cf);
360 template <
typename Func,
typename... Extra>
362 const Extra &...extra) {
363 static_assert(!std::is_member_function_pointer<Func>::value,
364 "def_classmethod(...) called with a non-static member "
367 std::forward<Func>(f), py::name(name), py::scope(thisClass),
368 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
369 thisClass.attr(cf.name()) =
370 py::reinterpret_borrow<py::object>(PyClassMethod_New(cf.ptr()));
391 scope, attrClassName, isaFunction,
393 .attr(
"Attribute")) {}
410 std::string captureTypeName(
412 py::cpp_function newCf(
413 [superCls, isaFunction, captureTypeName](py::object cls,
414 py::object otherAttribute) {
415 MlirAttribute rawAttribute = py::cast<MlirAttribute>(otherAttribute);
416 if (!isaFunction(rawAttribute)) {
417 auto origRepr = py::repr(otherAttribute).cast<std::string>();
418 throw std::invalid_argument(
419 (llvm::Twine(
"Cannot cast attribute to ") + captureTypeName +
420 " (from " + origRepr +
")")
423 py::object
self = superCls.attr(
"__new__")(cls, otherAttribute);
426 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_attr"));
427 thisClass.attr(
"__new__") = newCf;
432 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
433 py::arg(
"other_attribute"));
449 scope, typeClassName, isaFunction,
451 getTypeIDFunction) {}
469 std::string captureTypeName(
471 py::cpp_function newCf(
472 [superCls, isaFunction, captureTypeName](py::object cls,
473 py::object otherType) {
474 MlirType rawType = py::cast<MlirType>(otherType);
475 if (!isaFunction(rawType)) {
476 auto origRepr = py::repr(otherType).cast<std::string>();
477 throw std::invalid_argument((llvm::Twine(
"Cannot cast type to ") +
478 captureTypeName +
" (from " +
482 py::object
self = superCls.attr(
"__new__")(cls, otherType);
485 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_type"));
486 thisClass.attr(
"__new__") = newCf;
491 [isaFunction](MlirType other) {
return isaFunction(other); },
492 py::arg(
"other_type"));
493 def(
"__repr__", [superCls, captureTypeName](py::object
self) {
494 return py::repr(superCls(
self))
495 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName);
497 if (getTypeIDFunction) {
500 getTypeIDFunction())(pybind11::cpp_function(
501 [thisClass = thisClass](
const py::object &mlirType) {
502 return thisClass(mlirType);
518 scope, valueClassName, isaFunction,
537 std::string captureValueName(
539 py::cpp_function newCf(
540 [superCls, isaFunction, captureValueName](py::object cls,
541 py::object otherValue) {
542 MlirValue rawValue = py::cast<MlirValue>(otherValue);
543 if (!isaFunction(rawValue)) {
544 auto origRepr = py::repr(otherValue).cast<std::string>();
545 throw std::invalid_argument((llvm::Twine(
"Cannot cast value to ") +
546 captureValueName +
" (from " +
550 py::object
self = superCls.attr(
"__new__")(cls, otherValue);
553 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_value"));
554 thisClass.attr(
"__new__") = newCf;
559 [isaFunction](MlirValue other) {
return isaFunction(other); },
560 py::arg(
"other_value"));
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
#define MLIR_PYTHON_MAYBE_DOWNCAST_ATTR
Attribute on MLIR Python objects that expose a function for downcasting the corresponding Python obje...
static MlirBlock mlirPythonCapsuleToBlock(PyObject *capsule)
Extracts an MlirBlock from a capsule as produced from mlirPythonBlockToCapsule.
static PyObject * mlirPythonTypeIDToCapsule(MlirTypeID typeID)
Creates a capsule object encapsulating the raw C-API MlirTypeID.
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 MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule)
Extracts an MlirTypeID from a capsule as produced from mlirPythonTypeIDToCapsule.
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.
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
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, const py::object &superCls, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses with a provided mlir.ir.Type super-class.
MlirTypeID(*)() GetTypeIDFunctionTy
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses by looking up the super-class dynamically.
bool(*)(MlirType) IsAFunctionTy
Creates a custom subclass of mlir.ir.Value, implementing a casting constructor and type checking meth...
mlir_value_subclass(py::handle scope, const char *valueClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Value super-class.
mlir_value_subclass(py::handle scope, const char *valueClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
bool(*)(MlirValue) 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 mlirBlockIsNull(MlirBlock block)
Checks whether a block 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.
static bool mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id 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(MlirBlock, _("MlirBlock"))
bool load(handle src, bool)
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"))
PYBIND11_TYPE_CASTER(MlirTypeID, _("MlirTypeID"))
bool load(handle src, bool)
static handle cast(MlirTypeID v, return_value_policy, handle)
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"))