Skip to content

Commit fd527ce

Browse files
committed
Revert "[MLIR][python bindings] implement replace_all_uses_with on PyValue"
This reverts commit 3bab7cb because it breaks sanitizers. Differential Revision: https://reviews.llvm.org/D149188
1 parent 20d0f80 commit fd527ce

File tree

5 files changed

+74
-127
lines changed

5 files changed

+74
-127
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,12 +755,6 @@ mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData);
755755
/// operand if there are no uses.
756756
MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value);
757757

758-
/// Replace all uses of 'of' value with the 'with' value, updating anything in
759-
/// the IR that uses 'of' to use the other value instead. When this returns
760-
/// there are zero uses of 'of'.
761-
MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of,
762-
MlirValue with);
763-
764758
//===----------------------------------------------------------------------===//
765759
// OpOperand API.
766760
//===----------------------------------------------------------------------===//

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
#include "mlir-c/Bindings/Python/Interop.h"
1515
#include "mlir-c/BuiltinAttributes.h"
16+
#include "mlir-c/BuiltinTypes.h"
1617
#include "mlir-c/Debug.h"
1718
#include "mlir-c/Diagnostics.h"
1819
#include "mlir-c/IR.h"
20+
//#include "mlir-c/Registration.h"
1921
#include "llvm/ADT/ArrayRef.h"
2022
#include "llvm/ADT/SmallVector.h"
2123

@@ -152,11 +154,6 @@ position in the argument list. If the value is an operation result, this is
152154
equivalent to printing the operation that produced it.
153155
)";
154156

155-
static const char kValueReplaceAllUsesWithDocstring[] =
156-
R"(Replace all uses of value with the new value, updating anything in
157-
the IR that uses 'self' to use the other value instead.
158-
)";
159-
160157
//------------------------------------------------------------------------------
161158
// Utilities.
162159
//------------------------------------------------------------------------------
@@ -3319,18 +3316,10 @@ void mlir::python::populateIRCore(py::module &m) {
33193316
return printAccum.join();
33203317
},
33213318
kValueDunderStrDocstring)
3322-
.def_property_readonly("type",
3323-
[](PyValue &self) {
3324-
return PyType(
3325-
self.getParentOperation()->getContext(),
3326-
mlirValueGetType(self.get()));
3327-
})
3328-
.def(
3329-
"replace_all_uses_with",
3330-
[](PyValue &self, PyValue &with) {
3331-
mlirValueReplaceAllUsesOfWith(self.get(), with.get());
3332-
},
3333-
kValueReplaceAllUsesWithDocstring);
3319+
.def_property_readonly("type", [](PyValue &self) {
3320+
return PyType(self.getParentOperation()->getContext(),
3321+
mlirValueGetType(self.get()));
3322+
});
33343323
PyBlockArgument::bind(m);
33353324
PyOpResult::bind(m);
33363325
PyOpOperand::bind(m);

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,6 @@ MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
751751
return wrap(opOperand);
752752
}
753753

754-
void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
755-
unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
756-
}
757-
758754
//===----------------------------------------------------------------------===//
759755
// OpOperand API.
760756
//===----------------------------------------------------------------------===//

mlir/test/CAPI/ir.c

Lines changed: 68 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,6 @@
2828
#include <stdlib.h>
2929
#include <string.h>
3030

