10 #ifndef MLIR_BINDINGS_PYTHON_IRMODULES_H
11 #define MLIR_BINDINGS_PYTHON_IRMODULES_H
27 #include "llvm/ADT/DenseMap.h"
34 class PyDiagnosticHandler;
35 class PyInsertionPoint;
37 class DefaultingPyLocation;
39 class DefaultingPyMlirContext;
42 class PyOperationBase;
53 : referrent(referrent), object(std::move(object)) {
54 assert(this->referrent &&
55 "cannot construct PyObjectRef with null referrent");
56 assert(this->
object &&
"cannot construct PyObjectRef with null object");
59 : referrent(other.referrent), object(std::move(other.object)) {
60 other.referrent =
nullptr;
61 assert(!other.object);
64 : referrent(other.referrent), object(other.object ) {}
70 return object.ref_count();
77 assert(referrent &&
object);
79 auto stolen = std::move(
object);
83 T *
get() {
return referrent; }
85 assert(referrent &&
object);
89 assert(referrent &&
object);
92 operator bool()
const {
return referrent && object; }
96 pybind11::object object;
116 pybind11::object insertionPoint,
117 pybind11::object location)
118 : context(std::move(context)), insertionPoint(std::move(insertionPoint)),
119 location(std::move(location)), frameKind(frameKind) {}
145 static std::vector<PyThreadContextEntry> &
getStack();
148 static void push(
FrameKind frameKind, pybind11::object context,
149 pybind11::object insertionPoint, pybind11::object location);
152 pybind11::object context;
154 pybind11::object insertionPoint;
156 pybind11::object location;
185 MlirContext
get() {
return context; }
242 const pybind11::object &excVal,
243 const pybind11::object &excTb);
264 static LiveContextMap &getLiveContexts();
270 using LiveModuleMap =
272 LiveModuleMap liveModules;
278 using LiveOperationMap =
280 LiveOperationMap liveOperations;
282 bool emitErrorDiagnostics =
false;
292 :
public Defaulting<DefaultingPyMlirContext, PyMlirContext> {
306 assert(this->contextRef &&
307 "context object constructed with null context ref");
323 operator MlirLocation()
const {
return loc; }
324 MlirLocation
get()
const {
return loc; }
329 const pybind11::object &excVal,
330 const pybind11::object &excTb);
376 std::optional<pybind11::tuple> materializedNotes;
412 const pybind11::object &excVal,
413 const pybind11::object &excTb) {
419 pybind11::object callback;
420 std::optional<MlirDiagnosticHandlerID> registeredID;
421 bool hadError =
false;
430 ctx->
get(), handler, this,
434 assert(errors.empty() &&
"unhandled captured errors");
437 std::vector<PyDiagnostic::DiagnosticInfo>
take() {
438 return std::move(errors);
444 std::vector<PyDiagnostic::DiagnosticInfo> errors;
459 MlirDialect
get() {
return dialect; }
480 PyDialect(pybind11::object descriptor) : descriptor(std::move(descriptor)) {}
485 pybind11::object descriptor;
501 : registry(other.registry) {
502 other.registry = {
nullptr};
505 operator MlirDialectRegistry()
const {
return registry; }
506 MlirDialectRegistry
get()
const {
return registry; }
512 MlirDialectRegistry registry;
518 :
public Defaulting<DefaultingPyLocation, PyLocation> {
524 operator MlirLocation()
const {
return *
get(); }
541 MlirModule
get() {
return module; }
546 pybind11::reinterpret_borrow<pybind11::object>(handle));
564 pybind11::handle handle;
575 void print(std::optional<int64_t> largeElementsLimit,
bool enableDebugInfo,
576 bool prettyDebugInfo,
bool printGenericOpForm,
bool useLocalScope,
577 bool assumeVerified, py::object fileObject,
bool binary,
581 pybind11::object
getAsm(
bool binary,
582 std::optional<int64_t> largeElementsLimit,
583 bool enableDebugInfo,
bool prettyDebugInfo,
584 bool printGenericOpForm,
bool useLocalScope,
585 bool assumeVerified,
bool skipRegions);
589 std::optional<int64_t> bytecodeVersion);
624 pybind11::object parentKeepAlive = pybind11::object());
630 pybind11::object parentKeepAlive = pybind11::object());
635 const std::string &sourceStr,
636 const std::string &sourceName);
643 parentKeepAlive = pybind11::object();
647 operator MlirOperation()
const {
return get(); }
648 MlirOperation
get()
const {
655 this, pybind11::reinterpret_borrow<pybind11::object>(handle));
659 void setAttached(
const pybind11::object &parent = pybind11::object()) {
660 assert(!attached &&
"operation already attached");
664 assert(attached &&
"operation already detached");
686 static pybind11::object
687 create(
const std::string &name, std::optional<std::vector<PyType *>> results,
688 std::optional<std::vector<PyValue *>> operands,
689 std::optional<pybind11::dict> attributes,
690 std::optional<std::vector<PyBlock *>> successors,
int regions,
705 pybind11::object
clone(
const pybind11::object &ip);
710 MlirOperation operation,
711 pybind11::object parentKeepAlive);
713 MlirOperation operation;
714 pybind11::handle handle;
721 pybind11::object parentKeepAlive;
722 bool attached =
true;
736 PyOpView(
const pybind11::object &operationObject);
742 const pybind11::object &cls, std::optional<pybind11::list> resultTypeList,
743 pybind11::list operandList, std::optional<pybind11::dict> attributes,
744 std::optional<std::vector<PyBlock *>> successors,
746 const pybind11::object &maybeIp);
760 pybind11::object operationObject;
769 : parentOperation(std::move(parentOperation)), region(region) {
772 operator MlirRegion()
const {
return region; }
774 MlirRegion
get() {
return region; }
810 MlirAsmState
get() {
return state; }
814 MlirOpPrintingFlags flags;
823 : parentOperation(std::move(parentOperation)), block(block) {
827 MlirBlock
get() {
return block; }
863 const pybind11::object &excVal,
864 const pybind11::object &excTb);
873 : refOperation(std::move(refOperation)), block(std::move(block)) {}
875 std::optional<PyOperationRef> refOperation;
885 operator MlirType()
const {
return type; }
886 MlirType
get()
const {
return type; }
911 operator MlirTypeID()
const {
return typeID; }
912 MlirTypeID
get() {
return typeID; }
929 template <
typename DerivedTy,
typename BaseTy = PyType>
935 using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
942 : BaseTy(std::move(contextRef), t) {}
947 if (!DerivedTy::isaFunction(orig)) {
948 auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
949 throw py::value_error((llvm::Twine(
"Cannot cast type to ") +
950 DerivedTy::pyClassName +
" (from " + origRepr +
957 static void bind(pybind11::module &m) {
958 auto cls =
ClassTy(m, DerivedTy::pyClassName, pybind11::module_local());
959 cls.def(pybind11::init<PyType &>(), pybind11::keep_alive<0, 1>(),
960 pybind11::arg(
"cast_from_type"));
963 [](
PyType &otherType) ->
bool {
964 return DerivedTy::isaFunction(otherType);
966 pybind11::arg(
"other"));
967 cls.def_property_readonly_static(
968 "static_typeid", [](py::object & ) -> MlirTypeID {
969 if (DerivedTy::getTypeIdFunction)
970 return DerivedTy::getTypeIdFunction();
971 throw py::attribute_error(
972 (DerivedTy::pyClassName + llvm::Twine(
" has no typeid.")).str());
974 cls.def_property_readonly(
"typeid", [](
PyType &
self) {
975 return py::cast(
self).attr(
"typeid").cast<MlirTypeID>();
977 cls.def(
"__repr__", [](
DerivedTy &
self) {
979 printAccum.
parts.append(DerivedTy::pyClassName);
980 printAccum.
parts.append(
"(");
982 printAccum.
parts.append(
")");
983 return printAccum.
join();
986 if (DerivedTy::getTypeIdFunction) {
988 DerivedTy::getTypeIdFunction(),
989 pybind11::cpp_function(
993 DerivedTy::bindDerived(cls);
1007 operator MlirAttribute()
const {
return attr; }
1008 MlirAttribute
get()
const {
return attr; }
1043 std::unique_ptr<std::string> ownedName;
1051 template <
typename DerivedTy,
typename BaseTy = PyAttribute>
1057 using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
1064 : BaseTy(std::move(contextRef), attr) {}
1069 if (!DerivedTy::isaFunction(orig)) {
1070 auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
1071 throw py::value_error((llvm::Twine(
"Cannot cast attribute to ") +
1072 DerivedTy::pyClassName +
" (from " + origRepr +
1079 static void bind(pybind11::module &m) {
1080 auto cls =
ClassTy(m, DerivedTy::pyClassName, pybind11::buffer_protocol(),
1081 pybind11::module_local());
1082 cls.def(pybind11::init<PyAttribute &>(), pybind11::keep_alive<0, 1>(),
1083 pybind11::arg(
"cast_from_attr"));
1087 return DerivedTy::isaFunction(otherAttr);
1089 pybind11::arg(
"other"));
1090 cls.def_property_readonly(
1092 cls.def_property_readonly_static(
1093 "static_typeid", [](py::object & ) -> MlirTypeID {
1094 if (DerivedTy::getTypeIdFunction)
1095 return DerivedTy::getTypeIdFunction();
1096 throw py::attribute_error(
1097 (DerivedTy::pyClassName + llvm::Twine(
" has no typeid.")).str());
1099 cls.def_property_readonly(
"typeid", [](
PyAttribute &
self) {
1100 return py::cast(
self).attr(
"typeid").cast<MlirTypeID>();
1102 cls.def(
"__repr__", [](
DerivedTy &
self) {
1104 printAccum.
parts.append(DerivedTy::pyClassName);
1105 printAccum.
parts.append(
"(");
1108 printAccum.
parts.append(
")");
1109 return printAccum.
join();
1112 if (DerivedTy::getTypeIdFunction) {
1114 DerivedTy::getTypeIdFunction(),
1120 DerivedTy::bindDerived(cls);
1140 : parentOperation(std::move(parentOperation)), value(value) {}
1141 operator MlirValue()
const {
return value; }
1143 MlirValue
get() {
return value; }
1168 operator MlirAffineExpr()
const {
return affineExpr; }
1169 MlirAffineExpr
get()
const {
return affineExpr; }
1187 MlirAffineExpr affineExpr;
1195 operator MlirAffineMap()
const {
return affineMap; }
1196 MlirAffineMap
get()
const {
return affineMap; }
1208 MlirAffineMap affineMap;
1216 operator MlirIntegerSet()
const {
return integerSet; }
1217 MlirIntegerSet
get()
const {
return integerSet; }
1228 MlirIntegerSet integerSet;
1249 void dunderDel(
const std::string &name);
1262 const std::string &visibility);
1267 const std::string &newSymbol,
1272 pybind11::object callback);
1275 operator MlirSymbolTable() {
return symbolTable; }
1279 MlirSymbolTable symbolTable;
1305 struct type_caster<
mlir::python::DefaultingPyMlirContext>
1308 struct type_caster<
mlir::python::DefaultingPyLocation>
static std::string diag(const llvm::Value &value)
Base class for all objects that directly or indirectly depend on an MlirContext.
PyMlirContextRef & getContext()
Accesses the context reference.
BaseContextObject(PyMlirContextRef ref)
Used in function arguments when None should resolve to the current context manager set instance.
static PyLocation & resolve()
static constexpr const char kTypeDescription[]
Used in function arguments when None should resolve to the current context manager set instance.
static constexpr const char kTypeDescription[]
static PyMlirContext & resolve()
CRTP template for special wrapper types that are allowed to be passed in as 'None' function arguments...
ReferrentTy * get() const
Defaulting()=default
Type casters require the type to be default constructible, but using such an instance is illegal.
Wrapper around MlirAffineExpr. Affine expressions are owned by the context.
static PyAffineExpr createFromCapsule(pybind11::object capsule)
Creates a PyAffineExpr from the MlirAffineExpr wrapped by a capsule.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirAffineExpr.
PyAffineExpr(PyMlirContextRef contextRef, MlirAffineExpr affineExpr)
PyAffineExpr ceilDiv(const PyAffineExpr &other) const
PyAffineExpr mul(const PyAffineExpr &other) const
PyAffineExpr mod(const PyAffineExpr &other) const
PyAffineExpr floorDiv(const PyAffineExpr &other) const
bool operator==(const PyAffineExpr &other) const
PyAffineExpr add(const PyAffineExpr &other) const
MlirAffineExpr get() const
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirAffineMap.
PyAffineMap(PyMlirContextRef contextRef, MlirAffineMap affineMap)
bool operator==(const PyAffineMap &other) const
MlirAffineMap get() const
static PyAffineMap createFromCapsule(pybind11::object capsule)
Creates a PyAffineMap from the MlirAffineMap wrapped by a capsule.
Wrapper around an MlirAsmState.
PyAsmState(PyOperationBase &operation, bool useLocalScope)
PyAsmState(MlirValue value, bool useLocalScope)
PyAsmState(PyAsmState &other)=delete
PyAsmState(const PyAsmState &other)=delete
Wrapper around the generic MlirAttribute.
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
static PyAttribute createFromCapsule(pybind11::object capsule)
Creates a PyAttribute from the MlirAttribute wrapped by a capsule.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirAttribute.
MlirAttribute get() const
bool operator==(const PyAttribute &other) const
Wrapper around an MlirBlock.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirBlock.
PyOperationRef & getParentOperation()
PyBlock(PyOperationRef parentOperation, MlirBlock block)
CRTP base classes for Python attributes that subclass Attribute and should be castable from it (i....
pybind11::class_< DerivedTy, BaseTy > ClassTy
MlirTypeID(*)() GetTypeIDFunctionTy
static MlirAttribute castFrom(PyAttribute &orig)
static void bind(pybind11::module &m)
static constexpr GetTypeIDFunctionTy getTypeIdFunction
PyConcreteAttribute()=default
bool(*)(MlirAttribute) IsAFunctionTy
PyConcreteAttribute(PyAttribute &orig)
PyConcreteAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
static void bindDerived(ClassTy &m)
Implemented by derived classes to add methods to the Python subclass.
CRTP base classes for Python types that subclass Type and should be castable from it (i....
static void bind(pybind11::module &m)
pybind11::class_< DerivedTy, BaseTy > ClassTy
PyConcreteType(PyType &orig)
MlirTypeID(*)() GetTypeIDFunctionTy
bool(*)(MlirType) IsAFunctionTy
static void bindDerived(ClassTy &m)
Implemented by derived classes to add methods to the Python subclass.
static constexpr GetTypeIDFunctionTy getTypeIdFunction
PyConcreteType(PyMlirContextRef contextRef, MlirType t)
static MlirType castFrom(PyType &orig)
Represents a diagnostic handler attached to the context.
void contextExit(const pybind11::object &excType, const pybind11::object &excVal, const pybind11::object &excTb)
void detach()
Detaches the handler. Does nothing if not attached.
PyDiagnosticHandler(MlirContext context, pybind11::object callback)
pybind11::object contextEnter()
Python class mirroring the C MlirDiagnostic struct.
pybind11::str getMessage()
PyDiagnostic(MlirDiagnostic diagnostic)
MlirDiagnosticSeverity getSeverity()
pybind11::tuple getNotes()
Wrapper around an MlirDialect.
PyDialectDescriptor(PyMlirContextRef contextRef, MlirDialect dialect)
Wrapper around an MlirDialectRegistry.
PyDialectRegistry(PyDialectRegistry &&other) noexcept
PyDialectRegistry(PyDialectRegistry &)=delete
static PyDialectRegistry createFromCapsule(pybind11::object capsule)
MlirDialectRegistry get() const
pybind11::object getCapsule()
PyDialectRegistry(MlirDialectRegistry registry)
User-level dialect object.
PyDialect(pybind11::object descriptor)
pybind11::object getDescriptor()
User-level object for accessing dialects with dotted syntax such as: ctx.dialect.std.
PyDialects(PyMlirContextRef contextRef)
MlirDialect getDialectForKey(const std::string &key, bool attrError)
void registerTypeCaster(MlirTypeID mlirTypeID, pybind11::function typeCaster, bool replace=false)
Adds a user-friendly type caster.
static PyGlobals & get()
Most code should get the globals via this static accessor.
An insertion point maintains a pointer to a Block and a reference operation.
static PyInsertionPoint atBlockTerminator(PyBlock &block)
Shortcut to create an insertion point before the block terminator.
PyInsertionPoint(PyBlock &block)
Creates an insertion point positioned after the last operation in the block, but still inside the blo...
static PyInsertionPoint atBlockBegin(PyBlock &block)
Shortcut to create an insertion point at the beginning of the block.
void insert(PyOperationBase &operationBase)
Inserts an operation.
void contextExit(const pybind11::object &excType, const pybind11::object &excVal, const pybind11::object &excTb)
std::optional< PyOperationRef > & getRefOperation()
pybind11::object contextEnter()
Enter and exit the context manager.
static PyIntegerSet createFromCapsule(pybind11::object capsule)
Creates a PyIntegerSet from the MlirAffineMap wrapped by a capsule.
MlirIntegerSet get() const
bool operator==(const PyIntegerSet &other) const
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirIntegerSet.
PyIntegerSet(PyMlirContextRef contextRef, MlirIntegerSet integerSet)
Wrapper around an MlirLocation.
PyLocation(PyMlirContextRef contextRef, MlirLocation loc)
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirLocation.
static PyLocation createFromCapsule(pybind11::object capsule)
Creates a PyLocation from the MlirLocation wrapped by a capsule.
void contextExit(const pybind11::object &excType, const pybind11::object &excVal, const pybind11::object &excTb)
pybind11::object contextEnter()
Enter and exit the context manager.
pybind11::object attachDiagnosticHandler(pybind11::object callback)
Attaches a Python callback as a diagnostic handler, returning a registration object (internally a PyD...
PyMlirContext(const PyMlirContext &)=delete
PyMlirContext(PyMlirContext &&)=delete
MlirContext get()
Accesses the underlying MlirContext.
PyMlirContextRef getRef()
Gets a strong reference to this context, which will ensure it is kept alive for the life of the refer...
static pybind11::object createFromCapsule(pybind11::object capsule)
Creates a PyMlirContext from the MlirContext wrapped by a capsule.
void clearOperationsInside(PyOperationBase &op)
Clears all operations nested inside the given op using clearOperation(MlirOperation).
static size_t getLiveCount()
Gets the count of live context objects. Used for testing.
void clearOperationAndInside(PyOperationBase &op)
Clears the operaiton and all operations inside using clearOperation(MlirOperation).
static PyMlirContext * createNewContextForInit()
For the case of a python init (py::init) method, pybind11 is quite strict about needing to return a p...
size_t getLiveModuleCount()
Gets the count of live modules associated with this context.
pybind11::object contextEnter()
Enter and exit the context manager.
size_t clearLiveOperations()
Clears the live operations map, returning the number of entries which were invalidated.
std::vector< PyOperation * > getLiveOperationObjects()
Get a list of Python objects which are still in the live context map.
void contextExit(const pybind11::object &excType, const pybind11::object &excVal, const pybind11::object &excTb)
void clearOperation(MlirOperation op)
Removes an operation from the live operations map and sets it invalid.
static PyMlirContextRef forContext(MlirContext context)
Returns a context reference for the singleton PyMlirContext wrapper for the given context.
size_t getLiveOperationCount()
Gets the count of live operations associated with this context.
void setEmitErrorDiagnostics(bool value)
Controls whether error diagnostics should be propagated to diagnostic handlers, instead of being capt...
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirContext.
MlirModule get()
Gets the backing MlirModule.
static PyModuleRef forModule(MlirModule module)
Returns a PyModule reference for the given MlirModule.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirModule.
static pybind11::object createFromCapsule(pybind11::object capsule)
Creates a PyModule from the MlirModule wrapped by a capsule.
PyModuleRef getRef()
Gets a strong reference to this module.
PyModule(PyModule &)=delete
PyModule(PyMlirContext &&)=delete
Represents a Python MlirNamedAttr, carrying an optional owned name.
PyNamedAttribute(MlirAttribute attr, std::string ownedName)
Constructs a PyNamedAttr that retains an owned name.
MlirNamedAttribute namedAttr
Template for a reference to a concrete type which captures a python reference to its underlying pytho...
PyObjectRef(T *referrent, pybind11::object object)
pybind11::object getObject()
pybind11::object releaseObject()
Releases the object held by this instance, returning it.
PyObjectRef(PyObjectRef &&other) noexcept
PyObjectRef(const PyObjectRef &other)
A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for providing more instance-sp...
PyOpView(const pybind11::object &operationObject)
pybind11::object getOperationObject()
static pybind11::object buildGeneric(const pybind11::object &cls, std::optional< pybind11::list > resultTypeList, pybind11::list operandList, std::optional< pybind11::dict > attributes, std::optional< std::vector< PyBlock * >> successors, std::optional< int > regions, DefaultingPyLocation location, const pybind11::object &maybeIp)
static pybind11::object constructDerived(const pybind11::object &cls, const PyOperation &operation)
Construct an instance of a class deriving from OpView, bypassing its __init__ method.
PyOperation & getOperation() override
Each must provide access to the raw Operation.
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
void print(std::optional< int64_t > largeElementsLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, bool assumeVerified, py::object fileObject, bool binary, bool skipRegions)
Implements the bound 'print' method and helps with others.
void walk(std::function< MlirWalkResult(MlirOperation)> callback, MlirWalkOrder walkOrder)
virtual PyOperation & getOperation()=0
Each must provide access to the raw Operation.
void writeBytecode(const pybind11::object &fileObject, std::optional< int64_t > bytecodeVersion)
virtual ~PyOperationBase()=default
void moveAfter(PyOperationBase &other)
Moves the operation before or after the other operation.
void moveBefore(PyOperationBase &other)
bool verify()
Verify the operation.
pybind11::object getAsm(bool binary, std::optional< int64_t > largeElementsLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, bool assumeVerified, bool skipRegions)
pybind11::object clone(const pybind11::object &ip)
Clones this operation.
void detachFromParent()
Detaches the operation from its parent block and updates its state accordingly.
void erase()
Erases the underlying MlirOperation, removes its pointer from the parent context's live operations ma...
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirOperation.
PyOperation & getOperation() override
Each must provide access to the raw Operation.
MlirOperation get() const
void setAttached(const pybind11::object &parent=pybind11::object())
static pybind11::object create(const std::string &name, std::optional< std::vector< PyType * >> results, std::optional< std::vector< PyValue * >> operands, std::optional< pybind11::dict > attributes, std::optional< std::vector< PyBlock * >> successors, int regions, DefaultingPyLocation location, const pybind11::object &ip, bool inferType)
Creates an operation. See corresponding python docstring.
pybind11::object createOpView()
Creates an OpView suitable for this operation.
static PyOperationRef forOperation(PyMlirContextRef contextRef, MlirOperation operation, pybind11::object parentKeepAlive=pybind11::object())
Returns a PyOperation for the given MlirOperation, optionally associating it with a parentKeepAlive.
std::optional< PyOperationRef > getParentOperation()
Gets the parent operation or raises an exception if the operation has no parent.
PyBlock getBlock()
Gets the owning block or raises an exception if the operation has no owning block.
static PyOperationRef createDetached(PyMlirContextRef contextRef, MlirOperation operation, pybind11::object parentKeepAlive=pybind11::object())
Creates a detached operation.
static PyOperationRef parse(PyMlirContextRef contextRef, const std::string &sourceStr, const std::string &sourceName)
Parses a source string (either text assembly or bytecode), creating a detached operation.
static pybind11::object createFromCapsule(pybind11::object capsule)
Creates a PyOperation from the MlirOperation wrapped by a capsule.
void setInvalid()
Invalidate the operation.
Wrapper around an MlirRegion.
PyRegion(PyOperationRef parentOperation, MlirRegion region)
PyOperationRef & getParentOperation()
Bindings for MLIR symbol tables.
void dunderDel(const std::string &name)
Removes the operation with the given name from the symbol table and erases it, throws if there is no ...
static void replaceAllSymbolUses(const std::string &oldSymbol, const std::string &newSymbol, PyOperationBase &from)
Replaces all symbol uses within an operation.
static void setVisibility(PyOperationBase &symbol, const std::string &visibility)
static void setSymbolName(PyOperationBase &symbol, const std::string &name)
~PySymbolTable()
Destroys the symbol table.
MlirAttribute insert(PyOperationBase &symbol)
Inserts the given operation into the symbol table.
void erase(PyOperationBase &symbol)
Removes the given operation from the symbol table and erases it.
PySymbolTable(PyOperationBase &operation)
Constructs a symbol table for the given operation.
static MlirAttribute getSymbolName(PyOperationBase &symbol)
Gets and sets the name of a symbol op.
pybind11::object dunderGetItem(const std::string &name)
Returns the symbol (opview) with the given name, throws if there is no such symbol in the table.
static MlirAttribute getVisibility(PyOperationBase &symbol)
Gets and sets the visibility of a symbol op.
static void walkSymbolTables(PyOperationBase &from, bool allSymUsesVisible, pybind11::object callback)
Walks all symbol tables under and including 'from'.
Tracks an entry in the thread context stack.
static PyThreadContextEntry * getTopOfStack()
Stack management.
static void popLocation(PyLocation &location)
PyLocation * getLocation()
PyThreadContextEntry(FrameKind frameKind, pybind11::object context, pybind11::object insertionPoint, pybind11::object location)
static pybind11::object pushContext(PyMlirContext &context)
static PyLocation * getDefaultLocation()
Gets the top of stack location and returns nullptr if not defined.
static void popInsertionPoint(PyInsertionPoint &insertionPoint)
static void popContext(PyMlirContext &context)
static PyInsertionPoint * getDefaultInsertionPoint()
Gets the top of stack insertion point and return nullptr if not defined.
static pybind11::object pushInsertionPoint(PyInsertionPoint &insertionPoint)
static pybind11::object pushLocation(PyLocation &location)
PyMlirContext * getContext()
static PyMlirContext * getDefaultContext()
Gets the top of stack context and return nullptr if not defined.
static std::vector< PyThreadContextEntry > & getStack()
Gets the thread local stack.
PyInsertionPoint * getInsertionPoint()
A TypeID provides an efficient and unique identifier for a specific C++ type.
bool operator==(const PyTypeID &other) const
static PyTypeID createFromCapsule(pybind11::object capsule)
Creates a PyTypeID from the MlirTypeID wrapped by a capsule.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirTypeID.
PyTypeID(MlirTypeID typeID)
Wrapper around the generic MlirType.
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirType.
static PyType createFromCapsule(pybind11::object capsule)
Creates a PyType from the MlirType wrapped by a capsule.
PyType(PyMlirContextRef contextRef, MlirType type)
bool operator==(const PyType &other) const
Wrapper around the generic MlirValue.
static PyValue createFromCapsule(pybind11::object capsule)
Creates a PyValue from the MlirValue wrapped by a capsule.
PyValue(PyOperationRef parentOperation, MlirValue value)
pybind11::object getCapsule()
Gets a capsule wrapping the void* within the MlirValue.
virtual ~PyValue()=default
pybind11::object maybeDownCast()
PyOperationRef & getParentOperation()
MlirDiagnosticSeverity
Severity of a diagnostic.
MLIR_CAPI_EXPORTED MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(MlirContext context, MlirDiagnosticHandler handler, void *userData, void(*deleteUserData)(void *))
Attaches the diagnostic handler to the context.
MLIR_CAPI_EXPORTED void mlirContextDetachDiagnosticHandler(MlirContext context, MlirDiagnosticHandlerID id)
Detaches an attached diagnostic handler from the context given its identifier.
uint64_t MlirDiagnosticHandlerID
Opaque identifier of a diagnostic handler, useful to detach a handler.
MlirWalkOrder
Traversal order for operation walk.
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags)
Use local scope when printing the operation.
MLIR_CAPI_EXPORTED void mlirDialectRegistryDestroy(MlirDialectRegistry registry)
Takes a dialect registry owned by the caller and destroys it.
MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute)
Gets the type of this attribute.
MLIR_CAPI_EXPORTED void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData)
Prints a location by sending chunks of the string representation and forwarding userData tocallback`.
MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, void *userData)
Prints an attribute by sending chunks of the string representation and forwarding userData tocallback...
MlirWalkResult
Operation walk result.
MLIR_CAPI_EXPORTED MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags)
Creates new AsmState, as with AsmState the IR should not be mutated in-between using this state.
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
MLIR_CAPI_EXPORTED void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
static bool mlirDialectRegistryIsNull(MlirDialectRegistry registry)
Checks if the dialect registry is null.
static bool mlirRegionIsNull(MlirRegion region)
Checks whether a region is null.
MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
Destroys printing flags created with mlirOpPrintingFlagsCreate.
MLIR_CAPI_EXPORTED MlirDialectRegistry mlirDialectRegistryCreate(void)
Creates a dialect registry and transfers its ownership to the caller.
MLIR_CAPI_EXPORTED void mlirOperationRemoveFromParent(MlirOperation op)
Removes the given operation from its parent block.
MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void)
Creates new printing flags with defaults, intended for customization.
MLIR_CAPI_EXPORTED MlirAsmState mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags)
Creates new AsmState from value.
void populateIRAttributes(pybind11::module &m)
void populateIRTypes(pybind11::module &m)
PyObjectRef< PyMlirContext > PyMlirContextRef
Wrapper around MlirContext.
void populateIRAffine(pybind11::module &m)
PyObjectRef< PyModule > PyModuleRef
void populateIRCore(pybind11::module &m)
PyObjectRef< PyOperation > PyOperationRef
void populateIRInterfaces(py::module &m)
Include the generated interface declarations.
An opaque reference to a diagnostic, always owned by the diagnostics engine (context).
A logical result value, essentially a boolean with named states.
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
Custom exception that allows access to error diagnostic information.
MLIRError(llvm::Twine message, std::vector< PyDiagnostic::DiagnosticInfo > &&errorDiagnostics={})
std::vector< PyDiagnostic::DiagnosticInfo > errorDiagnostics
Materialized diagnostic information.
MlirDiagnosticSeverity severity
std::vector< DiagnosticInfo > notes
RAII object that captures any error diagnostics emitted to the provided context.
std::vector< PyDiagnostic::DiagnosticInfo > take()
ErrorCapture(PyMlirContextRef ctx)