Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mlir/lib/Bindings/Python/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -173,6 +192,7 @@ class PyGlobals {
llvm::StringSet<> loadedDialectModules;

TracebackLoc tracebackLoc;
TypeIDAllocator typeIDAllocator;
};

} // namespace python
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Bindings/Python/Pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Pass.h"

#include "Globals.h"
#include "IRModule.h"
#include "mlir-c/Pass.h"
// clang-format off
Expand Down Expand Up @@ -181,9 +182,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 = PyGlobals::get().allocateTypeID();
MlirExternalPassCallbacks callbacks;
callbacks.construct = [](void *obj) {
(void)nb::handle(static_cast<PyObject *>(obj)).inc_ref();
Expand Down