31-
MlirValue makeConstantLiteral(MlirContext ctx, const char *literalStr,
32-
const char *typeStr) {
33-
MlirLocation loc = mlirLocationUnknownGet(ctx);
34-
char attrStr[50];
35-
sprintf(attrStr, "%s : %s", literalStr, typeStr);
36-
MlirAttribute literal =
37-
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString(attrStr));
38-
MlirNamedAttribute valueAttr = mlirNamedAttributeGet(
39-
mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")), literal);
40-
MlirOperationState constState = mlirOperationStateGet(
41-
mlirStringRefCreateFromCString("arith.constant"), loc);
42-
MlirType type =
43-
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(typeStr));
44-
mlirOperationStateAddResults(&constState, 1, &type);
45-
mlirOperationStateAddAttributes(&constState, 1, &valueAttr);
46-
MlirOperation constOp = mlirOperationCreate(&constState);
47-
return mlirOperationGetResult(constOp, 0);
48-
}
49-
5031
static void registerAllUpstreamDialects(MlirContext ctx) {
5132
MlirDialectRegistry registry = mlirDialectRegistryCreate();
5233
mlirRegisterAllDialects(registry);
@@ -134,17 +115,26 @@ MlirModule makeAndDumpAdd(MlirContext ctx, MlirLocation location) {
134115
MlirOperation func = mlirOperationCreate(&funcState);
135116
mlirBlockInsertOwnedOperation(moduleBody, 0, func);
136117

137-
MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
138-
MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
118+
MlirType indexType =
119+
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString("index"));
120+
MlirAttribute indexZeroLiteral =
121+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
122+
MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
123+
mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
124+
indexZeroLiteral);
125+
MlirOperationState constZeroState = mlirOperationStateGet(
126+
mlirStringRefCreateFromCString("arith.constant"), location);
127+
mlirOperationStateAddResults(&constZeroState, 1, &indexType);
128+
mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
129+
MlirOperation constZero = mlirOperationCreate(&constZeroState);
139130
mlirBlockAppendOwnedOperation(funcBody, constZero);
140131

141132
MlirValue funcArg0 = mlirBlockGetArgument(funcBody, 0);
133+
MlirValue constZeroValue = mlirOperationGetResult(constZero, 0);
142134
MlirValue dimOperands[] = {funcArg0, constZeroValue};
143135
MlirOperationState dimState = mlirOperationStateGet(
144136
mlirStringRefCreateFromCString("memref.dim"), location);
145137
mlirOperationStateAddOperands(&dimState, 2, dimOperands);
146-
MlirType indexType =
147-
mlirTypeParseGet(ctx, mlirStringRefCreateFromCString("index"));
148138
mlirOperationStateAddResults(&dimState, 1, &indexType);
149139
MlirOperation dim = mlirOperationCreate(&dimState);
150140
mlirBlockAppendOwnedOperation(funcBody, dim);
@@ -163,11 +153,11 @@ MlirModule makeAndDumpAdd(MlirContext ctx, MlirLocation location) {
163153
mlirStringRefCreateFromCString("arith.constant"), location);
164154
mlirOperationStateAddResults(&constOneState, 1, &indexType);
165155
mlirOperationStateAddAttributes(&constOneState, 1, &indexOneValueAttr);
166-
MlirValue constOneValue = makeConstantLiteral(ctx, "1", "index");
167-
MlirOperation constOne = mlirOpResultGetOwner(constOneValue);
156+
MlirOperation constOne = mlirOperationCreate(&constOneState);
168157
mlirBlockAppendOwnedOperation(funcBody, constOne);
169158

170159
MlirValue dimValue = mlirOperationGetResult(dim, 0);
160+
MlirValue constOneValue = mlirOperationGetResult(constOne, 0);
171161
MlirValue loopOperands[] = {constZeroValue, dimValue, constOneValue};
172162
MlirOperationState loopState = mlirOperationStateGet(
173163
mlirStringRefCreateFromCString("scf.for"), location);
@@ -830,6 +820,11 @@ static int printBuiltinTypes(MlirContext ctx) {
830820
return 0;
831821
}
832822

