Skip to content

Conversation

PragmaTwice
Copy link
Member

Previously, each time we called PassManager.add(python_pass_callable), a new TypeID allocator was created and never released afterward. This approach could potentially lead to some issues. In this PR, we introduce a global TypeIDAllocator that is shared across all add calls to allocate IDs.

@llvmbot
Copy link
Member

llvmbot commented Oct 9, 2025

@llvm/pr-subscribers-mlir

Author: Twice (PragmaTwice)

Changes

Previously, each time we called PassManager.add(python_pass_callable), a new TypeID allocator was created and never released afterward. This approach could potentially lead to some issues. In this PR, we introduce a global TypeIDAllocator that is shared across all add calls to allocate IDs.


Full diff: https://github.com/llvm/llvm-project/pull/162594.diff

1 Files Affected:

  • (modified) mlir/lib/Bindings/Python/Pass.cpp (+18-3)
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();

@PragmaTwice PragmaTwice changed the title [MLIR][Python] Make the TypeID allocator global defined in PassManager.add [MLIR][Python] Make the TypeID allocator globally defined in PassManager.add Oct 9, 2025
@PragmaTwice
Copy link
Member Author

Thank you all! I'll merge it soon.

@PragmaTwice PragmaTwice merged commit b6bf196 into llvm:main Oct 9, 2025
9 checks passed
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
…ager.add` (#162594)

Previously, each time we called `PassManager.add(python_pass_callable)`,
a new `TypeID` allocator was created and never released afterward. This
approach could potentially lead to some issues. In this PR, we introduce
a global `TypeIDAllocator` that is shared across all `add` calls to
allocate IDs.
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
…ager.add` (llvm#162594)

Previously, each time we called `PassManager.add(python_pass_callable)`,
a new `TypeID` allocator was created and never released afterward. This
approach could potentially lead to some issues. In this PR, we introduce
a global `TypeIDAllocator` that is shared across all `add` calls to
allocate IDs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants