-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interpreter: (pdl) add an initial version of EqSat PDLMatcher (#3574)
This is an initial attempt of adding a PDL matcher for EqSat e-class ops. --------- Co-authored-by: Jianyi Cheng <[email protected]>
- Loading branch information
1 parent
ce85340
commit dd58ef0
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from xdsl.dialects import eqsat, pdl | ||
from xdsl.dialects.builtin import ( | ||
IntegerType, | ||
StringAttr, | ||
) | ||
from xdsl.interpreters.eqsat_pdl import EqsatPDLMatcher | ||
|
||
|
||
def test_match_type(): | ||
matcher = EqsatPDLMatcher() | ||
|
||
pdl_op = pdl.TypeOp() | ||
ssa_value = pdl_op.result | ||
ssa_value = eqsat.EClassOp(ssa_value).result | ||
xdsl_value = StringAttr("a") | ||
|
||
# New value | ||
assert matcher.match_type(ssa_value, pdl_op, xdsl_value) | ||
assert matcher.matching_context == {ssa_value: xdsl_value} | ||
|
||
# Same value | ||
assert matcher.match_type(ssa_value, pdl_op, xdsl_value) | ||
assert matcher.matching_context == {ssa_value: xdsl_value} | ||
|
||
# Other value | ||
assert not matcher.match_type(ssa_value, pdl_op, StringAttr("b")) | ||
assert matcher.matching_context == {ssa_value: xdsl_value} | ||
|
||
|
||
def test_match_fixed_type(): | ||
matcher = EqsatPDLMatcher() | ||
|
||
pdl_op = pdl.TypeOp(IntegerType(32)) | ||
xdsl_value = IntegerType(32) | ||
ssa_value = pdl_op.result | ||
ssa_value = eqsat.EClassOp(ssa_value).result | ||
|
||
assert matcher.match_type(ssa_value, pdl_op, xdsl_value) | ||
assert matcher.matching_context == {ssa_value: xdsl_value} | ||
|
||
|
||
def test_not_match_fixed_type(): | ||
matcher = EqsatPDLMatcher() | ||
|
||
pdl_op = pdl.TypeOp(IntegerType(64)) | ||
xdsl_value = IntegerType(32) | ||
ssa_value = pdl_op.result | ||
ssa_value = eqsat.EClassOp(ssa_value).result | ||
|
||
assert not matcher.match_type(ssa_value, pdl_op, xdsl_value) | ||
assert matcher.matching_context == {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from xdsl.dialects import eqsat, pdl | ||
from xdsl.interpreters.pdl import PDLMatcher | ||
from xdsl.ir import SSAValue | ||
|
||
|
||
@dataclass | ||
class EqsatPDLMatcher(PDLMatcher): | ||
def match_operand( | ||
self, ssa_val: SSAValue, pdl_op: pdl.OperandOp, xdsl_val: SSAValue | ||
): | ||
owner = xdsl_val.owner | ||
assert isinstance(owner, eqsat.EClassOp) | ||
assert ( | ||
len(owner.operands) == 1 | ||
), "newly converted eqsat always has 1 element in eclass" | ||
arg = owner.operands[0] | ||
res = super().match_operand(ssa_val, pdl_op, arg) | ||
return res |