MLIR 24.0.0git
Interfaces.h
Go to the documentation of this file.
1//===-- mlir-c/Interfaces.h - C API to Core MLIR IR interfaces ----*- 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 C interface to MLIR interface classes. It is
11// intended to contain interfaces defined in lib/Interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_C_INTERFACES_H
16#define MLIR_C_INTERFACES_H
17
18#include "mlir-c/IR.h"
19#include "mlir-c/Support.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25#define DEFINE_C_API_STRUCT(name, storage) \
26 struct name { \
27 storage *ptr; \
28 }; \
29 typedef struct name name
30
31DEFINE_C_API_STRUCT(MlirMemoryEffect, void);
32DEFINE_C_API_STRUCT(MlirMemoryEffectInstance, void);
33DEFINE_C_API_STRUCT(MlirSideEffectResource, void);
34
35#undef DEFINE_C_API_STRUCT
36
37/// Returns `true` if the given operation implements an interface identified by
38/// its TypeID.
40mlirOperationImplementsInterface(MlirOperation operation,
41 MlirTypeID interfaceTypeID);
42
43/// Returns `true` if the operation identified by its canonical string name
44/// implements the interface identified by its TypeID in the given context.
45/// Note that interfaces may be attached to operations in some contexts and not
46/// others.
49 MlirContext context,
50 MlirTypeID interfaceTypeID);
51
52//===----------------------------------------------------------------------===//
53// InferTypeOpInterface.
54//===----------------------------------------------------------------------===//
55
56/// Returns the interface TypeID of the InferTypeOpInterface.
58
59/// These callbacks are used to return multiple types from functions while
60/// transferring ownership to the caller. The first argument is the number of
61/// consecutive elements pointed to by the second argument. The third argument
62/// is an opaque pointer forwarded to the callback by the caller.
63typedef void (*MlirTypesCallback)(intptr_t, MlirType *, void *);
64
65/// Infers the return types of the operation identified by its canonical given
66/// the arguments that will be supplied to its generic builder. Calls `callback`
67/// with the types of inferred arguments, potentially several times, on success.
68/// Returns failure otherwise.
70 MlirStringRef opName, MlirContext context, MlirLocation location,
71 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
72 void *properties, intptr_t nRegions, MlirRegion *regions,
73 MlirTypesCallback callback, void *userData);
74
75//===----------------------------------------------------------------------===//
76// InferShapedTypeOpInterface.
77//===----------------------------------------------------------------------===//
78
79/// Returns the interface TypeID of the InferShapedTypeOpInterface.
81
82/// These callbacks are used to return multiple shaped type components from
83/// functions while transferring ownership to the caller. The first argument is
84/// the has rank boolean followed by the the rank and a pointer to the shape
85/// (if applicable). The next argument is the element type, then the attribute.
86/// The last argument is an opaque pointer forwarded to the callback by the
87/// caller. This callback will be called potentially multiple times for each
88/// shaped type components.
90 const int64_t *, MlirType,
91 MlirAttribute, void *);
92
93/// Infers the return shaped type components of the operation. Calls `callback`
94/// with the types of inferred arguments on success. Returns failure otherwise.
97 MlirStringRef opName, MlirContext context, MlirLocation location,
98 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
99 void *properties, intptr_t nRegions, MlirRegion *regions,
100 MlirShapedTypeComponentsCallback callback, void *userData);
101
102//===---------------------------------------------------------------------===//
103// ConditionallySpeculatable
104//===---------------------------------------------------------------------===//
105
106/// Enum representing the speculatability of an operation.
107typedef enum {
108 /// The operation is not speculatable.
110 /// The operation is speculatable.
112 /// The operation is speculatable if all nested operations are speculatable.
115
116/// Returns the interface TypeID of the ConditionallySpeculatable interface.
117MLIR_CAPI_EXPORTED MlirTypeID
119
120/// Callbacks for implementing ConditionallySpeculatable from external code.
121typedef struct {
122 /// Optional constructor for user data. Set to nullptr to disable it.
124 /// Optional destructor for user data. Set to nullptr to disable it.
126 /// Returns the speculatability of the given operation.
128 void *userData;
130
131/// Attach a new FallbackModel for the ConditionallySpeculatable interface to
132/// the named operation. The FallbackModel will call the provided callbacks.
135 MlirContext ctx, MlirStringRef opName,
137
138/// Returns the speculatability of the given operation.
139///
140/// The operation must implement the ConditionallySpeculatable interface.
143 MlirOperation operation);
144
145//===---------------------------------------------------------------------===//
146// MemoryEffectsOpInterface
147//===---------------------------------------------------------------------===//
148
149/// Returns the singleton instance of the allocate memory effect.
151
152/// Returns the singleton instance of the free memory effect.
153MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsFreeGet(void);
154
155/// Returns the singleton instance of the read memory effect.
156MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsReadGet(void);
157
158/// Returns the singleton instance of the write memory effect.
159MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsWriteGet(void);
160
161/// Returns the TypeID identifying the concrete type of the given memory effect.
162MLIR_CAPI_EXPORTED MlirTypeID
163mlirMemoryEffectGetEffectID(MlirMemoryEffect effect);
164
165/// Returns the singleton instance of the default side effect resource.
166MLIR_CAPI_EXPORTED MlirSideEffectResource
168
169/// Creates a memory effect instance without an associated IR entity.
170/// `parameters` may be a null attribute. The caller owns the returned instance
171/// and must destroy it with `mlirMemoryEffectInstanceDestroy`.
173 MlirMemoryEffect effect, MlirAttribute parameters, int stage,
174 bool effectOnFullRegion, MlirSideEffectResource resource);
175
176/// Creates a memory effect instance associated with an operation operand.
177/// `parameters` may be a null attribute. The caller owns the returned instance
178/// and must destroy it with `mlirMemoryEffectInstanceDestroy`.
179MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
180mlirMemoryEffectInstanceCreateForOpOperand(MlirMemoryEffect effect,
181 MlirOpOperand opOperand,
182 MlirAttribute parameters, int stage,
183 bool effectOnFullRegion,
184 MlirSideEffectResource resource);
185
186/// Creates a memory effect instance associated with an operation result.
187/// `result` must wrap an OpResult. `parameters` may be a null attribute. The
188/// caller owns the returned instance and must destroy it with
189/// `mlirMemoryEffectInstanceDestroy`.
190MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
191mlirMemoryEffectInstanceCreateForOpResult(MlirMemoryEffect effect,
192 MlirValue result,
193 MlirAttribute parameters, int stage,
194 bool effectOnFullRegion,
195 MlirSideEffectResource resource);
196
197/// Creates a memory effect instance associated with a block argument.
198/// `blockArgument` must wrap a BlockArgument. `parameters` may be a null
199/// attribute. The caller owns the returned instance and must destroy it with
200/// `mlirMemoryEffectInstanceDestroy`.
201MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
203 MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters,
204 int stage, bool effectOnFullRegion, MlirSideEffectResource resource);
205
206/// Creates a memory effect instance associated with a symbol. `symbol` must be
207/// a SymbolRefAttr. `parameters` may be a null attribute. The caller owns the
208/// returned instance and must destroy it with
209/// `mlirMemoryEffectInstanceDestroy`.
210MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
211mlirMemoryEffectInstanceCreateForSymbol(MlirMemoryEffect effect,
212 MlirAttribute symbol,
213 MlirAttribute parameters, int stage,
214 bool effectOnFullRegion,
215 MlirSideEffectResource resource);
216
217/// Creates a copy of a memory effect instance. The caller must destroy
218/// the returned instance with `mlirMemoryEffectInstanceDestroy`.
219MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
220mlirMemoryEffectInstanceClone(MlirMemoryEffectInstance instance);
221
222/// Destroys a memory effect instance created or cloned by APIs above.
224mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance);
225
226/// Returns the memory effect of the given instance.
227MLIR_CAPI_EXPORTED MlirMemoryEffect
228mlirMemoryEffectInstanceGetEffect(MlirMemoryEffectInstance instance);
229
230/// Returns the side effect resource of the given instance.
231MLIR_CAPI_EXPORTED MlirSideEffectResource
232mlirMemoryEffectInstanceGetResource(MlirMemoryEffectInstance instance);
233
234/// Returns the stage of the given instance.
236mlirMemoryEffectInstanceGetStage(MlirMemoryEffectInstance instance);
237
238/// Returns true if the given instance has effect on every single value of
239/// the resource.
241 MlirMemoryEffectInstance instance);
242
243/// Returns the parameters of the given instance, or a null attribute if there
244/// are no parameters.
245MLIR_CAPI_EXPORTED MlirAttribute
246mlirMemoryEffectInstanceGetParameters(MlirMemoryEffectInstance instance);
247
248/// Returns the value (OpOperand, OpResult, or BlockArgument) of the given
249/// instance, or a null value if there is no associated value.
250MLIR_CAPI_EXPORTED MlirValue
251mlirMemoryEffectInstanceGetValue(MlirMemoryEffectInstance instance);
252
253/// Returns the symbol reference of the given instance, or a null attribute if
254/// there is no associated symbol.
255MLIR_CAPI_EXPORTED MlirAttribute
256mlirMemoryEffectInstanceGetSymbolRef(MlirMemoryEffectInstance instance);
257
258/// Callback for receiving a batch of memory effect instances. `effects` points
259/// to `numEffects` consecutive instances. Ownership is not transferred, and
260/// the instances are valid only while `callback` is executing. The
261/// caller-provided `userData` is forwarded to the callback.
263 intptr_t numEffects, MlirMemoryEffectInstance *effects, void *userData);
264
265/// Returns the interface TypeID of the MemoryEffectsOpInterface.
267
268/// Callbacks for implementing MemoryEffectsOpInterface from external code.
269typedef struct {
270 /// Optional constructor for user data. Set to nullptr to disable it.
272 /// Optional destructor for user data. Set to nullptr to disable it.
274 /// Get memory effects callback. Implementations report effects by invoking
275 /// `callback` before returning. The supplied callback copies the instances,
276 /// so implementations retain ownership of the instances and only need to
277 /// keep them valid until `callback` returns.
278 void (*getEffects)(MlirOperation op,
280 void *callbackUserData, void *userData);
281 void *userData;
283
284/// Attach a new FallbackModel for the MemoryEffectsOpInterface to the named
285/// operation. The FallbackModel will call the provided callbacks.
287 MlirContext ctx, MlirStringRef opName,
289
290/// Gets the memory effects of the given operation. The operation must
291/// implement the MemoryEffectsOpInterface. Invokes `callback` once with all
292/// effects. Ownership is not transferred; call
293/// `mlirMemoryEffectInstanceClone` from the callback to keep a copy after the
294/// callback returns.
296 MlirOperation operation, MlirMemoryEffectInstancesCallback callback,
297 void *userData);
298
299#ifdef __cplusplus
300}
301#endif
302
303#endif // MLIR_C_INTERFACES_H
MLIR_CAPI_EXPORTED MlirLogicalResult mlirInferShapedTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, void *properties, intptr_t nRegions, MlirRegion *regions, MlirShapedTypeComponentsCallback callback, void *userData)
Infers the return shaped type components of the operation.
MLIR_CAPI_EXPORTED MlirSpeculatability mlirConditionallySpeculatableOpInterfaceGetSpeculatability(MlirOperation operation)
Returns the speculatability of the given operation.
MLIR_CAPI_EXPORTED MlirTypeID mlirMemoryEffectGetEffectID(MlirMemoryEffect effect)
Returns the TypeID identifying the concrete type of the given memory effect.
MLIR_CAPI_EXPORTED MlirTypeID mlirInferTypeOpInterfaceTypeID(void)
Returns the interface TypeID of the InferTypeOpInterface.
MLIR_CAPI_EXPORTED void mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance)
Destroys a memory effect instance created or cloned by APIs above.
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterfaceStatic(MlirStringRef operationName, MlirContext context, MlirTypeID interfaceTypeID)
Returns true if the operation identified by its canonical string name implements the interface identi...
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpOperand(MlirMemoryEffect effect, MlirOpOperand opOperand, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with an operation operand.
MLIR_CAPI_EXPORTED MlirAttribute mlirMemoryEffectInstanceGetSymbolRef(MlirMemoryEffectInstance instance)
Returns the symbol reference of the given instance, or a null attribute if there is no associated sym...
MLIR_CAPI_EXPORTED MlirTypeID mlirConditionallySpeculatableOpInterfaceTypeID(void)
Returns the interface TypeID of the ConditionallySpeculatable interface.
MLIR_CAPI_EXPORTED void mlirConditionallySpeculatableOpInterfaceAttachFallbackModel(MlirContext ctx, MlirStringRef opName, MlirConditionallySpeculatableOpInterfaceCallbacks callbacks)
Attach a new FallbackModel for the ConditionallySpeculatable interface to the named operation.
MlirSpeculatability
Enum representing the speculatability of an operation.
Definition Interfaces.h:107
@ MlirSpeculatabilityRecursivelySpeculatable
The operation is speculatable if all nested operations are speculatable.
Definition Interfaces.h:113
@ MlirSpeculatabilitySpeculatable
The operation is speculatable.
Definition Interfaces.h:111
@ MlirSpeculatabilityNotSpeculatable
The operation is not speculatable.
Definition Interfaces.h:109
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceClone(MlirMemoryEffectInstance instance)
Creates a copy of a memory effect instance.
MLIR_CAPI_EXPORTED bool mlirOperationImplementsInterface(MlirOperation operation, MlirTypeID interfaceTypeID)
Returns true if the given operation implements an interface identified by its TypeID.
MLIR_CAPI_EXPORTED MlirSideEffectResource mlirMemoryEffectInstanceGetResource(MlirMemoryEffectInstance instance)
Returns the side effect resource of the given instance.
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsReadGet(void)
Returns the singleton instance of the read memory effect.
MLIR_CAPI_EXPORTED MlirSideEffectResource mlirSideEffectsDefaultResourceGet(void)
Returns the singleton instance of the default side effect resource.
#define DEFINE_C_API_STRUCT(name, storage)
Definition Interfaces.h:25
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceCreate(MlirMemoryEffect effect, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance without an associated IR entity.
void(* MlirShapedTypeComponentsCallback)(bool, intptr_t, const int64_t *, MlirType, MlirAttribute, void *)
These callbacks are used to return multiple shaped type components from functions while transferring ...
Definition Interfaces.h:89
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectInstanceGetEffect(MlirMemoryEffectInstance instance)
Returns the memory effect of the given instance.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirInferTypeOpInterfaceInferReturnTypes(MlirStringRef opName, MlirContext context, MlirLocation location, intptr_t nOperands, MlirValue *operands, MlirAttribute attributes, void *properties, intptr_t nRegions, MlirRegion *regions, MlirTypesCallback callback, void *userData)
Infers the return types of the operation identified by its canonical given the arguments that will be...
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForOpResult(MlirMemoryEffect effect, MlirValue result, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with an operation result.
MLIR_CAPI_EXPORTED MlirAttribute mlirMemoryEffectInstanceGetParameters(MlirMemoryEffectInstance instance)
Returns the parameters of the given instance, or a null attribute if there are no parameters.
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsFreeGet(void)
Returns the singleton instance of the free memory effect.
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsWriteGet(void)
Returns the singleton instance of the write memory effect.
MLIR_CAPI_EXPORTED void mlirMemoryEffectsOpInterfaceGetEffects(MlirOperation operation, MlirMemoryEffectInstancesCallback callback, void *userData)
Gets the memory effects of the given operation.
MLIR_CAPI_EXPORTED MlirTypeID mlirMemoryEffectsOpInterfaceTypeID(void)
Returns the interface TypeID of the MemoryEffectsOpInterface.
void(* MlirMemoryEffectInstancesCallback)(intptr_t numEffects, MlirMemoryEffectInstance *effects, void *userData)
Callback for receiving a batch of memory effect instances.
Definition Interfaces.h:262
void(* MlirTypesCallback)(intptr_t, MlirType *, void *)
These callbacks are used to return multiple types from functions while transferring ownership to the ...
Definition Interfaces.h:63
MLIR_CAPI_EXPORTED MlirTypeID mlirInferShapedTypeOpInterfaceTypeID(void)
Returns the interface TypeID of the InferShapedTypeOpInterface.
MLIR_CAPI_EXPORTED void mlirMemoryEffectsOpInterfaceAttachFallbackModel(MlirContext ctx, MlirStringRef opName, MlirMemoryEffectsOpInterfaceCallbacks callbacks)
Attach a new FallbackModel for the MemoryEffectsOpInterface to the named operation.
MLIR_CAPI_EXPORTED bool mlirMemoryEffectInstanceGetEffectOnFullRegion(MlirMemoryEffectInstance instance)
Returns true if the given instance has effect on every single value of the resource.
MLIR_CAPI_EXPORTED int mlirMemoryEffectInstanceGetStage(MlirMemoryEffectInstance instance)
Returns the stage of the given instance.
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsAllocateGet(void)
Returns the singleton instance of the allocate memory effect.
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForSymbol(MlirMemoryEffect effect, MlirAttribute symbol, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with a symbol.
MLIR_CAPI_EXPORTED MlirValue mlirMemoryEffectInstanceGetValue(MlirMemoryEffectInstance instance)
Returns the value (OpOperand, OpResult, or BlockArgument) of the given instance, or a null value if t...
MLIR_CAPI_EXPORTED MlirMemoryEffectInstance mlirMemoryEffectInstanceCreateForBlockArgument(MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters, int stage, bool effectOnFullRegion, MlirSideEffectResource resource)
Creates a memory effect instance associated with a block argument.
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
Callbacks for implementing ConditionallySpeculatable from external code.
Definition Interfaces.h:121
void(* destruct)(void *userData)
Optional destructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:125
void(* construct)(void *userData)
Optional constructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:123
MlirSpeculatability(* getSpeculatability)(MlirOperation op, void *userData)
Returns the speculatability of the given operation.
Definition Interfaces.h:127
A logical result value, essentially a boolean with named states.
Definition Support.h:121
Callbacks for implementing MemoryEffectsOpInterface from external code.
Definition Interfaces.h:269
void(* construct)(void *userData)
Optional constructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:271
void(* destruct)(void *userData)
Optional destructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:273
void(* getEffects)(MlirOperation op, MlirMemoryEffectInstancesCallback callback, void *callbackUserData, void *userData)
Get memory effects callback.
Definition Interfaces.h:278
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78