19#ifndef MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
20#define MLIR_BINDINGS_PYTHON_NANOBINDADAPTORS_H
38nanobind::module_ &irModule() {
40 return std::make_unique<nanobind::module_>(
64static std::optional<nanobind::object>
66 if (PyCapsule_CheckExact(apiObject.ptr()))
67 return nanobind::borrow<nanobind::object>(apiObject);
68 nanobind::object api =
93struct type_caster<MlirAffineMap> {
104 cleanup_list *cleanup)
noexcept {
105 nanobind::object capsule =
107 return mlir::python::irModule()
116struct type_caster<MlirAttribute> {
127 cleanup_list *cleanup)
noexcept {
128 nanobind::object capsule =
130 return mlir::python::irModule()
140struct type_caster<MlirBlock> {
153struct type_caster<MlirContext> {
159 src = mlir::python::irModule().attr(
"Context").attr(
"current");
165 PyExc_RuntimeWarning,
166 "Passing None as MLIR Context is only allowed inside "
167 "the " MAKE_MLIR_PYTHON_QUALNAME(
"ir.Context")
" context manager.",
181struct type_caster<MlirDialectRegistry> {
191 static handle
from_cpp(MlirDialectRegistry v, rv_policy,
192 cleanup_list *cleanup)
noexcept {
193 nanobind::object capsule = nanobind::steal<nanobind::object>(
195 return mlir::python::irModule()
196 .attr(
"DialectRegistry")
204struct type_caster<MlirLocation> {
210 src = mlir::python::irModule().attr(
"Location").attr(
"current");
219 cleanup_list *cleanup)
noexcept {
220 nanobind::object capsule =
222 return mlir::python::irModule()
231struct type_caster<MlirModule> {
241 cleanup_list *cleanup)
noexcept {
242 nanobind::object capsule =
244 return mlir::python::irModule()
253struct type_caster<MlirFrozenRewritePatternSet> {
255 MlirFrozenRewritePatternSet,
264 static handle
from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
266 nanobind::object capsule = nanobind::steal<nanobind::object>(
269 .attr(
"FrozenRewritePatternSet")
277struct type_caster<MlirOperation> {
288 cleanup_list *cleanup)
noexcept {
289 if (v.ptr ==
nullptr)
290 return nanobind::none();
291 nanobind::object capsule =
293 return mlir::python::irModule()
302struct type_caster<MlirValue> {
312 cleanup_list *cleanup)
noexcept {
313 if (v.ptr ==
nullptr)
314 return nanobind::none();
315 nanobind::object capsule =
317 return mlir::python::irModule()
327struct type_caster<MlirPassManager> {
329 "passmanager.PassManager")))
341struct type_caster<MlirTypeID> {
351 cleanup_list *cleanup)
noexcept {
352 if (v.ptr ==
nullptr)
353 return nanobind::none();
354 nanobind::object capsule =
356 return mlir::python::irModule()
365struct type_caster<MlirType> {
375 cleanup_list *cleanup)
noexcept {
376 nanobind::object capsule =
378 return mlir::python::irModule()
391 cleanup_list *cleanup) noexcept {
392 return nanobind::str(s.data, s.length).release();
418 nanobind::object pyType =
419 nanobind::borrow<nanobind::object>((PyObject *)&PyType_Type);
420 nanobind::object metaclass = pyType(
superClass);
421 nanobind::dict attributes;
425 scope.attr(derivedClassName) =
thisClass;
426 thisClass.attr(
"__module__") = scope.attr(
"__name__");
429 template <
typename Func,
typename... Extra>
431 nanobind::object
cf = nanobind::cpp_function(
432 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
438 template <
typename Func,
typename... Extra>
440 const Extra &...extra) {
441 nanobind::object
cf = nanobind::cpp_function(
442 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
444 auto builtinProperty =
445 nanobind::borrow<nanobind::object>((PyObject *)&PyProperty_Type);
450 template <
typename Func,
typename... Extra>
452 const Extra &...extra) {
453 static_assert(!std::is_member_function_pointer<Func>::value,
454 "def_staticmethod(...) called with a non-static member "
456 nanobind::object
cf = nanobind::cpp_function(
457 std::forward<Func>(f),
458 nanobind::name(name),
464 template <
typename Func,
typename... Extra>
466 const Extra &...extra) {
467 static_assert(!std::is_member_function_pointer<Func>::value,
468 "def_classmethod(...) called with a non-static member "
470 nanobind::object
cf = nanobind::cpp_function(
471 std::forward<Func>(f),
472 nanobind::name(name),
475 return std::make_unique<nanobind::object>(
476 nanobind::module_::import_(
"builtins").attr(
"classmethod"));
501 irModule().attr(
"Attribute"),
502 getTypeIDFunction) {}
510 const nanobind::object &superCls,
521 std::string captureTypeName(
523 nanobind::object newCf = nanobind::cpp_function(
524 [superCls, isaFunction, captureTypeName](
525 nanobind::object cls, nanobind::object otherAttribute) {
526 MlirAttribute rawAttribute;
527 if (!nanobind::try_cast<MlirAttribute>(otherAttribute,
529 !isaFunction(rawAttribute)) {
531 nanobind::cast<std::string>(nanobind::repr(otherAttribute));
533 "Cannot cast attribute to ", captureTypeName,
" (from ",
536 nanobind::object self = superCls.attr(
"__new__")(cls, otherAttribute);
539 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
540 nanobind::arg(
"cast_from_attr"));
544 static const char kIsinstanceSig[] =
546 "ir")
".Attribute) -> bool";
549 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
550 nanobind::arg(
"other_attribute"), nanobind::sig(kIsinstanceSig));
551 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
552 return nanobind::cast<std::string>(
553 nanobind::repr(superCls(self))
554 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
556 if (getTypeIDFunction) {
559 [getTypeIDFunction]() {
return getTypeIDFunction(); },
566 getTypeIDFunction())(nanobind::cpp_function(
586 irModule().attr(
"Type"), getTypeIDFunction) {}
594 const nanobind::object &superCls,
605 std::string captureTypeName(
607 nanobind::object newCf = nanobind::cpp_function(
608 [superCls, isaFunction, captureTypeName](nanobind::object cls,
609 nanobind::object otherType) {
611 if (!nanobind::try_cast<MlirType>(otherType, rawType) ||
612 !isaFunction(rawType)) {
614 nanobind::cast<std::string>(nanobind::repr(otherType));
615 throw std::invalid_argument(
617 " (from ", origRepr,
")"));
619 nanobind::object self = superCls.attr(
"__new__")(cls, otherType);
622 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
623 nanobind::arg(
"cast_from_type"));
627 static const char kIsinstanceSig[] =
633 [isaFunction](MlirType other) {
return isaFunction(other); },
634 nanobind::arg(
"other_type"), nanobind::sig(kIsinstanceSig));
635 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
636 return nanobind::cast<std::string>(
637 nanobind::repr(superCls(self))
638 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
640 if (getTypeIDFunction) {
648 [getTypeIDFunction]() {
return getTypeIDFunction(); },
655 getTypeIDFunction())(nanobind::cpp_function(
673 irModule().attr(
"Value")) {}
681 const nanobind::object &superCls)
691 std::string captureValueName(
693 nanobind::object newCf = nanobind::cpp_function(
694 [superCls, isaFunction, captureValueName](nanobind::object cls,
695 nanobind::object otherValue) {
697 if (!nanobind::try_cast<MlirValue>(otherValue, rawValue) ||
698 !isaFunction(rawValue)) {
700 nanobind::cast<std::string>(nanobind::repr(otherValue));
702 "Cannot cast value to ", captureValueName,
" (from ", origRepr,
705 nanobind::object self = superCls.attr(
"__new__")(cls, otherValue);
708 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
709 nanobind::arg(
"cast_from_value"));
713 static const char kIsinstanceSig[] =
719 [isaFunction](MlirValue other) {
return isaFunction(other); },
720 nanobind::arg(
"other_value"), nanobind::sig(kIsinstanceSig));
static PyObject * mlirPythonTypeIDToCapsule(MlirTypeID typeID)
Creates a capsule object encapsulating the raw C-API MlirTypeID.
#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 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 * mlirPythonTypeToCapsule(MlirType type)
Creates a capsule object encapsulating the raw C-API MlirType.
static MlirAffineMap mlirPythonCapsuleToAffineMap(PyObject *capsule)
Extracts an MlirAffineMap from a capsule as produced from mlirPythonAffineMapToCapsule.
static PyObject * mlirPythonOperationToCapsule(MlirOperation operation)
Creates a capsule object encapsulating the raw C-API MlirOperation.
static PyObject * mlirPythonAttributeToCapsule(MlirAttribute attribute)
Creates a capsule object encapsulating the raw C-API MlirAttribute.
#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 * mlirPythonAffineMapToCapsule(MlirAffineMap affineMap)
Creates a capsule object encapsulating the raw C-API MlirAffineMap.
static PyObject * mlirPythonLocationToCapsule(MlirLocation loc)
Creates a capsule object encapsulating the raw C-API MlirLocation.
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 * mlirPythonValueToCapsule(MlirValue value)
Creates a capsule object encapsulating the raw C-API MlirValue.
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
static PyObject * mlirPythonFrozenRewritePatternSetToCapsule(MlirFrozenRewritePatternSet pm)
Creates a capsule object encapsulating the raw C-API MlirFrozenRewritePatternSet.
static MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule)
Extracts an MlirLocation from a capsule as produced from mlirPythonLocationToCapsule.
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
static PyObject * mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry)
Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
Safely calls Python initialization code on first use, avoiding deadlocks.
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.
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
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.
pure_subclass & def(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def_property_readonly(const char *name, Func &&f, const Extra &...extra)
pure_subclass(nanobind::handle scope, const char *derivedClassName, const nanobind::object &superClass)
pure_subclass & def_classmethod(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 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 mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id is null.
Include the generated interface declarations.
bool pyErrClearIfFalse(bool val)
Clears the Python error indicator if the given condition val is false and returns the condition.
std::string join(const Ts &...args)
Helper function to concatenate arguments into a std::string.
static std::optional< 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
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirAffineMap, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.AffineMap"))) bool from_python(handle src
static handle from_cpp(MlirAttribute v, rv_policy, cleanup_list *cleanup) noexcept
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirAttribute, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Attribute"))) bool from_python(handle src
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirBlock, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Block"))) bool from_python(handle src
NB_TYPE_CASTER(MlirContext, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Context"))) bool from_python(handle src
uint8_t cleanup_list *cleanup noexcept
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirDialectRegistry, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.DialectRegistry"))) bool from_python(handle src
static handle from_cpp(MlirDialectRegistry v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirFrozenRewritePatternSet, const_name(MAKE_MLIR_PYTHON_QUALNAME("rewrite.FrozenRewritePatternSet"))) bool from_python(handle src
static handle from_cpp(MlirFrozenRewritePatternSet v, rv_policy, handle) noexcept
uint8_t cleanup_list *cleanup noexcept
static handle from_cpp(MlirLocation v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirLocation, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Location"))) bool from_python(handle src
uint8_t cleanup_list *cleanup noexcept
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirModule, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Module"))) bool from_python(handle src
static handle from_cpp(MlirModule v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirOperation, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Operation"))) bool from_python(handle src
uint8_t cleanup_list *cleanup noexcept
static handle from_cpp(MlirOperation v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirPassManager, const_name(MAKE_MLIR_PYTHON_QUALNAME("passmanager.PassManager"))) bool from_python(handle src
uint8_t cleanup_list *cleanup noexcept
static handle from_cpp(MlirStringRef s, rv_policy, cleanup_list *cleanup) noexcept
uint8_t cleanup_list *cleanup noexcept
static handle from_cpp(MlirTypeID v, rv_policy, cleanup_list *cleanup) noexcept
NB_TYPE_CASTER(MlirTypeID, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID"))) bool from_python(handle src
NB_TYPE_CASTER(MlirType, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Type"))) bool from_python(handle src
static handle from_cpp(MlirType t, rv_policy, cleanup_list *cleanup) noexcept
uint8_t cleanup_list *cleanup noexcept
uint8_t cleanup_list *cleanup noexcept
NB_TYPE_CASTER(MlirValue, const_name(MAKE_MLIR_PYTHON_QUALNAME("ir.Value"))) bool from_python(handle src
static handle from_cpp(MlirValue v, rv_policy, cleanup_list *cleanup) noexcept