9#ifndef MLIR_BINDINGS_PYTHON_IRINTERFACES_H
10#define MLIR_BINDINGS_PYTHON_IRINTERFACES_H
17#include <nanobind/nanobind.h>
39template <
typename ConcreteIface>
42 using ClassTy = nanobind::class_<ConcreteIface>;
51 : obj(std::move(object)) {
52 if (!nanobind::try_cast<PyOperation *>(obj, operation)) {
54 if (nanobind::try_cast<PyOpView *>(obj, opview)) {
59 if (operation !=
nullptr) {
61 ConcreteIface::getInterfaceID())) {
62 std::string msg =
"the operation does not implement ";
63 throw nanobind::value_error((msg + ConcreteIface::pyClassName).c_str());
68 opName = std::string(stringRef.
data, stringRef.
length);
70 if (!nanobind::try_cast<std::string>(obj.attr(
"OPERATION_NAME"), opName))
71 throw nanobind::type_error(
72 "Op interface does not refer to an operation or OpView class");
76 context.
resolve().
get(), ConcreteIface::getInterfaceID())) {
77 std::string msg =
"the operation does not implement ";
78 throw nanobind::value_error((msg + ConcreteIface::pyClassName).c_str());
84 static void bind(nanobind::module_ &m) {
85 nanobind::class_<ConcreteIface> cls(m, ConcreteIface::pyClassName);
86 cls.def(nanobind::init<nanobind::object, DefaultingPyMlirContext>(),
87 nanobind::arg(
"object"),
88 nanobind::arg(
"context") = nanobind::none(),
89 "Creates an interface from a given operation/opview object or from "
90 "a subclass of OpView. Raises ValueError if the operation does not "
91 "implement the interface.")
94 "Returns an Operation for which the interface was constructed.")
96 "Returns an OpView subclass _instance_ for which the "
97 "interface was constructed");
98 ConcreteIface::bindDerived(cls);
112 if (operation ==
nullptr)
113 throw nanobind::type_error(
114 "Cannot get an operation from a static interface");
115 return operation->getRef().releaseObject();
121 nanobind::typed<nanobind::object, PyOpView>
getOpView() {
122 if (operation ==
nullptr)
123 throw nanobind::type_error(
124 "Cannot get an opview from a static interface");
125 return operation->createOpView();
135 nanobind::object obj;
MlirIdentifier mlirOperationGetName(MlirOperation op)
Used in function arguments when None should resolve to the current context manager set instance.
static PyMlirContext & resolve()
static void bind(nanobind::module_ &m)
Creates the Python bindings for this class in the given module.
MlirTypeID(*)() GetTypeIDFunctionTy
const std::string & getOpName()
Returns the canonical name of the operation this interface is constructed from.
PyConcreteOpInterface(nanobind::object object, DefaultingPyMlirContext context)
Constructs an interface instance from an object that is either an operation or a subclass of OpView.
nanobind::typed< nanobind::object, PyOperation > getOperationObject()
Returns the operation instance from which this object was constructed.
nanobind::typed< nanobind::object, PyOpView > getOpView()
Returns the opview of the operation instance from which this object was constructed.
nanobind::class_< ConcreteIface > ClassTy
bool isStatic()
Returns true if this object was constructed from a subclass of OpView rather than from an operation i...
static void bindDerived(ClassTy &cls)
Hook for derived classes to add class-specific bindings.
MlirContext get()
Accesses the underlying MlirContext.
A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for providing more instance-sp...
PyOperation & getOperation() override
Each must provide access to the raw Operation.
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterfaceStatic(MlirStringRef operationName, MlirContext context, MlirTypeID interfaceTypeID)
Returns true if the operation identified by its canonical string name implements the interface identi...
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
const char * data
Pointer to the first symbol.
size_t length
Length of the fragment.
MlirMemoryEffectInstancesList effects