Skip to content

Commit ff8b42f

Browse files
authored
add mlir module contextmaanger (#128)
1 parent 82cbdf3 commit ff8b42f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

mlir/extras/context.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ def __str__(self):
112112
return str(self.module)
113113

114114

115+
@contextmanager
116+
def mlir_mod(
117+
src: Optional[str] = None,
118+
location: ir.Location = None,
119+
) -> ir.Module:
120+
with ExitStack() as stack:
121+
if location is None:
122+
location = ir.Location.unknown()
123+
stack.enter_context(location)
124+
if src is not None:
125+
module = ir.Module.parse(src)
126+
else:
127+
module = ir.Module.create()
128+
ip = ir.InsertionPoint(module.body)
129+
stack.enter_context(ip)
130+
yield module
131+
132+
115133
@contextlib.contextmanager
116134
def enable_multithreading(context=None):
117135
from ..ir import Context

0 commit comments

Comments
 (0)