19 #ifndef MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
20 #define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
30 #include "llvm/ADT/Twine.h"
47 if (PyCapsule_CheckExact(apiObject.ptr()))
48 return nanobind::borrow<nanobind::object>(apiObject);
49 nanobind::object api =
52 std::string repr = nanobind::cast<std::string>(nanobind::repr(apiObject));
53 throw nanobind::type_error(
54 (llvm::Twine(
"Expected an MLIR object (got ") + repr +
").")
68 struct type_caster<MlirAffineMap> {
69 NB_TYPE_CASTER(MlirAffineMap, const_name(
"MlirAffineMap"))
70 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
78 static handle
from_cpp(MlirAffineMap v, rv_policy,
79 cleanup_list *cleanup) noexcept {
80 nanobind::object capsule =
91 struct type_caster<MlirAttribute> {
92 NB_TYPE_CASTER(MlirAttribute, const_name(
"MlirAttribute"))
93 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
98 static handle
from_cpp(MlirAttribute v, rv_policy,
99 cleanup_list *cleanup) noexcept {
100 nanobind::object capsule =
112 struct type_caster<MlirBlock> {
113 NB_TYPE_CASTER(MlirBlock, const_name(
"MlirBlock"))
114 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
123 struct type_caster<MlirContext> {
124 NB_TYPE_CASTER(MlirContext, const_name(
"MlirContext"))
125 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
143 struct type_caster<MlirDialectRegistry> {
144 NB_TYPE_CASTER(MlirDialectRegistry, const_name(
"MlirDialectRegistry"))
145 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
150 static handle
from_cpp(MlirDialectRegistry v, rv_policy,
151 cleanup_list *cleanup) noexcept {
152 nanobind::object capsule = nanobind::steal<nanobind::object>(
155 .attr(
"DialectRegistry")
163 struct type_caster<MlirLocation> {
164 NB_TYPE_CASTER(MlirLocation, const_name(
"MlirLocation"))
165 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
177 cleanup_list *cleanup) noexcept {
178 nanobind::object capsule =
189 struct type_caster<MlirModule> {
190 NB_TYPE_CASTER(MlirModule, const_name(
"MlirModule"))
191 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
197 cleanup_list *cleanup) noexcept {
198 nanobind::object capsule =
209 struct type_caster<MlirFrozenRewritePatternSet> {
211 const_name(
"MlirFrozenRewritePatternSet"))
212 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
215 return value.ptr !=
nullptr;
217 static handle
from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle) {
218 nanobind::object capsule = nanobind::steal<nanobind::object>(
221 .attr(
"FrozenRewritePatternSet")
229 struct type_caster<MlirOperation> {
230 NB_TYPE_CASTER(MlirOperation, const_name(
"MlirOperation"))
231 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
237 cleanup_list *cleanup) noexcept {
238 if (v.ptr ==
nullptr)
239 return nanobind::none();
240 nanobind::object capsule =
251 struct type_caster<MlirValue> {
252 NB_TYPE_CASTER(MlirValue, const_name(
"MlirValue"))
253 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
259 cleanup_list *cleanup) noexcept {
260 if (v.ptr ==
nullptr)
261 return nanobind::none();
262 nanobind::object capsule =
274 struct type_caster<MlirPassManager> {
275 NB_TYPE_CASTER(MlirPassManager, const_name(
"MlirPassManager"))
276 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
285 struct type_caster<MlirTypeID> {
286 NB_TYPE_CASTER(MlirTypeID, const_name(
"MlirTypeID"))
287 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
293 cleanup_list *cleanup) noexcept {
294 if (v.ptr ==
nullptr)
295 return nanobind::none();
296 nanobind::object capsule =
307 struct type_caster<MlirType> {
308 NB_TYPE_CASTER(MlirType, const_name(
"MlirType"))
309 bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) {
315 cleanup_list *cleanup) noexcept {
316 nanobind::object capsule =
331 cleanup_list *cleanup) noexcept {
332 return nanobind::str(s.data, s.length).release();
341 namespace nanobind_adaptors {
358 nanobind::object pyType =
359 nanobind::borrow<nanobind::object>((PyObject *)&PyType_Type);
360 nanobind::object metaclass = pyType(
superClass);
361 nanobind::dict attributes;
365 scope.attr(derivedClassName) =
thisClass;
366 thisClass.attr(
"__module__") = scope.attr(
"__name__");
369 template <
typename Func,
typename... Extra>
371 nanobind::object cf = nanobind::cpp_function(
372 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
378 template <
typename Func,
typename... Extra>
380 const Extra &...extra) {
381 nanobind::object cf = nanobind::cpp_function(
382 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
384 auto builtinProperty =
385 nanobind::borrow<nanobind::object>((PyObject *)&PyProperty_Type);
386 thisClass.attr(name) = builtinProperty(cf);
390 template <
typename Func,
typename... Extra>
392 const Extra &...extra) {
393 static_assert(!std::is_member_function_pointer<Func>::value,
394 "def_staticmethod(...) called with a non-static member "
396 nanobind::object cf = nanobind::cpp_function(
397 std::forward<Func>(f),
398 nanobind::name(name),
404 template <
typename Func,
typename... Extra>
406 const Extra &...extra) {
407 static_assert(!std::is_member_function_pointer<Func>::value,
408 "def_classmethod(...) called with a non-static member "
410 nanobind::object cf = nanobind::cpp_function(
411 std::forward<Func>(f),
412 nanobind::name(name),
415 nanobind::borrow<nanobind::object>(PyClassMethod_New(cf.ptr()));
438 scope, attrClassName, isaFunction,
441 getTypeIDFunction) {}
449 const nanobind::object &superCls,
460 std::string captureTypeName(
462 nanobind::object newCf = nanobind::cpp_function(
463 [superCls, isaFunction, captureTypeName](
464 nanobind::object cls, nanobind::object otherAttribute) {
465 MlirAttribute rawAttribute =
466 nanobind::cast<MlirAttribute>(otherAttribute);
467 if (!isaFunction(rawAttribute)) {
469 nanobind::cast<std::string>(nanobind::repr(otherAttribute));
470 throw std::invalid_argument(
471 (llvm::Twine(
"Cannot cast attribute to ") + captureTypeName +
472 " (from " + origRepr +
")")
475 nanobind::object
self = superCls.attr(
"__new__")(cls, otherAttribute);
478 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
479 nanobind::arg(
"cast_from_attr"));
483 static const char kIsinstanceSig[] =
485 "ir")
".Attribute) -> bool";
488 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
489 nanobind::arg(
"other_attribute"), nanobind::sig(kIsinstanceSig));
490 def(
"__repr__", [superCls, captureTypeName](nanobind::object
self) {
491 return nanobind::repr(superCls(
self))
492 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName);
494 if (getTypeIDFunction) {
496 [getTypeIDFunction]() {
return getTypeIDFunction(); });
499 getTypeIDFunction())(nanobind::cpp_function(
519 scope, typeClassName, isaFunction,
522 getTypeIDFunction) {}
530 const nanobind::object &superCls,
541 std::string captureTypeName(
543 nanobind::object newCf = nanobind::cpp_function(
544 [superCls, isaFunction, captureTypeName](nanobind::object cls,
545 nanobind::object otherType) {
546 MlirType rawType = nanobind::cast<MlirType>(otherType);
547 if (!isaFunction(rawType)) {
549 nanobind::cast<std::string>(nanobind::repr(otherType));
550 throw std::invalid_argument((llvm::Twine(
"Cannot cast type to ") +
551 captureTypeName +
" (from " +
555 nanobind::object
self = superCls.attr(
"__new__")(cls, otherType);
558 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
559 nanobind::arg(
"cast_from_type"));
563 static const char kIsinstanceSig[] =
565 "ir")
".Type) -> bool";
568 [isaFunction](MlirType other) {
return isaFunction(other); },
569 nanobind::arg(
"other_type"), nanobind::sig(kIsinstanceSig));
570 def(
"__repr__", [superCls, captureTypeName](nanobind::object
self) {
571 return nanobind::cast<std::string>(
572 nanobind::repr(superCls(
self))
573 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
575 if (getTypeIDFunction) {
582 [getTypeIDFunction]() {
return getTypeIDFunction(); });
585 getTypeIDFunction())(nanobind::cpp_function(
603 scope, valueClassName, isaFunction,
613 const nanobind::object &superCls)
623 std::string captureValueName(
625 nanobind::object newCf = nanobind::cpp_function(
626 [superCls, isaFunction, captureValueName](nanobind::object cls,
627 nanobind::object otherValue) {
628 MlirValue rawValue = nanobind::cast<MlirValue>(otherValue);
629 if (!isaFunction(rawValue)) {
631 nanobind::cast<std::string>(nanobind::repr(otherValue));
632 throw std::invalid_argument((llvm::Twine(
"Cannot cast value to ") +
633 captureValueName +
" (from " +
637 nanobind::object
self = superCls.attr(
"__new__")(cls, otherValue);
640 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
641 nanobind::arg(
"cast_from_value"));
645 static const char kIsinstanceSig[] =
647 "ir")
".Value) -> bool";
650 [isaFunction](MlirValue other) {
return isaFunction(other); },
651 nanobind::arg(
"other_value"), nanobind::sig(kIsinstanceSig));
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...
A pointer to a sized fragment of a string, not necessarily null-terminated.
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