xdslproject/xdsl

api: supporting replaceUsesWithIf

Closed this issue · 0 comments

Now we only have replace_by in the core, and in some cases we only want to replace part of the uses with a new value. This is implemented as replaceUsesWithIf in MLIR: https://mlir.llvm.org/doxygen/classmlir_1_1Operation.html#a6f5567fd62feeaf0cbba4877732c512a

Given the current implementation of replace_by:

xdsl/xdsl/ir/core.py

Lines 157 to 164 in cd2d585

def replace_by(self, value: SSAValue) -> None:
"""Replace the value by another value in all its uses."""
for use in self.uses.copy():
use.operation.operands[use.index] = value
# 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"

I wonder if it would be useful to add the following function:

    def replace_by_except(self, value: SSAValue, exception: [SSAValue]) -> None:
        """Replace the value by another value in its uses except the given exception."""
        for use in self.uses.copy():
            if use not in exception:
                use.operation.operands[use.index] = value
        assert len(self.uses) <= len(exception), "unexpected error in xdsl"