summaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/IRCore.cpp
diff options
context:
space:
mode:
authorNikhil Kalra <nkalra@apple.com>2025-02-12 14:02:41 -0800
committerGitHub <noreply@github.com>2025-02-12 14:02:41 -0800
commit65ed4fa57e6293f8e059a368ac52e2a57b1f78e4 (patch)
tree17a02320e8a2c39799c6d84f3e749c945b957926 /mlir/lib/Bindings/Python/IRCore.cpp
parentfa71238da800f3a818ec0e0649462389dc577890 (diff)
[mlir] Python: Parse ModuleOp from file path (#126572)
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. Re-lands [4e14b8a](https://github.com/llvm/llvm-project/commit/4e14b8afb44af58ab7073bb8c0b52875599b0ae1).
Diffstat (limited to 'mlir/lib/Bindings/Python/IRCore.cpp')
-rw-r--r--mlir/lib/Bindings/Python/IRCore.cpp14
1 files changed, 13 insertions, 1 deletions
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 47a85c2a486f..827db5f3eba8 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -299,7 +299,7 @@ struct PyAttrBuilderMap {
return *builder;
}
static void dunderSetItemNamed(const std::string &attributeKind,
- nb::callable func, bool replace) {
+ nb::callable func, bool replace) {
PyGlobals::get().registerAttributeBuilder(attributeKind, std::move(func),
replace);
}
@@ -3050,6 +3050,18 @@ void mlir::python::populateIRCore(nb::module_ &m) {
nb::arg("asm"), nb::arg("context").none() = nb::none(),
kModuleParseDocstring)
.def_static(
+ "parseFile",
+ [](const std::string &path, DefaultingPyMlirContext context) {
+ PyMlirContext::ErrorCapture errors(context->getRef());
+ MlirModule module = mlirModuleCreateParseFromFile(
+ context->get(), toMlirStringRef(path));
+ if (mlirModuleIsNull(module))
+ throw MLIRError("Unable to parse module assembly", errors.take());
+ return PyModule::forModule(module).releaseObject();
+ },
+ nb::arg("path"), nb::arg("context").none() = nb::none(),
+ kModuleParseDocstring)
+ .def_static(
"create",
[](DefaultingPyLocation loc) {
MlirModule module = mlirModuleCreateEmpty(loc);