brintos

brintos / llvm-project-archived public Read only

0
0
Text · 57.7 KiB · d2f4762 Raw
1291 lines · c
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 LLVM4// Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//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 time13// with C++ due to name mangling. So in addition to C, this interface enables14// tools written in such languages.15//16//===----------------------------------------------------------------------===//17 18#ifndef MLIR_C_IR_H19#define MLIR_C_IR_H20 21#include <stdbool.h>22#include <stdint.h>23 24#include "mlir-c/Support.h"25 26#ifdef __cplusplus27extern "C" {28#endif29 30//===----------------------------------------------------------------------===//31/// Opaque type declarations.32///33/// Types are exposed to C bindings as structs containing opaque pointers. They34/// are not supposed to be inspected from C. This allows the underlying35/// representation to change without affecting the API users. The use of structs36/// instead of typedefs enables some type safety as structs are not implicitly37/// convertible to each other.38///39/// Instances of these types may or may not own the underlying object (most40/// often only point to an IR fragment without owning it). The ownership41/// 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 name50 51DEFINE_C_API_STRUCT(MlirAsmState, void);52DEFINE_C_API_STRUCT(MlirBytecodeWriterConfig, void);53DEFINE_C_API_STRUCT(MlirContext, void);54DEFINE_C_API_STRUCT(MlirDialect, void);55DEFINE_C_API_STRUCT(MlirDialectRegistry, void);56DEFINE_C_API_STRUCT(MlirOperation, void);57DEFINE_C_API_STRUCT(MlirOpOperand, void);58DEFINE_C_API_STRUCT(MlirOpPrintingFlags, void);59DEFINE_C_API_STRUCT(MlirBlock, void);60DEFINE_C_API_STRUCT(MlirRegion, void);61DEFINE_C_API_STRUCT(MlirSymbolTable, void);62 63DEFINE_C_API_STRUCT(MlirAttribute, const void);64DEFINE_C_API_STRUCT(MlirIdentifier, const void);65DEFINE_C_API_STRUCT(MlirLocation, const void);66DEFINE_C_API_STRUCT(MlirModule, const void);67DEFINE_C_API_STRUCT(MlirType, const void);68DEFINE_C_API_STRUCT(MlirValue, const void);69 70#undef DEFINE_C_API_STRUCT71 72/// Named MLIR attribute.73///74/// A named attribute is essentially a (name, attribute) pair where the name is75/// a string.76struct MlirNamedAttribute {77  MlirIdentifier name;78  MlirAttribute attribute;79};80typedef struct MlirNamedAttribute MlirNamedAttribute;81 82//===----------------------------------------------------------------------===//83// Context API.84//===----------------------------------------------------------------------===//85 86/// Creates an MLIR context and transfers its ownership to the caller.87/// This sets the default multithreading option (enabled).88MLIR_CAPI_EXPORTED MlirContext mlirContextCreate(void);89 90/// Creates an MLIR context with an explicit setting of the multithreading91/// setting and transfers its ownership to the caller.92MLIR_CAPI_EXPORTED MlirContext93mlirContextCreateWithThreading(bool threadingEnabled);94 95/// Creates an MLIR context, setting the multithreading setting explicitly and96/// pre-loading the dialects from the provided DialectRegistry.97MLIR_CAPI_EXPORTED MlirContext mlirContextCreateWithRegistry(98    MlirDialectRegistry registry, bool threadingEnabled);99 100/// Checks if two contexts are equal.101MLIR_CAPI_EXPORTED bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2);102 103/// Checks whether a context is null.104static inline bool mlirContextIsNull(MlirContext context) {105  return !context.ptr;106}107 108/// Takes an MLIR context owned by the caller and destroys it.109MLIR_CAPI_EXPORTED void mlirContextDestroy(MlirContext context);110 111/// Sets whether unregistered dialects are allowed in this context.112MLIR_CAPI_EXPORTED void113mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow);114 115/// Returns whether the context allows unregistered dialects.116MLIR_CAPI_EXPORTED bool117mlirContextGetAllowUnregisteredDialects(MlirContext context);118 119/// Returns the number of dialects registered with the given context. A120/// registered dialect will be loaded if needed by the parser.121MLIR_CAPI_EXPORTED intptr_t122mlirContextGetNumRegisteredDialects(MlirContext context);123 124/// Append the contents of the given dialect registry to the registry associated125/// with the context.126MLIR_CAPI_EXPORTED void127mlirContextAppendDialectRegistry(MlirContext ctx, MlirDialectRegistry registry);128 129/// Returns the number of dialects loaded by the context.130 131MLIR_CAPI_EXPORTED intptr_t132mlirContextGetNumLoadedDialects(MlirContext context);133 134/// Gets the dialect instance owned by the given context using the dialect135/// namespace to identify it, loads (i.e., constructs the instance of) the136/// dialect if necessary. If the dialect is not registered with the context,137/// returns null. Use mlirContextLoad<Name>Dialect to load an unregistered138/// dialect.139MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context,140                                                           MlirStringRef name);141 142/// Set threading mode (must be set to false to mlir-print-ir-after-all).143MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context,144                                                        bool enable);145 146/// Eagerly loads all available dialects registered with a context, making147/// them available for use for IR construction.148MLIR_CAPI_EXPORTED void149mlirContextLoadAllAvailableDialects(MlirContext context);150 151/// Returns whether the given fully-qualified operation (i.e.152/// 'dialect.operation') is registered with the context. This will return true153/// if the dialect is loaded and the operation is registered within the154/// dialect.155MLIR_CAPI_EXPORTED bool mlirContextIsRegisteredOperation(MlirContext context,156                                                         MlirStringRef name);157 158/// Sets the thread pool of the context explicitly, enabling multithreading in159/// the process. This API should be used to avoid re-creating thread pools in160/// long-running applications that perform multiple compilations, see161/// the C++ documentation for MLIRContext for details.162MLIR_CAPI_EXPORTED void mlirContextSetThreadPool(MlirContext context,163                                                 MlirLlvmThreadPool threadPool);164 165/// Gets the number of threads of the thread pool of the context when166/// multithreading is enabled. Returns 1 if no multithreading.167MLIR_CAPI_EXPORTED unsigned mlirContextGetNumThreads(MlirContext context);168 169/// Gets the thread pool of the context when enabled multithreading, otherwise170/// an assertion is raised.171MLIR_CAPI_EXPORTED MlirLlvmThreadPool172mlirContextGetThreadPool(MlirContext context);173 174//===----------------------------------------------------------------------===//175// Dialect API.176//===----------------------------------------------------------------------===//177 178/// Returns the context that owns the dialect.179MLIR_CAPI_EXPORTED MlirContext mlirDialectGetContext(MlirDialect dialect);180 181/// Checks if the dialect is null.182static inline bool mlirDialectIsNull(MlirDialect dialect) {183  return !dialect.ptr;184}185 186/// Checks if two dialects that belong to the same context are equal. Dialects187/// from different contexts will not compare equal.188MLIR_CAPI_EXPORTED bool mlirDialectEqual(MlirDialect dialect1,189                                         MlirDialect dialect2);190 191/// Returns the namespace of the given dialect.192MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect);193 194//===----------------------------------------------------------------------===//195// DialectHandle API.196// Registration entry-points for each dialect are declared using the common197// MLIR_DECLARE_DIALECT_REGISTRATION_CAPI macro, which takes the dialect198// API name (i.e. "Func", "Tensor", "Linalg") and namespace (i.e. "func",199// "tensor", "linalg"). The following declarations are produced:200//201//   /// Gets the above hook methods in struct form for a dialect by namespace.202//   /// This is intended to facilitate dynamic lookup and registration of203//   /// dialects via a plugin facility based on shared library symbol lookup.204//   const MlirDialectHandle *mlirGetDialectHandle__{NAMESPACE}__();205//206// This is done via a common macro to facilitate future expansion to207// registration schemes.208//===----------------------------------------------------------------------===//209 210struct MlirDialectHandle {211  const void *ptr;212};213typedef struct MlirDialectHandle MlirDialectHandle;214 215#define MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(Name, Namespace)                \216  MLIR_CAPI_EXPORTED MlirDialectHandle mlirGetDialectHandle__##Namespace##__(  \217      void)218 219/// Returns the namespace associated with the provided dialect handle.220MLIR_CAPI_EXPORTED221MlirStringRef mlirDialectHandleGetNamespace(MlirDialectHandle);222 223/// Inserts the dialect associated with the provided dialect handle into the224/// provided dialect registry225MLIR_CAPI_EXPORTED void mlirDialectHandleInsertDialect(MlirDialectHandle,226                                                       MlirDialectRegistry);227 228/// Registers the dialect associated with the provided dialect handle.229MLIR_CAPI_EXPORTED void mlirDialectHandleRegisterDialect(MlirDialectHandle,230                                                         MlirContext);231 232/// Loads the dialect associated with the provided dialect handle.233MLIR_CAPI_EXPORTED MlirDialect mlirDialectHandleLoadDialect(MlirDialectHandle,234                                                            MlirContext);235 236//===----------------------------------------------------------------------===//237// DialectRegistry API.238//===----------------------------------------------------------------------===//239 240/// Creates a dialect registry and transfers its ownership to the caller.241MLIR_CAPI_EXPORTED MlirDialectRegistry mlirDialectRegistryCreate(void);242 243/// Checks if the dialect registry is null.244static inline bool mlirDialectRegistryIsNull(MlirDialectRegistry registry) {245  return !registry.ptr;246}247 248/// Takes a dialect registry owned by the caller and destroys it.249MLIR_CAPI_EXPORTED void250mlirDialectRegistryDestroy(MlirDialectRegistry registry);251 252//===----------------------------------------------------------------------===//253// Location API.254//===----------------------------------------------------------------------===//255 256/// Returns the underlying location attribute of this location.257MLIR_CAPI_EXPORTED MlirAttribute258mlirLocationGetAttribute(MlirLocation location);259 260/// Creates a location from a location attribute.261MLIR_CAPI_EXPORTED MlirLocation262mlirLocationFromAttribute(MlirAttribute attribute);263 264/// Creates an File/Line/Column location owned by the given context.265MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(266    MlirContext context, MlirStringRef filename, unsigned line, unsigned col);267 268/// Creates an File/Line/Column range location owned by the given context.269MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(270    MlirContext context, MlirStringRef filename, unsigned start_line,271    unsigned start_col, unsigned end_line, unsigned end_col);272 273/// Getter for filename of FileLineColRange.274MLIR_CAPI_EXPORTED MlirIdentifier275mlirLocationFileLineColRangeGetFilename(MlirLocation location);276 277/// Getter for start_line of FileLineColRange.278MLIR_CAPI_EXPORTED int279mlirLocationFileLineColRangeGetStartLine(MlirLocation location);280 281/// Getter for start_column of FileLineColRange.282MLIR_CAPI_EXPORTED int283mlirLocationFileLineColRangeGetStartColumn(MlirLocation location);284 285/// Getter for end_line of FileLineColRange.286MLIR_CAPI_EXPORTED int287mlirLocationFileLineColRangeGetEndLine(MlirLocation location);288 289/// Getter for end_column of FileLineColRange.290MLIR_CAPI_EXPORTED int291mlirLocationFileLineColRangeGetEndColumn(MlirLocation location);292 293/// TypeID Getter for FileLineColRange.294MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFileLineColRangeGetTypeID(void);295 296/// Checks whether the given location is an FileLineColRange.297MLIR_CAPI_EXPORTED bool mlirLocationIsAFileLineColRange(MlirLocation location);298 299/// Creates a call site location with a callee and a caller.300MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee,301                                                        MlirLocation caller);302 303/// Getter for callee of CallSite.304MLIR_CAPI_EXPORTED MlirLocation305mlirLocationCallSiteGetCallee(MlirLocation location);306 307/// Getter for caller of CallSite.308MLIR_CAPI_EXPORTED MlirLocation309mlirLocationCallSiteGetCaller(MlirLocation location);310 311/// TypeID Getter for CallSite.312MLIR_CAPI_EXPORTED MlirTypeID mlirLocationCallSiteGetTypeID(void);313 314/// Checks whether the given location is an CallSite.315MLIR_CAPI_EXPORTED bool mlirLocationIsACallSite(MlirLocation location);316 317/// Creates a fused location with an array of locations and metadata.318MLIR_CAPI_EXPORTED MlirLocation319mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,320                     MlirLocation const *locations, MlirAttribute metadata);321 322/// Getter for number of locations fused together.323MLIR_CAPI_EXPORTED unsigned324mlirLocationFusedGetNumLocations(MlirLocation location);325 326/// Getter for locations of Fused. Requires pre-allocated memory of327/// #fusedLocations X sizeof(MlirLocation).328MLIR_CAPI_EXPORTED void329mlirLocationFusedGetLocations(MlirLocation location,330                              MlirLocation *locationsCPtr);331 332/// Getter for metadata of Fused.333MLIR_CAPI_EXPORTED MlirAttribute334mlirLocationFusedGetMetadata(MlirLocation location);335 336/// TypeID Getter for Fused.337MLIR_CAPI_EXPORTED MlirTypeID mlirLocationFusedGetTypeID(void);338 339/// Checks whether the given location is an Fused.340MLIR_CAPI_EXPORTED bool mlirLocationIsAFused(MlirLocation location);341 342/// Creates a name location owned by the given context. Providing null location343/// for childLoc is allowed and if childLoc is null location, then the behavior344/// is the same as having unknown child location.345MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context,346                                                    MlirStringRef name,347                                                    MlirLocation childLoc);348 349/// Getter for name of Name.350MLIR_CAPI_EXPORTED MlirIdentifier351mlirLocationNameGetName(MlirLocation location);352 353/// Getter for childLoc of Name.354MLIR_CAPI_EXPORTED MlirLocation355mlirLocationNameGetChildLoc(MlirLocation location);356 357/// TypeID Getter for Name.358MLIR_CAPI_EXPORTED MlirTypeID mlirLocationNameGetTypeID(void);359 360/// Checks whether the given location is an Name.361MLIR_CAPI_EXPORTED bool mlirLocationIsAName(MlirLocation location);362 363/// Creates a location with unknown position owned by the given context.364MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context);365 366/// Gets the context that a location was created with.367MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location);368 369/// Checks if the location is null.370static inline bool mlirLocationIsNull(MlirLocation location) {371  return !location.ptr;372}373 374/// Checks if two locations are equal.375MLIR_CAPI_EXPORTED bool mlirLocationEqual(MlirLocation l1, MlirLocation l2);376 377/// Prints a location by sending chunks of the string representation and378/// forwarding `userData to `callback`. Note that the callback may be called379/// several times with consecutive chunks of the string.380MLIR_CAPI_EXPORTED void mlirLocationPrint(MlirLocation location,381                                          MlirStringCallback callback,382                                          void *userData);383 384//===----------------------------------------------------------------------===//385// Module API.386//===----------------------------------------------------------------------===//387 388/// Creates a new, empty module and transfers ownership to the caller.389MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateEmpty(MlirLocation location);390 391/// Parses a module from the string and transfers ownership to the caller.392MLIR_CAPI_EXPORTED MlirModule mlirModuleCreateParse(MlirContext context,393                                                    MlirStringRef module);394 395/// Parses a module from file and transfers ownership to the caller.396MLIR_CAPI_EXPORTED MlirModule397mlirModuleCreateParseFromFile(MlirContext context, MlirStringRef fileName);398 399/// Gets the context that a module was created with.400MLIR_CAPI_EXPORTED MlirContext mlirModuleGetContext(MlirModule module);401 402/// Gets the body of the module, i.e. the only block it contains.403MLIR_CAPI_EXPORTED MlirBlock mlirModuleGetBody(MlirModule module);404 405/// Checks whether a module is null.406static inline bool mlirModuleIsNull(MlirModule module) { return !module.ptr; }407 408/// Takes a module owned by the caller and deletes it.409MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module);410 411/// Views the module as a generic operation.412MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module);413 414/// Views the generic operation as a module.415/// The returned module is null when the input operation was not a ModuleOp.416MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op);417 418/// Checks if two modules are equal.419MLIR_CAPI_EXPORTED bool mlirModuleEqual(MlirModule lhs, MlirModule rhs);420 421/// Compute a hash for the given module.422MLIR_CAPI_EXPORTED size_t mlirModuleHashValue(MlirModule mod);423 424//===----------------------------------------------------------------------===//425// Operation state.426//===----------------------------------------------------------------------===//427 428/// An auxiliary class for constructing operations.429///430/// This class contains all the information necessary to construct the431/// operation. It owns the MlirRegions it has pointers to and does not own432/// anything else. By default, the state can be constructed from a name and433/// location, the latter being also used to access the context, and has no other434/// components. These components can be added progressively until the operation435/// is constructed. Users are not expected to rely on the internals of this436/// class and should use mlirOperationState* functions instead.437 438struct MlirOperationState {439  MlirStringRef name;440  MlirLocation location;441  intptr_t nResults;442  MlirType *results;443  intptr_t nOperands;444  MlirValue *operands;445  intptr_t nRegions;446  MlirRegion *regions;447  intptr_t nSuccessors;448  MlirBlock *successors;449  intptr_t nAttributes;450  MlirNamedAttribute *attributes;451  bool enableResultTypeInference;452};453typedef struct MlirOperationState MlirOperationState;454 455/// Constructs an operation state from a name and a location.456MLIR_CAPI_EXPORTED MlirOperationState mlirOperationStateGet(MlirStringRef name,457                                                            MlirLocation loc);458 459/// Adds a list of components to the operation state.460MLIR_CAPI_EXPORTED void mlirOperationStateAddResults(MlirOperationState *state,461                                                     intptr_t n,462                                                     MlirType const *results);463MLIR_CAPI_EXPORTED void464mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,465                              MlirValue const *operands);466MLIR_CAPI_EXPORTED void467mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,468                                  MlirRegion const *regions);469MLIR_CAPI_EXPORTED void470mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,471                                MlirBlock const *successors);472MLIR_CAPI_EXPORTED void473mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,474                                MlirNamedAttribute const *attributes);475 476/// Enables result type inference for the operation under construction. If477/// enabled, then the caller must not have called478/// mlirOperationStateAddResults(). Note that if enabled, the479/// mlirOperationCreate() call is failable: it will return a null operation480/// on inference failure and will emit diagnostics.481MLIR_CAPI_EXPORTED void482mlirOperationStateEnableResultTypeInference(MlirOperationState *state);483 484//===----------------------------------------------------------------------===//485// AsmState API.486// While many of these are simple settings that could be represented in a487// struct, they are wrapped in a heap allocated object and accessed via488// functions to maximize the possibility of compatibility over time.489//===----------------------------------------------------------------------===//490 491/// Creates new AsmState, as with AsmState the IR should not be mutated492/// in-between using this state.493/// Must be freed with a call to mlirAsmStateDestroy().494// TODO: This should be expanded to handle location & resouce map.495MLIR_CAPI_EXPORTED MlirAsmState496mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags);497 498/// Creates new AsmState from value.499/// Must be freed with a call to mlirAsmStateDestroy().500// TODO: This should be expanded to handle location & resouce map.501MLIR_CAPI_EXPORTED MlirAsmState502mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags);503 504/// Destroys printing flags created with mlirAsmStateCreate.505MLIR_CAPI_EXPORTED void mlirAsmStateDestroy(MlirAsmState state);506 507//===----------------------------------------------------------------------===//508// Op Printing flags API.509// While many of these are simple settings that could be represented in a510// struct, they are wrapped in a heap allocated object and accessed via511// functions to maximize the possibility of compatibility over time.512//===----------------------------------------------------------------------===//513 514/// Creates new printing flags with defaults, intended for customization.515/// Must be freed with a call to mlirOpPrintingFlagsDestroy().516MLIR_CAPI_EXPORTED MlirOpPrintingFlags mlirOpPrintingFlagsCreate(void);517 518/// Destroys printing flags created with mlirOpPrintingFlagsCreate.519MLIR_CAPI_EXPORTED void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags);520 521/// Enables the elision of large elements attributes by printing a lexically522/// valid but otherwise meaningless form instead of the element data. The523/// `largeElementLimit` is used to configure what is considered to be a "large"524/// ElementsAttr by providing an upper limit to the number of elements.525MLIR_CAPI_EXPORTED void526mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,527                                           intptr_t largeElementLimit);528 529/// Enables the elision of large resources strings by omitting them from the530/// `dialect_resources` section. The `largeResourceLimit` is used to configure531/// what is considered to be a "large" resource by providing an upper limit to532/// the string size.533MLIR_CAPI_EXPORTED void534mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags,535                                            intptr_t largeResourceLimit);536 537/// Enable or disable printing of debug information (based on `enable`). If538/// 'prettyForm' is set to true, debug information is printed in a more readable539/// 'pretty' form. Note: The IR generated with 'prettyForm' is not parsable.540MLIR_CAPI_EXPORTED void541mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable,542                                   bool prettyForm);543 544/// Always print operations in the generic form.545MLIR_CAPI_EXPORTED void546mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags);547 548/// Print the name and location, if NamedLoc, as a prefix to the SSA ID.549MLIR_CAPI_EXPORTED void550mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags);551 552/// Use local scope when printing the operation. This allows for using the553/// printer in a more localized and thread-safe setting, but may not554/// necessarily be identical to what the IR will look like when dumping555/// the full module.556MLIR_CAPI_EXPORTED void557mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags);558 559/// Do not verify the operation when using custom operation printers.560MLIR_CAPI_EXPORTED void561mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags);562 563/// Skip printing regions.564MLIR_CAPI_EXPORTED void565mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags);566 567//===----------------------------------------------------------------------===//568// Bytecode printing flags API.569//===----------------------------------------------------------------------===//570 571/// Creates new printing flags with defaults, intended for customization.572/// Must be freed with a call to mlirBytecodeWriterConfigDestroy().573MLIR_CAPI_EXPORTED MlirBytecodeWriterConfig574mlirBytecodeWriterConfigCreate(void);575 576/// Destroys printing flags created with mlirBytecodeWriterConfigCreate.577MLIR_CAPI_EXPORTED void578mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config);579 580/// Sets the version to emit in the writer config.581MLIR_CAPI_EXPORTED void582mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags,583                                           int64_t version);584 585//===----------------------------------------------------------------------===//586// Operation API.587//===----------------------------------------------------------------------===//588 589/// Creates an operation and transfers ownership to the caller.590/// Note that caller owned child objects are transferred in this call and must591/// not be further used. Particularly, this applies to any regions added to592/// the state (the implementation may invalidate any such pointers).593///594/// This call can fail under the following conditions, in which case, it will595/// return a null operation and emit diagnostics:596///   - Result type inference is enabled and cannot be performed.597MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreate(MlirOperationState *state);598 599/// Parses an operation, giving ownership to the caller. If parsing fails a null600/// operation will be returned, and an error diagnostic emitted.601///602/// `sourceStr` may be either the text assembly format, or binary bytecode603/// format. `sourceName` is used as the file name of the source; any IR without604/// locations will get a `FileLineColLoc` location with `sourceName` as the file605/// name.606MLIR_CAPI_EXPORTED MlirOperation mlirOperationCreateParse(607    MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName);608 609/// Creates a deep copy of an operation. The operation is not inserted and610/// ownership is transferred to the caller.611MLIR_CAPI_EXPORTED MlirOperation mlirOperationClone(MlirOperation op);612 613/// Takes an operation owned by the caller and destroys it.614MLIR_CAPI_EXPORTED void mlirOperationDestroy(MlirOperation op);615 616/// Removes the given operation from its parent block. The operation is not617/// destroyed. The ownership of the operation is transferred to the caller.618MLIR_CAPI_EXPORTED void mlirOperationRemoveFromParent(MlirOperation op);619 620/// Checks whether the underlying operation is null.621static inline bool mlirOperationIsNull(MlirOperation op) { return !op.ptr; }622 623/// Checks whether two operation handles point to the same operation. This does624/// not perform deep comparison.625MLIR_CAPI_EXPORTED bool mlirOperationEqual(MlirOperation op,626                                           MlirOperation other);627 628/// Compute a hash for the given operation.629MLIR_CAPI_EXPORTED size_t mlirOperationHashValue(MlirOperation op);630 631/// Gets the context this operation is associated with632MLIR_CAPI_EXPORTED MlirContext mlirOperationGetContext(MlirOperation op);633 634/// Gets the location of the operation.635MLIR_CAPI_EXPORTED MlirLocation mlirOperationGetLocation(MlirOperation op);636 637/// Sets the location of the operation.638MLIR_CAPI_EXPORTED void mlirOperationSetLocation(MlirOperation op,639                                                 MlirLocation loc);640 641/// Gets the type id of the operation.642/// Returns null if the operation does not have a registered operation643/// description.644MLIR_CAPI_EXPORTED MlirTypeID mlirOperationGetTypeID(MlirOperation op);645 646/// Gets the name of the operation as an identifier.647MLIR_CAPI_EXPORTED MlirIdentifier mlirOperationGetName(MlirOperation op);648 649/// Gets the block that owns this operation, returning null if the operation is650/// not owned.651MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetBlock(MlirOperation op);652 653/// Gets the operation that owns this operation, returning null if the operation654/// is not owned.655MLIR_CAPI_EXPORTED MlirOperation656mlirOperationGetParentOperation(MlirOperation op);657 658/// Returns the number of regions attached to the given operation.659MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumRegions(MlirOperation op);660 661/// Returns `pos`-th region attached to the operation.662MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetRegion(MlirOperation op,663                                                     intptr_t pos);664 665/// Returns an operation immediately following the given operation it its666/// enclosing block.667MLIR_CAPI_EXPORTED MlirOperation mlirOperationGetNextInBlock(MlirOperation op);668 669/// Returns the number of operands of the operation.670MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumOperands(MlirOperation op);671 672/// Returns `pos`-th operand of the operation.673MLIR_CAPI_EXPORTED MlirValue mlirOperationGetOperand(MlirOperation op,674                                                     intptr_t pos);675 676/// Sets the `pos`-th operand of the operation.677MLIR_CAPI_EXPORTED void mlirOperationSetOperand(MlirOperation op, intptr_t pos,678                                                MlirValue newValue);679 680/// Replaces the operands of the operation.681MLIR_CAPI_EXPORTED void mlirOperationSetOperands(MlirOperation op,682                                                 intptr_t nOperands,683                                                 MlirValue const *operands);684 685/// Returns the number of results of the operation.686MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumResults(MlirOperation op);687 688/// Returns `pos`-th result of the operation.689MLIR_CAPI_EXPORTED MlirValue mlirOperationGetResult(MlirOperation op,690                                                    intptr_t pos);691 692/// Returns the number of successor blocks of the operation.693MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumSuccessors(MlirOperation op);694 695/// Returns `pos`-th successor of the operation.696MLIR_CAPI_EXPORTED MlirBlock mlirOperationGetSuccessor(MlirOperation op,697                                                       intptr_t pos);698 699/// Set `pos`-th successor of the operation.700MLIR_CAPI_EXPORTED void701mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block);702 703/// Returns true if this operation defines an inherent attribute with this name.704/// Note: the attribute can be optional, so705/// `mlirOperationGetInherentAttributeByName` can still return a null attribute.706MLIR_CAPI_EXPORTED bool707mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name);708 709/// Returns an inherent attribute attached to the operation given its name.710MLIR_CAPI_EXPORTED MlirAttribute711mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name);712 713/// Sets an inherent attribute by name, replacing the existing if it exists.714/// This has no effect if "name" does not match an inherent attribute.715MLIR_CAPI_EXPORTED void716mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name,717                                        MlirAttribute attr);718 719/// Returns the number of discardable attributes attached to the operation.720MLIR_CAPI_EXPORTED intptr_t721mlirOperationGetNumDiscardableAttributes(MlirOperation op);722 723/// Return `pos`-th discardable attribute of the operation.724MLIR_CAPI_EXPORTED MlirNamedAttribute725mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos);726 727/// Returns a discardable attribute attached to the operation given its name.728MLIR_CAPI_EXPORTED MlirAttribute mlirOperationGetDiscardableAttributeByName(729    MlirOperation op, MlirStringRef name);730 731/// Sets a discardable attribute by name, replacing the existing if it exists or732/// adding a new one otherwise. The new `attr` Attribute is not allowed to be733/// null, use `mlirOperationRemoveDiscardableAttributeByName` to remove an734/// Attribute instead.735MLIR_CAPI_EXPORTED void736mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name,737                                           MlirAttribute attr);738 739/// Removes a discardable attribute by name. Returns false if the attribute was740/// not found and true if removed.741MLIR_CAPI_EXPORTED bool742mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,743                                              MlirStringRef name);744 745/// Returns the number of attributes attached to the operation.746/// Deprecated, please use `mlirOperationGetNumInherentAttributes` or747/// `mlirOperationGetNumDiscardableAttributes`.748MLIR_CAPI_EXPORTED intptr_t mlirOperationGetNumAttributes(MlirOperation op);749 750/// Return `pos`-th attribute of the operation.751/// Deprecated, please use `mlirOperationGetInherentAttribute` or752/// `mlirOperationGetDiscardableAttribute`.753MLIR_CAPI_EXPORTED MlirNamedAttribute754mlirOperationGetAttribute(MlirOperation op, intptr_t pos);755 756/// Returns an attribute attached to the operation given its name.757/// Deprecated, please use `mlirOperationGetInherentAttributeByName` or758/// `mlirOperationGetDiscardableAttributeByName`.759MLIR_CAPI_EXPORTED MlirAttribute760mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name);761 762/// Sets an attribute by name, replacing the existing if it exists or763/// adding a new one otherwise.764/// Deprecated, please use `mlirOperationSetInherentAttributeByName` or765/// `mlirOperationSetDiscardableAttributeByName`.766MLIR_CAPI_EXPORTED void mlirOperationSetAttributeByName(MlirOperation op,767                                                        MlirStringRef name,768                                                        MlirAttribute attr);769 770/// Removes an attribute by name. Returns false if the attribute was not found771/// and true if removed.772/// Deprecated, please use `mlirOperationRemoveInherentAttributeByName` or773/// `mlirOperationRemoveDiscardableAttributeByName`.774MLIR_CAPI_EXPORTED bool mlirOperationRemoveAttributeByName(MlirOperation op,775                                                           MlirStringRef name);776 777/// Prints an operation by sending chunks of the string representation and778/// forwarding `userData to `callback`. Note that the callback may be called779/// several times with consecutive chunks of the string.780MLIR_CAPI_EXPORTED void mlirOperationPrint(MlirOperation op,781                                           MlirStringCallback callback,782                                           void *userData);783 784/// Same as mlirOperationPrint but accepts flags controlling the printing785/// behavior.786MLIR_CAPI_EXPORTED void mlirOperationPrintWithFlags(MlirOperation op,787                                                    MlirOpPrintingFlags flags,788                                                    MlirStringCallback callback,789                                                    void *userData);790 791/// Same as mlirOperationPrint but accepts AsmState controlling the printing792/// behavior as well as caching computed names.793MLIR_CAPI_EXPORTED void mlirOperationPrintWithState(MlirOperation op,794                                                    MlirAsmState state,795                                                    MlirStringCallback callback,796                                                    void *userData);797 798/// Same as mlirOperationPrint but writing the bytecode format.799MLIR_CAPI_EXPORTED void mlirOperationWriteBytecode(MlirOperation op,800                                                   MlirStringCallback callback,801                                                   void *userData);802 803/// Same as mlirOperationWriteBytecode but with writer config and returns804/// failure only if desired bytecode could not be honored.805MLIR_CAPI_EXPORTED MlirLogicalResult mlirOperationWriteBytecodeWithConfig(806    MlirOperation op, MlirBytecodeWriterConfig config,807    MlirStringCallback callback, void *userData);808 809/// Prints an operation to stderr.810MLIR_CAPI_EXPORTED void mlirOperationDump(MlirOperation op);811 812/// Verify the operation and return true if it passes, false if it fails.813MLIR_CAPI_EXPORTED bool mlirOperationVerify(MlirOperation op);814 815/// Moves the given operation immediately after the other operation in its816/// parent block. The given operation may be owned by the caller or by its817/// current block. The other operation must belong to a block. In any case, the818/// ownership is transferred to the block of the other operation.819MLIR_CAPI_EXPORTED void mlirOperationMoveAfter(MlirOperation op,820                                               MlirOperation other);821 822/// Moves the given operation immediately before the other operation in its823/// parent block. The given operation may be owner by the caller or by its824/// current block. The other operation must belong to a block. In any case, the825/// ownership is transferred to the block of the other operation.826MLIR_CAPI_EXPORTED void mlirOperationMoveBefore(MlirOperation op,827                                                MlirOperation other);828 829/// Given an operation 'other' that is within the same parent block, return830/// whether the current operation is before 'other' in the operation list831/// of the parent block.832/// Note: This function has an average complexity of O(1), but worst case may833/// take O(N) where N is the number of operations within the parent block.834MLIR_CAPI_EXPORTED bool mlirOperationIsBeforeInBlock(MlirOperation op,835                                                     MlirOperation other);836/// Operation walk result.837typedef enum MlirWalkResult {838  MlirWalkResultAdvance,839  MlirWalkResultInterrupt,840  MlirWalkResultSkip841} MlirWalkResult;842 843/// Traversal order for operation walk.844typedef enum MlirWalkOrder {845  MlirWalkPreOrder,846  MlirWalkPostOrder847} MlirWalkOrder;848 849/// Operation walker type. The handler is passed an (opaque) reference to an850/// operation and a pointer to a `userData`.851typedef MlirWalkResult (*MlirOperationWalkCallback)(MlirOperation,852                                                    void *userData);853 854/// Walks operation `op` in `walkOrder` and calls `callback` on that operation.855/// `*userData` is passed to the callback as well and can be used to tunnel some856/// context or other data into the callback.857MLIR_CAPI_EXPORTED858void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,859                       void *userData, MlirWalkOrder walkOrder);860 861//===----------------------------------------------------------------------===//862// Region API.863//===----------------------------------------------------------------------===//864 865/// Creates a new empty region and transfers ownership to the caller.866MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate(void);867 868/// Takes a region owned by the caller and destroys it.869MLIR_CAPI_EXPORTED void mlirRegionDestroy(MlirRegion region);870 871/// Checks whether a region is null.872static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; }873 874/// Checks whether two region handles point to the same region. This does not875/// perform deep comparison.876MLIR_CAPI_EXPORTED bool mlirRegionEqual(MlirRegion region, MlirRegion other);877 878/// Gets the first block in the region.879MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region);880 881/// Takes a block owned by the caller and appends it to the given region.882MLIR_CAPI_EXPORTED void mlirRegionAppendOwnedBlock(MlirRegion region,883                                                   MlirBlock block);884 885/// Takes a block owned by the caller and inserts it at `pos` to the given886/// region. This is an expensive operation that linearly scans the region,887/// prefer insertAfter/Before instead.888MLIR_CAPI_EXPORTED void889mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block);890 891/// Takes a block owned by the caller and inserts it after the (non-owned)892/// reference block in the given region. The reference block must belong to the893/// region. If the reference block is null, prepends the block to the region.894MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockAfter(MlirRegion region,895                                                        MlirBlock reference,896                                                        MlirBlock block);897 898/// Takes a block owned by the caller and inserts it before the (non-owned)899/// reference block in the given region. The reference block must belong to the900/// region. If the reference block is null, appends the block to the region.901MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockBefore(MlirRegion region,902                                                         MlirBlock reference,903                                                         MlirBlock block);904 905/// Returns first region attached to the operation.906MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetFirstRegion(MlirOperation op);907 908/// Returns the region immediately following the given region in its parent909/// operation.910MLIR_CAPI_EXPORTED MlirRegion mlirRegionGetNextInOperation(MlirRegion region);911 912/// Moves the entire content of the source region to the target region.913MLIR_CAPI_EXPORTED void mlirRegionTakeBody(MlirRegion target,914                                           MlirRegion source);915 916//===----------------------------------------------------------------------===//917// Block API.918//===----------------------------------------------------------------------===//919 920/// Creates a new empty block with the given argument types and transfers921/// ownership to the caller.922MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs,923                                             MlirType const *args,924                                             MlirLocation const *locs);925 926/// Takes a block owned by the caller and destroys it.927MLIR_CAPI_EXPORTED void mlirBlockDestroy(MlirBlock block);928 929/// Detach a block from the owning region and assume ownership.930MLIR_CAPI_EXPORTED void mlirBlockDetach(MlirBlock block);931 932/// Checks whether a block is null.933static inline bool mlirBlockIsNull(MlirBlock block) { return !block.ptr; }934 935/// Checks whether two blocks handles point to the same block. This does not936/// perform deep comparison.937MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other);938 939/// Returns the closest surrounding operation that contains this block.940MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock);941 942/// Returns the region that contains this block.943MLIR_CAPI_EXPORTED MlirRegion mlirBlockGetParentRegion(MlirBlock block);944 945/// Returns the block immediately following the given block in its parent946/// region.947MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block);948 949/// Returns the first operation in the block.950MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetFirstOperation(MlirBlock block);951 952/// Returns the terminator operation in the block or null if no terminator.953MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetTerminator(MlirBlock block);954 955/// Takes an operation owned by the caller and appends it to the block.956MLIR_CAPI_EXPORTED void mlirBlockAppendOwnedOperation(MlirBlock block,957                                                      MlirOperation operation);958 959/// Takes an operation owned by the caller and inserts it as `pos` to the block.960/// This is an expensive operation that scans the block linearly, prefer961/// insertBefore/After instead.962MLIR_CAPI_EXPORTED void mlirBlockInsertOwnedOperation(MlirBlock block,963                                                      intptr_t pos,964                                                      MlirOperation operation);965 966/// Takes an operation owned by the caller and inserts it after the (non-owned)967/// reference operation in the given block. If the reference is null, prepends968/// the operation. Otherwise, the reference must belong to the block.969MLIR_CAPI_EXPORTED void970mlirBlockInsertOwnedOperationAfter(MlirBlock block, MlirOperation reference,971                                   MlirOperation operation);972 973/// Takes an operation owned by the caller and inserts it before the (non-owned)974/// reference operation in the given block. If the reference is null, appends975/// the operation. Otherwise, the reference must belong to the block.976MLIR_CAPI_EXPORTED void977mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference,978                                    MlirOperation operation);979 980/// Returns the number of arguments of the block.981MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumArguments(MlirBlock block);982 983/// Appends an argument of the specified type to the block. Returns the newly984/// added argument.985MLIR_CAPI_EXPORTED MlirValue mlirBlockAddArgument(MlirBlock block,986                                                  MlirType type,987                                                  MlirLocation loc);988 989/// Erase the argument at 'index' and remove it from the argument list.990MLIR_CAPI_EXPORTED void mlirBlockEraseArgument(MlirBlock block, unsigned index);991 992/// Inserts an argument of the specified type at a specified index to the block.993/// Returns the newly added argument.994MLIR_CAPI_EXPORTED MlirValue mlirBlockInsertArgument(MlirBlock block,995                                                     intptr_t pos,996                                                     MlirType type,997                                                     MlirLocation loc);998 999/// Returns `pos`-th argument of the block.1000MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block,1001                                                  intptr_t pos);1002 1003/// Prints a block by sending chunks of the string representation and1004/// forwarding `userData to `callback`. Note that the callback may be called1005/// several times with consecutive chunks of the string.1006MLIR_CAPI_EXPORTED void1007mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData);1008 1009/// Returns the number of successor blocks of the block.1010MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumSuccessors(MlirBlock block);1011 1012/// Returns `pos`-th successor of the block.1013MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetSuccessor(MlirBlock block,1014                                                   intptr_t pos);1015 1016/// Returns the number of predecessor blocks of the block.1017MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumPredecessors(MlirBlock block);1018 1019/// Returns `pos`-th predecessor of the block.1020///1021/// WARNING: This getter is more expensive than the others here because1022/// the impl actually iterates the use-def chain (of block operands) anew for1023/// each indexed access.1024MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetPredecessor(MlirBlock block,1025                                                     intptr_t pos);1026 1027//===----------------------------------------------------------------------===//1028// Value API.1029//===----------------------------------------------------------------------===//1030 1031/// Returns whether the value is null.1032static inline bool mlirValueIsNull(MlirValue value) { return !value.ptr; }1033 1034/// Returns 1 if two values are equal, 0 otherwise.1035MLIR_CAPI_EXPORTED bool mlirValueEqual(MlirValue value1, MlirValue value2);1036 1037/// Returns 1 if the value is a block argument, 0 otherwise.1038MLIR_CAPI_EXPORTED bool mlirValueIsABlockArgument(MlirValue value);1039 1040/// Returns 1 if the value is an operation result, 0 otherwise.1041MLIR_CAPI_EXPORTED bool mlirValueIsAOpResult(MlirValue value);1042 1043/// Returns the block in which this value is defined as an argument. Asserts if1044/// the value is not a block argument.1045MLIR_CAPI_EXPORTED MlirBlock mlirBlockArgumentGetOwner(MlirValue value);1046 1047/// Returns the position of the value in the argument list of its block.1048MLIR_CAPI_EXPORTED intptr_t mlirBlockArgumentGetArgNumber(MlirValue value);1049 1050/// Sets the type of the block argument to the given type.1051MLIR_CAPI_EXPORTED void mlirBlockArgumentSetType(MlirValue value,1052                                                 MlirType type);1053 1054/// Sets the location of the block argument to the given location.1055MLIR_CAPI_EXPORTED void mlirBlockArgumentSetLocation(MlirValue value,1056                                                     MlirLocation loc);1057 1058/// Returns an operation that produced this value as its result. Asserts if the1059/// value is not an op result.1060MLIR_CAPI_EXPORTED MlirOperation mlirOpResultGetOwner(MlirValue value);1061 1062/// Returns the position of the value in the list of results of the operation1063/// that produced it.1064MLIR_CAPI_EXPORTED intptr_t mlirOpResultGetResultNumber(MlirValue value);1065 1066/// Returns the type of the value.1067MLIR_CAPI_EXPORTED MlirType mlirValueGetType(MlirValue value);1068 1069/// Set the type of the value.1070MLIR_CAPI_EXPORTED void mlirValueSetType(MlirValue value, MlirType type);1071 1072/// Prints the value to the standard error stream.1073MLIR_CAPI_EXPORTED void mlirValueDump(MlirValue value);1074 1075/// Prints a value by sending chunks of the string representation and1076/// forwarding `userData to `callback`. Note that the callback may be called1077/// several times with consecutive chunks of the string.1078MLIR_CAPI_EXPORTED void1079mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData);1080 1081/// Prints a value as an operand (i.e., the ValueID).1082MLIR_CAPI_EXPORTED void mlirValuePrintAsOperand(MlirValue value,1083                                                MlirAsmState state,1084                                                MlirStringCallback callback,1085                                                void *userData);1086 1087/// Returns an op operand representing the first use of the value, or a null op1088/// operand if there are no uses.1089MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value);1090 1091/// Replace all uses of 'of' value with the 'with' value, updating anything in1092/// the IR that uses 'of' to use the other value instead.  When this returns1093/// there are zero uses of 'of'.1094MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of,1095                                                      MlirValue with);1096 1097/// Replace all uses of 'of' value with 'with' value, updating anything in the1098/// IR that uses 'of' to use 'with' instead, except if the user is listed in1099/// 'exceptions'. The 'exceptions' parameter is an array of MlirOperation1100/// pointers with a length of 'numExceptions'.1101MLIR_CAPI_EXPORTED void1102mlirValueReplaceAllUsesExcept(MlirValue of, MlirValue with,1103                              intptr_t numExceptions,1104                              MlirOperation *exceptions);1105 1106/// Gets the location of the value.1107MLIR_CAPI_EXPORTED MlirLocation mlirValueGetLocation(MlirValue v);1108 1109/// Gets the context that a value was created with.1110MLIR_CAPI_EXPORTED MlirContext mlirValueGetContext(MlirValue v);1111 1112//===----------------------------------------------------------------------===//1113// OpOperand API.1114//===----------------------------------------------------------------------===//1115 1116/// Returns whether the op operand is null.1117MLIR_CAPI_EXPORTED bool mlirOpOperandIsNull(MlirOpOperand opOperand);1118 1119/// Returns the value of an op operand.1120MLIR_CAPI_EXPORTED MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand);1121 1122/// Returns the owner operation of an op operand.1123MLIR_CAPI_EXPORTED MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand);1124 1125/// Returns the operand number of an op operand.1126MLIR_CAPI_EXPORTED unsigned1127mlirOpOperandGetOperandNumber(MlirOpOperand opOperand);1128 1129/// Returns an op operand representing the next use of the value, or a null op1130/// operand if there is no next use.1131MLIR_CAPI_EXPORTED MlirOpOperand1132mlirOpOperandGetNextUse(MlirOpOperand opOperand);1133 1134//===----------------------------------------------------------------------===//1135// Type API.1136//===----------------------------------------------------------------------===//1137 1138/// Parses a type. The type is owned by the context.1139MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context,1140                                             MlirStringRef type);1141 1142/// Gets the context that a type was created with.1143MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type);1144 1145/// Gets the type ID of the type.1146MLIR_CAPI_EXPORTED MlirTypeID mlirTypeGetTypeID(MlirType type);1147 1148/// Gets the dialect a type belongs to.1149MLIR_CAPI_EXPORTED MlirDialect mlirTypeGetDialect(MlirType type);1150 1151/// Checks whether a type is null.1152static inline bool mlirTypeIsNull(MlirType type) { return !type.ptr; }1153 1154/// Checks if two types are equal.1155MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2);1156 1157/// Prints a location by sending chunks of the string representation and1158/// forwarding `userData to `callback`. Note that the callback may be called1159/// several times with consecutive chunks of the string.1160MLIR_CAPI_EXPORTED void1161mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData);1162 1163/// Prints the type to the standard error stream.1164MLIR_CAPI_EXPORTED void mlirTypeDump(MlirType type);1165 1166//===----------------------------------------------------------------------===//1167// Attribute API.1168//===----------------------------------------------------------------------===//1169 1170/// Parses an attribute. The attribute is owned by the context.1171MLIR_CAPI_EXPORTED MlirAttribute mlirAttributeParseGet(MlirContext context,1172                                                       MlirStringRef attr);1173 1174/// Gets the context that an attribute was created with.1175MLIR_CAPI_EXPORTED MlirContext mlirAttributeGetContext(MlirAttribute attribute);1176 1177/// Gets the type of this attribute.1178MLIR_CAPI_EXPORTED MlirType mlirAttributeGetType(MlirAttribute attribute);1179 1180/// Gets the type id of the attribute.1181MLIR_CAPI_EXPORTED MlirTypeID mlirAttributeGetTypeID(MlirAttribute attribute);1182 1183/// Gets the dialect of the attribute.1184MLIR_CAPI_EXPORTED MlirDialect mlirAttributeGetDialect(MlirAttribute attribute);1185 1186/// Checks whether an attribute is null.1187static inline bool mlirAttributeIsNull(MlirAttribute attr) { return !attr.ptr; }1188 1189/// Checks if two attributes are equal.1190MLIR_CAPI_EXPORTED bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2);1191 1192/// Prints an attribute by sending chunks of the string representation and1193/// forwarding `userData to `callback`. Note that the callback may be called1194/// several times with consecutive chunks of the string.1195MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr,1196                                           MlirStringCallback callback,1197                                           void *userData);1198 1199/// Prints the attribute to the standard error stream.1200MLIR_CAPI_EXPORTED void mlirAttributeDump(MlirAttribute attr);1201 1202/// Associates an attribute with the name. Takes ownership of neither.1203MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name,1204                                                            MlirAttribute attr);1205 1206//===----------------------------------------------------------------------===//1207// Identifier API.1208//===----------------------------------------------------------------------===//1209 1210/// Gets an identifier with the given string value.1211MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context,1212                                                    MlirStringRef str);1213 1214/// Returns the context associated with this identifier1215MLIR_CAPI_EXPORTED MlirContext mlirIdentifierGetContext(MlirIdentifier);1216 1217/// Checks whether two identifiers are the same.1218MLIR_CAPI_EXPORTED bool mlirIdentifierEqual(MlirIdentifier ident,1219                                            MlirIdentifier other);1220 1221/// Gets the string value of the identifier.1222MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident);1223 1224//===----------------------------------------------------------------------===//1225// Symbol and SymbolTable API.1226//===----------------------------------------------------------------------===//1227 1228/// Returns the name of the attribute used to store symbol names compatible with1229/// symbol tables.1230MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolTableGetSymbolAttributeName(void);1231 1232/// Returns the name of the attribute used to store symbol visibility.1233MLIR_CAPI_EXPORTED MlirStringRef1234mlirSymbolTableGetVisibilityAttributeName(void);1235 1236/// Creates a symbol table for the given operation. If the operation does not1237/// have the SymbolTable trait, returns a null symbol table.1238MLIR_CAPI_EXPORTED MlirSymbolTable1239mlirSymbolTableCreate(MlirOperation operation);1240 1241/// Returns true if the symbol table is null.1242static inline bool mlirSymbolTableIsNull(MlirSymbolTable symbolTable) {1243  return !symbolTable.ptr;1244}1245 1246/// Destroys the symbol table created with mlirSymbolTableCreate. This does not1247/// affect the operations in the table.1248MLIR_CAPI_EXPORTED void mlirSymbolTableDestroy(MlirSymbolTable symbolTable);1249 1250/// Looks up a symbol with the given name in the given symbol table and returns1251/// the operation that corresponds to the symbol. If the symbol cannot be found,1252/// returns a null operation.1253MLIR_CAPI_EXPORTED MlirOperation1254mlirSymbolTableLookup(MlirSymbolTable symbolTable, MlirStringRef name);1255 1256/// Inserts the given operation into the given symbol table. The operation must1257/// have the symbol trait. If the symbol table already has a symbol with the1258/// same name, renames the symbol being inserted to ensure name uniqueness. Note1259/// that this does not move the operation itself into the block of the symbol1260/// table operation, this should be done separately. Returns the name of the1261/// symbol after insertion.1262MLIR_CAPI_EXPORTED MlirAttribute1263mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation);1264 1265/// Removes the given operation from the symbol table and erases it.1266MLIR_CAPI_EXPORTED void mlirSymbolTableErase(MlirSymbolTable symbolTable,1267                                             MlirOperation operation);1268 1269/// Attempt to replace all uses that are nested within the given operation1270/// of the given symbol 'oldSymbol' with the provided 'newSymbol'. This does1271/// not traverse into nested symbol tables. Will fail atomically if there are1272/// any unknown operations that may be potential symbol tables.1273MLIR_CAPI_EXPORTED MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(1274    MlirStringRef oldSymbol, MlirStringRef newSymbol, MlirOperation from);1275 1276/// Walks all symbol table operations nested within, and including, `op`. For1277/// each symbol table operation, the provided callback is invoked with the op1278/// and a boolean signifying if the symbols within that symbol table can be1279/// treated as if all uses within the IR are visible to the caller.1280/// `allSymUsesVisible` identifies whether all of the symbol uses of symbols1281/// within `op` are visible.1282MLIR_CAPI_EXPORTED void mlirSymbolTableWalkSymbolTables(1283    MlirOperation from, bool allSymUsesVisible,1284    void (*callback)(MlirOperation, bool, void *userData), void *userData);1285 1286#ifdef __cplusplus1287}1288#endif1289 1290#endif // MLIR_C_IR_H1291