[Mlir-commits] [mlir] [MLIR][Python] Make the TypeID allocator globally defined in `PassManager.add` (PR #162594)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Oct 8 23:12:21 PDT 2025


https://github.com/PragmaTwice updated https://github.com/llvm/llvm-project/pull/162594

>From 7b4be6c4d2a52d2a7b1d1786044c13b25a232e22 Mon Sep 17 00:00:00 2001
From: PragmaTwice <twice at apache.org>
Date: Thu, 9 Oct 2025 11:52:45 +0800
Subject: [PATCH 1/2] [MLIR][Python] Make the TypeID allocator global defined
 in PassManager.add

---
 mlir/lib/Bindings/Python/Pass.cpp | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Bindings/Python/Pass.cpp b/mlir/lib/Bindings/Python/Pass.cpp
index e489585fd5f50..e371a8cf76a9d 100644
--- a/mlir/lib/Bindings/Python/Pass.cpp
+++ b/mlir/lib/Bindings/Python/Pass.cpp
@@ -52,6 +52,23 @@ class PyPassManager {
   MlirPassManager passManager;
 };
 
+class PyTypeIDAllocator {
+public:
+  PyTypeIDAllocator() : allocator(mlirTypeIDAllocatorCreate()) {}
+  ~PyTypeIDAllocator() {
+    if (!allocator.ptr)
+      mlirTypeIDAllocatorDestroy(allocator);
+  }
+
+  MlirTypeIDAllocator get() { return allocator; }
+  MlirTypeID allocate() { return mlirTypeIDAllocatorAllocateTypeID(allocator); }
+
+private:
+  MlirTypeIDAllocator allocator;
+};
+
+PyTypeIDAllocator globalTypeIDAllocator;
+
 } // namespace
 
 /// Create the `mlir.passmanager` here.
@@ -181,9 +198,7 @@ void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
               name = nb::cast<std::string>(
                   nb::borrow<nb::str>(run.attr("__name__")));
             }
-            MlirTypeIDAllocator typeIDAllocator = mlirTypeIDAllocatorCreate();
-            MlirTypeID passID =
-                mlirTypeIDAllocatorAllocateTypeID(typeIDAllocator);
+            MlirTypeID passID = globalTypeIDAllocator.allocate();
             MlirExternalPassCallbacks callbacks;
             callbacks.construct = [](void *obj) {
               (void)nb::handle(static_cast<PyObject *>(obj)).inc_ref();

>From 3b3c3f6b4f85eda3f8b1f0fb0659202a83ce8204 Mon Sep 17 00:00:00 2001
From: PragmaTwice <twice at apache.org>
Date: Thu, 9 Oct 2025 14:11:31 +0800
Subject: [PATCH 2/2] move to PyGlobals

---
 mlir/lib/Bindings/Python/Globals.h | 20 ++++++++++++++++++++
 mlir/lib/Bindings/Python/Pass.cpp  | 20 ++------------------
 2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/mlir/lib/Bindings/Python/Globals.h b/mlir/lib/Bindings/Python/Globals.h
index 71a051cb3d9f5..6b6b4b598e366 100644
--- a/mlir/lib/Bindings/Python/Globals.h
+++ b/mlir/lib/Bindings/Python/Globals.h
@@ -151,6 +151,25 @@ class PyGlobals {
 
   TracebackLoc &getTracebackLoc() { return tracebackLoc; }
 
+  class TypeIDAllocator {
+  public:
+    TypeIDAllocator() : allocator(mlirTypeIDAllocatorCreate()) {}
+    ~TypeIDAllocator() {
+      if (!allocator.ptr)
+        mlirTypeIDAllocatorDestroy(allocator);
+    }
+
+    MlirTypeIDAllocator get() { return allocator; }
+    MlirTypeID allocate() {
+      return mlirTypeIDAllocatorAllocateTypeID(allocator);
+    }
+
+  private:
+    MlirTypeIDAllocator allocator;
+  };
+
+  MlirTypeID allocateTypeID() { return typeIDAllocator.allocate(); }
+
 private:
   static PyGlobals *instance;
 
@@ -173,6 +192,7 @@ class PyGlobals {
   llvm::StringSet<> loadedDialectModules;
 
   TracebackLoc tracebackLoc;
+  TypeIDAllocator typeIDAllocator;
 };
 
 } // namespace python
diff --git a/mlir/lib/Bindings/Python/Pass.cpp b/mlir/lib/Bindings/Python/Pass.cpp
index e371a8cf76a9d..3c9f72a9d20d2 100644
--- a/mlir/lib/Bindings/Python/Pass.cpp
+++ b/mlir/lib/Bindings/Python/Pass.cpp
@@ -8,6 +8,7 @@
 
 #include "Pass.h"
 
+#include "Globals.h"
 #include "IRModule.h"
 #include "mlir-c/Pass.h"
 // clang-format off
@@ -52,23 +53,6 @@ class PyPassManager {
   MlirPassManager passManager;
 };
 
-class PyTypeIDAllocator {
-public:
-  PyTypeIDAllocator() : allocator(mlirTypeIDAllocatorCreate()) {}
-  ~PyTypeIDAllocator() {
-    if (!allocator.ptr)
-      mlirTypeIDAllocatorDestroy(allocator);
-  }
-
-  MlirTypeIDAllocator get() { return allocator; }
-  MlirTypeID allocate() { return mlirTypeIDAllocatorAllocateTypeID(allocator); }
-
-private:
-  MlirTypeIDAllocator allocator;
-};
-
-PyTypeIDAllocator globalTypeIDAllocator;
-
 } // namespace
 
 /// Create the `mlir.passmanager` here.
@@ -198,7 +182,7 @@ void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
               name = nb::cast<std::string>(
                   nb::borrow<nb::str>(run.attr("__name__")));
             }
-            MlirTypeID passID = globalTypeIDAllocator.allocate();
+            MlirTypeID passID = PyGlobals::get().allocateTypeID();
             MlirExternalPassCallbacks callbacks;
             callbacks.construct = [](void *obj) {
               (void)nb::handle(static_cast<PyObject *>(obj)).inc_ref();



More information about the Mlir-commits mailing list