Skip to content

Pdl interp

pdl_interp

PDLInterpFunctions dataclass

Bases: InterpreterFunctions

Interpreter functions for the pdl_interp dialect. All operations that get a value from the IR will return None if the requested value cannot be determined.

Native constraints are implemented via impl_external. Clients that want to provide extra constraint declarations must do so with a similar extra InterpreterFunctions subclass registered with the interpreter:

@register_impls
class TestImplFunctions(InterpreterFunctions):
    @impl_external("test_constraint")
    def run_test_constraint(
        self, interp: Interpreter, op: Operation, args: PythonValues
    ) -> tuple[bool, PythonValues]:
        assert isinstance(args[0], int)
        (x,) = args
        return True, (x + 42,)

Note that the return type of a native constraint must be tuple[bool, PythonValues].

Source code in xdsl/interpreters/pdl_interp.py
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
@register_impls
class PDLInterpFunctions(InterpreterFunctions):
    """
    Interpreter functions for the pdl_interp dialect.
    All operations that get a value from the IR will return None if the requested value
    cannot be determined.

    Native constraints are implemented via `impl_external`. Clients that want to provide
    extra constraint declarations must do so with a similar extra `InterpreterFunctions`
    subclass registered with the interpreter:

    ```py
    @register_impls
    class TestImplFunctions(InterpreterFunctions):
        @impl_external("test_constraint")
        def run_test_constraint(
            self, interp: Interpreter, op: Operation, args: PythonValues
        ) -> tuple[bool, PythonValues]:
            assert isinstance(args[0], int)
            (x,) = args
            return True, (x + 42,)
    ```

    Note that the return type of a native constraint must be `tuple[bool, PythonValues]`.
    """

    @staticmethod
    def get_ctx(interpreter: Interpreter) -> Context:
        return interpreter.get_data(
            PDLInterpFunctions,
            "ctx",
            lambda: Context(),
        )

    @staticmethod
    def set_ctx(interpreter: Interpreter, ctx: Context) -> None:
        interpreter.set_data(
            PDLInterpFunctions,
            "ctx",
            ctx,
        )

    @staticmethod
    def get_pending_rewrites(
        interpreter: Interpreter,
    ) -> list[tuple[SymbolRefAttr, Operation, tuple[Any, ...]]]:
        """
        Returns the list of pending rewrites to be executed. Each entry is a tuple of (rewriter, root, args).
        """
        return interpreter.get_data(
            PDLInterpFunctions,
            "pending_rewrites",
            lambda: [],
        )

    @staticmethod
    def get_rewriter(interpreter: Interpreter) -> PatternRewriter:
        rewriter: PatternRewriter | None = interpreter.get_data(
            PDLInterpFunctions, "rewriter", lambda: None
        )
        if rewriter is None:
            raise InterpretationError(
                "Expected an active rewriter when calling a pdl_interp function."
            )
        return rewriter

    @staticmethod
    def set_rewriter(
        interpreter: Interpreter, rewriter: PatternRewriter | None
    ) -> None:
        interpreter.set_data(PDLInterpFunctions, "rewriter", rewriter)

    @impl(pdl_interp.GetOperandOp)
    def run_get_operand(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetOperandOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)
        if op.index.value.data >= len(args[0].operands):
            return (None,)
        else:
            return (args[0].operands[op.index.value.data],)

    @impl(pdl_interp.GetResultOp)
    def run_get_result(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetResultOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)
        if len(args[0].results) <= op.index.value.data:
            return (None,)
        return (args[0].results[op.index.value.data],)

    @impl(pdl_interp.GetResultsOp)
    def run_get_results(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetResultsOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)
        src_op = args[0]
        assert isinstance(src_op, IRDLOperation)
        if op.index is not None:
            # get the field name of the result group:
            if op.index.value.data >= len(src_op.get_irdl_definition().results):
                return (None,)
            field = src_op.get_irdl_definition().results[op.index.value.data][0]
            results = getattr(src_op, field)
            if isa(results, OpResult):
                results = (results,)
        else:
            results = src_op.results

        if isinstance(op.result_types[0], ValueType):
            if len(results) != 1:
                return (None,)
            return (results[0],)
        return (results,)

    @impl(pdl_interp.GetAttributeOp)
    def run_get_attribute(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetAttributeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)
        attrname = op.constraint_name.data
        if attrname in args[0].attributes:
            return (args[0].attributes[attrname],)
        elif attrname in args[0].properties:
            return (args[0].properties[attrname],)
        else:
            return (None,)

    @impl(pdl_interp.GetValueTypeOp)
    def run_get_value_type(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetValueTypeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], SSAValue)
        value = cast(SSAValue, args[0])
        return (value.type,)

    @impl(pdl_interp.GetDefiningOpOp)
    def run_get_defining_op(
        self,
        interpreter: Interpreter,
        op: pdl_interp.GetDefiningOpOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        if args[0] is None:
            return (None,)
        assert isinstance(args[0], SSAValue)
        if not isinstance(args[0], OpResult):
            return (None,)
        return (args[0].owner,)

    @impl_terminator(pdl_interp.CheckOperationNameOp)
    def run_check_operation_name(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CheckOperationNameOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)
        cond = args[0].name == op.operation_name.data
        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.CheckOperandCountOp)
    def run_check_operand_count(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CheckOperandCountOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)

        operand_count = len(args[0].operands)
        expected_count = op.count.value.data

        # If compareAtLeast is set, check if operand count is >= expected
        # Otherwise check for exact match
        if "compareAtLeast" in op.properties:
            cond = operand_count >= expected_count
        else:
            cond = operand_count == expected_count

        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.CheckResultCountOp)
    def run_check_result_count(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CheckResultCountOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        assert isinstance(args[0], Operation)

        result_count = len(args[0].results)
        expected_count = op.count.value.data

        # If compareAtLeast is set, check if result count is >= expected
        # Otherwise check for exact match
        if "compareAtLeast" in op.properties:
            cond = result_count >= expected_count
        else:
            cond = result_count == expected_count

        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.SwitchOperationNameOp)
    def run_switch_operation_name(
        self,
        interpreter: Interpreter,
        op: pdl_interp.SwitchOperationNameOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        input_op: Operation = args[0]
        op_name = input_op.name
        for name, block in zip(op.case_values, op.cases):
            if name.data == op_name:
                return Successor(block, ()), ()
        return Successor(op.default_dest, ()), ()

    @impl_terminator(pdl_interp.CheckAttributeOp)
    def run_check_attribute(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CheckAttributeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        # args[0] should be the attribute value to check
        attribute = args[0]
        # Compare with the constant value from properties
        cond = attribute == op.constantValue

        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.CheckTypeOp)
    def run_check_type(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CheckTypeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        value = args[0]
        cond = value == op.type

        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.IsNotNullOp)
    def run_is_not_null(
        self,
        interpreter: Interpreter,
        op: pdl_interp.IsNotNullOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        # Check if the value is not None
        cond = args[0] is not None
        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl_terminator(pdl_interp.AreEqualOp)
    def run_are_equal(
        self,
        interpreter: Interpreter,
        op: pdl_interp.AreEqualOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 2
        # Compare the two values for equality
        cond = args[0] == args[1]
        successor = op.true_dest if cond else op.false_dest
        return Successor(successor, ()), ()

    @impl(pdl_interp.ReplaceOp)
    def run_replace(
        self,
        interpreter: Interpreter,
        op: pdl_interp.ReplaceOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert args
        input_op = args[0]
        assert isinstance(input_op, Operation)

        # Get replacement values (if any)
        repl_values: list[SSAValue] = []
        for i in range(0, len(args) - 1):
            if isa(op.repl_values.types[i], ValueType):
                repl_values.append(args[i + 1])
            elif isa(op.repl_values.types[i], RangeType[ValueType]):
                repl_values.extend(args[i + 1])

        if len(input_op.results) != len(repl_values):
            raise InterpretationError(
                "Number of replacement values should match number of results"
            )
        # Replace the operation with the replacement values
        self.get_rewriter(interpreter).replace_op(
            input_op, new_ops=[], new_results=repl_values
        )
        return ()

    @impl(pdl_interp.CreateAttributeOp)
    def run_create_attribute(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CreateAttributeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        # Simply return the attribute value
        return (op.value,)

    @impl(pdl_interp.CreateTypeOp)
    def run_create_type(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CreateTypeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        # Simply return the type value
        return (op.value,)

    @impl(pdl_interp.CreateTypesOp)
    def run_create_types(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CreateTypesOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        # Return the list of types from the array attribute
        types = list(op.value.data)
        return (types,)

    @impl_terminator(pdl_interp.SwitchAttributeOp)
    def run_switch_attribute(
        self,
        interpreter: Interpreter,
        op: pdl_interp.SwitchAttributeOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        input_attr = args[0]
        for case_value, block in zip(op.caseValues.data, op.cases):
            if input_attr == case_value:
                return Successor(block, ()), ()
        return Successor(op.defaultDest, ()), ()

    @staticmethod
    def create_operation(
        interpreter: Interpreter,
        args: tuple[Any, ...],
        op_name: str,
        attr_names: list[str],
        num_operands: int,
        num_attributes: int,
    ) -> IRDLOperation:
        # Get operation name
        ctx = PDLInterpFunctions.get_ctx(interpreter)
        op_type = ctx.get_optional_op(op_name)
        if op_type is None:
            raise InterpretationError(
                f"Could not find op type for name {op_name} in context"
            )

        # Split args into operands, attributes and result types based on operand segments
        operands = list(args[:num_operands])

        assert issubclass(op_type, IRDLOperation)
        existing_properties = op_type.get_irdl_definition().properties.keys()

        attributes: dict[str, Attribute] = {}
        properties: dict[str, Attribute] = {}
        for name, prop_or_attr in zip(
            attr_names,
            args[num_operands : num_operands + num_attributes],
        ):
            if name in existing_properties:
                properties[name] = prop_or_attr
            else:
                attributes[name] = prop_or_attr
        result_types = list(args[num_operands + num_attributes :])

        # Create the new operation
        result_op = op_type.create(
            operands=operands,
            result_types=result_types,
            attributes=attributes,
            properties=properties,
        )
        return result_op

    @impl(pdl_interp.CreateOperationOp)
    def run_create_operation(
        self,
        interpreter: Interpreter,
        op: pdl_interp.CreateOperationOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        result_op = PDLInterpFunctions.create_operation(
            interpreter,
            args,
            op.constraint_name.data,
            [name.data for name in op.input_attribute_names.data],
            len(op.input_operands),
            len(op.input_attributes),
        )

        rewriter = self.get_rewriter(interpreter)
        rewriter.insert_op(result_op)

        return (result_op,)

    @impl_callable(pdl_interp.FuncOp)
    def call_func(
        self, interpreter: Interpreter, op: pdl_interp.FuncOp, args: tuple[Any, ...]
    ):
        if op.sym_name.data == "matcher":
            assert len(args) == 1
            root_op = args[0]
            assert isinstance(root_op, Operation)
            rewriter = self.get_rewriter(interpreter)
            rewriter.current_operation = root_op
            rewriter.insertion_point = InsertPoint.before(root_op)

        return interpreter.run_ssacfg_region(op.body, args, op.sym_name.data)

    @impl_terminator(pdl_interp.ApplyConstraintOp)
    def run_apply_constraint(
        self,
        interpreter: Interpreter,
        op: pdl_interp.ApplyConstraintOp,
        args: tuple[Any, ...],
    ) -> tuple[Successor, PythonValues]:
        constraint_name = op.constraint_name.data

        passed, results = interpreter.call_external(constraint_name, op, args)
        assert isinstance(passed, bool)
        assert isinstance(results, tuple)
        any_results = cast(tuple[Any], results)

        terminator = op.true_dest if passed != bool(op.is_negated) else op.false_dest

        return Successor(terminator, ()), any_results

    @impl_terminator(pdl_interp.RecordMatchOp)
    def run_recordmatch(
        self,
        interpreter: Interpreter,
        op: pdl_interp.RecordMatchOp,
        args: tuple[Any, ...],
    ):
        PDLInterpFunctions.get_pending_rewrites(interpreter).append(
            (
                op.rewriter,
                PDLInterpFunctions.get_rewriter(interpreter).current_operation,
                args,
            )
        )
        return Successor(op.dest, ()), ()

    @impl_terminator(pdl_interp.FinalizeOp)
    def run_finalize(
        self, interpreter: Interpreter, op: pdl_interp.FinalizeOp, args: tuple[Any, ...]
    ):
        return ReturnedValues(()), ()

    @impl_terminator(pdl_interp.ForEachOp)
    def run_foreach(
        self,
        interpreter: Interpreter,
        op: pdl_interp.ForEachOp,
        args: tuple[Any, ...],
    ) -> tuple[Any, ...]:
        assert len(args) == 1
        values = args[0]

        # Iterate over each value in the range
        for value in values:
            interpreter.run_ssacfg_region(op.region, (value,), "foreach")

        return Successor(op.successor, ()), ()

    @impl_terminator(pdl_interp.ContinueOp)
    def run_continue(
        self, interpreter: Interpreter, op: pdl_interp.ContinueOp, args: tuple[Any, ...]
    ):
        return ReturnedValues(args), ()

    def apply_pending_rewrites(self, interpreter: Interpreter):
        rewriter = PDLInterpFunctions.get_rewriter(interpreter)
        pending_rewrites = PDLInterpFunctions.get_pending_rewrites(interpreter)
        for rewriter_op, root, args in pending_rewrites:
            rewriter.current_operation = root
            rewriter.insertion_point = InsertPoint.before(root)

            interpreter.call_op(rewriter_op, args)
        pending_rewrites.clear()

get_ctx(interpreter: Interpreter) -> Context staticmethod

Source code in xdsl/interpreters/pdl_interp.py
52
53
54
55
56
57
58
@staticmethod
def get_ctx(interpreter: Interpreter) -> Context:
    return interpreter.get_data(
        PDLInterpFunctions,
        "ctx",
        lambda: Context(),
    )

set_ctx(interpreter: Interpreter, ctx: Context) -> None staticmethod

Source code in xdsl/interpreters/pdl_interp.py
60
61
62
63
64
65
66
@staticmethod
def set_ctx(interpreter: Interpreter, ctx: Context) -> None:
    interpreter.set_data(
        PDLInterpFunctions,
        "ctx",
        ctx,
    )

get_pending_rewrites(interpreter: Interpreter) -> list[tuple[SymbolRefAttr, Operation, tuple[Any, ...]]] staticmethod

Returns the list of pending rewrites to be executed. Each entry is a tuple of (rewriter, root, args).

Source code in xdsl/interpreters/pdl_interp.py
68
69
70
71
72
73
74
75
76
77
78
79
@staticmethod
def get_pending_rewrites(
    interpreter: Interpreter,
) -> list[tuple[SymbolRefAttr, Operation, tuple[Any, ...]]]:
    """
    Returns the list of pending rewrites to be executed. Each entry is a tuple of (rewriter, root, args).
    """
    return interpreter.get_data(
        PDLInterpFunctions,
        "pending_rewrites",
        lambda: [],
    )

get_rewriter(interpreter: Interpreter) -> PatternRewriter staticmethod

Source code in xdsl/interpreters/pdl_interp.py
81
82
83
84
85
86
87
88
89
90
@staticmethod
def get_rewriter(interpreter: Interpreter) -> PatternRewriter:
    rewriter: PatternRewriter | None = interpreter.get_data(
        PDLInterpFunctions, "rewriter", lambda: None
    )
    if rewriter is None:
        raise InterpretationError(
            "Expected an active rewriter when calling a pdl_interp function."
        )
    return rewriter

set_rewriter(interpreter: Interpreter, rewriter: PatternRewriter | None) -> None staticmethod

Source code in xdsl/interpreters/pdl_interp.py
92
93
94
95
96
@staticmethod
def set_rewriter(
    interpreter: Interpreter, rewriter: PatternRewriter | None
) -> None:
    interpreter.set_data(PDLInterpFunctions, "rewriter", rewriter)

run_get_operand(interpreter: Interpreter, op: pdl_interp.GetOperandOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@impl(pdl_interp.GetOperandOp)
def run_get_operand(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetOperandOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)
    if op.index.value.data >= len(args[0].operands):
        return (None,)
    else:
        return (args[0].operands[op.index.value.data],)

run_get_result(interpreter: Interpreter, op: pdl_interp.GetResultOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
112
113
114
115
116
117
118
119
120
121
122
123
@impl(pdl_interp.GetResultOp)
def run_get_result(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetResultOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)
    if len(args[0].results) <= op.index.value.data:
        return (None,)
    return (args[0].results[op.index.value.data],)

run_get_results(interpreter: Interpreter, op: pdl_interp.GetResultsOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
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
@impl(pdl_interp.GetResultsOp)
def run_get_results(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetResultsOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)
    src_op = args[0]
    assert isinstance(src_op, IRDLOperation)
    if op.index is not None:
        # get the field name of the result group:
        if op.index.value.data >= len(src_op.get_irdl_definition().results):
            return (None,)
        field = src_op.get_irdl_definition().results[op.index.value.data][0]
        results = getattr(src_op, field)
        if isa(results, OpResult):
            results = (results,)
    else:
        results = src_op.results

    if isinstance(op.result_types[0], ValueType):
        if len(results) != 1:
            return (None,)
        return (results[0],)
    return (results,)

run_get_attribute(interpreter: Interpreter, op: pdl_interp.GetAttributeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@impl(pdl_interp.GetAttributeOp)
def run_get_attribute(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetAttributeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)
    attrname = op.constraint_name.data
    if attrname in args[0].attributes:
        return (args[0].attributes[attrname],)
    elif attrname in args[0].properties:
        return (args[0].properties[attrname],)
    else:
        return (None,)

run_get_value_type(interpreter: Interpreter, op: pdl_interp.GetValueTypeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
170
171
172
173
174
175
176
177
178
179
180
@impl(pdl_interp.GetValueTypeOp)
def run_get_value_type(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetValueTypeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], SSAValue)
    value = cast(SSAValue, args[0])
    return (value.type,)

run_get_defining_op(interpreter: Interpreter, op: pdl_interp.GetDefiningOpOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
@impl(pdl_interp.GetDefiningOpOp)
def run_get_defining_op(
    self,
    interpreter: Interpreter,
    op: pdl_interp.GetDefiningOpOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    if args[0] is None:
        return (None,)
    assert isinstance(args[0], SSAValue)
    if not isinstance(args[0], OpResult):
        return (None,)
    return (args[0].owner,)

run_check_operation_name(interpreter: Interpreter, op: pdl_interp.CheckOperationNameOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
197
198
199
200
201
202
203
204
205
206
207
208
@impl_terminator(pdl_interp.CheckOperationNameOp)
def run_check_operation_name(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CheckOperationNameOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)
    cond = args[0].name == op.operation_name.data
    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_check_operand_count(interpreter: Interpreter, op: pdl_interp.CheckOperandCountOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
@impl_terminator(pdl_interp.CheckOperandCountOp)
def run_check_operand_count(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CheckOperandCountOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)

    operand_count = len(args[0].operands)
    expected_count = op.count.value.data

    # If compareAtLeast is set, check if operand count is >= expected
    # Otherwise check for exact match
    if "compareAtLeast" in op.properties:
        cond = operand_count >= expected_count
    else:
        cond = operand_count == expected_count

    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_check_result_count(interpreter: Interpreter, op: pdl_interp.CheckResultCountOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
@impl_terminator(pdl_interp.CheckResultCountOp)
def run_check_result_count(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CheckResultCountOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    assert isinstance(args[0], Operation)

    result_count = len(args[0].results)
    expected_count = op.count.value.data

    # If compareAtLeast is set, check if result count is >= expected
    # Otherwise check for exact match
    if "compareAtLeast" in op.properties:
        cond = result_count >= expected_count
    else:
        cond = result_count == expected_count

    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_switch_operation_name(interpreter: Interpreter, op: pdl_interp.SwitchOperationNameOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
256
257
258
259
260
261
262
263
264
265
266
267
268
@impl_terminator(pdl_interp.SwitchOperationNameOp)
def run_switch_operation_name(
    self,
    interpreter: Interpreter,
    op: pdl_interp.SwitchOperationNameOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    input_op: Operation = args[0]
    op_name = input_op.name
    for name, block in zip(op.case_values, op.cases):
        if name.data == op_name:
            return Successor(block, ()), ()
    return Successor(op.default_dest, ()), ()

run_check_attribute(interpreter: Interpreter, op: pdl_interp.CheckAttributeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@impl_terminator(pdl_interp.CheckAttributeOp)
def run_check_attribute(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CheckAttributeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    # args[0] should be the attribute value to check
    attribute = args[0]
    # Compare with the constant value from properties
    cond = attribute == op.constantValue

    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_check_type(interpreter: Interpreter, op: pdl_interp.CheckTypeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
286
287
288
289
290
291
292
293
294
295
296
297
298
@impl_terminator(pdl_interp.CheckTypeOp)
def run_check_type(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CheckTypeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    value = args[0]
    cond = value == op.type

    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_is_not_null(interpreter: Interpreter, op: pdl_interp.IsNotNullOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
300
301
302
303
304
305
306
307
308
309
310
311
@impl_terminator(pdl_interp.IsNotNullOp)
def run_is_not_null(
    self,
    interpreter: Interpreter,
    op: pdl_interp.IsNotNullOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    # Check if the value is not None
    cond = args[0] is not None
    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_are_equal(interpreter: Interpreter, op: pdl_interp.AreEqualOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
313
314
315
316
317
318
319
320
321
322
323
324
@impl_terminator(pdl_interp.AreEqualOp)
def run_are_equal(
    self,
    interpreter: Interpreter,
    op: pdl_interp.AreEqualOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 2
    # Compare the two values for equality
    cond = args[0] == args[1]
    successor = op.true_dest if cond else op.false_dest
    return Successor(successor, ()), ()

run_replace(interpreter: Interpreter, op: pdl_interp.ReplaceOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
@impl(pdl_interp.ReplaceOp)
def run_replace(
    self,
    interpreter: Interpreter,
    op: pdl_interp.ReplaceOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert args
    input_op = args[0]
    assert isinstance(input_op, Operation)

    # Get replacement values (if any)
    repl_values: list[SSAValue] = []
    for i in range(0, len(args) - 1):
        if isa(op.repl_values.types[i], ValueType):
            repl_values.append(args[i + 1])
        elif isa(op.repl_values.types[i], RangeType[ValueType]):
            repl_values.extend(args[i + 1])

    if len(input_op.results) != len(repl_values):
        raise InterpretationError(
            "Number of replacement values should match number of results"
        )
    # Replace the operation with the replacement values
    self.get_rewriter(interpreter).replace_op(
        input_op, new_ops=[], new_results=repl_values
    )
    return ()

run_create_attribute(interpreter: Interpreter, op: pdl_interp.CreateAttributeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
355
356
357
358
359
360
361
362
363
@impl(pdl_interp.CreateAttributeOp)
def run_create_attribute(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CreateAttributeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    # Simply return the attribute value
    return (op.value,)

run_create_type(interpreter: Interpreter, op: pdl_interp.CreateTypeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
365
366
367
368
369
370
371
372
373
@impl(pdl_interp.CreateTypeOp)
def run_create_type(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CreateTypeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    # Simply return the type value
    return (op.value,)

run_create_types(interpreter: Interpreter, op: pdl_interp.CreateTypesOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
375
376
377
378
379
380
381
382
383
384
@impl(pdl_interp.CreateTypesOp)
def run_create_types(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CreateTypesOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    # Return the list of types from the array attribute
    types = list(op.value.data)
    return (types,)

run_switch_attribute(interpreter: Interpreter, op: pdl_interp.SwitchAttributeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
386
387
388
389
390
391
392
393
394
395
396
397
398
@impl_terminator(pdl_interp.SwitchAttributeOp)
def run_switch_attribute(
    self,
    interpreter: Interpreter,
    op: pdl_interp.SwitchAttributeOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    input_attr = args[0]
    for case_value, block in zip(op.caseValues.data, op.cases):
        if input_attr == case_value:
            return Successor(block, ()), ()
    return Successor(op.defaultDest, ()), ()

create_operation(interpreter: Interpreter, args: tuple[Any, ...], op_name: str, attr_names: list[str], num_operands: int, num_attributes: int) -> IRDLOperation staticmethod

Source code in xdsl/interpreters/pdl_interp.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
@staticmethod
def create_operation(
    interpreter: Interpreter,
    args: tuple[Any, ...],
    op_name: str,
    attr_names: list[str],
    num_operands: int,
    num_attributes: int,
) -> IRDLOperation:
    # Get operation name
    ctx = PDLInterpFunctions.get_ctx(interpreter)
    op_type = ctx.get_optional_op(op_name)
    if op_type is None:
        raise InterpretationError(
            f"Could not find op type for name {op_name} in context"
        )

    # Split args into operands, attributes and result types based on operand segments
    operands = list(args[:num_operands])

    assert issubclass(op_type, IRDLOperation)
    existing_properties = op_type.get_irdl_definition().properties.keys()

    attributes: dict[str, Attribute] = {}
    properties: dict[str, Attribute] = {}
    for name, prop_or_attr in zip(
        attr_names,
        args[num_operands : num_operands + num_attributes],
    ):
        if name in existing_properties:
            properties[name] = prop_or_attr
        else:
            attributes[name] = prop_or_attr
    result_types = list(args[num_operands + num_attributes :])

    # Create the new operation
    result_op = op_type.create(
        operands=operands,
        result_types=result_types,
        attributes=attributes,
        properties=properties,
    )
    return result_op

run_create_operation(interpreter: Interpreter, op: pdl_interp.CreateOperationOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
@impl(pdl_interp.CreateOperationOp)
def run_create_operation(
    self,
    interpreter: Interpreter,
    op: pdl_interp.CreateOperationOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    result_op = PDLInterpFunctions.create_operation(
        interpreter,
        args,
        op.constraint_name.data,
        [name.data for name in op.input_attribute_names.data],
        len(op.input_operands),
        len(op.input_attributes),
    )

    rewriter = self.get_rewriter(interpreter)
    rewriter.insert_op(result_op)

    return (result_op,)

call_func(interpreter: Interpreter, op: pdl_interp.FuncOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/pdl_interp.py
465
466
467
468
469
470
471
472
473
474
475
476
477
@impl_callable(pdl_interp.FuncOp)
def call_func(
    self, interpreter: Interpreter, op: pdl_interp.FuncOp, args: tuple[Any, ...]
):
    if op.sym_name.data == "matcher":
        assert len(args) == 1
        root_op = args[0]
        assert isinstance(root_op, Operation)
        rewriter = self.get_rewriter(interpreter)
        rewriter.current_operation = root_op
        rewriter.insertion_point = InsertPoint.before(root_op)

    return interpreter.run_ssacfg_region(op.body, args, op.sym_name.data)

run_apply_constraint(interpreter: Interpreter, op: pdl_interp.ApplyConstraintOp, args: tuple[Any, ...]) -> tuple[Successor, PythonValues]

Source code in xdsl/interpreters/pdl_interp.py
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
@impl_terminator(pdl_interp.ApplyConstraintOp)
def run_apply_constraint(
    self,
    interpreter: Interpreter,
    op: pdl_interp.ApplyConstraintOp,
    args: tuple[Any, ...],
) -> tuple[Successor, PythonValues]:
    constraint_name = op.constraint_name.data

    passed, results = interpreter.call_external(constraint_name, op, args)
    assert isinstance(passed, bool)
    assert isinstance(results, tuple)
    any_results = cast(tuple[Any], results)

    terminator = op.true_dest if passed != bool(op.is_negated) else op.false_dest

    return Successor(terminator, ()), any_results

run_recordmatch(interpreter: Interpreter, op: pdl_interp.RecordMatchOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/pdl_interp.py
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
@impl_terminator(pdl_interp.RecordMatchOp)
def run_recordmatch(
    self,
    interpreter: Interpreter,
    op: pdl_interp.RecordMatchOp,
    args: tuple[Any, ...],
):
    PDLInterpFunctions.get_pending_rewrites(interpreter).append(
        (
            op.rewriter,
            PDLInterpFunctions.get_rewriter(interpreter).current_operation,
            args,
        )
    )
    return Successor(op.dest, ()), ()

run_finalize(interpreter: Interpreter, op: pdl_interp.FinalizeOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/pdl_interp.py
513
514
515
516
517
@impl_terminator(pdl_interp.FinalizeOp)
def run_finalize(
    self, interpreter: Interpreter, op: pdl_interp.FinalizeOp, args: tuple[Any, ...]
):
    return ReturnedValues(()), ()

run_foreach(interpreter: Interpreter, op: pdl_interp.ForEachOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl_interp.py
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
@impl_terminator(pdl_interp.ForEachOp)
def run_foreach(
    self,
    interpreter: Interpreter,
    op: pdl_interp.ForEachOp,
    args: tuple[Any, ...],
) -> tuple[Any, ...]:
    assert len(args) == 1
    values = args[0]

    # Iterate over each value in the range
    for value in values:
        interpreter.run_ssacfg_region(op.region, (value,), "foreach")

    return Successor(op.successor, ()), ()

run_continue(interpreter: Interpreter, op: pdl_interp.ContinueOp, args: tuple[Any, ...])

Source code in xdsl/interpreters/pdl_interp.py
535
536
537
538
539
@impl_terminator(pdl_interp.ContinueOp)
def run_continue(
    self, interpreter: Interpreter, op: pdl_interp.ContinueOp, args: tuple[Any, ...]
):
    return ReturnedValues(args), ()

apply_pending_rewrites(interpreter: Interpreter)

Source code in xdsl/interpreters/pdl_interp.py
541
542
543
544
545
546
547
548
549
def apply_pending_rewrites(self, interpreter: Interpreter):
    rewriter = PDLInterpFunctions.get_rewriter(interpreter)
    pending_rewrites = PDLInterpFunctions.get_pending_rewrites(interpreter)
    for rewriter_op, root, args in pending_rewrites:
        rewriter.current_operation = root
        rewriter.insertion_point = InsertPoint.before(root)

        interpreter.call_op(rewriter_op, args)
    pending_rewrites.clear()