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