diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp index 7818caf2e8a55..212228fbac91e 100644 --- a/mlir/lib/Bindings/Python/IRAttributes.cpp +++ b/mlir/lib/Bindings/Python/IRAttributes.cpp @@ -485,7 +485,7 @@ class PyArrayAttribute : public PyConcreteAttribute { PyArrayAttributeIterator &dunderIter() { return *this; } - nb::typed dunderNext() { + nb::object dunderNext() { // TODO: Throw is an inefficient way to stop iteration. if (nextIndex >= mlirArrayAttrGetNumElements(attr.get())) throw nb::stop_iteration(); diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp index 609502041f4ae..81386f2227a7f 100644 --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -513,7 +513,7 @@ class PyOperationIterator { PyOperationIterator &dunderIter() { return *this; } - nb::typed dunderNext() { + nb::object dunderNext() { parentOperation->checkValid(); if (mlirOperationIsNull(next)) { throw nb::stop_iteration(); @@ -562,7 +562,7 @@ class PyOperationList { return count; } - nb::typed dunderGetItem(intptr_t index) { + nb::object dunderGetItem(intptr_t index) { parentOperation->checkValid(); if (index < 0) { index += dunderLen(); @@ -1534,7 +1534,7 @@ nb::object PyOperation::create(std::string_view name, return created.getObject(); } -nb::typed PyOperation::clone(const nb::object &maybeIp) { +nb::object PyOperation::clone(const nb::object &maybeIp) { MlirOperation clonedOperation = mlirOperationClone(operation); PyOperationRef cloned = PyOperation::createDetached(getContext(), clonedOperation); @@ -1543,7 +1543,7 @@ nb::typed PyOperation::clone(const nb::object &maybeIp) { return cloned->createOpView(); } -nb::typed PyOperation::createOpView() { +nb::object PyOperation::createOpView() { checkValid(); MlirIdentifier ident = mlirOperationGetName(get()); MlirStringRef identStr = mlirIdentifierStr(ident); @@ -1638,9 +1638,9 @@ class PyOpResult : public PyConcreteValue { /// Returns the list of types of the values held by container. template -static std::vector> -getValueTypes(Container &container, PyMlirContextRef &context) { - std::vector> result; +static std::vector getValueTypes(Container &container, + PyMlirContextRef &context) { + std::vector result; result.reserve(container.size()); for (int i = 0, e = container.size(); i < e; ++i) { result.push_back(PyType(context->getRef(), @@ -2133,7 +2133,7 @@ PyAttribute PyAttribute::createFromCapsule(nb::object capsule) { PyMlirContext::forContext(mlirAttributeGetContext(rawAttr)), rawAttr); } -nb::typed PyAttribute::maybeDownCast() { +nb::object PyAttribute::maybeDownCast() { MlirTypeID mlirTypeID = mlirAttributeGetTypeID(this->get()); assert(!mlirTypeIDIsNull(mlirTypeID) && "mlirTypeID was expected to be non-null."); @@ -2179,7 +2179,7 @@ PyType PyType::createFromCapsule(nb::object capsule) { rawType); } -nb::typed PyType::maybeDownCast() { +nb::object PyType::maybeDownCast() { MlirTypeID mlirTypeID = mlirTypeGetTypeID(this->get()); assert(!mlirTypeIDIsNull(mlirTypeID) && "mlirTypeID was expected to be non-null."); @@ -2219,7 +2219,7 @@ nb::object PyValue::getCapsule() { return nb::steal(mlirPythonValueToCapsule(get())); } -nanobind::typed PyValue::maybeDownCast() { +nb::object PyValue::maybeDownCast() { MlirType type = mlirValueGetType(get()); MlirTypeID mlirTypeID = mlirTypeGetTypeID(type); assert(!mlirTypeIDIsNull(mlirTypeID) && @@ -2263,8 +2263,7 @@ PySymbolTable::PySymbolTable(PyOperationBase &operation) } } -nb::typed -PySymbolTable::dunderGetItem(const std::string &name) { +nb::object PySymbolTable::dunderGetItem(const std::string &name) { operation->checkValid(); MlirOperation symbol = mlirSymbolTableLookup( symbolTable, mlirStringRefCreate(name.data(), name.length())); @@ -2678,8 +2677,7 @@ class PyOpAttributeMap { PyOpAttributeMap(PyOperationRef operation) : operation(std::move(operation)) {} - nb::typed - dunderGetItemNamed(const std::string &name) { + nb::object dunderGetItemNamed(const std::string &name) { MlirAttribute attr = mlirOperationGetAttributeByName(operation->get(), toMlirStringRef(name)); if (mlirAttributeIsNull(attr)) { diff --git a/mlir/lib/Bindings/Python/IRInterfaces.cpp b/mlir/lib/Bindings/Python/IRInterfaces.cpp index 6c53289c5011e..44aad10ded082 100644 --- a/mlir/lib/Bindings/Python/IRInterfaces.cpp +++ b/mlir/lib/Bindings/Python/IRInterfaces.cpp @@ -223,7 +223,7 @@ class PyConcreteOpInterface { /// Returns the opview of the operation instance from which this object was /// constructed. Throws a type error if this object was constructed form a /// subclass of OpView. - nb::typed getOpView() { + nb::object getOpView() { if (operation == nullptr) { throw nb::type_error("Cannot get an opview from a static interface"); } diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index 414f37cc97f2a..6e97c00d478f1 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -76,7 +76,7 @@ class PyObjectRef { /// 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::typed releaseObject() { + nanobind::object releaseObject() { assert(referrent && object); referrent = nullptr; auto stolen = std::move(object); @@ -88,12 +88,14 @@ class PyObjectRef { assert(referrent && object); return referrent; } - nanobind::typed getObject() { + nanobind::object getObject() { assert(referrent && object); return object; } operator bool() const { return referrent && object; } + using NBTypedT = nanobind::typed; + private: T *referrent; nanobind::object object; @@ -680,7 +682,7 @@ class PyOperation : public PyOperationBase, public BaseContextObject { PyLocation &location, const nanobind::object &ip, bool inferType); /// Creates an OpView suitable for this operation. - nanobind::typed createOpView(); + nanobind::object createOpView(); /// Erases the underlying MlirOperation, removes its pointer from the /// parent context's live operations map, and sets the valid bit false. @@ -690,7 +692,7 @@ class PyOperation : public PyOperationBase, public BaseContextObject { void setInvalid() { valid = false; } /// Clones this operation. - nanobind::typed clone(const nanobind::object &ip); + nanobind::object clone(const nanobind::object &ip); PyOperation(PyMlirContextRef contextRef, MlirOperation operation); @@ -890,7 +892,7 @@ class PyType : public BaseContextObject { /// is taken by calling this function. static PyType createFromCapsule(nanobind::object capsule); - nanobind::typed maybeDownCast(); + nanobind::object maybeDownCast(); private: MlirType type; @@ -1020,7 +1022,7 @@ class PyAttribute : public BaseContextObject { /// is taken by calling this function. static PyAttribute createFromCapsule(nanobind::object capsule); - nanobind::typed maybeDownCast(); + nanobind::object maybeDownCast(); private: MlirAttribute attr; @@ -1178,7 +1180,7 @@ class PyValue { /// Gets a capsule wrapping the void* within the MlirValue. nanobind::object getCapsule(); - nanobind::typed maybeDownCast(); + nanobind::object maybeDownCast(); /// Creates a PyValue from the MlirValue wrapped by a capsule. Ownership of /// the underlying MlirValue is still tied to the owning operation. @@ -1269,8 +1271,7 @@ class PySymbolTable { /// Returns the symbol (opview) with the given name, throws if there is no /// such symbol in the table. - nanobind::typed - dunderGetItem(const std::string &name); + nanobind::object dunderGetItem(const std::string &name); /// Removes the given operation from the symbol table and erases it. void erase(PyOperationBase &symbol); diff --git a/mlir/lib/Bindings/Python/IRTypes.cpp b/mlir/lib/Bindings/Python/IRTypes.cpp index 09ef64d4e0baf..a7aa1c65c6c43 100644 --- a/mlir/lib/Bindings/Python/IRTypes.cpp +++ b/mlir/lib/Bindings/Python/IRTypes.cpp @@ -731,7 +731,8 @@ class PyRankedTensorType MlirAttribute encoding = mlirRankedTensorTypeGetEncoding(self.get()); if (mlirAttributeIsNull(encoding)) return std::nullopt; - return PyAttribute(self.getContext(), encoding).maybeDownCast(); + return nb::cast>( + PyAttribute(self.getContext(), encoding).maybeDownCast()); }); } }; @@ -793,9 +794,9 @@ class PyMemRefType : public PyConcreteType { .def_prop_ro( "layout", [](PyMemRefType &self) -> nb::typed { - return PyAttribute(self.getContext(), - mlirMemRefTypeGetLayout(self)) - .maybeDownCast(); + return nb::cast>( + PyAttribute(self.getContext(), mlirMemRefTypeGetLayout(self)) + .maybeDownCast()); }, "The layout of the MemRef type.") .def( @@ -824,7 +825,8 @@ class PyMemRefType : public PyConcreteType { MlirAttribute a = mlirMemRefTypeGetMemorySpace(self); if (mlirAttributeIsNull(a)) return std::nullopt; - return PyAttribute(self.getContext(), a).maybeDownCast(); + return nb::cast>( + PyAttribute(self.getContext(), a).maybeDownCast()); }, "Returns the memory space of the given MemRef type."); } @@ -865,7 +867,8 @@ class PyUnrankedMemRefType MlirAttribute a = mlirUnrankedMemrefGetMemorySpace(self); if (mlirAttributeIsNull(a)) return std::nullopt; - return PyAttribute(self.getContext(), a).maybeDownCast(); + return nb::cast>( + PyAttribute(self.getContext(), a).maybeDownCast()); }, "Returns the memory space of the given Unranked MemRef type."); } diff --git a/mlir/lib/Bindings/Python/NanobindUtils.h b/mlir/lib/Bindings/Python/NanobindUtils.h index 40b3215f6f5fe..64ea4329f65f1 100644 --- a/mlir/lib/Bindings/Python/NanobindUtils.h +++ b/mlir/lib/Bindings/Python/NanobindUtils.h @@ -276,7 +276,7 @@ class Sliceable { /// Returns the element at the given slice index. Supports negative indices /// by taking elements in inverse order. Returns a nullptr object if out /// of bounds. - nanobind::typed getItem(intptr_t index) { + nanobind::object getItem(intptr_t index) { // Negative indices mean we count from the end. index = wrapIndex(index); if (index < 0) { diff --git a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel index a6e8c515f0874..3daf9290921e7 100644 --- a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel @@ -53,14 +53,6 @@ filegroup( ]), ) -filegroup( - name = "IRPyIFiles", - srcs = [ - "mlir/_mlir_libs/_mlir/__init__.pyi", - "mlir/_mlir_libs/_mlir/ir.pyi", - ], -) - filegroup( name = "MlirLibsPyFiles", srcs = [ @@ -75,13 +67,6 @@ filegroup( ], ) -filegroup( - name = "PassManagerPyIFiles", - srcs = [ - "mlir/_mlir_libs/_mlir/passmanager.pyi", - ], -) - filegroup( name = "RewritePyFiles", srcs = [