9 #ifndef MLIR_BINDINGS_PYTHON_IRMODULES_H 10 #define MLIR_BINDINGS_PYTHON_IRMODULES_H 22 #include "llvm/ADT/DenseMap.h" 23 #include "llvm/ADT/Optional.h" 30 class PyDiagnosticHandler;
31 class PyInsertionPoint;
33 class DefaultingPyLocation;
35 class DefaultingPyMlirContext;
48 : referrent(referrent), object(std::move(object)) {
49 assert(this->referrent &&
50 "cannot construct PyObjectRef with null referrent");
51 assert(this->
object &&
"cannot construct PyObjectRef with null object");
54 : referrent(other.referrent), object(std::move(other.object)) {
55 other.referrent =
nullptr;
56 assert(!other.object);
59 : referrent(other.referrent), object(other.object ) {}
65 return object.ref_count();
72 assert(referrent &&
object);
74 auto stolen = std::move(
object);
78 T *
get() {
return referrent; }
80 assert(referrent &&
object);
84 assert(referrent &&
object);
87 operator bool()
const {
return referrent && object; }
91 pybind11::object object;
111 pybind11::object insertionPoint,
112 pybind11::object location)
113 : context(std::move(context)), insertionPoint(std::move(insertionPoint)),
114 location(std::move(location)), frameKind(frameKind) {}
134 static pybind11::object pushInsertionPoint(
PyInsertionPoint &insertionPoint);
136 static pybind11::object pushLocation(
PyLocation &location);
137 static void popLocation(
PyLocation &location);
140 static std::vector<PyThreadContextEntry> &getStack();
143 static void push(
FrameKind frameKind, pybind11::object context,
144 pybind11::object insertionPoint, pybind11::object location);
147 pybind11::object context;
149 pybind11::object insertionPoint;
151 pybind11::object location;
180 MlirContext
get() {
return context; }
189 pybind11::object getCapsule();
195 static pybind11::object createFromCapsule(pybind11::object capsule);
198 static size_t getLiveCount();
202 size_t getLiveOperationCount();
208 size_t clearLiveOperations();
212 size_t getLiveModuleCount();
215 pybind11::object contextEnter();
216 void contextExit(
const pybind11::object &excType,
217 const pybind11::object &excVal,
218 const pybind11::object &excTb);
222 pybind11::object attachDiagnosticHandler(pybind11::object callback);
259 :
public Defaulting<DefaultingPyMlirContext, PyMlirContext> {
262 static constexpr
const char kTypeDescription[] =
"mlir.ir.Context";
273 assert(this->contextRef &&
274 "context object constructed with null context ref");
295 pybind11::str getMessage();
296 pybind11::tuple getNotes();
341 const pybind11::object &excVal,
342 const pybind11::object &excTb) {
348 pybind11::object callback;
350 bool hadError =
false;
364 MlirDialect
get() {
return dialect; }
377 MlirDialect getDialectForKey(
const std::string &key,
bool attrError);
385 PyDialect(pybind11::object descriptor) : descriptor(std::move(descriptor)) {}
390 pybind11::object descriptor;
406 other.registry = {
nullptr};
409 operator MlirDialectRegistry()
const {
return registry; }
410 MlirDialectRegistry
get()
const {
return registry; }
412 pybind11::object getCapsule();
416 MlirDialectRegistry registry;
425 operator MlirLocation()
const {
return loc; }
426 MlirLocation
get()
const {
return loc; }
429 pybind11::object contextEnter();
430 void contextExit(
const pybind11::object &excType,
431 const pybind11::object &excVal,
432 const pybind11::object &excTb);
435 pybind11::object getCapsule();
441 static PyLocation createFromCapsule(pybind11::object capsule);
450 :
public Defaulting<DefaultingPyLocation, PyLocation> {
453 static constexpr
const char kTypeDescription[] =
"mlir.ir.Location";
456 operator MlirLocation()
const {
return *
get(); }
473 MlirModule
get() {
return module; }
478 pybind11::reinterpret_borrow<pybind11::object>(handle));
485 pybind11::object getCapsule();
491 static pybind11::object createFromCapsule(pybind11::object capsule);
496 pybind11::handle handle;
505 void print(pybind11::object fileObject,
bool binary,
507 bool prettyDebugInfo,
bool printGenericOpForm,
bool useLocalScope,
508 bool assumeVerified);
509 pybind11::object getAsm(
bool binary,
511 bool enableDebugInfo,
bool prettyDebugInfo,
512 bool printGenericOpForm,
bool useLocalScope,
513 bool assumeVerified);
540 pybind11::object parentKeepAlive = pybind11::object());
546 pybind11::object parentKeepAlive = pybind11::object());
553 parentKeepAlive = pybind11::object();
557 operator MlirOperation()
const {
return get(); }
558 MlirOperation
get()
const {
565 this, pybind11::reinterpret_borrow<pybind11::object>(handle));
569 void setAttached(
const pybind11::object &parent = pybind11::object()) {
570 assert(!attached &&
"operation already attached");
574 assert(attached &&
"operation already detached");
577 void checkValid()
const;
588 pybind11::object getCapsule();
593 static pybind11::object createFromCapsule(pybind11::object capsule);
596 static pybind11::object
597 create(
const std::string &name,
llvm::Optional<std::vector<PyType *>> results,
604 pybind11::object createOpView();
614 pybind11::object clone(
const pybind11::object &ip);
619 MlirOperation operation,
620 pybind11::object parentKeepAlive);
622 MlirOperation operation;
623 pybind11::handle handle;
630 pybind11::object parentKeepAlive;
631 bool attached =
true;
645 PyOpView(
const pybind11::object &operationObject);
648 static pybind11::object createRawSubclass(
const pybind11::object &userClass);
652 static pybind11::object
653 buildGeneric(
const pybind11::object &cls, pybind11::list resultTypeList,
654 pybind11::list operandList,
658 const pybind11::object &maybeIp);
662 pybind11::object operationObject;
671 : parentOperation(std::move(parentOperation)), region(region) {
674 operator MlirRegion()
const {
return region; }
676 MlirRegion
get() {
return region; }
692 : parentOperation(std::move(parentOperation)), block(block) {
696 MlirBlock
get() {
return block; }
727 pybind11::object contextEnter();
728 void contextExit(
const pybind11::object &excType,
729 const pybind11::object &excVal,
730 const pybind11::object &excTb);
738 : refOperation(std::move(refOperation)), block(std::move(block)) {}
750 operator MlirType()
const {
return type; }
751 MlirType
get()
const {
return type; }
754 pybind11::object getCapsule();
760 static PyType createFromCapsule(pybind11::object capsule);
771 template <
typename DerivedTy,
typename BaseTy = PyType>
777 using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
782 :
BaseTy(std::move(contextRef), t) {}
787 if (!DerivedTy::isaFunction(orig)) {
788 auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
789 throw SetPyError(PyExc_ValueError, llvm::Twine(
"Cannot cast type to ") +
790 DerivedTy::pyClassName +
791 " (from " + origRepr +
")");
796 static void bind(pybind11::module &m) {
797 auto cls =
ClassTy(m, DerivedTy::pyClassName, pybind11::module_local());
798 cls.def(pybind11::init<PyType &>(), pybind11::keep_alive<0, 1>(),
799 pybind11::arg(
"cast_from_type"));
802 [](
PyType &otherType) ->
bool {
803 return DerivedTy::isaFunction(otherType);
805 pybind11::arg(
"other"));
806 DerivedTy::bindDerived(cls);
820 operator MlirAttribute()
const {
return attr; }
821 MlirAttribute
get()
const {
return attr; }
824 pybind11::object getCapsule();
830 static PyAttribute createFromCapsule(pybind11::object capsule);
856 std::unique_ptr<std::string> ownedName;
864 template <
typename DerivedTy,
typename BaseTy = PyAttribute>
870 using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
875 :
BaseTy(std::move(contextRef), attr) {}
880 if (!DerivedTy::isaFunction(orig)) {
881 auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
883 llvm::Twine(
"Cannot cast attribute to ") +
884 DerivedTy::pyClassName +
" (from " + origRepr +
")");
889 static void bind(pybind11::module &m) {
890 auto cls =
ClassTy(m, DerivedTy::pyClassName, pybind11::buffer_protocol(),
891 pybind11::module_local());
892 cls.def(pybind11::init<PyAttribute &>(), pybind11::keep_alive<0, 1>(),
893 pybind11::arg(
"cast_from_attr"));
897 return DerivedTy::isaFunction(otherAttr);
899 pybind11::arg(
"other"));
900 cls.def_property_readonly(
"type", [](
PyAttribute &attr) {
903 DerivedTy::bindDerived(cls);
919 : parentOperation(std::move(parentOperation)), value(value) {}
920 operator MlirValue()
const {
return value; }
928 pybind11::object getCapsule();
932 static PyValue createFromCapsule(pybind11::object capsule);
945 operator MlirAffineExpr()
const {
return affineExpr; }
946 MlirAffineExpr
get()
const {
return affineExpr; }
949 pybind11::object getCapsule();
955 static PyAffineExpr createFromCapsule(pybind11::object capsule);
964 MlirAffineExpr affineExpr;
972 operator MlirAffineMap()
const {
return affineMap; }
973 MlirAffineMap
get()
const {
return affineMap; }
976 pybind11::object getCapsule();
982 static PyAffineMap createFromCapsule(pybind11::object capsule);
985 MlirAffineMap affineMap;
993 operator MlirIntegerSet()
const {
return integerSet; }
994 MlirIntegerSet
get()
const {
return integerSet; }
997 pybind11::object getCapsule();
1002 static PyIntegerSet createFromCapsule(pybind11::object capsule);
1005 MlirIntegerSet integerSet;
1019 pybind11::object dunderGetItem(
const std::string &name);
1026 void dunderDel(
const std::string &name);
1034 static void setSymbolName(
PyOperationBase &symbol,
const std::string &name);
1039 const std::string &visibility);
1043 static void replaceAllSymbolUses(
const std::string &oldSymbol,
1044 const std::string &newSymbol,
1048 static void walkSymbolTables(
PyOperationBase &from,
bool allSymUsesVisible,
1049 pybind11::object callback);
1052 operator MlirSymbolTable() {
return symbolTable; }
1056 MlirSymbolTable symbolTable;
1072 struct type_caster<
mlir::python::DefaultingPyMlirContext>
1075 struct type_caster<mlir::python::DefaultingPyLocation>
1081 #endif // MLIR_BINDINGS_PYTHON_IRMODULES_H Include the generated interface declarations.
PyRegion(PyOperationRef parentOperation, MlirRegion region)
static MlirAttribute castFrom(PyAttribute &orig)
void setAttached(const pybind11::object &parent=pybind11::object())
MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute)
Gets the type of this attribute.
PyLocation(PyMlirContextRef contextRef, MlirLocation loc)
MLIR_CAPI_EXPORTED void mlirOperationRemoveFromParent(MlirOperation op)
Removes the given operation from its parent block.
PyDialectRegistry(PyDialectRegistry &&other)
bool(*)(MlirType) IsAFunctionTy
pybind11::object getObject()
Used in function arguments when None should resolve to the current context manager set instance...
static bool mlirRegionIsNull(MlirRegion region)
Checks whether a region is null.
static void bind(pybind11::module &m)
PyDialect(pybind11::object descriptor)
PyOperation & getOperation() override
Each must provide access to the raw Operation.
PyAffineExpr(PyMlirContextRef contextRef, MlirAffineExpr affineExpr)
PyAffineMap(PyMlirContextRef contextRef, MlirAffineMap affineMap)
PyObjectRef< PyMlirContext > PyMlirContextRef
Wrapper around MlirContext.
static void bindDerived(ClassTy &m)
Implemented by derived classes to add methods to the Python subclass.
PyModuleRef getRef()
Gets a strong reference to this module.
MLIR_CAPI_EXPORTED void mlirDialectRegistryDestroy(MlirDialectRegistry registry)
Takes a dialect registry owned by the caller and destroys it.
PyObjectRef< PyOperation > PyOperationRef
Wrapper around an MlirBlock.
Wrapper around an MlirRegion.
Used in function arguments when None should resolve to the current context manager set instance...
Wrapper around an MlirDialect.
MlirDiagnosticSeverity
Severity of a diagnostic.
static constexpr const bool value
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
PyConcreteAttribute(PyAttribute &orig)
PyDiagnostic(MlirDiagnostic diagnostic)
PyOperationRef & getParentOperation()
void setInvalid()
Invalidate the operation.
void populateIRInterfaces(py::module &m)
PyOperationRef & getParentOperation()
pybind11::object releaseObject()
Releases the object held by this instance, returning it.
pybind11::class_< DerivedTy, BaseTy > ClassTy
int64_t floorDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR's floordiv operation on constants.
Template for a reference to a concrete type which captures a python reference to its underlying pytho...
Wrapper around the generic MlirType.
void populateIRAttributes(pybind11::module &m)
void populateIRTypes(pybind11::module &m)
User-level dialect object.
Represents a diagnostic handler attached to the context.
PyConcreteType(PyType &orig)
static bool mlirDialectRegistryIsNull(MlirDialectRegistry registry)
Checks if the dialect registry is null.
CRTP base classes for Python types that subclass Type and should be castable from it (i...
MLIR_CAPI_EXPORTED void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
int64_t ceilDiv(int64_t lhs, int64_t rhs)
Returns the result of MLIR's ceildiv operation on constants.
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
MLIR_CAPI_EXPORTED MlirDialectRegistry mlirDialectRegistryCreate()
Creates a dialect registry and transfers its ownership to the caller.
bool(*)(MlirAttribute) IsAFunctionTy
void contextExit(const pybind11::object &excType, const pybind11::object &excVal, const pybind11::object &excTb)
pybind11::error_already_set SetPyError(PyObject *excClass, const llvm::Twine &message)
pybind11::object getDescriptor()
An insertion point maintains a pointer to a Block and a reference operation.
User-level object for accessing dialects with dotted syntax such as: ctx.dialect.std.
static MlirType castFrom(PyType &orig)
int64_t mod(int64_t lhs, int64_t rhs)
Returns MLIR's mod operation on constants.
Wrapper around the generic MlirAttribute.
PyDialectRegistry(MlirDialectRegistry registry)
Wrapper around an MlirDialectRegistry.
void populateIRCore(pybind11::module &m)
BaseContextObject(PyMlirContextRef ref)
Wrapper around MlirAffineExpr. Affine expressions are owned by the context.
void detachFromParent()
Detaches the operation from its parent block and updates its state accordingly.
Wrapper around an MlirLocation.
PyDialectDescriptor(PyMlirContextRef contextRef, MlirDialect dialect)
Base class for all objects that directly or indirectly depend on an MlirContext.
PyOperation & getOperation() override
Each must provide access to the raw Operation.
PyBlock(PyOperationRef parentOperation, MlirBlock block)
static void bind(pybind11::module &m)
An opaque reference to a diagnostic, always owned by the diagnostics engine (context).
PyMlirContextRef & getContext()
Accesses the context reference.
Represents a Python MlirNamedAttr, carrying an optional owned name.
PyValue(PyOperationRef parentOperation, MlirValue value)
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
CRTP base classes for Python attributes that subclass Attribute and should be castable from it (i...
PyMlirContextRef getRef()
Gets a strong reference to this context, which will ensure it is kept alive for the life of the refer...
PyConcreteAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
Bindings for MLIR symbol tables.
~PySymbolTable()
Destroys the symbol table.
PyObjectRef(const PyObjectRef &other)
PyType(PyMlirContextRef contextRef, MlirType type)
PyThreadContextEntry(FrameKind frameKind, pybind11::object context, pybind11::object insertionPoint, pybind11::object location)
A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for providing more instance-sp...
MlirNamedAttribute namedAttr
void populateIRAffine(pybind11::module &m)
PyDialects(PyMlirContextRef contextRef)
Wrapper around the generic MlirValue.
PyObjectRef< PyModule > PyModuleRef
PyObjectRef(PyObjectRef &&other)
PyOperationRef & getParentOperation()
CRTP template for special wrapper types that are allowed to be passed in as 'None' function arguments...
static void bindDerived(ClassTy &m)
Implemented by derived classes to add methods to the Python subclass.
pybind11::object getOperationObject()
pybind11::object contextEnter()
PyConcreteType(PyMlirContextRef contextRef, MlirType t)
Tracks an entry in the thread context stack.
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
PyObjectRef(T *referrent, pybind11::object object)
Defaulting()=default
Type casters require the type to be default constructible, but using such an instance is illegal...
PyIntegerSet(PyMlirContextRef contextRef, MlirIntegerSet integerSet)
Python class mirroring the C MlirDiagnostic struct.
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...
pybind11::class_< DerivedTy, BaseTy > ClassTy