File tree 5 files changed +56
-1
lines changed
python/mlir/_mlir_libs/_mlir 5 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -309,6 +309,10 @@ MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateEmpty(MlirLocation location);
309
309
MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateParse (MlirContext context ,
310
310
MlirStringRef module );
311
311
312
+ /// Parses a module from file and transfers ownership to the caller.
313
+ MLIR_CAPI_EXPORTED MlirModule
314
+ mlirModuleCreateParseFromFile (MlirContext context , MlirStringRef fileName );
315
+
312
316
/// Gets the context that a module was created with.
313
317
MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext (MlirModule module );
314
318
Original file line number Diff line number Diff line change @@ -299,7 +299,7 @@ struct PyAttrBuilderMap {
299
299
return *builder;
300
300
}
301
301
static void dunderSetItemNamed (const std::string &attributeKind,
302
- nb::callable func, bool replace) {
302
+ nb::callable func, bool replace) {
303
303
PyGlobals::get ().registerAttributeBuilder (attributeKind, std::move (func),
304
304
replace);
305
305
}
@@ -3049,6 +3049,18 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3049
3049
},
3050
3050
nb::arg (" asm" ), nb::arg (" context" ).none () = nb::none (),
3051
3051
kModuleParseDocstring )
3052
+ .def_static (
3053
+ " parseFile" ,
3054
+ [](const std::string &path, DefaultingPyMlirContext context) {
3055
+ PyMlirContext::ErrorCapture errors (context->getRef ());
3056
+ MlirModule module = mlirModuleCreateParseFromFile (
3057
+ context->get (), toMlirStringRef (path));
3058
+ if (mlirModuleIsNull (module))
3059
+ throw MLIRError (" Unable to parse module assembly" , errors.take ());
3060
+ return PyModule::forModule (module).releaseObject ();
3061
+ },
3062
+ nb::arg (" path" ), nb::arg (" context" ).none () = nb::none (),
3063
+ kModuleParseDocstring )
3052
3064
.def_static (
3053
3065
" create" ,
3054
3066
[](DefaultingPyLocation loc) {
Original file line number Diff line number Diff line change 22
22
#include " mlir/IR/Location.h"
23
23
#include " mlir/IR/Operation.h"
24
24
#include " mlir/IR/OperationSupport.h"
25
+ #include " mlir/IR/OwningOpRef.h"
25
26
#include " mlir/IR/Types.h"
26
27
#include " mlir/IR/Value.h"
27
28
#include " mlir/IR/Verifier.h"
@@ -328,6 +329,15 @@ MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
328
329
return MlirModule{owning.release ().getOperation ()};
329
330
}
330
331
332
+ MlirModule mlirModuleCreateParseFromFile (MlirContext context,
333
+ MlirStringRef fileName) {
334
+ OwningOpRef<ModuleOp> owning =
335
+ parseSourceFile<ModuleOp>(unwrap (fileName), unwrap (context));
336
+ if (!owning)
337
+ return MlirModule{nullptr };
338
+ return MlirModule{owning.release ().getOperation ()};
339
+ }
340
+
331
341
MlirContext mlirModuleGetContext (MlirModule module) {
332
342
return wrap (unwrap (module).getContext ());
333
343
}
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ import abc
46
46
import collections
47
47
from collections .abc import Callable , Sequence
48
48
import io
49
+ from pathlib import Path
49
50
from typing import Any , ClassVar , TypeVar , overload
50
51
51
52
__all__ = [
@@ -2129,6 +2130,15 @@ class Module:
2129
2130
2130
2131
Returns a new MlirModule or raises an MLIRError if the parsing fails.
2131
2132
2133
+ See also: https://mlir.llvm.org/docs/LangRef/
2134
+ """
2135
+ @staticmethod
2136
+ def parseFile (path : str , context : Context | None = None ) -> Module :
2137
+ """
2138
+ Parses a module's assembly format from file.
2139
+
2140
+ Returns a new MlirModule or raises an MLIRError if the parsing fails.
2141
+
2132
2142
See also: https://mlir.llvm.org/docs/LangRef/
2133
2143
"""
2134
2144
def _CAPICreate (self ) -> Any : ...
Original file line number Diff line number Diff line change 1
1
# RUN: %PYTHON %s | FileCheck %s
2
2
3
3
import gc
4
+ from tempfile import NamedTemporaryFile
4
5
from mlir .ir import *
5
6
6
7
@@ -27,6 +28,24 @@ def testParseSuccess():
27
28
print (str (module ))
28
29
29
30
31
+ # Verify successful parse from file.
32
+ # CHECK-LABEL: TEST: testParseFromFileSuccess
33
+ # CHECK: module @successfulParse
34
+ @run
35
+ def testParseFromFileSuccess ():
36
+ ctx = Context ()
37
+ with NamedTemporaryFile (mode = "w" ) as tmp_file :
38
+ tmp_file .write (r"""module @successfulParse {}""" )
39
+ tmp_file .flush ()
40
+ module = Module .parseFile (tmp_file .name , ctx )
41
+ assert module .context is ctx
42
+ print ("CLEAR CONTEXT" )
43
+ ctx = None # Ensure that module captures the context.
44
+ gc .collect ()
45
+ module .operation .verify ()
46
+ print (str (module ))
47
+
48
+
30
49
# Verify parse error.
31
50
# CHECK-LABEL: TEST: testParseError
32
51
# CHECK: testParseError: <
You can’t perform that action at this time.
0 commit comments