[Mlir-commits] [mlir] 8b6bea9 - Use bool in place of int for boolean things in the C API

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Nov 29 16:42:23 PST 2020


Author: George
Date: 2020-11-29T16:40:57-08:00
New Revision: 8b6bea9bff80a80d3cdbceb6d2218e6bac819696

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

LOG: Use bool in place of int for boolean things in the C API

`bool` is pretty well supported by now in C, and using it in place of `int` is not only more semantically accurate, but also improves automatic bindings for languages like Swift.

There is more discussion here: https://llvm.discourse.group/t/adding-mlirbool-to-c-bindings/2280/5

Reviewed By: ftynse, mehdi_amini

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 2ca5b80b825a..902b2b988622 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -18,6 +18,7 @@
 #ifndef MLIR_C_IR_H
 #define MLIR_C_IR_H
 
+#include <stdbool.h>
 #include <stdint.h>
 
 #include "mlir-c/Support.h"
@@ -82,10 +83,10 @@ typedef struct MlirNamedAttribute MlirNamedAttribute;
 MLIR_CAPI_EXPORTED MlirContext mlirContextCreate();
 
 /// Checks if two contexts are equal.
-MLIR_CAPI_EXPORTED int mlirContextEqual(MlirContext ctx1, MlirContext ctx2);
+MLIR_CAPI_EXPORTED bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2);
 
 /// Checks whether a context is null.
-static inline int mlirContextIsNull(MlirContext context) {
+static inline bool mlirContextIsNull(MlirContext context) {
   return !context.ptr;
 }
 
@@ -126,14 +127,14 @@ MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
 MLIR_CAPI_EXPORTED MlirContext mlirDialectGetContext(MlirDialect dialect);
 
 /// Checks if the dialect is null.
-static inline int mlirDialectIsNull(MlirDialect dialect) {
+static inline bool mlirDialectIsNull(MlirDialect dialect) {
   return !dialect.ptr;
 }
 
 /** Checks if two dialects that belong to the same context are equal. Dialects
  * from 
diff erent contexts will not compare equal. */
-MLIR_CAPI_EXPORTED int mlirDialectEqual(MlirDialect dialect1,
-                                        MlirDialect dialect2);
+MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1,
+                                         MlirDialect dialect2);
 
 /// Returns the namespace of the given dialect.
 MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect);
@@ -177,7 +178,7 @@ MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext(MlirModule module);
 MLIR_CAPI_EXPORTED MlirBlock mlirModuleGetBody(MlirModule module);
 
 /// Checks whether a module is null.
-static inline int mlirModuleIsNull(MlirModule module) { return !module.ptr; }
+static inline bool mlirModuleIsNull(MlirModule module) { return !module.ptr; }
 
 /// Takes a module owned by the caller and deletes it.
 MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module);
@@ -287,12 +288,12 @@ mlirOperationCreate(const MlirOperationState *state);
 MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op);
 
 /// Checks whether the underlying operation is null.
-static inline int mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
+static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
 
 /** Checks whether two operation handles point to the same operation. This does
  * not perform deep comparison. */
-MLIR_CAPI_EXPORTED int mlirOperationEqual(MlirOperation op,
-                                          MlirOperation other);
+MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op,
+                                           MlirOperation other);
 
 /// Gets the name of the operation as an identifier.
 MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op);
@@ -388,7 +389,7 @@ MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate();
 MLIR_CAPI_EXPORTED void mlirRegionDestroy(MlirRegion region);
 
 /// Checks whether a region is null.
-static inline int mlirRegionIsNull(MlirRegion region) { return !region.ptr; }
+static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; }
 
 /// Gets the first block in the region.
 MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region);
@@ -430,11 +431,11 @@ MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs,
 MLIR_CAPI_EXPORTED void mlirBlockDestroy(MlirBlock block);
 
 /// Checks whether a block is null.
-static inline int mlirBlockIsNull(MlirBlock block) { return !block.ptr; }
+static inline bool mlirBlockIsNull(MlirBlock block) { return !block.ptr; }
 
 /** Checks whether two blocks handles point to the same block. This does not
  * perform deep comparison. */
-MLIR_CAPI_EXPORTED int mlirBlockEqual(MlirBlock block, MlirBlock other);
+MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other);
 
 /** Returns the block immediately following the given block in its parent
  * region. */
@@ -489,10 +490,10 @@ mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData);
 //===----------------------------------------------------------------------===//
 
 /// Returns whether the value is null.
