9#ifndef MLIR_BINDINGS_PYTHON_IRINTERFACES_H
10#define MLIR_BINDINGS_PYTHON_IRINTERFACES_H
17#include <nanobind/nanobind.h>
24 R
"(Creates an interface from a given operation/opview object or from a
25subclass of OpView. Raises ValueError if the operation does not implement the
29 R
"(Returns an Operation for which the interface was constructed.)";
32 R
"(Returns an OpView subclass _instance_ for which the interface was
51template <
typename ConcreteIface>
54 using ClassTy = nanobind::class_<ConcreteIface>;
63 : obj(std::move(object)) {
64 if (!nanobind::try_cast<PyOperation *>(obj, operation)) {
66 if (nanobind::try_cast<PyOpView *>(obj, opview)) {
71 if (operation !=
nullptr) {
73 ConcreteIface::getInterfaceID())) {
74 std::string msg =
"the operation does not implement ";
75 throw nanobind::value_error((msg + ConcreteIface::pyClassName).c_str());
80 opName = std::string(stringRef.
data, stringRef.
length);
82 if (!nanobind::try_cast<std::string>(obj.attr(
"OPERATION_NAME"), opName))
83 throw nanobind::type_error(
84 "Op interface does not refer to an operation or OpView class");
88 context.
resolve().
get(), ConcreteIface::getInterfaceID())) {
89 std::string msg =
"the operation does not implement ";
90 throw nanobind::value_error((msg + ConcreteIface::pyClassName).c_str());
96 static void bind(nanobind::module_ &m) {
97 nanobind::class_<ConcreteIface> cls(m, ConcreteIface::pyClassName);
98 cls.def(nanobind::init<nanobind::object, DefaultingPyMlirContext>(),
99 nanobind::arg(
"object"),
104 ConcreteIface::bindDerived(cls);
118 if (operation ==
nullptr)
119 throw nanobind::type_error(
120 "Cannot get an operation from a static interface");
121 return operation->getRef().releaseObject();
127 nanobind::typed<nanobind::object, PyOpView>
getOpView() {
128 if (operation ==
nullptr)
129 throw nanobind::type_error(
130 "Cannot get an opview from a static interface");
131 return operation->createOpView();
141 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.
static constexpr const char * operationDoc
static constexpr const char * opviewDoc
static constexpr const char * constructorDoc
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