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.)";
39 R
"(Given the arguments required to build an operation, attempts to infer
40 its return shaped type components. Raises ValueError on failure.)";
49 if (!operandList || operandList->empty()) {
54 mlirOperands.reserve(operandList->size());
56 if (it.value().is_none())
61 val = py::cast<PyValue *>(it.value());
63 throw py::cast_error();
64 mlirOperands.push_back(val->
get());
66 }
catch (py::cast_error &err) {
72 auto vals = py::cast<py::sequence>(it.value());
73 for (py::object v : vals) {
75 val = py::cast<PyValue *>(v);
77 throw py::cast_error();
78 mlirOperands.push_back(val->
get());
79 }
catch (py::cast_error &err) {
80 throw py::value_error(
81 (llvm::Twine(
"Operand ") + llvm::Twine(it.index()) +
82 " must be a Value or Sequence of Values (" + err.what() +
")")
87 }
catch (py::cast_error &err) {
88 throw py::value_error((llvm::Twine(
"Operand ") + llvm::Twine(it.index()) +
89 " must be a Value or Sequence of Values (" +
94 throw py::cast_error();
103 wrapRegions(std::optional<std::vector<PyRegion>> regions) {
107 mlirRegions.reserve(regions->size());
109 mlirRegions.push_back(region);
134 template <
typename ConcreteIface>
145 : obj(std::move(object)) {
147 operation = &py::cast<PyOperation &>(obj);
148 }
catch (py::cast_error &) {
154 }
catch (py::cast_error &) {
158 if (operation !=
nullptr) {
160 ConcreteIface::getInterfaceID())) {
161 std::string msg =
"the operation does not implement ";
162 throw py::value_error(msg + ConcreteIface::pyClassName);
167 opName = std::string(stringRef.
data, stringRef.
length);
170 opName = obj.attr(
"OPERATION_NAME").template cast<std::string>();
171 }
catch (py::cast_error &) {
172 throw py::type_error(
173 "Op interface does not refer to an operation or OpView class");
178 context.
resolve().
get(), ConcreteIface::getInterfaceID())) {
179 std::string msg =
"the operation does not implement ";
180 throw py::value_error(msg + ConcreteIface::pyClassName);
186 static void bind(py::module &m) {
187 py::class_<ConcreteIface> cls(m, ConcreteIface::pyClassName,
189 cls.def(py::init<py::object, DefaultingPyMlirContext>(), py::arg(
"object"),
191 .def_property_readonly(
"operation",
196 ConcreteIface::bindDerived(cls);
210 if (operation ==
nullptr) {
211 throw py::type_error(
"Cannot get an operation from a static interface");
221 if (operation ==
nullptr) {
222 throw py::type_error(
"Cannot get an opview from a static interface");
245 constexpr
static const char *
pyClassName =
"InferTypeOpInterface";
260 data->
inferredTypes.reserve(data->inferredTypes.size() + nTypes);
261 for (intptr_t i = 0; i < nTypes; ++i) {
262 data->inferredTypes.emplace_back(data->pyMlirContext.getRef(), types[i]);
270 std::optional<PyAttribute> attributes,
void *properties,
271 std::optional<std::vector<PyRegion>> regions,
277 std::vector<PyType> inferredTypes;
282 MlirAttribute attributeDict =
286 opNameRef, pyContext.
get(), location.
resolve(), mlirOperands.size(),
287 mlirOperands.data(), attributeDict, properties, mlirRegions.size(),
291 throw py::value_error(
"Failed to infer result types");
294 return inferredTypes;
299 py::arg(
"operands") = py::none(),
300 py::arg(
"attributes") = py::none(),
301 py::arg(
"properties") = py::none(), py::arg(
"regions") = py::none(),
302 py::arg(
"context") = py::none(), py::arg(
"loc") = py::none(),
312 : shape(shape), elementType(elementType), ranked(true) {}
314 MlirAttribute attribute)
315 : shape(shape), elementType(elementType), attribute(attribute),
319 : shape(other.shape), elementType(other.elementType),
320 attribute(other.attribute), ranked(other.ranked) {}
322 static void bind(py::module &m) {
323 py::class_<PyShapedTypeComponents>(m,
"ShapedTypeComponents",
325 .def_property_readonly(
328 "Returns the element type of the shaped type components.")
334 py::arg(
"element_type"),
335 "Create an shaped type components object with only the element "
339 [](py::list shape,
PyType &elementType) {
342 py::arg(
"shape"), py::arg(
"element_type"),
343 "Create a ranked shaped type components object.")
349 py::arg(
"shape"), py::arg(
"element_type"), py::arg(
"attribute"),
350 "Create a ranked shaped type components object with attribute.")
351 .def_property_readonly(
354 "Returns whether the given shaped type component is ranked.")
355 .def_property_readonly(
361 return py::int_(
self.shape.size());
363 "Returns the rank of the given ranked shaped type components. If "
364 "the shaped type components does not have a rank, None is "
366 .def_property_readonly(
372 return py::list(
self.shape);
374 "Returns the shape of the ranked shaped type components as a list "
375 "of integers. Returns none if the shaped type component does not "
384 MlirType elementType;
385 MlirAttribute attribute;
397 constexpr
static const char *
pyClassName =
"InferShapedTypeOpInterface";
409 const int64_t *shape, MlirType elementType,
410 MlirAttribute attribute,
void *userData) {
416 for (intptr_t i = 0; i < rank; ++i) {
417 shapeList.append(shape[i]);
419 data->inferredShapedTypeComponents.emplace_back(shapeList, elementType,
427 std::optional<py::list> operandList,
428 std::optional<PyAttribute> attributes,
void *properties,
429 std::optional<std::vector<PyRegion>> regions,
434 std::vector<PyShapedTypeComponents> inferredShapedTypeComponents;
439 MlirAttribute attributeDict =
443 opNameRef, pyContext.
get(), location.
resolve(), mlirOperands.size(),
444 mlirOperands.data(), attributeDict, properties, mlirRegions.size(),
448 throw py::value_error(
"Failed to infer result shape type components");
451 return inferredShapedTypeComponents;
455 cls.def(
"inferReturnTypeComponents",
457 py::arg(
"operands") = py::none(),
458 py::arg(
"attributes") = py::none(), py::arg(
"regions") = py::none(),
459 py::arg(
"properties") = py::none(), py::arg(
"context") = 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()
Wrapper around the generic MlirAttribute.
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 InferShapedTypeOpInterface.
constexpr static const char * pyClassName
static void appendResultsCallback(bool hasRank, intptr_t rank, const int64_t *shape, MlirType elementType, MlirAttribute attribute, void *userData)
Appends the shaped type components provided as unpacked shape, element type, attribute to the user-da...
std::vector< PyShapedTypeComponents > inferReturnTypeComponents(std::optional< py::list > operandList, std::optional< PyAttribute > attributes, void *properties, std::optional< std::vector< PyRegion >> regions, DefaultingPyMlirContext context, DefaultingPyLocation location)
Given the arguments required to build an operation, attempts to infer the shaped type components.
constexpr static GetTypeIDFunctionTy getInterfaceID
static void bindDerived(ClassTy &cls)
Python wrapper for InferTypeOpInterface.
constexpr static GetTypeIDFunctionTy getInterfaceID
std::vector< PyType > inferReturnTypes(std::optional< py::list > operandList, std::optional< PyAttribute > attributes, void *properties, 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 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 an shaped type components.
PyShapedTypeComponents(PyShapedTypeComponents &)=delete
static void bind(py::module &m)
pybind11::object getCapsule()
PyShapedTypeComponents(py::list shape, MlirType elementType)
PyShapedTypeComponents(MlirType elementType)
PyShapedTypeComponents(PyShapedTypeComponents &&other)
PyShapedTypeComponents(py::list shape, MlirType elementType, MlirAttribute attribute)
static PyShapedTypeComponents createFromCapsule(pybind11::object capsule)
Wrapper around the generic MlirType.
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 MlirLogicalResult mlirInferShapedTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, void *properties, intptr_t nRegions, MlirRegion *regions, MlirShapedTypeComponentsCallback callback, void *userData)
Infers the return shaped type components of the operation.
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, void *properties, 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 MlirTypeID mlirInferShapedTypeOpInterfaceTypeID()
Returns the interface TypeID of the InferShapedTypeOpInterface.
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
constexpr static const char * inferReturnTypeComponentsDoc
void populateIRInterfaces(py::module &m)
This header declares functions that assist 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< PyShapedTypeComponents > & inferredShapedTypeComponents
C-style user-data structure for type appending callback.
std::vector< PyType > & inferredTypes
PyMlirContext & pyMlirContext