18 #ifndef MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H 19 #define MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H 21 #include <pybind11/pybind11.h> 22 #include <pybind11/pytypes.h> 23 #include <pybind11/stl.h> 28 #include "llvm/ADT/Optional.h" 29 #include "llvm/ADT/Twine.h" 51 if (PyCapsule_CheckExact(apiObject.ptr()))
52 return py::reinterpret_borrow<py::object>(apiObject);
54 auto repr = py::repr(apiObject).cast<std::string>();
56 (llvm::Twine(
"Expected an MLIR object (got ") + repr +
").").str());
68 struct type_caster<MlirAffineMap> {
69 PYBIND11_TYPE_CASTER(MlirAffineMap, _(
"MlirAffineMap"));
70 bool load(handle src,
bool) {
78 static handle
cast(MlirAffineMap v, return_value_policy, handle) {
90 struct type_caster<MlirAttribute> {
91 PYBIND11_TYPE_CASTER(MlirAttribute, _(
"MlirAttribute"));
92 bool load(handle src,
bool) {
97 static handle
cast(MlirAttribute v, return_value_policy, handle) {
109 struct type_caster<MlirContext> {
110 PYBIND11_TYPE_CASTER(MlirContext, _(
"MlirContext"));
129 struct type_caster<MlirLocation> {
130 PYBIND11_TYPE_CASTER(MlirLocation, _(
"MlirLocation"));
142 static handle
cast(MlirLocation v, return_value_policy, handle) {
154 struct type_caster<MlirModule> {
155 PYBIND11_TYPE_CASTER(MlirModule, _(
"MlirModule"));
161 static handle
cast(MlirModule v, return_value_policy, handle) {
173 struct type_caster<MlirOperation> {
174 PYBIND11_TYPE_CASTER(MlirOperation, _(
"MlirOperation"));
180 static handle
cast(MlirOperation v, return_value_policy, handle) {
181 if (v.ptr ==
nullptr)
194 struct type_caster<MlirPassManager> {
195 PYBIND11_TYPE_CASTER(MlirPassManager, _(
"MlirPassManager"));
205 struct type_caster<MlirType> {
206 PYBIND11_TYPE_CASTER(MlirType, _(
"MlirType"));
212 static handle
cast(MlirType t, return_value_policy, handle) {
243 const py::object &superClass) {
245 py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
246 py::object metaclass = pyType(superClass);
250 metaclass(derivedClassName, py::make_tuple(superClass), attributes);
251 scope.attr(derivedClassName) = thisClass;
254 template <
typename Func,
typename... Extra>
257 std::forward<Func>(f), py::name(name), py::is_method(thisClass),
258 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
259 thisClass.attr(cf.name()) = cf;
263 template <
typename Func,
typename... Extra>
265 const Extra &... extra) {
267 std::forward<Func>(f), py::name(name), py::is_method(thisClass),
268 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
269 auto builtinProperty =
270 py::reinterpret_borrow<py::object>((PyObject *)&PyProperty_Type);
271 thisClass.attr(name) = builtinProperty(cf);
275 template <
typename Func,
typename... Extra>
277 const Extra &... extra) {
279 "def_staticmethod(...) called with a non-static member " 282 std::forward<Func>(f), py::name(name), py::scope(thisClass),
283 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
284 thisClass.attr(cf.name()) = py::staticmethod(cf);
288 template <
typename Func,
typename... Extra>
290 const Extra &... extra) {
292 "def_classmethod(...) called with a non-static member " 295 std::forward<Func>(f), py::name(name), py::scope(thisClass),
296 py::sibling(py::getattr(thisClass, name, py::none())), extra...);
297 thisClass.attr(cf.name()) =
298 py::reinterpret_borrow<py::object>(PyClassMethod_New(cf.ptr()));
319 scope, attrClassName, isaFunction,
321 .attr(
"Attribute")) {}
338 std::string captureTypeName(
340 py::cpp_function newCf(
341 [superCls, isaFunction, captureTypeName](py::object cls,
342 py::object otherAttribute) {
343 MlirAttribute rawAttribute = py::cast<MlirAttribute>(otherAttribute);
344 if (!isaFunction(rawAttribute)) {
345 auto origRepr = py::repr(otherAttribute).cast<std::string>();
346 throw std::invalid_argument(
347 (llvm::Twine(
"Cannot cast attribute to ") + captureTypeName +
348 " (from " + origRepr +
")")
351 py::object
self = superCls.attr(
"__new__")(cls, otherAttribute);
354 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_attr"));
355 thisClass.attr(
"__new__") = newCf;
360 [isaFunction](MlirAttribute other) {
return isaFunction(other); },
361 py::arg(
"other_attribute"));
375 scope, typeClassName, isaFunction,
393 std::string captureTypeName(
395 py::cpp_function newCf(
396 [superCls, isaFunction, captureTypeName](py::object cls,
397 py::object otherType) {
398 MlirType rawType = py::cast<MlirType>(otherType);
399 if (!isaFunction(rawType)) {
400 auto origRepr = py::repr(otherType).cast<std::string>();
401 throw std::invalid_argument((llvm::Twine(
"Cannot cast type to ") +
402 captureTypeName +
" (from " +
406 py::object
self = superCls.attr(
"__new__")(cls, otherType);
409 py::name(
"__new__"), py::arg(
"cls"), py::arg(
"cast_from_type"));
410 thisClass.attr(
"__new__") = newCf;
415 [isaFunction](MlirType other) {
return isaFunction(other); },
416 py::arg(
"other_type"));
424 #endif // MLIR_BINDINGS_PYTHON_PYBINDADAPTORS_H TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
mlir_attribute_subclass(py::handle scope, const char *attrClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
Creates a custom subclass of mlir.ir.Type, implementing a casting constructor and type checking metho...
static PyObject * mlirPythonLocationToCapsule(MlirLocation loc)
Creates a capsule object encapsulating the raw C-API MlirLocation.
static bool mlirAffineMapIsNull(MlirAffineMap affineMap)
Checks whether an affine map is null.
static MlirModule mlirPythonCapsuleToModule(PyObject *capsule)
Extracts an MlirModule from a capsule as produced from mlirPythonModuleToCapsule. ...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
static MlirContext mlirPythonCapsuleToContext(PyObject *capsule)
Extracts a MlirContext from a capsule as produced from mlirPythonContextToCapsule.
Creates a custom subclass of mlir.ir.Attribute, implementing a casting constructor and type checking ...
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Type super-class.
static MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule)
Extracts an MlirOperations from a capsule as produced from mlirPythonOperationToCapsule.
pure_subclass(py::handle scope, const char *derivedClassName, const py::object &superClass)
static bool mlirAttributeIsNull(MlirAttribute attr)
Checks whether an attribute is null.
bool load(handle src, bool)
bool load(handle src, bool)
static bool mlirPassManagerIsNull(MlirPassManager passManager)
Checks if a PassManager is null.
static MlirType mlirPythonCapsuleToType(PyObject *capsule)
Extracts an MlirType from a capsule as produced from mlirPythonTypeToCapsule.
static handle cast(MlirModule v, return_value_policy, handle)
static handle cast(MlirAttribute v, return_value_policy, handle)
bool load(handle src, bool)
static handle cast(MlirOperation v, return_value_policy, handle)
static constexpr const bool value
static py::object mlirApiObjectToCapsule(py::handle apiObject)
Helper to convert a presumed MLIR API object to a capsule, accepting either an explicit Capsule (whic...
bool(*)(MlirAttribute) IsAFunctionTy
bool load(handle src, bool)
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
pure_subclass & def_classmethod(const char *name, Func &&f, const Extra &... extra)
static PyObject * mlirPythonTypeToCapsule(MlirType type)
Creates a capsule object encapsulating the raw C-API MlirType.
static PyObject * mlirPythonAffineMapToCapsule(MlirAffineMap affineMap)
Creates a capsule object encapsulating the raw C-API MlirAffineMap.
Provides a facility like py::class_ for defining a new class in a scope, but this allows extension of...
pure_subclass & def_staticmethod(const char *name, Func &&f, const Extra &... extra)
bool load(handle src, bool)
bool load(handle src, bool)
#define MAKE_MLIR_PYTHON_QUALNAME(local)
mlir_attribute_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction, const py::object &superCls)
Subclasses with a provided mlir.ir.Attribute super-class.
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
bool(*)(MlirType) IsAFunctionTy
pure_subclass & def(const char *name, Func &&f, const Extra &... extra)
static MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule)
Extracts an MlirLocation from a capsule as produced from mlirPythonLocationToCapsule.
static handle cast(MlirLocation v, return_value_policy, handle)
static handle cast(MlirAffineMap v, return_value_policy, handle)
static handle cast(MlirType t, return_value_policy, handle)
pure_subclass & def_property_readonly(const char *name, Func &&f, const Extra &... extra)
bool load(handle src, bool)
static PyObject * mlirPythonOperationToCapsule(MlirOperation operation)
Creates a capsule object encapsulating the raw C-API MlirOperation.
mlir_type_subclass(py::handle scope, const char *typeClassName, IsAFunctionTy isaFunction)
Subclasses by looking up the super-class dynamically.
static bool mlirContextIsNull(MlirContext context)
Checks whether a context is null.
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
static PyObject * mlirPythonAttributeToCapsule(MlirAttribute attribute)
Creates a capsule object encapsulating the raw C-API MlirAttribute.
static MlirPassManager mlirPythonCapsuleToPassManager(PyObject *capsule)
Extracts an MlirPassManager from a capsule as produced from mlirPythonPassManagerToCapsule.
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 bool mlirOperationIsNull(MlirOperation op)
Checks whether the underlying operation is null.
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
bool load(handle src, bool)
static bool mlirModuleIsNull(MlirModule module)
Checks whether a module is null.
static MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule)
Extracts an MlirAttribute from a capsule as produced from mlirPythonAttributeToCapsule.
py::object get_class() const