MLIR 23.0.0git
IRInterfaces.h
Go to the documentation of this file.
1//===- IRInterfaces.h - IR Interfaces for Python Bindings -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_BINDINGS_PYTHON_IRINTERFACES_H
10#define MLIR_BINDINGS_PYTHON_IRINTERFACES_H
11
12#include "mlir-c/IR.h"
13#include "mlir-c/Interfaces.h"
14#include "mlir-c/Support.h"
16
17#include <nanobind/nanobind.h>
18
19namespace mlir {
20namespace python {
22
23/// CRTP base class for Python classes representing MLIR Op interfaces.
24/// Interface hierarchies are flat so no base class is expected here. The
25/// derived class is expected to define the following static fields:
26/// - `const char *pyClassName` - the name of the Python class to create;
27/// - `GetTypeIDFunctionTy getInterfaceID` - the function producing the TypeID
28/// of the interface.
29/// Derived classes may redefine the `bindDerived(ClassTy &)` method to bind
30/// interface-specific methods.
31///
32/// An interface class may be constructed from either an Operation/OpView object
33/// or from a subclass of OpView. In the latter case, only the static interface
34/// methods are available, similarly to calling ConcereteOp::staticMethod on the
35/// C++ side. Implementations of concrete interfaces can use the `isStatic`
36/// method to check whether the interface object was constructed from a class or
37/// an operation/opview instance. The `getOpName` always succeeds and returns a
38/// canonical name of the operation suitable for lookups.
39template <typename ConcreteIface>
41protected:
42 using ClassTy = nanobind::class_<ConcreteIface>;
43 using GetTypeIDFunctionTy = MlirTypeID (*)();
44
45public:
46 /// Constructs an interface instance from an object that is either an
47 /// operation or a subclass of OpView. In the latter case, only the static
48 /// methods of the interface are accessible to the caller.
49 PyConcreteOpInterface(nanobind::object object,
51 : obj(std::move(object)) {
52 if (!nanobind::try_cast<PyOperation *>(obj, operation)) {
53 PyOpView *opview;
54 if (nanobind::try_cast<PyOpView *>(obj, opview)) {
55 operation = &opview->getOperation();
56 };
57 }
58
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());
64 }
65
66 MlirIdentifier identifier = mlirOperationGetName(*operation);
67 MlirStringRef stringRef = mlirIdentifierStr(identifier);
68 opName = std::string(stringRef.data, stringRef.length);
69 } else {
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");
73
75 mlirStringRefCreate(opName.data(), opName.length()),
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());
79 }
80 }
81 }
82
83 /// Creates the Python bindings for this class in the given module.
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.")
92 .def_prop_ro(
94 "Returns an Operation for which the interface was constructed.")
95 .def_prop_ro("opview", &PyConcreteOpInterface::getOpView,
96 "Returns an OpView subclass _instance_ for which the "
97 "interface was constructed");
98 ConcreteIface::bindDerived(cls);
99 }
100
101 /// Hook for derived classes to add class-specific bindings.
102 static void bindDerived(ClassTy &cls) {}
103
104 /// Returns `true` if this object was constructed from a subclass of OpView
105 /// rather than from an operation instance.
106 bool isStatic() { return operation == nullptr; }
107
108 /// Returns the operation instance from which this object was constructed.
109 /// Throws a type error if this object was constructed from a subclass of
110 /// OpView.
111 nanobind::typed<nanobind::object, PyOperation> getOperationObject() {
112 if (operation == nullptr)
113 throw nanobind::type_error(
114 "Cannot get an operation from a static interface");
115 return operation->getRef().releaseObject();
116 }
117
118 /// Returns the opview of the operation instance from which this object was
119 /// constructed. Throws a type error if this object was constructed form a
120 /// subclass of OpView.
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();
126 }
127
128 /// Returns the canonical name of the operation this interface is constructed
129 /// from.
130 const std::string &getOpName() { return opName; }
131
132private:
133 PyOperation *operation = nullptr;
134 std::string opName;
135 nanobind::object obj;
136};
137
139 MlirMemoryEffectInstancesList effects;
140};
141
142} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
143} // namespace python
144} // namespace mlir
145
146#endif // MLIR_BINDINGS_PYTHON_IRINTERFACES_H
MlirIdentifier mlirOperationGetName(MlirOperation op)
Definition IR.cpp:668
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:278
static void bind(nanobind::module_ &m)
Creates the Python bindings for this class in the given module.
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.
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.
Definition IRCore.h:211
A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for providing more instance-sp...
Definition IRCore.h:734
PyOperation & getOperation() override
Each must provide access to the raw Operation.
Definition IRCore.h:737
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Definition IR.cpp:1336
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.
Definition Support.h:87
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80