diff options
author | Jacques Pienaar <jpienaar@google.com> | 2024-12-18 19:31:32 +0000 |
---|---|---|
committer | Jacques Pienaar <jpienaar@google.com> | 2024-12-18 19:31:32 +0000 |
commit | 6e8b3a3e0cad614954fc387df22d59d941f081c3 (patch) | |
tree | 8c0c961d13304d67b0e7432c5affe3bc1df79fae /mlir/lib/Bindings/Python/IRModule.h | |
parent | 41bd35b58bb482fd466aa4b13aa44a810ad6470f (diff) | |
download | llvm-6e8b3a3e0cad614954fc387df22d59d941f081c3.zip llvm-6e8b3a3e0cad614954fc387df22d59d941f081c3.tar.gz llvm-6e8b3a3e0cad614954fc387df22d59d941f081c3.tar.bz2 |
Revert "[mlir python] Port Python core code to nanobind. (#118583)"
This reverts commit 41bd35b58bb482fd466aa4b13aa44a810ad6470f.
Breakage detected, rolling back.
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r-- | mlir/lib/Bindings/Python/IRModule.h | 332 |
1 files changed, 160 insertions, 172 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index a242ff2..172898c 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -10,22 +10,20 @@ #ifndef MLIR_BINDINGS_PYTHON_IRMODULES_H #define MLIR_BINDINGS_PYTHON_IRMODULES_H -#include <nanobind/nanobind.h> -#include <nanobind/stl/string.h> - #include <optional> #include <utility> #include <vector> #include "Globals.h" -#include "NanobindUtils.h" +#include "PybindUtils.h" + #include "mlir-c/AffineExpr.h" #include "mlir-c/AffineMap.h" #include "mlir-c/Diagnostics.h" #include "mlir-c/IR.h" #include "mlir-c/IntegerSet.h" #include "mlir-c/Transforms.h" -#include "mlir/Bindings/Python/NanobindAdaptors.h" +#include "mlir/Bindings/Python/PybindAdaptors.h" #include "llvm/ADT/DenseMap.h" namespace mlir { @@ -51,7 +49,7 @@ class PyValue; template <typename T> class PyObjectRef { public: - PyObjectRef(T *referrent, nanobind::object object) + PyObjectRef(T *referrent, pybind11::object object) : referrent(referrent), object(std::move(object)) { assert(this->referrent && "cannot construct PyObjectRef with null referrent"); @@ -69,13 +67,13 @@ public: int getRefCount() { if (!object) return 0; - return Py_REFCNT(object.ptr()); + return object.ref_count(); } /// Releases the object held by this instance, returning it. /// This is the proper thing to return from a function that wants to return /// the reference. Note that this does not work from initializers. - nanobind::object releaseObject() { + pybind11::object releaseObject() { assert(referrent && object); referrent = nullptr; auto stolen = std::move(object); @@ -87,7 +85,7 @@ public: assert(referrent && object); return referrent; } - nanobind::object getObject() { + pybind11::object getObject() { assert(referrent && object); return object; } @@ -95,7 +93,7 @@ public: private: T *referrent; - nanobind::object object; + pybind11::object object; }; /// Tracks an entry in the thread context stack. New entries are pushed onto @@ -114,9 +112,9 @@ public: Location, }; - PyThreadContextEntry(FrameKind frameKind, nanobind::object context, - nanobind::object insertionPoint, - nanobind::object location) + PyThreadContextEntry(FrameKind frameKind, pybind11::object context, + pybind11::object insertionPoint, + pybind11::object location) : context(std::move(context)), insertionPoint(std::move(insertionPoint)), location(std::move(location)), frameKind(frameKind) {} @@ -136,26 +134,26 @@ public: /// Stack management. static PyThreadContextEntry *getTopOfStack(); - static nanobind::object pushContext(nanobind::object context); + static pybind11::object pushContext(PyMlirContext &context); static void popContext(PyMlirContext &context); - static nanobind::object pushInsertionPoint(nanobind::object insertionPoint); + static pybind11::object pushInsertionPoint(PyInsertionPoint &insertionPoint); static void popInsertionPoint(PyInsertionPoint &insertionPoint); - static nanobind::object pushLocation(nanobind::object location); + static pybind11::object pushLocation(PyLocation &location); static void popLocation(PyLocation &location); /// Gets the thread local stack. static std::vector<PyThreadContextEntry> &getStack(); private: - static void push(FrameKind frameKind, nanobind::object context, - nanobind::object insertionPoint, nanobind::object location); + static void push(FrameKind frameKind, pybind11::object context, + pybind11::object insertionPoint, pybind11::object location); /// An object reference to the PyContext. - nanobind::object context; + pybind11::object context; /// An object reference to the current insertion point. - nanobind::object insertionPoint; + pybind11::object insertionPoint; /// An object reference to the current location. - nanobind::object location; + pybind11::object location; // The kind of push that was performed. FrameKind frameKind; }; @@ -165,15 +163,14 @@ using PyMlirContextRef = PyObjectRef<PyMlirContext>; class PyMlirContext { public: PyMlirContext() = delete; - PyMlirContext(MlirContext context); PyMlirContext(const PyMlirContext &) = delete; PyMlirContext(PyMlirContext &&) = delete; - /// For the case of a python __init__ (nanobind::init) method, pybind11 is - /// quite strict about needing to return a pointer that is not yet associated - /// to an nanobind::object. Since the forContext() method acts like a pool, - /// possibly returning a recycled context, it does not satisfy this need. The - /// usual way in python to accomplish such a thing is to override __new__, but + /// For the case of a python __init__ (py::init) method, pybind11 is quite + /// strict about needing to return a pointer that is not yet associated to + /// an py::object. Since the forContext() method acts like a pool, possibly + /// returning a recycled context, it does not satisfy this need. The usual + /// way in python to accomplish such a thing is to override __new__, but /// that is also not supported by pybind11. Instead, we use this entry /// point which always constructs a fresh context (which cannot alias an /// existing one because it is fresh). @@ -190,17 +187,17 @@ public: /// Gets a strong reference to this context, which will ensure it is kept /// alive for the life of the reference. PyMlirContextRef getRef() { - return PyMlirContextRef(this, nanobind::cast(this)); + return PyMlirContextRef(this, pybind11::cast(this)); } /// Gets a capsule wrapping the void* within the MlirContext. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyMlirContext from the MlirContext wrapped by a capsule. /// Note that PyMlirContext instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirContext /// is taken by calling this function. - static nanobind::object createFromCapsule(nanobind::object capsule); + static pybind11::object createFromCapsule(pybind11::object capsule); /// Gets the count of live context objects. Used for testing. static size_t getLiveCount(); @@ -240,14 +237,14 @@ public: size_t getLiveModuleCount(); /// Enter and exit the context manager. - static nanobind::object contextEnter(nanobind::object context); - void contextExit(const nanobind::object &excType, - const nanobind::object &excVal, - const nanobind::object &excTb); + pybind11::object contextEnter(); + void contextExit(const pybind11::object &excType, + const pybind11::object &excVal, + const pybind11::object &excTb); /// Attaches a Python callback as a diagnostic handler, returning a /// registration object (internally a PyDiagnosticHandler). - nanobind::object attachDiagnosticHandler(nanobind::object callback); + pybind11::object attachDiagnosticHandler(pybind11::object callback); /// Controls whether error diagnostics should be propagated to diagnostic /// handlers, instead of being captured by `ErrorCapture`. @@ -255,6 +252,8 @@ public: struct ErrorCapture; private: + PyMlirContext(MlirContext context); + // Interns the mapping of live MlirContext::ptr to PyMlirContext instances, // preserving the relationship that an MlirContext maps to a single // PyMlirContext wrapper. This could be replaced in the future with an @@ -269,7 +268,7 @@ private: // from this map, and while it still exists as an instance, any // attempt to access it will raise an error. using LiveModuleMap = - llvm::DenseMap<const void *, std::pair<nanobind::handle, PyModule *>>; + llvm::DenseMap<const void *, std::pair<pybind11::handle, PyModule *>>; LiveModuleMap liveModules; // Interns all live operations associated with this context. Operations @@ -277,7 +276,7 @@ private: // removed from this map, and while it still exists as an instance, any // attempt to access it will raise an error. using LiveOperationMap = - llvm::DenseMap<void *, std::pair<nanobind::handle, PyOperation *>>; + llvm::DenseMap<void *, std::pair<pybind11::handle, PyOperation *>>; LiveOperationMap liveOperations; bool emitErrorDiagnostics = false; @@ -325,19 +324,19 @@ public: MlirLocation get() const { return loc; } /// Enter and exit the context manager. - static nanobind::object contextEnter(nanobind::object location); - void contextExit(const nanobind::object &excType, - const nanobind::object &excVal, - const nanobind::object &excTb); + pybind11::object contextEnter(); + void contextExit(const pybind11::object &excType, + const pybind11::object &excVal, + const pybind11::object &excTb); /// Gets a capsule wrapping the void* within the MlirLocation. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyLocation from the MlirLocation wrapped by a capsule. /// Note that PyLocation instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirLocation /// is taken by calling this function. - static PyLocation createFromCapsule(nanobind::object capsule); + static PyLocation createFromCapsule(pybind11::object capsule); private: MlirLocation loc; @@ -354,8 +353,8 @@ public: bool isValid() { return valid; } MlirDiagnosticSeverity getSeverity(); PyLocation getLocation(); - nanobind::str getMessage(); - nanobind::tuple getNotes(); + pybind11::str getMessage(); + pybind11::tuple getNotes(); /// Materialized diagnostic information. This is safe to access outside the /// diagnostic callback. @@ -374,7 +373,7 @@ private: /// If notes have been materialized from the diagnostic, then this will /// be populated with the corresponding objects (all castable to /// PyDiagnostic). - std::optional<nanobind::tuple> materializedNotes; + std::optional<pybind11::tuple> materializedNotes; bool valid = true; }; @@ -399,7 +398,7 @@ private: /// is no way to attach an existing handler object). class PyDiagnosticHandler { public: - PyDiagnosticHandler(MlirContext context, nanobind::object callback); + PyDiagnosticHandler(MlirContext context, pybind11::object callback); ~PyDiagnosticHandler(); bool isAttached() { return registeredID.has_value(); } @@ -408,16 +407,16 @@ public: /// Detaches the handler. Does nothing if not attached. void detach(); - nanobind::object contextEnter() { return nanobind::cast(this); } - void contextExit(const nanobind::object &excType, - const nanobind::object &excVal, - const nanobind::object &excTb) { + pybind11::object contextEnter() { return pybind11::cast(this); } + void contextExit(const pybind11::object &excType, + const pybind11::object &excVal, + const pybind11::object &excTb) { detach(); } private: MlirContext context; - nanobind::object callback; + pybind11::object callback; std::optional<MlirDiagnosticHandlerID> registeredID; bool hadError = false; friend class PyMlirContext; @@ -478,12 +477,12 @@ public: /// objects of this type will be returned directly. class PyDialect { public: - PyDialect(nanobind::object descriptor) : descriptor(std::move(descriptor)) {} + PyDialect(pybind11::object descriptor) : descriptor(std::move(descriptor)) {} - nanobind::object getDescriptor() { return descriptor; } + pybind11::object getDescriptor() { return descriptor; } private: - nanobind::object descriptor; + pybind11::object descriptor; }; /// Wrapper around an MlirDialectRegistry. @@ -506,8 +505,8 @@ public: operator MlirDialectRegistry() const { return registry; } MlirDialectRegistry get() const { return registry; } - nanobind::object getCapsule(); - static PyDialectRegistry createFromCapsule(nanobind::object capsule); + pybind11::object getCapsule(); + static PyDialectRegistry createFromCapsule(pybind11::object capsule); private: MlirDialectRegistry registry; @@ -543,25 +542,26 @@ public: /// Gets a strong reference to this module. PyModuleRef getRef() { - return PyModuleRef(this, nanobind::borrow<nanobind::object>(handle)); + return PyModuleRef(this, + pybind11::reinterpret_borrow<pybind11::object>(handle)); } /// Gets a capsule wrapping the void* within the MlirModule. /// Note that the module does not (yet) provide a corresponding factory for /// constructing from a capsule as that would require uniquing PyModule /// instances, which is not currently done. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyModule from the MlirModule wrapped by a capsule. /// Note that PyModule instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirModule /// is taken by calling this function. - static nanobind::object createFromCapsule(nanobind::object capsule); + static pybind11::object createFromCapsule(pybind11::object capsule); private: PyModule(PyMlirContextRef contextRef, MlirModule module); MlirModule module; - nanobind::handle handle; + pybind11::handle handle; }; class PyAsmState; @@ -574,18 +574,18 @@ public: /// Implements the bound 'print' method and helps with others. void print(std::optional<int64_t> largeElementsLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, - bool assumeVerified, nanobind::object fileObject, bool binary, + bool assumeVerified, py::object fileObject, bool binary, bool skipRegions); - void print(PyAsmState &state, nanobind::object fileObject, bool binary); + void print(PyAsmState &state, py::object fileObject, bool binary); - nanobind::object getAsm(bool binary, + pybind11::object getAsm(bool binary, std::optional<int64_t> largeElementsLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, bool assumeVerified, bool skipRegions); // Implement the bound 'writeBytecode' method. - void writeBytecode(const nanobind::object &fileObject, + void writeBytecode(const pybind11::object &fileObject, std::optional<int64_t> bytecodeVersion); // Implement the walk method. @@ -621,13 +621,13 @@ public: /// it with a parentKeepAlive. static PyOperationRef forOperation(PyMlirContextRef contextRef, MlirOperation operation, - nanobind::object parentKeepAlive = nanobind::object()); + pybind11::object parentKeepAlive = pybind11::object()); /// Creates a detached operation. The operation must not be associated with /// any existing live operation. static PyOperationRef createDetached(PyMlirContextRef contextRef, MlirOperation operation, - nanobind::object parentKeepAlive = nanobind::object()); + pybind11::object parentKeepAlive = pybind11::object()); /// Parses a source string (either text assembly or bytecode), creating a /// detached operation. @@ -640,7 +640,7 @@ public: void detachFromParent() { mlirOperationRemoveFromParent(getOperation()); setDetached(); - parentKeepAlive = nanobind::object(); + parentKeepAlive = pybind11::object(); } /// Gets the backing operation. @@ -651,11 +651,12 @@ public: } PyOperationRef getRef() { - return PyOperationRef(this, nanobind::borrow<nanobind::object>(handle)); + return PyOperationRef( + this, pybind11::reinterpret_borrow<pybind11::object>(handle)); } bool isAttached() { return attached; } - void setAttached(const nanobind::object &parent = nanobind::object()) { + void setAttached(const pybind11::object &parent = pybind11::object()) { assert(!attached && "operation already attached"); attached = true; } @@ -674,24 +675,24 @@ public: std::optional<PyOperationRef> getParentOperation(); /// Gets a capsule wrapping the void* within the MlirOperation. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyOperation from the MlirOperation wrapped by a capsule. /// Ownership of the underlying MlirOperation is taken by calling this /// function. - static nanobind::object createFromCapsule(nanobind::object capsule); + static pybind11::object createFromCapsule(pybind11::object capsule); /// Creates an operation. See corresponding python docstring. - static nanobind::object + static pybind11::object create(const std::string &name, std::optional<std::vector<PyType *>> results, std::optional<std::vector<PyValue *>> operands, - std::optional<nanobind::dict> attributes, + std::optional<pybind11::dict> attributes, std::optional<std::vector<PyBlock *>> successors, int regions, - DefaultingPyLocation location, const nanobind::object &ip, + DefaultingPyLocation location, const pybind11::object &ip, bool inferType); /// Creates an OpView suitable for this operation. - nanobind::object createOpView(); + pybind11::object createOpView(); /// Erases the underlying MlirOperation, removes its pointer from the /// parent context's live operations map, and sets the valid bit false. @@ -701,23 +702,23 @@ public: void setInvalid() { valid = false; } /// Clones this operation. - nanobind::object clone(const nanobind::object &ip); + pybind11::object clone(const pybind11::object &ip); private: PyOperation(PyMlirContextRef contextRef, MlirOperation operation); static PyOperationRef createInstance(PyMlirContextRef contextRef, MlirOperation operation, - nanobind::object parentKeepAlive); + pybind11::object parentKeepAlive); MlirOperation operation; - nanobind::handle handle; + pybind11::handle handle; // Keeps the parent alive, regardless of whether it is an Operation or // Module. // TODO: As implemented, this facility is only sufficient for modeling the // trivial module parent back-reference. Generalize this to also account for // transitions from detached to attached and address TODOs in the // ir_operation.py regarding testing corresponding lifetime guarantees. - nanobind::object parentKeepAlive; + pybind11::object parentKeepAlive; bool attached = true; bool valid = true; @@ -732,17 +733,17 @@ private: /// python types. class PyOpView : public PyOperationBase { public: - PyOpView(const nanobind::object &operationObject); + PyOpView(const pybind11::object &operationObject); PyOperation &getOperation() override { return operation; } - nanobind::object getOperationObject() { return operationObject; } + pybind11::object getOperationObject() { return operationObject; } - static nanobind::object buildGeneric( - const nanobind::object &cls, std::optional<nanobind::list> resultTypeList, - nanobind::list operandList, std::optional<nanobind::dict> attributes, + 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 nanobind::object &maybeIp); + const pybind11::object &maybeIp); /// Construct an instance of a class deriving from OpView, bypassing its /// `__init__` method. The derived class will typically define a constructor @@ -751,12 +752,12 @@ public: /// /// The caller is responsible for verifying that `operation` is a valid /// operation to construct `cls` with. - static nanobind::object constructDerived(const nanobind::object &cls, - const nanobind::object &operation); + static pybind11::object constructDerived(const pybind11::object &cls, + const PyOperation &operation); private: PyOperation &operation; // For efficient, cast-free access from C++ - nanobind::object operationObject; // Holds the reference. + pybind11::object operationObject; // Holds the reference. }; /// Wrapper around an MlirRegion. @@ -829,7 +830,7 @@ public: void checkValid() { return parentOperation->checkValid(); } /// Gets a capsule wrapping the void* within the MlirBlock. - nanobind::object getCapsule(); + pybind11::object getCapsule(); private: PyOperationRef parentOperation; @@ -857,10 +858,10 @@ public: void insert(PyOperationBase &operationBase); /// Enter and exit the context manager. - static nanobind::object contextEnter(nanobind::object insertionPoint); - void contextExit(const nanobind::object &excType, - const nanobind::object &excVal, - const nanobind::object &excTb); + pybind11::object contextEnter(); + void contextExit(const pybind11::object &excType, + const pybind11::object &excVal, + const pybind11::object &excTb); PyBlock &getBlock() { return block; } std::optional<PyOperationRef> &getRefOperation() { return refOperation; } @@ -885,13 +886,13 @@ public: MlirType get() const { return type; } /// Gets a capsule wrapping the void* within the MlirType. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyType from the MlirType wrapped by a capsule. /// Note that PyType instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirType /// is taken by calling this function. - static PyType createFromCapsule(nanobind::object capsule); + static PyType createFromCapsule(pybind11::object capsule); private: MlirType type; @@ -911,10 +912,10 @@ public: MlirTypeID get() { return typeID; } /// Gets a capsule wrapping the void* within the MlirTypeID. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyTypeID from the MlirTypeID wrapped by a capsule. - static PyTypeID createFromCapsule(nanobind::object capsule); + static PyTypeID createFromCapsule(pybind11::object capsule); private: MlirTypeID typeID; @@ -931,7 +932,7 @@ public: // Derived classes must define statics for: // IsAFunctionTy isaFunction // const char *pyClassName - using ClassTy = nanobind::class_<DerivedTy, BaseTy>; + using ClassTy = pybind11::class_<DerivedTy, BaseTy>; using IsAFunctionTy = bool (*)(MlirType); using GetTypeIDFunctionTy = MlirTypeID (*)(); static constexpr GetTypeIDFunctionTy getTypeIdFunction = nullptr; @@ -944,38 +945,34 @@ public: static MlirType castFrom(PyType &orig) { if (!DerivedTy::isaFunction(orig)) { - auto origRepr = - nanobind::cast<std::string>(nanobind::repr(nanobind::cast(orig))); - throw nanobind::value_error((llvm::Twine("Cannot cast type to ") + - DerivedTy::pyClassName + " (from " + - origRepr + ")") - .str() - .c_str()); + auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>(); + throw py::value_error((llvm::Twine("Cannot cast type to ") + + DerivedTy::pyClassName + " (from " + origRepr + + ")") + .str()); } return orig; } - static void bind(nanobind::module_ &m) { - auto cls = ClassTy(m, DerivedTy::pyClassName); - cls.def(nanobind::init<PyType &>(), nanobind::keep_alive<0, 1>(), - nanobind::arg("cast_from_type")); + static void bind(pybind11::module &m) { + auto cls = ClassTy(m, DerivedTy::pyClassName, pybind11::module_local()); + cls.def(pybind11::init<PyType &>(), pybind11::keep_alive<0, 1>(), + pybind11::arg("cast_from_type")); cls.def_static( "isinstance", [](PyType &otherType) -> bool { return DerivedTy::isaFunction(otherType); }, - nanobind::arg("other")); - cls.def_prop_ro_static( - "static_typeid", [](nanobind::object & /*class*/) -> MlirTypeID { + pybind11::arg("other")); + cls.def_property_readonly_static( + "static_typeid", [](py::object & /*class*/) -> MlirTypeID { if (DerivedTy::getTypeIdFunction) return DerivedTy::getTypeIdFunction(); - throw nanobind::attribute_error( - (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")) - .str() - .c_str()); + throw py::attribute_error( + (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")).str()); }); - cls.def_prop_ro("typeid", [](PyType &self) { - return nanobind::cast<MlirTypeID>(nanobind::cast(self).attr("typeid")); + cls.def_property_readonly("typeid", [](PyType &self) { + return py::cast(self).attr("typeid").cast<MlirTypeID>(); }); cls.def("__repr__", [](DerivedTy &self) { PyPrintAccumulator printAccum; @@ -989,8 +986,8 @@ public: if (DerivedTy::getTypeIdFunction) { PyGlobals::get().registerTypeCaster( DerivedTy::getTypeIdFunction(), - nanobind::cast<nanobind::callable>(nanobind::cpp_function( - [](PyType pyType) -> DerivedTy { return pyType; }))); + pybind11::cpp_function( + [](PyType pyType) -> DerivedTy { return pyType; })); } DerivedTy::bindDerived(cls); @@ -1011,13 +1008,13 @@ public: MlirAttribute get() const { return attr; } /// Gets a capsule wrapping the void* within the MlirAttribute. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyAttribute from the MlirAttribute wrapped by a capsule. /// Note that PyAttribute instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirAttribute /// is taken by calling this function. - static PyAttribute createFromCapsule(nanobind::object capsule); + static PyAttribute createFromCapsule(pybind11::object capsule); private: MlirAttribute attr; @@ -1057,7 +1054,7 @@ public: // Derived classes must define statics for: // IsAFunctionTy isaFunction // const char *pyClassName - using ClassTy = nanobind::class_<DerivedTy, BaseTy>; + using ClassTy = pybind11::class_<DerivedTy, BaseTy>; using IsAFunctionTy = bool (*)(MlirAttribute); using GetTypeIDFunctionTy = MlirTypeID (*)(); static constexpr GetTypeIDFunctionTy getTypeIdFunction = nullptr; @@ -1070,45 +1067,37 @@ public: static MlirAttribute castFrom(PyAttribute &orig) { if (!DerivedTy::isaFunction(orig)) { - auto origRepr = - nanobind::cast<std::string>(nanobind::repr(nanobind::cast(orig))); - throw nanobind::value_error((llvm::Twine("Cannot cast attribute to ") + - DerivedTy::pyClassName + " (from " + - origRepr + ")") - .str() - .c_str()); + auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>(); + throw py::value_error((llvm::Twine("Cannot cast attribute to ") + + DerivedTy::pyClassName + " (from " + origRepr + + ")") + .str()); } return orig; } - static void bind(nanobind::module_ &m, PyType_Slot *slots = nullptr) { - ClassTy cls; - if (slots) { - cls = ClassTy(m, DerivedTy::pyClassName, nanobind::type_slots(slots)); - } else { - cls = ClassTy(m, DerivedTy::pyClassName); - } - cls.def(nanobind::init<PyAttribute &>(), nanobind::keep_alive<0, 1>(), - nanobind::arg("cast_from_attr")); + static void bind(pybind11::module &m) { + auto cls = ClassTy(m, DerivedTy::pyClassName, pybind11::buffer_protocol(), + pybind11::module_local()); + cls.def(pybind11::init<PyAttribute &>(), pybind11::keep_alive<0, 1>(), + pybind11::arg("cast_from_attr")); cls.def_static( "isinstance", [](PyAttribute &otherAttr) -> bool { return DerivedTy::isaFunction(otherAttr); }, - nanobind::arg("other")); - cls.def_prop_ro( + pybind11::arg("other")); + cls.def_property_readonly( "type", [](PyAttribute &attr) { return mlirAttributeGetType(attr); }); - cls.def_prop_ro_static( - "static_typeid", [](nanobind::object & /*class*/) -> MlirTypeID { + cls.def_property_readonly_static( + "static_typeid", [](py::object & /*class*/) -> MlirTypeID { if (DerivedTy::getTypeIdFunction) return DerivedTy::getTypeIdFunction(); - throw nanobind::attribute_error( - (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")) - .str() - .c_str()); + throw py::attribute_error( + (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")).str()); }); - cls.def_prop_ro("typeid", [](PyAttribute &self) { - return nanobind::cast<MlirTypeID>(nanobind::cast(self).attr("typeid")); + cls.def_property_readonly("typeid", [](PyAttribute &self) { + return py::cast(self).attr("typeid").cast<MlirTypeID>(); }); cls.def("__repr__", [](DerivedTy &self) { PyPrintAccumulator printAccum; @@ -1123,10 +1112,9 @@ public: if (DerivedTy::getTypeIdFunction) { PyGlobals::get().registerTypeCaster( DerivedTy::getTypeIdFunction(), - nanobind::cast<nanobind::callable>( - nanobind::cpp_function([](PyAttribute pyAttribute) -> DerivedTy { - return pyAttribute; - }))); + pybind11::cpp_function([](PyAttribute pyAttribute) -> DerivedTy { + return pyAttribute; + })); } DerivedTy::bindDerived(cls); @@ -1158,13 +1146,13 @@ public: void checkValid() { return parentOperation->checkValid(); } /// Gets a capsule wrapping the void* within the MlirValue. - nanobind::object getCapsule(); + pybind11::object getCapsule(); - nanobind::object maybeDownCast(); + pybind11::object maybeDownCast(); /// Creates a PyValue from the MlirValue wrapped by a capsule. Ownership of /// the underlying MlirValue is still tied to the owning operation. - static PyValue createFromCapsule(nanobind::object capsule); + static PyValue createFromCapsule(pybind11::object capsule); private: PyOperationRef parentOperation; @@ -1181,13 +1169,13 @@ public: MlirAffineExpr get() const { return affineExpr; } /// Gets a capsule wrapping the void* within the MlirAffineExpr. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyAffineExpr from the MlirAffineExpr wrapped by a capsule. /// Note that PyAffineExpr instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirAffineExpr /// is taken by calling this function. - static PyAffineExpr createFromCapsule(nanobind::object capsule); + static PyAffineExpr createFromCapsule(pybind11::object capsule); PyAffineExpr add(const PyAffineExpr &other) const; PyAffineExpr mul(const PyAffineExpr &other) const; @@ -1208,13 +1196,13 @@ public: MlirAffineMap get() const { return affineMap; } /// Gets a capsule wrapping the void* within the MlirAffineMap. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyAffineMap from the MlirAffineMap wrapped by a capsule. /// Note that PyAffineMap instances are uniqued, so the returned object /// may be a pre-existing object. Ownership of the underlying MlirAffineMap /// is taken by calling this function. - static PyAffineMap createFromCapsule(nanobind::object capsule); + static PyAffineMap createFromCapsule(pybind11::object capsule); private: MlirAffineMap affineMap; @@ -1229,12 +1217,12 @@ public: MlirIntegerSet get() const { return integerSet; } /// Gets a capsule wrapping the void* within the MlirIntegerSet. - nanobind::object getCapsule(); + pybind11::object getCapsule(); /// Creates a PyIntegerSet from the MlirAffineMap wrapped by a capsule. /// Note that PyIntegerSet instances may be uniqued, so the returned object /// may be a pre-existing object. Integer sets are owned by the context. - static PyIntegerSet createFromCapsule(nanobind::object capsule); + static PyIntegerSet createFromCapsule(pybind11::object capsule); private: MlirIntegerSet integerSet; @@ -1251,7 +1239,7 @@ public: /// Returns the symbol (opview) with the given name, throws if there is no /// such symbol in the table. - nanobind::object dunderGetItem(const std::string &name); + pybind11::object dunderGetItem(const std::string &name); /// Removes the given operation from the symbol table and erases it. void erase(PyOperationBase &symbol); @@ -1281,7 +1269,7 @@ public: /// Walks all symbol tables under and including 'from'. static void walkSymbolTables(PyOperationBase &from, bool allSymUsesVisible, - nanobind::object callback); + pybind11::object callback); /// Casts the bindings class into the C API structure. operator MlirSymbolTable() { return symbolTable; } @@ -1301,16 +1289,16 @@ struct MLIRError { std::vector<PyDiagnostic::DiagnosticInfo> errorDiagnostics; }; -void populateIRAffine(nanobind::module_ &m); -void populateIRAttributes(nanobind::module_ &m); -void populateIRCore(nanobind::module_ &m); -void populateIRInterfaces(nanobind::module_ &m); -void populateIRTypes(nanobind::module_ &m); +void populateIRAffine(pybind11::module &m); +void populateIRAttributes(pybind11::module &m); +void populateIRCore(pybind11::module &m); +void populateIRInterfaces(pybind11::module &m); +void populateIRTypes(pybind11::module &m); } // namespace python } // namespace mlir -namespace nanobind { +namespace pybind11 { namespace detail { template <> @@ -1321,6 +1309,6 @@ struct type_caster<mlir::python::DefaultingPyLocation> : MlirDefaultingCaster<mlir::python::DefaultingPyLocation> {}; } // namespace detail -} // namespace nanobind +} // namespace pybind11 #endif // MLIR_BINDINGS_PYTHON_IRMODULES_H |