llvm/mlir/lib/CAPI/IR/IR.cpp

//===- IR.cpp - C Interface for Core MLIR APIs ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir-c/IR.h"
#include "mlir-c/Support.h"

#include "mlir/AsmParser/AsmParser.h"
#include "mlir/Bytecode/BytecodeWriter.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Support.h"
#include "mlir/CAPI/Utils.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/OperationSupport.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/Value.h"
#include "mlir/IR/Verifier.h"
#include "mlir/IR/Visitors.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Parser/Parser.h"
#include "llvm/Support/ThreadPool.h"

#include <cstddef>
#include <memory>
#include <optional>

usingnamespacemlir;

//===----------------------------------------------------------------------===//
// Context API.
//===----------------------------------------------------------------------===//

MlirContext mlirContextCreate() {}

static inline MLIRContext::Threading toThreadingEnum(bool threadingEnabled) {}

MlirContext mlirContextCreateWithThreading(bool threadingEnabled) {}

MlirContext mlirContextCreateWithRegistry(MlirDialectRegistry registry,
                                          bool threadingEnabled) {}

bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {}

void mlirContextDestroy(MlirContext context) {}

void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {}

bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {}
intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) {}

void mlirContextAppendDialectRegistry(MlirContext ctx,
                                      MlirDialectRegistry registry) {}

// TODO: expose a cheaper way than constructing + sorting a vector only to take
// its size.
intptr_t mlirContextGetNumLoadedDialects(MlirContext context) {}

MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
                                        MlirStringRef name) {}

bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {}

void mlirContextEnableMultithreading(MlirContext context, bool enable) {}

void mlirContextLoadAllAvailableDialects(MlirContext context) {}

void mlirContextSetThreadPool(MlirContext context,
                              MlirLlvmThreadPool threadPool) {}

//===----------------------------------------------------------------------===//
// Dialect API.
//===----------------------------------------------------------------------===//

MlirContext mlirDialectGetContext(MlirDialect dialect) {}

bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {}

MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) {}

//===----------------------------------------------------------------------===//
// DialectRegistry API.
//===----------------------------------------------------------------------===//

MlirDialectRegistry mlirDialectRegistryCreate() {}

void mlirDialectRegistryDestroy(MlirDialectRegistry registry) {}

//===----------------------------------------------------------------------===//
// AsmState API.
//===----------------------------------------------------------------------===//

MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op,
                                            MlirOpPrintingFlags flags) {}

static Operation *findParent(Operation *op, bool shouldUseLocalScope) {}

MlirAsmState mlirAsmStateCreateForValue(MlirValue value,
                                        MlirOpPrintingFlags flags) {}

/// Destroys printing flags created with mlirAsmStateCreate.
void mlirAsmStateDestroy(MlirAsmState state) {}

//===----------------------------------------------------------------------===//
// Printing flags API.
//===----------------------------------------------------------------------===//

MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {}

void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {}

void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
                                                intptr_t largeElementLimit) {}

void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags,
                                                 intptr_t largeResourceLimit) {}

void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable,
                                        bool prettyForm) {}

void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {}

void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {}

void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags) {}

void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags) {}
//===----------------------------------------------------------------------===//
// Bytecode printing flags API.
//===----------------------------------------------------------------------===//

MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate() {}

void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config) {}

void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags,
                                                int64_t version) {}

//===----------------------------------------------------------------------===//
// Location API.
//===----------------------------------------------------------------------===//

MlirAttribute mlirLocationGetAttribute(MlirLocation location) {}

MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {}

MlirLocation mlirLocationFileLineColGet(MlirContext context,
                                        MlirStringRef filename, unsigned line,
                                        unsigned col) {}

MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {}

MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
                                  MlirLocation const *locations,
                                  MlirAttribute metadata) {}

MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
                                 MlirLocation childLoc) {}

MlirLocation mlirLocationUnknownGet(MlirContext context) {}

bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {}

MlirContext mlirLocationGetContext(MlirLocation location) {}

void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
                       void *userData) {}

//===----------------------------------------------------------------------===//
// Module API.
//===----------------------------------------------------------------------===//

MlirModule mlirModuleCreateEmpty(MlirLocation location) {}

MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {}

MlirContext mlirModuleGetContext(MlirModule module) {}

