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 =
82struct type_caster<MlirAffineMap> {
92 static handle
from_cpp(MlirAffineMap v, rv_policy,
93 cleanup_list *cleanup)
noexcept {
94 nanobind::object capsule =
96 return mlir::python::irModule()
105struct type_caster<MlirAttribute> {
111 return !mlirAttributeIsNull(value);
116 cleanup_list *cleanup)
noexcept {
117 nanobind::object capsule =
119 return mlir::python::irModule()
129struct type_caster<MlirBlock> {
142struct type_caster<MlirContext> {
148 src = mlir::python::irModule().attr(
"Context").attr(
"current");
154 PyExc_RuntimeWarning,
155 "Passing None as MLIR Context is only allowed inside "
156 "the " MAKE_MLIR_PYTHON_QUALNAME(
"ir.Context")
" context manager.",
170struct type_caster<MlirDialectRegistry> {
180 static handle
from_cpp(MlirDialectRegistry v, rv_policy,
181 cleanup_list *cleanup)
noexcept {
182 nanobind::object capsule = nanobind::steal<nanobind::object>(
184 return mlir::python::irModule()
185 .attr(
"DialectRegistry")
193struct type_caster<MlirLocation> {
199 src = mlir::python::irModule().attr(
"Location").attr(
"current");
208 cleanup_list *cleanup)
noexcept {
209 nanobind::object capsule =
211 return mlir::python::irModule()
220struct type_caster<MlirModule> {
225 return !mlirModuleIsNull(value);
230 cleanup_list *cleanup)
noexcept {
231 nanobind::object capsule =
233 return mlir::python::irModule()
242struct type_caster<MlirFrozenRewritePatternSet> {
244 MlirFrozenRewritePatternSet,
249 return value.ptr !=
nullptr;
253 static handle
from_cpp(MlirFrozenRewritePatternSet v, rv_policy,
255 nanobind::object capsule = nanobind::steal<nanobind::object>(
258 .attr(
"FrozenRewritePatternSet")
266struct type_caster<MlirOperation> {
272 return !mlirOperationIsNull(value);
277 cleanup_list *cleanup)
noexcept {
278 if (v.ptr ==
nullptr)
279 return nanobind::none();
280 nanobind::object capsule =
282 return mlir::python::irModule()
291struct type_caster<MlirValue> {
296 return !mlirValueIsNull(value);
301 cleanup_list *cleanup)
noexcept {
302 if (v.ptr ==
nullptr)
303 return nanobind::none();
304 nanobind::object capsule =
306 return mlir::python::irModule()
316struct type_caster<MlirPassManager> {
318 "passmanager.PassManager")))
330struct type_caster<MlirTypeID> {
340 cleanup_list *cleanup)
noexcept {
341 if (v.ptr ==
nullptr)
342 return nanobind::none();
343 nanobind::object capsule =
345 return mlir::python::irModule()
354struct type_caster<MlirType> {
364 cleanup_list *cleanup)
noexcept {
365 nanobind::object capsule =
367 return mlir::python::irModule()
380 cleanup_list *cleanup) noexcept {
381 return nanobind::str(s.data, s.length).release();
407 nanobind::object pyType =
408 nanobind::borrow<nanobind::object>((PyObject *)&PyType_Type);
409 nanobind::object metaclass = pyType(
superClass);
410 nanobind::dict attributes;
414 scope.attr(derivedClassName) =
thisClass;
415 thisClass.attr(
"__module__") = scope.attr(
"__name__");
418 template <
typename Func,
typename... Extra>
420 nanobind::object
cf = nanobind::cpp_function(
421 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
427 template <
typename Func,
typename... Extra>
429 const Extra &...extra) {
430 nanobind::object
cf = nanobind::cpp_function(
431 std::forward<Func>(f), nanobind::name(name), nanobind::is_method(),
433 auto builtinProperty =
434 nanobind::borrow<nanobind::object>((PyObject *)&PyProperty_Type);
439 template <
typename Func,
typename... Extra>
441 const Extra &...extra) {
442 static_assert(!std::is_member_function_pointer<Func>::value,
443 "def_staticmethod(...) called with a non-static member "
445 nanobind::object
cf = nanobind::cpp_function(
446 std::forward<Func>(f),
447 nanobind::name(name),
453 template <
typename Func,
typename... Extra>
455 const Extra &...extra) {
456 static_assert(!std::is_member_function_pointer<Func>::value,
457 "def_classmethod(...) called with a non-static member "
459 nanobind::object
cf = nanobind::cpp_function(
460 std::forward<Func>(f),
461 nanobind::name(name),
464 return std::make_unique<nanobind::object>(
465 nanobind::module_::import_(
"builtins").attr(
"classmethod"));
490 irModule().attr(
"Attribute"),
491 getTypeIDFunction) {}
499 const nanobind::object &superCls,
510 std::string captureTypeName(
512 nanobind::object newCf = nanobind::cpp_function(
513 [superCls, isaFunction, captureTypeName](
514 nanobind::object cls, nanobind::object otherAttribute) {
515 MlirAttribute rawAttribute;
516 if (!nanobind::try_cast<MlirAttribute>(otherAttribute,
518 !isaFunction(rawAttribute)) {
520 nanobind::cast<std::string>(nanobind::repr(otherAttribute));
522 "Cannot cast attribute to ", captureTypeName,
" (from ",
525 nanobind::object self = superCls.attr(
"__new__")(cls, otherAttribute);
528 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
529 nanobind::arg(
"cast_from_attr"));
533 static const char kIsinstanceSig[] =
535 "ir")
".Attribute) -> bool";
538 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
539 nanobind::arg(
"other_attribute"), nanobind::sig(kIsinstanceSig));
540 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
541 return nanobind::cast<std::string>(
542 nanobind::repr(superCls(self))
543 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
545 if (getTypeIDFunction) {
548 [getTypeIDFunction]() {
return getTypeIDFunction(); },
555 getTypeIDFunction())(nanobind::cpp_function(
575 irModule().attr(
"Type"), getTypeIDFunction) {}
583 const nanobind::object &superCls,
594 std::string captureTypeName(
596 nanobind::object newCf = nanobind::cpp_function(
597 [superCls, isaFunction, captureTypeName](nanobind::object cls,
598 nanobind::object otherType) {
600 if (!nanobind::try_cast<MlirType>(otherType, rawType) ||
601 !isaFunction(rawType)) {
603 nanobind::cast<std::string>(nanobind::repr(otherType));
604 throw std::invalid_argument(
606 " (from ", origRepr,
")"));
608 nanobind::object self = superCls.attr(
"__new__")(cls, otherType);
611 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
612 nanobind::arg(
"cast_from_type"));
616 static const char kIsinstanceSig[] =
622 [isaFunction](MlirType other) {
return isaFunction(other); },
623 nanobind::arg(
"other_type"), nanobind::sig(kIsinstanceSig));
624 def(
"__repr__", [superCls, captureTypeName](nanobind::object self) {
625 return nanobind::cast<std::string>(
626 nanobind::repr(superCls(self))
627 .attr(
"replace")(superCls.attr(
"__name__"), captureTypeName));
629 if (getTypeIDFunction) {
637 [getTypeIDFunction]() {
return getTypeIDFunction(); },
644 getTypeIDFunction())(nanobind::cpp_function(
662 irModule().attr(
"Value")) {}
670 const nanobind::object &superCls)
680 std::string captureValueName(
682 nanobind::object newCf = nanobind::cpp_function(
683 [superCls, isaFunction, captureValueName](nanobind::object cls,
684 nanobind::object otherValue) {
686 if (!nanobind::try_cast<MlirValue>(otherValue, rawValue) ||
687 !isaFunction(rawValue)) {
689 nanobind::cast<std::string>(nanobind::repr(otherValue));
691 "Cannot cast value to ", captureValueName,
" (from ", origRepr,
694 nanobind::object self = superCls.attr(
"__new__")(cls, otherValue);
697 nanobind::name(
"__new__"), nanobind::arg(
"cls"),
698 nanobind::arg(
"cast_from_value"));
702 static const char kIsinstanceSig[] =
708 [isaFunction](MlirValue other) {
return isaFunction(other); },
709 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.
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