Skip to content

Pdl

pdl

PDLMatcher dataclass

Tracks the xDSL values corresponding to PDL SSA values during interpretation. A new instance is created per operation being checked against.

Source code in xdsl/interpreters/pdl.py
 18
 19
 20
 21
 22
 23
 24
 25
 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
@dataclass
class PDLMatcher:
    """
    Tracks the xDSL values corresponding to PDL SSA values during
    interpretation. A new instance is created per operation being checked
    against.
    """

    matching_context: dict[SSAValue, Operation | Attribute | SSAValue] = field(
        default_factory=dict[SSAValue, Operation | Attribute | SSAValue]
    )
    """
    For each SSAValue that is an OpResult of an operation in the PDL dialect,
    the corresponding xDSL object.
    """

    native_constraints: dict[str, Callable[..., bool]] = field(
        default_factory=lambda: {}
    )
    """
    The functions that can be used in `pdl.apply_native_constraint`. Note that we do
    not verify that the functions are used with the correct types.
    """

    def get_constant_or_matched_value(
        self, ssa_val: SSAValue
    ) -> Operation | Attribute | SSAValue:
        """
        Get the value that is already matched, or that is defined by a constant such as
        the result of a constant `pdl.attribute`, or of a `pdl.type`, or of a matched
        operation.
        """
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val]
        if isinstance(ssa_val.owner, pdl.AttributeOp):
            if ssa_val.owner.value is None:
                raise InterpretationError("expected constant `pdl.attribute`")
            return ssa_val.owner.value
        if isinstance(ssa_val.owner, pdl.TypeOp):
            if ssa_val.owner.constantType is None:
                raise InterpretationError("expected constant `pdl.type`")
            return ssa_val.owner.constantType
        raise InterpretationError("expected constant or matched value")

    def match_operand(
        self, ssa_val: SSAValue, pdl_op: pdl.OperandOp, xdsl_val: SSAValue
    ):
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val] == xdsl_val

        if pdl_op.value_type is not None:
            assert isinstance(pdl_op.value_type, OpResult)
            assert isinstance(pdl_op.value_type.op, pdl.TypeOp)

            if not self.match_type(
                pdl_op.value_type, pdl_op.value_type.op, xdsl_val.type
            ):
                return False

        self.matching_context[ssa_val] = xdsl_val

        return True

    def match_result(
        self, ssa_val: SSAValue, pdl_op: pdl.ResultOp, xdsl_operand: SSAValue
    ):
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val] == xdsl_operand

        root_pdl_op_value = pdl_op.parent_
        assert isinstance(root_pdl_op_value, OpResult)
        assert isinstance(root_pdl_op_value.op, pdl.OperationOp)

        if not isinstance(xdsl_operand, OpResult):
            return False

        xdsl_op = xdsl_operand.op

        if not self.match_operation(root_pdl_op_value, root_pdl_op_value.op, xdsl_op):
            return False

        original_op = root_pdl_op_value.op

        index = pdl_op.index.value.data

        if original_op.type_values and len(original_op.type_values) <= index:
            return False

        self.matching_context[ssa_val] = xdsl_op.results[index]

        return True

    def match_type(self, ssa_val: SSAValue, pdl_op: pdl.TypeOp, xdsl_attr: Attribute):
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val] == xdsl_attr

        if pdl_op.constantType is None or pdl_op.constantType == xdsl_attr:
            self.matching_context[ssa_val] = xdsl_attr
            return True
        else:
            return False

    def match_attribute(
        self,
        ssa_val: SSAValue,
        pdl_op: pdl.AttributeOp,
        attr_name: str,
        xdsl_attr: Attribute,
    ):
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val] == xdsl_attr

        if pdl_op.value is not None:
            if pdl_op.value != xdsl_attr:
                return False

        if pdl_op.value_type is not None:
            assert isinstance(pdl_op.value_type, OpResult)
            assert isinstance(pdl_op.value_type.op, pdl.TypeOp)

            assert isa(xdsl_attr, IntegerAttr[IntegerType]), (
                "Only handle integer types for now"
            )

            if not self.match_type(
                pdl_op.value_type, pdl_op.value_type.op, xdsl_attr.type
            ):
                return False

        self.matching_context[ssa_val] = xdsl_attr

        return True

    def match_operation(
        self, ssa_val: SSAValue, pdl_op: pdl.OperationOp, xdsl_op: Operation
    ) -> bool:
        if ssa_val in self.matching_context:
            return self.matching_context[ssa_val] == xdsl_op

        if pdl_op.opName is not None:
            if xdsl_op.name != pdl_op.opName.data:
                return False

        attribute_value_names = [avn.data for avn in pdl_op.attributeValueNames.data]

        for avn, av in zip(attribute_value_names, pdl_op.attribute_values):
            assert isinstance(av, OpResult)
            assert isinstance(av.op, pdl.AttributeOp)
            if (attr := xdsl_op.get_attr_or_prop(avn)) is None:
                return False

            if not self.match_attribute(av, av.op, avn, attr):
                return False

        pdl_operands = pdl_op.operand_values
        xdsl_operands = xdsl_op.operands

        if len(pdl_operands) != len(xdsl_operands):
            return False

        for pdl_operand, xdsl_operand in zip(pdl_operands, xdsl_operands):
            assert isinstance(pdl_operand, OpResult)
            assert isinstance(pdl_operand.op, pdl.OperandOp | pdl.ResultOp)
            match pdl_operand.op:
                case pdl.OperandOp():
                    if not self.match_operand(
                        pdl_operand, pdl_operand.op, xdsl_operand
                    ):
                        return False
                case pdl.ResultOp():
                    if not self.match_result(pdl_operand, pdl_operand.op, xdsl_operand):
                        return False

        pdl_results = pdl_op.type_values
        xdsl_results = xdsl_op.results

        if len(pdl_results) != len(xdsl_results):
            return False

        for pdl_result, xdsl_result in zip(pdl_results, xdsl_results):
            assert isinstance(pdl_result, OpResult)
            assert isinstance(pdl_result.op, pdl.TypeOp)
            if not self.match_type(pdl_result, pdl_result.op, xdsl_result.type):
                return False

        self.matching_context[ssa_val] = xdsl_op

        return True

    def check_native_constraints(self, pdl_op: pdl.ApplyNativeConstraintOp) -> bool:
        args = [
            self.get_constant_or_matched_value(operand) for operand in pdl_op.operands
        ]
        name = pdl_op.constraint_name.data
        if name not in self.native_constraints:
            raise InterpretationError(f"{name} PDL native constraint is not registered")
        return self.native_constraints[name](*args)

