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 often
40 * only point to an IR fragment without owning it). The ownership semantics is
41 * 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(MlirContext, void);
52 DEFINE_C_API_STRUCT(MlirDialect, void);
53 DEFINE_C_API_STRUCT(MlirOperation, void);
54 DEFINE_C_API_STRUCT(MlirOpPrintingFlags, void);
55 DEFINE_C_API_STRUCT(MlirBlock, void);
56 DEFINE_C_API_STRUCT(MlirRegion, void);
57
58 DEFINE_C_API_STRUCT(MlirAttribute, const void);
59 DEFINE_C_API_STRUCT(MlirIdentifier, const void);
60 DEFINE_C_API_STRUCT(MlirLocation, const void);
61 DEFINE_C_API_STRUCT(MlirModule, const void);
62 DEFINE_C_API_STRUCT(MlirType, const void);
63 DEFINE_C_API_STRUCT(MlirValue, const void);
64
65 #undef DEFINE_C_API_STRUCT
66
67 /** Named MLIR attribute.
68 *
69 * A named attribute is essentially a (name, attribute) pair where the name is
70 * a string.
71 */
72 struct MlirNamedAttribute {
73 MlirStringRef name;
74 MlirAttribute attribute;
75 };
76 typedef struct MlirNamedAttribute MlirNamedAttribute;
77
78 //===----------------------------------------------------------------------===//
79 // Context API.
80 //===----------------------------------------------------------------------===//
81
82 /// Creates an MLIR context and transfers its ownership to the caller.
83 MLIR_CAPI_EXPORTED MlirContext mlirContextCreate();
84
85 /// Checks if two contexts are equal.
86 MLIR_CAPI_EXPORTED bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2);
87
88 /// Checks whether a context is null.
mlirContextIsNull(MlirContext context)89 static inline bool mlirContextIsNull(MlirContext context) {
90 return !context.ptr;
91 }
92
93 /// Takes an MLIR context owned by the caller and destroys it.
94 MLIR_CAPI_EXPORTED void mlirContextDestroy(MlirContext context);
95
96 /// Sets whether unregistered dialects are allowed in this context.
97 MLIR_CAPI_EXPORTED void
98 mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow);
99
100 /// Returns whether the context allows unregistered dialects.
101 MLIR_CAPI_EXPORTED bool
102 mlirContextGetAllowUnregisteredDialects(MlirContext context);
103
104 /** Returns the number of dialects registered with the given context. A
105 * registered dialect will be loaded if needed by the parser. */
106 MLIR_CAPI_EXPORTED intptr_t
107 mlirContextGetNumRegisteredDialects(MlirContext context);
108
109 /** Returns the number of dialects loaded by the context.
110 */
111 MLIR_CAPI_EXPORTED intptr_t
112 mlirContextGetNumLoadedDialects(MlirContext context);
113
114 /** Gets the dialect instance owned by the given context using the dialect
115 * namespace to identify it, loads (i.e., constructs the instance of) the
116 * dialect if necessary. If the dialect is not registered with the context,
117 * returns null. Use mlirContextLoad<Name>Dialect to load an unregistered
118 * dialect. */
119 MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
120 MlirStringRef name);
121
122 //===----------------------------------------------------------------------===//
123 // Dialect API.
124 //===----------------------------------------------------------------------===//
125
126 /// Returns the context that owns the dialect.
127 MLIR_CAPI_EXPORTED MlirContext mlirDialectGetContext(MlirDialect dialect);
128
129 /// Checks if the dialect is null.
mlirDialectIsNull(MlirDialect dialect)130 static inline bool mlirDialectIsNull(MlirDialect dialect) {
131 return !dialect.ptr;
132 }
133
134 /** Checks if two dialects that belong to the same context are equal. Dialects
135 * from different contexts will not compare equal. */
136 MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1,
137 MlirDialect dialect2);
138
139 /// Returns the namespace of the given dialect.
140 MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect);
141
142 //===----------------------------------------------------------------------===//
143 // Location API.
144 //===----------------------------------------------------------------------===//
145
146 /// Creates an File/Line/Column location owned by the given context.
147 MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(
148 MlirContext context, MlirStringRef filename, unsigned line, unsigned col);
149
150 /// Creates a location with unknown position owned by the given context.
151 MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);
152
153 /// Gets the context that a location was created with.
154 MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location);
155
156 /// Checks if the location is null.
mlirLocationIsNull(MlirLocation location)157 static inline bool mlirLocationIsNull(MlirLocation location) {
158 return !location.ptr;
159 }
160
161 /// Checks if two locations are equal.
162 MLIR_CAPI_EXPORTED bool mlirLocationEqual(MlirLocation l1, MlirLocation l2);
163
164 /** Prints a location by sending chunks of the string representation and
165 * forwarding `userData to `callback`. Note that the callback may be called
166 * several times with consecutive chunks of the string. */
167 MLIR_CAPI_EXPORTED void mlirLocationPrint(MlirLocation location,
168 MlirStringCallback callback,
169 void *userData);
170
171 //===----------------------------------------------------------------------===//
172 // Module API.
173 //===----------------------------------------------------------------------===//
174
175 /// Creates a new, empty module and transfers ownership to the caller.
176 MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateEmpty(MlirLocation location);
177
178 /// Parses a module from the string and transfers ownership to the caller.
179 MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateParse(MlirContext context,
180 MlirStringRef module);
181
182 /// Gets the context that a module was created with.
183 MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext(MlirModule module);
184
185 /// Gets the body of the module, i.e. the only block it contains.
186 MLIR_CAPI_EXPORTED MlirBlock mlirModuleGetBody(MlirModule module);
187
188 /// Checks whether a module is null.
mlirModuleIsNull(MlirModule module)189 static inline bool mlirModuleIsNull(MlirModule module) { return !module.ptr; }
190
191 /// Takes a module owned by the caller and deletes it.
192 MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module);
193
194 /// Views the module as a generic operation.
195 MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module);
196
197 //===----------------------------------------------------------------------===//
198 // Operation state.
199 //===----------------------------------------------------------------------===//
200
201 /** An auxiliary class for constructing operations.
202 *
203 * This class contains all the information necessary to construct the operation.
204 * It owns the MlirRegions it has pointers to and does not own anything else.
205 * By default, the state can be constructed from a name and location, the latter
206 * being also used to access the context, and has no other components. These
207 * components can be added progressively until the operation is constructed.
208 * Users are not expected to rely on the internals of this class and should use
209 * mlirOperationState* functions instead.
210 */
211 struct MlirOperationState {
212 MlirStringRef name;
213 MlirLocation location;
214 intptr_t nResults;
215 MlirType *results;
216 intptr_t nOperands;
217 MlirValue *operands;
218 intptr_t nRegions;
219 MlirRegion *regions;
220 intptr_t nSuccessors;
221 MlirBlock *successors;
222 intptr_t nAttributes;
223 MlirNamedAttribute *attributes;
224 };
225 typedef struct MlirOperationState MlirOperationState;
226
227 /// Constructs an operation state from a name and a location.
228 MLIR_CAPI_EXPORTED MlirOperationState mlirOperationStateGet(MlirStringRef name,
229 MlirLocation loc);
230
231 /// Adds a list of components to the operation state.
232 MLIR_CAPI_EXPORTED void mlirOperationStateAddResults(MlirOperationState *state,
233 intptr_t n,
234 MlirType const *results);
235 MLIR_CAPI_EXPORTED void
236 mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
237 MlirValue const *operands);
238 MLIR_CAPI_EXPORTED void
239 mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
240 MlirRegion const *regions);
241 MLIR_CAPI_EXPORTED void
242 mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
243 MlirBlock const *successors);
244 MLIR_CAPI_EXPORTED void
245 mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
246 MlirNamedAttribute const *attributes);
247
248 //===----------------------------------------------------------------------===//
249 // Op Printing flags API.
250 // While many of these are simple settings that could be represented in a
251 // struct, they are wrapped in a heap allocated object and accessed via
252 // functions to maximize the possibility of compatibility over time.
253 //===----------------------------------------------------------------------===//
254
255 /** Creates new printing flags with defaults, intended for customization.
256 * Must be freed with a call to mlirOpPrintingFlagsDestroy(). */
257 MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate();
258
259 /// Destroys printing flags created with mlirOpPrintingFlagsCreate.
260 MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags);
261
262 /** Enables the elision of large elements attributes by printing a lexically
263 * valid but otherwise meaningless form instead of the element data. The
264 * `largeElementLimit` is used to configure what is considered to be a "large"
265 * ElementsAttr by providing an upper limit to the number of elements. */
266 MLIR_CAPI_EXPORTED void
267 mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
268 intptr_t largeElementLimit);
269
270 /** Enable printing of debug information. If 'prettyForm' is set to true,
271 * debug information is printed in a more readable 'pretty' form. Note: The
272 * IR generated with 'prettyForm' is not parsable. */
273 MLIR_CAPI_EXPORTED void
274 mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool prettyForm);
275
276 /// Always print operations in the generic form.
277 MLIR_CAPI_EXPORTED void
278 mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags);
279
280 /** Use local scope when printing the operation. This allows for using the
281 * printer in a more localized and thread-safe setting, but may not
282 * necessarily be identical to what the IR will look like when dumping
283 * the full module. */
284 MLIR_CAPI_EXPORTED void
285 mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags);
286
287 //===----------------------------------------------------------------------===//
288 // Operation API.
289 //===----------------------------------------------------------------------===//
290
291 /// Creates an operation and transfers ownership to the caller.
292 MLIR_CAPI_EXPORTED MlirOperation
293 mlirOperationCreate(const MlirOperationState *state);
294
295 /// Takes an operation owned by the caller and destroys it.
296 MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op);
297
298 /// Checks whether the underlying operation is null.
mlirOperationIsNull(MlirOperation op)299 static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; }
300
301 /** Checks whether two operation handles point to the same operation. This does
302 * not perform deep comparison. */
303 MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op,
304 MlirOperation other);
305
306 /// Gets the name of the operation as an identifier.
307 MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op);
308
309 /** Gets the block that owns this operation, returning null if the operation is
310 * not owned. */
311 MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetBlock(MlirOperation op);
312
313 /** Gets the operation that owns this operation, returning null if the operation
314 * is not owned. */
315 MLIR_CAPI_EXPORTED MlirOperation
316 mlirOperationGetParentOperation(MlirOperation op);
317
318 /// Returns the number of regions attached to the given operation.
319 MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumRegions(MlirOperation op);
320
321 /// Returns `pos`-th region attached to the operation.
322 MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetRegion(MlirOperation op,
323 intptr_t pos);
324
325 /** Returns an operation immediately following the given operation it its
326 * enclosing block. */
327 MLIR_CAPI_EXPORTED MlirOperation mlirOperationGetNextInBlock(MlirOperation op);
328
329 /// Returns the number of operands of the operation.
330 MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumOperands(MlirOperation op);
331
332 /// Returns `pos`-th operand of the operation.
333 MLIR_CAPI_EXPORTED MlirValue mlirOperationGetOperand(MlirOperation op,
334 intptr_t pos);
335
336 /// Returns the number of results of the operation.
337 MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumResults(MlirOperation op);
338
339 /// Returns `pos`-th result of the operation.
340 MLIR_CAPI_EXPORTED MlirValue mlirOperationGetResult(MlirOperation op,
341 intptr_t pos);
342
343 /// Returns the number of successor blocks of the operation.
344 MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumSuccessors(MlirOperation op);
345
346 /// Returns `pos`-th successor of the operation.
347 MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetSuccessor(MlirOperation op,
348 intptr_t pos);
349
350 /// Returns the number of attributes attached to the operation.
351 MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumAttributes(MlirOperation op);
352
353 /// Return `pos`-th attribute of the operation.
354 MLIR_CAPI_EXPORTED MlirNamedAttribute
355 mlirOperationGetAttribute(MlirOperation op, intptr_t pos);
356
357 /// Returns an attribute attached to the operation given its name.
358 MLIR_CAPI_EXPORTED MlirAttribute
359 mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name);
360
361 /** Sets an attribute by name, replacing the existing if it exists or
362 * adding a new one otherwise. */
363 MLIR_CAPI_EXPORTED void mlirOperationSetAttributeByName(MlirOperation op,
364 MlirStringRef name,
365 MlirAttribute attr);
366
367 /** Removes an attribute by name. Returns false if the attribute was not found
368 * and true if removed. */
369 MLIR_CAPI_EXPORTED bool mlirOperationRemoveAttributeByName(MlirOperation op,
370 MlirStringRef name);
371
372 /** Prints an operation by sending chunks of the string representation and
373 * forwarding `userData to `callback`. Note that the callback may be called
374 * several times with consecutive chunks of the string. */
375 MLIR_CAPI_EXPORTED void mlirOperationPrint(MlirOperation op,
376 MlirStringCallback callback,
377 void *userData);
378
379 /** Same as mlirOperationPrint but accepts flags controlling the printing
380 * behavior. */
381 MLIR_CAPI_EXPORTED void mlirOperationPrintWithFlags(MlirOperation op,
382 MlirOpPrintingFlags flags,
383 MlirStringCallback callback,
384 void *userData);
385
386 /// Prints an operation to stderr.
387 MLIR_CAPI_EXPORTED void mlirOperationDump(MlirOperation op);
388
389 /// Verify the operation and return true if it passes, false if it fails.
390 MLIR_CAPI_EXPORTED bool mlirOperationVerify(MlirOperation op);
391
392 //===----------------------------------------------------------------------===//
393 // Region API.
394 //===----------------------------------------------------------------------===//
395
396 /// Creates a new empty region and transfers ownership to the caller.
397 MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate();
398
399 /// Takes a region owned by the caller and destroys it.
400 MLIR_CAPI_EXPORTED void mlirRegionDestroy(MlirRegion region);
401
402 /// Checks whether a region is null.
mlirRegionIsNull(MlirRegion region)403 static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; }
404
405 /// Gets the first block in the region.
406 MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region);
407
408 /// Takes a block owned by the caller and appends it to the given region.
409 MLIR_CAPI_EXPORTED void mlirRegionAppendOwnedBlock(MlirRegion region,
410 MlirBlock block);
411
412 /** Takes a block owned by the caller and inserts it at `pos` to the given
413 * region. This is an expensive operation that linearly scans the region, prefer
414 * insertAfter/Before instead. */
415 MLIR_CAPI_EXPORTED void
416 mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block);
417
418 /** Takes a block owned by the caller and inserts it after the (non-owned)
419 * reference block in the given region. The reference block must belong to the
420 * region. If the reference block is null, prepends the block to the region. */
421 MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockAfter(MlirRegion region,
422 MlirBlock reference,
423 MlirBlock block);
424
425 /** Takes a block owned by the caller and inserts it before the (non-owned)
426 * reference block in the given region. The reference block must belong to the
427 * region. If the reference block is null, appends the block to the region. */
428 MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockBefore(MlirRegion region,
429 MlirBlock reference,
430 MlirBlock block);
431
432 //===----------------------------------------------------------------------===//
433 // Block API.
434 //===----------------------------------------------------------------------===//
435
436 /** Creates a new empty block with the given argument types and transfers
437 * ownership to the caller. */
438 MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs,
439 MlirType const *args);
440
441 /// Takes a block owned by the caller and destroys it.
442 MLIR_CAPI_EXPORTED void mlirBlockDestroy(MlirBlock block);
443
444 /// Checks whether a block is null.
mlirBlockIsNull(MlirBlock block)445 static inline bool mlirBlockIsNull(MlirBlock block) { return !block.ptr; }
446
447 /** Checks whether two blocks handles point to the same block. This does not
448 * perform deep comparison. */
449 MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other);
450
451 /** Returns the block immediately following the given block in its parent
452 * region. */
453 MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block);
454
455 /// Returns the first operation in the block.
456 MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetFirstOperation(MlirBlock block);
457
458 /// Returns the terminator operation in the block or null if no terminator.
459 MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetTerminator(MlirBlock block);
460
461 /// Takes an operation owned by the caller and appends it to the block.
462 MLIR_CAPI_EXPORTED void mlirBlockAppendOwnedOperation(MlirBlock block,
463 MlirOperation operation);
464
465 /** Takes an operation owned by the caller and inserts it as `pos` to the block.
466 This is an expensive operation that scans the block linearly, prefer
467 insertBefore/After instead. */
468 MLIR_CAPI_EXPORTED void mlirBlockInsertOwnedOperation(MlirBlock block,
469 intptr_t pos,
470 MlirOperation operation);
471
472 /** Takes an operation owned by the caller and inserts it after the (non-owned)
473 * reference operation in the given block. If the reference is null, prepends
474 * the operation. Otherwise, the reference must belong to the block. */
475 MLIR_CAPI_EXPORTED void
476 mlirBlockInsertOwnedOperationAfter(MlirBlock block, MlirOperation reference,
477 MlirOperation operation);
478
479 /** Takes an operation owned by the caller and inserts it before the (non-owned)
480 * reference operation in the given block. If the reference is null, appends the
481 * operation. Otherwise, the reference must belong to the block. */
482 MLIR_CAPI_EXPORTED void
483 mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference,
484 MlirOperation operation);
485
486 /// Returns the number of arguments of the block.
487 MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumArguments(MlirBlock block);
488
489 /// Returns `pos`-th argument of the block.
490 MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block,
491 intptr_t pos);
492
493 /** Prints a block by sending chunks of the string representation and
494 * forwarding `userData to `callback`. Note that the callback may be called
495 * several times with consecutive chunks of the string. */
496 MLIR_CAPI_EXPORTED void
497 mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData);
498
499 //===----------------------------------------------------------------------===//
500 // Value API.
501 //===----------------------------------------------------------------------===//
502
503 /// Returns whether the value is null.
mlirValueIsNull(MlirValue value)504 static inline bool mlirValueIsNull(MlirValue value) { return !value.ptr; }
505
506 /// Returns 1 if two values are equal, 0 otherwise.
507 bool mlirValueEqual(MlirValue value1, MlirValue value2);
508
509 /// Returns 1 if the value is a block argument, 0 otherwise.
510 MLIR_CAPI_EXPORTED bool mlirValueIsABlockArgument(MlirValue value);
511
512 /// Returns 1 if the value is an operation result, 0 otherwise.
513 MLIR_CAPI_EXPORTED bool mlirValueIsAOpResult(MlirValue value);
514
515 /** Returns the block in which this value is defined as an argument. Asserts if
516 * the value is not a block argument. */
517 MLIR_CAPI_EXPORTED MlirBlock mlirBlockArgumentGetOwner(MlirValue value);
518
519 /// Returns the position of the value in the argument list of its block.
520 MLIR_CAPI_EXPORTED intptr_t mlirBlockArgumentGetArgNumber(MlirValue value);
521
522 /// Sets the type of the block argument to the given type.
523 MLIR_CAPI_EXPORTED void mlirBlockArgumentSetType(MlirValue value,
524 MlirType type);
525
526 /** Returns an operation that produced this value as its result. Asserts if the
527 * value is not an op result. */
528 MLIR_CAPI_EXPORTED MlirOperation mlirOpResultGetOwner(MlirValue value);
529
530 /** Returns the position of the value in the list of results of the operation
531 * that produced it. */
532 MLIR_CAPI_EXPORTED intptr_t mlirOpResultGetResultNumber(MlirValue value);
533
534 /// Returns the type of the value.
535 MLIR_CAPI_EXPORTED MlirType mlirValueGetType(MlirValue value);
536
537 /// Prints the value to the standard error stream.
538 MLIR_CAPI_EXPORTED void mlirValueDump(MlirValue value);
539
540 /** Prints a value by sending chunks of the string representation and
541 * forwarding `userData to `callback`. Note that the callback may be called
542 * several times with consecutive chunks of the string. */
543 MLIR_CAPI_EXPORTED void
544 mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData);
545
546 //===----------------------------------------------------------------------===//
547 // Type API.
548 //===----------------------------------------------------------------------===//
549
550 /// Parses a type. The type is owned by the context.
551 MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context,
552 MlirStringRef type);
553
554 /// Gets the context that a type was created with.
555 MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type);
556
557 /// Checks whether a type is null.
mlirTypeIsNull(MlirType type)558 static inline bool mlirTypeIsNull(MlirType type) { return !type.ptr; }
559
560 /// Checks if two types are equal.
561 MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2);
562
563 /** Prints a location by sending chunks of the string representation and
564 * forwarding `userData to `callback`. Note that the callback may be called
565 * several times with consecutive chunks of the string. */
566 MLIR_CAPI_EXPORTED void
567 mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData);
568
569 /// Prints the type to the standard error stream.
570 MLIR_CAPI_EXPORTED void mlirTypeDump(MlirType type);
571
572 //===----------------------------------------------------------------------===//
573 // Attribute API.
574 //===----------------------------------------------------------------------===//
575
576 /// Parses an attribute. The attribute is owned by the context.
577 MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeParseGet(MlirContext context,
578 MlirStringRef attr);
579
580 /// Gets the context that an attribute was created with.
581 MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute);
582
583 /// Gets the type of this attribute.
584 MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute);
585
586 /// Checks whether an attribute is null.
mlirAttributeIsNull(MlirAttribute attr)587 static inline bool mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; }
588
589 /// Checks if two attributes are equal.
590 MLIR_CAPI_EXPORTED bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2);
591
592 /** Prints an attribute by sending chunks of the string representation and
593 * forwarding `userData to `callback`. Note that the callback may be called
594 * several times with consecutive chunks of the string. */
595 MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr,
596 MlirStringCallback callback,
597 void *userData);
598
599 /// Prints the attribute to the standard error stream.
600 MLIR_CAPI_EXPORTED void mlirAttributeDump(MlirAttribute attr);
601
602 /// Associates an attribute with the name. Takes ownership of neither.
603 MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirStringRef name,
604 MlirAttribute attr);
605
606 //===----------------------------------------------------------------------===//
607 // Identifier API.
608 //===----------------------------------------------------------------------===//
609
610 /// Gets an identifier with the given string value.
611 MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context,
612 MlirStringRef str);
613
614 /// Checks whether two identifiers are the same.
615 MLIR_CAPI_EXPORTED bool mlirIdentifierEqual(MlirIdentifier ident,
616 MlirIdentifier other);
617
618 /// Gets the string value of the identifier.
619 MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident);
620
621 #ifdef __cplusplus
622 }
623 #endif
624
625 #endif // MLIR_C_IR_H
626