From f05ff4f7570ca009f22f5e2fe8c6361f28faaa8a Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Mon, 23 Aug 2021 20:01:07 -0700 Subject: [PATCH] [mlir][python] Apply py::module_local() to all classes. * This allows multiple MLIR-API embedding downstreams to co-exist in the same process. * I believe this is the last thing needed to enable isolated embedding. Differential Revision: https://reviews.llvm.org/D108605 --- mlir/lib/Bindings/Python/ExecutionEngineModule.cpp | 2 +- mlir/lib/Bindings/Python/IRAffine.cpp | 11 ++--- mlir/lib/Bindings/Python/IRAttributes.cpp | 3 +- mlir/lib/Bindings/Python/IRCore.cpp | 52 +++++++++++----------- mlir/lib/Bindings/Python/MainModule.cpp | 2 +- mlir/lib/Bindings/Python/Pass.cpp | 2 +- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp index 510e3f8..765ff28 100644 --- a/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp +++ b/mlir/lib/Bindings/Python/ExecutionEngineModule.cpp @@ -58,7 +58,7 @@ PYBIND11_MODULE(_mlirExecutionEngine, m) { //---------------------------------------------------------------------------- // Mapping of the top-level PassManager //---------------------------------------------------------------------------- - py::class_(m, "ExecutionEngine") + py::class_(m, "ExecutionEngine", py::module_local()) .def(py::init<>([](MlirModule module, int optLevel, const std::vector &sharedLibPaths) { llvm::SmallVector libPaths; diff --git a/mlir/lib/Bindings/Python/IRAffine.cpp b/mlir/lib/Bindings/Python/IRAffine.cpp index 0a2a566..5314bad 100644 --- a/mlir/lib/Bindings/Python/IRAffine.cpp +++ b/mlir/lib/Bindings/Python/IRAffine.cpp @@ -97,7 +97,7 @@ public: } static void bind(py::module &m) { - auto cls = ClassTy(m, DerivedTy::pyClassName); + auto cls = ClassTy(m, DerivedTy::pyClassName, py::module_local()); cls.def(py::init()); DerivedTy::bindDerived(cls); } @@ -367,7 +367,8 @@ public: bool isEq() { return mlirIntegerSetIsConstraintEq(set, pos); } static void bind(py::module &m) { - py::class_(m, "IntegerSetConstraint") + py::class_(m, "IntegerSetConstraint", + py::module_local()) .def_property_readonly("expr", &PyIntegerSetConstraint::getExpr) .def_property_readonly("is_eq", &PyIntegerSetConstraint::isEq); } @@ -427,7 +428,7 @@ void mlir::python::populateIRAffine(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyAffineExpr and derived classes. //---------------------------------------------------------------------------- - py::class_(m, "AffineExpr") + py::class_(m, "AffineExpr", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyAffineExpr::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyAffineExpr::createFromCapsule) @@ -515,7 +516,7 @@ void mlir::python::populateIRAffine(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyAffineMap. //---------------------------------------------------------------------------- - py::class_(m, "AffineMap") + py::class_(m, "AffineMap", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyAffineMap::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyAffineMap::createFromCapsule) @@ -686,7 +687,7 @@ void mlir::python::populateIRAffine(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyIntegerSet. //---------------------------------------------------------------------------- - py::class_(m, "IntegerSet") + py::class_(m, "IntegerSet", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyIntegerSet::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyIntegerSet::createFromCapsule) diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp index 0af762d..bb4b5f4 100644 --- a/mlir/lib/Bindings/Python/IRAttributes.cpp +++ b/mlir/lib/Bindings/Python/IRAttributes.cpp @@ -65,7 +65,8 @@ public: } static void bind(py::module &m) { - py::class_(m, "ArrayAttributeIterator") + py::class_(m, "ArrayAttributeIterator", + py::module_local()) .def("__iter__", &PyArrayAttributeIterator::dunderIter) .def("__next__", &PyArrayAttributeIterator::dunderNext); } diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp index 7add4eb..8672b77 100644 --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -163,7 +163,7 @@ struct PyGlobalDebugFlag { static void bind(py::module &m) { // Debug flags. - py::class_(m, "_GlobalDebug") + py::class_(m, "_GlobalDebug", py::module_local()) .def_property_static("flag", &PyGlobalDebugFlag::get, &PyGlobalDebugFlag::set, "LLVM-wide debug flag"); } @@ -192,7 +192,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "RegionIterator") + py::class_(m, "RegionIterator", py::module_local()) .def("__iter__", &PyRegionIterator::dunderIter) .def("__next__", &PyRegionIterator::dunderNext); } @@ -224,7 +224,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "RegionSequence") + py::class_(m, "RegionSequence", py::module_local()) .def("__len__", &PyRegionList::dunderLen) .def("__getitem__", &PyRegionList::dunderGetItem); } @@ -252,7 +252,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "BlockIterator") + py::class_(m, "BlockIterator", py::module_local()) .def("__iter__", &PyBlockIterator::dunderIter) .def("__next__", &PyBlockIterator::dunderNext); } @@ -317,7 +317,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "BlockList") + py::class_(m, "BlockList", py::module_local()) .def("__getitem__", &PyBlockList::dunderGetItem) .def("__iter__", &PyBlockList::dunderIter) .def("__len__", &PyBlockList::dunderLen) @@ -349,7 +349,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "OperationIterator") + py::class_(m, "OperationIterator", py::module_local()) .def("__iter__", &PyOperationIterator::dunderIter) .def("__next__", &PyOperationIterator::dunderNext); } @@ -405,7 +405,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "OperationList") + py::class_(m, "OperationList", py::module_local()) .def("__getitem__", &PyOperationList::dunderGetItem) .def("__iter__", &PyOperationList::dunderIter) .def("__len__", &PyOperationList::dunderLen); @@ -1539,7 +1539,7 @@ public: /// Binds the Python module objects to functions of this class. static void bind(py::module &m) { - auto cls = ClassTy(m, DerivedTy::pyClassName); + auto cls = ClassTy(m, DerivedTy::pyClassName, py::module_local()); cls.def(py::init(), py::keep_alive<0, 1>()); DerivedTy::bindDerived(cls); } @@ -1617,7 +1617,7 @@ public: /// Defines a Python class in the bindings. static void bind(py::module &m) { - py::class_(m, "BlockArgumentList") + py::class_(m, "BlockArgumentList", py::module_local()) .def("__len__", &PyBlockArgumentList::dunderLen) .def("__getitem__", &PyBlockArgumentList::dunderGetItem); } @@ -1764,7 +1764,7 @@ public: } static void bind(py::module &m) { - py::class_(m, "OpAttributeMap") + py::class_(m, "OpAttributeMap", py::module_local()) .def("__contains__", &PyOpAttributeMap::dunderContains) .def("__len__", &PyOpAttributeMap::dunderLen) .def("__getitem__", &PyOpAttributeMap::dunderGetItemNamed) @@ -1787,7 +1787,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of MlirContext. //---------------------------------------------------------------------------- - py::class_(m, "Context") + py::class_(m, "Context", py::module_local()) .def(py::init<>(&PyMlirContext::createNewContextForInit)) .def_static("_get_live_count", &PyMlirContext::getLiveCount) .def("_get_context_again", @@ -1851,7 +1851,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyDialectDescriptor //---------------------------------------------------------------------------- - py::class_(m, "DialectDescriptor") + py::class_(m, "DialectDescriptor", py::module_local()) .def_property_readonly("namespace", [](PyDialectDescriptor &self) { MlirStringRef ns = @@ -1869,7 +1869,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyDialects //---------------------------------------------------------------------------- - py::class_(m, "Dialects") + py::class_(m, "Dialects", py::module_local()) .def("__getitem__", [=](PyDialects &self, std::string keyName) { MlirDialect dialect = @@ -1889,7 +1889,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyDialect //---------------------------------------------------------------------------- - py::class_(m, "Dialect") + py::class_(m, "Dialect", py::module_local()) .def(py::init(), "descriptor") .def_property_readonly( "descriptor", [](PyDialect &self) { return self.getDescriptor(); }) @@ -1904,7 +1904,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of Location //---------------------------------------------------------------------------- - py::class_(m, "Location") + py::class_(m, "Location", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyLocation::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyLocation::createFromCapsule) .def("__enter__", &PyLocation::contextEnter) @@ -1956,7 +1956,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of Module //---------------------------------------------------------------------------- - py::class_(m, "Module") + py::class_(m, "Module", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyModule::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyModule::createFromCapsule) .def_static( @@ -2025,7 +2025,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of Operation. //---------------------------------------------------------------------------- - py::class_(m, "_OperationBase") + py::class_(m, "_OperationBase", py::module_local()) .def("__eq__", [](PyOperationBase &self, PyOperationBase &other) { return &self.getOperation() == &other.getOperation(); @@ -2112,7 +2112,7 @@ void mlir::python::populateIRCore(py::module &m) { "Verify the operation and return true if it passes, false if it " "fails."); - py::class_(m, "Operation") + py::class_(m, "Operation", py::module_local()) .def_static("create", &PyOperation::create, py::arg("name"), py::arg("results") = py::none(), py::arg("operands") = py::none(), @@ -2149,7 +2149,7 @@ void mlir::python::populateIRCore(py::module &m) { .def_property_readonly("opview", &PyOperation::createOpView); auto opViewClass = - py::class_(m, "OpView") + py::class_(m, "OpView", py::module_local()) .def(py::init()) .def_property_readonly("operation", &PyOpView::getOperationObject) .def_property_readonly( @@ -2174,7 +2174,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyRegion. //---------------------------------------------------------------------------- - py::class_(m, "Region") + py::class_(m, "Region", py::module_local()) .def_property_readonly( "blocks", [](PyRegion &self) { @@ -2198,7 +2198,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyBlock. //---------------------------------------------------------------------------- - py::class_(m, "Block") + py::class_(m, "Block", py::module_local()) .def_property_readonly( "owner", [](PyBlock &self) { @@ -2288,7 +2288,7 @@ void mlir::python::populateIRCore(py::module &m) { // Mapping of PyInsertionPoint. //---------------------------------------------------------------------------- - py::class_(m, "InsertionPoint") + py::class_(m, "InsertionPoint", py::module_local()) .def(py::init(), py::arg("block"), "Inserts after the last operation but still inside the block.") .def("__enter__", &PyInsertionPoint::contextEnter) @@ -2318,7 +2318,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyAttribute. //---------------------------------------------------------------------------- - py::class_(m, "Attribute") + py::class_(m, "Attribute", py::module_local()) // Delegate to the PyAttribute copy constructor, which will also lifetime // extend the backing context which owns the MlirAttribute. .def(py::init(), py::arg("cast_from_type"), @@ -2389,7 +2389,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyNamedAttribute //---------------------------------------------------------------------------- - py::class_(m, "NamedAttribute") + py::class_(m, "NamedAttribute", py::module_local()) .def("__repr__", [](PyNamedAttribute &self) { PyPrintAccumulator printAccum; @@ -2425,7 +2425,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of PyType. //---------------------------------------------------------------------------- - py::class_(m, "Type") + py::class_(m, "Type", py::module_local()) // Delegate to the PyType copy constructor, which will also lifetime // extend the backing context which owns the MlirType. .def(py::init(), py::arg("cast_from_type"), @@ -2479,7 +2479,7 @@ void mlir::python::populateIRCore(py::module &m) { //---------------------------------------------------------------------------- // Mapping of Value. //---------------------------------------------------------------------------- - py::class_(m, "Value") + py::class_(m, "Value", py::module_local()) .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyValue::getCapsule) .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyValue::createFromCapsule) .def_property_readonly( diff --git a/mlir/lib/Bindings/Python/MainModule.cpp b/mlir/lib/Bindings/Python/MainModule.cpp index 073ac90..cbade53 100644 --- a/mlir/lib/Bindings/Python/MainModule.cpp +++ b/mlir/lib/Bindings/Python/MainModule.cpp @@ -26,7 +26,7 @@ using namespace mlir::python; PYBIND11_MODULE(_mlir, m) { m.doc() = "MLIR Python Native Extension"; - py::class_(m, "_Globals") + py::class_(m, "_Globals", py::module_local()) .def_property("dialect_search_modules", &PyGlobals::getDialectSearchPrefixes, &PyGlobals::setDialectSearchPrefixes) diff --git a/mlir/lib/Bindings/Python/Pass.cpp b/mlir/lib/Bindings/Python/Pass.cpp index f2433573..6aa1c65 100644 --- a/mlir/lib/Bindings/Python/Pass.cpp +++ b/mlir/lib/Bindings/Python/Pass.cpp @@ -55,7 +55,7 @@ void mlir::python::populatePassManagerSubmodule(py::module &m) { //---------------------------------------------------------------------------- // Mapping of the top-level PassManager //---------------------------------------------------------------------------- - py::class_(m, "PassManager") + py::class_(m, "PassManager", py::module_local()) .def(py::init<>([](DefaultingPyMlirContext context) { MlirPassManager passManager = mlirPassManagerCreate(context->get()); -- 2.7.4