Skip to content

Arith

arith

ArithFunctions dataclass

Bases: InterpreterFunctions

Source code in xdsl/interpreters/arith.py
 43
 44
 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
 82
 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@register_impls
class ArithFunctions(InterpreterFunctions):
    @impl(arith.ConstantOp)
    def run_constant(
        self, interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues
    ) -> PythonValues:
        interpreter.interpreter_assert(
            isinstance(op.value, IntegerAttr | FloatAttr),
            f"arith.constant not implemented for {type(op.value)}",
        )
        value = cast(IntegerAttr, op.value)
        return (value.value.data,)

    @impl(arith.SubiOp)
    def run_subi(self, interpreter: Interpreter, op: arith.SubiOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs - rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.AddiOp)
    def run_addi(self, interpreter: Interpreter, op: arith.AddiOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs + rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.MuliOp)
    def run_muli(self, interpreter: Interpreter, op: arith.MuliOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs * rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.AndIOp)
    def run_andi(self, interpreter: Interpreter, op: arith.AndIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs & rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.OrIOp)
    def run_ori(self, interpreter: Interpreter, op: arith.OrIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs | rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.XOrIOp)
    def run_xori(self, interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues):
        assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
        lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
        rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
        return (to_signed(lhs ^ rhs, _int_bitwidth(interpreter, op.result.type)),)

    @impl(arith.SubfOp)
    def run_subf(self, interpreter: Interpreter, op: arith.SubfOp, args: PythonValues):
        return (args[0] - args[1],)

    @impl(arith.AddfOp)
    def run_addf(self, interpreter: Interpreter, op: arith.AddfOp, args: PythonValues):
        return (args[0] + args[1],)

    @impl(arith.MulfOp)
    def run_mulf(self, interpreter: Interpreter, op: arith.MulfOp, args: PythonValues):
        return (args[0] * args[1],)

    @impl(arith.MinimumfOp)
    def run_minimumf(
        self, interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues
    ):
        if isnan(args[0]) or isnan(args[1]):
            return (float("NaN"),)
        if args[0] == 0 and args[1] == 0:
            if copysign(1.0, args[0]) < 0 or copysign(1.0, args[1]) < 0:
                return (-0.0,)
            else:
                return (0.0,)
        return (min(args[0], args[1]),)

    @impl(arith.MaximumfOp)
    def run_maximumf(
        self, interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues
    ):
        if isnan(args[0]) or isnan(args[1]):
            return (float("NaN"),)
        if args[0] == 0 and args[1] == 0:
            if copysign(1.0, args[0]) > 0 or copysign(1.0, args[1]) > 0:
                return (0.0,)
            else:
                return (-0.0,)
        return (max(args[0], args[1]),)

    @impl(arith.CmpiOp)
    def run_cmpi(self, interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues):
        match op.predicate.value.data:
            case 0:  # "eq"
                return (args[0] == args[1],)
            case 1:  # "ne"
                return (args[0] != args[1],)
            case 2:  # "slt"
                return (args[0] < args[1],)
            case 3:  # "sle"
                return (args[0] <= args[1],)
            case 4:  # "sgt"
                return (args[0] > args[1],)
            case 5:  # "sge"
                return (args[0] >= args[1],)
            case 6:  # "ult"
                return (args[0] < args[1],)
            case 7:  # "ule"
                return (args[0] <= args[1],)
            case 8:  # "ugt"
                return (args[0] > args[1],)
            case 9:  # "uge"
                return (args[0] >= args[1],)
            case _:
                raise InterpretationError(
                    f"arith.cmpi predicate {op.predicate} mot implemented yet."
                )

    @impl(arith.CmpfOp)
    def run_cmpf(self, interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues):
        x = args[0]
        y = args[1]

        o = not isnan(x) and not isnan(y)
        u = isnan(x) or isnan(y)

        match op.predicate.value.data:
            case 0:
                return (False,)
            case 1:
                return ((x == y) and o,)
            case 2:
                return ((x > y) and o,)
            case 3:
                return ((x >= y) and o,)
            case 4:
                return ((x < y) and o,)
            case 5:
                return ((x <= y) and o,)
            case 6:
                return ((x != y) and o,)
            case 7:
                return (o,)
            case 8:
                return ((x == y) or u,)
            case 9:
                return ((x > y) or u,)
            case 10:
                return ((x >= y) or u,)
            case 11:
                return ((x < y) or u,)
            case 12:
                return ((x <= y) or u,)
            case 13:
                return ((x != y) or u,)
            case 14:
                return (u,)
            case 15:
                return (True,)
            case _:
                raise InterpretationError(
                    f"arith.cmpf predicate {op.predicate} mot implemented yet."
                )

    @impl(arith.ShLIOp)
    def run_shlsi(self, interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues):
        lhs: int
        rhs: int
        (lhs, rhs) = args
        assert rhs >= 0
        return (lhs << rhs,)

    @impl(arith.ShRSIOp)
    def run_shrsi(
        self, interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues
    ):
        lhs: int
        rhs: int
        (lhs, rhs) = args
        assert rhs >= 0
        return (lhs >> rhs,)

    @impl(arith.DivSIOp)
    def run_divsi(
        self, interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues
    ):
        lhs: int
        rhs: int
        (lhs, rhs) = args
        assert rhs != 0
        div = abs(lhs) // abs(rhs)
        if (lhs > 0) != (rhs > 0):
            div = -div
        return (div,)

    @impl(arith.RemSIOp)
    def run_remsi(
        self, interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues
    ):
        lhs: int
        rhs: int
        (lhs, rhs) = args
        assert rhs != 0
        div = abs(lhs) // abs(rhs)
        if (lhs > 0) != (rhs > 0):
            div = -div
        return (lhs - div * rhs,)

    @impl(arith.FloorDivSIOp)
    def run_floordivsi(
        self, interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues
    ):
        lhs: int
        rhs: int
        (lhs, rhs) = args
        assert rhs != 0
        return (lhs // rhs,)

    @impl(arith.IndexCastOp)
    def run_indexcast(
        self, interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues
    ):
        assert len(args) == 1
        result = args[0]

        assert isa(op.input.type, builtin.IndexType | builtin.IntegerType)

        input_bitwidth = _int_bitwidth(interpreter, op.input.type)
        result_bitwidth = _int_bitwidth(interpreter, op.result.type)

        if input_bitwidth > result_bitwidth:
            result = _truncate(result, result_bitwidth)
        elif input_bitwidth < result_bitwidth:
            result = _sign_extend(result, input_bitwidth)

        return (result,)

run_constant(interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues) -> PythonValues

Source code in xdsl/interpreters/arith.py
45
46
47
48
49
50
51
52
53
54
@impl(arith.ConstantOp)
def run_constant(
    self, interpreter: Interpreter, op: arith.ConstantOp, args: PythonValues
) -> PythonValues:
    interpreter.interpreter_assert(
        isinstance(op.value, IntegerAttr | FloatAttr),
        f"arith.constant not implemented for {type(op.value)}",
    )
    value = cast(IntegerAttr, op.value)
    return (value.value.data,)

run_subi(interpreter: Interpreter, op: arith.SubiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
56
57
58
59
60
61
@impl(arith.SubiOp)
def run_subi(self, interpreter: Interpreter, op: arith.SubiOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs - rhs, _int_bitwidth(interpreter, op.result.type)),)

run_addi(interpreter: Interpreter, op: arith.AddiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
63
64
65
66
67
68
@impl(arith.AddiOp)
def run_addi(self, interpreter: Interpreter, op: arith.AddiOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs + rhs, _int_bitwidth(interpreter, op.result.type)),)

run_muli(interpreter: Interpreter, op: arith.MuliOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
70
71
72
73
74
75
@impl(arith.MuliOp)
def run_muli(self, interpreter: Interpreter, op: arith.MuliOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs * rhs, _int_bitwidth(interpreter, op.result.type)),)

run_andi(interpreter: Interpreter, op: arith.AndIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
77
78
79
80
81
82
@impl(arith.AndIOp)
def run_andi(self, interpreter: Interpreter, op: arith.AndIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs & rhs, _int_bitwidth(interpreter, op.result.type)),)

run_ori(interpreter: Interpreter, op: arith.OrIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
84
85
86
87
88
89
@impl(arith.OrIOp)
def run_ori(self, interpreter: Interpreter, op: arith.OrIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs | rhs, _int_bitwidth(interpreter, op.result.type)),)

run_xori(interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
91
92
93
94
95
96
@impl(arith.XOrIOp)
def run_xori(self, interpreter: Interpreter, op: arith.XOrIOp, args: PythonValues):
    assert isa(op.result.type, builtin.IndexType | builtin.IntegerType)
    lhs = to_signed(args[0], _int_bitwidth(interpreter, op.result.type))
    rhs = to_signed(args[1], _int_bitwidth(interpreter, op.result.type))
    return (to_signed(lhs ^ rhs, _int_bitwidth(interpreter, op.result.type)),)

run_subf(interpreter: Interpreter, op: arith.SubfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
 98
 99
100
@impl(arith.SubfOp)
def run_subf(self, interpreter: Interpreter, op: arith.SubfOp, args: PythonValues):
    return (args[0] - args[1],)

run_addf(interpreter: Interpreter, op: arith.AddfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
102
103
104
@impl(arith.AddfOp)
def run_addf(self, interpreter: Interpreter, op: arith.AddfOp, args: PythonValues):
    return (args[0] + args[1],)

run_mulf(interpreter: Interpreter, op: arith.MulfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
106
107
108
@impl(arith.MulfOp)
def run_mulf(self, interpreter: Interpreter, op: arith.MulfOp, args: PythonValues):
    return (args[0] * args[1],)

run_minimumf(interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
110
111
112
113
114
115
116
117
118
119
120
121
@impl(arith.MinimumfOp)
def run_minimumf(
    self, interpreter: Interpreter, op: arith.MinimumfOp, args: PythonValues
):
    if isnan(args[0]) or isnan(args[1]):
        return (float("NaN"),)
    if args[0] == 0 and args[1] == 0:
        if copysign(1.0, args[0]) < 0 or copysign(1.0, args[1]) < 0:
            return (-0.0,)
        else:
            return (0.0,)
    return (min(args[0], args[1]),)

run_maximumf(interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
123
124
125
126
127
128
129
130
131
132
133
134
@impl(arith.MaximumfOp)
def run_maximumf(
    self, interpreter: Interpreter, op: arith.MaximumfOp, args: PythonValues
):
    if isnan(args[0]) or isnan(args[1]):
        return (float("NaN"),)
    if args[0] == 0 and args[1] == 0:
        if copysign(1.0, args[0]) > 0 or copysign(1.0, args[1]) > 0:
            return (0.0,)
        else:
            return (-0.0,)
    return (max(args[0], args[1]),)

run_cmpi(interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
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
@impl(arith.CmpiOp)
def run_cmpi(self, interpreter: Interpreter, op: arith.CmpiOp, args: PythonValues):
    match op.predicate.value.data:
        case 0:  # "eq"
            return (args[0] == args[1],)
        case 1:  # "ne"
            return (args[0] != args[1],)
        case 2:  # "slt"
            return (args[0] < args[1],)
        case 3:  # "sle"
            return (args[0] <= args[1],)
        case 4:  # "sgt"
            return (args[0] > args[1],)
        case 5:  # "sge"
            return (args[0] >= args[1],)
        case 6:  # "ult"
            return (args[0] < args[1],)
        case 7:  # "ule"
            return (args[0] <= args[1],)
        case 8:  # "ugt"
            return (args[0] > args[1],)
        case 9:  # "uge"
            return (args[0] >= args[1],)
        case _:
            raise InterpretationError(
                f"arith.cmpi predicate {op.predicate} mot implemented yet."
            )

run_cmpf(interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
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
197
198
199
200
201
202
203
204
205
206
207
208
@impl(arith.CmpfOp)
def run_cmpf(self, interpreter: Interpreter, op: arith.CmpfOp, args: PythonValues):
    x = args[0]
    y = args[1]

    o = not isnan(x) and not isnan(y)
    u = isnan(x) or isnan(y)

    match op.predicate.value.data:
        case 0:
            return (False,)
        case 1:
            return ((x == y) and o,)
        case 2:
            return ((x > y) and o,)
        case 3:
            return ((x >= y) and o,)
        case 4:
            return ((x < y) and o,)
        case 5:
            return ((x <= y) and o,)
        case 6:
            return ((x != y) and o,)
        case 7:
            return (o,)
        case 8:
            return ((x == y) or u,)
        case 9:
            return ((x > y) or u,)
        case 10:
            return ((x >= y) or u,)
        case 11:
            return ((x < y) or u,)
        case 12:
            return ((x <= y) or u,)
        case 13:
            return ((x != y) or u,)
        case 14:
            return (u,)
        case 15:
            return (True,)
        case _:
            raise InterpretationError(
                f"arith.cmpf predicate {op.predicate} mot implemented yet."
            )

run_shlsi(interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
210
211
212
213
214
215
216
@impl(arith.ShLIOp)
def run_shlsi(self, interpreter: Interpreter, op: arith.ShLIOp, args: PythonValues):
    lhs: int
    rhs: int
    (lhs, rhs) = args
    assert rhs >= 0
    return (lhs << rhs,)

run_shrsi(interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
218
219
220
221
222
223
224
225
226
@impl(arith.ShRSIOp)
def run_shrsi(
    self, interpreter: Interpreter, op: arith.ShRSIOp, args: PythonValues
):
    lhs: int
    rhs: int
    (lhs, rhs) = args
    assert rhs >= 0
    return (lhs >> rhs,)

run_divsi(interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
228
229
230
231
232
233
234
235
236
237
238
239
@impl(arith.DivSIOp)
def run_divsi(
    self, interpreter: Interpreter, op: arith.DivSIOp, args: PythonValues
):
    lhs: int
    rhs: int
    (lhs, rhs) = args
    assert rhs != 0
    div = abs(lhs) // abs(rhs)
    if (lhs > 0) != (rhs > 0):
        div = -div
    return (div,)

run_remsi(interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
241
242
243
244
245
246
247
248
249
250
251
252
@impl(arith.RemSIOp)
def run_remsi(
    self, interpreter: Interpreter, op: arith.RemSIOp, args: PythonValues
):
    lhs: int
    rhs: int
    (lhs, rhs) = args
    assert rhs != 0
    div = abs(lhs) // abs(rhs)
    if (lhs > 0) != (rhs > 0):
        div = -div
    return (lhs - div * rhs,)

run_floordivsi(interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
254
255
256
257
258
259
260
261
262
@impl(arith.FloorDivSIOp)
def run_floordivsi(
    self, interpreter: Interpreter, op: arith.FloorDivSIOp, args: PythonValues
):
    lhs: int
    rhs: int
    (lhs, rhs) = args
    assert rhs != 0
    return (lhs // rhs,)

run_indexcast(interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues)

Source code in xdsl/interpreters/arith.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@impl(arith.IndexCastOp)
def run_indexcast(
    self, interpreter: Interpreter, op: arith.IndexCastOp, args: PythonValues
):
    assert len(args) == 1
    result = args[0]

    assert isa(op.input.type, builtin.IndexType | builtin.IntegerType)

    input_bitwidth = _int_bitwidth(interpreter, op.input.type)
    result_bitwidth = _int_bitwidth(interpreter, op.result.type)

    if input_bitwidth > result_bitwidth:
        result = _truncate(result, result_bitwidth)
    elif input_bitwidth < result_bitwidth:
        result = _sign_extend(result, input_bitwidth)

    return (result,)