Skip to content

Commit 23d2665

Browse files
authored
[PyCDE] Import MLIR: provide hook to preprocess ops (#8624)
Adds the ability for users to load the MLIR file and preprocess or skip pre-lowering ops.
1 parent f0d44e9 commit 23d2665

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

frontends/PyCDE/src/pycde/system.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ def import_mlir(self,
164164
lowering: Optional[List[str]] = None,
165165
output_filename: Optional[str] = None,
166166
importer: Optional[Callable] = None,
167+
preprocess_op: Optional[Callable[[ir.OpView],
168+
Optional[ir.OpView]]] = None,
167169
debug: bool = False) -> Dict[str, Any]:
168170
"""Import mlir asm created elsewhere into our space. Exactly one of the
169171
arguments module_str or file must be provided.
@@ -176,6 +178,8 @@ def import_mlir(self,
176178
output_filename: Optional filename to set on the imported ops.
177179
importer: Optional custom importer function which takes a System and the
178180
op. Must return a 3-tuple of (name, obj, proxy).
181+
preprocess_op: Optional preprocessing function which takes an Operation and
182+
returns a modified Operation or None to skip.
179183
debug: Whether to enable debug output for the import process.
180184
"""
181185

@@ -234,6 +238,12 @@ def import_builtin(
234238

235239
ret: Dict[str, Any] = {}
236240
for op in compat_mod.body:
241+
if preprocess_op is not None:
242+
op = preprocess_op(op)
243+
if op is None:
244+
# If the op was preprocessed to None, skip it.
245+
continue
246+
237247
# TODO: handle symbolrefs pointing to potentially renamed symbols.
238248
imported_obj = None
239249
imported_name = None

frontends/PyCDE/test/test_import_hw_modules.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# RUN: %PYTHON% %s %t | FileCheck %s
22

3-
from pycde.circt.ir import Module as IrModule
43
from pycde.circt.dialects import hw
54

65
from pycde import Input, Output, System, generator, Module, types
@@ -22,6 +21,8 @@
2221
hw.output %0 : i1
2322
}
2423
24+
hw.module.extern @skip()
25+
2526
hw.type_scope @otherScope {}
2627
"""
2728

@@ -54,8 +55,15 @@ def generate(ports):
5455
ports.out1 = outs[1]
5556

5657

58+
def preprocess_op(op):
59+
"""Preprocess the operation to skip the `skip` module."""
60+
if isinstance(op, hw.HWModuleExternOp) and op.name.value == "skip":
61+
return None
62+
return op
63+
64+
5765
system = System([Top], output_directory=sys.argv[1])
58-
imported_modules = system.import_mlir(mlir_module)
66+
imported_modules = system.import_mlir(mlir_module, preprocess_op=preprocess_op)
5967
imported_modules["add"].add_metadata(Metadata(name="add"))
6068
system.generate()
6169
TypeAlias.declare_aliases(system.mod)

0 commit comments

Comments
 (0)