21 R
"(Creates an interface from a given operation/opview object or from a 22 subclass of OpView. Raises ValueError if the operation does not implement the 26 R
"(Returns an Operation for which the interface was constructed.)"; 29 R
"(Returns an OpView subclass _instance_ for which the interface was 33 R
"(Given the arguments required to build an operation, attempts to infer 34 its return types. Raises ValueError on failure.)"; 52 template <
typename ConcreteIface>
55 using ClassTy = py::class_<ConcreteIface>;
63 : obj(std::move(object)) {
65 operation = &py::cast<PyOperation &>(obj);
66 }
catch (py::cast_error &) {
71 operation = &py::cast<PyOpView &>(obj).getOperation();
72 }
catch (py::cast_error &) {
76 if (operation !=
nullptr) {
78 ConcreteIface::getInterfaceID())) {
79 std::string msg =
"the operation does not implement ";
80 throw py::value_error(msg + ConcreteIface::pyClassName);
85 opName = std::string(stringRef.
data, stringRef.
length);
88 opName = obj.attr(
"OPERATION_NAME").template cast<std::string>();
89 }
catch (py::cast_error &) {
91 "Op interface does not refer to an operation or OpView class");
96 context.
resolve().
get(), ConcreteIface::getInterfaceID())) {
97 std::string msg =
"the operation does not implement ";
98 throw py::value_error(msg + ConcreteIface::pyClassName);
104 static void bind(py::module &m) {
105 py::class_<ConcreteIface> cls(m,
"InferTypeOpInterface",
107 cls.def(py::init<py::object, DefaultingPyMlirContext>(), py::arg(
"object"),
108 py::arg(
"context") = py::none(), constructorDoc)
109 .def_property_readonly(
"operation",
114 ConcreteIface::bindDerived(cls);
128 if (operation ==
nullptr) {
129 throw py::type_error(
"Cannot get an operation from a static interface");
139 if (operation ==
nullptr) {
140 throw py::type_error(
"Cannot get an opview from a static interface");
163 constexpr
static const char *pyClassName =
"InferTypeOpInterface";
178 data->
inferredTypes.reserve(data->inferredTypes.size() + nTypes);
179 for (intptr_t i = 0; i < nTypes; ++i) {
180 data->inferredTypes.emplace_back(data->pyMlirContext.getRef(), types[i]);
196 mlirOperands.reserve(operands->size());
198 mlirOperands.push_back(
value);
203 mlirRegions.reserve(regions->size());
205 mlirRegions.push_back(region);
209 std::vector<PyType> inferredTypes;
214 MlirAttribute attributeDict =
218 opNameRef, pyContext.get(), location.
resolve(), mlirOperands.size(),
219 mlirOperands.data(), attributeDict, mlirRegions.size(),
220 mlirRegions.data(), &appendResultsCallback, &data);
223 throw py::value_error(
"Failed to infer result types");
226 return inferredTypes;
231 py::arg(
"operands") = py::none(),
232 py::arg(
"attributes") = py::none(), py::arg(
"regions") = py::none(),
233 py::arg(
"context") = py::none(), py::arg(
"loc") = py::none(),
234 inferReturnTypesDoc);
Include the generated interface declarations.
static void bindDerived(ClassTy &cls)
const char * data
Pointer to the first symbol.
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...
MlirTypeID(*)() GetTypeIDFunctionTy
std::vector< PyType > inferReturnTypes(llvm::Optional< std::vector< PyValue >> operands, llvm::Optional< PyAttribute > attributes, llvm::Optional< std::vector< PyRegion >> regions, DefaultingPyMlirContext context, DefaultingPyLocation location)
Given the arguments required to build an operation, attempts to infer its return types.
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...
bool isStatic()
Returns true if this object was constructed from a subclass of OpView rather than from an operation i...
static constexpr const char * opviewDoc
Used in function arguments when None should resolve to the current context manager set instance...
static constexpr const char * constructorDoc
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
static PyLocation & resolve()
static void bind(py::module &m)
Creates the Python bindings for this class in the given module.
MLIR_CAPI_EXPORTED MlirTypeID mlirInferTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferTypeOpInterface.
pybind11::object createOpView()
Creates an OpView suitable for this operation.
static constexpr const char * inferReturnTypesDoc
Wrapper around an MlirRegion.
Used in function arguments when None should resolve to the current context manager set instance...
C-style user-data structure for type appending callback.
static constexpr const bool value
py::object getOpView()
Returns the opview of the operation instance from which this object was constructed.
void populateIRInterfaces(py::module &m)
static constexpr const char * operationDoc
pybind11::object releaseObject()
Releases the object held by this instance, returning it.
PyConcreteOpInterface(py::object object, DefaultingPyMlirContext context)
Constructs an interface instance from an object that is either an operation or a subclass of OpView...
A logical result value, essentially a boolean with named states.
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...
MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op)
Gets the name of the operation as an identifier.
static void bindDerived(ClassTy &cls)
Hook for derived classes to add class-specific bindings.
size_t length
Length of the fragment.
MlirContext get()
Accesses the underlying MlirContext.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeGetNull()
Returns an empty attribute.
A pointer to a sized fragment of a string, not necessarily null-terminated.
PyMlirContext & pyMlirContext
CRTP base class for Python classes representing MLIR Op interfaces.
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Wrapper around the generic MlirValue.
py::class_< PyInferTypeOpInterface > ClassTy
static PyMlirContext & resolve()
Python wrapper for InterTypeOpInterface.
py::object getOperationObject()
Returns the operation instance from which this object was constructed.
std::vector< PyType > & inferredTypes
const std::string & getOpName()
Returns the canonical name of the operation this interface is constructed from.
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.