1 | //===-- mlir-c/IR.h - C API to Core MLIR IR classes ---------------*- 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 core IR classes. |
11 | // |
12 | // Many exotic languages can interoperate with C code but have a harder time |
13 | // with C++ due to name mangling. So in addition to C, this interface enables |
14 | // tools written in such languages. |
15 | // |
16 | //===----------------------------------------------------------------------===// |
17 | |
18 | #ifndef MLIR_C_IR_H |
19 | #define MLIR_C_IR_H |
20 | |
21 | #include <stdbool.h> |
22 | #include <stdint.h> |
23 | |
24 | #include "mlir-c/Support.h" |
25 | |
26 | #ifdef __cplusplus |
27 | extern "C" { |
28 | #endif |
29 | |
30 | //===----------------------------------------------------------------------===// |
31 | /// Opaque type declarations. |
32 | /// |
33 | /// Types are exposed to C bindings as structs containing opaque pointers. They |
34 | /// are not supposed to be inspected from C. This allows the underlying |
35 | /// representation to change without affecting the API users. The use of structs |
36 | /// instead of typedefs enables some type safety as structs are not implicitly |
37 | /// convertible to each other. |
38 | /// |
39 | /// Instances of these types may or may not own the underlying object (most |
40 | /// often only point to an IR fragment without owning it). The ownership |
41 | /// semantics is defined by how an instance of the type was obtained. |
42 | |
43 | //===----------------------------------------------------------------------===// |
44 | |
45 | #define DEFINE_C_API_STRUCT(name, storage) \ |
46 | struct name { \ |
47 | storage *ptr; \ |
48 | }; \ |
49 | typedef struct name name |
50 | |
51 | DEFINE_C_API_STRUCT(MlirAsmState, void); |
52 | DEFINE_C_API_STRUCT(MlirBytecodeWriterConfig, void); |
53 | DEFINE_C_API_STRUCT(MlirContext, void); |
54 | DEFINE_C_API_STRUCT(MlirDialect, void); |
55 | DEFINE_C_API_STRUCT(MlirDialectRegistry, void); |
56 | DEFINE_C_API_STRUCT(MlirOperation, void); |
57 | DEFINE_C_API_STRUCT(MlirOpOperand, void); |
58 | DEFINE_C_API_STRUCT(MlirOpPrintingFlags, void); |
59 | DEFINE_C_API_STRUCT(MlirBlock, void); |
60 | DEFINE_C_API_STRUCT(MlirRegion, void); |
61 | DEFINE_C_API_STRUCT(MlirSymbolTable, void); |
62 | |
63 | DEFINE_C_API_STRUCT(MlirAttribute, const void); |
64 | DEFINE_C_API_STRUCT(MlirIdentifier, const void); |
65 | DEFINE_C_API_STRUCT(MlirLocation, const void); |
66 | DEFINE_C_API_STRUCT(MlirModule, const void); |
67 | DEFINE_C_API_STRUCT(MlirType, const void); |
68 | DEFINE_C_API_STRUCT(MlirValue, const void); |
69 | |
70 | #undef DEFINE_C_API_STRUCT |
71 | |
72 | /// Named MLIR attribute. |
73 | /// |
74 | /// A named attribute is essentially a (name, attribute) pair where the name is |
75 | /// a string. |
76 | struct MlirNamedAttribute { |
77 | MlirIdentifier name; |
78 | MlirAttribute attribute; |
79 | }; |
80 | typedef struct MlirNamedAttribute MlirNamedAttribute; |
81 | |
82 | //===----------------------------------------------------------------------===// |
83 | // Context API. |
84 | //===----------------------------------------------------------------------===// |
85 | |
86 | /// Creates an MLIR context and transfers its ownership to the caller. |
87 | /// This sets the default multithreading option (enabled). |
88 | MLIR_CAPI_EXPORTED MlirContext mlirContextCreate(void); |
89 | |
90 | /// Creates an MLIR context with an explicit setting of the multithreading |
91 | /// setting and transfers its ownership to the caller. |
92 | MLIR_CAPI_EXPORTED MlirContext |
93 | mlirContextCreateWithThreading(bool threadingEnabled); |
94 | |
95 | /// Creates an MLIR context, setting the multithreading setting explicitly and |
96 | /// pre-loading the dialects from the provided DialectRegistry. |
97 | MLIR_CAPI_EXPORTED MlirContext mlirContextCreateWithRegistry( |
98 | MlirDialectRegistry registry, bool threadingEnabled); |
99 | |
100 | /// Checks if two contexts are equal. |
101 | MLIR_CAPI_EXPORTED bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2); |
102 | |
103 | /// Checks whether a context is null. |
104 | static inline bool mlirContextIsNull(MlirContext context) { |
105 | return !context.ptr; |
106 | } |
107 | |
108 | /// Takes an MLIR context owned by the caller and destroys it. |
109 | MLIR_CAPI_EXPORTED void mlirContextDestroy(MlirContext context); |
110 | |
111 | /// Sets whether unregistered dialects are allowed in this context. |
112 | MLIR_CAPI_EXPORTED void |
113 | mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow); |
114 | |
115 | /// Returns whether the context allows unregistered dialects. |
116 | MLIR_CAPI_EXPORTED bool |
117 | mlirContextGetAllowUnregisteredDialects(MlirContext context); |
118 | |
119 | /// Returns the number of dialects registered with the given context. A |
120 | /// registered dialect will be loaded if needed by the parser. |
121 | MLIR_CAPI_EXPORTED intptr_t |
122 | mlirContextGetNumRegisteredDialects(MlirContext context); |
123 | |
124 | /// Append the contents of the given dialect registry to the registry associated |
125 | /// with the context. |
126 | MLIR_CAPI_EXPORTED void |
127 | mlirContextAppendDialectRegistry(MlirContext ctx, MlirDialectRegistry registry); |
128 | |
129 | /// Returns the number of dialects loaded by the context. |
130 | |
131 | MLIR_CAPI_EXPORTED intptr_t |
132 | mlirContextGetNumLoadedDialects(MlirContext context); |
133 | |
134 | /// Gets the dialect instance owned by the given context using the dialect |
135 | /// namespace to identify it, loads (i.e., constructs the instance of) the |
136 | /// dialect if necessary. If the dialect is not registered with the context, |
137 | /// returns null. Use mlirContextLoad<Name>Dialect to load an unregistered |
138 | /// dialect. |
139 | MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context, |
140 | MlirStringRef name); |
141 | |
142 | /// Set threading mode (must be set to false to mlir-print-ir-after-all). |
143 | MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context, |
144 | bool enable); |
145 | |
146 | /// Eagerly loads all available dialects registered with a context, making |
147 | /// them available for use for IR construction. |
148 | MLIR_CAPI_EXPORTED void |
149 | mlirContextLoadAllAvailableDialects(MlirContext context); |
150 | |
151 | /// Returns whether the given fully-qualified operation (i.e. |
152 | /// 'dialect.operation') is registered with the context. This will return true |
153 | /// if the dialect is loaded and the operation is registered within the |
154 | /// dialect. |
155 | MLIR_CAPI_EXPORTED bool mlirContextIsRegisteredOperation(MlirContext context, |
156 | MlirStringRef name); |
157 | |
158 | /// Sets the thread pool of the context explicitly, enabling multithreading in |
159 | /// the process. This API should be used to avoid re-creating thread pools in |
160 | /// long-running applications that perform multiple compilations, see |
161 | /// the C++ documentation for MLIRContext for details. |
162 | MLIR_CAPI_EXPORTED void mlirContextSetThreadPool(MlirContext context, |
163 | MlirLlvmThreadPool threadPool); |
164 | |
165 | /// Gets the number of threads of the thread pool of the context when |
166 | /// multithreading is enabled. Returns 1 if no multithreading. |
167 | MLIR_CAPI_EXPORTED unsigned mlirContextGetNumThreads(MlirContext context); |
168 | |
169 | /// Gets the thread pool of the context when enabled multithreading, otherwise |
170 | /// an assertion is raised. |
171 | MLIR_CAPI_EXPORTED MlirLlvmThreadPool |
172 | mlirContextGetThreadPool(MlirContext context); |
173 | |
174 | //===----------------------------------------------------------------------===// |
175 | // Dialect API. |
176 | //===----------------------------------------------------------------------===// |
177 | |
178 | /// Returns the context that owns the dialect. |
179 | MLIR_CAPI_EXPORTED MlirContext mlirDialectGetContext(MlirDialect dialect); |
180 | |
181 | /// Checks if the dialect is null. |
182 | static inline bool mlirDialectIsNull(MlirDialect dialect) { |
183 | return !dialect.ptr; |
184 | } |
185 | |
186 | /// Checks if two dialects that belong to the same context are equal. Dialects |
187 | /// from different contexts will not compare equal. |
188 | MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1, |
189 | MlirDialect dialect2); |
190 | |
191 | /// Returns the namespace of the given dialect. |
192 | MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect); |
193 | |
194 | //===----------------------------------------------------------------------===// |
195 | // DialectHandle API. |
196 | // Registration entry-points for each dialect are declared using the common |
197 | // MLIR_DECLARE_DIALECT_REGISTRATION_CAPI macro, which takes the dialect |
198 | // API name (i.e. "Func", "Tensor", "Linalg") and namespace (i.e. "func", |
199 | // "tensor", "linalg"). The following declarations are produced: |
200 | // |
201 | // /// Gets the above hook methods in struct form for a dialect by namespace. |
202 | // /// This is intended to facilitate dynamic lookup and registration of |
203 | // /// dialects via a plugin facility based on shared library symbol lookup. |
204 | // const MlirDialectHandle *mlirGetDialectHandle__{NAMESPACE}__(); |
205 | // |
206 | // This is done via a common macro to facilitate future expansion to |
207 | // registration schemes. |
208 | //===----------------------------------------------------------------------===// |
209 | |
210 | struct MlirDialectHandle { |
211 | const void *ptr; |
212 | }; |
213 | typedef struct MlirDialectHandle MlirDialectHandle; |
214 | |
215 | #define MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Name, Namespace) \ |
216 | MLIR_CAPI_EXPORTED MlirDialectHandle mlirGetDialectHandle__##Namespace##__( \ |
217 | void) |
218 | |
219 | /// Returns the namespace associated with the provided dialect handle. |
220 | MLIR_CAPI_EXPORTED |
221 | MlirStringRef mlirDialectHandleGetNamespace(MlirDialectHandle); |
222 | |
223 | /// Inserts the dialect associated with the provided dialect handle into the |
224 | /// provided dialect registry |
225 | MLIR_CAPI_EXPORTED void mlirDialectHandleInsertDialect(MlirDialectHandle, |
226 | MlirDialectRegistry); |
227 | |
228 | /// Registers the dialect associated with the provided dialect handle. |
229 | MLIR_CAPI_EXPORTED void mlirDialectHandleRegisterDialect(MlirDialectHandle, |
230 | MlirContext); |
231 | |
232 | /// Loads the dialect associated with the provided dialect handle. |
233 | MLIR_CAPI_EXPORTED MlirDialect mlirDialectHandleLoadDialect(MlirDialectHandle, |
234 | MlirContext); |
235 | |
236 | //===----------------------------------------------------------------------===// |
237 | // DialectRegistry API. |
238 | //===----------------------------------------------------------------------===// |
239 | |
240 | /// Creates a dialect registry and transfers its ownership to the caller. |
241 | MLIR_CAPI_EXPORTED MlirDialectRegistry mlirDialectRegistryCreate(void); |
242 | |
243 | /// Checks if the dialect registry is null. |
244 | static inline bool mlirDialectRegistryIsNull(MlirDialectRegistry registry) { |
245 | return !registry.ptr; |
246 | } |
247 | |
248 | /// Takes a dialect registry owned by the caller and destroys it. |
249 | MLIR_CAPI_EXPORTED void |
250 | mlirDialectRegistryDestroy(MlirDialectRegistry registry); |
251 | |
252 | //===----------------------------------------------------------------------===// |
253 | // Location API. |
254 | //===----------------------------------------------------------------------===// |
255 | |
256 | /// Returns the underlying location attribute of this location. |
257 | MLIR_CAPI_EXPORTED MlirAttribute |
258 | mlirLocationGetAttribute(MlirLocation location); |
259 | |
260 | /// Creates a location from a location attribute. |
261 | MLIR_CAPI_EXPORTED MlirLocation |
262 | mlirLocationFromAttribute(MlirAttribute attribute); |
263 | |
264 | /// Creates an File/Line/Column location owned by the given context. |
265 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet( |
266 | MlirContext context, MlirStringRef filename, unsigned line, unsigned col); |
267 | |
268 | /// Creates an File/Line/Column range location owned by the given context. |
269 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet( |
270 | MlirContext context, MlirStringRef filename, unsigned start_line, |
271 | unsigned start_col, unsigned end_line, unsigned end_col); |
272 | |
273 | /// Getter for filename of FileLineColRange. |
274 | MLIR_CAPI_EXPORTED MlirIdentifier |
275 | mlirLocationFileLineColRangeGetFilename(MlirLocation location); |
276 | |
277 | /// Getter for start_line of FileLineColRange. |
278 | MLIR_CAPI_EXPORTED int |
279 | mlirLocationFileLineColRangeGetStartLine(MlirLocation location); |
280 | |
281 | /// Getter for start_column of FileLineColRange. |
282 | MLIR_CAPI_EXPORTED int |
283 | mlirLocationFileLineColRangeGetStartColumn(MlirLocation location); |
284 | |
285 | /// Getter for end_line of FileLineColRange. |
286 | MLIR_CAPI_EXPORTED int |
287 | mlirLocationFileLineColRangeGetEndLine(MlirLocation location); |
288 | |
289 | /// Getter for end_column of FileLineColRange. |
290 | MLIR_CAPI_EXPORTED int |
291 | mlirLocationFileLineColRangeGetEndColumn(MlirLocation location); |
292 | |
293 | /// TypeID Getter for FileLineColRange. |
294 | MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFileLineColRangeGetTypeID(void); |
295 | |
296 | /// Checks whether the given location is an FileLineColRange. |
297 | MLIR_CAPI_EXPORTED bool mlirLocationIsAFileLineColRange(MlirLocation location); |
298 | |
299 | /// Creates a call site location with a callee and a caller. |
300 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee, |
301 | MlirLocation caller); |
302 | |
303 | /// Getter for callee of CallSite. |
304 | MLIR_CAPI_EXPORTED MlirLocation |
305 | mlirLocationCallSiteGetCallee(MlirLocation location); |
306 | |
307 | /// Getter for caller of CallSite. |
308 | MLIR_CAPI_EXPORTED MlirLocation |
309 | mlirLocationCallSiteGetCaller(MlirLocation location); |
310 | |
311 | /// TypeID Getter for CallSite. |
312 | MLIR_CAPI_EXPORTED MlirTypeID mlirLocationCallSiteGetTypeID(void); |
313 | |
314 | /// Checks whether the given location is an CallSite. |
315 | MLIR_CAPI_EXPORTED bool mlirLocationIsACallSite(MlirLocation location); |
316 | |
317 | /// Creates a fused location with an array of locations and metadata. |
318 | MLIR_CAPI_EXPORTED MlirLocation |
319 | mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, |
320 | MlirLocation const *locations, MlirAttribute metadata); |
321 | |
322 | /// Getter for number of locations fused together. |
323 | MLIR_CAPI_EXPORTED unsigned |
324 | mlirLocationFusedGetNumLocations(MlirLocation location); |
325 | |
326 | /// Getter for locations of Fused. Requires pre-allocated memory of |
327 | /// #fusedLocations X sizeof(MlirLocation). |
328 | MLIR_CAPI_EXPORTED void |
329 | mlirLocationFusedGetLocations(MlirLocation location, |
330 | MlirLocation *locationsCPtr); |
331 | |
332 | /// Getter for metadata of Fused. |
333 | MLIR_CAPI_EXPORTED MlirAttribute |
334 | mlirLocationFusedGetMetadata(MlirLocation location); |
335 | |
336 | /// TypeID Getter for Fused. |
337 | MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFusedGetTypeID(void); |
338 | |
339 | /// Checks whether the given location is an Fused. |
340 | MLIR_CAPI_EXPORTED bool mlirLocationIsAFused(MlirLocation location); |
341 | |
342 | /// Creates a name location owned by the given context. Providing null location |
343 | /// for childLoc is allowed and if childLoc is null location, then the behavior |
344 | /// is the same as having unknown child location. |
345 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context, |
346 | MlirStringRef name, |
347 | MlirLocation childLoc); |
348 | |
349 | /// Getter for name of Name. |
350 | MLIR_CAPI_EXPORTED MlirIdentifier |
351 | mlirLocationNameGetName(MlirLocation location); |
352 | |
353 | /// Getter for childLoc of Name. |
354 | MLIR_CAPI_EXPORTED MlirLocation |
355 | mlirLocationNameGetChildLoc(MlirLocation location); |
356 | |
357 | /// TypeID Getter for Name. |
358 | MLIR_CAPI_EXPORTED MlirTypeID mlirLocationNameGetTypeID(void); |
359 | |
360 | /// Checks whether the given location is an Name. |
361 | MLIR_CAPI_EXPORTED bool mlirLocationIsAName(MlirLocation location); |
362 | |
363 | /// Creates a location with unknown position owned by the given context. |
364 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context); |
365 | |
366 | /// Gets the context that a location was created with. |
367 | MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location); |
368 | |
369 | /// Checks if the location is null. |
370 | static inline bool mlirLocationIsNull(MlirLocation location) { |
371 | return !location.ptr; |
372 | } |
373 | |
374 | /// Checks if two locations are equal. |
375 | MLIR_CAPI_EXPORTED bool mlirLocationEqual(MlirLocation l1, MlirLocation l2); |
376 | |
377 | /// Prints a location by sending chunks of the string representation and |
378 | /// forwarding `userData to `callback`. Note that the callback may be called |
379 | /// several times with consecutive chunks of the string. |
380 | MLIR_CAPI_EXPORTED void mlirLocationPrint(MlirLocation location, |
381 | MlirStringCallback callback, |
382 | void *userData); |
383 | |
384 | //===----------------------------------------------------------------------===// |
385 | // Module API. |
386 | //===----------------------------------------------------------------------===// |
387 | |
388 | /// Creates a new, empty module and transfers ownership to the caller. |
389 | MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateEmpty(MlirLocation location); |
390 | |
391 | /// Parses a module from the string and transfers ownership to the caller. |
392 | MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateParse(MlirContext context, |
393 | MlirStringRef module); |
394 | |
395 | /// Parses a module from file and transfers ownership to the caller. |
396 | MLIR_CAPI_EXPORTED MlirModule |
397 | mlirModuleCreateParseFromFile(MlirContext context, MlirStringRef fileName); |
398 | |
399 | /// Gets the context that a module was created with. |
400 | MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext(MlirModule module); |
401 | |
402 | /// Gets the body of the module, i.e. the only block it contains. |
403 | MLIR_CAPI_EXPORTED MlirBlock mlirModuleGetBody(MlirModule module); |
404 | |
405 | /// Checks whether a module is null. |
406 | static inline bool mlirModuleIsNull(MlirModule module) { return !module.ptr; } |
407 | |
408 | /// Takes a module owned by the caller and deletes it. |
409 | MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module); |
410 | |
411 | /// Views the module as a generic operation. |
412 | MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module); |
413 | |
414 | /// Views the generic operation as a module. |
415 | /// The returned module is null when the input operation was not a ModuleOp. |
416 | MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op); |
417 | |
418 | //===----------------------------------------------------------------------===// |
419 | // Operation state. |
420 | //===----------------------------------------------------------------------===// |
421 | |
422 | /// An auxiliary class for constructing operations. |
423 | /// |
424 | /// This class contains all the information necessary to construct the |
425 | /// operation. It owns the MlirRegions it has pointers to and does not own |
426 | /// anything else. By default, the state can be constructed from a name and |
427 | /// location, the latter being also used to access the context, and has no other |
428 | /// components. These components can be added progressively until the operation |
429 | /// is constructed. Users are not expected to rely on the internals of this |
430 | /// class and should use mlirOperationState* functions instead. |
431 | |
432 | struct MlirOperationState { |
433 | MlirStringRef name; |
434 | MlirLocation location; |
435 | intptr_t nResults; |
436 | MlirType *results; |
437 | intptr_t nOperands; |
438 | MlirValue *operands; |
439 | intptr_t nRegions; |
440 | MlirRegion *regions; |
441 | intptr_t nSuccessors; |
442 | MlirBlock *successors; |
443 | intptr_t nAttributes; |
444 | MlirNamedAttribute *attributes; |
445 | bool enableResultTypeInference; |
446 | }; |
447 | typedef struct MlirOperationState MlirOperationState; |
448 | |
449 | /// Constructs an operation state from a name and a location. |
450 | MLIR_CAPI_EXPORTED MlirOperationState mlirOperationStateGet(MlirStringRef name, |
451 | MlirLocation loc); |
452 | |
453 | /// Adds a list of components to the operation state. |
454 | MLIR_CAPI_EXPORTED void mlirOperationStateAddResults(MlirOperationState *state, |
455 | intptr_t n, |
456 | MlirType const *results); |
457 | MLIR_CAPI_EXPORTED void |
458 | mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, |
459 | MlirValue const *operands); |
460 | MLIR_CAPI_EXPORTED void |
461 | mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, |
462 | MlirRegion const *regions); |
463 | MLIR_CAPI_EXPORTED void |
464 | mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, |
465 | MlirBlock const *successors); |
466 | MLIR_CAPI_EXPORTED void |
467 | mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, |
468 | MlirNamedAttribute const *attributes); |
469 | |
470 | /// Enables result type inference for the operation under construction. If |
471 | /// enabled, then the caller must not have called |
472 | /// mlirOperationStateAddResults(). Note that if enabled, the |
473 | /// mlirOperationCreate() call is failable: it will return a null operation |
474 | /// on inference failure and will emit diagnostics. |
475 | MLIR_CAPI_EXPORTED void |
476 | mlirOperationStateEnableResultTypeInference(MlirOperationState *state); |
477 | |
478 | //===----------------------------------------------------------------------===// |
479 | // AsmState API. |
480 | // While many of these are simple settings that could be represented in a |
481 | // struct, they are wrapped in a heap allocated object and accessed via |
482 | // functions to maximize the possibility of compatibility over time. |
483 | //===----------------------------------------------------------------------===// |
484 | |
485 | /// Creates new AsmState, as with AsmState the IR should not be mutated |
486 | /// in-between using this state. |
487 | /// Must be freed with a call to mlirAsmStateDestroy(). |
488 | // TODO: This should be expanded to handle location & resouce map. |
489 | MLIR_CAPI_EXPORTED MlirAsmState |
490 | mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags); |
491 | |
492 | /// Creates new AsmState from value. |
493 | /// Must be freed with a call to mlirAsmStateDestroy(). |
494 | // TODO: This should be expanded to handle location & resouce map. |
495 | MLIR_CAPI_EXPORTED MlirAsmState |
496 | mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags); |
497 | |
498 | /// Destroys printing flags created with mlirAsmStateCreate. |
499 | MLIR_CAPI_EXPORTED void mlirAsmStateDestroy(MlirAsmState state); |
500 | |
501 | //===----------------------------------------------------------------------===// |
502 | // Op Printing flags API. |
503 | // While many of these are simple settings that could be represented in a |
504 | // struct, they are wrapped in a heap allocated object and accessed via |
505 | // functions to maximize the possibility of compatibility over time. |
506 | //===----------------------------------------------------------------------===// |
507 | |
508 | /// Creates new printing flags with defaults, intended for customization. |
509 | /// Must be freed with a call to mlirOpPrintingFlagsDestroy(). |
510 | MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void); |
511 | |
512 | /// Destroys printing flags created with mlirOpPrintingFlagsCreate. |
513 | MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags); |
514 | |
515 | /// Enables the elision of large elements attributes by printing a lexically |
516 | /// valid but otherwise meaningless form instead of the element data. The |
517 | /// `largeElementLimit` is used to configure what is considered to be a "large" |
518 | /// ElementsAttr by providing an upper limit to the number of elements. |
519 | MLIR_CAPI_EXPORTED void |
520 | mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, |
521 | intptr_t largeElementLimit); |
522 | |
523 | /// Enables the elision of large resources strings by omitting them from the |
524 | /// `dialect_resources` section. The `largeResourceLimit` is used to configure |
525 | /// what is considered to be a "large" resource by providing an upper limit to |
526 | /// the string size. |
527 | MLIR_CAPI_EXPORTED void |
528 | mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, |
529 | intptr_t largeResourceLimit); |
530 | |
531 | /// Enable or disable printing of debug information (based on `enable`). If |
532 | /// 'prettyForm' is set to true, debug information is printed in a more readable |
533 | /// 'pretty' form. Note: The IR generated with 'prettyForm' is not parsable. |
534 | MLIR_CAPI_EXPORTED void |
535 | mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, |
536 | bool prettyForm); |
537 | |
538 | /// Always print operations in the generic form. |
539 | MLIR_CAPI_EXPORTED void |
540 | mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags); |
541 | |
542 | /// Print the name and location, if NamedLoc, as a prefix to the SSA ID. |
543 | MLIR_CAPI_EXPORTED void |
544 | mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags); |
545 | |
546 | /// Use local scope when printing the operation. This allows for using the |
547 | /// printer in a more localized and thread-safe setting, but may not |
548 | /// necessarily be identical to what the IR will look like when dumping |
549 | /// the full module. |
550 | MLIR_CAPI_EXPORTED void |
551 | mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags); |
552 | |
553 | /// Do not verify the operation when using custom operation printers. |
554 | MLIR_CAPI_EXPORTED void |
555 | mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags); |
556 | |
557 | /// Skip printing regions. |
558 | MLIR_CAPI_EXPORTED void |
559 | mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags); |
560 | |
561 | //===----------------------------------------------------------------------===// |
562 | // Bytecode printing flags API. |
563 | //===----------------------------------------------------------------------===// |
564 | |
565 | /// Creates new printing flags with defaults, intended for customization. |
566 | /// Must be freed with a call to mlirBytecodeWriterConfigDestroy(). |
567 | MLIR_CAPI_EXPORTED MlirBytecodeWriterConfig |
568 | mlirBytecodeWriterConfigCreate(void); |
569 | |
570 | /// Destroys printing flags created with mlirBytecodeWriterConfigCreate. |
571 | MLIR_CAPI_EXPORTED void |
572 | mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config); |
573 | |
574 | /// Sets the version to emit in the writer config. |
575 | MLIR_CAPI_EXPORTED void |
576 | mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, |
577 | int64_t version); |
578 | |
579 | //===----------------------------------------------------------------------===// |
580 | // Operation API. |
581 | //===----------------------------------------------------------------------===// |
582 | |
583 | /// Creates an operation and transfers ownership to the caller. |
584 | /// Note that caller owned child objects are transferred in this call and must |
585 | /// not be further used. Particularly, this applies to any regions added to |
586 | /// the state (the implementation may invalidate any such pointers). |
587 | /// |
588 | /// This call can fail under the following conditions, in which case, it will |
589 | /// return a null operation and emit diagnostics: |
590 | /// - Result type inference is enabled and cannot be performed. |
591 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreate(MlirOperationState *state); |
592 | |
593 | /// Parses an operation, giving ownership to the caller. If parsing fails a null |
594 | /// operation will be returned, and an error diagnostic emitted. |
595 | /// |
596 | /// `sourceStr` may be either the text assembly format, or binary bytecode |
597 | /// format. `sourceName` is used as the file name of the source; any IR without |
598 | /// locations will get a `FileLineColLoc` location with `sourceName` as the file |
599 | /// name. |
600 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreateParse( |
601 | MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName); |
602 | |
603 | /// Creates a deep copy of an operation. The operation is not inserted and |
604 | /// ownership is transferred to the caller. |
605 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationClone(MlirOperation op); |
606 | |
607 | /// Takes an operation owned by the caller and destroys it. |
608 | MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op); |
609 | |
610 | /// Removes the given operation from its parent block. The operation is not |
611 | /// destroyed. The ownership of the operation is transferred to the caller. |
612 | MLIR_CAPI_EXPORTED void mlirOperationRemoveFromParent(MlirOperation op); |
613 | |
614 | /// Checks whether the underlying operation is null. |
615 | static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; } |
616 | |
617 | /// Checks whether two operation handles point to the same operation. This does |
618 | /// not perform deep comparison. |
619 | MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op, |
620 | MlirOperation other); |
621 | |
622 | /// Gets the context this operation is associated with |
623 | MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op); |
624 | |
625 | /// Gets the location of the operation. |
626 | MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op); |
627 | |
628 | /// Gets the type id of the operation. |
629 | /// Returns null if the operation does not have a registered operation |
630 | /// description. |
631 | MLIR_CAPI_EXPORTED MlirTypeID mlirOperationGetTypeID(MlirOperation op); |
632 | |
633 | /// Gets the name of the operation as an identifier. |
634 | MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op); |
635 | |
636 | /// Gets the block that owns this operation, returning null if the operation is |
637 | /// not owned. |
638 | MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetBlock(MlirOperation op); |
639 | |
640 | /// Gets the operation that owns this operation, returning null if the operation |
641 | /// is not owned. |
642 | MLIR_CAPI_EXPORTED MlirOperation |
643 | mlirOperationGetParentOperation(MlirOperation op); |
644 | |
645 | /// Returns the number of regions attached to the given operation. |
646 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumRegions(MlirOperation op); |
647 | |
648 | /// Returns `pos`-th region attached to the operation. |
649 | MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetRegion(MlirOperation op, |
650 | intptr_t pos); |
651 | |
652 | /// Returns an operation immediately following the given operation it its |
653 | /// enclosing block. |
654 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationGetNextInBlock(MlirOperation op); |
655 | |
656 | /// Returns the number of operands of the operation. |
657 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumOperands(MlirOperation op); |
658 | |
659 | /// Returns `pos`-th operand of the operation. |
660 | MLIR_CAPI_EXPORTED MlirValue mlirOperationGetOperand(MlirOperation op, |
661 | intptr_t pos); |
662 | |
663 | /// Sets the `pos`-th operand of the operation. |
664 | MLIR_CAPI_EXPORTED void mlirOperationSetOperand(MlirOperation op, intptr_t pos, |
665 | MlirValue newValue); |
666 | |
667 | /// Replaces the operands of the operation. |
668 | MLIR_CAPI_EXPORTED void mlirOperationSetOperands(MlirOperation op, |
669 | intptr_t nOperands, |
670 | MlirValue const *operands); |
671 | |
672 | /// Returns the number of results of the operation. |
673 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumResults(MlirOperation op); |
674 | |
675 | /// Returns `pos`-th result of the operation. |
676 | MLIR_CAPI_EXPORTED MlirValue mlirOperationGetResult(MlirOperation op, |
677 | intptr_t pos); |
678 | |
679 | /// Returns the number of successor blocks of the operation. |
680 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumSuccessors(MlirOperation op); |
681 | |
682 | /// Returns `pos`-th successor of the operation. |
683 | MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetSuccessor(MlirOperation op, |
684 | intptr_t pos); |
685 | |
686 | /// Set `pos`-th successor of the operation. |
687 | MLIR_CAPI_EXPORTED void |
688 | mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block); |
689 | |
690 | /// Returns true if this operation defines an inherent attribute with this name. |
691 | /// Note: the attribute can be optional, so |
692 | /// `mlirOperationGetInherentAttributeByName` can still return a null attribute. |
693 | MLIR_CAPI_EXPORTED bool |
694 | mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name); |
695 | |
696 | /// Returns an inherent attribute attached to the operation given its name. |
697 | MLIR_CAPI_EXPORTED MlirAttribute |
698 | mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name); |
699 | |
700 | /// Sets an inherent attribute by name, replacing the existing if it exists. |
701 | /// This has no effect if "name" does not match an inherent attribute. |
702 | MLIR_CAPI_EXPORTED void |
703 | mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name, |
704 | MlirAttribute attr); |
705 | |
706 | /// Returns the number of discardable attributes attached to the operation. |
707 | MLIR_CAPI_EXPORTED intptr_t |
708 | mlirOperationGetNumDiscardableAttributes(MlirOperation op); |
709 | |
710 | /// Return `pos`-th discardable attribute of the operation. |
711 | MLIR_CAPI_EXPORTED MlirNamedAttribute |
712 | mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos); |
713 | |
714 | /// Returns a discardable attribute attached to the operation given its name. |
715 | MLIR_CAPI_EXPORTED MlirAttribute mlirOperationGetDiscardableAttributeByName( |
716 | MlirOperation op, MlirStringRef name); |
717 | |
718 | /// Sets a discardable attribute by name, replacing the existing if it exists or |
719 | /// adding a new one otherwise. The new `attr` Attribute is not allowed to be |
720 | /// null, use `mlirOperationRemoveDiscardableAttributeByName` to remove an |
721 | /// Attribute instead. |
722 | MLIR_CAPI_EXPORTED void |
723 | mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name, |
724 | MlirAttribute attr); |
725 | |
726 | /// Removes a discardable attribute by name. Returns false if the attribute was |
727 | /// not found and true if removed. |
728 | MLIR_CAPI_EXPORTED bool |
729 | mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, |
730 | MlirStringRef name); |
731 | |
732 | /// Returns the number of attributes attached to the operation. |
733 | /// Deprecated, please use `mlirOperationGetNumInherentAttributes` or |
734 | /// `mlirOperationGetNumDiscardableAttributes`. |
735 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumAttributes(MlirOperation op); |
736 | |
737 | /// Return `pos`-th attribute of the operation. |
738 | /// Deprecated, please use `mlirOperationGetInherentAttribute` or |
739 | /// `mlirOperationGetDiscardableAttribute`. |
740 | MLIR_CAPI_EXPORTED MlirNamedAttribute |
741 | mlirOperationGetAttribute(MlirOperation op, intptr_t pos); |
742 | |
743 | /// Returns an attribute attached to the operation given its name. |
744 | /// Deprecated, please use `mlirOperationGetInherentAttributeByName` or |
745 | /// `mlirOperationGetDiscardableAttributeByName`. |
746 | MLIR_CAPI_EXPORTED MlirAttribute |
747 | mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name); |
748 | |
749 | /// Sets an attribute by name, replacing the existing if it exists or |
750 | /// adding a new one otherwise. |
751 | /// Deprecated, please use `mlirOperationSetInherentAttributeByName` or |
752 | /// `mlirOperationSetDiscardableAttributeByName`. |
753 | MLIR_CAPI_EXPORTED void mlirOperationSetAttributeByName(MlirOperation op, |
754 | MlirStringRef name, |
755 | MlirAttribute attr); |
756 | |
757 | /// Removes an attribute by name. Returns false if the attribute was not found |
758 | /// and true if removed. |
759 | /// Deprecated, please use `mlirOperationRemoveInherentAttributeByName` or |
760 | /// `mlirOperationRemoveDiscardableAttributeByName`. |
761 | MLIR_CAPI_EXPORTED bool mlirOperationRemoveAttributeByName(MlirOperation op, |
762 | MlirStringRef name); |
763 | |
764 | /// Prints an operation by sending chunks of the string representation and |
765 | /// forwarding `userData to `callback`. Note that the callback may be called |
766 | /// several times with consecutive chunks of the string. |
767 | MLIR_CAPI_EXPORTED void mlirOperationPrint(MlirOperation op, |
768 | MlirStringCallback callback, |
769 | void *userData); |
770 | |
771 | /// Same as mlirOperationPrint but accepts flags controlling the printing |
772 | /// behavior. |
773 | MLIR_CAPI_EXPORTED void mlirOperationPrintWithFlags(MlirOperation op, |
774 | MlirOpPrintingFlags flags, |
775 | MlirStringCallback callback, |
776 | void *userData); |
777 | |
778 | /// Same as mlirOperationPrint but accepts AsmState controlling the printing |
779 | /// behavior as well as caching computed names. |
780 | MLIR_CAPI_EXPORTED void mlirOperationPrintWithState(MlirOperation op, |
781 | MlirAsmState state, |
782 | MlirStringCallback callback, |
783 | void *userData); |
784 | |
785 | /// Same as mlirOperationPrint but writing the bytecode format. |
786 | MLIR_CAPI_EXPORTED void mlirOperationWriteBytecode(MlirOperation op, |
787 | MlirStringCallback callback, |
788 | void *userData); |
789 | |
790 | /// Same as mlirOperationWriteBytecode but with writer config and returns |
791 | /// failure only if desired bytecode could not be honored. |
792 | MLIR_CAPI_EXPORTED MlirLogicalResult mlirOperationWriteBytecodeWithConfig( |
793 | MlirOperation op, MlirBytecodeWriterConfig config, |
794 | MlirStringCallback callback, void *userData); |
795 | |
796 | /// Prints an operation to stderr. |
797 | MLIR_CAPI_EXPORTED void mlirOperationDump(MlirOperation op); |
798 | |
799 | /// Verify the operation and return true if it passes, false if it fails. |
800 | MLIR_CAPI_EXPORTED bool mlirOperationVerify(MlirOperation op); |
801 | |
802 | /// Moves the given operation immediately after the other operation in its |
803 | /// parent block. The given operation may be owned by the caller or by its |
804 | /// current block. The other operation must belong to a block. In any case, the |
805 | /// ownership is transferred to the block of the other operation. |
806 | MLIR_CAPI_EXPORTED void mlirOperationMoveAfter(MlirOperation op, |
807 | MlirOperation other); |
808 | |
809 | /// Moves the given operation immediately before the other operation in its |
810 | /// parent block. The given operation may be owner by the caller or by its |
811 | /// current block. The other operation must belong to a block. In any case, the |
812 | /// ownership is transferred to the block of the other operation. |
813 | MLIR_CAPI_EXPORTED void mlirOperationMoveBefore(MlirOperation op, |
814 | MlirOperation other); |
815 | |
816 | /// Operation walk result. |
817 | typedef enum MlirWalkResult { |
818 | MlirWalkResultAdvance, |
819 | MlirWalkResultInterrupt, |
820 | MlirWalkResultSkip |
821 | } MlirWalkResult; |
822 | |
823 | /// Traversal order for operation walk. |
824 | typedef enum MlirWalkOrder { |
825 | MlirWalkPreOrder, |
826 | MlirWalkPostOrder |
827 | } MlirWalkOrder; |
828 | |
829 | /// Operation walker type. The handler is passed an (opaque) reference to an |
830 | /// operation and a pointer to a `userData`. |
831 | typedef MlirWalkResult (*MlirOperationWalkCallback)(MlirOperation, |
832 | void *userData); |
833 | |
834 | /// Walks operation `op` in `walkOrder` and calls `callback` on that operation. |
835 | /// `*userData` is passed to the callback as well and can be used to tunnel some |
836 | /// context or other data into the callback. |
837 | MLIR_CAPI_EXPORTED |
838 | void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, |
839 | void *userData, MlirWalkOrder walkOrder); |
840 | |
841 | //===----------------------------------------------------------------------===// |
842 | // Region API. |
843 | //===----------------------------------------------------------------------===// |
844 | |
845 | /// Creates a new empty region and transfers ownership to the caller. |
846 | MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate(void); |
847 | |
848 | /// Takes a region owned by the caller and destroys it. |
849 | MLIR_CAPI_EXPORTED void mlirRegionDestroy(MlirRegion region); |
850 | |
851 | /// Checks whether a region is null. |
852 | static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; } |
853 | |
854 | /// Checks whether two region handles point to the same region. This does not |
855 | /// perform deep comparison. |
856 | MLIR_CAPI_EXPORTED bool mlirRegionEqual(MlirRegion region, MlirRegion other); |
857 | |
858 | /// Gets the first block in the region. |
859 | MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region); |
860 | |
861 | /// Takes a block owned by the caller and appends it to the given region. |
862 | MLIR_CAPI_EXPORTED void mlirRegionAppendOwnedBlock(MlirRegion region, |
863 | MlirBlock block); |
864 | |
865 | /// Takes a block owned by the caller and inserts it at `pos` to the given |
866 | /// region. This is an expensive operation that linearly scans the region, |
867 | /// prefer insertAfter/Before instead. |
868 | MLIR_CAPI_EXPORTED void |
869 | mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block); |
870 | |
871 | /// Takes a block owned by the caller and inserts it after the (non-owned) |
872 | /// reference block in the given region. The reference block must belong to the |
873 | /// region. If the reference block is null, prepends the block to the region. |
874 | MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockAfter(MlirRegion region, |
875 | MlirBlock reference, |
876 | MlirBlock block); |
877 | |
878 | /// Takes a block owned by the caller and inserts it before the (non-owned) |
879 | /// reference block in the given region. The reference block must belong to the |
880 | /// region. If the reference block is null, appends the block to the region. |
881 | MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockBefore(MlirRegion region, |
882 | MlirBlock reference, |
883 | MlirBlock block); |
884 | |
885 | /// Returns first region attached to the operation. |
886 | MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetFirstRegion(MlirOperation op); |
887 | |
888 | /// Returns the region immediately following the given region in its parent |
889 | /// operation. |
890 | MLIR_CAPI_EXPORTED MlirRegion mlirRegionGetNextInOperation(MlirRegion region); |
891 | |
892 | /// Moves the entire content of the source region to the target region. |
893 | MLIR_CAPI_EXPORTED void mlirRegionTakeBody(MlirRegion target, |
894 | MlirRegion source); |
895 | |
896 | //===----------------------------------------------------------------------===// |
897 | // Block API. |
898 | //===----------------------------------------------------------------------===// |
899 | |
900 | /// Creates a new empty block with the given argument types and transfers |
901 | /// ownership to the caller. |
902 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs, |
903 | MlirType const *args, |
904 | MlirLocation const *locs); |
905 | |
906 | /// Takes a block owned by the caller and destroys it. |
907 | MLIR_CAPI_EXPORTED void mlirBlockDestroy(MlirBlock block); |
908 | |
909 | /// Detach a block from the owning region and assume ownership. |
910 | MLIR_CAPI_EXPORTED void mlirBlockDetach(MlirBlock block); |
911 | |
912 | /// Checks whether a block is null. |
913 | static inline bool mlirBlockIsNull(MlirBlock block) { return !block.ptr; } |
914 | |
915 | /// Checks whether two blocks handles point to the same block. This does not |
916 | /// perform deep comparison. |
917 | MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other); |
918 | |
919 | /// Returns the closest surrounding operation that contains this block. |
920 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock); |
921 | |
922 | /// Returns the region that contains this block. |
923 | MLIR_CAPI_EXPORTED MlirRegion mlirBlockGetParentRegion(MlirBlock block); |
924 | |
925 | /// Returns the block immediately following the given block in its parent |
926 | /// region. |
927 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block); |
928 | |
929 | /// Returns the first operation in the block. |
930 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetFirstOperation(MlirBlock block); |
931 | |
932 | /// Returns the terminator operation in the block or null if no terminator. |
933 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetTerminator(MlirBlock block); |
934 | |
935 | /// Takes an operation owned by the caller and appends it to the block. |
936 | MLIR_CAPI_EXPORTED void mlirBlockAppendOwnedOperation(MlirBlock block, |
937 | MlirOperation operation); |
938 | |
939 | /// Takes an operation owned by the caller and inserts it as `pos` to the block. |
940 | /// This is an expensive operation that scans the block linearly, prefer |
941 | /// insertBefore/After instead. |
942 | MLIR_CAPI_EXPORTED void mlirBlockInsertOwnedOperation(MlirBlock block, |
943 | intptr_t pos, |
944 | MlirOperation operation); |
945 | |
946 | /// Takes an operation owned by the caller and inserts it after the (non-owned) |
947 | /// reference operation in the given block. If the reference is null, prepends |
948 | /// the operation. Otherwise, the reference must belong to the block. |
949 | MLIR_CAPI_EXPORTED void |
950 | mlirBlockInsertOwnedOperationAfter(MlirBlock block, MlirOperation reference, |
951 | MlirOperation operation); |
952 | |
953 | /// Takes an operation owned by the caller and inserts it before the (non-owned) |
954 | /// reference operation in the given block. If the reference is null, appends |
955 | /// the operation. Otherwise, the reference must belong to the block. |
956 | MLIR_CAPI_EXPORTED void |
957 | mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference, |
958 | MlirOperation operation); |
959 | |
960 | /// Returns the number of arguments of the block. |
961 | MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumArguments(MlirBlock block); |
962 | |
963 | /// Appends an argument of the specified type to the block. Returns the newly |
964 | /// added argument. |
965 | MLIR_CAPI_EXPORTED MlirValue mlirBlockAddArgument(MlirBlock block, |
966 | MlirType type, |
967 | MlirLocation loc); |
968 | |
969 | /// Erase the argument at 'index' and remove it from the argument list. |
970 | MLIR_CAPI_EXPORTED void mlirBlockEraseArgument(MlirBlock block, unsigned index); |
971 | |
972 | /// Inserts an argument of the specified type at a specified index to the block. |
973 | /// Returns the newly added argument. |
974 | MLIR_CAPI_EXPORTED MlirValue mlirBlockInsertArgument(MlirBlock block, |
975 | intptr_t pos, |
976 | MlirType type, |
977 | MlirLocation loc); |
978 | |
979 | /// Returns `pos`-th argument of the block. |
980 | MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block, |
981 | intptr_t pos); |
982 | |
983 | /// Prints a block by sending chunks of the string representation and |
984 | /// forwarding `userData to `callback`. Note that the callback may be called |
985 | /// several times with consecutive chunks of the string. |
986 | MLIR_CAPI_EXPORTED void |
987 | mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData); |
988 | |
989 | //===----------------------------------------------------------------------===// |
990 | // Value API. |
991 | //===----------------------------------------------------------------------===// |
992 | |
993 | /// Returns whether the value is null. |
994 | static inline bool mlirValueIsNull(MlirValue value) { return !value.ptr; } |
995 | |
996 | /// Returns 1 if two values are equal, 0 otherwise. |
997 | MLIR_CAPI_EXPORTED bool mlirValueEqual(MlirValue value1, MlirValue value2); |
998 | |
999 | /// Returns 1 if the value is a block argument, 0 otherwise. |
1000 | MLIR_CAPI_EXPORTED bool mlirValueIsABlockArgument(MlirValue value); |
1001 | |
1002 | /// Returns 1 if the value is an operation result, 0 otherwise. |
1003 | MLIR_CAPI_EXPORTED bool mlirValueIsAOpResult(MlirValue value); |
1004 | |
1005 | /// Returns the block in which this value is defined as an argument. Asserts if |
1006 | /// the value is not a block argument. |
1007 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockArgumentGetOwner(MlirValue value); |
1008 | |
1009 | /// Returns the position of the value in the argument list of its block. |
1010 | MLIR_CAPI_EXPORTED intptr_t mlirBlockArgumentGetArgNumber(MlirValue value); |
1011 | |
1012 | /// Sets the type of the block argument to the given type. |
1013 | MLIR_CAPI_EXPORTED void mlirBlockArgumentSetType(MlirValue value, |
1014 | MlirType type); |
1015 | |
1016 | /// Returns an operation that produced this value as its result. Asserts if the |
1017 | /// value is not an op result. |
1018 | MLIR_CAPI_EXPORTED MlirOperation mlirOpResultGetOwner(MlirValue value); |
1019 | |
1020 | /// Returns the position of the value in the list of results of the operation |
1021 | /// that produced it. |
1022 | MLIR_CAPI_EXPORTED intptr_t mlirOpResultGetResultNumber(MlirValue value); |
1023 | |
1024 | /// Returns the type of the value. |
1025 | MLIR_CAPI_EXPORTED MlirType mlirValueGetType(MlirValue value); |
1026 | |
1027 | /// Set the type of the value. |
1028 | MLIR_CAPI_EXPORTED void mlirValueSetType(MlirValue value, MlirType type); |
1029 | |
1030 | /// Prints the value to the standard error stream. |
1031 | MLIR_CAPI_EXPORTED void mlirValueDump(MlirValue value); |
1032 | |
1033 | /// Prints a value by sending chunks of the string representation and |
1034 | /// forwarding `userData to `callback`. Note that the callback may be called |
1035 | /// several times with consecutive chunks of the string. |
1036 | MLIR_CAPI_EXPORTED void |
1037 | mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData); |
1038 | |
1039 | /// Prints a value as an operand (i.e., the ValueID). |
1040 | MLIR_CAPI_EXPORTED void mlirValuePrintAsOperand(MlirValue value, |
1041 | MlirAsmState state, |
1042 | MlirStringCallback callback, |
1043 | void *userData); |
1044 | |
1045 | /// Returns an op operand representing the first use of the value, or a null op |
1046 | /// operand if there are no uses. |
1047 | MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value); |
1048 | |
1049 | /// Replace all uses of 'of' value with the 'with' value, updating anything in |
1050 | /// the IR that uses 'of' to use the other value instead. When this returns |
1051 | /// there are zero uses of 'of'. |
1052 | MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of, |
1053 | MlirValue with); |
1054 | |
1055 | /// Replace all uses of 'of' value with 'with' value, updating anything in the |
1056 | /// IR that uses 'of' to use 'with' instead, except if the user is listed in |
1057 | /// 'exceptions'. The 'exceptions' parameter is an array of MlirOperation |
1058 | /// pointers with a length of 'numExceptions'. |
1059 | MLIR_CAPI_EXPORTED void |
1060 | mlirValueReplaceAllUsesExcept(MlirValue of, MlirValue with, |
1061 | intptr_t numExceptions, |
1062 | MlirOperation *exceptions); |
1063 | |
1064 | /// Gets the location of the value. |
1065 | MLIR_CAPI_EXPORTED MlirLocation mlirValueGetLocation(MlirValue v); |
1066 | |
1067 | /// Gets the context that a value was created with. |
1068 | MLIR_CAPI_EXPORTED MlirContext mlirValueGetContext(MlirValue v); |
1069 | |
1070 | //===----------------------------------------------------------------------===// |
1071 | // OpOperand API. |
1072 | //===----------------------------------------------------------------------===// |
1073 | |
1074 | /// Returns whether the op operand is null. |
1075 | MLIR_CAPI_EXPORTED bool mlirOpOperandIsNull(MlirOpOperand opOperand); |
1076 | |
1077 | /// Returns the value of an op operand. |
1078 | MLIR_CAPI_EXPORTED MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand); |
1079 | |
1080 | /// Returns the owner operation of an op operand. |
1081 | MLIR_CAPI_EXPORTED MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand); |
1082 | |
1083 | /// Returns the operand number of an op operand. |
1084 | MLIR_CAPI_EXPORTED unsigned |
1085 | mlirOpOperandGetOperandNumber(MlirOpOperand opOperand); |
1086 | |
1087 | /// Returns an op operand representing the next use of the value, or a null op |
1088 | /// operand if there is no next use. |
1089 | MLIR_CAPI_EXPORTED MlirOpOperand |
1090 | mlirOpOperandGetNextUse(MlirOpOperand opOperand); |
1091 | |
1092 | //===----------------------------------------------------------------------===// |
1093 | // Type API. |
1094 | //===----------------------------------------------------------------------===// |
1095 | |
1096 | /// Parses a type. The type is owned by the context. |
1097 | MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context, |
1098 | MlirStringRef type); |
1099 | |
1100 | /// Gets the context that a type was created with. |
1101 | MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type); |
1102 | |
1103 | /// Gets the type ID of the type. |
1104 | MLIR_CAPI_EXPORTED MlirTypeID mlirTypeGetTypeID(MlirType type); |
1105 | |
1106 | /// Gets the dialect a type belongs to. |
1107 | MLIR_CAPI_EXPORTED MlirDialect mlirTypeGetDialect(MlirType type); |
1108 | |
1109 | /// Checks whether a type is null. |
1110 | static inline bool mlirTypeIsNull(MlirType type) { return !type.ptr; } |
1111 | |
1112 | /// Checks if two types are equal. |
1113 | MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2); |
1114 | |
1115 | /// Prints a location by sending chunks of the string representation and |
1116 | /// forwarding `userData to `callback`. Note that the callback may be called |
1117 | /// several times with consecutive chunks of the string. |
1118 | MLIR_CAPI_EXPORTED void |
1119 | mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData); |
1120 | |
1121 | /// Prints the type to the standard error stream. |
1122 | MLIR_CAPI_EXPORTED void mlirTypeDump(MlirType type); |
1123 | |
1124 | //===----------------------------------------------------------------------===// |
1125 | // Attribute API. |
1126 | //===----------------------------------------------------------------------===// |
1127 | |
1128 | /// Parses an attribute. The attribute is owned by the context. |
1129 | MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeParseGet(MlirContext context, |
1130 | MlirStringRef attr); |
1131 | |
1132 | /// Gets the context that an attribute was created with. |
1133 | MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute); |
1134 | |
1135 | /// Gets the type of this attribute. |
1136 | MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute); |
1137 | |
1138 | /// Gets the type id of the attribute. |
1139 | MLIR_CAPI_EXPORTED MlirTypeID mlirAttributeGetTypeID(MlirAttribute attribute); |
1140 | |
1141 | /// Gets the dialect of the attribute. |
1142 | MLIR_CAPI_EXPORTED MlirDialect mlirAttributeGetDialect(MlirAttribute attribute); |
1143 | |
1144 | /// Checks whether an attribute is null. |
1145 | static inline bool mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; } |
1146 | |
1147 | /// Checks if two attributes are equal. |
1148 | MLIR_CAPI_EXPORTED bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2); |
1149 | |
1150 | /// Prints an attribute by sending chunks of the string representation and |
1151 | /// forwarding `userData to `callback`. Note that the callback may be called |
1152 | /// several times with consecutive chunks of the string. |
1153 | MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr, |
1154 | MlirStringCallback callback, |
1155 | void *userData); |
1156 | |
1157 | /// Prints the attribute to the standard error stream. |
1158 | MLIR_CAPI_EXPORTED void mlirAttributeDump(MlirAttribute attr); |
1159 | |
1160 | /// Associates an attribute with the name. Takes ownership of neither. |
1161 | MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, |
1162 | MlirAttribute attr); |
1163 | |
1164 | //===----------------------------------------------------------------------===// |
1165 | // Identifier API. |
1166 | //===----------------------------------------------------------------------===// |
1167 | |
1168 | /// Gets an identifier with the given string value. |
1169 | MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context, |
1170 | MlirStringRef str); |
1171 | |
1172 | /// Returns the context associated with this identifier |
1173 | MLIR_CAPI_EXPORTED MlirContext mlirIdentifierGetContext(MlirIdentifier); |
1174 | |
1175 | /// Checks whether two identifiers are the same. |
1176 | MLIR_CAPI_EXPORTED bool mlirIdentifierEqual(MlirIdentifier ident, |
1177 | MlirIdentifier other); |
1178 | |
1179 | /// Gets the string value of the identifier. |
1180 | MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident); |
1181 | |
1182 | //===----------------------------------------------------------------------===// |
1183 | // Symbol and SymbolTable API. |
1184 | //===----------------------------------------------------------------------===// |
1185 | |
1186 | /// Returns the name of the attribute used to store symbol names compatible with |
1187 | /// symbol tables. |
1188 | MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolTableGetSymbolAttributeName(void); |
1189 | |
1190 | /// Returns the name of the attribute used to store symbol visibility. |
1191 | MLIR_CAPI_EXPORTED MlirStringRef |
1192 | mlirSymbolTableGetVisibilityAttributeName(void); |
1193 | |
1194 | /// Creates a symbol table for the given operation. If the operation does not |
1195 | /// have the SymbolTable trait, returns a null symbol table. |
1196 | MLIR_CAPI_EXPORTED MlirSymbolTable |
1197 | mlirSymbolTableCreate(MlirOperation operation); |
1198 | |
1199 | /// Returns true if the symbol table is null. |
1200 | static inline bool mlirSymbolTableIsNull(MlirSymbolTable symbolTable) { |
1201 | return !symbolTable.ptr; |
1202 | } |
1203 | |
1204 | /// Destroys the symbol table created with mlirSymbolTableCreate. This does not |
1205 | /// affect the operations in the table. |
1206 | MLIR_CAPI_EXPORTED void mlirSymbolTableDestroy(MlirSymbolTable symbolTable); |
1207 | |
1208 | /// Looks up a symbol with the given name in the given symbol table and returns |
1209 | /// the operation that corresponds to the symbol. If the symbol cannot be found, |
1210 | /// returns a null operation. |
1211 | MLIR_CAPI_EXPORTED MlirOperation |
1212 | mlirSymbolTableLookup(MlirSymbolTable symbolTable, MlirStringRef name); |
1213 | |
1214 | /// Inserts the given operation into the given symbol table. The operation must |
1215 | /// have the symbol trait. If the symbol table already has a symbol with the |
1216 | /// same name, renames the symbol being inserted to ensure name uniqueness. Note |
1217 | /// that this does not move the operation itself into the block of the symbol |
1218 | /// table operation, this should be done separately. Returns the name of the |
1219 | /// symbol after insertion. |
1220 | MLIR_CAPI_EXPORTED MlirAttribute |
1221 | mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation); |
1222 | |
1223 | /// Removes the given operation from the symbol table and erases it. |
1224 | MLIR_CAPI_EXPORTED void mlirSymbolTableErase(MlirSymbolTable symbolTable, |
1225 | MlirOperation operation); |
1226 | |
1227 | /// Attempt to replace all uses that are nested within the given operation |
1228 | /// of the given symbol 'oldSymbol' with the provided 'newSymbol'. This does |
1229 | /// not traverse into nested symbol tables. Will fail atomically if there are |
1230 | /// any unknown operations that may be potential symbol tables. |
1231 | MLIR_CAPI_EXPORTED MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses( |
1232 | MlirStringRef oldSymbol, MlirStringRef newSymbol, MlirOperation from); |
1233 | |
1234 | /// Walks all symbol table operations nested within, and including, `op`. For |
1235 | /// each symbol table operation, the provided callback is invoked with the op |
1236 | /// and a boolean signifying if the symbols within that symbol table can be |
1237 | /// treated as if all uses within the IR are visible to the caller. |
1238 | /// `allSymUsesVisible` identifies whether all of the symbol uses of symbols |
1239 | /// within `op` are visible. |
1240 | MLIR_CAPI_EXPORTED void mlirSymbolTableWalkSymbolTables( |
1241 | MlirOperation from, bool allSymUsesVisible, |
1242 | void (*callback)(MlirOperation, bool, void *userData), void *userData); |
1243 | |
1244 | #ifdef __cplusplus |
1245 | } |
1246 | #endif |
1247 | |
1248 | #endif // MLIR_C_IR_H |
1249 | |