Skip to content

Commit d3e6c2d

Browse files
committed
Surface clone APIs in CAPI
Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D102987
1 parent e7a268f commit d3e6c2d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

mlir/include/mlir-c/IR.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags);
326326
/// - Result type inference is enabled and cannot be performed.
327327
MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreate(MlirOperationState *state);
328328

329+
/// Creates a deep copy of an operation. The operation is not inserted and
330+
/// ownership is transferred to the caller.
331+
MLIR_CAPI_EXPORTED MlirOperation mlirOperationClone(MlirOperation op);
332+
329333
/// Takes an operation owned by the caller and destroys it.
330334
MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op);
331335

mlir/lib/CAPI/IR/IR.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ MlirOperation mlirOperationCreate(MlirOperationState *state) {
313313
return result;
314314
}
315315

316+
MlirOperation mlirOperationClone(MlirOperation op) {
317+
return wrap(unwrap(op)->clone());
318+
}
319+
316320
void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
317321

318322
bool mlirOperationEqual(MlirOperation op, MlirOperation other) {

mlir/test/CAPI/ir.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,38 @@ int testOperands() {
16191619
return 0;
16201620
}
16211621

1622+
/// Tests clone APIs.
1623+
int testClone() {
1624+
fprintf(stderr, "@testClone\n");
1625+
// CHECK-LABEL: @testClone
1626+
1627+
MlirContext ctx = mlirContextCreate();
1628+
MlirLocation loc = mlirLocationUnknownGet(ctx);
1629+
MlirType indexType = mlirIndexTypeGet(ctx);
1630+
MlirStringRef valueStringRef = mlirStringRefCreateFromCString("value");
1631+
1632+
MlirAttribute indexZeroLiteral =
1633+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
1634+
MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
1635+
MlirOperationState constZeroState = mlirOperationStateGet(
1636+
mlirStringRefCreateFromCString("std.constant"), loc);
1637+
mlirOperationStateAddResults(&constZeroState, 1, &indexType);
1638+
mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
1639+
MlirOperation constZero = mlirOperationCreate(&constZeroState);
1640+
1641+
MlirAttribute indexOneLiteral =
1642+
mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
1643+
MlirOperation constOne = mlirOperationClone(constZero);
1644+
mlirOperationSetAttributeByName(constOne, valueStringRef, indexOneLiteral);
1645+
1646+
mlirOperationPrint(constZero, printToStderr, NULL);
1647+
mlirOperationPrint(constOne, printToStderr, NULL);
1648+
// CHECK: %0 = "std.constant"() {value = 0 : index} : () -> index
1649+
// CHECK: %0 = "std.constant"() {value = 1 : index} : () -> index
1650+
1651+
return 0;
1652+
}
1653+
16221654
// Wraps a diagnostic into additional text we can match against.
16231655
MlirLogicalResult errorHandler(MlirDiagnostic diagnostic, void *userData) {
16241656
fprintf(stderr, "processing diagnostic (userData: %ld) <<\n", (long)userData);
@@ -1698,6 +1730,8 @@ int main() {
16981730
return 10;
16991731
if (testOperands())
17001732
return 11;
1733+
if (testClone())
1734+
return 12;
17011735

17021736
mlirContextDestroy(ctx);
17031737

0 commit comments

Comments
 (0)