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

core: Honour safe erase in rewriter.earse_op #1241

Merged
merged 6 commits into from
Jul 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_rewriter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Callable

import pytest
from conftest import assert_print_op

from xdsl.dialects.arith import Addi, Arith, Constant
Expand Down Expand Up @@ -441,3 +442,34 @@ def transformation(module: ModuleOp, rewriter: Rewriter) -> None:
rewriter.replace_op(return_op, [new_op])

rewrite_and_compare(prog, expected, transformation)


# Test erase operation
def test_erase_op():
prog = """\
"builtin.module"() ({
%0 = "arith.constant"() {"value" = 42 : i32} : () -> i32
%1 = "arith.addi"(%0, %0) : (i32, i32) -> i32
}) : () -> ()
"""

expected = """\
"builtin.module"() ({
%0 = "arith.addi"(%1, %1) : (i32, i32) -> i32
}) : () -> ()
"""

def transformation_safe(module: ModuleOp, rewriter: Rewriter) -> None:
constant_op = module.ops.first
assert constant_op is not None
rewriter.erase_op(constant_op, safe_erase=True)

def transformation_unsafe(module: ModuleOp, rewriter: Rewriter) -> None:
constant_op = module.ops.first
assert constant_op is not None
rewriter.erase_op(constant_op, safe_erase=False)

rewrite_and_compare(prog, expected, transformation_unsafe)

with pytest.raises(Exception):
rewrite_and_compare(prog, expected, transformation_safe)
2 changes: 1 addition & 1 deletion xdsl/pattern_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def erase_op(self, op: Operation, safe_erase: bool = True):
"""
self.has_done_action = True
if op == self.current_operation:
return self.erase_matched_op()
return self.erase_matched_op(safe_erase)
if not self._can_modify_op(op):
raise Exception(
"PatternRewriter can only erase operations that are the matched operation"
Expand Down