Skip to content

Commit

Permalink
core: Walk regions for rewritings instead of ops
Browse files Browse the repository at this point in the history
Currently, the pattern rewriter walker walks an operation, and tries to
apply patterns on all contained operations, including itself.
This is not what we would want, as rewriting a toplevel operation is
dangerous, if that operation does not have a parent.

This commit changes the walker to walk regions instead of operations, so
it is clear that the toplevel operation will not be modified. This is
similar to what MLIR currently does in applyPatternsAndFoldGreedily.
  • Loading branch information
math-fehr committed Nov 22, 2024
1 parent 020cae7 commit 39a2e0a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 63 deletions.
4 changes: 2 additions & 2 deletions docs/database_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
")\n",
"\n",
"\n",
"walker.rewrite_op(sel)"
"walker.rewrite_region(sel.filter)"
]
},
{
Expand Down Expand Up @@ -387,7 +387,7 @@
" walk_reverse=False,\n",
")\n",
"\n",
"walker.rewrite_op(sel)"
"walker.rewrite_region(sel.filter)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions docs/xdsl-introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@
" apply_recursively=True,\n",
" walk_reverse=False,\n",
")\n",
"walker.rewrite_module(filtered)\n",
"walker.rewrite_region(filtered.filter)\n",
"printer.print_op(filtered)"
]
},
Expand Down Expand Up @@ -808,7 +808,7 @@
" apply_recursively=True,\n",
" walk_reverse=False,\n",
")\n",
"walker.rewrite_module(filtered)\n",
"walker.rewrite_region(filtered.filter)\n",
"printer.print_op(filtered)"
]
},
Expand Down
97 changes: 62 additions & 35 deletions tests/pattern_rewriter/test_pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,21 @@ def test_insert_op_at_start():
"""Test rewrites where operations are inserted with a given position."""

prog = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

expected = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, mod: ModuleOp, rewriter: PatternRewriter):
def match_and_rewrite(self, mod: test.TestOp, rewriter: PatternRewriter):
new_cst = Constant.from_int_and_width(42, i32)

rewriter.insert_op(new_cst, InsertPoint.at_start(mod.regions[0].blocks[0]))
Expand All @@ -404,20 +408,24 @@ def test_insert_op_before():
"""Test rewrites where operations are inserted before a given operation."""

prog = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

expected = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, mod: ModuleOp, rewriter: PatternRewriter):
def match_and_rewrite(self, mod: test.TestOp, rewriter: PatternRewriter):
new_cst = Constant.from_int_and_width(42, i32)

first_op = mod.ops.first
first_op = mod.regions[0].block.ops.first
assert first_op is not None
rewriter.insert_op(new_cst, InsertPoint.before(first_op))

Expand All @@ -433,20 +441,24 @@ def test_insert_op_after():
"""Test rewrites where operations are inserted after a given operation."""

prog = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

expected = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
%1 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, mod: ModuleOp, rewriter: PatternRewriter):
def match_and_rewrite(self, mod: test.TestOp, rewriter: PatternRewriter):
new_cst = Constant.from_int_and_width(42, i32)

first_op = mod.ops.first
first_op = mod.regions[0].block.ops.first
assert first_op is not None
rewriter.insert_op(new_cst, InsertPoint.after(first_op))

Expand Down Expand Up @@ -592,17 +604,21 @@ def test_delete_inner_op():
"""Test rewrites where an operation inside a region of the matched op is deleted."""

prog = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

expected = """"builtin.module"() ({
^0:
"test.op"() ({
^0:
}) : () -> ()
}) : () -> ()"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: ModuleOp, rewriter: PatternRewriter):
first_op = op.ops.first
def match_and_rewrite(self, op: test.TestOp, rewriter: PatternRewriter):
first_op = op.regions[0].block.ops.first

assert first_op is not None
rewriter.erase_op(first_op)
Expand All @@ -619,17 +635,21 @@ def test_replace_inner_op():
"""Test rewrites where an operation inside a region of the matched op is deleted."""

prog = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 5 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

expected = """"builtin.module"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
"test.op"() ({
%0 = "arith.constant"() <{"value" = 42 : i32}> : () -> i32
}) : () -> ()
}) : () -> ()"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: ModuleOp, rewriter: PatternRewriter):
first_op = op.ops.first
def match_and_rewrite(self, op: test.TestOp, rewriter: PatternRewriter):
first_op = op.regions[0].block.ops.first

