Skip to content

Commit

Permalink
transformations: (pdl) Different type matching between polymorphic an…
Browse files Browse the repository at this point in the history
…d fixed case (#3405)

Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
  • Loading branch information
qaco and superlopuh authored Nov 11, 2024
1 parent f3414f3 commit c5a1c4e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
26 changes: 26 additions & 0 deletions tests/filecheck/interpreters/test_pdl_match_type.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: xdsl-opt %s -p apply-pdl | filecheck %s

%x = arith.constant 42: i32
%y = arith.constant 84: i64

pdl.pattern : benefit(1) {
%in_type = pdl.type: i32
%value = pdl.attribute: %in_type
%constant_op = pdl.operation "arith.constant" {"value" = %value} -> (%in_type: !pdl.type)
pdl.rewrite %constant_op {
pdl.erase %constant_op
}
}

// CHECK: builtin.module {
// CHECK-NEXT: %y = arith.constant 84 : i64
// CHECK-NEXT: pdl.pattern : benefit(1) {
// CHECK-NEXT: %in_type = pdl.type : i32
// CHECK-NEXT: %value = pdl.attribute : %in_type
// CHECK-NEXT: %constant_op = pdl.operation "arith.constant" {"value" = %value} -> (%in_type : !pdl.type)
// CHECK-NEXT: pdl.rewrite %constant_op {
// CHECK-NEXT: pdl.erase %constant_op
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT:
25 changes: 25 additions & 0 deletions tests/filecheck/interpreters/test_pdl_polymorphic.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: xdsl-opt %s -p apply-pdl | filecheck %s

%x = arith.constant 42: i32
%y = arith.constant 84: i64

pdl.pattern : benefit(1) {
%in_type = pdl.type
%value = pdl.attribute
%constant_op = pdl.operation "arith.constant" {"value" = %value} -> (%in_type: !pdl.type)
pdl.rewrite %constant_op {
pdl.erase %constant_op
}
}

// CHECK: builtin.module {
// CHECK-NEXT: pdl.pattern : benefit(1) {
// CHECK-NEXT: %in_type = pdl.type
// CHECK-NEXT: %value = pdl.attribute
// CHECK-NEXT: %constant_op = pdl.operation "arith.constant" {"value" = %value} -> (%in_type : !pdl.type)
// CHECK-NEXT: pdl.rewrite %constant_op {
// CHECK-NEXT: pdl.erase %constant_op
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT:
42 changes: 42 additions & 0 deletions tests/interpreters/test_pdl_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,48 @@ def even_length_string(attr: Attribute) -> bool:
assert new_input_module_true.is_structurally_equivalent(input_module_true)


def test_match_type():
matcher = PDLMatcher()

pdl_op = pdl.TypeOp()
ssa_value = pdl_op.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 = PDLMatcher()

pdl_op = pdl.TypeOp(IntegerType(32))
xdsl_value = IntegerType(32)
ssa_value = pdl_op.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 = PDLMatcher()

pdl_op = pdl.TypeOp(IntegerType(64))
xdsl_value = IntegerType(32)
ssa_value = pdl_op.result

assert not matcher.match_type(ssa_value, pdl_op, xdsl_value)
assert matcher.matching_context == {}


def test_native_constraint_constant_parameter():
"""
Check that `pdl.apply_native_constraint` can take constant attribute parameters
Expand Down
8 changes: 5 additions & 3 deletions xdsl/interpreters/pdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ 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

self.matching_context[ssa_val] = xdsl_attr

return True
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,
Expand Down

0 comments on commit c5a1c4e

Please sign in to comment.