Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewriting: Fix TypeConversionPattern recursive support for DictionaryAttr #2374

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/pattern_rewriter/test_pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,34 @@ def convert_type(self, typ: IntegerType) -> IndexType | None:
op_modified=1,
)

prog = """\
"builtin.module"() ({
"test.op"() {"dict_nest" = {"hello" = i32}} : () -> ()
}) : () -> ()
"""

expected_recursive = """
"builtin.module"() ({
"test.op"() {"dict_nest" = {"hello" = index}} : () -> ()
}) : () -> ()
"""

rewrite_and_compare(
prog,
prog,
PatternRewriteWalker(Rewrite(recursive=False)),
expect_rewrite=False,
)

rewrite_and_compare(
prog,
expected_recursive,
PatternRewriteWalker(Rewrite(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
5 changes: 4 additions & 1 deletion xdsl/pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import TypeVar, Union, final, get_args, get_origin

from xdsl.builder import BuilderListener, InsertPoint
from xdsl.dialects.builtin import ArrayAttr, ModuleOp
from xdsl.dialects.builtin import ArrayAttr, DictionaryAttr, ModuleOp
from xdsl.ir import (
Attribute,
Block,
Expand Down Expand Up @@ -441,6 +441,9 @@ def _convert_type_rec(self, typ: Attribute) -> Attribute | None:
if isa(typ, ArrayAttr[Attribute]):
parameters = tuple(self._convert_type_rec(p) or p for p in typ)
inp = type(typ).new(parameters)
if isa(typ, DictionaryAttr):
parameters = {k: self._convert_type_rec(v) for k, v in typ.data.items()}
inp = type(typ).new(parameters)
converted = self.convert_type(inp)
return converted if converted is not None else inp

Expand Down
Loading