15 #include "llvm/ADT/STLExtras.h"
23 R
"(Creates an interface from a given operation/opview object or from a
24 subclass of OpView. Raises ValueError if the operation does not implement the
28 R
"(Returns an Operation for which the interface was constructed.)";
31 R
"(Returns an OpView subclass _instance_ for which the interface was
35 R
"(Given the arguments required to build an operation, attempts to infer
36 its return types. Raises ValueError on failure.)";
54 template <
typename ConcreteIface>
57 using ClassTy = py::class_<ConcreteIface>;
65 : obj(std::move(object)) {
67 operation = &py::cast<PyOperation &>(obj);
68 }
catch (py::cast_error &) {
74 }
catch (py::cast_error &) {
78 if (operation !=
nullptr) {
80 ConcreteIface::getInterfaceID())) {
81 std::string msg =
"the operation does not implement ";
82 throw py::value_error(msg + ConcreteIface::pyClassName);
87 opName = std::string(stringRef.
data, stringRef.
length);
90 opName = obj.attr(
"OPERATION_NAME").template cast<std::string>();
91 }
catch (py::cast_error &) {
93 "Op interface does not refer to an operation or OpView class");
98 context.
resolve().
get(), ConcreteIface::getInterfaceID())) {
99 std::string msg =
"the operation does not implement ";
100 throw py::value_error(msg + ConcreteIface::pyClassName);
106 static void bind(py::module &m) {
107 py::class_<ConcreteIface> cls(m,
"InferTypeOpInterface",
109 cls.def(py::init<py::object, DefaultingPyMlirContext>(), py::arg(
"object"),
111 .def_property_readonly(
"operation",
116 ConcreteIface::bindDerived(cls);
130 if (operation ==
nullptr) {
131 throw py::type_error(
"Cannot get an operation from a static interface");
141 if (operation ==
nullptr) {
142 throw py::type_error(
"Cannot get an opview from a static interface");
165 constexpr
static const char *
pyClassName =
"InferTypeOpInterface";
180 data->
inferredTypes.reserve(data->inferredTypes.size() + nTypes);
181 for (intptr_t i = 0; i < nTypes; ++i) {
182 data->inferredTypes.emplace_back(data->pyMlirContext.getRef(), types[i]);
190 std::optional<PyAttribute> attributes,
191 std::optional<std::vector<PyRegion>> regions,
197 if (operandList && !operandList->empty()) {
199 mlirOperands.reserve(operandList->size());
203 val = py::cast<PyValue *>(it.value());
205 throw py::cast_error();
206 mlirOperands.push_back(val->
get());
208 }
catch (py::cast_error &err) {
214 auto vals = py::cast<py::sequence>(it.value());
215 for (py::object v : vals) {
217 val = py::cast<PyValue *>(v);
219 throw py::cast_error();
220 mlirOperands.push_back(val->
get());
221 }
catch (py::cast_error &err) {
222 throw py::value_error(
223 (llvm::Twine(
"Operand ") + llvm::Twine(it.index()) +
224 " must be a Value or Sequence of Values (" + err.what() +
230 }
catch (py::cast_error &err) {
231 throw py::value_error(
232 (llvm::Twine(
"Operand ") + llvm::Twine(it.index()) +
233 " must be a Value or Sequence of Values (" + err.what() +
")")
237 throw py::cast_error();
242 mlirRegions.reserve(regions->size());
244 mlirRegions.push_back(region);
248 std::vector<PyType> inferredTypes;
253 MlirAttribute attributeDict =
257 opNameRef, pyContext.
get(), location.
resolve(), mlirOperands.size(),
258 mlirOperands.data(), attributeDict, mlirRegions.size(),
262 throw py::value_error(
"Failed to infer result types");
265 return inferredTypes;
270 py::arg(
"operands") = py::none(),
271 py::arg(
"attributes") = py::none(), py::arg(
"regions") = py::none(),
272 py::arg(
"context") = py::none(), py::arg(
"loc") = py::none(),
Used in function arguments when None should resolve to the current context manager set instance.
static PyLocation & resolve()
Used in function arguments when None should resolve to the current context manager set instance.
static PyMlirContext & resolve()
CRTP base class for Python classes representing MLIR Op interfaces.
static void bind(py::module &m)
Creates the Python bindings for this class in the given module.
py::object getOpView()
Returns the opview of the operation instance from which this object was constructed.
MlirTypeID(*)() GetTypeIDFunctionTy
py::class_< ConcreteIface > ClassTy
PyConcreteOpInterface(py::object object, DefaultingPyMlirContext context)
Constructs an interface instance from an object that is either an operation or a subclass of OpView.
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.
py::object getOperationObject()
Returns the operation instance from which this object was constructed.
const std::string & getOpName()
Returns the canonical name of the operation this interface is constructed from.
Python wrapper for InterTypeOpInterface.
std::vector< PyType > inferReturnTypes(std::optional< py::list > operandList, std::optional< PyAttribute > attributes, std::optional< std::vector< PyRegion >> regions, DefaultingPyMlirContext context, DefaultingPyLocation location)
Given the arguments required to build an operation, attempts to infer its return types.
constexpr static GetTypeIDFunctionTy getInterfaceID
constexpr static const char * pyClassName
static void bindDerived(ClassTy &cls)
static void appendResultsCallback(intptr_t nTypes, MlirType *types, void *userData)
Appends the types provided as the two first arguments to the user-data structure (expects AppendResul...
MlirContext get()
Accesses the underlying MlirContext.
pybind11::object releaseObject()
Releases the object held by this instance, returning it.
PyOperation & getOperation() override
Each must provide access to the raw Operation.
pybind11::object createOpView()
Creates an OpView suitable for this operation.
Wrapper around an MlirRegion.
Wrapper around the generic MlirValue.
MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeGetNull(void)
Returns an empty attribute.
MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op)
Gets the name of the operation as an identifier.
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 MlirTypeID mlirInferTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferTypeOpInterface.
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirInferTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback, void *userData)
Infers the return types of the operation identified by its canonical given the arguments that will be...
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
constexpr static const char * inferReturnTypesDoc
constexpr static const char * operationDoc
constexpr static const char * opviewDoc
constexpr static const char * constructorDoc
void populateIRInterfaces(py::module &m)
This header declares functions that assit transformations in the MemRef dialect.
A logical result value, essentially a boolean with named states.
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.
C-style user-data structure for type appending callback.
std::vector< PyType > & inferredTypes
PyMlirContext & pyMlirContext