Skip to content

Commit 533600d

Browse files
committed
Expose replaceAllUsesExcept to Python bindings
1 parent 8c4331c commit 533600d

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,22 @@ MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value);
956956
MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of,
957957
MlirValue with);
958958

959+
/// Replace all uses of 'of' value with 'with' value, updating anything in the
960+
/// IR that uses 'of' to use 'with' instead, except if the user is listed in
961+
/// 'exceptions'. The 'exceptions' parameter is an array of MlirOperation
962+
/// pointers with a length of 'numExceptions'.
963+
MLIR_CAPI_EXPORTED void
964+
mlirValueReplaceAllUsesExceptWithSet(MlirValue of, MlirValue with,
965+
MlirOperation *exceptions,
966+
intptr_t numExceptions);
967+
968+
/// Replace all uses of 'of' value with 'with' value, updating anything in the
969+
/// IR that uses 'of' to use 'with' instead, except if the user is
970+
/// 'exceptedUser'.
971+
MLIR_CAPI_EXPORTED void
972+
mlirValueReplaceAllUsesExceptWithSingle(MlirValue of, MlirValue with,
973+
MlirOperation exceptedUser);
974+
959975
//===----------------------------------------------------------------------===//
960976
// OpOperand API.
961977
//===----------------------------------------------------------------------===//

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ static const char kValueReplaceAllUsesWithDocstring[] =
178178
the IR that uses 'self' to use the other value instead.
179179
)";
180180

181+
static const char kValueReplaceAllUsesExceptDocstring[] =
182+
R"("Replace all uses of this value with the 'with' value, except for those
183+
in 'exceptions'. 'exceptions' can be either a single operation or a list of
184+
operations.
185+
)";
186+
181187
//------------------------------------------------------------------------------
182188
// Utilities.
183189
//------------------------------------------------------------------------------
@@ -3718,6 +3724,32 @@ void mlir::python::populateIRCore(py::module &m) {
37183724
mlirValueReplaceAllUsesOfWith(self.get(), with.get());
37193725
},
37203726
kValueReplaceAllUsesWithDocstring)
3727+
.def(
3728+
"replace_all_uses_except",
3729+
[](PyValue &self, PyValue &with, py::object exceptions) {
3730+
MlirValue selfValue = self.get();
3731+
MlirValue withValue = with.get();
3732+
3733+
// Check if 'exceptions' is a list
3734+
if (py::isinstance<py::list>(exceptions)) {
3735+
// Convert Python list to a vector of MlirOperations
3736+
std::vector<MlirOperation> exceptionOps;
3737+
for (py::handle exception : exceptions) {
3738+
exceptionOps.push_back(exception.cast<PyOperation &>().get());
3739+
}
3740+
mlirValueReplaceAllUsesExceptWithSet(
3741+
selfValue, withValue, exceptionOps.data(),
3742+
static_cast<intptr_t>(exceptionOps.size()));
3743+
} else {
3744+
// Assume 'exceptions' is a single Operation
3745+
MlirOperation exceptedUser =
3746+
exceptions.cast<PyOperation &>().get();
3747+
mlirValueReplaceAllUsesExceptWithSingle(selfValue, withValue,
3748+
exceptedUser);
3749+
}
3750+
},
3751+
py::arg("with"), py::arg("exceptions"),
3752+
kValueReplaceAllUsesExceptDocstring)
37213753
.def(MLIR_PYTHON_MAYBE_DOWNCAST_ATTR,
37223754
[](PyValue &self) { return self.maybeDownCast(); });
37233755
PyBlockArgument::bind(m);

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mlir/IR/Visitors.h"
2929
#include "mlir/Interfaces/InferTypeOpInterface.h"
3030
#include "mlir/Parser/Parser.h"
31+
#include "llvm/ADT/SmallPtrSet.h"
3132
#include "llvm/Support/ThreadPool.h"
3233

3334
#include <cstddef>
@@ -1009,6 +1010,31 @@ void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
10091010
unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
10101011
}
10111012

