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)
|