[Mlir-commits] [mlir] b85f2f5 - [mlir][CAPI] Add APIs for mlirOperationGetName and Identifier.

Stella Laurenzo llvmlistbot at llvm.org
Mon Nov 2 10:55:29 PST 2020


Author: Stella Laurenzo
Date: 2020-11-02T18:52:13Z
New Revision: b85f2f5c5ff72b7a5aabea16e9e51c38ec1e7f72

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

LOG: [mlir][CAPI] Add APIs for mlirOperationGetName and Identifier.

Reviewed By: ftynse

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

Added: 
    

Modified: 
    mlir/include/mlir-c/IR.h
    mlir/include/mlir/CAPI/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 52152960f415..77c66e027c22 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -54,11 +54,12 @@ DEFINE_C_API_STRUCT(MlirOpPrintingFlags, void);
 DEFINE_C_API_STRUCT(MlirBlock, void);
 DEFINE_C_API_STRUCT(MlirRegion, void);
 
-DEFINE_C_API_STRUCT(MlirValue, const void);
 DEFINE_C_API_STRUCT(MlirAttribute, const void);
-DEFINE_C_API_STRUCT(MlirType, const void);
+DEFINE_C_API_STRUCT(MlirIdentifier, const void);
 DEFINE_C_API_STRUCT(MlirLocation, const void);
 DEFINE_C_API_STRUCT(MlirModule, const void);
+DEFINE_C_API_STRUCT(MlirType, const void);
+DEFINE_C_API_STRUCT(MlirValue, const void);
 
 /** Named MLIR attribute.
  *
@@ -285,6 +286,9 @@ static inline int mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
  * not perform deep comparison. */
 int mlirOperationEqual(MlirOperation op, MlirOperation other);
 
+/** Gets the name of the operation as an identifier. */
+MlirIdentifier mlirOperationGetName(MlirOperation op);
+
 /** Gets the block that owns this operation, returning null if the operation is
  * not owned. */
 MlirBlock mlirOperationGetBlock(MlirOperation op);
@@ -552,6 +556,19 @@ void mlirAttributeDump(MlirAttribute attr);
 /** Associates an attribute with the name. Takes ownership of neither. */
 MlirNamedAttribute mlirNamedAttributeGet(const char *name, MlirAttribute attr);
 
+/*============================================================================*/
+/* Identifier API.                                                            */
+/*============================================================================*/
+
+/** Gets an identifier with the given string value. */
+MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str);
+
+/** Checks whether two identifiers are the same. */
+int mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other);
+
+/** Gets the string value of the identifier. */
+MlirStringRef mlirIdentifierStr(MlirIdentifier ident);
+
 #ifdef __cplusplus
 }
 #endif

diff  --git a/mlir/include/mlir/CAPI/IR.h b/mlir/include/mlir/CAPI/IR.h
index c7509d91671c..598b48140a9a 100644
--- a/mlir/include/mlir/CAPI/IR.h
+++ b/mlir/include/mlir/CAPI/IR.h
@@ -16,6 +16,7 @@
 #define MLIR_INCLUDE_MLIR_CAPI_IR_H
 
 #include "mlir/CAPI/Wrap.h"
+#include "mlir/IR/Identifier.h"
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/Module.h"
 #include "mlir/IR/Operation.h"
@@ -28,9 +29,10 @@ DEFINE_C_API_PTR_METHODS(MlirOpPrintingFlags, mlir::OpPrintingFlags)
 DEFINE_C_API_PTR_METHODS(MlirRegion, mlir::Region)
 
 DEFINE_C_API_METHODS(MlirAttribute, mlir::Attribute)
+DEFINE_C_API_METHODS(MlirIdentifier, mlir::Identifier);
 DEFINE_C_API_METHODS(MlirLocation, mlir::Location)
+DEFINE_C_API_METHODS(MlirModule, mlir::ModuleOp)
 DEFINE_C_API_METHODS(MlirType, mlir::Type)
 DEFINE_C_API_METHODS(MlirValue, mlir::Value)
-DEFINE_C_API_METHODS(MlirModule, mlir::ModuleOp)
 
 #endif // MLIR_INCLUDE_MLIR_CAPI_IR_H

diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 7bc89f66e2d5..101e963159c9 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -249,6 +249,10 @@ int mlirOperationEqual(MlirOperation op, MlirOperation other) {
   return unwrap(op) == unwrap(other);
 }
 
+MlirIdentifier mlirOperationGetName(MlirOperation op) {
+  return wrap(unwrap(op)->getName().getIdentifier());
+}
+
 MlirBlock mlirOperationGetBlock(MlirOperation op) {
   return wrap(unwrap(op)->getBlock());
 }
@@ -576,3 +580,19 @@ void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
 MlirNamedAttribute mlirNamedAttributeGet(const char *name, MlirAttribute attr) {
   return MlirNamedAttribute{name, attr};
 }
+
+/*============================================================================*/
+/* Identifier API.                                                            */
+/*============================================================================*/
+
+MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
+  return wrap(Identifier::get(unwrap(str), unwrap(context)));
+}
+
+int mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
+  return unwrap(ident) == unwrap(other);
+}
+
+MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
+  return wrap(unwrap(ident).strref());
+}

diff  --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 51b3dca88253..a27b8342ed9b 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -281,6 +281,19 @@ static void printFirstOfEach(MlirContext ctx, MlirOperation operation) {
   mlirOperationPrint(operation, printToStderr, NULL);
   fprintf(stderr, "\n");
 
+  // Get the operation name and print it.
+  MlirIdentifier ident = mlirOperationGetName(operation);
+  MlirStringRef identStr = mlirIdentifierStr(ident);
+  fprintf(stderr, "Operation name: '");
+  for (size_t i = 0; i < identStr.length; ++i)
+    fputc(identStr.data[i], stderr);
+  fprintf(stderr, "'\n");
+
+  // Get the identifier again and verify equal.
+  MlirIdentifier identAgain = mlirIdentifierGet(ctx, identStr);
+  fprintf(stderr, "Identifier equal: %d\n",
+          mlirIdentifierEqual(ident, identAgain));
+
   // Get the block terminator and print it.
   MlirOperation terminator = mlirBlockGetTerminator(block);
   fprintf(stderr, "Terminator: ");
@@ -1127,6 +1140,8 @@ int main() {
   // CHECK:   }
   // CHECK: return
   // CHECK: First operation: {{.*}} = constant 0 : index
+  // CHECK: Operation name: 'std.constant'
+  // CHECK: Identifier equal: 1
   // CHECK: Terminator: return
   // CHECK: Get attr 0: 0 : index
   // CHECK: Get attr 0 by name: 0 : index


        


More information about the Mlir-commits mailing list