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(MlirMemoryEffectInstancesList, void);
34DEFINE_C_API_STRUCT(MlirSideEffectResource, void);
35
36#undef DEFINE_C_API_STRUCT
37
38/// Returns `true` if the given operation implements an interface identified by
39/// its TypeID.
41mlirOperationImplementsInterface(MlirOperation operation,
42 MlirTypeID interfaceTypeID);
43
44/// Returns `true` if the operation identified by its canonical string name
45/// implements the interface identified by its TypeID in the given context.
46/// Note that interfaces may be attached to operations in some contexts and not
47/// others.
50 MlirContext context,
51 MlirTypeID interfaceTypeID);
52
53//===----------------------------------------------------------------------===//
54// InferTypeOpInterface.
55//===----------------------------------------------------------------------===//
56
57/// Returns the interface TypeID of the InferTypeOpInterface.
59
60/// These callbacks are used to return multiple types from functions while
61/// transferring ownership to the caller. The first argument is the number of
62/// consecutive elements pointed to by the second argument. The third argument
63/// is an opaque pointer forwarded to the callback by the caller.
64typedef void (*MlirTypesCallback)(intptr_t, MlirType *, void *);
65
66/// Infers the return types of the operation identified by its canonical given
67/// the arguments that will be supplied to its generic builder. Calls `callback`
68/// with the types of inferred arguments, potentially several times, on success.
69/// Returns failure otherwise.
71 MlirStringRef opName, MlirContext context, MlirLocation location,
72 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
73 void *properties, intptr_t nRegions, MlirRegion *regions,
74 MlirTypesCallback callback, void *userData);
75
76//===----------------------------------------------------------------------===//
77// InferShapedTypeOpInterface.
78//===----------------------------------------------------------------------===//
79
80/// Returns the interface TypeID of the InferShapedTypeOpInterface.
82
83/// These callbacks are used to return multiple shaped type components from
84/// functions while transferring ownership to the caller. The first argument is
85/// the has rank boolean followed by the the rank and a pointer to the shape
86/// (if applicable). The next argument is the element type, then the attribute.
87/// The last argument is an opaque pointer forwarded to the callback by the
88/// caller. This callback will be called potentially multiple times for each
89/// shaped type components.
91 const int64_t *, MlirType,
92 MlirAttribute, void *);
93
94/// Infers the return shaped type components of the operation. Calls `callback`
95/// with the types of inferred arguments on success. Returns failure otherwise.
98 MlirStringRef opName, MlirContext context, MlirLocation location,
99 intptr_t nOperands, MlirValue *operands, MlirAttribute attributes,
100 void *properties, intptr_t nRegions, MlirRegion *regions,
101 MlirShapedTypeComponentsCallback callback, void *userData);
102
103//===---------------------------------------------------------------------===//
104// ConditionallySpeculatable
105//===---------------------------------------------------------------------===//
106
107/// Enum representing the speculatability of an operation.
108typedef enum {
109 /// The operation is not speculatable.
111 /// The operation is speculatable.
113 /// The operation is speculatable if all nested operations are speculatable.
116
117/// Returns the interface TypeID of the ConditionallySpeculatable interface.
118MLIR_CAPI_EXPORTED MlirTypeID
120
121/// Callbacks for implementing ConditionallySpeculatable from external code.
122typedef struct {
123 /// Optional constructor for user data. Set to nullptr to disable it.
125 /// Optional destructor for user data. Set to nullptr to disable it.
127 /// Returns the speculatability of the given operation.
129 void *userData;
131
132/// Attach a new FallbackModel for the ConditionallySpeculatable interface to
133/// the named operation. The FallbackModel will call the provided callbacks.
136 MlirContext ctx, MlirStringRef opName,
138
139/// Returns the speculatability of the given operation.
140///
141/// The operation must implement the ConditionallySpeculatable interface.
144 MlirOperation operation);
145
146//===---------------------------------------------------------------------===//
147// MemoryEffectsOpInterface
148//===---------------------------------------------------------------------===//
149
150/// Returns the borrowed singleton instance of the allocate memory effect.
152
153/// Returns the borrowed singleton instance of the free memory effect.
154MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsFreeGet(void);
155
156/// Returns the borrowed singleton instance of the read memory effect.
157MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsReadGet(void);
158
159/// Returns the borrowed singleton instance of the write memory effect.
160MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsWriteGet(void);
161
162/// Returns the borrowed singleton instance of the default side effect
163/// resource.
164MLIR_CAPI_EXPORTED MlirSideEffectResource
166
167/// Creates a memory effect instance without an associated IR entity.
168/// `parameters` may be a null attribute. The caller owns the returned instance
169/// and must destroy it with `mlirMemoryEffectInstanceDestroy`.
171 MlirMemoryEffect effect, MlirAttribute parameters, int stage,
172 bool effectOnFullRegion, MlirSideEffectResource resource);
173
174/// Creates a memory effect instance associated with an operation operand.
175/// `parameters` may be a null attribute. The caller owns the returned instance
176/// and must destroy it with `mlirMemoryEffectInstanceDestroy`.
177MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
178mlirMemoryEffectInstanceCreateForOpOperand(MlirMemoryEffect effect,
179 MlirOpOperand opOperand,
180 MlirAttribute parameters, int stage,
181 bool effectOnFullRegion,
182 MlirSideEffectResource resource);
183
184/// Creates a memory effect instance associated with an operation result.
185/// `result` must wrap an OpResult. `parameters` may be a null attribute. The
186/// caller owns the returned instance and must destroy it with
187/// `mlirMemoryEffectInstanceDestroy`.
188MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
189mlirMemoryEffectInstanceCreateForOpResult(MlirMemoryEffect effect,
190 MlirValue result,
191 MlirAttribute parameters, int stage,
192 bool effectOnFullRegion,
193 MlirSideEffectResource resource);
194
195/// Creates a memory effect instance associated with a block argument.
196/// `blockArgument` must wrap a BlockArgument. `parameters` may be a null
197/// attribute. The caller owns the returned instance and must destroy it with
198/// `mlirMemoryEffectInstanceDestroy`.
199MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
201 MlirMemoryEffect effect, MlirValue blockArgument, MlirAttribute parameters,
202 int stage, bool effectOnFullRegion, MlirSideEffectResource resource);
203
204/// Creates a memory effect instance associated with a symbol. `symbol` must be
205/// a SymbolRefAttr. `parameters` may be a null attribute. The caller owns the
206/// returned instance and must destroy it with
207/// `mlirMemoryEffectInstanceDestroy`.
208MLIR_CAPI_EXPORTED MlirMemoryEffectInstance
209mlirMemoryEffectInstanceCreateForSymbol(MlirMemoryEffect effect,
210 MlirAttribute symbol,
211 MlirAttribute parameters, int stage,
212 bool effectOnFullRegion,
213 MlirSideEffectResource resource);
214
215/// Destroys a memory effect instance created by one of the functions above.
217mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance);
218
219/// Appends a copy of `instance` to the given list. This does not take ownership
220/// of `instance`; the caller remains responsible for destroying it.
222mlirMemoryEffectInstancesListAppend(MlirMemoryEffectInstancesList list,
223 MlirMemoryEffectInstance instance);
224
225/// Returns the interface TypeID of the MemoryEffectsOpInterface.
227
228/// Callbacks for implementing MemoryEffectsOpInterface from external code.
229typedef struct {
230 /// Optional constructor for user data. Set to nullptr to disable it.
232 /// Optional destructor for user data. Set to nullptr to disable it.
234 /// Get memory effects callback.
235 void (*getEffects)(MlirOperation op, MlirMemoryEffectInstancesList effects,
236 void *userData);
237 void *userData;
239
240/// Attach a new FallbackModel for the MemoryEffectsOpInterface to the named
241/// operation. The FallbackModel will call the provided callbacks.
243 MlirContext ctx, MlirStringRef opName,
245
246#ifdef __cplusplus
247}
248#endif
249
250#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 mlirInferTypeOpInterfaceTypeID(void)
Returns the interface TypeID of the InferTypeOpInterface.
MLIR_CAPI_EXPORTED void mlirMemoryEffectInstanceDestroy(MlirMemoryEffectInstance instance)
Destroys a memory effect instance created by one of the functions 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 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:108
@ MlirSpeculatabilityRecursivelySpeculatable
The operation is speculatable if all nested operations are speculatable.
Definition Interfaces.h:114
@ MlirSpeculatabilitySpeculatable
The operation is speculatable.
Definition Interfaces.h:112
@ MlirSpeculatabilityNotSpeculatable
The operation is not speculatable.
Definition Interfaces.h:110
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 MlirMemoryEffect mlirMemoryEffectsReadGet(void)
Returns the borrowed singleton instance of the read memory effect.
MLIR_CAPI_EXPORTED MlirSideEffectResource mlirSideEffectsDefaultResourceGet(void)
Returns the borrowed 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:90
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 MlirMemoryEffect mlirMemoryEffectsFreeGet(void)
Returns the borrowed singleton instance of the free memory effect.
MLIR_CAPI_EXPORTED MlirMemoryEffect mlirMemoryEffectsWriteGet(void)
Returns the borrowed singleton instance of the write memory effect.
MLIR_CAPI_EXPORTED MlirTypeID mlirMemoryEffectsOpInterfaceTypeID(void)
Returns the interface TypeID of the MemoryEffectsOpInterface.
void(* MlirTypesCallback)(intptr_t, MlirType *, void *)
These callbacks are used to return multiple types from functions while transferring ownership to the ...
Definition Interfaces.h:64
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 MlirMemoryEffect mlirMemoryEffectsAllocateGet(void)
Returns the borrowed 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 void mlirMemoryEffectInstancesListAppend(MlirMemoryEffectInstancesList list, MlirMemoryEffectInstance instance)
Appends a copy of instance to the given list.
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:122
void(* destruct)(void *userData)
Optional destructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:126
void(* construct)(void *userData)
Optional constructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:124
MlirSpeculatability(* getSpeculatability)(MlirOperation op, void *userData)
Returns the speculatability of the given operation.
Definition Interfaces.h:128
A logical result value, essentially a boolean with named states.
Definition Support.h:121
Callbacks for implementing MemoryEffectsOpInterface from external code.
Definition Interfaces.h:229
void(* construct)(void *userData)
Optional constructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:231
void(* getEffects)(MlirOperation op, MlirMemoryEffectInstancesList effects, void *userData)
Get memory effects callback.
Definition Interfaces.h:235
void(* destruct)(void *userData)
Optional destructor for user data. Set to nullptr to disable it.
Definition Interfaces.h:233
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78