matching_context: dict[SSAValue, Operation | Attribute | SSAValue] = field(default_factory=(dict[SSAValue, Operation | Attribute | SSAValue])) class-attribute instance-attribute

For each SSAValue that is an OpResult of an operation in the PDL dialect, the corresponding xDSL object.

native_constraints: dict[str, Callable[..., bool]] = field(default_factory=(lambda: {})) class-attribute instance-attribute

The functions that can be used in pdl.apply_native_constraint. Note that we do not verify that the functions are used with the correct types.

__init__(matching_context: dict[SSAValue, Operation | Attribute | SSAValue] = dict[SSAValue, Operation | Attribute | SSAValue](), native_constraints: dict[str, Callable[..., bool]] = (lambda: {})()) -> None

get_constant_or_matched_value(ssa_val: SSAValue) -> Operation | Attribute | SSAValue

Get the value that is already matched, or that is defined by a constant such as the result of a constant pdl.attribute, or of a pdl.type, or of a matched operation.

Source code in xdsl/interpreters/pdl.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def get_constant_or_matched_value(
    self, ssa_val: SSAValue
) -> Operation | Attribute | SSAValue:
    """
    Get the value that is already matched, or that is defined by a constant such as
    the result of a constant `pdl.attribute`, or of a `pdl.type`, or of a matched
    operation.
    """
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val]
    if isinstance(ssa_val.owner, pdl.AttributeOp):
        if ssa_val.owner.value is None:
            raise InterpretationError("expected constant `pdl.attribute`")
        return ssa_val.owner.value
    if isinstance(ssa_val.owner, pdl.TypeOp):
        if ssa_val.owner.constantType is None:
            raise InterpretationError("expected constant `pdl.type`")
        return ssa_val.owner.constantType
    raise InterpretationError("expected constant or matched value")