assert first_op is not None
rewriter.replace_op(first_op, [Constant.from_int_and_width(42, i32)])
Expand Down Expand Up @@ -1048,29 +1068,36 @@ def test_move_region_contents_to_new_regions():

prog = """\
"builtin.module"() ({
%0 = "test.op"() : () -> !test.type<"int">
%1 = "test.op"() ({
^0:
%2 = "test.op"() : () -> !test.type<"int">
}) : () -> !test.type<"int">
"test.op"() ({
%0 = "test.op"() : () -> !test.type<"int">
%1 = "test.op"() ({
^0:
%2 = "test.op"() : () -> !test.type<"int">
}) : () -> !test.type<"int">
}) : () -> ()
}) : () -> ()
"""

expected = """\
"builtin.module"() ({
%0 = "test.op"() : () -> !test.type<"int">
%1 = "test.op"() ({
}) : () -> !test.type<"int">
%2 = "test.op"() ({
%3 = "test.op"() : () -> !test.type<"int">
}) : () -> !test.type<"int">
"test.op"() ({
%0 = "test.op"() : () -> !test.type<"int">
%1 = "test.op"() ({
}) : () -> !test.type<"int">
%2 = "test.op"() ({
%3 = "test.op"() : () -> !test.type<"int">
}) : () -> !test.type<"int">
}) : () -> ()
}) : () -> ()
"""

class Rewrite(RewritePattern):
@op_type_rewrite_pattern
def match_and_rewrite(self, op: ModuleOp, rewriter: PatternRewriter):
ops_iter = iter(op.ops)
def match_and_rewrite(self, op: test.TestOp, rewriter: PatternRewriter):
# Match the toplevel test.op
if not isinstance(op.parent_op(), ModuleOp):
return
ops_iter = iter(op.regions[0].block.ops)

