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 | //===----------------------------------------------------------------------===// |
166 | // Dialect API. |
167 | //===----------------------------------------------------------------------===// |
168 | |
169 | /// Returns the context that owns the dialect. |
170 | MLIR_CAPI_EXPORTED MlirContext mlirDialectGetContext(MlirDialect dialect); |
171 | |
172 | /// Checks if the dialect is null. |
173 | static inline bool mlirDialectIsNull(MlirDialect dialect) { |
174 | return !dialect.ptr; |
175 | } |
176 | |
177 | /// Checks if two dialects that belong to the same context are equal. Dialects |
178 | /// from different contexts will not compare equal. |
179 | MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1, |
180 | MlirDialect dialect2); |
181 | |
182 | /// Returns the namespace of the given dialect. |
183 | MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect); |
184 | |
185 | //===----------------------------------------------------------------------===// |
186 | // DialectHandle API. |
187 | // Registration entry-points for each dialect are declared using the common |
188 | // MLIR_DECLARE_DIALECT_REGISTRATION_CAPI macro, which takes the dialect |
189 | // API name (i.e. "Func", "Tensor", "Linalg") and namespace (i.e. "func", |
190 | // "tensor", "linalg"). The following declarations are produced: |
191 | // |
192 | // /// Gets the above hook methods in struct form for a dialect by namespace. |
193 | // /// This is intended to facilitate dynamic lookup and registration of |
194 | // /// dialects via a plugin facility based on shared library symbol lookup. |
195 | // const MlirDialectHandle *mlirGetDialectHandle__{NAMESPACE}__(); |
196 | // |
197 | // This is done via a common macro to facilitate future expansion to |
198 | // registration schemes. |
199 | //===----------------------------------------------------------------------===// |
200 | |
201 | struct MlirDialectHandle { |
202 | const void *ptr; |
203 | }; |
204 | typedef struct MlirDialectHandle MlirDialectHandle; |
205 | |
206 | #define MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Name, Namespace) \ |
207 | MLIR_CAPI_EXPORTED MlirDialectHandle mlirGetDialectHandle__##Namespace##__( \ |
208 | void) |
209 | |
210 | /// Returns the namespace associated with the provided dialect handle. |
211 | MLIR_CAPI_EXPORTED |
212 | MlirStringRef mlirDialectHandleGetNamespace(MlirDialectHandle); |
213 | |
214 | /// Inserts the dialect associated with the provided dialect handle into the |
215 | /// provided dialect registry |
216 | MLIR_CAPI_EXPORTED void mlirDialectHandleInsertDialect(MlirDialectHandle, |
217 | MlirDialectRegistry); |
218 | |
219 | /// Registers the dialect associated with the provided dialect handle. |
220 | MLIR_CAPI_EXPORTED void mlirDialectHandleRegisterDialect(MlirDialectHandle, |
221 | MlirContext); |
222 | |
223 | /// Loads the dialect associated with the provided dialect handle. |
224 | MLIR_CAPI_EXPORTED MlirDialect mlirDialectHandleLoadDialect(MlirDialectHandle, |
225 | MlirContext); |
226 | |
227 | //===----------------------------------------------------------------------===// |
228 | // DialectRegistry API. |
229 | //===----------------------------------------------------------------------===// |
230 | |
231 | /// Creates a dialect registry and transfers its ownership to the caller. |
232 | MLIR_CAPI_EXPORTED MlirDialectRegistry mlirDialectRegistryCreate(void); |
233 | |
234 | /// Checks if the dialect registry is null. |
235 | static inline bool mlirDialectRegistryIsNull(MlirDialectRegistry registry) { |
236 | return !registry.ptr; |
237 | } |
238 | |
239 | /// Takes a dialect registry owned by the caller and destroys it. |
240 | MLIR_CAPI_EXPORTED void |
241 | mlirDialectRegistryDestroy(MlirDialectRegistry registry); |
242 | |
243 | //===----------------------------------------------------------------------===// |
244 | // Location API. |
245 | //===----------------------------------------------------------------------===// |
246 | |
247 | /// Returns the underlying location attribute of this location. |
248 | MLIR_CAPI_EXPORTED MlirAttribute |
249 | mlirLocationGetAttribute(MlirLocation location); |
250 | |
251 | /// Creates a location from a location attribute. |
252 | MLIR_CAPI_EXPORTED MlirLocation |
253 | mlirLocationFromAttribute(MlirAttribute attribute); |
254 | |
255 | /// Creates an File/Line/Column location owned by the given context. |
256 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet( |
257 | MlirContext context, MlirStringRef filename, unsigned line, unsigned col); |
258 | |
259 | /// Creates a call site location with a callee and a caller. |
260 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee, |
261 | MlirLocation caller); |
262 | |
263 | /// Creates a fused location with an array of locations and metadata. |
264 | MLIR_CAPI_EXPORTED MlirLocation |
265 | mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, |
266 | MlirLocation const *locations, MlirAttribute metadata); |
267 | |
268 | /// Creates a name location owned by the given context. Providing null location |
269 | /// for childLoc is allowed and if childLoc is null location, then the behavior |
270 | /// is the same as having unknown child location. |
271 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context, |
272 | MlirStringRef name, |
273 | MlirLocation childLoc); |
274 | |
275 | /// Creates a location with unknown position owned by the given context. |
276 | MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context); |
277 | |
278 | /// Gets the context that a location was created with. |
279 | MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location); |
280 | |
281 | /// Checks if the location is null. |
282 | static inline bool mlirLocationIsNull(MlirLocation location) { |
283 | return !location.ptr; |
284 | } |
285 | |
286 | /// Checks if two locations are equal. |
287 | MLIR_CAPI_EXPORTED bool mlirLocationEqual(MlirLocation l1, MlirLocation l2); |
288 | |
289 | /// Prints a location by sending chunks of the string representation and |
290 | /// forwarding `userData to `callback`. Note that the callback may be called |
291 | /// several times with consecutive chunks of the string. |
292 | MLIR_CAPI_EXPORTED void mlirLocationPrint(MlirLocation location, |
293 | MlirStringCallback callback, |
294 | void *userData); |
295 | |
296 | //===----------------------------------------------------------------------===// |
297 | // Module API. |
298 | //===----------------------------------------------------------------------===// |
299 | |
300 | /// Creates a new, empty module and transfers ownership to the caller. |
301 | MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateEmpty(MlirLocation location); |
302 | |
303 | /// Parses a module from the string and transfers ownership to the caller. |
304 | MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateParse(MlirContext context, |
305 | MlirStringRef module); |
306 | |
307 | /// Gets the context that a module was created with. |
308 | MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext(MlirModule module); |
309 | |
310 | /// Gets the body of the module, i.e. the only block it contains. |
311 | MLIR_CAPI_EXPORTED MlirBlock mlirModuleGetBody(MlirModule module); |
312 | |
313 | /// Checks whether a module is null. |
314 | static inline bool mlirModuleIsNull(MlirModule module) { return !module.ptr; } |
315 | |
316 | /// Takes a module owned by the caller and deletes it. |
317 | MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module); |
318 | |
319 | /// Views the module as a generic operation. |
320 | MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module); |
321 | |
322 | /// Views the generic operation as a module. |
323 | /// The returned module is null when the input operation was not a ModuleOp. |
324 | MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op); |
325 | |
326 | //===----------------------------------------------------------------------===// |
327 | // Operation state. |
328 | //===----------------------------------------------------------------------===// |
329 | |
330 | /// An auxiliary class for constructing operations. |
331 | /// |
332 | /// This class contains all the information necessary to construct the |
333 | /// operation. It owns the MlirRegions it has pointers to and does not own |
334 | /// anything else. By default, the state can be constructed from a name and |
335 | /// location, the latter being also used to access the context, and has no other |
336 | /// components. These components can be added progressively until the operation |
337 | /// is constructed. Users are not expected to rely on the internals of this |
338 | /// class and should use mlirOperationState* functions instead. |
339 | |
340 | struct MlirOperationState { |
341 | MlirStringRef name; |
342 | MlirLocation location; |
343 | intptr_t nResults; |
344 | MlirType *results; |
345 | intptr_t nOperands; |
346 | MlirValue *operands; |
347 | intptr_t nRegions; |
348 | MlirRegion *regions; |
349 | intptr_t nSuccessors; |
350 | MlirBlock *successors; |
351 | intptr_t nAttributes; |
352 | MlirNamedAttribute *attributes; |
353 | bool enableResultTypeInference; |
354 | }; |
355 | typedef struct MlirOperationState MlirOperationState; |
356 | |
357 | /// Constructs an operation state from a name and a location. |
358 | MLIR_CAPI_EXPORTED MlirOperationState mlirOperationStateGet(MlirStringRef name, |
359 | MlirLocation loc); |
360 | |
361 | /// Adds a list of components to the operation state. |
362 | MLIR_CAPI_EXPORTED void mlirOperationStateAddResults(MlirOperationState *state, |
363 | intptr_t n, |
364 | MlirType const *results); |
365 | MLIR_CAPI_EXPORTED void |
366 | mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, |
367 | MlirValue const *operands); |
368 | MLIR_CAPI_EXPORTED void |
369 | mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, |
370 | MlirRegion const *regions); |
371 | MLIR_CAPI_EXPORTED void |
372 | mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, |
373 | MlirBlock const *successors); |
374 | MLIR_CAPI_EXPORTED void |
375 | mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, |
376 | MlirNamedAttribute const *attributes); |
377 | |
378 | /// Enables result type inference for the operation under construction. If |
379 | /// enabled, then the caller must not have called |
380 | /// mlirOperationStateAddResults(). Note that if enabled, the |
381 | /// mlirOperationCreate() call is failable: it will return a null operation |
382 | /// on inference failure and will emit diagnostics. |
383 | MLIR_CAPI_EXPORTED void |
384 | mlirOperationStateEnableResultTypeInference(MlirOperationState *state); |
385 | |
386 | //===----------------------------------------------------------------------===// |
387 | // AsmState API. |
388 | // While many of these are simple settings that could be represented in a |
389 | // struct, they are wrapped in a heap allocated object and accessed via |
390 | // functions to maximize the possibility of compatibility over time. |
391 | //===----------------------------------------------------------------------===// |
392 | |
393 | /// Creates new AsmState, as with AsmState the IR should not be mutated |
394 | /// in-between using this state. |
395 | /// Must be freed with a call to mlirAsmStateDestroy(). |
396 | // TODO: This should be expanded to handle location & resouce map. |
397 | MLIR_CAPI_EXPORTED MlirAsmState |
398 | mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags); |
399 | |
400 | /// Creates new AsmState from value. |
401 | /// Must be freed with a call to mlirAsmStateDestroy(). |
402 | // TODO: This should be expanded to handle location & resouce map. |
403 | MLIR_CAPI_EXPORTED MlirAsmState |
404 | mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags); |
405 | |
406 | /// Destroys printing flags created with mlirAsmStateCreate. |
407 | MLIR_CAPI_EXPORTED void mlirAsmStateDestroy(MlirAsmState state); |
408 | |
409 | //===----------------------------------------------------------------------===// |
410 | // Op Printing flags API. |
411 | // While many of these are simple settings that could be represented in a |
412 | // struct, they are wrapped in a heap allocated object and accessed via |
413 | // functions to maximize the possibility of compatibility over time. |
414 | //===----------------------------------------------------------------------===// |
415 | |
416 | /// Creates new printing flags with defaults, intended for customization. |
417 | /// Must be freed with a call to mlirOpPrintingFlagsDestroy(). |
418 | MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void); |
419 | |
420 | /// Destroys printing flags created with mlirOpPrintingFlagsCreate. |
421 | MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags); |
422 | |
423 | /// Enables the elision of large elements attributes by printing a lexically |
424 | /// valid but otherwise meaningless form instead of the element data. The |
425 | /// `largeElementLimit` is used to configure what is considered to be a "large" |
426 | /// ElementsAttr by providing an upper limit to the number of elements. |
427 | MLIR_CAPI_EXPORTED void |
428 | mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, |
429 | intptr_t largeElementLimit); |
430 | |
431 | /// Enable or disable printing of debug information (based on `enable`). If |
432 | /// 'prettyForm' is set to true, debug information is printed in a more readable |
433 | /// 'pretty' form. Note: The IR generated with 'prettyForm' is not parsable. |
434 | MLIR_CAPI_EXPORTED void |
435 | mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, |
436 | bool prettyForm); |
437 | |
438 | /// Always print operations in the generic form. |
439 | MLIR_CAPI_EXPORTED void |
440 | mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags); |
441 | |
442 | /// Use local scope when printing the operation. This allows for using the |
443 | /// printer in a more localized and thread-safe setting, but may not |
444 | /// necessarily be identical to what the IR will look like when dumping |
445 | /// the full module. |
446 | MLIR_CAPI_EXPORTED void |
447 | mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags); |
448 | |
449 | /// Do not verify the operation when using custom operation printers. |
450 | MLIR_CAPI_EXPORTED void |
451 | mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags); |
452 | |
453 | //===----------------------------------------------------------------------===// |
454 | // Bytecode printing flags API. |
455 | //===----------------------------------------------------------------------===// |
456 | |
457 | /// Creates new printing flags with defaults, intended for customization. |
458 | /// Must be freed with a call to mlirBytecodeWriterConfigDestroy(). |
459 | MLIR_CAPI_EXPORTED MlirBytecodeWriterConfig |
460 | mlirBytecodeWriterConfigCreate(void); |
461 | |
462 | /// Destroys printing flags created with mlirBytecodeWriterConfigCreate. |
463 | MLIR_CAPI_EXPORTED void |
464 | mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config); |
465 | |
466 | /// Sets the version to emit in the writer config. |
467 | MLIR_CAPI_EXPORTED void |
468 | mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, |
469 | int64_t version); |
470 | |
471 | //===----------------------------------------------------------------------===// |
472 | // Operation API. |
473 | //===----------------------------------------------------------------------===// |
474 | |
475 | /// Creates an operation and transfers ownership to the caller. |
476 | /// Note that caller owned child objects are transferred in this call and must |
477 | /// not be further used. Particularly, this applies to any regions added to |
478 | /// the state (the implementation may invalidate any such pointers). |
479 | /// |
480 | /// This call can fail under the following conditions, in which case, it will |
481 | /// return a null operation and emit diagnostics: |
482 | /// - Result type inference is enabled and cannot be performed. |
483 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreate(MlirOperationState *state); |
484 | |
485 | /// Parses an operation, giving ownership to the caller. If parsing fails a null |
486 | /// operation will be returned, and an error diagnostic emitted. |
487 | /// |
488 | /// `sourceStr` may be either the text assembly format, or binary bytecode |
489 | /// format. `sourceName` is used as the file name of the source; any IR without |
490 | /// locations will get a `FileLineColLoc` location with `sourceName` as the file |
491 | /// name. |
492 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreateParse( |
493 | MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName); |
494 | |
495 | /// Creates a deep copy of an operation. The operation is not inserted and |
496 | /// ownership is transferred to the caller. |
497 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationClone(MlirOperation op); |
498 | |
499 | /// Takes an operation owned by the caller and destroys it. |
500 | MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op); |
501 | |
502 | /// Removes the given operation from its parent block. The operation is not |
503 | /// destroyed. The ownership of the operation is transferred to the caller. |
504 | MLIR_CAPI_EXPORTED void mlirOperationRemoveFromParent(MlirOperation op); |
505 | |
506 | /// Checks whether the underlying operation is null. |
507 | static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; } |
508 | |
509 | /// Checks whether two operation handles point to the same operation. This does |
510 | /// not perform deep comparison. |
511 | MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op, |
512 | MlirOperation other); |
513 | |
514 | /// Gets the context this operation is associated with |
515 | MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op); |
516 | |
517 | /// Gets the location of the operation. |
518 | MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op); |
519 | |
520 | /// Gets the type id of the operation. |
521 | /// Returns null if the operation does not have a registered operation |
522 | /// description. |
523 | MLIR_CAPI_EXPORTED MlirTypeID mlirOperationGetTypeID(MlirOperation op); |
524 | |
525 | /// Gets the name of the operation as an identifier. |
526 | MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op); |
527 | |
528 | /// Gets the block that owns this operation, returning null if the operation is |
529 | /// not owned. |
530 | MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetBlock(MlirOperation op); |
531 | |
532 | /// Gets the operation that owns this operation, returning null if the operation |
533 | /// is not owned. |
534 | MLIR_CAPI_EXPORTED MlirOperation |
535 | mlirOperationGetParentOperation(MlirOperation op); |
536 | |
537 | /// Returns the number of regions attached to the given operation. |
538 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumRegions(MlirOperation op); |
539 | |
540 | /// Returns `pos`-th region attached to the operation. |
541 | MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetRegion(MlirOperation op, |
542 | intptr_t pos); |
543 | |
544 | /// Returns an operation immediately following the given operation it its |
545 | /// enclosing block. |
546 | MLIR_CAPI_EXPORTED MlirOperation mlirOperationGetNextInBlock(MlirOperation op); |
547 | |
548 | /// Returns the number of operands of the operation. |
549 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumOperands(MlirOperation op); |
550 | |
551 | /// Returns `pos`-th operand of the operation. |
552 | MLIR_CAPI_EXPORTED MlirValue mlirOperationGetOperand(MlirOperation op, |
553 | intptr_t pos); |
554 | |
555 | /// Sets the `pos`-th operand of the operation. |
556 | MLIR_CAPI_EXPORTED void mlirOperationSetOperand(MlirOperation op, intptr_t pos, |
557 | MlirValue newValue); |
558 | |
559 | /// Replaces the operands of the operation. |
560 | MLIR_CAPI_EXPORTED void mlirOperationSetOperands(MlirOperation op, |
561 | intptr_t nOperands, |
562 | MlirValue const *operands); |
563 | |
564 | /// Returns the number of results of the operation. |
565 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumResults(MlirOperation op); |
566 | |
567 | /// Returns `pos`-th result of the operation. |
568 | MLIR_CAPI_EXPORTED MlirValue mlirOperationGetResult(MlirOperation op, |
569 | intptr_t pos); |
570 | |
571 | /// Returns the number of successor blocks of the operation. |
572 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumSuccessors(MlirOperation op); |
573 | |
574 | /// Returns `pos`-th successor of the operation. |
575 | MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetSuccessor(MlirOperation op, |
576 | intptr_t pos); |
577 | |
578 | /// Set `pos`-th successor of the operation. |
579 | MLIR_CAPI_EXPORTED void |
580 | mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block); |
581 | |
582 | /// Returns true if this operation defines an inherent attribute with this name. |
583 | /// Note: the attribute can be optional, so |
584 | /// `mlirOperationGetInherentAttributeByName` can still return a null attribute. |
585 | MLIR_CAPI_EXPORTED bool |
586 | mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name); |
587 | |
588 | /// Returns an inherent attribute attached to the operation given its name. |
589 | MLIR_CAPI_EXPORTED MlirAttribute |
590 | mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name); |
591 | |
592 | /// Sets an inherent attribute by name, replacing the existing if it exists. |
593 | /// This has no effect if "name" does not match an inherent attribute. |
594 | MLIR_CAPI_EXPORTED void |
595 | mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name, |
596 | MlirAttribute attr); |
597 | |
598 | /// Returns the number of discardable attributes attached to the operation. |
599 | MLIR_CAPI_EXPORTED intptr_t |
600 | mlirOperationGetNumDiscardableAttributes(MlirOperation op); |
601 | |
602 | /// Return `pos`-th discardable attribute of the operation. |
603 | MLIR_CAPI_EXPORTED MlirNamedAttribute |
604 | mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos); |
605 | |
606 | /// Returns a discardable attribute attached to the operation given its name. |
607 | MLIR_CAPI_EXPORTED MlirAttribute mlirOperationGetDiscardableAttributeByName( |
608 | MlirOperation op, MlirStringRef name); |
609 | |
610 | /// Sets a discardable attribute by name, replacing the existing if it exists or |
611 | /// adding a new one otherwise. The new `attr` Attribute is not allowed to be |
612 | /// null, use `mlirOperationRemoveDiscardableAttributeByName` to remove an |
613 | /// Attribute instead. |
614 | MLIR_CAPI_EXPORTED void |
615 | mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name, |
616 | MlirAttribute attr); |
617 | |
618 | /// Removes a discardable attribute by name. Returns false if the attribute was |
619 | /// not found and true if removed. |
620 | MLIR_CAPI_EXPORTED bool |
621 | mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, |
622 | MlirStringRef name); |
623 | |
624 | /// Returns the number of attributes attached to the operation. |
625 | /// Deprecated, please use `mlirOperationGetNumInherentAttributes` or |
626 | /// `mlirOperationGetNumDiscardableAttributes`. |
627 | MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumAttributes(MlirOperation op); |
628 | |
629 | /// Return `pos`-th attribute of the operation. |
630 | /// Deprecated, please use `mlirOperationGetInherentAttribute` or |
631 | /// `mlirOperationGetDiscardableAttribute`. |
632 | MLIR_CAPI_EXPORTED MlirNamedAttribute |
633 | mlirOperationGetAttribute(MlirOperation op, intptr_t pos); |
634 | |
635 | /// Returns an attribute attached to the operation given its name. |
636 | /// Deprecated, please use `mlirOperationGetInherentAttributeByName` or |
637 | /// `mlirOperationGetDiscardableAttributeByName`. |
638 | MLIR_CAPI_EXPORTED MlirAttribute |
639 | mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name); |
640 | |
641 | /// Sets an attribute by name, replacing the existing if it exists or |
642 | /// adding a new one otherwise. |
643 | /// Deprecated, please use `mlirOperationSetInherentAttributeByName` or |
644 | /// `mlirOperationSetDiscardableAttributeByName`. |
645 | MLIR_CAPI_EXPORTED void mlirOperationSetAttributeByName(MlirOperation op, |
646 | MlirStringRef name, |
647 | MlirAttribute attr); |
648 | |
649 | /// Removes an attribute by name. Returns false if the attribute was not found |
650 | /// and true if removed. |
651 | /// Deprecated, please use `mlirOperationRemoveInherentAttributeByName` or |
652 | /// `mlirOperationRemoveDiscardableAttributeByName`. |
653 | MLIR_CAPI_EXPORTED bool mlirOperationRemoveAttributeByName(MlirOperation op, |
654 | MlirStringRef name); |
655 | |
656 | /// Prints an operation by sending chunks of the string representation and |
657 | /// forwarding `userData to `callback`. Note that the callback may be called |
658 | /// several times with consecutive chunks of the string. |
659 | MLIR_CAPI_EXPORTED void mlirOperationPrint(MlirOperation op, |
660 | MlirStringCallback callback, |
661 | void *userData); |
662 | |
663 | /// Same as mlirOperationPrint but accepts flags controlling the printing |
664 | /// behavior. |
665 | MLIR_CAPI_EXPORTED void mlirOperationPrintWithFlags(MlirOperation op, |
666 | MlirOpPrintingFlags flags, |
667 | MlirStringCallback callback, |
668 | void *userData); |
669 | |
670 | /// Same as mlirOperationPrint but accepts AsmState controlling the printing |
671 | /// behavior as well as caching computed names. |
672 | MLIR_CAPI_EXPORTED void mlirOperationPrintWithState(MlirOperation op, |
673 | MlirAsmState state, |
674 | MlirStringCallback callback, |
675 | void *userData); |
676 | |
677 | /// Same as mlirOperationPrint but writing the bytecode format. |
678 | MLIR_CAPI_EXPORTED void mlirOperationWriteBytecode(MlirOperation op, |
679 | MlirStringCallback callback, |
680 | void *userData); |
681 | |
682 | /// Same as mlirOperationWriteBytecode but with writer config and returns |
683 | /// failure only if desired bytecode could not be honored. |
684 | MLIR_CAPI_EXPORTED MlirLogicalResult mlirOperationWriteBytecodeWithConfig( |
685 | MlirOperation op, MlirBytecodeWriterConfig config, |
686 | MlirStringCallback callback, void *userData); |
687 | |
688 | /// Prints an operation to stderr. |
689 | MLIR_CAPI_EXPORTED void mlirOperationDump(MlirOperation op); |
690 | |
691 | /// Verify the operation and return true if it passes, false if it fails. |
692 | MLIR_CAPI_EXPORTED bool mlirOperationVerify(MlirOperation op); |
693 | |
694 | /// Moves the given operation immediately after the other operation in its |
695 | /// parent block. The given operation may be owned by the caller or by its |
696 | /// current block. The other operation must belong to a block. In any case, the |
697 | /// ownership is transferred to the block of the other operation. |
698 | MLIR_CAPI_EXPORTED void mlirOperationMoveAfter(MlirOperation op, |
699 | MlirOperation other); |
700 | |
701 | /// Moves the given operation immediately before the other operation in its |
702 | /// parent block. The given operation may be owner by the caller or by its |
703 | /// current block. The other operation must belong to a block. In any case, the |
704 | /// ownership is transferred to the block of the other operation. |
705 | MLIR_CAPI_EXPORTED void mlirOperationMoveBefore(MlirOperation op, |
706 | MlirOperation other); |
707 | |
708 | /// Traversal order for operation walk. |
709 | typedef enum MlirWalkOrder { |
710 | MlirWalkPreOrder, |
711 | MlirWalkPostOrder |
712 | } MlirWalkOrder; |
713 | |
714 | /// Operation walker type. The handler is passed an (opaque) reference to an |
715 | /// operation and a pointer to a `userData`. |
716 | typedef void (*MlirOperationWalkCallback)(MlirOperation, void *userData); |
717 | |
718 | /// Walks operation `op` in `walkOrder` and calls `callback` on that operation. |
719 | /// `*userData` is passed to the callback as well and can be used to tunnel some |
720 | /// context or other data into the callback. |
721 | MLIR_CAPI_EXPORTED |
722 | void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, |
723 | void *userData, MlirWalkOrder walkOrder); |
724 | |
725 | //===----------------------------------------------------------------------===// |
726 | // Region API. |
727 | //===----------------------------------------------------------------------===// |
728 | |
729 | /// Creates a new empty region and transfers ownership to the caller. |
730 | MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate(void); |
731 | |
732 | /// Takes a region owned by the caller and destroys it. |
733 | MLIR_CAPI_EXPORTED void mlirRegionDestroy(MlirRegion region); |
734 | |
735 | /// Checks whether a region is null. |
736 | static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; } |
737 | |
738 | /// Checks whether two region handles point to the same region. This does not |
739 | /// perform deep comparison. |
740 | MLIR_CAPI_EXPORTED bool mlirRegionEqual(MlirRegion region, MlirRegion other); |
741 | |
742 | /// Gets the first block in the region. |
743 | MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region); |
744 | |
745 | /// Takes a block owned by the caller and appends it to the given region. |
746 | MLIR_CAPI_EXPORTED void mlirRegionAppendOwnedBlock(MlirRegion region, |
747 | MlirBlock block); |
748 | |
749 | /// Takes a block owned by the caller and inserts it at `pos` to the given |
750 | /// region. This is an expensive operation that linearly scans the region, |
751 | /// prefer insertAfter/Before instead. |
752 | MLIR_CAPI_EXPORTED void |
753 | mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block); |
754 | |
755 | /// Takes a block owned by the caller and inserts it after the (non-owned) |
756 | /// reference block in the given region. The reference block must belong to the |
757 | /// region. If the reference block is null, prepends the block to the region. |
758 | MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockAfter(MlirRegion region, |
759 | MlirBlock reference, |
760 | MlirBlock block); |
761 | |
762 | /// Takes a block owned by the caller and inserts it before the (non-owned) |
763 | /// reference block in the given region. The reference block must belong to the |
764 | /// region. If the reference block is null, appends the block to the region. |
765 | MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockBefore(MlirRegion region, |
766 | MlirBlock reference, |
767 | MlirBlock block); |
768 | |
769 | /// Returns first region attached to the operation. |
770 | MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetFirstRegion(MlirOperation op); |
771 | |
772 | /// Returns the region immediately following the given region in its parent |
773 | /// operation. |
774 | MLIR_CAPI_EXPORTED MlirRegion mlirRegionGetNextInOperation(MlirRegion region); |
775 | |
776 | /// Moves the entire content of the source region to the target region. |
777 | MLIR_CAPI_EXPORTED void mlirRegionTakeBody(MlirRegion target, |
778 | MlirRegion source); |
779 | |
780 | //===----------------------------------------------------------------------===// |
781 | // Block API. |
782 | //===----------------------------------------------------------------------===// |
783 | |
784 | /// Creates a new empty block with the given argument types and transfers |
785 | /// ownership to the caller. |
786 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs, |
787 | MlirType const *args, |
788 | MlirLocation const *locs); |
789 | |
790 | /// Takes a block owned by the caller and destroys it. |
791 | MLIR_CAPI_EXPORTED void mlirBlockDestroy(MlirBlock block); |
792 | |
793 | /// Detach a block from the owning region and assume ownership. |
794 | MLIR_CAPI_EXPORTED void mlirBlockDetach(MlirBlock block); |
795 | |
796 | /// Checks whether a block is null. |
797 | static inline bool mlirBlockIsNull(MlirBlock block) { return !block.ptr; } |
798 | |
799 | /// Checks whether two blocks handles point to the same block. This does not |
800 | /// perform deep comparison. |
801 | MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other); |
802 | |
803 | /// Returns the closest surrounding operation that contains this block. |
804 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock); |
805 | |
806 | /// Returns the region that contains this block. |
807 | MLIR_CAPI_EXPORTED MlirRegion mlirBlockGetParentRegion(MlirBlock block); |
808 | |
809 | /// Returns the block immediately following the given block in its parent |
810 | /// region. |
811 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block); |
812 | |
813 | /// Returns the first operation in the block. |
814 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetFirstOperation(MlirBlock block); |
815 | |
816 | /// Returns the terminator operation in the block or null if no terminator. |
817 | MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetTerminator(MlirBlock block); |
818 | |
819 | /// Takes an operation owned by the caller and appends it to the block. |
820 | MLIR_CAPI_EXPORTED void mlirBlockAppendOwnedOperation(MlirBlock block, |
821 | MlirOperation operation); |
822 | |
823 | /// Takes an operation owned by the caller and inserts it as `pos` to the block. |
824 | /// This is an expensive operation that scans the block linearly, prefer |
825 | /// insertBefore/After instead. |
826 | MLIR_CAPI_EXPORTED void mlirBlockInsertOwnedOperation(MlirBlock block, |
827 | intptr_t pos, |
828 | MlirOperation operation); |
829 | |
830 | /// Takes an operation owned by the caller and inserts it after the (non-owned) |
831 | /// reference operation in the given block. If the reference is null, prepends |
832 | /// the operation. Otherwise, the reference must belong to the block. |
833 | MLIR_CAPI_EXPORTED void |
834 | mlirBlockInsertOwnedOperationAfter(MlirBlock block, MlirOperation reference, |
835 | MlirOperation operation); |
836 | |
837 | /// Takes an operation owned by the caller and inserts it before the (non-owned) |
838 | /// reference operation in the given block. If the reference is null, appends |
839 | /// the operation. Otherwise, the reference must belong to the block. |
840 | MLIR_CAPI_EXPORTED void |
841 | mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference, |
842 | MlirOperation operation); |
843 | |
844 | /// Returns the number of arguments of the block. |
845 | MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumArguments(MlirBlock block); |
846 | |
847 | /// Appends an argument of the specified type to the block. Returns the newly |
848 | /// added argument. |
849 | MLIR_CAPI_EXPORTED MlirValue mlirBlockAddArgument(MlirBlock block, |
850 | MlirType type, |
851 | MlirLocation loc); |
852 | |
853 | /// Inserts an argument of the specified type at a specified index to the block. |
854 | /// Returns the newly added argument. |
855 | MLIR_CAPI_EXPORTED MlirValue mlirBlockInsertArgument(MlirBlock block, |
856 | intptr_t pos, |
857 | MlirType type, |
858 | MlirLocation loc); |
859 | |
860 | /// Returns `pos`-th argument of the block. |
861 | MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block, |
862 | intptr_t pos); |
863 | |
864 | /// Prints a block by sending chunks of the string representation and |
865 | /// forwarding `userData to `callback`. Note that the callback may be called |
866 | /// several times with consecutive chunks of the string. |
867 | MLIR_CAPI_EXPORTED void |
868 | mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData); |
869 | |
870 | //===----------------------------------------------------------------------===// |
871 | // Value API. |
872 | //===----------------------------------------------------------------------===// |
873 | |
874 | /// Returns whether the value is null. |
875 | static inline bool mlirValueIsNull(MlirValue value) { return !value.ptr; } |
876 | |
877 | /// Returns 1 if two values are equal, 0 otherwise. |
878 | MLIR_CAPI_EXPORTED bool mlirValueEqual(MlirValue value1, MlirValue value2); |
879 | |
880 | /// Returns 1 if the value is a block argument, 0 otherwise. |
881 | MLIR_CAPI_EXPORTED bool mlirValueIsABlockArgument(MlirValue value); |
882 | |
883 | /// Returns 1 if the value is an operation result, 0 otherwise. |
884 | MLIR_CAPI_EXPORTED bool mlirValueIsAOpResult(MlirValue value); |
885 | |
886 | /// Returns the block in which this value is defined as an argument. Asserts if |
887 | /// the value is not a block argument. |
888 | MLIR_CAPI_EXPORTED MlirBlock mlirBlockArgumentGetOwner(MlirValue value); |
889 | |
890 | /// Returns the position of the value in the argument list of its block. |
891 | MLIR_CAPI_EXPORTED intptr_t mlirBlockArgumentGetArgNumber(MlirValue value); |
892 | |
893 | /// Sets the type of the block argument to the given type. |
894 | MLIR_CAPI_EXPORTED void mlirBlockArgumentSetType(MlirValue value, |
895 | MlirType type); |
896 | |
897 | /// Returns an operation that produced this value as its result. Asserts if the |
898 | /// value is not an op result. |
899 | MLIR_CAPI_EXPORTED MlirOperation mlirOpResultGetOwner(MlirValue value); |
900 | |
901 | /// Returns the position of the value in the list of results of the operation |
902 | /// that produced it. |
903 | MLIR_CAPI_EXPORTED intptr_t mlirOpResultGetResultNumber(MlirValue value); |
904 | |
905 | /// Returns the type of the value. |
906 | MLIR_CAPI_EXPORTED MlirType mlirValueGetType(MlirValue value); |
907 | |
908 | /// Set the type of the value. |
909 | MLIR_CAPI_EXPORTED void mlirValueSetType(MlirValue value, MlirType type); |
910 | |
911 | /// Prints the value to the standard error stream. |
912 | MLIR_CAPI_EXPORTED void mlirValueDump(MlirValue value); |
913 | |
914 | /// Prints a value by sending chunks of the string representation and |
915 | /// forwarding `userData to `callback`. Note that the callback may be called |
916 | /// several times with consecutive chunks of the string. |
917 | MLIR_CAPI_EXPORTED void |
918 | mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData); |
919 | |
920 | /// Prints a value as an operand (i.e., the ValueID). |
921 | MLIR_CAPI_EXPORTED void mlirValuePrintAsOperand(MlirValue value, |
922 | MlirAsmState state, |
923 | MlirStringCallback callback, |
924 | void *userData); |
925 | |
926 | /// Returns an op operand representing the first use of the value, or a null op |
927 | /// operand if there are no uses. |
928 | MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value); |
929 | |
930 | /// Replace all uses of 'of' value with the 'with' value, updating anything in |
931 | /// the IR that uses 'of' to use the other value instead. When this returns |
932 | /// there are zero uses of 'of'. |
933 | MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of, |
934 | MlirValue with); |
935 | |
936 | //===----------------------------------------------------------------------===// |
937 | // OpOperand API. |
938 | //===----------------------------------------------------------------------===// |
939 | |
940 | /// Returns whether the op operand is null. |
941 | MLIR_CAPI_EXPORTED bool mlirOpOperandIsNull(MlirOpOperand opOperand); |
942 | |
943 | /// Returns the value of an op operand. |
944 | MLIR_CAPI_EXPORTED MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand); |
945 | |
946 | /// Returns the owner operation of an op operand. |
947 | MLIR_CAPI_EXPORTED MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand); |
948 | |
949 | /// Returns the operand number of an op operand. |
950 | MLIR_CAPI_EXPORTED unsigned |
951 | mlirOpOperandGetOperandNumber(MlirOpOperand opOperand); |
952 | |
953 | /// Returns an op operand representing the next use of the value, or a null op |
954 | /// operand if there is no next use. |
955 | MLIR_CAPI_EXPORTED MlirOpOperand |
956 | mlirOpOperandGetNextUse(MlirOpOperand opOperand); |
957 | |
958 | //===----------------------------------------------------------------------===// |
959 | // Type API. |
960 | //===----------------------------------------------------------------------===// |
961 | |
962 | /// Parses a type. The type is owned by the context. |
963 | MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context, |
964 | MlirStringRef type); |
965 | |
966 | /// Gets the context that a type was created with. |
967 | MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type); |
968 | |
969 | /// Gets the type ID of the type. |
970 | MLIR_CAPI_EXPORTED MlirTypeID mlirTypeGetTypeID(MlirType type); |
971 | |
972 | /// Gets the dialect a type belongs to. |
973 | MLIR_CAPI_EXPORTED MlirDialect mlirTypeGetDialect(MlirType type); |
974 | |
975 | /// Checks whether a type is null. |
976 | static inline bool mlirTypeIsNull(MlirType type) { return !type.ptr; } |
977 | |
978 | /// Checks if two types are equal. |
979 | MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2); |
980 | |
981 | /// Prints a location by sending chunks of the string representation and |
982 | /// forwarding `userData to `callback`. Note that the callback may be called |
983 | /// several times with consecutive chunks of the string. |
984 | MLIR_CAPI_EXPORTED void |
985 | mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData); |
986 | |
987 | /// Prints the type to the standard error stream. |
988 | MLIR_CAPI_EXPORTED void mlirTypeDump(MlirType type); |
989 | |
990 | //===----------------------------------------------------------------------===// |
991 | // Attribute API. |
992 | //===----------------------------------------------------------------------===// |
993 | |
994 | /// Parses an attribute. The attribute is owned by the context. |
995 | MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeParseGet(MlirContext context, |
996 | MlirStringRef attr); |
997 | |
998 | /// Gets the context that an attribute was created with. |
999 | MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute); |
1000 | |
1001 | /// Gets the type of this attribute. |
1002 | MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute); |
1003 | |
1004 | /// Gets the type id of the attribute. |
1005 | MLIR_CAPI_EXPORTED MlirTypeID mlirAttributeGetTypeID(MlirAttribute attribute); |
1006 | |
1007 | /// Gets the dialect of the attribute. |
1008 | MLIR_CAPI_EXPORTED MlirDialect mlirAttributeGetDialect(MlirAttribute attribute); |
1009 | |
1010 | /// Checks whether an attribute is null. |
1011 | static inline bool mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; } |
1012 | |
1013 | /// Checks if two attributes are equal. |
1014 | MLIR_CAPI_EXPORTED bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2); |
1015 | |
1016 | /// Prints an attribute by sending chunks of the string representation and |
1017 | /// forwarding `userData to `callback`. Note that the callback may be called |
1018 | /// several times with consecutive chunks of the string. |
1019 | MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr, |
1020 | MlirStringCallback callback, |
1021 | void *userData); |
1022 | |
1023 | /// Prints the attribute to the standard error stream. |
1024 | MLIR_CAPI_EXPORTED void mlirAttributeDump(MlirAttribute attr); |
1025 | |
1026 | /// Associates an attribute with the name. Takes ownership of neither. |
1027 | MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, |
1028 | MlirAttribute attr); |
1029 | |
1030 | //===----------------------------------------------------------------------===// |
1031 | // Identifier API. |
1032 | //===----------------------------------------------------------------------===// |
1033 | |
1034 | /// Gets an identifier with the given string value. |
1035 | MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context, |
1036 | MlirStringRef str); |
1037 | |
1038 | /// Returns the context associated with this identifier |
1039 | MLIR_CAPI_EXPORTED MlirContext mlirIdentifierGetContext(MlirIdentifier); |
1040 | |
1041 | /// Checks whether two identifiers are the same. |
1042 | MLIR_CAPI_EXPORTED bool mlirIdentifierEqual(MlirIdentifier ident, |
1043 | MlirIdentifier other); |
1044 | |
1045 | /// Gets the string value of the identifier. |
1046 | MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident); |
1047 | |
1048 | //===----------------------------------------------------------------------===// |
1049 | // Symbol and SymbolTable API. |
1050 | //===----------------------------------------------------------------------===// |
1051 | |
1052 | /// Returns the name of the attribute used to store symbol names compatible with |
1053 | /// symbol tables. |
1054 | MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolTableGetSymbolAttributeName(void); |
1055 | |
1056 | /// Returns the name of the attribute used to store symbol visibility. |
1057 | MLIR_CAPI_EXPORTED MlirStringRef |
1058 | mlirSymbolTableGetVisibilityAttributeName(void); |
1059 | |
1060 | /// Creates a symbol table for the given operation. If the operation does not |
1061 | /// have the SymbolTable trait, returns a null symbol table. |
1062 | MLIR_CAPI_EXPORTED MlirSymbolTable |
1063 | mlirSymbolTableCreate(MlirOperation operation); |
1064 | |
1065 | /// Returns true if the symbol table is null. |
1066 | static inline bool mlirSymbolTableIsNull(MlirSymbolTable symbolTable) { |
1067 | return !symbolTable.ptr; |
1068 | } |
1069 | |
1070 | /// Destroys the symbol table created with mlirSymbolTableCreate. This does not |
1071 | /// affect the operations in the table. |
1072 | MLIR_CAPI_EXPORTED void mlirSymbolTableDestroy(MlirSymbolTable symbolTable); |
1073 | |
1074 | /// Looks up a symbol with the given name in the given symbol table and returns |
1075 | /// the operation that corresponds to the symbol. If the symbol cannot be found, |
1076 | /// returns a null operation. |
1077 | MLIR_CAPI_EXPORTED MlirOperation |
1078 | mlirSymbolTableLookup(MlirSymbolTable symbolTable, MlirStringRef name); |
1079 | |
1080 | /// Inserts the given operation into the given symbol table. The operation must |
1081 | /// have the symbol trait. If the symbol table already has a symbol with the |
1082 | /// same name, renames the symbol being inserted to ensure name uniqueness. Note |
1083 | /// that this does not move the operation itself into the block of the symbol |
1084 | /// table operation, this should be done separately. Returns the name of the |
1085 | /// symbol after insertion. |
1086 | MLIR_CAPI_EXPORTED MlirAttribute |
1087 | mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation); |
1088 | |
1089 | /// Removes the given operation from the symbol table and erases it. |
1090 | MLIR_CAPI_EXPORTED void mlirSymbolTableErase(MlirSymbolTable symbolTable, |
1091 | MlirOperation operation); |
1092 | |
1093 | /// Attempt to replace all uses that are nested within the given operation |
1094 | /// of the given symbol 'oldSymbol' with the provided 'newSymbol'. This does |
1095 | /// not traverse into nested symbol tables. Will fail atomically if there are |
1096 | /// any unknown operations that may be potential symbol tables. |
1097 | MLIR_CAPI_EXPORTED MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses( |
1098 | MlirStringRef oldSymbol, MlirStringRef newSymbol, MlirOperation from); |
1099 | |
1100 | /// Walks all symbol table operations nested within, and including, `op`. For |
1101 | /// each symbol table operation, the provided callback is invoked with the op |
1102 | /// and a boolean signifying if the symbols within that symbol table can be |
1103 | /// treated as if all uses within the IR are visible to the caller. |
1104 | /// `allSymUsesVisible` identifies whether all of the symbol uses of symbols |
1105 | /// within `op` are visible. |
1106 | MLIR_CAPI_EXPORTED void mlirSymbolTableWalkSymbolTables( |
1107 | MlirOperation from, bool allSymUsesVisible, |
1108 | void (*callback)(MlirOperation, bool, void *userData), void *userData); |
1109 | |
1110 | #ifdef __cplusplus |
1111 | } |
1112 | #endif |
1113 | |
1114 | #endif // MLIR_C_IR_H |
1115 | |