823+
void callbackSetFixedLengthString(const char *data, intptr_t len,
824+
void *userData) {
825+
strncpy(userData, data, len);
826+
}
827+
833828
bool stringIsEqual(const char *lhs, MlirStringRef rhs) {
834829
if (strlen(lhs) != rhs.length) {
835830
return false;
@@ -1799,10 +1794,32 @@ int testOperands(void) {
17991794
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("arith"));
18001795
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("test"));
18011796
MlirLocation loc = mlirLocationUnknownGet(ctx);
1797+
MlirType indexType = mlirIndexTypeGet(ctx);
18021798

18031799
// Create some constants to use as operands.
1804-
MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
1805-
MlirValue constOneValue = makeConstantLiteral(ctx, "1", "index");
1800+
MlirAttribute indexZeroLiteral =
1801+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
1802+
MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
1803+
mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
1804+
indexZeroLiteral);
1805+
MlirOperationState constZeroState = mlirOperationStateGet(
1806+
mlirStringRefCreateFromCString("arith.constant"), loc);
1807+
mlirOperationStateAddResults(&constZeroState, 1, &indexType);
1808+
mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
1809+
MlirOperation constZero = mlirOperationCreate(&constZeroState);
1810+
MlirValue constZeroValue = mlirOperationGetResult(constZero, 0);
1811+
1812+
MlirAttribute indexOneLiteral =
1813+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
1814+
MlirNamedAttribute indexOneValueAttr = mlirNamedAttributeGet(
1815+
mlirIdentifierGet(ctx, mlirStringRefCreateFromCString("value")),
1816+
indexOneLiteral);
1817+
MlirOperationState constOneState = mlirOperationStateGet(
1818+
mlirStringRefCreateFromCString("arith.constant"), loc);
1819+
mlirOperationStateAddResults(&constOneState, 1, &indexType);
1820+
mlirOperationStateAddAttributes(&constOneState, 1, &indexOneValueAttr);
1821+
MlirOperation constOne = mlirOperationCreate(&constOneState);
1822+
MlirValue constOneValue = mlirOperationGetResult(constOne, 0);
18061823

18071824
// Create the operation under test.
18081825
mlirContextSetAllowUnregisteredDialects(ctx, true);
@@ -1856,50 +1873,9 @@ int testOperands(void) {
18561873
return 3;
18571874
}
18581875

1859-
MlirOperationState op2State =
1860-
mlirOperationStateGet(mlirStringRefCreateFromCString("dummy.op2"), loc);
1861-
MlirValue initialOperands2[] = {constOneValue};
1862-
mlirOperationStateAddOperands(&op2State, 1, initialOperands2);
1863-
MlirOperation op2 = mlirOperationCreate(&op2State);
1864-
1865-
MlirOpOperand use3 = mlirValueGetFirstUse(constOneValue);
1866-
fprintf(stderr, "First use owner: ");
1867-
mlirOperationPrint(mlirOpOperandGetOwner(use3), printToStderr, NULL);
1868-
fprintf(stderr, "\n");
1869-
// CHECK: First use owner: "dummy.op2"
1870-
1871-
use3 = mlirOpOperandGetNextUse(mlirValueGetFirstUse(constOneValue));
1872-
fprintf(stderr, "Second use owner: ");
1873-
mlirOperationPrint(mlirOpOperandGetOwner(use3), printToStderr, NULL);
1874-
fprintf(stderr, "\n");
1875-
// CHECK: Second use owner: "dummy.op"
1876-
1877-
MlirValue constTwoValue = makeConstantLiteral(ctx, "2", "index");
1878-
mlirValueReplaceAllUsesOfWith(constOneValue, constTwoValue);
1879-
1880-
use3 = mlirValueGetFirstUse(constOneValue);
1881-
if (!mlirOpOperandIsNull(use3)) {
1882-
fprintf(stderr, "ERROR: Use should be null\n");
1883-
return 4;
1884-
}
1885-
1886-
MlirOpOperand use4 = mlirValueGetFirstUse(constTwoValue);
1887-
fprintf(stderr, "First replacement use owner: ");
1888-
mlirOperationPrint(mlirOpOperandGetOwner(use4), printToStderr, NULL);
1889-
fprintf(stderr, "\n");
1890-
// CHECK: First replacement use owner: "dummy.op"
1891-
1892-
use4 = mlirOpOperandGetNextUse(mlirValueGetFirstUse(constTwoValue));
1893-
fprintf(stderr, "Second replacement use owner: ");
1894-
mlirOperationPrint(mlirOpOperandGetOwner(use4), printToStderr, NULL);
1895-
fprintf(stderr, "\n");
1896-
// CHECK: Second replacement use owner: "dummy.op2"
1897-
18981876
mlirOperationDestroy(op);
1899-
mlirOperationDestroy(op2);
1900-
mlirOperationDestroy(mlirOpResultGetOwner(constZeroValue));
1901-
mlirOperationDestroy(mlirOpResultGetOwner(constOneValue));
1902-
mlirOperationDestroy(mlirOpResultGetOwner(constTwoValue));
1877+
mlirOperationDestroy(constZero);
1878+
mlirOperationDestroy(constOne);
19031879
mlirContextDestroy(ctx);
19041880