MlirBlock mlirModuleGetBody(MlirModule module) {}

void mlirModuleDestroy(MlirModule module) {}

MlirOperation mlirModuleGetOperation(MlirModule module) {}

MlirModule mlirModuleFromOperation(MlirOperation op) {}

//===----------------------------------------------------------------------===//
// Operation state API.
//===----------------------------------------------------------------------===//

MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {}

#define APPEND_ELEMS(type, sizeName, elemName)

void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
                                  MlirType const *results) {}

void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
                                   MlirValue const *operands) {}
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
                                       MlirRegion const *regions) {}
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
                                     MlirBlock const *successors) {}
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
                                     MlirNamedAttribute const *attributes) {}

void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {}

//===----------------------------------------------------------------------===//
// Operation API.
//===----------------------------------------------------------------------===//

static LogicalResult inferOperationTypes(OperationState &state) {}

MlirOperation mlirOperationCreate(MlirOperationState *state) {}

MlirOperation mlirOperationCreateParse(MlirContext context,
                                       MlirStringRef sourceStr,
                                       MlirStringRef sourceName) {}

MlirOperation mlirOperationClone(MlirOperation op) {}

void mlirOperationDestroy(MlirOperation op) {}

void mlirOperationRemoveFromParent(MlirOperation op) {}

bool mlirOperationEqual(MlirOperation op, MlirOperation other) {}

MlirContext mlirOperationGetContext(MlirOperation op) {}

MlirLocation mlirOperationGetLocation(MlirOperation op) {}

MlirTypeID mlirOperationGetTypeID(MlirOperation op) {}

MlirIdentifier mlirOperationGetName(MlirOperation op) {}

MlirBlock mlirOperationGetBlock(MlirOperation op) {}

MlirOperation mlirOperationGetParentOperation(MlirOperation op) {}

intptr_t mlirOperationGetNumRegions(MlirOperation op) {}

MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {}

MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {}

MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {}

MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {}

intptr_t mlirOperationGetNumOperands(MlirOperation op) {}

MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {}

void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
                             MlirValue newValue) {}

void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands,
                              MlirValue const *operands) {}

intptr_t mlirOperationGetNumResults(MlirOperation op) {}

MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {}

intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {}

MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {}

MLIR_CAPI_EXPORTED bool
mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name) {}

MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
                                                      MlirStringRef name) {}

void mlirOperationSetInherentAttributeByName(MlirOperation op,
                                             MlirStringRef name,
                                             MlirAttribute attr) {}

intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op) {}

MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op,
                                                        intptr_t pos) {}

MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
                                                         MlirStringRef name) {}

void mlirOperationSetDiscardableAttributeByName(MlirOperation op,
                                                MlirStringRef name,
                                                MlirAttribute attr) {}

bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op,
                                                   MlirStringRef name) {}

void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos,
                               MlirBlock block) {}

intptr_t mlirOperationGetNumAttributes(MlirOperation op) {}

MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {}

MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
                                              MlirStringRef name) {}

void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
                                     MlirAttribute attr) {}

bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {}

void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
                        void *userData) {}

void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
                                 MlirStringCallback callback, void *userData) {}

void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state,
                                 MlirStringCallback callback, void *userData) {}

void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
                                void *userData) {}

MlirLogicalResult mlirOperationWriteBytecodeWithConfig(
    MlirOperation op, MlirBytecodeWriterConfig config,
    MlirStringCallback callback, void *userData) {}

void mlirOperationDump(MlirOperation op) {}

bool mlirOperationVerify(MlirOperation op) {}

void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {}

void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {}

static mlir::WalkResult unwrap(MlirWalkResult result) {}

void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
                       void *userData, MlirWalkOrder walkOrder) {}

//===----------------------------------------------------------------------===//
// Region API.
//===----------------------------------------------------------------------===//

MlirRegion mlirRegionCreate() {}

bool mlirRegionEqual(MlirRegion region, MlirRegion other) {}

MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {}

void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {}

void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
                                MlirBlock block) {}

void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
                                     MlirBlock block) {}

void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
                                      MlirBlock block) {}

void mlirRegionDestroy(MlirRegion region) {}

void mlirRegionTakeBody(MlirRegion target, MlirRegion source) {}

