-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MLIR] Make minor fixes and improvements
* Adds a tool to convert an MLIR file to Verilog with the same options we have in the top down flow. Useful for debugging (e.g. hand-modify .mlir file, recompile to Verilog). * Always checks Verilog compilation for backend tests by default. Previously, this was turned off since (a) CI didn't always have `circt` available; (b) `circt-opt` wasn't standardized; and (c) running `circt-opt` was slow. Now all of these issues are addressed with v2.3.0.
- Loading branch information
Showing
6 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pathlib | ||
import tempfile | ||
import textwrap | ||
|
||
from tools.mlir_to_verilog_main import main | ||
|
||
|
||
def test_basic(): | ||
|
||
with tempfile.TemporaryDirectory() as tempdir: | ||
tempdir = pathlib.Path(tempdir) | ||
infile = tempdir / "infile.mlir" | ||
with open(infile, "w") as f: | ||
f.write("module {}\n") | ||
outfile = tempdir / "outfile.v" | ||
main([str(infile), "--outfile", str(outfile)]) | ||
assert outfile.is_file() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import argparse | ||
import dataclasses | ||
import logging | ||
import os | ||
import sys | ||
from typing import Dict | ||
|
||
from magma.backend.mlir.mlir_to_verilog import mlir_to_verilog, MlirToVerilogOpts | ||
from magma.common import slice_opts | ||
|
||
|
||
logging.basicConfig(level=os.environ.get("LOGLEVEL", "WARNING").upper()) | ||
|
||
|
||
def _field_to_argument_params(field: dataclasses.Field) -> Dict: | ||
if field.default_factory is not dataclasses.MISSING: | ||
raise TypeError(field) | ||
params = {} | ||
params["required"] = field.default is dataclasses.MISSING | ||
if field.type is bool and not params["required"] and not field.default: | ||
params["action"] = "store_true" | ||
return params | ||
if not params["required"]: | ||
params["default"] = field.default | ||
params["action"] = "store" | ||
params["type"] = field.type | ||
return params | ||
|
||
|
||
def _add_dataclass_arguments(parser: argparse.ArgumentParser, cls: type): | ||
assert dataclasses.is_dataclass(cls) | ||
for field in dataclasses.fields(cls): | ||
params = _field_to_argument_params(field) | ||
parser.add_argument(f"--{field.name}", **params) | ||
|
||
|
||
def main(prog_args = None) -> int: | ||
parser = argparse.ArgumentParser( | ||
"Compile a (MLIR) .mlir file to verilog (.v/.sv)" | ||
) | ||
|
||
parser.add_argument( | ||
"infile", | ||
metavar="<input filename>", | ||
action="store", | ||
type=str, | ||
help="Input MLIR file", | ||
) | ||
parser.add_argument( | ||
"--outfile", | ||
metavar="<output filename>", | ||
action="store", | ||
type=argparse.FileType("w"), | ||
required=False, | ||
default=sys.stdout, | ||
) | ||
_add_dataclass_arguments(parser, MlirToVerilogOpts) | ||
|
||
args = parser.parse_args(prog_args) | ||
opts = slice_opts(vars(args), MlirToVerilogOpts) | ||
|
||
logging.debug(f"Running with opts: {opts}") | ||
if opts.split_verilog and args.outfile is not sys.stdout: | ||
logging.warning( | ||
f"outfile ({args.outfile.name}) ignored with split_verilog enabled" | ||
) | ||
|
||
with open(args.infile, "r") as f_in: | ||
mlir_to_verilog(f_in, args.outfile, opts) | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(main()) | ||
|