Skip to content

Pyrdl to irdl

pyrdl_to_irdl

depython_name(name: str)

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
26
27
28
29
def depython_name(name: str):
    if name[-1] == "_" and keyword.iskeyword(name[:-1]):
        return name[:-1]
    return name

constraint_to_irdl(builder: Builder, constraint: AttrConstraint) -> SSAValue

Convert an attribute constraint to IRDL. This will create new operations at the provided builder location.

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
32
33
34
35
36
37
38
def constraint_to_irdl(builder: Builder, constraint: AttrConstraint) -> SSAValue:
    """
    Convert an attribute constraint to IRDL.
    This will create new operations at the provided builder location.
    """
    any_op = builder.insert(AnyOp())
    return any_op.output

range_to_irdl(builder: Builder, constraint: RangeConstraint) -> SSAValue

Convert a range constraint to IRDL. This will create new operations at the provided builder location.

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
41
42
43
44
45
46
47
def range_to_irdl(builder: Builder, constraint: RangeConstraint) -> SSAValue:
    """
    Convert a range constraint to IRDL.
    This will create new operations at the provided builder location.
    """
    any_op = builder.insert(AnyOp())
    return any_op.output

op_def_to_irdl(op: type[IRDLOperation]) -> OperationOp

Convert an operation definition to an IRDL operation definition.

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
 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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def op_def_to_irdl(op: type[IRDLOperation]) -> OperationOp:
    """Convert an operation definition to an IRDL operation definition."""
    op_def = op.get_irdl_definition()

    block = Block()
    builder = Builder(InsertPoint.at_end(block))

    # Operands
    operand_values: list[SSAValue] = []
    operand_variadicities: list[VariadicityAttr] = []
    operand_names: list[StringAttr] = []
    for operand in op_def.operands:
        operand_values.append(range_to_irdl(builder, operand[1].constr))
        if isinstance(operand[1], OptionalDef):
            operand_variadicities.append(VariadicityAttr.OPTIONAL)
        elif isinstance(operand[1], VariadicDef):
            operand_variadicities.append(VariadicityAttr.VARIADIC)
        else:
            operand_variadicities.append(VariadicityAttr.SINGLE)
        operand_names.append(StringAttr(depython_name(operand[0])))
    if operand_values:
        builder.insert(
            OperandsOp(
                operand_values,
                VariadicityArrayAttr(ArrayAttr(operand_variadicities)),
                ArrayAttr(operand_names),
            )
        )

    # Results
    result_values: list[SSAValue] = []
    result_variadicities: list[VariadicityAttr] = []
    result_names: list[StringAttr] = []
    for result in op_def.results:
        result_values.append(range_to_irdl(builder, result[1].constr))
        if isinstance(result[1], OptionalDef):
            result_variadicities.append(VariadicityAttr.OPTIONAL)
        elif isinstance(result[1], VariadicDef):
            result_variadicities.append(VariadicityAttr.VARIADIC)
        else:
            result_variadicities.append(VariadicityAttr.SINGLE)
        result_names.append(StringAttr(depython_name(result[0])))
    if result_values:
        builder.insert(
            ResultsOp(
                result_values,
                VariadicityArrayAttr(ArrayAttr(result_variadicities)),
                ArrayAttr(result_names),
            )
        )

    return OperationOp(Dialect.split_name(op_def.name)[1], Region([block]))

attr_def_to_irdl(attr: type[ParametrizedAttribute]) -> AttributeOp

Convert an attribute definition to an IRDL attribute definition.

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def attr_def_to_irdl(
    attr: type[ParametrizedAttribute],
) -> AttributeOp:
    """Convert an attribute definition to an IRDL attribute definition."""
    attr_def = attr.get_irdl_definition()

    block = Block()
    builder = Builder(InsertPoint.at_end(block))

    # Parameters
    param_values: list[SSAValue] = []
    names: list[StringAttr] = []
    for param in attr_def.parameters:
        param_values.append(constraint_to_irdl(builder, param[1].constr))
        names.append(StringAttr(depython_name(param[0])))
    builder.insert(ParametersOp(param_values, ArrayAttr(names)))

    return AttributeOp(Dialect.split_name(attr_def.name)[1], Region([block]))

dialect_to_irdl(dialect: Dialect, name: str) -> DialectOp

Convert a dialect definition to an IRDL dialect definition.

Source code in xdsl/dialects/irdl/pyrdl_to_irdl.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def dialect_to_irdl(dialect: Dialect, name: str) -> DialectOp:
    """Convert a dialect definition to an IRDL dialect definition."""
    block = Block()
    builder = Builder(InsertPoint.at_end(block))

    for attribute in dialect.attributes:
        if not issubclass(attribute, ParametrizedAttribute):
            raise ValueError(
                "Can only convert ParametrizedAttribute attributes to IRDL"
            )
        builder.insert(attr_def_to_irdl(attribute))

    for operation in dialect.operations:
        if not issubclass(operation, IRDLOperation):
            raise ValueError("Can only convert IRDLOperations operations to IRDL")
        builder.insert(op_def_to_irdl(operation))

    return DialectOp(name, Region([block]))