//===----------------------------------------------------------------------===//
// Block API.
//===----------------------------------------------------------------------===//

MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
                          MlirLocation const *locs) {}

bool mlirBlockEqual(MlirBlock block, MlirBlock other) {}

MlirOperation mlirBlockGetParentOperation(MlirBlock block) {}

MlirRegion mlirBlockGetParentRegion(MlirBlock block) {}

MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {}

MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {}

MlirOperation mlirBlockGetTerminator(MlirBlock block) {}

void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {}

void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
                                   MlirOperation operation) {}

void mlirBlockInsertOwnedOperationAfter(MlirBlock block,
                                        MlirOperation reference,
                                        MlirOperation operation) {}

void mlirBlockInsertOwnedOperationBefore(MlirBlock block,
                                         MlirOperation reference,
                                         MlirOperation operation) {}

void mlirBlockDestroy(MlirBlock block) {}

void mlirBlockDetach(MlirBlock block) {}

intptr_t mlirBlockGetNumArguments(MlirBlock block) {}

MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
                               MlirLocation loc) {}

void mlirBlockEraseArgument(MlirBlock block, unsigned index) {}

MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
                                  MlirLocation loc) {}

MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {}

void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
                    void *userData) {}

//===----------------------------------------------------------------------===//
// Value API.
//===----------------------------------------------------------------------===//

bool mlirValueEqual(MlirValue value1, MlirValue value2) {}

bool mlirValueIsABlockArgument(MlirValue value) {}

bool mlirValueIsAOpResult(MlirValue value) {}

MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {}

intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {}

void mlirBlockArgumentSetType(MlirValue value, MlirType type) {}

MlirOperation mlirOpResultGetOwner(MlirValue value) {}

intptr_t mlirOpResultGetResultNumber(MlirValue value) {}

MlirType mlirValueGetType(MlirValue value) {}

void mlirValueSetType(MlirValue value, MlirType type) {}

void mlirValueDump(MlirValue value) {}

void mlirValuePrint(MlirValue value, MlirStringCallback callback,
                    void *userData) {}

void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state,
                             MlirStringCallback callback, void *userData) {}

MlirOpOperand mlirValueGetFirstUse(MlirValue value) {}

void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {}

//===----------------------------------------------------------------------===//
// OpOperand API.
//===----------------------------------------------------------------------===//

bool mlirOpOperandIsNull(MlirOpOperand opOperand) {}

MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {}

MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) {}

unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {}

MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {}

//===----------------------------------------------------------------------===//
// Type API.
//===----------------------------------------------------------------------===//

MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {}

MlirContext mlirTypeGetContext(MlirType type) {}

MlirTypeID mlirTypeGetTypeID(MlirType type) {}

MlirDialect mlirTypeGetDialect(MlirType type) {}

bool mlirTypeEqual(MlirType t1, MlirType t2) {}

void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {}

void mlirTypeDump(MlirType type) {}

//===----------------------------------------------------------------------===//
// Attribute API.
//===----------------------------------------------------------------------===//

MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {}

MlirContext mlirAttributeGetContext(MlirAttribute attribute) {}

MlirType mlirAttributeGetType(MlirAttribute attribute) {}

MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {}

MlirDialect mlirAttributeGetDialect(MlirAttribute attr) {}

bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {}

void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
                        void *userData) {}

void mlirAttributeDump(MlirAttribute attr) {}

MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name,
                                         MlirAttribute attr) {}

//===----------------------------------------------------------------------===//
// Identifier API.
//===----------------------------------------------------------------------===//

MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {}

MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {}

bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {}

MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {}

//===----------------------------------------------------------------------===//
// Symbol and SymbolTable API.
//===----------------------------------------------------------------------===//

MlirStringRef mlirSymbolTableGetSymbolAttributeName() {}

MlirStringRef mlirSymbolTableGetVisibilityAttributeName() {}

MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {}

void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {}

MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
                                    MlirStringRef name) {}

MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
                                    MlirOperation operation) {}

void mlirSymbolTableErase(MlirSymbolTable symbolTable,
                          MlirOperation operation) {}

MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol,
                                                      MlirStringRef newSymbol,
                                                      MlirOperation from) {}

void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
                                     void (*callback)(MlirOperation, bool,
                                                      void *userData),
                                     void *userData) {}