-static inline int mlirValueIsNull(MlirValue value) { return !value.ptr; }
+static inline bool mlirValueIsNull(MlirValue value) { return !value.ptr; }
 
 /// Returns 1 if two values are equal, 0 otherwise.
-int mlirValueEqual(MlirValue value1, MlirValue value2);
+bool mlirValueEqual(MlirValue value1, MlirValue value2);
 
 /// Returns 1 if the value is a block argument, 0 otherwise.
 MLIR_CAPI_EXPORTED int mlirValueIsABlockArgument(MlirValue value);
@@ -543,10 +544,10 @@ MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context,
 MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type);
 
 /// Checks whether a type is null.
-static inline int mlirTypeIsNull(MlirType type) { return !type.ptr; }
+static inline bool mlirTypeIsNull(MlirType type) { return !type.ptr; }
 
 /// Checks if two types are equal.
-MLIR_CAPI_EXPORTED int mlirTypeEqual(MlirType t1, MlirType t2);
+MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2);
 
 /** Prints a location by sending chunks of the string representation and
  * forwarding `userData to `callback`. Note that the callback may be called
@@ -572,10 +573,10 @@ MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute);
 MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute);
 
 /// Checks whether an attribute is null.
-static inline int mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; }
+static inline bool mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; }
 
 /// Checks if two attributes are equal.
-MLIR_CAPI_EXPORTED int mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2);
+MLIR_CAPI_EXPORTED bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2);
 
 /** Prints an attribute by sending chunks of the string representation and
  * forwarding `userData to `callback`. Note that the callback may be called
@@ -600,8 +601,8 @@ MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context,
                                                     MlirStringRef str);
 
 /// Checks whether two identifiers are the same.
-MLIR_CAPI_EXPORTED int mlirIdentifierEqual(MlirIdentifier ident,
-                                           MlirIdentifier other);
+MLIR_CAPI_EXPORTED bool mlirIdentifierEqual(MlirIdentifier ident,
+                                            MlirIdentifier other);
 
 /// Gets the string value of the identifier.
 MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident);

diff  --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index cf76811f6c12..c5eec20d85c4 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -30,7 +30,7 @@ MlirContext mlirContextCreate() {
   return wrap(context);
 }
 
-int mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
+bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
   return unwrap(ctx1) == unwrap(ctx2);
 }
 
@@ -66,7 +66,7 @@ MlirContext mlirDialectGetContext(MlirDialect dialect) {
   return wrap(unwrap(dialect)->getContext());
 }
 
-int mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
+bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
   return unwrap(dialect1) == unwrap(dialect2);
 }
 
@@ -245,7 +245,7 @@ MlirOperation mlirOperationCreate(const MlirOperationState *state) {
 
 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
 
-int mlirOperationEqual(MlirOperation op, MlirOperation other) {
+bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
   return unwrap(op) == unwrap(other);
 }
 
@@ -398,7 +398,7 @@ MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) {
   return wrap(b);
 }
 
-int mlirBlockEqual(MlirBlock block, MlirBlock other) {
+bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
   return unwrap(block) == unwrap(other);
 }
 
@@ -480,7 +480,7 @@ void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
 // Value API.
 //===----------------------------------------------------------------------===//
 
-int mlirValueEqual(MlirValue value1, MlirValue value2) {
+bool mlirValueEqual(MlirValue value1, MlirValue value2) {
   return unwrap(value1) == unwrap(value2);
 }
 
@@ -538,7 +538,9 @@ MlirContext mlirTypeGetContext(MlirType type) {
   return wrap(unwrap(type).getContext());
 }
 
-int mlirTypeEqual(MlirType t1, MlirType t2) { return unwrap(t1) == unwrap(t2); }
+bool mlirTypeEqual(MlirType t1, MlirType t2) {
+  return unwrap(t1) == unwrap(t2);
+}
 
 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
   detail::CallbackOstream stream(callback, userData);
@@ -563,7 +565,7 @@ MlirType mlirAttributeGetType(MlirAttribute attribute) {
   return wrap(unwrap(attribute).getType());
 }
 
-int mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
+bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
   return unwrap(a1) == unwrap(a2);
 }
 
@@ -588,7 +590,7 @@ MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
   return wrap(Identifier::get(unwrap(str), unwrap(context)));
 }
 
-int mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
+bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
   return unwrap(ident) == unwrap(other);
 }
 


        


More information about the Mlir-commits mailing list