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 <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21
22 //===----------------------------------------------------------------------===//
23 // Visibility annotations.
24 // Use MLIR_CAPI_EXPORTED for exported functions.
25 //
26 // On Windows, if MLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC is defined, then
27 // __declspec(dllexport) and __declspec(dllimport) will be generated. This
28 // can only be enabled if actually building DLLs. It is generally, mutually
29 // exclusive with the use of other mechanisms for managing imports/exports
30 // (i.e. CMake's WINDOWS_EXPORT_ALL_SYMBOLS feature).
31 //===----------------------------------------------------------------------===//
32
33 #if (defined(_WIN32) || defined(__CYGWIN__)) && \
34 !defined(MLIR_CAPI_ENABLE_WINDOWS_DLL_DECLSPEC)
35 // Visibility annotations disabled.
36 #define MLIR_CAPI_EXPORTED
37 #elif defined(_WIN32) || defined(__CYGWIN__)
38 // Windows visibility declarations.
39 #if MLIR_CAPI_BUILDING_LIBRARY
40 #define MLIR_CAPI_EXPORTED __declspec(dllexport)
41 #else
42 #define MLIR_CAPI_EXPORTED __declspec(dllimport)
43 #endif
44 #else
45 // Non-windows: use visibility attributes.
46 #define MLIR_CAPI_EXPORTED __attribute__((visibility("default")))
47 #endif
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #define DEFINE_C_API_STRUCT(name, storage) \
54 struct name { \
55 storage *ptr; \
56 }; \
57 typedef struct name name
58
59 DEFINE_C_API_STRUCT(MlirTypeID, const void);
60 DEFINE_C_API_STRUCT(MlirTypeIDAllocator, void);
61
62 #undef DEFINE_C_API_STRUCT
63
64 //===----------------------------------------------------------------------===//
65 // MlirStringRef.
66 //===----------------------------------------------------------------------===//
67
68 /// A pointer to a sized fragment of a string, not necessarily null-terminated.
69 /// Does not own the underlying string. This is equivalent to llvm::StringRef.
70
71 struct MlirStringRef {
72 const char *data; ///< Pointer to the first symbol.
73 size_t length; ///< Length of the fragment.
74 };
75 typedef struct MlirStringRef MlirStringRef;
76
77 /// Constructs a string reference from the pointer and length. The pointer need
78 /// not reference to a null-terminated string.
79
mlirStringRefCreate(const char * str,size_t length)80 inline static MlirStringRef mlirStringRefCreate(const char *str,
81 size_t length) {
82 MlirStringRef result;
83 result.data = str;
84 result.length = length;
85 return result;
86 }
87
88 /// Constructs a string reference from a null-terminated C string. Prefer
89 /// mlirStringRefCreate if the length of the string is known.
90 MLIR_CAPI_EXPORTED MlirStringRef
91 mlirStringRefCreateFromCString(const char *str);
92
93 /// Returns true if two string references are equal, false otherwise.
94 MLIR_CAPI_EXPORTED bool mlirStringRefEqual(MlirStringRef string,
95 MlirStringRef other);
96
97 /// A callback for returning string references.
98 ///
99 /// This function is called back by the functions that need to return a
100 /// reference to the portion of the string with the following arguments:
101 /// - an MlirStringRef representing the current portion of the string
102 /// - a pointer to user data forwarded from the printing call.
103 typedef void (*MlirStringCallback)(MlirStringRef, void *);
104
105 //===----------------------------------------------------------------------===//
106 // MlirLogicalResult.
107 //===----------------------------------------------------------------------===//
108
109 /// A logical result value, essentially a boolean with named states. LLVM
110 /// convention for using boolean values to designate success or failure of an
111 /// operation is a moving target, so MLIR opted for an explicit class.
112 /// Instances of MlirLogicalResult must only be inspected using the associated
113 /// functions.
114 struct MlirLogicalResult {
115 int8_t value;
116 };
117 typedef struct MlirLogicalResult MlirLogicalResult;
118
119 /// Checks if the given logical result represents a success.
mlirLogicalResultIsSuccess(MlirLogicalResult res)120 inline static bool mlirLogicalResultIsSuccess(MlirLogicalResult res) {
121 return res.value != 0;
122 }
123
124 /// Checks if the given logical result represents a failure.
mlirLogicalResultIsFailure(MlirLogicalResult res)125 inline static bool mlirLogicalResultIsFailure(MlirLogicalResult res) {
126 return res.value == 0;
127 }
128
129 /// Creates a logical result representing a success.
mlirLogicalResultSuccess()130 inline static MlirLogicalResult mlirLogicalResultSuccess() {
131 MlirLogicalResult res = {1};
132 return res;
133 }
134
135 /// Creates a logical result representing a failure.
mlirLogicalResultFailure()136 inline static MlirLogicalResult mlirLogicalResultFailure() {
137 MlirLogicalResult res = {0};
138 return res;
139 }
140
141 //===----------------------------------------------------------------------===//
142 // TypeID API.
143 //===----------------------------------------------------------------------===//
144
145 /// `ptr` must be 8 byte aligned and unique to a type valid for the duration of
146 /// the returned type id's usage
147 MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDCreate(const void *ptr);
148
149 /// Checks whether a type id is null.
mlirTypeIDIsNull(MlirTypeID typeID)150 static inline bool mlirTypeIDIsNull(MlirTypeID typeID) { return !typeID.ptr; }
151
152 /// Checks if two type ids are equal.
153 MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2);
154
155 /// Returns the hash value of the type id.
156 MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID);
157
158 //===----------------------------------------------------------------------===//
159 // TypeIDAllocator API.
160 //===----------------------------------------------------------------------===//
161
162 /// Creates a type id allocator for dynamic type id creation
163 MLIR_CAPI_EXPORTED MlirTypeIDAllocator mlirTypeIDAllocatorCreate();
164
165 /// Deallocates the allocator and all allocated type ids
166 MLIR_CAPI_EXPORTED void
167 mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator);
168
169 /// Allocates a type id that is valid for the lifetime of the allocator
170 MLIR_CAPI_EXPORTED MlirTypeID
171 mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator);
172
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif // MLIR_C_SUPPORT_H
178