1//===-- mlir-c/Dialect/Transform.h - C API for Transform Dialect --*- 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#ifndef MLIR_C_DIALECT_TRANSFORM_H
11#define MLIR_C_DIALECT_TRANSFORM_H
12
13#include "mlir-c/IR.h"
14#include "mlir-c/Interfaces.h"
15#include "mlir-c/Rewrite.h"
16#include "mlir-c/Support.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Transform, transform);
23
24#define DEFINE_C_API_STRUCT(name, storage) \
25 struct name { \
26 storage *ptr; \
27 }; \
28 typedef struct name name
29
30DEFINE_C_API_STRUCT(MlirTransformResults, void);
31DEFINE_C_API_STRUCT(MlirTransformRewriter, void);
32DEFINE_C_API_STRUCT(MlirTransformState, void);
33
34#undef DEFINE_C_API_STRUCT
35
36//===---------------------------------------------------------------------===//
37// DiagnosedSilenceableFailure
38//===---------------------------------------------------------------------===//
39
40/// Enum representing the result of a transform operation.
41typedef enum {
42 /// The operation succeeded.
43 MlirDiagnosedSilenceableFailureSuccess,
44 /// The operation failed in a silenceable way.
45 MlirDiagnosedSilenceableFailureSilenceableFailure,
46 /// The operation failed definitively.
47 MlirDiagnosedSilenceableFailureDefiniteFailure
48} MlirDiagnosedSilenceableFailure;
49
50//===---------------------------------------------------------------------===//
51// AnyOpType
52//===---------------------------------------------------------------------===//
53
54MLIR_CAPI_EXPORTED bool mlirTypeIsATransformAnyOpType(MlirType type);
55
56MLIR_CAPI_EXPORTED MlirTypeID mlirTransformAnyOpTypeGetTypeID(void);
57
58MLIR_CAPI_EXPORTED MlirType mlirTransformAnyOpTypeGet(MlirContext ctx);
59
60MLIR_CAPI_EXPORTED MlirStringRef mlirTransformAnyOpTypeGetName(void);
61
62//===---------------------------------------------------------------------===//
63// AnyParamType
64//===---------------------------------------------------------------------===//
65
66MLIR_CAPI_EXPORTED bool mlirTypeIsATransformAnyParamType(MlirType type);
67
68MLIR_CAPI_EXPORTED MlirTypeID mlirTransformAnyParamTypeGetTypeID(void);
69
70MLIR_CAPI_EXPORTED MlirType mlirTransformAnyParamTypeGet(MlirContext ctx);
71
72MLIR_CAPI_EXPORTED MlirStringRef mlirTransformAnyParamTypeGetName(void);
73
74//===---------------------------------------------------------------------===//
75// AnyValueType
76//===---------------------------------------------------------------------===//
77
78MLIR_CAPI_EXPORTED bool mlirTypeIsATransformAnyValueType(MlirType type);
79
80MLIR_CAPI_EXPORTED MlirTypeID mlirTransformAnyValueTypeGetTypeID(void);
81
82MLIR_CAPI_EXPORTED MlirType mlirTransformAnyValueTypeGet(MlirContext ctx);
83
84MLIR_CAPI_EXPORTED MlirStringRef mlirTransformAnyValueTypeGetName(void);
85
86//===---------------------------------------------------------------------===//
87// OperationType
88//===---------------------------------------------------------------------===//
89
90MLIR_CAPI_EXPORTED bool mlirTypeIsATransformOperationType(MlirType type);
91
92MLIR_CAPI_EXPORTED MlirTypeID mlirTransformOperationTypeGetTypeID(void);
93
94MLIR_CAPI_EXPORTED MlirType
95mlirTransformOperationTypeGet(MlirContext ctx, MlirStringRef operationName);
96
97MLIR_CAPI_EXPORTED MlirStringRef mlirTransformOperationTypeGetName(void);
98
99MLIR_CAPI_EXPORTED MlirStringRef
100mlirTransformOperationTypeGetOperationName(MlirType type);
101
102//===---------------------------------------------------------------------===//
103// ParamType
104//===---------------------------------------------------------------------===//
105
106MLIR_CAPI_EXPORTED bool mlirTypeIsATransformParamType(MlirType type);
107
108MLIR_CAPI_EXPORTED MlirTypeID mlirTransformParamTypeGetTypeID(void);
109
110MLIR_CAPI_EXPORTED MlirType mlirTransformParamTypeGet(MlirContext ctx,
111 MlirType type);
112
113MLIR_CAPI_EXPORTED MlirStringRef mlirTransformParamTypeGetName(void);
114
115MLIR_CAPI_EXPORTED MlirType mlirTransformParamTypeGetType(MlirType type);
116
117//===---------------------------------------------------------------------===//
118// TransformRewriter
119//===---------------------------------------------------------------------===//
120
121/// Cast the TransformRewriter to a RewriterBase
122MLIR_CAPI_EXPORTED MlirRewriterBase
123mlirTransformRewriterAsBase(MlirTransformRewriter rewriter);
124
125//===---------------------------------------------------------------------===//
126// TransformResults
127//===---------------------------------------------------------------------===//
128
129/// Set the payload operations for a transform result by iterating over a list.
130MLIR_CAPI_EXPORTED void mlirTransformResultsSetOps(MlirTransformResults results,
131 MlirValue result,
132 intptr_t numOps,
133 MlirOperation *ops);
134
135/// Set the payload values for a transform result by iterating over a list.
136MLIR_CAPI_EXPORTED void
137mlirTransformResultsSetValues(MlirTransformResults results, MlirValue result,
138 intptr_t numValues, MlirValue *values);
139
140/// Set the parameters for a transform result by iterating over a list.
141MLIR_CAPI_EXPORTED void
142mlirTransformResultsSetParams(MlirTransformResults results, MlirValue result,
143 intptr_t numParams, MlirAttribute *params);
144
145//===---------------------------------------------------------------------===//
146// TransformState
147//===---------------------------------------------------------------------===//
148
149/// Callback for iterating over payload operations.
150typedef void (*MlirOperationCallback)(MlirOperation, void *userData);
151
152/// Iterate over payload operations associated with the transform IR value.
153/// Calls the callback for each payload operation.
154MLIR_CAPI_EXPORTED void
155mlirTransformStateForEachPayloadOp(MlirTransformState state, MlirValue value,
156 MlirOperationCallback callback,
157 void *userData);
158
159/// Callback for iterating over payload values.
160typedef void (*MlirValueCallback)(MlirValue, void *userData);
161
162/// Iterate over payload values associated with the transform IR value.
163/// Calls the callback for each payload value.
164MLIR_CAPI_EXPORTED void
165mlirTransformStateForEachPayloadValue(MlirTransformState state, MlirValue value,
166 MlirValueCallback callback,
167 void *userData);
168
169/// Callback for iterating over parameters.
170typedef void (*MlirAttributeCallback)(MlirAttribute, void *userData);
171
172/// Iterate over parameters associated with the transform IR value.
173/// Calls the callback for each parameter.
174MLIR_CAPI_EXPORTED void
175mlirTransformStateForEachParam(MlirTransformState state, MlirValue value,
176 MlirAttributeCallback callback, void *userData);
177
178//===---------------------------------------------------------------------===//
179// TransformOpInterface
180//===---------------------------------------------------------------------===//
181
182/// Returns the interface TypeID of the TransformOpInterface.
183MLIR_CAPI_EXPORTED MlirTypeID mlirTransformOpInterfaceTypeID(void);
184
185/// Callbacks for implementing TransformOpInterface from external code.
186typedef struct {
187 /// Optional constructor for the user data.
188 /// Set to nullptr to disable it.
189 void (*construct)(void *userData);
190 /// Optional destructor for the user data.
191 /// Set to nullptr to disable it.
192 void (*destruct)(void *userData);
193 /// Apply callback that implements the transformation.
194 MlirDiagnosedSilenceableFailure (*apply)(MlirOperation op,
195 MlirTransformRewriter rewriter,
196 MlirTransformResults results,
197 MlirTransformState state,
198 void *userData);
199 /// Callback to check if repeated handle operands are allowed.
200 bool (*allowsRepeatedHandleOperands)(MlirOperation op, void *userData);
201 void *userData;
202} MlirTransformOpInterfaceCallbacks;
203
204/// Attach TransformOpInterface to the operation with the given name using
205/// the provided callbacks.
206MLIR_CAPI_EXPORTED void mlirTransformOpInterfaceAttachFallbackModel(
207 MlirContext ctx, MlirStringRef opName,
208 MlirTransformOpInterfaceCallbacks callbacks);
209
210//===---------------------------------------------------------------------===//
211// PatternDescriptorOpInterface
212//===---------------------------------------------------------------------===//
213
214/// Returns the interface TypeID of the PatternDescriptorOpInterface.
215MLIR_CAPI_EXPORTED MlirTypeID mlirPatternDescriptorOpInterfaceTypeID(void);
216
217/// Callbacks for implementing PatternDescriptorOpInterface from external code.
218typedef struct {
219 /// Optional constructor for the user data.
220 /// Set to nullptr to disable it.
221 void (*construct)(void *userData);
222 /// Optional destructor for the user data.
223 /// Set to nullptr to disable it.
224 void (*destruct)(void *userData);
225 /// Callback to populate rewrite patterns into the given pattern set.
226 void (*populatePatterns)(MlirOperation op, MlirRewritePatternSet patterns,
227 void *userData);
228 /// Optional callback to populate rewrite patterns with transform state.
229 /// Set to nullptr to use the default implementation (calls populatePatterns).
230 void (*populatePatternsWithState)(MlirOperation op,
231 MlirRewritePatternSet patterns,
232 MlirTransformState state, void *userData);
233 void *userData;
234} MlirPatternDescriptorOpInterfaceCallbacks;
235
236/// Attach PatternDescriptorOpInterface to the operation with the given name
237/// using the provided callbacks.
238MLIR_CAPI_EXPORTED void mlirPatternDescriptorOpInterfaceAttachFallbackModel(
239 MlirContext ctx, MlirStringRef opName,
240 MlirPatternDescriptorOpInterfaceCallbacks callbacks);
241
242//===---------------------------------------------------------------------===//
243// Transform-specifc MemoryEffectsOpInterface helpers
244//===---------------------------------------------------------------------===//
245
246/// Invokes `callback` with `OnlyReadsHandle` effects corresponding to operands
247/// which have been marked as having those effects.
248MLIR_CAPI_EXPORTED void
249mlirTransformOnlyReadsHandle(MlirOpOperand *operands, intptr_t numOperands,
250 MlirMemoryEffectInstancesCallback callback,
251 void *userData);
252
253/// Invokes `callback` with `ConsumesHandle` effects corresponding to operands
254/// which have been marked as having those effects.
255MLIR_CAPI_EXPORTED void
256mlirTransformConsumesHandle(MlirOpOperand *operands, intptr_t numOperands,
257 MlirMemoryEffectInstancesCallback callback,
258 void *userData);
259
260/// Invokes `callback` with `ProducesHandle` effects corresponding to results
261/// which have been marked as having those effects.
262MLIR_CAPI_EXPORTED void
263mlirTransformProducesHandle(MlirValue *results, intptr_t numResults,
264 MlirMemoryEffectInstancesCallback callback,
265 void *userData);
266
267/// Invokes `callback` with `ModifiesPayload` effects.
268MLIR_CAPI_EXPORTED void
269mlirTransformModifiesPayload(MlirMemoryEffectInstancesCallback callback,
270 void *userData);
271
272/// Invokes `callback` with `OnlyReadsPayload` effects.
273MLIR_CAPI_EXPORTED void
274mlirTransformOnlyReadsPayload(MlirMemoryEffectInstancesCallback callback,
275 void *userData);
276
277#ifdef __cplusplus
278}
279#endif
280
281#include "mlir/Dialect/Transform/Transforms/Passes.capi.h.inc"
282
283#endif // MLIR_C_DIALECT_TRANSFORM_H
284