MLIR 22.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#define MLIR_PYTHON_API_EXPORTED MLIR_CAPI_EXPORTED
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55#define DEFINE_C_API_STRUCT(name, storage) \
56 struct name { \
57 storage *ptr; \
58 }; \
59 typedef struct name name
60
61/// Re-export llvm::ThreadPool so as to avoid including the LLVM C API directly.
62DEFINE_C_API_STRUCT(MlirLlvmThreadPool, void);
63DEFINE_C_API_STRUCT(MlirTypeID, const void);
64DEFINE_C_API_STRUCT(MlirTypeIDAllocator, void);
65
66#undef DEFINE_C_API_STRUCT
67
68//===----------------------------------------------------------------------===//
69// MlirStringRef.
70//===----------------------------------------------------------------------===//
71
72/// A pointer to a sized fragment of a string, not necessarily null-terminated.
73/// Does not own the underlying string. This is equivalent to llvm::StringRef.
74
76 const char *data; ///< Pointer to the first symbol.
77 size_t length; ///< Length of the fragment.
78};
80
81/// Constructs a string reference from the pointer and length. The pointer need
82/// not reference to a null-terminated string.
83
84inline static MlirStringRef mlirStringRefCreate(const char *str,
85 size_t length) {
87 result.data = str;
88 result.length = length;
89 return result;
90}
91
92/// Constructs a string reference from a null-terminated C string. Prefer
93/// mlirStringRefCreate if the length of the string is known.
95mlirStringRefCreateFromCString(const char *str);
96
97/// Returns true if two string references are equal, false otherwise.
99 MlirStringRef other);
100
101/// A callback for returning string references.
102///
103/// This function is called back by the functions that need to return a
104/// reference to the portion of the string with the following arguments:
105/// - an MlirStringRef representing the current portion of the string
106/// - a pointer to user data forwarded from the printing call.
108
109//===----------------------------------------------------------------------===//
110// MlirLogicalResult.
111//===----------------------------------------------------------------------===//
112
113/// A logical result value, essentially a boolean with named states. LLVM
114/// convention for using boolean values to designate success or failure of an
115/// operation is a moving target, so MLIR opted for an explicit class.
116/// Instances of MlirLogicalResult must only be inspected using the associated
117/// functions.
119 int8_t value;
120};
122
123/// Checks if the given logical result represents a success.
125 return res.value != 0;
126}
127
128/// Checks if the given logical result represents a failure.
130 return res.value == 0;
131}
132
133/// Creates a logical result representing a success.
135 MlirLogicalResult res = {1};
136 return res;
137}
138
139/// Creates a logical result representing a failure.
141 MlirLogicalResult res = {0};
142 return res;
143}
144
145//===----------------------------------------------------------------------===//
146// MlirLlvmThreadPool.
147//===----------------------------------------------------------------------===//
148
149/// Create an LLVM thread pool. This is reexported here to avoid directly
150/// pulling in the LLVM headers directly.
151MLIR_CAPI_EXPORTED MlirLlvmThreadPool mlirLlvmThreadPoolCreate(void);
152
153/// Destroy an LLVM thread pool.
154MLIR_CAPI_EXPORTED void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool pool);
155
156//===----------------------------------------------------------------------===//
157// TypeID API.
158//===----------------------------------------------------------------------===//
159
160/// `ptr` must be 8 byte aligned and unique to a type valid for the duration of
161/// the returned type id's usage
162MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDCreate(const void *ptr);
163
164/// Checks whether a type id is null.
165static inline bool mlirTypeIDIsNull(MlirTypeID typeID) { return !typeID.ptr; }
166
167/// Checks if two type ids are equal.
168MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2);
169
170/// Returns the hash value of the type id.
171MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID);
172
173//===----------------------------------------------------------------------===//
174// TypeIDAllocator API.
175//===----------------------------------------------------------------------===//
176
177/// Creates a type id allocator for dynamic type id creation
178MLIR_CAPI_EXPORTED MlirTypeIDAllocator mlirTypeIDAllocatorCreate(void);
179
180/// Deallocates the allocator and all allocated type ids
182mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator);
183
184/// Allocates a type id that is valid for the lifetime of the allocator
185MLIR_CAPI_EXPORTED MlirTypeID
186mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator);
187
188#ifdef __cplusplus
189}
190#endif
191
192#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's usag...
Definition Support.cpp:38
MLIR_CAPI_EXPORTED void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition Support.cpp:63
MLIR_CAPI_EXPORTED bool mlirStringRefEqual(MlirStringRef string, MlirStringRef other)
Returns true if two string references are equal, false otherwise.
Definition Support.cpp:19
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition Support.h:84
static MlirLogicalResult mlirLogicalResultFailure(void)
Creates a logical result representing a failure.
Definition Support.h:140
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition Support.cpp:67
#define DEFINE_C_API_STRUCT(name, storage)
Definition Support.h:55
MLIR_CAPI_EXPORTED void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool pool)
Destroy an LLVM thread pool.
Definition Support.cpp:31
MLIR_CAPI_EXPORTED MlirLlvmThreadPool mlirLlvmThreadPoolCreate(void)
Create an LLVM thread pool.
Definition Support.cpp:27
MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
Definition Support.cpp:51
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
Definition Support.h:134
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition Support.h:124
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
Definition Support.h:129
MLIR_CAPI_EXPORTED MlirTypeIDAllocator mlirTypeIDAllocatorCreate(void)
Creates a type id allocator for dynamic type id creation.
Definition Support.cpp:59
static bool mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id is null.
Definition Support.h:165
MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
Definition Support.cpp:47
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition Support.h:107
MLIR_CAPI_EXPORTED MlirStringRef mlirStringRefCreateFromCString(const char *str)
Constructs a string reference from a null-terminated C string.
Definition Support.cpp:15
A logical result value, essentially a boolean with named states.
Definition Support.h:118
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75
const char * data
Pointer to the first symbol.
Definition Support.h:76
size_t length
Length of the fragment.
Definition Support.h:77