[Mlir-commits] [mlir] [MLIR][Python] Remove partial LLVM APIs in python bindings (6/6) (PR #180986)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Feb 11 20:04:31 PST 2026


https://github.com/RattataKing updated https://github.com/llvm/llvm-project/pull/180986

>From 4e26bf66b789b6b4f05a9137c42f154944a829e3 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Wed, 11 Feb 2026 18:21:38 +0000
Subject: [PATCH 1/5] Remove llvm support from mlir cmake

---
 mlir/cmake/modules/AddMLIRPython.cmake | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 97873d0c07ab8..8d28999585787 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -474,7 +474,6 @@ function(add_mlir_python_modules name)
         MLIR_BINDINGS_PYTHON_NB_DOMAIN ${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
         _PRIVATE_SUPPORT_LIB
         LINK_LIBS PRIVATE
-          LLVMSupport
           ${sources_target}
           ${ARG_COMMON_CAPI_LINK_LIBS}
       )

>From 3c77334509a5f3deadbda8a8d63cd185f0d4b471 Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Wed, 11 Feb 2026 19:54:15 +0000
Subject: [PATCH 2/5] Use capi for threadpool

---
 mlir/include/mlir-c/Support.h               |  4 ++++
 mlir/include/mlir/Bindings/Python/Globals.h |  3 ---
 mlir/include/mlir/Bindings/Python/IRCore.h  |  9 ++++-----
 mlir/lib/Bindings/Python/IRCore.cpp         | 13 +++++++++++--
 mlir/lib/CAPI/IR/Support.cpp                |  4 ++++
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/mlir/include/mlir-c/Support.h b/mlir/include/mlir-c/Support.h
index 26da8eee022c5..50cb65e0ac7f5 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 23cccdd36279a..33e1130e6979d 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 e551a49bb34a8..0c7431875a5b4 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 @@ class MLIR_PYTHON_API_EXPORTED PyThreadContextEntry {
 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<llvm::ThreadPoolInterface> ownedThreadPool;
+  MlirLlvmThreadPool threadPool;
 };
 
 /// Wrapper around MlirContext.
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index bffe5da45f6dc..94db3509d406a 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -441,12 +441,21 @@ void PyOpOperandIterator::bind(nb::module_ &m) {
 //------------------------------------------------------------------------------
 
 PyThreadPool::PyThreadPool() {
-  ownedThreadPool = std::make_unique<llvm::DefaultThreadPool>();
+  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 3a41fb2a00a85..343f718ce7e7e 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.
 //===----------------------------------------------------------------------===//

>From a01506dc12c19f3907b41287832807f57de6bdff Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Wed, 11 Feb 2026 20:08:45 +0000
Subject: [PATCH 3/5] Apply clang-format to PyThreadPool constructor

---
 mlir/lib/Bindings/Python/IRCore.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 94db3509d406a..9a0e496122f4e 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -440,9 +440,7 @@ void PyOpOperandIterator::bind(nb::module_ &m) {
 // PyThreadPool
 //------------------------------------------------------------------------------
 
-PyThreadPool::PyThreadPool() {
-  threadPool = mlirLlvmThreadPoolCreate();
-}
+PyThreadPool::PyThreadPool() { threadPool = mlirLlvmThreadPoolCreate(); }
 
 PyThreadPool::~PyThreadPool() {
   if (threadPool.ptr)

>From e0fc907950a0eca5af7fcf69c91ba8658805decd Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Wed, 11 Feb 2026 20:09:38 +0000
Subject: [PATCH 4/5] Add missing lib

---
 mlir/lib/Bindings/Python/IRCore.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 9a0e496122f4e..f61180cdbc34d 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 <array>
 #include <functional>
 #include <optional>
 #include <string>

>From cdf1ec185d80068f14542173247acba137ae175c Mon Sep 17 00:00:00 2001
From: Amily Wu <amilywu2 at amd.com>
Date: Thu, 12 Feb 2026 04:04:17 +0000
Subject: [PATCH 5/5] Add reminder comment

---
 mlir/cmake/modules/AddMLIRPython.cmake | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mlir/cmake/modules/AddMLIRPython.cmake b/mlir/cmake/modules/AddMLIRPython.cmake
index 8d28999585787..54c59f41404b7 100644
--- a/mlir/cmake/modules/AddMLIRPython.cmake
+++ b/mlir/cmake/modules/AddMLIRPython.cmake
@@ -474,6 +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 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}
       )



More information about the Mlir-commits mailing list