1 //===-- mlir-c/Support.h - Helpers for C API to Core MLIR ---------*- C -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 // This header declares the auxiliary data structures used in C APIs to core
11 // MLIR functionality.
13 //===----------------------------------------------------------------------===//
15 #ifndef MLIR_C_SUPPORT_H
16 #define MLIR_C_SUPPORT_H
22 //===----------------------------------------------------------------------===//
23 // Visibility annotations.
24 // Use MLIR_CAPI_EXPORTED for exported functions.
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 //===----------------------------------------------------------------------===//
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)
42 #define MLIR_CAPI_EXPORTED __declspec(dllimport)
45 // Non-windows: use visibility attributes.
46 #define MLIR_CAPI_EXPORTED __attribute__((visibility("default")))
53 #define DEFINE_C_API_STRUCT(name, storage) \
57 typedef struct name name
59 /// Re-export llvm::ThreadPool so as to avoid including the LLVM C API directly.
60 DEFINE_C_API_STRUCT(MlirLlvmThreadPool
, void);
61 DEFINE_C_API_STRUCT(MlirTypeID
, const void);
62 DEFINE_C_API_STRUCT(MlirTypeIDAllocator
, void);
64 #undef DEFINE_C_API_STRUCT
66 //===----------------------------------------------------------------------===//
68 //===----------------------------------------------------------------------===//
70 /// A pointer to a sized fragment of a string, not necessarily null-terminated.
71 /// Does not own the underlying string. This is equivalent to llvm::StringRef.
73 struct MlirStringRef
{
74 const char *data
; ///< Pointer to the first symbol.
75 size_t length
; ///< Length of the fragment.
77 typedef struct MlirStringRef MlirStringRef
;
79 /// Constructs a string reference from the pointer and length. The pointer need
80 /// not reference to a null-terminated string.
82 inline static MlirStringRef
mlirStringRefCreate(const char *str
,
86 result
.length
= length
;
90 /// Constructs a string reference from a null-terminated C string. Prefer
91 /// mlirStringRefCreate if the length of the string is known.
92 MLIR_CAPI_EXPORTED MlirStringRef
93 mlirStringRefCreateFromCString(const char *str
);
95 /// Returns true if two string references are equal, false otherwise.
96 MLIR_CAPI_EXPORTED
bool mlirStringRefEqual(MlirStringRef string
,
99 /// A callback for returning string references.
101 /// This function is called back by the functions that need to return a
102 /// reference to the portion of the string with the following arguments:
103 /// - an MlirStringRef representing the current portion of the string
104 /// - a pointer to user data forwarded from the printing call.
105 typedef void (*MlirStringCallback
)(MlirStringRef
, void *);
107 //===----------------------------------------------------------------------===//
108 // MlirLogicalResult.
109 //===----------------------------------------------------------------------===//
111 /// A logical result value, essentially a boolean with named states. LLVM
112 /// convention for using boolean values to designate success or failure of an
113 /// operation is a moving target, so MLIR opted for an explicit class.
114 /// Instances of MlirLogicalResult must only be inspected using the associated
116 struct MlirLogicalResult
{
119 typedef struct MlirLogicalResult MlirLogicalResult
;
121 /// Checks if the given logical result represents a success.
122 inline static bool mlirLogicalResultIsSuccess(MlirLogicalResult res
) {
123 return res
.value
!= 0;
126 /// Checks if the given logical result represents a failure.
127 inline static bool mlirLogicalResultIsFailure(MlirLogicalResult res
) {
128 return res
.value
== 0;
131 /// Creates a logical result representing a success.
132 inline static MlirLogicalResult
mlirLogicalResultSuccess(void) {
133 MlirLogicalResult res
= {1};
137 /// Creates a logical result representing a failure.
138 inline static MlirLogicalResult
mlirLogicalResultFailure(void) {
139 MlirLogicalResult res
= {0};
143 //===----------------------------------------------------------------------===//
144 // MlirLlvmThreadPool.
145 //===----------------------------------------------------------------------===//
147 /// Create an LLVM thread pool. This is reexported here to avoid directly
148 /// pulling in the LLVM headers directly.
149 MLIR_CAPI_EXPORTED MlirLlvmThreadPool
mlirLlvmThreadPoolCreate(void);
151 /// Destroy an LLVM thread pool.
152 MLIR_CAPI_EXPORTED
void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool pool
);
154 //===----------------------------------------------------------------------===//
156 //===----------------------------------------------------------------------===//
158 /// `ptr` must be 8 byte aligned and unique to a type valid for the duration of
159 /// the returned type id's usage
160 MLIR_CAPI_EXPORTED MlirTypeID
mlirTypeIDCreate(const void *ptr
);
162 /// Checks whether a type id is null.
163 static inline bool mlirTypeIDIsNull(MlirTypeID typeID
) { return !typeID
.ptr
; }
165 /// Checks if two type ids are equal.
166 MLIR_CAPI_EXPORTED
bool mlirTypeIDEqual(MlirTypeID typeID1
, MlirTypeID typeID2
);
168 /// Returns the hash value of the type id.
169 MLIR_CAPI_EXPORTED
size_t mlirTypeIDHashValue(MlirTypeID typeID
);
171 //===----------------------------------------------------------------------===//
172 // TypeIDAllocator API.
173 //===----------------------------------------------------------------------===//
175 /// Creates a type id allocator for dynamic type id creation
176 MLIR_CAPI_EXPORTED MlirTypeIDAllocator
mlirTypeIDAllocatorCreate(void);
178 /// Deallocates the allocator and all allocated type ids
179 MLIR_CAPI_EXPORTED
void
180 mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator
);
182 /// Allocates a type id that is valid for the lifetime of the allocator
183 MLIR_CAPI_EXPORTED MlirTypeID
184 mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator
);
190 #endif // MLIR_C_SUPPORT_H