Skip to content

Varith

varith

integerOrFloatLike = ContainerOf(AnyOf([IntegerType, IndexType, BFloat16Type, Float16Type, Float32Type, Float64Type, Float80Type, Float128Type])) module-attribute

Varith = Dialect('varith', [VarithAddOp, VarithMulOp, VarithSwitchOp]) module-attribute

VarithOp

Bases: IRDLOperation

Variadic arithmetic operation

Source code in xdsl/dialects/varith.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class VarithOp(IRDLOperation):
    """
    Variadic arithmetic operation
    """

    T: ClassVar = VarConstraint("T", integerOrFloatLike)

    args = var_operand_def(T)
    res = result_def(T)

    traits = traits_def(Pure())

    assembly_format = "$args attr-dict `:` type($res)"

    def __init__(self, *args: SSAValue | Operation):
        assert len(args) > 0
        super().__init__(operands=[args], result_types=[SSAValue.get(args[-1]).type])

T: ClassVar = VarConstraint('T', integerOrFloatLike) class-attribute instance-attribute

args = var_operand_def(T) class-attribute instance-attribute

res = result_def(T) class-attribute instance-attribute

traits = traits_def(Pure()) class-attribute instance-attribute

assembly_format = '$args attr-dict `:` type($res)' class-attribute instance-attribute

__init__(*args: SSAValue | Operation)

Source code in xdsl/dialects/varith.py
68
69
70
def __init__(self, *args: SSAValue | Operation):
    assert len(args) > 0
    super().__init__(operands=[args], result_types=[SSAValue.get(args[-1]).type])

VarithAddOp dataclass

Bases: VarithOp

Source code in xdsl/dialects/varith.py
73
74
75
@irdl_op_definition
class VarithAddOp(VarithOp):
    name = "varith.add"

name = 'varith.add' class-attribute instance-attribute

VarithMulOp dataclass

Bases: VarithOp

Source code in xdsl/dialects/varith.py
78
79
80
@irdl_op_definition
class VarithMulOp(VarithOp):
    name = "varith.mul"

name = 'varith.mul' class-attribute instance-attribute

VarithSwitchOp

Bases: IRDLOperation

Variadic selection operation

Similar to cf.switch, this operation returns the argument corresponding to flag, returning the default value otherwise.

Source code in xdsl/dialects/varith.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
@irdl_op_definition
class VarithSwitchOp(IRDLOperation):
    """
    Variadic selection operation

    Similar to `cf.switch`, this operation returns the argument corresponding to
    `flag`, returning the default value otherwise.
    """

    name = "varith.switch"

    T: ClassVar = VarConstraint("T", AnyAttr())

    flag = operand_def(IntegerType | IndexType)
    case_values = prop_def(DenseIntElementsAttr)

    default_arg = operand_def(T)
    args = var_operand_def(T)

    result = result_def(T)

    traits = traits_def(Pure())

    def __init__(
        self,
        flag: SSAValue | Operation,
        case_values: DenseIntElementsAttr,
        default_arg: SSAValue | Operation,
        *args: SSAValue | Operation,
        attr_dict: dict[str, Attribute] | None = None,
    ):
        super().__init__(
            operands=[
                flag,
                default_arg,
                args,
            ],
            properties={
                "case_values": case_values,
            },
            attributes=attr_dict,
            result_types=(SSAValue.get(default_arg).type,),
        )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        unresolved_flag = parser.parse_unresolved_operand()
        parser.parse_punctuation(":")
        flag_type = parser.parse_type()
        assert isa(flag_type, IntegerType | IndexType)
        flag = parser.resolve_operand(unresolved_flag, flag_type)
        parser.parse_punctuation("->")
        return_type = parser.parse_type()
        parser.parse_punctuation(",")
        parser.parse_punctuation("[")
        parser.parse_keyword("default")
        parser.parse_punctuation(":")
        default_arg = parser.resolve_operand(
            parser.parse_unresolved_operand(), return_type
        )

        values: list[int] = []
        args: list[SSAValue] = []
        while parser.parse_optional_punctuation(","):
            values.append(parser.parse_integer())
            parser.parse_punctuation(":")
            args.append(
                parser.resolve_operand(parser.parse_unresolved_operand(), return_type)
            )
        parser.parse_punctuation("]")
        attr_dict = parser.parse_optional_attr_dict()

        case_values = DenseIntElementsAttr.from_list(
            VectorType(flag_type, (len(values),)), values
        )

        return cls(
            flag,
            case_values,
            default_arg,
            *args,
            attr_dict=attr_dict,
        )

    @staticmethod
    def _print_case(printer: Printer, case_name: str, arg: SSAValue):
        printer.print_string(case_name)
        printer.print_string(": ")
        printer.print_operand(arg)

    def print(self, printer: Printer):
        printer.print_string(" ")
        printer.print_operand(self.flag)
        printer.print_string(" : ")
        printer.print_attribute(self.flag.type)
        printer.print_string(" -> ")
        printer.print_attribute(self.result.type)
        printer.print_string(", [")
        with printer.indented():
            printer.print_string("\n")
            cases = [("default", self.default_arg)] + [
                (str(c), arg)
                for (c, arg) in zip(
                    self.case_values.get_values(),
                    self.args,
                    strict=True,
                )
            ]
            printer.print_list(
                cases, lambda x: self._print_case(printer, x[0], x[1]), ",\n"
            )
        printer.print_string("\n]")
        if self.attributes:
            printer.print_attr_dict(self.attributes)

