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 =
96 }
catch (python_error &e) {
109struct type_caster<MlirAffineMap> {
120 cleanup_list *cleanup)
noexcept {
122 nanobind::object capsule =
124 return mlir::python::irModule()
134struct type_caster<MlirAttribute> {
145 cleanup_list *cleanup)
noexcept {
147 nanobind::object capsule =
149 return mlir::python::irModule()
160struct type_caster<MlirBlock> {
173struct type_caster<MlirContext> {
179 src = mlir::python::irModule().attr(
"Context").attr(
"current");
185 PyExc_RuntimeWarning,
186 "Passing None as MLIR Context is only allowed inside "
187 "the " MAKE_MLIR_PYTHON_QUALNAME(
"ir.Context")
" context manager.",
201struct type_caster<MlirDialectRegistry> {
211 static handle
from_cpp(MlirDialectRegistry v, rv_policy,
212 cleanup_list *cleanup)
noexcept {
214 nanobind::object capsule = nanobind::steal<nanobind::object>(
216 return mlir::python::irModule()
217 .attr(
"DialectRegistry")
226struct type_caster<MlirLocation> {
232 src = mlir::python::irModule().attr(
"Location").attr(
"current");
241 cleanup_list *cleanup)
noexcept {
243 nanobind::object capsule =
245 return mlir::python::irModule()
255struct type_caster<MlirModule> {
265 cleanup_list *cleanup)
noexcept {
267 nanobind::object capsule =
269 return mlir::python::irModule()
279struct type_caster<MlirFrozenRewritePatternSet> {
281 MlirFrozenRewritePatternSet,
290 static handle
from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
293 nanobind::object capsule = nanobind::steal<nanobind::object>(
296 .attr(
"FrozenRewritePatternSet")
305struct type_caster<MlirOperation> {
316 cleanup_list *cleanup)
noexcept {
317 if (v.ptr ==
nullptr)
318 return nanobind::none();
320 nanobind::object capsule =
322 return mlir::python::irModule()
332struct type_caster<MlirValue> {
342 cleanup_list *cleanup)
noexcept {
343 if (v.ptr ==
nullptr)
344 return nanobind::none();
346 nanobind::object capsule =
348 return mlir::python::irModule()
359struct type_caster<MlirPassManager> {
361 "passmanager.PassManager")))
373struct type_caster<MlirTypeID> {
383 cleanup_list *cleanup)
noexcept {
384 if (v.ptr ==
nullptr)
385 return nanobind::none();
387 nanobind::object capsule =
389 return mlir::python::irModule()
399struct type_caster<MlirType> {
409 cleanup_list *cleanup)
noexcept {
411 nanobind::object capsule =
413 return mlir::python::irModule()
427 cleanup_list *cleanup) noexcept {
429 [&]() -> handle {
return nanobind::str(s.data, s.length).release(); });
455 nanobind::object pyType =
456 nanobind::borrow<nanobind::object>((PyObject *)&PyType_Type);
457 nanobind::object metaclass = pyType(
superClass);
458 nanobind::dict attributes;
462 scope.attr(derivedClassName) =
thisClass;
463 thisClass.attr(
"__module__") = scope.attr(
"__name__");
466 template <
typename Func,
typename... Extra>
468 nanobind::object
cf = nanobind::cpp_function(
469 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
475 template <
typename Func,
typename... Extra>
477 const Extra &...extra) {
478 nanobind::object
cf = nanobind::cpp_function(
479 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
481 auto builtinProperty =
482 nanobind::borrow<nanobind::object>((PyObject *)&PyProperty_Type);
487 template <
typename Func,
typename... Extra>
489 const Extra &...extra) {
490 static_assert(!std::is_member_function_pointer<Func>::value,
491 "def_staticmethod(...) called with a non-static member "
493 nanobind::object
cf = nanobind::cpp_function(
494 std::forward<Func>(f),
495 nanobind::name(name),
501 template <
typename Func,
typename... Extra>
503 const Extra &...extra) {
504 static_assert(!std::is_member_function_pointer<Func>::value,
505 "def_classmethod(...) called with a non-static member "
507 nanobind::object
cf = nanobind::cpp_function(
508 std::forward<Func>(f),
509 nanobind::name(name),
512 return std::make_unique<nanobind::object>(
513 nanobind::module_::import_(
"builtins").attr(
"classmethod"));
538 irModule().attr(
"Attribute"),
539 getTypeIDFunction) {}
547 const nanobind::object &superCls,
558 std::string captureTypeName(
560 nanobind::object newCf = nanobind::cpp_function(
561 [superCls, isaFunction, captureTypeName](
562 nanobind::object cls, nanobind::object otherAttribute) {
563 MlirAttribute rawAttribute;
564 if (!nanobind::try_cast<MlirAttribute>(otherAttribute,
566 !isaFunction(rawAttribute)) {
568 nanobind::cast<std::string>(nanobind::repr(otherAttribute));
570 "Cannot cast attribute to ", captureTypeName,
" (from ",
573 nanobind::object self = superCls.attr(
"__new__")(cls, otherAttribute);
576 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
577 nanobind::arg(
"cast_from_attr"));
581 static const char kIsinstanceSig[] =
583 "ir")
".Attribute) -> bool";
586 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
587 nanobind::arg(
"other_attribute"), nanobind::sig(kIsinstanceSig));
588 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
589 return nanobind::cast<std::string>(
590 nanobind::repr(superCls(self))
591 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
593 if (getTypeIDFunction) {
596 [getTypeIDFunction]() {
return getTypeIDFunction(); },
603 getTypeIDFunction())(nanobind::cpp_function(
623 irModule().attr(
"Type"), getTypeIDFunction) {}
631 const nanobind::object &superCls,
642 std::string captureTypeName(
644 nanobind::object newCf = nanobind::cpp_function(
645 [superCls, isaFunction, captureTypeName](nanobind::object cls,
646 nanobind::object otherType) {
648 if (!nanobind::try_cast<MlirType>(otherType, rawType) ||
649 !isaFunction(rawType)) {
651 nanobind::cast<std::string>(nanobind::repr(otherType));
652 throw std::invalid_argument(
654 " (from ", origRepr,
")"));
656 nanobind::object self = superCls.attr(
"__new__")(cls, otherType);
659 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
660 nanobind::arg(
"cast_from_type"));
664 static const char kIsinstanceSig[] =
670 [isaFunction](MlirType other) {
return isaFunction(other); },
671 nanobind::arg(
"other_type"), nanobind::sig(kIsinstanceSig));
672 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
673 return nanobind::cast<std::string>(
674 nanobind::repr(superCls(self))
675 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
677 if (getTypeIDFunction) {
685 [getTypeIDFunction]() {
return getTypeIDFunction(); },
692 getTypeIDFunction())(nanobind::cpp_function(
710 irModule().attr(
"Value")) {}
718 const nanobind::object &superCls)
728 std::string captureValueName(
730 nanobind::object newCf = nanobind::cpp_function(
731 [superCls, isaFunction, captureValueName](nanobind::object cls,
732 nanobind::object otherValue) {
734 if (!nanobind::try_cast<MlirValue>(otherValue, rawValue) ||
735 !isaFunction(rawValue)) {
737 nanobind::cast<std::string>(nanobind::repr(otherValue));
739 "Cannot cast value to ", captureValueName,
" (from ", origRepr,
742 nanobind::object self = superCls.attr(
"__new__")(cls, otherValue);
745 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
746 nanobind::arg(
"cast_from_value"));
750 static const char kIsinstanceSig[] =
756 [isaFunction](MlirValue other) {
return isaFunction(other); },
757 nanobind::arg(
"other_value"), nanobind::sig(kIsinstanceSig));
if(failed(verifyVectorMemoryOp(getOperation(), memrefType, getVectorType()))) return failure()
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.
static handle mlirFromCppNoThrow(F &&f) noexcept
Invokes f, which creates the Python object for a from_cpp conversion below.
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