diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake index 97873d0c07ab..54c59f41404b 100644 --- a/mlir/cmake/modules/AddMLIRPython.cmake +++ b/mlir/cmake/modules/AddMLIRPython.cmake @@ -474,7 +474,10 @@ function(add_mlir_python_modules name) MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN} _PRIVATE_SUPPORT_LIB LINK_LIBS PRIVATE - LLVMSupport + # LLVMSupport is intentionally removed to avoid introducing an LLVM dependency + # for the mlir-python bindings. Do not add new dependencies on the C++ LLVM/MLIR + # libraries; use the C++ standard library instead, or wrap LLVM functionality in + # the C API first. ${sources_target} ${ARG_COMMON_CAPI_LINK_LIBS} ) diff --git a/mlir/include/mlir-c/Support.h b/mlir/include/mlir-c/Support.h index 26da8eee022c..50cb65e0ac7f 100644 --- a/mlir/include/mlir-c/Support.h +++ b/mlir/include/mlir-c/Support.h @@ -156,6 +156,10 @@ MLIR_CAPI_EXPORTED MlirLlvmThreadPool mlirLlvmThreadPoolCreate(void); /// Destroy an LLVM thread pool. MLIR_CAPI_EXPORTED void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool pool); +/// Returns the maximum number of threads in the thread pool. +MLIR_CAPI_EXPORTED int +mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool pool); + //===----------------------------------------------------------------------===// // MlirLlvmRawFdOStream. //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Bindings/Python/Globals.h b/mlir/include/mlir/Bindings/Python/Globals.h index 23cccdd36279..33e1130e6979 100644 --- a/mlir/include/mlir/Bindings/Python/Globals.h +++ b/mlir/include/mlir/Bindings/Python/Globals.h @@ -22,9 +22,6 @@ #include "mlir/Bindings/Python/NanobindUtils.h" #include "mlir/CAPI/Support.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/Support/Regex.h" - namespace mlir { namespace python { namespace MLIR_BINDINGS_PYTHON_DOMAIN { diff --git a/mlir/include/mlir/Bindings/Python/IRCore.h b/mlir/include/mlir/Bindings/Python/IRCore.h index e551a49bb34a..0c7431875a5b 100644 --- a/mlir/include/mlir/Bindings/Python/IRCore.h +++ b/mlir/include/mlir/Bindings/Python/IRCore.h @@ -31,8 +31,6 @@ #include "mlir/Bindings/Python/Nanobind.h" #include "mlir/Bindings/Python/NanobindAdaptors.h" -#include "llvm/Support/ThreadPool.h" - namespace mlir { namespace python { namespace MLIR_BINDINGS_PYTHON_DOMAIN { @@ -183,16 +181,17 @@ private: class MLIR_PYTHON_API_EXPORTED PyThreadPool { public: PyThreadPool(); + ~PyThreadPool(); PyThreadPool(const PyThreadPool &) = delete; PyThreadPool(PyThreadPool &&) = delete; - int getMaxConcurrency() const { return ownedThreadPool->getMaxConcurrency(); } - MlirLlvmThreadPool get() { return wrap(ownedThreadPool.get()); } + int getMaxConcurrency() const; + MlirLlvmThreadPool get() { return threadPool; } std::string _mlir_thread_pool_ptr() const; private: - std::unique_ptr ownedThreadPool; + MlirLlvmThreadPool threadPool; }; /// Wrapper around MlirContext. diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp index bffe5da45f6d..f61180cdbc34 100644 --- a/mlir/lib/Bindings/Python/IRCore.cpp +++ b/mlir/lib/Bindings/Python/IRCore.cpp @@ -20,6 +20,7 @@ #include "mlir-c/IR.h" #include "mlir-c/Support.h" +#include #include #include #include @@ -440,13 +441,20 @@ void PyOpOperandIterator::bind(nb::module_ &m) { // PyThreadPool //------------------------------------------------------------------------------ -PyThreadPool::PyThreadPool() { - ownedThreadPool = std::make_unique(); +PyThreadPool::PyThreadPool() { threadPool = mlirLlvmThreadPoolCreate(); } + +PyThreadPool::~PyThreadPool() { + if (threadPool.ptr) + mlirLlvmThreadPoolDestroy(threadPool); +} + +int PyThreadPool::getMaxConcurrency() const { + return mlirLlvmThreadPoolGetMaxConcurrency(threadPool); } std::string PyThreadPool::_mlir_thread_pool_ptr() const { std::stringstream ss; - ss << ownedThreadPool.get(); + ss << threadPool.ptr; return ss.str(); } diff --git a/mlir/lib/CAPI/IR/Support.cpp b/mlir/lib/CAPI/IR/Support.cpp index 3a41fb2a00a8..343f718ce7e7 100644 --- a/mlir/lib/CAPI/IR/Support.cpp +++ b/mlir/lib/CAPI/IR/Support.cpp @@ -35,6 +35,10 @@ void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool threadPool) { delete unwrap(threadPool); } +int mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool threadPool) { + return unwrap(threadPool)->getMaxConcurrency(); +} + //===----------------------------------------------------------------------===// // LLVM raw_fd_ostream API. //===----------------------------------------------------------------------===//