MLIR  16.0.0git
Support.h
Go to the documentation of this file.
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 };
76 
77 /// Constructs a string reference from the pointer and length. The pointer need
78 /// not reference to a null-terminated string.
79 
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.
91 mlirStringRefCreateFromCString(const char *str);
92 
93 /// Returns true if two string references are equal, false otherwise.
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.
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.
115  int8_t value;
116 };
118 
119 /// Checks if the given logical result represents a success.
121  return res.value != 0;
122 }
123 
124 /// Checks if the given logical result represents a failure.
126  return res.value == 0;
127 }
128 
129 /// Creates a logical result representing a success.
131  MlirLogicalResult res = {1};
132  return res;
133 }
134 
135 /// Creates a logical result representing a failure.
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.
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
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
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDCreate(const void *ptr)
ptr must be 8 byte aligned and unique to a type valid for the duration of the returned type id&#39;s usag...
Definition: Support.cpp:27
const char * data
Pointer to the first symbol.
Definition: Support.h:72
MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
Definition: Support.cpp:40
struct MlirStringRef MlirStringRef
Definition: Support.h:75
static MlirLogicalResult mlirLogicalResultSuccess()
Creates a logical result representing a success.
Definition: Support.h:130
MLIR_CAPI_EXPORTED bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other)
Returns true if two string references are equal, false otherwise.
Definition: Support.cpp:18
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Definition: Support.h:125
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition: Support.h:120
#define MLIR_CAPI_EXPORTED
Definition: Support.h:46
MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
Definition: Support.cpp:36
#define DEFINE_C_API_STRUCT(name, storage)
Definition: Support.h:53
static MlirLogicalResult mlirLogicalResultFailure()
Creates a logical result representing a failure.
Definition: Support.h:136
A logical result value, essentially a boolean with named states.
Definition: Support.h:114
MLIR_CAPI_EXPORTED void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition: Support.cpp:52
MLIR_CAPI_EXPORTED MlirStringRef mlirStringRefCreateFromCString(const char *str)
Constructs a string reference from a null-terminated C string.
Definition: Support.cpp:14
size_t length
Length of the fragment.
Definition: Support.h:73
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition: Support.h:80
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition: Support.h:103
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition: Support.cpp:56
MLIR_CAPI_EXPORTED MlirTypeIDAllocator mlirTypeIDAllocatorCreate()
Creates a type id allocator for dynamic type id creation.
Definition: Support.cpp:48
static bool mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id is null.
Definition: Support.h:150