match_operand(ssa_val: SSAValue, pdl_op: pdl.OperandOp, xdsl_val: SSAValue)

Source code in xdsl/interpreters/pdl.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def match_operand(
    self, ssa_val: SSAValue, pdl_op: pdl.OperandOp, xdsl_val: SSAValue
):
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val] == xdsl_val

    if pdl_op.value_type is not None:
        assert isinstance(pdl_op.value_type, OpResult)
        assert isinstance(pdl_op.value_type.op, pdl.TypeOp)

        if not self.match_type(
            pdl_op.value_type, pdl_op.value_type.op, xdsl_val.type
        ):
            return False

    self.matching_context[ssa_val] = xdsl_val

    return True

match_result(ssa_val: SSAValue, pdl_op: pdl.ResultOp, xdsl_operand: SSAValue)

Source code in xdsl/interpreters/pdl.py
 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
def match_result(
    self, ssa_val: SSAValue, pdl_op: pdl.ResultOp, xdsl_operand: SSAValue
):
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val] == xdsl_operand

    root_pdl_op_value = pdl_op.parent_
    assert isinstance(root_pdl_op_value, OpResult)
    assert isinstance(root_pdl_op_value.op, pdl.OperationOp)

    if not isinstance(xdsl_operand, OpResult):
        return False

    xdsl_op = xdsl_operand.op

    if not self.match_operation(root_pdl_op_value, root_pdl_op_value.op, xdsl_op):
        return False

    original_op = root_pdl_op_value.op

    index = pdl_op.index.value.data

    if original_op.type_values and len(original_op.type_values) <= index:
        return False

    self.matching_context[ssa_val] = xdsl_op.results[index]

    return True

match_type(ssa_val: SSAValue, pdl_op: pdl.TypeOp, xdsl_attr: Attribute)

Source code in xdsl/interpreters/pdl.py
110
111
112
113
114
115
116
117
118
def match_type(self, ssa_val: SSAValue, pdl_op: pdl.TypeOp, xdsl_attr: Attribute):
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val] == xdsl_attr

    if pdl_op.constantType is None or pdl_op.constantType == xdsl_attr:
        self.matching_context[ssa_val] = xdsl_attr
        return True
    else:
        return False

match_attribute(ssa_val: SSAValue, pdl_op: pdl.AttributeOp, attr_name: str, xdsl_attr: Attribute)

Source code in xdsl/interpreters/pdl.py
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
def match_attribute(
    self,
    ssa_val: SSAValue,
    pdl_op: pdl.AttributeOp,
    attr_name: str,
    xdsl_attr: Attribute,
):
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val] == xdsl_attr

    if pdl_op.value is not None:
        if pdl_op.value != xdsl_attr:
            return False

    if pdl_op.value_type is not None:
        assert isinstance(pdl_op.value_type, OpResult)
        assert isinstance(pdl_op.value_type.op, pdl.TypeOp)

        assert isa(xdsl_attr, IntegerAttr[IntegerType]), (
            "Only handle integer types for now"
        )

        if not self.match_type(
            pdl_op.value_type, pdl_op.value_type.op, xdsl_attr.type
        ):
            return False

    self.matching_context[ssa_val] = xdsl_attr

    return True

match_operation(ssa_val: SSAValue, pdl_op: pdl.OperationOp, xdsl_op: Operation) -> bool