_ = next(ops_iter) # skip first op
old_op = next(ops_iter)
Expand Down
16 changes: 8 additions & 8 deletions xdsl/pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ class PatternRewriteWalker:
That way, all uses are replaced before the definitions.
"""

post_walk_func: Callable[[Operation, PatternRewriterListener], bool] | None = field(
post_walk_func: Callable[[Region, PatternRewriterListener], bool] | None = field(
default=None
)
"""
Expand Down Expand Up @@ -773,34 +773,34 @@ def rewrite_module(self, module: ModuleOp) -> bool:
Rewrite operations nested in the given operation by repeatedly applying the
pattern. Returns `True` if the IR was mutated.
"""
return self.rewrite_op(module)
return self.rewrite_region(module.body)

def rewrite_op(self, op: Operation) -> bool:
def rewrite_region(self, region: Region) -> bool:
"""
Rewrite operations nested in the given operation by repeatedly applying the
pattern. Returns `True` if the IR was mutated.
"""
pattern_listener = self._get_rewriter_listener()

self._populate_worklist(op)
self._populate_worklist(region)
op_was_modified = self._process_worklist(pattern_listener)
if self.post_walk_func is not None:
op_was_modified |= self.post_walk_func(op, pattern_listener)
op_was_modified |= self.post_walk_func(region, pattern_listener)

if not self.apply_recursively:
return op_was_modified

result = op_was_modified

while op_was_modified:
self._populate_worklist(op)
self._populate_worklist(region)
op_was_modified = self._process_worklist(pattern_listener)
if self.post_walk_func is not None:
op_was_modified |= self.post_walk_func(op, pattern_listener)
op_was_modified |= self.post_walk_func(region, pattern_listener)

return result

def _populate_worklist(self, op: Operation) -> None:
def _populate_worklist(self, op: Operation | Region | Block) -> None:
"""Populate the worklist with all nested operations."""
# We walk in reverse order since we use a stack for our worklist.
for sub_op in op.walk(
Expand Down
5 changes: 2 additions & 3 deletions xdsl/transforms/apply_pdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ class ApplyPDLPass(ModulePass):
pdl_file: str | None = None

def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
payload_module = op
if self.pdl_file is not None:
assert os.path.exists(self.pdl_file)
with open(self.pdl_file) as f:
pdl_module_str = f.read()
parser = Parser(ctx, pdl_module_str)
pdl_module = parser.parse_module()
else:
pdl_module = payload_module
pdl_module = op
rewrite_patterns: list[RewritePattern] = [
PDLRewritePattern(op, ctx, None)
for op in pdl_module.walk()
if isinstance(op, pdl.RewriteOp)
]
pattern_applier = GreedyRewritePatternApplier(rewrite_patterns)
PatternRewriteWalker(pattern_applier).rewrite_op(payload_module)
PatternRewriteWalker(pattern_applier).rewrite_module(op)
2 changes: 1 addition & 1 deletion xdsl/transforms/convert_qref_to_qssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class ConvertQRefToQssa(ModulePass):
def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
PatternRewriteWalker(
ConvertQRefToQssaPattern(), apply_recursively=False
).rewrite_op(op)
).rewrite_module(op)
2 changes: 1 addition & 1 deletion xdsl/transforms/convert_qssa_to_qref.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ class ConvertQssaToQRef(ModulePass):
name = "convert-qssa-to-qref"

def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
PatternRewriteWalker(ConvertQssaToQRefPattern()).rewrite_op(op)
PatternRewriteWalker(ConvertQssaToQRefPattern()).rewrite_module(op)
2 changes: 1 addition & 1 deletion xdsl/transforms/convert_scf_to_cf.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,4 @@ def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
ForLowering(),
]
)
).rewrite_op(op)
).rewrite_module(op)
4 changes: 2 additions & 2 deletions xdsl/transforms/convert_stencil_to_csl_stencil.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def match_and_rewrite(self, op: dmp.SwapOp, rewriter: PatternRewriter, /):
ConvertAccessOpFromPrefetchPattern(arg_idx)
)

nested_rewriter.rewrite_op(new_apply_op)
nested_rewriter.rewrite_region(new_apply_op.region)


def split_ops(
Expand Down Expand Up @@ -415,7 +415,7 @@ def match_and_rewrite(self, op: stencil.ApplyOp, rewriter: PatternRewriter, /):
PatternRewriteWalker(
SplitVarithOpPattern(op.region.block.args[prefetch_idx]),
apply_recursively=False,
).rewrite_op(op)
).rewrite_region(op.region)

# determine how ops should be split across the two regions
chunk_region_ops, done_exchange_ops = split_ops(
Expand Down
8 changes: 3 additions & 5 deletions xdsl/transforms/dead_code_elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,12 @@ def region_dce(region: Region, listener: PatternRewriterListener | None = None)
return live_set.changed


def op_dce(op: Operation, listener: PatternRewriterListener | None = None):
changed = tuple(region_dce(region, listener) for region in op.regions)

return any(changed)
def op_dce(region: Region, listener: PatternRewriterListener | None = None):
return region_dce(region, listener)


class DeadCodeElimination(ModulePass):
name = "dce"

def apply(self, ctx: MLContext, op: ModuleOp) -> None:
op_dce(op)
op_dce(op.body)
6 changes: 3 additions & 3 deletions xdsl/transforms/varith_transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
]
),
walk_reverse=True,
).rewrite_op(op)
).rewrite_module(op)


class ConvertVarithToArithPass(ModulePass):
Expand All @@ -268,7 +268,7 @@ def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
PatternRewriteWalker(
VarithToArithPattern(),
apply_recursively=False,
).rewrite_op(op)
).rewrite_module(op)


class VarithFuseRepeatedOperandsPass(ModulePass):
Expand All @@ -285,4 +285,4 @@ def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None:
PatternRewriteWalker(
FuseRepeatedAddArgsPattern(self.min_reps),
apply_recursively=False,
).rewrite_op(op)
).rewrite_module(op)

0 comments on commit 39a2e0a

Please sign in to comment.