Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions mlir/lib/Bindings/Python/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "NanobindUtils.h"
#include "mlir-c/IR.h"
#include "mlir-c/Support.h"
#include "mlir/CAPI/Support.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -151,6 +152,29 @@ class PyGlobals {

TracebackLoc &getTracebackLoc() { return tracebackLoc; }

class TypeIDAllocator {
public:
TypeIDAllocator() : allocator(mlirTypeIDAllocatorCreate()) {}
~TypeIDAllocator() {
if (allocator.ptr)
mlirTypeIDAllocatorDestroy(allocator);
}
TypeIDAllocator(const TypeIDAllocator &) = delete;
TypeIDAllocator(TypeIDAllocator &&other) : allocator(other.allocator) {
other.allocator.ptr = nullptr;
}

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 +197,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