1 //===-- mlir-c/Support.h - Helpers for C API to Core MLIR ---------*- C -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 4 // Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This header declares the auxiliary data structures used in C APIs to core 11 // MLIR functionality. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef MLIR_C_SUPPORT_H 16 #define MLIR_C_SUPPORT_H 17 18 #include <stddef.h> 19 #include <stdint.h> 20 21 //===----------------------------------------------------------------------===// 22 // Visibility annotations. 23 // Use MLIR_CAPI_EXPORTED for exported functions. 24 //===----------------------------------------------------------------------===// 25 26 #if defined(MLIR_CAPI_DISABLE_VISIBILITY_ANNOTATIONS) 27 #define MLIR_CAPI_EXPORTED 28 #elif defined(_WIN32) || defined(__CYGWIN__) 29 // Windows visibility declarations. 30 #if MLIR_CAPI_BUILDING_LIBRARY 31 #define MLIR_CAPI_EXPORTED __declspec(dllexport) 32 #else 33 #define MLIR_CAPI_EXPORTED __declspec(dllimport) 34 #endif 35 #else 36 // Non-windows: use visibility attributes. 37 #define MLIR_CAPI_EXPORTED __attribute__((visibility("default"))) 38 #endif 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 //===----------------------------------------------------------------------===// 45 // MlirStringRef. 46 //===----------------------------------------------------------------------===// 47 48 /** A pointer to a sized fragment of a string, not necessarily null-terminated. 49 * Does not own the underlying string. This is equivalent to llvm::StringRef. 50 */ 51 struct MlirStringRef { 52 const char *data; /**< Pointer to the first symbol. */ 53 size_t length; /**< Length of the fragment. */ 54 }; 55 typedef struct MlirStringRef MlirStringRef; 56 57 /** Constructs a string reference from the pointer and length. The pointer need 58 * not reference to a null-terminated string. 59 */ mlirStringRefCreate(const char * str,size_t length)60inline static MlirStringRef mlirStringRefCreate(const char *str, 61 size_t length) { 62 MlirStringRef result; 63 result.data = str; 64 result.length = length; 65 return result; 66 } 67 68 /** Constructs a string reference from a null-terminated C string. Prefer 69 * mlirStringRefCreate if the length of the string is known. 70 */ 71 MLIR_CAPI_EXPORTED MlirStringRef 72 mlirStringRefCreateFromCString(const char *str); 73 74 /** A callback for returning string references. 75 * 76 * This function is called back by the functions that need to return a reference 77 * to the portion of the string with the following arguments: 78 * - an MlirStringRef represening the current portion of the string 79 * - a pointer to user data forwarded from the printing call. 80 */ 81 typedef void (*MlirStringCallback)(MlirStringRef, void *); 82 83 //===----------------------------------------------------------------------===// 84 // MlirLogicalResult. 85 //===----------------------------------------------------------------------===// 86 87 /** A logical result value, essentially a boolean with named states. LLVM 88 * convention for using boolean values to designate success or failure of an 89 * operation is a moving target, so MLIR opted for an explicit class. 90 * Instances of MlirLogicalResult must only be inspected using the associated 91 * functions. */ 92 struct MlirLogicalResult { 93 int8_t value; 94 }; 95 typedef struct MlirLogicalResult MlirLogicalResult; 96 97 /// Checks if the given logical result represents a success. mlirLogicalResultIsSuccess(MlirLogicalResult res)98inline static bool mlirLogicalResultIsSuccess(MlirLogicalResult res) { 99 return res.value != 0; 100 } 101 102 /// Checks if the given logical result represents a failure. mlirLogicalResultIsFailure(MlirLogicalResult res)103inline static bool mlirLogicalResultIsFailure(MlirLogicalResult res) { 104 return res.value == 0; 105 } 106 107 /// Creates a logical result representing a success. mlirLogicalResultSuccess()108inline static MlirLogicalResult mlirLogicalResultSuccess() { 109 MlirLogicalResult res = {1}; 110 return res; 111 } 112 113 /// Creates a logical result representing a failure. mlirLogicalResultFailure()114inline static MlirLogicalResult mlirLogicalResultFailure() { 115 MlirLogicalResult res = {0}; 116 return res; 117 } 118 119 #ifdef __cplusplus 120 } 121 #endif 122 123 #endif // MLIR_C_SUPPORT_H 124