19051881
return 0;
@@ -1914,10 +1890,19 @@ int testClone(void) {
19141890
registerAllUpstreamDialects(ctx);
19151891

19161892
mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));
1893+
MlirLocation loc = mlirLocationUnknownGet(ctx);
1894+
MlirType indexType = mlirIndexTypeGet(ctx);
19171895
MlirStringRef valueStringRef = mlirStringRefCreateFromCString("value");
19181896

1919-
MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
1920-
MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
1897+
MlirAttribute indexZeroLiteral =
1898+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
1899+
MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
1900+
mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
1901+
MlirOperationState constZeroState = mlirOperationStateGet(
1902+
mlirStringRefCreateFromCString("arith.constant"), loc);
1903+
mlirOperationStateAddResults(&constZeroState, 1, &indexType);
1904+
mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
1905+
MlirOperation constZero = mlirOperationCreate(&constZeroState);
19211906

19221907
MlirAttribute indexOneLiteral =
19231908
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
@@ -1995,10 +1980,19 @@ int testTypeID(MlirContext ctx) {
19951980
}
19961981

19971982
MlirLocation loc = mlirLocationUnknownGet(ctx);
1983+
MlirType indexType = mlirIndexTypeGet(ctx);
1984+
MlirStringRef valueStringRef = mlirStringRefCreateFromCString("value");
19981985

19991986
// Create a registered operation, which should have a type id.
2000-
MlirValue constZeroValue = makeConstantLiteral(ctx, "0", "index");
2001-
MlirOperation constZero = mlirOpResultGetOwner(constZeroValue);
1987+
MlirAttribute indexZeroLiteral =
1988+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
1989+
MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(
1990+
mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
1991+
MlirOperationState constZeroState = mlirOperationStateGet(
1992+
mlirStringRefCreateFromCString("arith.constant"), loc);
1993+
mlirOperationStateAddResults(&constZeroState, 1, &indexType);
1994+
mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
1995+
MlirOperation constZero = mlirOperationCreate(&constZeroState);
20021996

20031997
if (!mlirOperationVerify(constZero)) {
20041998
fprintf(stderr, "ERROR: Expected operation to verify correctly\n");

mlir/test/python/ir/value.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,3 @@ def testValueUses():
111111
assert use.owner in [op1, op2]
112112
print(f"Use owner: {use.owner}")
113113
print(f"Use operand_number: {use.operand_number}")
114-
115-
# CHECK-LABEL: TEST: testValueReplaceAllUsesWith
116-
@run
117-
def testValueReplaceAllUsesWith():
118-
ctx = Context()
119-
ctx.allow_unregistered_dialects = True
120-
with Location.unknown(ctx):
121-
i32 = IntegerType.get_signless(32)
122-
module = Module.create()
123-
with InsertionPoint(module.body):
124-
value = Operation.create("custom.op1", results=[i32]).results[0]
125-
op1 = Operation.create("custom.op2", operands=[value])
126-
op2 = Operation.create("custom.op2", operands=[value])
127-
value2 = Operation.create("custom.op3", results=[i32]).results[0]
128-
value.replace_all_uses_with(value2)
129-
130-
assert len(list(value.uses)) == 0
131-
132-
# CHECK: Use owner: "custom.op2"
133-
# CHECK: Use operand_number: 0
134-
# CHECK: Use owner: "custom.op2"
135-
# CHECK: Use operand_number: 0
136-
for use in value2.uses:
137-
assert use.owner in [op1, op2]
138-
print(f"Use owner: {use.owner}")
139-
print(f"Use operand_number: {use.operand_number}")

0 commit comments

Comments
 (0)