[Mlir-commits] [mlir] d3e6c2d - Surface clone APIs in CAPI

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon May 24 11:53:57 PDT 2021


Author: George
Date: 2021-05-24T11:53:00-07:00
New Revision: d3e6c2ddc3d3574d04d8d5daa3b3fdc5f6262be4

URL: https://github.com/llvm/llvm-project/commit/d3e6c2ddc3d3574d04d8d5daa3b3fdc5f6262be4
DIFF: https://github.com/llvm/llvm-project/commit/d3e6c2ddc3d3574d04d8d5daa3b3fdc5f6262be4.diff

LOG: Surface clone APIs in CAPI

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D102987

Added: 
    

Modified: 
    mlir/include/mlir-c/IR.h
    mlir/lib/CAPI/IR/IR.cpp
    mlir/test/CAPI/ir.c

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 638eea9b86c95..b0866385c34ae 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -326,6 +326,10 @@ mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags);
 ///   - Result type inference is enabled and cannot be performed.
 MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreate(MlirOperationState *state);
 
+/// Creates a deep copy of an operation. The operation is not inserted and
+/// ownership is transferred to the caller.
+MLIR_CAPI_EXPORTED MlirOperation mlirOperationClone(MlirOperation op);
+
 /// Takes an operation owned by the caller and destroys it.
 MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op);
 

diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index ebabd6899e06b..2721efde31f89 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -313,6 +313,10 @@ MlirOperation mlirOperationCreate(MlirOperationState *state) {
   return result;
 }
 
+MlirOperation mlirOperationClone(MlirOperation op) {
+  return wrap(unwrap(op)->clone());
+}
+
 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
 
 bool mlirOperationEqual(MlirOperation op, MlirOperation other) {

diff  --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index be9799e249bab..e3782064658c6 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -1619,6 +1619,38 @@ int testOperands() {
   return 0;
 }
 
+/// Tests clone APIs.
+int testClone() {
+  fprintf(stderr, "@testClone\n");
+  // CHECK-LABEL: @testClone
+
+  MlirContext ctx = mlirContextCreate();
+  MlirLocation loc = mlirLocationUnknownGet(ctx);
+  MlirType indexType = mlirIndexTypeGet(ctx);
+  MlirStringRef valueStringRef =  mlirStringRefCreateFromCString("value");
+
+  MlirAttribute indexZeroLiteral =
+      mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("0 : index"));
+  MlirNamedAttribute indexZeroValueAttr = mlirNamedAttributeGet(mlirIdentifierGet(ctx, valueStringRef), indexZeroLiteral);
+  MlirOperationState constZeroState = mlirOperationStateGet(
+      mlirStringRefCreateFromCString("std.constant"), loc);
+  mlirOperationStateAddResults(&constZeroState, 1, &indexType);
+  mlirOperationStateAddAttributes(&constZeroState, 1, &indexZeroValueAttr);
+  MlirOperation constZero = mlirOperationCreate(&constZeroState);
+
+  MlirAttribute indexOneLiteral =
+      mlirAttributeParseGet(ctx, mlirStringRefCreateFromCString("1 : index"));
+  MlirOperation constOne = mlirOperationClone(constZero);
+  mlirOperationSetAttributeByName(constOne, valueStringRef, indexOneLiteral);
+
+  mlirOperationPrint(constZero, printToStderr, NULL);
+  mlirOperationPrint(constOne, printToStderr, NULL);
+  // CHECK: %0 = "std.constant"() {value = 0 : index} : () -> index
+  // CHECK: %0 = "std.constant"() {value = 1 : index} : () -> index
+
+  return 0;
+}
+
 // Wraps a diagnostic into additional text we can match against.
 MlirLogicalResult errorHandler(MlirDiagnostic diagnostic, void *userData) {
   fprintf(stderr, "processing diagnostic (userData: %ld) <<\n", (long)userData);
@@ -1698,6 +1730,8 @@ int main() {
     return 10;
   if (testOperands())
     return 11;
+  if (testClone())
+    return 12;
 
   mlirContextDestroy(ctx);
 


        


More information about the Mlir-commits mailing list