Source code in xdsl/interpreters/pdl.py
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
def match_operation(
    self, ssa_val: SSAValue, pdl_op: pdl.OperationOp, xdsl_op: Operation
) -> bool:
    if ssa_val in self.matching_context:
        return self.matching_context[ssa_val] == xdsl_op

    if pdl_op.opName is not None:
        if xdsl_op.name != pdl_op.opName.data:
            return False

    attribute_value_names = [avn.data for avn in pdl_op.attributeValueNames.data]

    for avn, av in zip(attribute_value_names, pdl_op.attribute_values):
        assert isinstance(av, OpResult)
        assert isinstance(av.op, pdl.AttributeOp)
        if (attr := xdsl_op.get_attr_or_prop(avn)) is None:
            return False

        if not self.match_attribute(av, av.op, avn, attr):
            return False

    pdl_operands = pdl_op.operand_values
    xdsl_operands = xdsl_op.operands

    if len(pdl_operands) != len(xdsl_operands):
        return False

    for pdl_operand, xdsl_operand in zip(pdl_operands, xdsl_operands):
        assert isinstance(pdl_operand, OpResult)
        assert isinstance(pdl_operand.op, pdl.OperandOp | pdl.ResultOp)
        match pdl_operand.op:
            case pdl.OperandOp():
                if not self.match_operand(
                    pdl_operand, pdl_operand.op, xdsl_operand
                ):
                    return False
            case pdl.ResultOp():
                if not self.match_result(pdl_operand, pdl_operand.op, xdsl_operand):
                    return False

    pdl_results = pdl_op.type_values
    xdsl_results = xdsl_op.results

    if len(pdl_results) != len(xdsl_results):
        return False

    for pdl_result, xdsl_result in zip(pdl_results, xdsl_results):
        assert isinstance(pdl_result, OpResult)
        assert isinstance(pdl_result.op, pdl.TypeOp)
        if not self.match_type(pdl_result, pdl_result.op, xdsl_result.type):
            return False

    self.matching_context[ssa_val] = xdsl_op

    return True

check_native_constraints(pdl_op: pdl.ApplyNativeConstraintOp) -> bool

Source code in xdsl/interpreters/pdl.py
207
208
209
210
211
212
213
214
def check_native_constraints(self, pdl_op: pdl.ApplyNativeConstraintOp) -> bool:
    args = [
        self.get_constant_or_matched_value(operand) for operand in pdl_op.operands
    ]
    name = pdl_op.constraint_name.data
    if name not in self.native_constraints:
        raise InterpretationError(f"{name} PDL native constraint is not registered")
    return self.native_constraints[name](*args)

PDLRewritePattern dataclass

Bases: RewritePattern

Source code in xdsl/interpreters/pdl.py
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
@dataclass
class PDLRewritePattern(RewritePattern):
    functions: PDLRewriteFunctions
    pdl_rewrite_op: pdl.RewriteOp
    interpreter: Interpreter
    native_constraints: dict[str, Callable[..., bool]]

    def __init__(
        self,
        pdl_rewrite_op: pdl.RewriteOp,
        ctx: Context,
        file: IO[str] | None = None,
        native_constraints: dict[str, Callable[..., bool]] | None = None,
    ):
        pdl_pattern = pdl_rewrite_op.parent_op()
        assert isinstance(pdl_pattern, pdl.PatternOp)
        pdl_module = pdl_pattern.parent_op()
        assert isinstance(pdl_module, ModuleOp)
        self.functions = PDLRewriteFunctions(ctx)
        self.interpreter = Interpreter(pdl_module, file=file)
        self.interpreter.register_implementations(self.functions)
        self.pdl_rewrite_op = pdl_rewrite_op
        if native_constraints is None:
            native_constraints = {}
        self.native_constraints = native_constraints

    def match_and_rewrite(self, xdsl_op: Operation, rewriter: PatternRewriter) -> None:
        pdl_op_val = self.pdl_rewrite_op.root
        assert pdl_op_val is not None, "TODO: handle None root op in pdl.RewriteOp"
        assert self.pdl_rewrite_op.body is not None, (
            "TODO: handle None body op in pdl.RewriteOp"
        )

        assert isinstance(pdl_op_val, OpResult)
        pdl_op = pdl_op_val.op

        assert isinstance(pdl_op, pdl.OperationOp)
        matcher = PDLMatcher(native_constraints=self.native_constraints)
        if not matcher.match_operation(pdl_op_val, pdl_op, xdsl_op):
            return

        parent = self.pdl_rewrite_op.parent_op()
        assert isinstance(parent, pdl.PatternOp)
        for constraint_op in parent.walk():
            if isinstance(constraint_op, pdl.ApplyNativeConstraintOp):
                if not matcher.check_native_constraints(constraint_op):
                    return

        self.interpreter.push_scope("rewrite")
        self.interpreter.set_values(matcher.matching_context.items())
        self.functions.rewriter = rewriter

        self.interpreter.run_ssacfg_region(self.pdl_rewrite_op.body, ())

        self.interpreter.pop_scope()

