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

api: add replace_by_if helper to control ssa value replacement #3196

Merged
merged 1 commit into from
Oct 1, 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
14 changes: 14 additions & 0 deletions tests/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,17 @@ class MyOperation(Operation):
name = "dialect.op"

assert MyOperation.dialect_name() == "dialect"


def test_replace_by_if():
a = TestSSAValue(i32)
b = test.TestOp((a,))
c = test.TestOp((a,))

assert set(u.operation for u in a.uses) == {b, c}

d = TestSSAValue(i32)
a.replace_by_if(d, lambda u: u.operation is not c)

assert set(u.operation for u in a.uses) == {c}
assert set(u.operation for u in d.uses) == {b}
24 changes: 22 additions & 2 deletions xdsl/ir/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

import re
from abc import ABC, abstractmethod
from collections.abc import Hashable, Iterable, Iterator, Mapping, Reversible, Sequence
from collections.abc import (
Callable,
Hashable,
Iterable,
Iterator,
Mapping,
Reversible,
Sequence,
)
from dataclasses import dataclass, field
from io import StringIO
from itertools import chain
Expand Down Expand Up @@ -161,7 +169,19 @@ def replace_by(self, value: SSAValue) -> None:
# carry over name if possible
if value.name_hint is None:
value.name_hint = self.name_hint
assert len(self.uses) == 0, "unexpected error in xdsl"
assert not self.uses, "unexpected error in xdsl"

def replace_by_if(self, value: SSAValue, test: Callable[[Use], bool]):
"""
Replace the value by another value in all its uses that pass the given test
function.
"""
for use in self.uses.copy():
if test(use):
use.operation.operands[use.index] = value
# carry over name if possible
if value.name_hint is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have an option for changing the name?
Compared to replace_by, we don't change all uses, so we might run this function multiple times per value, making this name change a bit weird?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt we'll ever hit the case you describe, it seems to be primarily useful for "replace everything except" as opposed to some targeted replacing. I'll merge this PR, and happily add an option if you are sure that it would be a good idea

value.name_hint = self.name_hint

def erase(self, safe_erase: bool = True) -> None:
"""
Expand Down
5 changes: 3 additions & 2 deletions xdsl/transforms/convert_memref_stream_to_snitch_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ def match_and_rewrite(
cast_op := builtin.UnrealizedConversionCastOp.get((arg,), (arg.type,)),
InsertPoint.at_start(new_body),
)
arg.replace_by(cast_op.results[0])
cast_op.operands = (arg,)
arg.replace_by_if(
cast_op.results[0], lambda use: use.operation is not cast_op
)
rewriter.modify_value_type(arg, stream_type)


Expand Down
12 changes: 3 additions & 9 deletions xdsl/transforms/convert_qref_to_qssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@ def match_and_rewrite(self, op: QRefBase, rewriter: PatternRewriter):
if not new_op.is_gate:
return

# For gates we replace any other occurences of the original operands
# with the results of the new operation
# For gates we replace any other occurences of the original operands with the
# results of the new operation, except for when used by the new operation.

old_operands = tuple(new_op.operands)

# We do this by replacing all uses of the operand...
for operand, result in zip(op.operands, new_op.results):
operand.replace_by(result)

# and then resetting the operands of the new operation
new_op.operands = old_operands
operand.replace_by_if(result, lambda use: use.operation is not new_op)


class ConvertQRefToQssa(ModulePass):
Expand Down
Loading