Skip to content

Irdl to pyrdl

irdl_to_pyrdl

Script to convert an IRDL program to an xDSL dialect in Python.

main()

Source code in xdsl/tools/irdl_to_pyrdl.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def main():
    # Parse CLI arguments
    arg_parser = argparse.ArgumentParser(
        description="Convert an IRDL program to a Python definition of a xDSL dialect."
    )
    arg_parser.add_argument(
        "-o", "--output-file", type=str, required=False, help="path to output file"
    )
    arg_parser.add_argument("input_file", type=str, help="path to input file")
    args = arg_parser.parse_args()

    ctx = Context()
    for dialect_factory in get_all_dialects().values():
        ctx.load_dialect(dialect_factory())

    # Parse the input file
    f = open(args.input_file)
    parser = Parser(ctx, f.read(), name=args.input_file)
    module = parser.parse_module()

    # Prepare the output file
    if args.output_file is None:
        file = sys.stdout
    else:
        file = open(args.output_file, "w")

    # Output the Python code
    print("from xdsl.irdl import *", file=file)
    print("from xdsl.ir import *\n\n", file=file)
    for op in module.walk():
        if isinstance(op, DialectOp):
            print(convert_dialect(op), file=file)