Skip to content

Commit 86cc934

Browse files
authored
[python] Expose replaceUsesOfWith C API (#171892)
This PR exposes the `replaceUsesOfWith` C API to Python
1 parent 8e999e3 commit 86cc934

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,10 @@ MLIR_CAPI_EXPORTED
858858
void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
859859
void *userData, MlirWalkOrder walkOrder);
860860

861+
/// Replace uses of 'of' value with the 'with' value inside the 'op' operation.
862+
MLIR_CAPI_EXPORTED void
863+
mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue of, MlirValue with);
864+
861865
//===----------------------------------------------------------------------===//
862866
// Region API.
863867
//===----------------------------------------------------------------------===//

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,14 @@ void mlir::python::populateIRCore(nb::module_ &m) {
39393939
return PyOpSuccessors(self.getOperation().getRef());
39403940
},
39413941
"Returns the list of Operation successors.")
3942+
.def(
3943+
"replace_uses_of_with",
3944+
[](PyOperation &self, PyValue &of, PyValue &with) {
3945+
mlirOperationReplaceUsesOfWith(self.get(), of.get(), with.get());
3946+
},
3947+
"of"_a, "with_"_a,
3948+
"Replaces uses of the 'of' value with the 'with' value inside the "
3949+
"operation.")
39423950
.def("_set_invalid", &PyOperation::setInvalid,
39433951
"Invalidate the operation.");
39443952

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,11 @@ void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
902902
}
903903
}
904904

905+
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue,
906+
MlirValue newValue) {
907+
unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
908+
}
909+
905910
//===----------------------------------------------------------------------===//
906911
// Region API.
907912
//===----------------------------------------------------------------------===//

mlir/test/python/ir/operation.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,40 @@ def callback(op):
12081208
print("Exception raised")
12091209

12101210

1211+
# CHECK-LABEL: TEST: testOpReplaceUsesWith
1212+
@run
1213+
def testOpReplaceUsesWith():
1214+
ctx = Context()
1215+
ctx.allow_unregistered_dialects = True
1216+
with Location.unknown(ctx):
1217+
m = Module.create()
1218+
i32 = IntegerType.get_signless(32)
1219+
with InsertionPoint(m.body):
1220+
value = Operation.create("custom.op1", results=[i32]).results[0]
1221+
value2 = Operation.create("custom.op2", results=[i32]).results[0]
1222+
op = Operation.create("custom.op3", operands=[value])
1223+
op2 = Operation.create("custom.op4", operands=[value])
1224+
op.replace_uses_of_with(value, value2)
1225+
1226+
assert len(list(value.uses)) == 1
1227+
1228+
# CHECK: Use owner: "custom.op4"
1229+
# CHECK: Use operand_number: 0
1230+
for use in value.uses:
1231+
assert use.owner in [op2]
1232+
print(f"Use owner: {use.owner}")
1233+
print(f"Use operand_number: {use.operand_number}")
1234+
1235+
assert len(list(value2.uses)) == 1
1236+
1237+
# CHECK: Use owner: "custom.op3"
1238+
# CHECK: Use operand_number: 0
1239+
for use in value2.uses:
1240+
assert use.owner in [op]
1241+
print(f"Use owner: {use.owner}")
1242+
print(f"Use operand_number: {use.operand_number}")
1243+
1244+
12111245
# CHECK-LABEL: TEST: testGetOwnerConcreteOpview
12121246
@run
12131247
def testGetOwnerConcreteOpview():

0 commit comments

Comments
 (0)