1013+
void mlirValueReplaceAllUsesExceptWithSet(MlirValue oldValue,
1014+
MlirValue newValue,
1015+
MlirOperation *exceptions,
1016+
intptr_t numExceptions) {
1017+
auto oldValueCpp = unwrap(oldValue);
1018+
auto newValueCpp = unwrap(newValue);
1019+
1020+
llvm::SmallPtrSet<mlir::Operation *, 4> exceptionSet;
1021+
for (intptr_t i = 0; i < numExceptions; ++i) {
1022+
exceptionSet.insert(unwrap(exceptions[i]));
1023+
}
1024+
1025+
oldValueCpp.replaceAllUsesExcept(newValueCpp, exceptionSet);
1026+
}
1027+
1028+
void mlirValueReplaceAllUsesExceptWithSingle(MlirValue oldValue,
1029+
MlirValue newValue,
1030+
MlirOperation exceptedUser) {
1031+
auto oldValueCpp = unwrap(oldValue);
1032+
auto newValueCpp = unwrap(newValue);
1033+
auto exceptedUserCpp = unwrap(exceptedUser);
1034+
1035+
oldValueCpp.replaceAllUsesExcept(newValueCpp, exceptedUserCpp);
1036+
}
1037+
10121038
//===----------------------------------------------------------------------===//
10131039
// OpOperand API.
10141040
//===----------------------------------------------------------------------===//

mlir/test/python/ir/value.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,77 @@ def testValueReplaceAllUsesWith():
148148
print(f"Use operand_number: {use.operand_number}")
149149

150150

151+
# CHECK-LABEL: TEST: testValueReplaceAllUsesWithExcept
152+
@run
153+
def testValueReplaceAllUsesWithExcept():
154+
ctx = Context()
155+
ctx.allow_unregistered_dialects = True
156+
with Location.unknown(ctx):
157+
i32 = IntegerType.get_signless(32)
158+
module = Module.create()
159+
with InsertionPoint(module.body):
160+
value = Operation.create("custom.op1", results=[i32]).results[0]
161+
op1 = Operation.create("custom.op1", operands=[value])
162+
op2 = Operation.create("custom.op2", operands=[value])
163+
value2 = Operation.create("custom.op3", results=[i32]).results[0]
164+
value.replace_all_uses_except(value2, [op1])
165+
166+
assert len(list(value.uses)) == 1
167+
168+
# CHECK: Use owner: "custom.op2"
169+
# CHECK: Use operand_number: 0
170+
for use in value2.uses:
171+
assert use.owner in [op2]
172+
print(f"Use owner: {use.owner}")
173+
print(f"Use operand_number: {use.operand_number}")
174+
175+
# CHECK: Use owner: "custom.op1"
176+
# CHECK: Use operand_number: 0
177+
for use in value.uses:
178+
assert use.owner in [op1]
179+
print(f"Use owner: {use.owner}")
180+
print(f"Use operand_number: {use.operand_number}")
181+
182+
183+
# CHECK-LABEL: TEST: testValueReplaceAllUsesWithMultipleExceptions
184+
@run
185+
def testValueReplaceAllUsesWithMultipleExceptions():
186+
ctx = Context()
187+
ctx.allow_unregistered_dialects = True
188+
with Location.unknown(ctx):
189+
i32 = IntegerType.get_signless(32)
190+
module = Module.create()
191+
with InsertionPoint(module.body):
192+
value = Operation.create("custom.op1", results=[i32]).results[0]
193+
op1 = Operation.create("custom.op1", operands=[value])
194+
op2 = Operation.create("custom.op2", operands=[value])
195+
op3 = Operation.create("custom.op3", operands=[value])
196+
value2 = Operation.create("custom.op4", results=[i32]).results[0]
197+
198+
# Replace all uses of `value` with `value2`, except for `op1` and `op2`.
199+
value.replace_all_uses_except(value2, [op1, op2])
200+
201+
# After replacement, only `op3` should use `value2`, while `op1` and `op2` should still use `value`.
202+
assert len(list(value.uses)) == 2
203+
assert len(list(value2.uses)) == 1
204+
205+
# CHECK: Use owner: "custom.op3"
206+
# CHECK: Use operand_number: 0
207+
for use in value2.uses:
208+
assert use.owner in [op3]
209+
print(f"Use owner: {use.owner}")
210+
print(f"Use operand_number: {use.operand_number}")
211+
212+
# CHECK: Use owner: "custom.op1"
213+
# CHECK: Use operand_number: 0
214+
# CHECK: Use owner: "custom.op2"
215+
# CHECK: Use operand_number: 0
216+
for use in value.uses:
217+
assert use.owner in [op1, op2]
218+
print(f"Use owner: {use.owner}")
219+
print(f"Use operand_number: {use.operand_number}")
220+
221+
151222
# CHECK-LABEL: TEST: testValuePrintAsOperand
152223
@run
153224
def testValuePrintAsOperand():

0 commit comments

Comments
 (0)