19 #ifndef MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
20 #define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
28 #include "llvm/ADT/Twine.h"
45 if (PyCapsule_CheckExact(apiObject.ptr()))
46 return nanobind::borrow<nanobind::object>(apiObject);
48 std::string repr = nanobind::cast<std::string>(nanobind::repr(apiObject));
49 throw nanobind::type_error(
50 (llvm::Twine(
"Expected an MLIR object (got ") + repr +
").")
64 struct type_caster<MlirAffineMap> {
65 NB_TYPE_CASTER(MlirAffineMap, const_name(
"MlirAffineMap"))
66 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
74 static handle
from_cpp(MlirAffineMap v, rv_policy,
75 cleanup_list *cleanup) noexcept {
76 nanobind::object capsule =
87 struct type_caster<MlirAttribute> {
88 NB_TYPE_CASTER(MlirAttribute, const_name(
"MlirAttribute"))
89 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
94 static handle
from_cpp(MlirAttribute v, rv_policy,
95 cleanup_list *cleanup) noexcept {
96 nanobind::object capsule =
108 struct type_caster<MlirBlock> {
109 NB_TYPE_CASTER(MlirBlock, const_name(
"MlirBlock"))
110 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
119 struct type_caster<MlirContext> {
120 NB_TYPE_CASTER(MlirContext, const_name(
"MlirContext"))
121 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
139 struct type_caster<MlirDialectRegistry> {
140 NB_TYPE_CASTER(MlirDialectRegistry, const_name(
"MlirDialectRegistry"))
141 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
146 static handle
from_cpp(MlirDialectRegistry v, rv_policy,
147 cleanup_list *cleanup) noexcept {
148 nanobind::object capsule = nanobind::steal<nanobind::object>(
151 .attr(
"DialectRegistry")
159 struct type_caster<MlirLocation> {
160 NB_TYPE_CASTER(MlirLocation, const_name(
"MlirLocation"))
161 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
173 cleanup_list *cleanup) noexcept {
174 nanobind::object capsule =
185 struct type_caster<MlirModule> {
186 NB_TYPE_CASTER(MlirModule, const_name(
"MlirModule"))
187 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
193 cleanup_list *cleanup) noexcept {
194 nanobind::object capsule =
205 struct type_caster<MlirFrozenRewritePatternSet> {
207 const_name(
"MlirFrozenRewritePatternSet"))
208 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
211 return value.ptr !=
nullptr;
213 static handle
from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle) {
214 nanobind::object capsule = nanobind::steal<nanobind::object>(
217 .attr(
"FrozenRewritePatternSet")
225 struct type_caster<MlirOperation> {
226 NB_TYPE_CASTER(MlirOperation, const_name(
"MlirOperation"))
227 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
233 cleanup_list *cleanup) noexcept {
234 if (v.ptr ==
nullptr)
235 return nanobind::none();
236 nanobind::object capsule =
247 struct type_caster<MlirValue> {
248 NB_TYPE_CASTER(MlirValue, const_name(
"MlirValue"))
249 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
255 cleanup_list *cleanup) noexcept {
256 if (v.ptr ==
nullptr)
257 return nanobind::none();
258 nanobind::object capsule =
270 struct type_caster<MlirPassManager> {
271 NB_TYPE_CASTER(MlirPassManager, const_name(
"MlirPassManager"))
272 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
281 struct type_caster<MlirTypeID> {
282 NB_TYPE_CASTER(MlirTypeID, const_name(
"MlirTypeID"))
283 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
289 cleanup_list *cleanup) noexcept {
290 if (v.ptr ==
nullptr)
291 return nanobind::none();
292 nanobind::object capsule =
303 struct type_caster<MlirType> {
304 NB_TYPE_CASTER(MlirType, const_name(
"MlirType"))
305 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
311 cleanup_list *cleanup) noexcept {
312 nanobind::object capsule =
327 namespace nanobind_adaptors {
344 nanobind::object pyType =
345 nanobind::borrow<nanobind::object>((PyObject *)&PyType_Type);
346 nanobind::object metaclass = pyType(
superClass);
347 nanobind::dict attributes;
351 scope.attr(derivedClassName) =
thisClass;
354 template <
typename Func,
typename... Extra>
356 nanobind::object cf = nanobind::cpp_function(
357 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
363 template <
typename Func,
typename... Extra>
365 const Extra &...extra) {
366 nanobind::object cf = nanobind::cpp_function(
367 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
369 auto builtinProperty =
370 nanobind::borrow<nanobind::object>((PyObject *)&PyProperty_Type);
371 thisClass.attr(name) = builtinProperty(cf);
375 template <
typename Func,
typename... Extra>
377 const Extra &...extra) {
378 static_assert(!std::is_member_function_pointer<Func>::value,
379 "def_staticmethod(...) called with a non-static member "
381 nanobind::object cf = nanobind::cpp_function(
382 std::forward<Func>(f),
383 nanobind::name(name),
389 template <
typename Func,
typename... Extra>
391 const Extra &...extra) {
392 static_assert(!std::is_member_function_pointer<Func>::value,
393 "def_classmethod(...) called with a non-static member "
395 nanobind::object cf = nanobind::cpp_function(
396 std::forward<Func>(f),
397 nanobind::name(name),
400 nanobind::borrow<nanobind::object>(PyClassMethod_New(cf.ptr()));
423 scope, attrClassName, isaFunction,
426 getTypeIDFunction) {}
434 const nanobind::object &superCls,
445 std::string captureTypeName(
447 nanobind::object newCf = nanobind::cpp_function(
448 [superCls, isaFunction, captureTypeName](
449 nanobind::object cls, nanobind::object otherAttribute) {
450 MlirAttribute rawAttribute =
451 nanobind::cast<MlirAttribute>(otherAttribute);
452 if (!isaFunction(rawAttribute)) {
454 nanobind::cast<std::string>(nanobind::repr(otherAttribute));
455 throw std::invalid_argument(
456 (llvm::Twine(
"Cannot cast attribute to ") + captureTypeName +
457 " (from " + origRepr +
")")
460 nanobind::object
self = superCls.attr(
"__new__")(cls, otherAttribute);
463 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
464 nanobind::arg(
"cast_from_attr"));
470 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
471 nanobind::arg(
"other_attribute"));
472 def(
"__repr__", [superCls, captureTypeName](nanobind::object
self) {
473 return nanobind::repr(superCls(
self))
474 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName);
476 if (getTypeIDFunction) {
478 [getTypeIDFunction]() {
return getTypeIDFunction(); });
481 getTypeIDFunction())(nanobind::cpp_function(
501 scope, typeClassName, isaFunction,
504 getTypeIDFunction) {}
512 const nanobind::object &superCls,
523 std::string captureTypeName(
525 nanobind::object newCf = nanobind::cpp_function(
526 [superCls, isaFunction, captureTypeName](nanobind::object cls,
527 nanobind::object otherType) {
528 MlirType rawType = nanobind::cast<MlirType>(otherType);
529 if (!isaFunction(rawType)) {
531 nanobind::cast<std::string>(nanobind::repr(otherType));
532 throw std::invalid_argument((llvm::Twine(
"Cannot cast type to ") +
533 captureTypeName +
" (from " +
537 nanobind::object
self = superCls.attr(
"__new__")(cls, otherType);
540 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
541 nanobind::arg(
"cast_from_type"));
547 [isaFunction](MlirType other) {
return isaFunction(other); },
548 nanobind::arg(
"other_type"));
549 def(
"__repr__", [superCls, captureTypeName](nanobind::object
self) {
550 return nanobind::repr(superCls(
self))
551 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName);
553 if (getTypeIDFunction) {
560 [getTypeIDFunction]() {
return getTypeIDFunction(); });
563 getTypeIDFunction())(nanobind::cpp_function(
581 scope, valueClassName, isaFunction,
591 const nanobind::object &superCls)
601 std::string captureValueName(
603 nanobind::object newCf = nanobind::cpp_function(
604 [superCls, isaFunction, captureValueName](nanobind::object cls,
605 nanobind::object otherValue) {
606 MlirValue rawValue = nanobind::cast<MlirValue>(otherValue);
607 if (!isaFunction(rawValue)) {
609 nanobind::cast<std::string>(nanobind::repr(otherValue));
610 throw std::invalid_argument((llvm::Twine(
"Cannot cast value to ") +
611 captureValueName +
" (from " +
615 nanobind::object
self = superCls.attr(
"__new__")(cls, otherValue);
618 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
619 nanobind::arg(
"cast_from_value"));
625 [isaFunction](MlirValue other) {
return isaFunction(other); },
626 nanobind::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.
static MlirFrozenRewritePatternSet mlirPythonCapsuleToFrozenRewritePatternSet(PyObject *capsule)
Extracts an MlirFrozenRewritePatternSet from a capsule as produced from mlirPythonFrozenRewritePatter...
#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 PyObject * mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm)
Creates a capsule object encapsulating the raw C-API MlirFrozenRewritePatternSet.
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(nanobind::handle scope, const char *attrClassName, IsAFunctionTy isaFunction, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses by looking up the super-class dynamically.
MlirTypeID(*)() GetTypeIDFunctionTy
mlir_attribute_subclass(nanobind::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const nanobind::object &superCls, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses with a provided mlir.ir.Attribute super-class.
Creates a custom subclass of mlir.ir.Type, implementing a casting constructor and type checking metho...
mlir_type_subclass(nanobind::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const nanobind::object &superCls, GetTypeIDFunctionTy getTypeIDFunction=nullptr)
Subclasses with a provided mlir.ir.Type super-class.
MlirTypeID(*)() GetTypeIDFunctionTy
mlir_type_subclass(nanobind::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(nanobind::handle scope, const char *valueClassName, IsAFunctionTy isaFunction, const nanobind::object &superCls)
Subclasses with a provided mlir.ir.Value super-class.
bool(*)(MlirValue) IsAFunctionTy
mlir_value_subclass(nanobind::handle scope, const char *valueClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
Provides a facility like nanobind::class_ for defining a new class in a scope, but this allows extens...
pure_subclass & def(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def_classmethod(const char *name, Func &&f, const Extra &...extra)
pure_subclass(nanobind::handle scope, const char *derivedClassName, const nanobind::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)
nanobind::object thisClass
nanobind::object get_class() const
nanobind::object superClass
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 nanobind::object mlirApiObjectToCapsule(nanobind::handle apiObject)
Helper to convert a presumed MLIR API object to a capsule, accepting either an explicit Capsule (whic...
static handle from_cpp(MlirAffineMap v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirAttribute v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirDialectRegistry v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirFrozenRewritePatternSet, const_name("MlirFrozenRewritePatternSet")) bool from_python(handle src
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle)
static handle from_cpp(MlirLocation v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirModule v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirOperation v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirTypeID v, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirType t, rv_policy, cleanup_list *cleanup) noexcept
static handle from_cpp(MlirValue v, rv_policy, cleanup_list *cleanup) noexcept