Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit b191fe5

Browse files
authored
[mlir] Python: Parse ModuleOp from file path (#125736)
For extremely large models, it may be inefficient to load the model into memory in Python prior to passing it to the MLIR C APIs for deserialization. This change adds an API to parse a ModuleOp directly from a file path.
1 parent 127dc9a commit b191fe5

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

IRCore.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <filesystem>
910
#include <optional>
1011
#include <utility>
1112

@@ -299,7 +300,7 @@ struct PyAttrBuilderMap {
299300
return *builder;
300301
}
301302
static void dunderSetItemNamed(const std::string &attributeKind,
302-
nb::callable func, bool replace) {
303+
nb::callable func, bool replace) {
303304
PyGlobals::get().registerAttributeBuilder(attributeKind, std::move(func),
304305
replace);
305306
}
@@ -3049,6 +3050,19 @@ void mlir::python::populateIRCore(nb::module_ &m) {
30493050
},
30503051
nb::arg("asm"), nb::arg("context").none() = nb::none(),
30513052
kModuleParseDocstring)
3053+
.def_static(
3054+
"parse",
3055+
[](const std::filesystem::path &path,
3056+
DefaultingPyMlirContext context) {
3057+
PyMlirContext::ErrorCapture errors(context->getRef());
3058+
MlirModule module = mlirModuleCreateParseFromFile(
3059+
context->get(), toMlirStringRef(path.string()));
3060+
if (mlirModuleIsNull(module))
3061+
throw MLIRError("Unable to parse module assembly", errors.take());
3062+
return PyModule::forModule(module).releaseObject();
3063+
},
3064+
nb::arg("asm"), nb::arg("context").none() = nb::none(),
3065+
kModuleParseDocstring)
30523066
.def_static(
30533067
"create",
30543068
[](DefaultingPyLocation loc) {

0 commit comments

Comments
 (0)