functions: PDLRewriteFunctions = PDLRewriteFunctions(ctx) instance-attribute

interpreter: Interpreter = Interpreter(pdl_module, file=file) instance-attribute

pdl_rewrite_op: pdl.RewriteOp = pdl_rewrite_op instance-attribute

native_constraints: dict[str, Callable[..., bool]] = native_constraints instance-attribute

__init__(pdl_rewrite_op: pdl.RewriteOp, ctx: Context, file: IO[str] | None = None, native_constraints: dict[str, Callable[..., bool]] | None = None)

Source code in xdsl/interpreters/pdl.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def __init__(
    self,
    pdl_rewrite_op: pdl.RewriteOp,
    ctx: Context,
    file: IO[str] | None = None,
    native_constraints: dict[str, Callable[..., bool]] | None = None,
):
    pdl_pattern = pdl_rewrite_op.parent_op()
    assert isinstance(pdl_pattern, pdl.PatternOp)
    pdl_module = pdl_pattern.parent_op()
    assert isinstance(pdl_module, ModuleOp)
    self.functions = PDLRewriteFunctions(ctx)
    self.interpreter = Interpreter(pdl_module, file=file)
    self.interpreter.register_implementations(self.functions)
    self.pdl_rewrite_op = pdl_rewrite_op
    if native_constraints is None:
        native_constraints = {}
    self.native_constraints = native_constraints

match_and_rewrite(xdsl_op: Operation, rewriter: PatternRewriter) -> None

Source code in xdsl/interpreters/pdl.py
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
def match_and_rewrite(self, xdsl_op: Operation, rewriter: PatternRewriter) -> None:
    pdl_op_val = self.pdl_rewrite_op.root
    assert pdl_op_val is not None, "TODO: handle None root op in pdl.RewriteOp"
    assert self.pdl_rewrite_op.body is not None, (
        "TODO: handle None body op in pdl.RewriteOp"
    )

    assert isinstance(pdl_op_val, OpResult)
    pdl_op = pdl_op_val.op

    assert isinstance(pdl_op, pdl.OperationOp)
    matcher = PDLMatcher(native_constraints=self.native_constraints)
    if not matcher.match_operation(pdl_op_val, pdl_op, xdsl_op):
        return

    parent = self.pdl_rewrite_op.parent_op()
    assert isinstance(parent, pdl.PatternOp)
    for constraint_op in parent.walk():
        if isinstance(constraint_op, pdl.ApplyNativeConstraintOp):
            if not matcher.check_native_constraints(constraint_op):
                return

    self.interpreter.push_scope("rewrite")
    self.interpreter.set_values(matcher.matching_context.items())
    self.functions.rewriter = rewriter

    self.interpreter.run_ssacfg_region(self.pdl_rewrite_op.body, ())

    self.interpreter.pop_scope()

PDLRewriteFunctions dataclass

Bases: InterpreterFunctions

The implementations in this class are for the RHS of the rewrite. The SSA values referenced within the rewrite block are guaranteed to have been matched with the corresponding IR elements. The interpreter context stores the IR elements by SSA values.

