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