Skip to content

Irdl to pyrdl

irdl_to_pyrdl

Translate an IRDL program to a Python program creating the corresponding xDSL dialects.

python_name(name: str)

Source code in xdsl/dialects/irdl/irdl_to_pyrdl.py
19
20
21
22
def python_name(name: str):
    if keyword.iskeyword(name):
        return f"{name}_"
    return name

convert_type_or_attr(op: TypeOp | AttributeOp, dialect_name: str) -> str

Convert an IRDL type or attribute to Python code creating that type or attribute in xDSL.

Source code in xdsl/dialects/irdl/irdl_to_pyrdl.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def convert_type_or_attr(op: TypeOp | AttributeOp, dialect_name: str) -> str:
    """
    Convert an IRDL type or attribute to Python code creating
    that type or attribute in xDSL.
    """
    type_addition = ", TypeAttribute" if isinstance(op, TypeOp) else ""
    res = f"""\
@irdl_attr_definition
class {op.sym_name.data}(ParametrizedAttribute{type_addition}):
    name = "{dialect_name}.{op.sym_name.data}"
"""

    for sub_op in op.body.ops:
        if not isinstance(sub_op, ParametersOp):
            continue
        for name in sub_op.names:
            res += f"    {python_name(name.data)}: Attribute\n"
    return res

convert_op(op: OperationOp, dialect_name: str) -> str

Convert an IRDL operation to Python code creating that operation in xDSL.

Source code in xdsl/dialects/irdl/irdl_to_pyrdl.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def convert_op(op: OperationOp, dialect_name: str) -> str:
    """Convert an IRDL operation to Python code creating that operation in xDSL."""
    res = f"""\
@irdl_op_definition
class {op.get_py_class_name()}(IRDLOperation):
    name = "{dialect_name}.{op.sym_name.data}"
"""

    for sub_op in op.body.ops:
        if isinstance(sub_op, OperandsOp):
            for name, var in zip(sub_op.names, sub_op.variadicity.value):
                py_name = python_name(name.data)
                match var:
                    case VariadicityAttr.SINGLE:
                        res += f"    {py_name} = operand_def()\n"
                    case VariadicityAttr.OPTIONAL:
                        res += f"    {py_name} = opt_operand_def()\n"
                    case VariadicityAttr.VARIADIC:
                        res += f"    {py_name} = var_operand_def()\n"
                    case _:
                        pass

        if isinstance(sub_op, ResultsOp):
            for name, var in zip(sub_op.names, sub_op.variadicity.value):
                py_name = python_name(name.data)
                match var:
                    case VariadicityAttr.SINGLE:
                        res += f"    {py_name} = result_def()\n"
                    case VariadicityAttr.OPTIONAL:
                        res += f"    {py_name} = opt_result_def()\n"
                    case VariadicityAttr.VARIADIC:
                        res += f"    {py_name} = var_result_def()\n"
                    case _:
                        pass
    res += "    regs = var_region_def()\n"
    res += "    succs = var_successor_def()\n"
    return res

convert_dialect(dialect: DialectOp) -> str

Convert an IRDL dialect to Python code creating that dialect in xDSL.

Source code in xdsl/dialects/irdl/irdl_to_pyrdl.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def convert_dialect(dialect: DialectOp) -> str:
    """Convert an IRDL dialect to Python code creating that dialect in xDSL."""
    res = ""
    ops: list[str] = []
    attrs: list[str] = []
    for op in dialect.body.ops:
        if isinstance(op, TypeOp) or isinstance(op, AttributeOp):
            res += convert_type_or_attr(op, dialect.sym_name.data) + "\n\n"
            attrs += [op.sym_name.data]
        if isinstance(op, OperationOp):
            res += convert_op(op, dialect.sym_name.data) + "\n\n"
            ops += [op.get_py_class_name()]
    op_list = "[" + ", ".join(ops) + "]"
    attr_list = "[" + ", ".join(attrs) + "]"
    return (
        res
        + dialect.sym_name.data
        + f' = Dialect("{dialect.sym_name.data}", {op_list}, {attr_list})'
    )