Source code in xdsl/interpreters/pdl.py
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
@register_impls
@dataclass
class PDLRewriteFunctions(InterpreterFunctions):
    """
    The implementations in this class are for the RHS of the rewrite. The SSA values
    referenced within the rewrite block are guaranteed to have been matched with the
    corresponding IR elements. The interpreter context stores the IR elements by SSA
    values.
    """

    ctx: Context
    _rewriter: PatternRewriter | None = field(default=None)

    @property
    def rewriter(self) -> PatternRewriter:
        assert self._rewriter is not None
        return self._rewriter

    @rewriter.setter
    def rewriter(self, rewriter: PatternRewriter):
        self._rewriter = rewriter

    @impl(pdl.OperationOp)
    def run_operation(
        self, interpreter: Interpreter, op: pdl.OperationOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        assert op.opName is not None
        op_name = op.opName.data
        op_type = self.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"
            )

        attribute_value_names = [avn.data for avn in op.attributeValueNames.data]

        # How to deal with operandSegmentSizes?
        # operand_values, attribute_values, type_values = args

        operand_values = interpreter.get_values(op.operand_values)
        for operand in operand_values:
            assert isinstance(operand, SSAValue)

        attribute_values = interpreter.get_values(op.attribute_values)

        for attribute in attribute_values:
            assert isinstance(attribute, Attribute)

        type_values = interpreter.get_values(op.type_values)

        for type_value in type_values:
            assert isinstance(type_value, TypeAttribute)

        attributes = dict[str, Attribute]()
        properties = dict[str, Attribute]()

        # If the op is an IRDL-defined operation, get the property names.
        if issubclass(op_type, IRDLOperation):
            property_names = op_type.get_irdl_definition().properties.keys()
        else:
            property_names = []

        # Move the attributes to the attribute or property dictionary
        # depending on whether they are a properties or not.
        for attribute_name, attribute_value in zip(
            attribute_value_names, attribute_values
        ):
            if attribute_name in property_names:
                properties[attribute_name] = attribute_value
            else:
                attributes[attribute_name] = attribute_value

        result_op = op_type.create(
            operands=operand_values,
            result_types=type_values,
            attributes=attributes,
            properties=properties,
        )

        # Insert the new operation before the root operation
        self.rewriter.insert_op(result_op)

        return (result_op,)

    @impl(pdl.ResultOp)
    def run_result(
        self, interpreter: Interpreter, op: pdl.ResultOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        (parent,) = args
        assert isinstance(parent, Operation)
        return (parent.results[op.index.value.data],)

    @impl(pdl.AttributeOp)
    def run_attribute(
        self, interpreter: Interpreter, op: pdl.AttributeOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        assert isinstance(op.value, Attribute)
        return (op.value,)

    @impl(pdl.ReplaceOp)
    def run_replace(
        self, interpreter: Interpreter, op: pdl.ReplaceOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        rewriter = self.rewriter

        (old,) = interpreter.get_values((op.op_value,))

        if op.repl_operation is not None:
            (new_op,) = interpreter.get_values((op.repl_operation,))
            rewriter.replace_op(old, new_ops=[], new_results=new_op.results)
        elif len(op.repl_values):
            new_vals = interpreter.get_values(op.repl_values)
            rewriter.replace_op(old, new_ops=[], new_results=list(new_vals))
        else:
            raise ValueError(
                "Either a replacing operatoin or values must be provided with replace op"
            )

        return ()

    @impl(pdl.EraseOp)
    def run_erase(
        self, interpreter: Interpreter, op: pdl.EraseOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        (old,) = interpreter.get_values((op.op_value,))
        self.rewriter.erase_op(old)
        return ()

    @impl(pdl.TypeOp)
    def run_type(
        self, interpreter: Interpreter, op: pdl.TypeOp, args: tuple[Any, ...]
    ) -> tuple[Any, ...]:
        assert isinstance(op.constantType, Attribute)
        return (op.constantType,)

ctx: Context instance-attribute

rewriter: PatternRewriter property writable

__init__(ctx: Context, _rewriter: PatternRewriter | None = None) -> None

run_operation(interpreter: Interpreter, op: pdl.OperationOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl.py
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
@impl(pdl.OperationOp)
def run_operation(
    self, interpreter: Interpreter, op: pdl.OperationOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    assert op.opName is not None
    op_name = op.opName.data
    op_type = self.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"
        )

    attribute_value_names = [avn.data for avn in op.attributeValueNames.data]

    # How to deal with operandSegmentSizes?
    # operand_values, attribute_values, type_values = args

    operand_values = interpreter.get_values(op.operand_values)
    for operand in operand_values:
        assert isinstance(operand, SSAValue)

    attribute_values = interpreter.get_values(op.attribute_values)

    for attribute in attribute_values:
        assert isinstance(attribute, Attribute)

    type_values = interpreter.get_values(op.type_values)

    for type_value in type_values:
        assert isinstance(type_value, TypeAttribute)

    attributes = dict[str, Attribute]()
    properties = dict[str, Attribute]()

    # If the op is an IRDL-defined operation, get the property names.
    if issubclass(op_type, IRDLOperation):
        property_names = op_type.get_irdl_definition().properties.keys()
    else:
        property_names = []

    # Move the attributes to the attribute or property dictionary
    # depending on whether they are a properties or not.
    for attribute_name, attribute_value in zip(
        attribute_value_names, attribute_values
    ):
        if attribute_name in property_names:
            properties[attribute_name] = attribute_value
        else:
            attributes[attribute_name] = attribute_value

    result_op = op_type.create(
        operands=operand_values,
        result_types=type_values,
        attributes=attributes,
        properties=properties,
    )

    # Insert the new operation before the root operation
    self.rewriter.insert_op(result_op)

    return (result_op,)

run_result(interpreter: Interpreter, op: pdl.ResultOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl.py
359
360
361
362
363
364
365
@impl(pdl.ResultOp)
def run_result(
    self, interpreter: Interpreter, op: pdl.ResultOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    (parent,) = args
    assert isinstance(parent, Operation)
    return (parent.results[op.index.value.data],)

run_attribute(interpreter: Interpreter, op: pdl.AttributeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl.py
367
368
369
370
371
372
@impl(pdl.AttributeOp)
def run_attribute(
    self, interpreter: Interpreter, op: pdl.AttributeOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    assert isinstance(op.value, Attribute)
    return (op.value,)

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

Source code in xdsl/interpreters/pdl.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
@impl(pdl.ReplaceOp)
def run_replace(
    self, interpreter: Interpreter, op: pdl.ReplaceOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    rewriter = self.rewriter

    (old,) = interpreter.get_values((op.op_value,))

    if op.repl_operation is not None:
        (new_op,) = interpreter.get_values((op.repl_operation,))
        rewriter.replace_op(old, new_ops=[], new_results=new_op.results)
    elif len(op.repl_values):
        new_vals = interpreter.get_values(op.repl_values)
        rewriter.replace_op(old, new_ops=[], new_results=list(new_vals))
    else:
        raise ValueError(
            "Either a replacing operatoin or values must be provided with replace op"
        )

    return ()

run_erase(interpreter: Interpreter, op: pdl.EraseOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl.py
395
396
397
398
399
400
401
@impl(pdl.EraseOp)
def run_erase(
    self, interpreter: Interpreter, op: pdl.EraseOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    (old,) = interpreter.get_values((op.op_value,))
    self.rewriter.erase_op(old)
    return ()

run_type(interpreter: Interpreter, op: pdl.TypeOp, args: tuple[Any, ...]) -> tuple[Any, ...]

Source code in xdsl/interpreters/pdl.py
403
404
405
406
407
408
@impl(pdl.TypeOp)
def run_type(
    self, interpreter: Interpreter, op: pdl.TypeOp, args: tuple[Any, ...]
) -> tuple[Any, ...]:
    assert isinstance(op.constantType, Attribute)
    return (op.constantType,)