name = 'varith.switch' class-attribute instance-attribute

T: ClassVar = VarConstraint('T', AnyAttr()) class-attribute instance-attribute

flag = operand_def(IntegerType | IndexType) class-attribute instance-attribute

case_values = prop_def(DenseIntElementsAttr) class-attribute instance-attribute

default_arg = operand_def(T) class-attribute instance-attribute

args = var_operand_def(T) class-attribute instance-attribute

result = result_def(T) class-attribute instance-attribute

traits = traits_def(Pure()) class-attribute instance-attribute

__init__(flag: SSAValue | Operation, case_values: DenseIntElementsAttr, default_arg: SSAValue | Operation, *args: SSAValue | Operation, attr_dict: dict[str, Attribute] | None = None)

Source code in xdsl/dialects/varith.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def __init__(
    self,
    flag: SSAValue | Operation,
    case_values: DenseIntElementsAttr,
    default_arg: SSAValue | Operation,
    *args: SSAValue | Operation,
    attr_dict: dict[str, Attribute] | None = None,
):
    super().__init__(
        operands=[
            flag,
            default_arg,
            args,
        ],
        properties={
            "case_values": case_values,
        },
        attributes=attr_dict,
        result_types=(SSAValue.get(default_arg).type,),
    )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/varith.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
@classmethod
def parse(cls, parser: Parser) -> Self:
    unresolved_flag = parser.parse_unresolved_operand()
    parser.parse_punctuation(":")
    flag_type = parser.parse_type()
    assert isa(flag_type, IntegerType | IndexType)
    flag = parser.resolve_operand(unresolved_flag, flag_type)
    parser.parse_punctuation("->")
    return_type = parser.parse_type()
    parser.parse_punctuation(",")
    parser.parse_punctuation("[")
    parser.parse_keyword("default")
    parser.parse_punctuation(":")
    default_arg = parser.resolve_operand(
        parser.parse_unresolved_operand(), return_type
    )

    values: list[int] = []
    args: list[SSAValue] = []
    while parser.parse_optional_punctuation(","):
        values.append(parser.parse_integer())
        parser.parse_punctuation(":")
        args.append(
            parser.resolve_operand(parser.parse_unresolved_operand(), return_type)
        )
    parser.parse_punctuation("]")
    attr_dict = parser.parse_optional_attr_dict()

    case_values = DenseIntElementsAttr.from_list(
        VectorType(flag_type, (len(values),)), values
    )

    return cls(
        flag,
        case_values,
        default_arg,
        *args,
        attr_dict=attr_dict,
    )

print(printer: Printer)

Source code in xdsl/dialects/varith.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def print(self, printer: Printer):
    printer.print_string(" ")
    printer.print_operand(self.flag)
    printer.print_string(" : ")
    printer.print_attribute(self.flag.type)
    printer.print_string(" -> ")
    printer.print_attribute(self.result.type)
    printer.print_string(", [")
    with printer.indented():
        printer.print_string("\n")
        cases = [("default", self.default_arg)] + [
            (str(c), arg)
            for (c, arg) in zip(
                self.case_values.get_values(),
                self.args,
                strict=True,
            )
        ]
        printer.print_list(
            cases, lambda x: self._print_case(printer, x[0], x[1]), ",\n"
        )
    printer.print_string("\n]")
    if self.attributes:
        printer.print_attr_dict(self.attributes)