Skip to content

Commit

Permalink
core: (rewriting) Fix recursive type conversion in block arguments fo…
Browse files Browse the repository at this point in the history
…r nested regions (#2871)

closes #2857 

Solves the issue mentioned there and adds the reported testcase as a
regression test.

Before, this was actually quite broken as not applying the recursive
rewrite, but the normal rewrite, to the region would actually mean that
the produced func_op did not even verify (the `function_type` attribute
got rewritten properly, whereas the entry_block args would still have
the old type).

The reason why the old type was being printed is that the
`print_func_op_like`-utility function defaults to printing the
entry_block arguments in some cases.
  • Loading branch information
webmiche authored Jul 10, 2024
1 parent 2469213 commit 57e3075
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions tests/pattern_rewriter/test_pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,41 @@ def convert_type(self, typ: IntegerType) -> IndexType | None:
)


def test_recursive_type_conversion_in_regions():

prog = """\
"builtin.module"() ({
"func.func"() <{"function_type" = (memref<2x4xui16>) -> (), "sym_name" = "main", "sym_visibility" = "private"}> ({
^bb0(%arg0 : memref<2x4xui16>):
"func.return"() : () -> ()
}) : () -> ()
}) : () -> ()
"""
expected_prog = """\
"builtin.module"() ({
"func.func"() <{"function_type" = (memref<2x4xindex>) -> (), "sym_name" = "main", "sym_visibility" = "private"}> ({
^0(%arg0 : memref<2x4xindex>):
"func.return"() : () -> ()
}) : () -> ()
}) : () -> ()
"""

class IndexConversion(TypeConversionPattern):

@attr_type_rewrite_pattern
def convert_type(self, typ: IntegerType) -> IndexType:
return IndexType()

rewrite_and_compare(
prog,
expected_prog,
PatternRewriteWalker(IndexConversion(recursive=True)),
op_inserted=1,
op_removed=1,
op_replaced=1,
)


def test_no_change():
"""Test that doing nothing successfully does not report doing something."""

Expand Down
2 changes: 1 addition & 1 deletion xdsl/pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def match_and_rewrite(self, op: Operation, rewriter: PatternRewriter):
for region in op.regions:
for block in region.blocks:
for arg in block.args:
converted = self.convert_type(arg.type)
converted = self._convert_type_rec(arg.type)
if converted is not None and converted != arg.type:
rewriter.modify_block_argument_type(arg, converted)
if changed:
Expand Down

0 comments on commit 57e3075

Please sign in to comment.