175f239e9SAlex Zinenko //===- IR.cpp - C Interface for Core MLIR APIs ----------------------------===//
275f239e9SAlex Zinenko //
375f239e9SAlex Zinenko // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
475f239e9SAlex Zinenko // See https://llvm.org/LICENSE.txt for license information.
575f239e9SAlex Zinenko // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
675f239e9SAlex Zinenko //
775f239e9SAlex Zinenko //===----------------------------------------------------------------------===//
875f239e9SAlex Zinenko
975f239e9SAlex Zinenko #include "mlir-c/IR.h"
1064c0c9f0SAlex Zinenko #include "mlir-c/Support.h"
1175f239e9SAlex Zinenko
12*c60b897dSRiver Riddle #include "mlir/AsmParser/AsmParser.h"
1374f57784SAlex Zinenko #include "mlir/CAPI/IR.h"
1464c0c9f0SAlex Zinenko #include "mlir/CAPI/Support.h"
15b76f523bSzhanghb97 #include "mlir/CAPI/Utils.h"
1675f239e9SAlex Zinenko #include "mlir/IR/Attributes.h"
1765fcddffSRiver Riddle #include "mlir/IR/BuiltinOps.h"
18f9dc2b70SMehdi Amini #include "mlir/IR/Dialect.h"
19f7bf8a86SJacques Pienaar #include "mlir/IR/Location.h"
2075f239e9SAlex Zinenko #include "mlir/IR/Operation.h"
2175f239e9SAlex Zinenko #include "mlir/IR/Types.h"
221c215949SMehdi Amini #include "mlir/IR/Verifier.h"
2352586c46SStella Laurenzo #include "mlir/Interfaces/InferTypeOpInterface.h"
249eaff423SRiver Riddle #include "mlir/Parser/Parser.h"
2575f239e9SAlex Zinenko
26caa159f0SNicolas Vasilache #include "llvm/Support/Debug.h"
27782a97a9SDaniel Resnick #include <cstddef>
28caa159f0SNicolas Vasilache
2975f239e9SAlex Zinenko using namespace mlir;
3075f239e9SAlex Zinenko
31c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
32c7994bd9SMehdi Amini // Context API.
33c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
3475f239e9SAlex Zinenko
mlirContextCreate()3575f239e9SAlex Zinenko MlirContext mlirContextCreate() {
36e7021232SMehdi Amini auto *context = new MLIRContext;
3775f239e9SAlex Zinenko return wrap(context);
3875f239e9SAlex Zinenko }
3975f239e9SAlex Zinenko
mlirContextEqual(MlirContext ctx1,MlirContext ctx2)408b6bea9bSGeorge bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
412d1362e0SStella Laurenzo return unwrap(ctx1) == unwrap(ctx2);
422d1362e0SStella Laurenzo }
432d1362e0SStella Laurenzo
mlirContextDestroy(MlirContext context)4475f239e9SAlex Zinenko void mlirContextDestroy(MlirContext context) { delete unwrap(context); }
4575f239e9SAlex Zinenko
mlirContextSetAllowUnregisteredDialects(MlirContext context,bool allow)4662195b75SStella Laurenzo void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {
474cf754c4SStella Laurenzo unwrap(context)->allowUnregisteredDialects(allow);
484cf754c4SStella Laurenzo }
494cf754c4SStella Laurenzo
mlirContextGetAllowUnregisteredDialects(MlirContext context)5062195b75SStella Laurenzo bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {
514cf754c4SStella Laurenzo return unwrap(context)->allowsUnregisteredDialects();
524cf754c4SStella Laurenzo }
mlirContextGetNumRegisteredDialects(MlirContext context)5364c0c9f0SAlex Zinenko intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) {
5464c0c9f0SAlex Zinenko return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size());
5564c0c9f0SAlex Zinenko }
5664c0c9f0SAlex Zinenko
mlirContextAppendDialectRegistry(MlirContext ctx,MlirDialectRegistry registry)5797fc5682SDaniel Resnick void mlirContextAppendDialectRegistry(MlirContext ctx,
5897fc5682SDaniel Resnick MlirDialectRegistry registry) {
5997fc5682SDaniel Resnick unwrap(ctx)->appendDialectRegistry(*unwrap(registry));
6097fc5682SDaniel Resnick }
6197fc5682SDaniel Resnick
6264c0c9f0SAlex Zinenko // TODO: expose a cheaper way than constructing + sorting a vector only to take
6364c0c9f0SAlex Zinenko // its size.
mlirContextGetNumLoadedDialects(MlirContext context)6464c0c9f0SAlex Zinenko intptr_t mlirContextGetNumLoadedDialects(MlirContext context) {
6564c0c9f0SAlex Zinenko return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size());
6664c0c9f0SAlex Zinenko }
6764c0c9f0SAlex Zinenko
mlirContextGetOrLoadDialect(MlirContext context,MlirStringRef name)6864c0c9f0SAlex Zinenko MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
6964c0c9f0SAlex Zinenko MlirStringRef name) {
7064c0c9f0SAlex Zinenko return wrap(unwrap(context)->getOrLoadDialect(unwrap(name)));
7164c0c9f0SAlex Zinenko }
7264c0c9f0SAlex Zinenko
mlirContextIsRegisteredOperation(MlirContext context,MlirStringRef name)739a9214faSStella Laurenzo bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
749a9214faSStella Laurenzo return unwrap(context)->isOperationRegistered(unwrap(name));
759a9214faSStella Laurenzo }
769a9214faSStella Laurenzo
mlirContextEnableMultithreading(MlirContext context,bool enable)77caa159f0SNicolas Vasilache void mlirContextEnableMultithreading(MlirContext context, bool enable) {
78caa159f0SNicolas Vasilache return unwrap(context)->enableMultithreading(enable);
79caa159f0SNicolas Vasilache }
80caa159f0SNicolas Vasilache
mlirContextLoadAllAvailableDialects(MlirContext context)815e83a5b4SStella Laurenzo void mlirContextLoadAllAvailableDialects(MlirContext context) {
825e83a5b4SStella Laurenzo unwrap(context)->loadAllAvailableDialects();
835e83a5b4SStella Laurenzo }
845e83a5b4SStella Laurenzo
85c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
86c7994bd9SMehdi Amini // Dialect API.
87c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
8864c0c9f0SAlex Zinenko
mlirDialectGetContext(MlirDialect dialect)8964c0c9f0SAlex Zinenko MlirContext mlirDialectGetContext(MlirDialect dialect) {
9064c0c9f0SAlex Zinenko return wrap(unwrap(dialect)->getContext());
9164c0c9f0SAlex Zinenko }
9264c0c9f0SAlex Zinenko
mlirDialectEqual(MlirDialect dialect1,MlirDialect dialect2)938b6bea9bSGeorge bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
9464c0c9f0SAlex Zinenko return unwrap(dialect1) == unwrap(dialect2);
9564c0c9f0SAlex Zinenko }
9664c0c9f0SAlex Zinenko
mlirDialectGetNamespace(MlirDialect dialect)9764c0c9f0SAlex Zinenko MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) {
9864c0c9f0SAlex Zinenko return wrap(unwrap(dialect)->getNamespace());
9964c0c9f0SAlex Zinenko }
1004cf754c4SStella Laurenzo
101c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
10297fc5682SDaniel Resnick // DialectRegistry API.
10397fc5682SDaniel Resnick //===----------------------------------------------------------------------===//
10497fc5682SDaniel Resnick
mlirDialectRegistryCreate()10597fc5682SDaniel Resnick MlirDialectRegistry mlirDialectRegistryCreate() {
10697fc5682SDaniel Resnick return wrap(new DialectRegistry());
10797fc5682SDaniel Resnick }
10897fc5682SDaniel Resnick
mlirDialectRegistryDestroy(MlirDialectRegistry registry)10997fc5682SDaniel Resnick void mlirDialectRegistryDestroy(MlirDialectRegistry registry) {
11097fc5682SDaniel Resnick delete unwrap(registry);
11197fc5682SDaniel Resnick }
11297fc5682SDaniel Resnick
11397fc5682SDaniel Resnick //===----------------------------------------------------------------------===//
114c7994bd9SMehdi Amini // Printing flags API.
115c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
11674a58ec9SStella Laurenzo
mlirOpPrintingFlagsCreate()11774a58ec9SStella Laurenzo MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {
11874a58ec9SStella Laurenzo return wrap(new OpPrintingFlags());
11974a58ec9SStella Laurenzo }
12074a58ec9SStella Laurenzo
mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)12174a58ec9SStella Laurenzo void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {
12274a58ec9SStella Laurenzo delete unwrap(flags);
12374a58ec9SStella Laurenzo }
12474a58ec9SStella Laurenzo
mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,intptr_t largeElementLimit)12574a58ec9SStella Laurenzo void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
12674a58ec9SStella Laurenzo intptr_t largeElementLimit) {
12774a58ec9SStella Laurenzo unwrap(flags)->elideLargeElementsAttrs(largeElementLimit);
12874a58ec9SStella Laurenzo }
12974a58ec9SStella Laurenzo
mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags,bool prettyForm)13074a58ec9SStella Laurenzo void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags,
13162195b75SStella Laurenzo bool prettyForm) {
13274a58ec9SStella Laurenzo unwrap(flags)->enableDebugInfo(/*prettyForm=*/prettyForm);
13374a58ec9SStella Laurenzo }
13474a58ec9SStella Laurenzo
mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)13574a58ec9SStella Laurenzo void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
13674a58ec9SStella Laurenzo unwrap(flags)->printGenericOpForm();
13774a58ec9SStella Laurenzo }
13874a58ec9SStella Laurenzo
mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags)13974a58ec9SStella Laurenzo void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
14074a58ec9SStella Laurenzo unwrap(flags)->useLocalScope();
14174a58ec9SStella Laurenzo }
14274a58ec9SStella Laurenzo
143c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
144c7994bd9SMehdi Amini // Location API.
145c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
14675f239e9SAlex Zinenko
mlirLocationFileLineColGet(MlirContext context,MlirStringRef filename,unsigned line,unsigned col)14775f239e9SAlex Zinenko MlirLocation mlirLocationFileLineColGet(MlirContext context,
148df9ae599SGeorge MlirStringRef filename, unsigned line,
14975f239e9SAlex Zinenko unsigned col) {
150a4bb667dSRiver Riddle return wrap(Location(
151a4bb667dSRiver Riddle FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
15275f239e9SAlex Zinenko }
15375f239e9SAlex Zinenko
mlirLocationCallSiteGet(MlirLocation callee,MlirLocation caller)1544a327bd2SGeorge MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
155a4bb667dSRiver Riddle return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
1564a327bd2SGeorge }
1574a327bd2SGeorge
mlirLocationFusedGet(MlirContext ctx,intptr_t nLocations,MlirLocation const * locations,MlirAttribute metadata)1580a1e569dSJacques Pienaar MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
1590a1e569dSJacques Pienaar MlirLocation const *locations,
1600a1e569dSJacques Pienaar MlirAttribute metadata) {
1610a1e569dSJacques Pienaar SmallVector<Location, 4> locs;
1620a1e569dSJacques Pienaar ArrayRef<Location> unwrappedLocs = unwrapList(nLocations, locations, locs);
1630a1e569dSJacques Pienaar return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
1640a1e569dSJacques Pienaar }
1650a1e569dSJacques Pienaar
mlirLocationNameGet(MlirContext context,MlirStringRef name,MlirLocation childLoc)166f7bf8a86SJacques Pienaar MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
167f7bf8a86SJacques Pienaar MlirLocation childLoc) {
168f7bf8a86SJacques Pienaar if (mlirLocationIsNull(childLoc))
169f7bf8a86SJacques Pienaar return wrap(
170195730a6SRiver Riddle Location(NameLoc::get(StringAttr::get(unwrap(context), unwrap(name)))));
171f7bf8a86SJacques Pienaar return wrap(Location(NameLoc::get(
172195730a6SRiver Riddle StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
173f7bf8a86SJacques Pienaar }
174f7bf8a86SJacques Pienaar
mlirLocationUnknownGet(MlirContext context)17575f239e9SAlex Zinenko MlirLocation mlirLocationUnknownGet(MlirContext context) {
176a4bb667dSRiver Riddle return wrap(Location(UnknownLoc::get(unwrap(context))));
17775f239e9SAlex Zinenko }
17875f239e9SAlex Zinenko
mlirLocationEqual(MlirLocation l1,MlirLocation l2)17962195b75SStella Laurenzo bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
180bd2083c2SStella Laurenzo return unwrap(l1) == unwrap(l2);
181bd2083c2SStella Laurenzo }
182bd2083c2SStella Laurenzo
mlirLocationGetContext(MlirLocation location)18385185b61SStella Laurenzo MlirContext mlirLocationGetContext(MlirLocation location) {
18485185b61SStella Laurenzo return wrap(unwrap(location).getContext());
18585185b61SStella Laurenzo }
18685185b61SStella Laurenzo
mlirLocationPrint(MlirLocation location,MlirStringCallback callback,void * userData)187da562974SAlex Zinenko void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
188321aa19eSAlex Zinenko void *userData) {
189b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
190321aa19eSAlex Zinenko unwrap(location).print(stream);
191321aa19eSAlex Zinenko }
192321aa19eSAlex Zinenko
193c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
194c7994bd9SMehdi Amini // Module API.
195c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
19675f239e9SAlex Zinenko
mlirModuleCreateEmpty(MlirLocation location)19775f239e9SAlex Zinenko MlirModule mlirModuleCreateEmpty(MlirLocation location) {
19875f239e9SAlex Zinenko return wrap(ModuleOp::create(unwrap(location)));
19975f239e9SAlex Zinenko }
20075f239e9SAlex Zinenko
mlirModuleCreateParse(MlirContext context,MlirStringRef module)201df9ae599SGeorge MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
2028f66ab1cSSanjoy Das OwningOpRef<ModuleOp> owning =
203dfaadf6bSChristian Sigg parseSourceString<ModuleOp>(unwrap(module), unwrap(context));
20495b77f2eSStella Laurenzo if (!owning)
20595b77f2eSStella Laurenzo return MlirModule{nullptr};
20675f239e9SAlex Zinenko return MlirModule{owning.release().getOperation()};
20775f239e9SAlex Zinenko }
20875f239e9SAlex Zinenko
mlirModuleGetContext(MlirModule module)20985185b61SStella Laurenzo MlirContext mlirModuleGetContext(MlirModule module) {
21085185b61SStella Laurenzo return wrap(unwrap(module).getContext());
21185185b61SStella Laurenzo }
21285185b61SStella Laurenzo
mlirModuleGetBody(MlirModule module)21372023442SMehdi Amini MlirBlock mlirModuleGetBody(MlirModule module) {
21472023442SMehdi Amini return wrap(unwrap(module).getBody());
21572023442SMehdi Amini }
21672023442SMehdi Amini
mlirModuleDestroy(MlirModule module)21775f239e9SAlex Zinenko void mlirModuleDestroy(MlirModule module) {
2188f66ab1cSSanjoy Das // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
2198f66ab1cSSanjoy Das // called.
2208f66ab1cSSanjoy Das OwningOpRef<ModuleOp>(unwrap(module));
22175f239e9SAlex Zinenko }
22275f239e9SAlex Zinenko
mlirModuleGetOperation(MlirModule module)22375f239e9SAlex Zinenko MlirOperation mlirModuleGetOperation(MlirModule module) {
22475f239e9SAlex Zinenko return wrap(unwrap(module).getOperation());
22575f239e9SAlex Zinenko }
22675f239e9SAlex Zinenko
mlirModuleFromOperation(MlirOperation op)227d89602edSAdam Paszke MlirModule mlirModuleFromOperation(MlirOperation op) {
228d89602edSAdam Paszke return wrap(dyn_cast<ModuleOp>(unwrap(op)));
229d89602edSAdam Paszke }
230d89602edSAdam Paszke
231c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
232c7994bd9SMehdi Amini // Operation state API.
233c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
23475f239e9SAlex Zinenko
mlirOperationStateGet(MlirStringRef name,MlirLocation loc)235df9ae599SGeorge MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {
23675f239e9SAlex Zinenko MlirOperationState state;
23775f239e9SAlex Zinenko state.name = name;
23875f239e9SAlex Zinenko state.location = loc;
23975f239e9SAlex Zinenko state.nResults = 0;
24075f239e9SAlex Zinenko state.results = nullptr;
24175f239e9SAlex Zinenko state.nOperands = 0;
24275f239e9SAlex Zinenko state.operands = nullptr;
24375f239e9SAlex Zinenko state.nRegions = 0;
24475f239e9SAlex Zinenko state.regions = nullptr;
24575f239e9SAlex Zinenko state.nSuccessors = 0;
24675f239e9SAlex Zinenko state.successors = nullptr;
24775f239e9SAlex Zinenko state.nAttributes = 0;
24875f239e9SAlex Zinenko state.attributes = nullptr;
24952586c46SStella Laurenzo state.enableResultTypeInference = false;
25075f239e9SAlex Zinenko return state;
25175f239e9SAlex Zinenko }
25275f239e9SAlex Zinenko
25375f239e9SAlex Zinenko #define APPEND_ELEMS(type, sizeName, elemName) \
25475f239e9SAlex Zinenko state->elemName = \
25575f239e9SAlex Zinenko (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
25675f239e9SAlex Zinenko memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
25775f239e9SAlex Zinenko state->sizeName += n;
25875f239e9SAlex Zinenko
mlirOperationStateAddResults(MlirOperationState * state,intptr_t n,MlirType const * results)259af838584SAlex Zinenko void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
260beb889c1SGeorge MlirType const *results) {
26175f239e9SAlex Zinenko APPEND_ELEMS(MlirType, nResults, results);
26275f239e9SAlex Zinenko }
26375f239e9SAlex Zinenko
mlirOperationStateAddOperands(MlirOperationState * state,intptr_t n,MlirValue const * operands)264af838584SAlex Zinenko void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
265beb889c1SGeorge MlirValue const *operands) {
26675f239e9SAlex Zinenko APPEND_ELEMS(MlirValue, nOperands, operands);
26775f239e9SAlex Zinenko }
mlirOperationStateAddOwnedRegions(MlirOperationState * state,intptr_t n,MlirRegion const * regions)268af838584SAlex Zinenko void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
269beb889c1SGeorge MlirRegion const *regions) {
27075f239e9SAlex Zinenko APPEND_ELEMS(MlirRegion, nRegions, regions);
27175f239e9SAlex Zinenko }
mlirOperationStateAddSuccessors(MlirOperationState * state,intptr_t n,MlirBlock const * successors)272af838584SAlex Zinenko void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
273beb889c1SGeorge MlirBlock const *successors) {
27475f239e9SAlex Zinenko APPEND_ELEMS(MlirBlock, nSuccessors, successors);
27575f239e9SAlex Zinenko }
mlirOperationStateAddAttributes(MlirOperationState * state,intptr_t n,MlirNamedAttribute const * attributes)276af838584SAlex Zinenko void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
277beb889c1SGeorge MlirNamedAttribute const *attributes) {
27875f239e9SAlex Zinenko APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
27975f239e9SAlex Zinenko }
28075f239e9SAlex Zinenko
mlirOperationStateEnableResultTypeInference(MlirOperationState * state)28152586c46SStella Laurenzo void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {
28252586c46SStella Laurenzo state->enableResultTypeInference = true;
28352586c46SStella Laurenzo }
28452586c46SStella Laurenzo
285c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
286c7994bd9SMehdi Amini // Operation API.
287c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
28875f239e9SAlex Zinenko
inferOperationTypes(OperationState & state)28952586c46SStella Laurenzo static LogicalResult inferOperationTypes(OperationState &state) {
29052586c46SStella Laurenzo MLIRContext *context = state.getContext();
291edc6c0ecSRiver Riddle Optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
292edc6c0ecSRiver Riddle if (!info) {
29352586c46SStella Laurenzo emitError(state.location)
29452586c46SStella Laurenzo << "type inference was requested for the operation " << state.name
29552586c46SStella Laurenzo << ", but the operation was not registered. Ensure that the dialect "
29652586c46SStella Laurenzo "containing the operation is linked into MLIR and registered with "
29752586c46SStella Laurenzo "the context";
29852586c46SStella Laurenzo return failure();
29952586c46SStella Laurenzo }
30052586c46SStella Laurenzo
30152586c46SStella Laurenzo // Fallback to inference via an op interface.
302edc6c0ecSRiver Riddle auto *inferInterface = info->getInterface<InferTypeOpInterface>();
30352586c46SStella Laurenzo if (!inferInterface) {
30452586c46SStella Laurenzo emitError(state.location)
30552586c46SStella Laurenzo << "type inference was requested for the operation " << state.name
30652586c46SStella Laurenzo << ", but the operation does not support type inference. Result "
30752586c46SStella Laurenzo "types must be specified explicitly.";
30852586c46SStella Laurenzo return failure();
30952586c46SStella Laurenzo }
31052586c46SStella Laurenzo
31152586c46SStella Laurenzo if (succeeded(inferInterface->inferReturnTypes(
31252586c46SStella Laurenzo context, state.location, state.operands,
31352586c46SStella Laurenzo state.attributes.getDictionary(context), state.regions, state.types)))
31452586c46SStella Laurenzo return success();
31552586c46SStella Laurenzo
31652586c46SStella Laurenzo // Diagnostic emitted by interface.
31752586c46SStella Laurenzo return failure();
31852586c46SStella Laurenzo }
31952586c46SStella Laurenzo
mlirOperationCreate(MlirOperationState * state)32052586c46SStella Laurenzo MlirOperation mlirOperationCreate(MlirOperationState *state) {
32175f239e9SAlex Zinenko assert(state);
322df9ae599SGeorge OperationState cppState(unwrap(state->location), unwrap(state->name));
32375f239e9SAlex Zinenko SmallVector<Type, 4> resultStorage;
32475f239e9SAlex Zinenko SmallVector<Value, 8> operandStorage;
32575f239e9SAlex Zinenko SmallVector<Block *, 2> successorStorage;
32675f239e9SAlex Zinenko cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
32775f239e9SAlex Zinenko cppState.addOperands(
32875f239e9SAlex Zinenko unwrapList(state->nOperands, state->operands, operandStorage));
32975f239e9SAlex Zinenko cppState.addSuccessors(
33075f239e9SAlex Zinenko unwrapList(state->nSuccessors, state->successors, successorStorage));
33175f239e9SAlex Zinenko
33275f239e9SAlex Zinenko cppState.attributes.reserve(state->nAttributes);
333af838584SAlex Zinenko for (intptr_t i = 0; i < state->nAttributes; ++i)
334df9ae599SGeorge cppState.addAttribute(unwrap(state->attributes[i].name),
33575f239e9SAlex Zinenko unwrap(state->attributes[i].attribute));
33675f239e9SAlex Zinenko
337af838584SAlex Zinenko for (intptr_t i = 0; i < state->nRegions; ++i)
33875f239e9SAlex Zinenko cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
33975f239e9SAlex Zinenko
34069eb7e36SJing Pu free(state->results);
34169eb7e36SJing Pu free(state->operands);
34269eb7e36SJing Pu free(state->successors);
343af838584SAlex Zinenko free(state->regions);
34469eb7e36SJing Pu free(state->attributes);
34552586c46SStella Laurenzo
34652586c46SStella Laurenzo // Infer result types.
34752586c46SStella Laurenzo if (state->enableResultTypeInference) {
34852586c46SStella Laurenzo assert(cppState.types.empty() &&
34952586c46SStella Laurenzo "result type inference enabled and result types provided");
35052586c46SStella Laurenzo if (failed(inferOperationTypes(cppState)))
35152586c46SStella Laurenzo return {nullptr};
35252586c46SStella Laurenzo }
35352586c46SStella Laurenzo
35452586c46SStella Laurenzo MlirOperation result = wrap(Operation::create(cppState));
355af838584SAlex Zinenko return result;
35675f239e9SAlex Zinenko }
35775f239e9SAlex Zinenko
mlirOperationClone(MlirOperation op)358d3e6c2ddSGeorge MlirOperation mlirOperationClone(MlirOperation op) {
359d3e6c2ddSGeorge return wrap(unwrap(op)->clone());
360d3e6c2ddSGeorge }
361d3e6c2ddSGeorge
mlirOperationDestroy(MlirOperation op)36275f239e9SAlex Zinenko void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
36375f239e9SAlex Zinenko
mlirOperationRemoveFromParent(MlirOperation op)36424685aaeSAlex Zinenko void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
36524685aaeSAlex Zinenko
mlirOperationEqual(MlirOperation op,MlirOperation other)3668b6bea9bSGeorge bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
36739613c2cSAlex Zinenko return unwrap(op) == unwrap(other);
36839613c2cSAlex Zinenko }
36939613c2cSAlex Zinenko
mlirOperationGetContext(MlirOperation op)3708f130f10SGeorge MlirContext mlirOperationGetContext(MlirOperation op) {
3718f130f10SGeorge return wrap(unwrap(op)->getContext());
3728f130f10SGeorge }
3738f130f10SGeorge
mlirOperationGetLocation(MlirOperation op)374d5429a13Srkayaith MlirLocation mlirOperationGetLocation(MlirOperation op) {
375d5429a13Srkayaith return wrap(unwrap(op)->getLoc());
376d5429a13Srkayaith }
377d5429a13Srkayaith
mlirOperationGetTypeID(MlirOperation op)378782a97a9SDaniel Resnick MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
379edc6c0ecSRiver Riddle if (auto info = unwrap(op)->getRegisteredInfo())
380edc6c0ecSRiver Riddle return wrap(info->getTypeID());
381782a97a9SDaniel Resnick return {nullptr};
382782a97a9SDaniel Resnick }
383782a97a9SDaniel Resnick
mlirOperationGetName(MlirOperation op)384b85f2f5cSStella Laurenzo MlirIdentifier mlirOperationGetName(MlirOperation op) {
385b85f2f5cSStella Laurenzo return wrap(unwrap(op)->getName().getIdentifier());
386b85f2f5cSStella Laurenzo }
387b85f2f5cSStella Laurenzo
mlirOperationGetBlock(MlirOperation op)388c645ea5eSStella Laurenzo MlirBlock mlirOperationGetBlock(MlirOperation op) {
389c645ea5eSStella Laurenzo return wrap(unwrap(op)->getBlock());
390c645ea5eSStella Laurenzo }
391c645ea5eSStella Laurenzo
mlirOperationGetParentOperation(MlirOperation op)392c645ea5eSStella Laurenzo MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
393c645ea5eSStella Laurenzo return wrap(unwrap(op)->getParentOp());
394c645ea5eSStella Laurenzo }
395c645ea5eSStella Laurenzo
mlirOperationGetNumRegions(MlirOperation op)396af838584SAlex Zinenko intptr_t mlirOperationGetNumRegions(MlirOperation op) {
397af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(op)->getNumRegions());
39875f239e9SAlex Zinenko }
39975f239e9SAlex Zinenko
mlirOperationGetRegion(MlirOperation op,intptr_t pos)400af838584SAlex Zinenko MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
401af838584SAlex Zinenko return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
40275f239e9SAlex Zinenko }
40375f239e9SAlex Zinenko
mlirOperationGetFirstRegion(MlirOperation op)404d1a688ceSJacques Pienaar MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
405d1a688ceSJacques Pienaar Operation *cppOp = unwrap(op);
406d1a688ceSJacques Pienaar if (cppOp->getNumRegions() == 0)
407d1a688ceSJacques Pienaar return wrap(static_cast<Region *>(nullptr));
408d1a688ceSJacques Pienaar return wrap(&cppOp->getRegion(0));
409d1a688ceSJacques Pienaar }
410d1a688ceSJacques Pienaar
mlirRegionGetNextInOperation(MlirRegion region)411d1a688ceSJacques Pienaar MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
412d1a688ceSJacques Pienaar Region *cppRegion = unwrap(region);
413d1a688ceSJacques Pienaar Operation *parent = cppRegion->getParentOp();
414d1a688ceSJacques Pienaar intptr_t next = cppRegion->getRegionNumber() + 1;
415d1a688ceSJacques Pienaar if (parent->getNumRegions() > next)
416d1a688ceSJacques Pienaar return wrap(&parent->getRegion(next));
417d1a688ceSJacques Pienaar return wrap(static_cast<Region *>(nullptr));
418d1a688ceSJacques Pienaar }
419d1a688ceSJacques Pienaar
mlirOperationGetNextInBlock(MlirOperation op)42075f239e9SAlex Zinenko MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
42175f239e9SAlex Zinenko return wrap(unwrap(op)->getNextNode());
42275f239e9SAlex Zinenko }
42375f239e9SAlex Zinenko
mlirOperationGetNumOperands(MlirOperation op)424af838584SAlex Zinenko intptr_t mlirOperationGetNumOperands(MlirOperation op) {
425af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(op)->getNumOperands());
42675f239e9SAlex Zinenko }
42775f239e9SAlex Zinenko
mlirOperationGetOperand(MlirOperation op,intptr_t pos)428af838584SAlex Zinenko MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
429af838584SAlex Zinenko return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
43075f239e9SAlex Zinenko }
43175f239e9SAlex Zinenko
mlirOperationSetOperand(MlirOperation op,intptr_t pos,MlirValue newValue)43263d16d06SMike Urbach void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
43363d16d06SMike Urbach MlirValue newValue) {
43463d16d06SMike Urbach unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
43563d16d06SMike Urbach }
43663d16d06SMike Urbach
mlirOperationGetNumResults(MlirOperation op)437af838584SAlex Zinenko intptr_t mlirOperationGetNumResults(MlirOperation op) {
438af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(op)->getNumResults());
43975f239e9SAlex Zinenko }
44075f239e9SAlex Zinenko
mlirOperationGetResult(MlirOperation op,intptr_t pos)441af838584SAlex Zinenko MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
442af838584SAlex Zinenko return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
44375f239e9SAlex Zinenko }
44475f239e9SAlex Zinenko
mlirOperationGetNumSuccessors(MlirOperation op)445af838584SAlex Zinenko intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
446af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
44775f239e9SAlex Zinenko }
44875f239e9SAlex Zinenko
mlirOperationGetSuccessor(MlirOperation op,intptr_t pos)449af838584SAlex Zinenko MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
450af838584SAlex Zinenko return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
45175f239e9SAlex Zinenko }
45275f239e9SAlex Zinenko
mlirOperationGetNumAttributes(MlirOperation op)453af838584SAlex Zinenko intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
454af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
45575f239e9SAlex Zinenko }
45675f239e9SAlex Zinenko
mlirOperationGetAttribute(MlirOperation op,intptr_t pos)457af838584SAlex Zinenko MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {
45875f239e9SAlex Zinenko NamedAttribute attr = unwrap(op)->getAttrs()[pos];
4590c7890c8SRiver Riddle return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
46075f239e9SAlex Zinenko }
46175f239e9SAlex Zinenko
mlirOperationGetAttributeByName(MlirOperation op,MlirStringRef name)46275f239e9SAlex Zinenko MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
463df9ae599SGeorge MlirStringRef name) {
464df9ae599SGeorge return wrap(unwrap(op)->getAttr(unwrap(name)));
46575f239e9SAlex Zinenko }
46675f239e9SAlex Zinenko
mlirOperationSetAttributeByName(MlirOperation op,MlirStringRef name,MlirAttribute attr)467df9ae599SGeorge void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
4684aa21716SStella Laurenzo MlirAttribute attr) {
469df9ae599SGeorge unwrap(op)->setAttr(unwrap(name), unwrap(attr));
4704aa21716SStella Laurenzo }
4714aa21716SStella Laurenzo
mlirOperationRemoveAttributeByName(MlirOperation op,MlirStringRef name)47262195b75SStella Laurenzo bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {
473fc5cf50eSRiver Riddle return !!unwrap(op)->removeAttr(unwrap(name));
4744aa21716SStella Laurenzo }
4754aa21716SStella Laurenzo
mlirOperationPrint(MlirOperation op,MlirStringCallback callback,void * userData)476da562974SAlex Zinenko void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
477321aa19eSAlex Zinenko void *userData) {
478b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
479321aa19eSAlex Zinenko unwrap(op)->print(stream);
480321aa19eSAlex Zinenko }
481321aa19eSAlex Zinenko
mlirOperationPrintWithFlags(MlirOperation op,MlirOpPrintingFlags flags,MlirStringCallback callback,void * userData)48274a58ec9SStella Laurenzo void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
48374a58ec9SStella Laurenzo MlirStringCallback callback, void *userData) {
48474a58ec9SStella Laurenzo detail::CallbackOstream stream(callback, userData);
48574a58ec9SStella Laurenzo unwrap(op)->print(stream, *unwrap(flags));
48674a58ec9SStella Laurenzo }
48774a58ec9SStella Laurenzo
mlirOperationDump(MlirOperation op)48875f239e9SAlex Zinenko void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
48975f239e9SAlex Zinenko
mlirOperationVerify(MlirOperation op)4901c215949SMehdi Amini bool mlirOperationVerify(MlirOperation op) {
4911c215949SMehdi Amini return succeeded(verify(unwrap(op)));
4921c215949SMehdi Amini }
4931c215949SMehdi Amini
mlirOperationMoveAfter(MlirOperation op,MlirOperation other)49424685aaeSAlex Zinenko void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
49524685aaeSAlex Zinenko return unwrap(op)->moveAfter(unwrap(other));
49624685aaeSAlex Zinenko }
49724685aaeSAlex Zinenko
mlirOperationMoveBefore(MlirOperation op,MlirOperation other)49824685aaeSAlex Zinenko void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
49924685aaeSAlex Zinenko return unwrap(op)->moveBefore(unwrap(other));
50024685aaeSAlex Zinenko }
50124685aaeSAlex Zinenko
502c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
503c7994bd9SMehdi Amini // Region API.
504c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
50575f239e9SAlex Zinenko
mlirRegionCreate()50675f239e9SAlex Zinenko MlirRegion mlirRegionCreate() { return wrap(new Region); }
50775f239e9SAlex Zinenko
mlirRegionEqual(MlirRegion region,MlirRegion other)5088e6c55c9SStella Laurenzo bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
5098e6c55c9SStella Laurenzo return unwrap(region) == unwrap(other);
5108e6c55c9SStella Laurenzo }
5118e6c55c9SStella Laurenzo
mlirRegionGetFirstBlock(MlirRegion region)51275f239e9SAlex Zinenko MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
51375f239e9SAlex Zinenko Region *cppRegion = unwrap(region);
51475f239e9SAlex Zinenko if (cppRegion->empty())
51575f239e9SAlex Zinenko return wrap(static_cast<Block *>(nullptr));
51675f239e9SAlex Zinenko return wrap(&cppRegion->front());
51775f239e9SAlex Zinenko }
51875f239e9SAlex Zinenko
mlirRegionAppendOwnedBlock(MlirRegion region,MlirBlock block)51975f239e9SAlex Zinenko void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
52075f239e9SAlex Zinenko unwrap(region)->push_back(unwrap(block));
52175f239e9SAlex Zinenko }
52275f239e9SAlex Zinenko
mlirRegionInsertOwnedBlock(MlirRegion region,intptr_t pos,MlirBlock block)523af838584SAlex Zinenko void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
52475f239e9SAlex Zinenko MlirBlock block) {
52575f239e9SAlex Zinenko auto &blockList = unwrap(region)->getBlocks();
52675f239e9SAlex Zinenko blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
52775f239e9SAlex Zinenko }
52875f239e9SAlex Zinenko
mlirRegionInsertOwnedBlockAfter(MlirRegion region,MlirBlock reference,MlirBlock block)529c538169eSAlex Zinenko void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
530c538169eSAlex Zinenko MlirBlock block) {
531c538169eSAlex Zinenko Region *cppRegion = unwrap(region);
532c538169eSAlex Zinenko if (mlirBlockIsNull(reference)) {
533c538169eSAlex Zinenko cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
534c538169eSAlex Zinenko return;
535c538169eSAlex Zinenko }
536c538169eSAlex Zinenko
537c538169eSAlex Zinenko assert(unwrap(reference)->getParent() == unwrap(region) &&
538c538169eSAlex Zinenko "expected reference block to belong to the region");
539c538169eSAlex Zinenko cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
540c538169eSAlex Zinenko unwrap(block));
541c538169eSAlex Zinenko }
542c538169eSAlex Zinenko
mlirRegionInsertOwnedBlockBefore(MlirRegion region,MlirBlock reference,MlirBlock block)543c538169eSAlex Zinenko void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
544c538169eSAlex Zinenko MlirBlock block) {
545c538169eSAlex Zinenko if (mlirBlockIsNull(reference))
546c538169eSAlex Zinenko return mlirRegionAppendOwnedBlock(region, block);
547c538169eSAlex Zinenko
548c538169eSAlex Zinenko assert(unwrap(reference)->getParent() == unwrap(region) &&
549c538169eSAlex Zinenko "expected reference block to belong to the region");
550c538169eSAlex Zinenko unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
551c538169eSAlex Zinenko unwrap(block));
552c538169eSAlex Zinenko }
553c538169eSAlex Zinenko
mlirRegionDestroy(MlirRegion region)55475f239e9SAlex Zinenko void mlirRegionDestroy(MlirRegion region) {
55575f239e9SAlex Zinenko delete static_cast<Region *>(region.ptr);
55675f239e9SAlex Zinenko }
55775f239e9SAlex Zinenko
558c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
559c7994bd9SMehdi Amini // Block API.
560c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
56175f239e9SAlex Zinenko
mlirBlockCreate(intptr_t nArgs,MlirType const * args,MlirLocation const * locs)562e084679fSRiver Riddle MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
563e084679fSRiver Riddle MlirLocation const *locs) {
56475f239e9SAlex Zinenko Block *b = new Block;
565af838584SAlex Zinenko for (intptr_t i = 0; i < nArgs; ++i)
566e084679fSRiver Riddle b->addArgument(unwrap(args[i]), unwrap(locs[i]));
56775f239e9SAlex Zinenko return wrap(b);
56875f239e9SAlex Zinenko }
56975f239e9SAlex Zinenko
mlirBlockEqual(MlirBlock block,MlirBlock other)5708b6bea9bSGeorge bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
57139613c2cSAlex Zinenko return unwrap(block) == unwrap(other);
57239613c2cSAlex Zinenko }
57339613c2cSAlex Zinenko
mlirBlockGetParentOperation(MlirBlock block)5748f130f10SGeorge MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
5758f130f10SGeorge return wrap(unwrap(block)->getParentOp());
5768f130f10SGeorge }
5778f130f10SGeorge
mlirBlockGetParentRegion(MlirBlock block)5788e6c55c9SStella Laurenzo MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
5798e6c55c9SStella Laurenzo return wrap(unwrap(block)->getParent());
5808e6c55c9SStella Laurenzo }
5818e6c55c9SStella Laurenzo
mlirBlockGetNextInRegion(MlirBlock block)58275f239e9SAlex Zinenko MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
58375f239e9SAlex Zinenko return wrap(unwrap(block)->getNextNode());
58475f239e9SAlex Zinenko }
58575f239e9SAlex Zinenko
mlirBlockGetFirstOperation(MlirBlock block)58675f239e9SAlex Zinenko MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
58775f239e9SAlex Zinenko Block *cppBlock = unwrap(block);
58875f239e9SAlex Zinenko if (cppBlock->empty())
58975f239e9SAlex Zinenko return wrap(static_cast<Operation *>(nullptr));
59075f239e9SAlex Zinenko return wrap(&cppBlock->front());
59175f239e9SAlex Zinenko }
59275f239e9SAlex Zinenko
mlirBlockGetTerminator(MlirBlock block)593c645ea5eSStella Laurenzo MlirOperation mlirBlockGetTerminator(MlirBlock block) {
594c645ea5eSStella Laurenzo Block *cppBlock = unwrap(block);
595c645ea5eSStella Laurenzo if (cppBlock->empty())
596c645ea5eSStella Laurenzo return wrap(static_cast<Operation *>(nullptr));
597c645ea5eSStella Laurenzo Operation &back = cppBlock->back();
598fe7c0d90SRiver Riddle if (!back.hasTrait<OpTrait::IsTerminator>())
599c645ea5eSStella Laurenzo return wrap(static_cast<Operation *>(nullptr));
600c645ea5eSStella Laurenzo return wrap(&back);
601c645ea5eSStella Laurenzo }
602c645ea5eSStella Laurenzo
mlirBlockAppendOwnedOperation(MlirBlock block,MlirOperation operation)60375f239e9SAlex Zinenko void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
60475f239e9SAlex Zinenko unwrap(block)->push_back(unwrap(operation));
60575f239e9SAlex Zinenko }
60675f239e9SAlex Zinenko
mlirBlockInsertOwnedOperation(MlirBlock block,intptr_t pos,MlirOperation operation)607af838584SAlex Zinenko void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
60875f239e9SAlex Zinenko MlirOperation operation) {
60975f239e9SAlex Zinenko auto &opList = unwrap(block)->getOperations();
61075f239e9SAlex Zinenko opList.insert(std::next(opList.begin(), pos), unwrap(operation));
61175f239e9SAlex Zinenko }
61275f239e9SAlex Zinenko
mlirBlockInsertOwnedOperationAfter(MlirBlock block,MlirOperation reference,MlirOperation operation)613c538169eSAlex Zinenko void mlirBlockInsertOwnedOperationAfter(MlirBlock block,
614c538169eSAlex Zinenko MlirOperation reference,
615c538169eSAlex Zinenko MlirOperation operation) {
616c538169eSAlex Zinenko Block *cppBlock = unwrap(block);
617c538169eSAlex Zinenko if (mlirOperationIsNull(reference)) {
618c538169eSAlex Zinenko cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
619c538169eSAlex Zinenko return;
620c538169eSAlex Zinenko }
621c538169eSAlex Zinenko
622c538169eSAlex Zinenko assert(unwrap(reference)->getBlock() == unwrap(block) &&
623c538169eSAlex Zinenko "expected reference operation to belong to the block");
624c538169eSAlex Zinenko cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
625c538169eSAlex Zinenko unwrap(operation));
626c538169eSAlex Zinenko }
627c538169eSAlex Zinenko
mlirBlockInsertOwnedOperationBefore(MlirBlock block,MlirOperation reference,MlirOperation operation)628c538169eSAlex Zinenko void mlirBlockInsertOwnedOperationBefore(MlirBlock block,
629c538169eSAlex Zinenko MlirOperation reference,
630c538169eSAlex Zinenko MlirOperation operation) {
631c538169eSAlex Zinenko if (mlirOperationIsNull(reference))
632c538169eSAlex Zinenko return mlirBlockAppendOwnedOperation(block, operation);
633c538169eSAlex Zinenko
634c538169eSAlex Zinenko assert(unwrap(reference)->getBlock() == unwrap(block) &&
635c538169eSAlex Zinenko "expected reference operation to belong to the block");
636c538169eSAlex Zinenko unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
637c538169eSAlex Zinenko unwrap(operation));
638c538169eSAlex Zinenko }
639c538169eSAlex Zinenko
mlirBlockDestroy(MlirBlock block)64075f239e9SAlex Zinenko void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
64175f239e9SAlex Zinenko
mlirBlockDetach(MlirBlock block)6428d8738f6SJohn Demme void mlirBlockDetach(MlirBlock block) {
6438d8738f6SJohn Demme Block *b = unwrap(block);
6448d8738f6SJohn Demme b->getParent()->getBlocks().remove(b);
6458d8738f6SJohn Demme }
6468d8738f6SJohn Demme
mlirBlockGetNumArguments(MlirBlock block)647af838584SAlex Zinenko intptr_t mlirBlockGetNumArguments(MlirBlock block) {
648af838584SAlex Zinenko return static_cast<intptr_t>(unwrap(block)->getNumArguments());
64975f239e9SAlex Zinenko }
65075f239e9SAlex Zinenko
mlirBlockAddArgument(MlirBlock block,MlirType type,MlirLocation loc)651e084679fSRiver Riddle MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
652e084679fSRiver Riddle MlirLocation loc) {
653e084679fSRiver Riddle return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
6549db61142SGeorge }
6559db61142SGeorge
mlirBlockGetArgument(MlirBlock block,intptr_t pos)656af838584SAlex Zinenko MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
657af838584SAlex Zinenko return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
65875f239e9SAlex Zinenko }
65975f239e9SAlex Zinenko
mlirBlockPrint(MlirBlock block,MlirStringCallback callback,void * userData)660da562974SAlex Zinenko void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
661321aa19eSAlex Zinenko void *userData) {
662b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
663321aa19eSAlex Zinenko unwrap(block)->print(stream);
664321aa19eSAlex Zinenko }
665321aa19eSAlex Zinenko
666c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
667c7994bd9SMehdi Amini // Value API.
668c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
66975f239e9SAlex Zinenko
mlirValueEqual(MlirValue value1,MlirValue value2)6708b6bea9bSGeorge bool mlirValueEqual(MlirValue value1, MlirValue value2) {
6716c7e6b2cSAlex Zinenko return unwrap(value1) == unwrap(value2);
6726c7e6b2cSAlex Zinenko }
6736c7e6b2cSAlex Zinenko
mlirValueIsABlockArgument(MlirValue value)67462195b75SStella Laurenzo bool mlirValueIsABlockArgument(MlirValue value) {
67539613c2cSAlex Zinenko return unwrap(value).isa<BlockArgument>();
67639613c2cSAlex Zinenko }
67739613c2cSAlex Zinenko
mlirValueIsAOpResult(MlirValue value)67862195b75SStella Laurenzo bool mlirValueIsAOpResult(MlirValue value) {
67939613c2cSAlex Zinenko return unwrap(value).isa<OpResult>();
68039613c2cSAlex Zinenko }
68139613c2cSAlex Zinenko
mlirBlockArgumentGetOwner(MlirValue value)68239613c2cSAlex Zinenko MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
68339613c2cSAlex Zinenko return wrap(unwrap(value).cast<BlockArgument>().getOwner());
68439613c2cSAlex Zinenko }
68539613c2cSAlex Zinenko
mlirBlockArgumentGetArgNumber(MlirValue value)68639613c2cSAlex Zinenko intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {
68739613c2cSAlex Zinenko return static_cast<intptr_t>(
68839613c2cSAlex Zinenko unwrap(value).cast<BlockArgument>().getArgNumber());
68939613c2cSAlex Zinenko }
69039613c2cSAlex Zinenko
mlirBlockArgumentSetType(MlirValue value,MlirType type)69139613c2cSAlex Zinenko void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
69239613c2cSAlex Zinenko unwrap(value).cast<BlockArgument>().setType(unwrap(type));
69339613c2cSAlex Zinenko }
69439613c2cSAlex Zinenko
mlirOpResultGetOwner(MlirValue value)69539613c2cSAlex Zinenko MlirOperation mlirOpResultGetOwner(MlirValue value) {
69639613c2cSAlex Zinenko return wrap(unwrap(value).cast<OpResult>().getOwner());
69739613c2cSAlex Zinenko }
69839613c2cSAlex Zinenko
mlirOpResultGetResultNumber(MlirValue value)69939613c2cSAlex Zinenko intptr_t mlirOpResultGetResultNumber(MlirValue value) {
70039613c2cSAlex Zinenko return static_cast<intptr_t>(
70139613c2cSAlex Zinenko unwrap(value).cast<OpResult>().getResultNumber());
70239613c2cSAlex Zinenko }
70339613c2cSAlex Zinenko
mlirValueGetType(MlirValue value)70475f239e9SAlex Zinenko MlirType mlirValueGetType(MlirValue value) {
70575f239e9SAlex Zinenko return wrap(unwrap(value).getType());
70675f239e9SAlex Zinenko }
70775f239e9SAlex Zinenko
mlirValueDump(MlirValue value)708580915d6SAlex Zinenko void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
709580915d6SAlex Zinenko
mlirValuePrint(MlirValue value,MlirStringCallback callback,void * userData)710da562974SAlex Zinenko void mlirValuePrint(MlirValue value, MlirStringCallback callback,
711321aa19eSAlex Zinenko void *userData) {
712b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
713321aa19eSAlex Zinenko unwrap(value).print(stream);
714321aa19eSAlex Zinenko }
715321aa19eSAlex Zinenko
716c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
717c7994bd9SMehdi Amini // Type API.
718c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
71975f239e9SAlex Zinenko
mlirTypeParseGet(MlirContext context,MlirStringRef type)720df9ae599SGeorge MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
721df9ae599SGeorge return wrap(mlir::parseType(unwrap(type), unwrap(context)));
72275f239e9SAlex Zinenko }
72375f239e9SAlex Zinenko
mlirTypeGetContext(MlirType type)7242d1362e0SStella Laurenzo MlirContext mlirTypeGetContext(MlirType type) {
7252d1362e0SStella Laurenzo return wrap(unwrap(type).getContext());
7262d1362e0SStella Laurenzo }
7272d1362e0SStella Laurenzo
mlirTypeGetTypeID(MlirType type)728782a97a9SDaniel Resnick MlirTypeID mlirTypeGetTypeID(MlirType type) {
729782a97a9SDaniel Resnick return wrap(unwrap(type).getTypeID());
730782a97a9SDaniel Resnick }
731782a97a9SDaniel Resnick
mlirTypeEqual(MlirType t1,MlirType t2)7328b6bea9bSGeorge bool mlirTypeEqual(MlirType t1, MlirType t2) {
7338b6bea9bSGeorge return unwrap(t1) == unwrap(t2);
7348b6bea9bSGeorge }
73574f57784SAlex Zinenko
mlirTypePrint(MlirType type,MlirStringCallback callback,void * userData)736da562974SAlex Zinenko void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
737b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
738321aa19eSAlex Zinenko unwrap(type).print(stream);
739321aa19eSAlex Zinenko }
740321aa19eSAlex Zinenko
mlirTypeDump(MlirType type)74175f239e9SAlex Zinenko void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
74275f239e9SAlex Zinenko
743c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
744c7994bd9SMehdi Amini // Attribute API.
745c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
74675f239e9SAlex Zinenko
mlirAttributeParseGet(MlirContext context,MlirStringRef attr)747df9ae599SGeorge MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
748df9ae599SGeorge return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
74975f239e9SAlex Zinenko }
75075f239e9SAlex Zinenko
mlirAttributeGetContext(MlirAttribute attribute)75185185b61SStella Laurenzo MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
75285185b61SStella Laurenzo return wrap(unwrap(attribute).getContext());
75385185b61SStella Laurenzo }
75485185b61SStella Laurenzo
mlirAttributeGetType(MlirAttribute attribute)7556771b98cSStella Laurenzo MlirType mlirAttributeGetType(MlirAttribute attribute) {
7566771b98cSStella Laurenzo return wrap(unwrap(attribute).getType());
7576771b98cSStella Laurenzo }
7586771b98cSStella Laurenzo
mlirAttributeGetTypeID(MlirAttribute attr)759782a97a9SDaniel Resnick MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
760782a97a9SDaniel Resnick return wrap(unwrap(attr).getTypeID());
761782a97a9SDaniel Resnick }
762782a97a9SDaniel Resnick
mlirAttributeEqual(MlirAttribute a1,MlirAttribute a2)7638b6bea9bSGeorge bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
764da562974SAlex Zinenko return unwrap(a1) == unwrap(a2);
765da562974SAlex Zinenko }
766da562974SAlex Zinenko
mlirAttributePrint(MlirAttribute attr,MlirStringCallback callback,void * userData)767da562974SAlex Zinenko void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
768321aa19eSAlex Zinenko void *userData) {
769b76f523bSzhanghb97 detail::CallbackOstream stream(callback, userData);
770321aa19eSAlex Zinenko unwrap(attr).print(stream);
771321aa19eSAlex Zinenko }
772321aa19eSAlex Zinenko
mlirAttributeDump(MlirAttribute attr)77375f239e9SAlex Zinenko void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
77475f239e9SAlex Zinenko
mlirNamedAttributeGet(MlirIdentifier name,MlirAttribute attr)775aadcb26eSMehdi Amini MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name,
776df9ae599SGeorge MlirAttribute attr) {
77775f239e9SAlex Zinenko return MlirNamedAttribute{name, attr};
77875f239e9SAlex Zinenko }
779b85f2f5cSStella Laurenzo
780c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
781c7994bd9SMehdi Amini // Identifier API.
782c7994bd9SMehdi Amini //===----------------------------------------------------------------------===//
783b85f2f5cSStella Laurenzo
mlirIdentifierGet(MlirContext context,MlirStringRef str)784b85f2f5cSStella Laurenzo MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
785195730a6SRiver Riddle return wrap(StringAttr::get(unwrap(context), unwrap(str)));
786b85f2f5cSStella Laurenzo }
787b85f2f5cSStella Laurenzo
mlirIdentifierGetContext(MlirIdentifier ident)7886962bd68SGeorge MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
7896962bd68SGeorge return wrap(unwrap(ident).getContext());
7906962bd68SGeorge }
7916962bd68SGeorge
mlirIdentifierEqual(MlirIdentifier ident,MlirIdentifier other)7928b6bea9bSGeorge bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
793b85f2f5cSStella Laurenzo return unwrap(ident) == unwrap(other);
794b85f2f5cSStella Laurenzo }
795b85f2f5cSStella Laurenzo
mlirIdentifierStr(MlirIdentifier ident)796b85f2f5cSStella Laurenzo MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
797b85f2f5cSStella Laurenzo return wrap(unwrap(ident).strref());
798b85f2f5cSStella Laurenzo }
799782a97a9SDaniel Resnick
800782a97a9SDaniel Resnick //===----------------------------------------------------------------------===//
80130d61893SAlex Zinenko // Symbol and SymbolTable API.
80230d61893SAlex Zinenko //===----------------------------------------------------------------------===//
80330d61893SAlex Zinenko
mlirSymbolTableGetSymbolAttributeName()80430d61893SAlex Zinenko MlirStringRef mlirSymbolTableGetSymbolAttributeName() {
80530d61893SAlex Zinenko return wrap(SymbolTable::getSymbolAttrName());
80630d61893SAlex Zinenko }
80730d61893SAlex Zinenko
mlirSymbolTableGetVisibilityAttributeName()808bdc31837SStella Laurenzo MlirStringRef mlirSymbolTableGetVisibilityAttributeName() {
809bdc31837SStella Laurenzo return wrap(SymbolTable::getVisibilityAttrName());
810bdc31837SStella Laurenzo }
811bdc31837SStella Laurenzo
mlirSymbolTableCreate(MlirOperation operation)81230d61893SAlex Zinenko MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
81330d61893SAlex Zinenko if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
81430d61893SAlex Zinenko return wrap(static_cast<SymbolTable *>(nullptr));
81530d61893SAlex Zinenko return wrap(new SymbolTable(unwrap(operation)));
81630d61893SAlex Zinenko }
81730d61893SAlex Zinenko
mlirSymbolTableDestroy(MlirSymbolTable symbolTable)81830d61893SAlex Zinenko void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
81930d61893SAlex Zinenko delete unwrap(symbolTable);
82030d61893SAlex Zinenko }
82130d61893SAlex Zinenko
mlirSymbolTableLookup(MlirSymbolTable symbolTable,MlirStringRef name)82230d61893SAlex Zinenko MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
82330d61893SAlex Zinenko MlirStringRef name) {
82430d61893SAlex Zinenko return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
82530d61893SAlex Zinenko }
82630d61893SAlex Zinenko
mlirSymbolTableInsert(MlirSymbolTable symbolTable,MlirOperation operation)82730d61893SAlex Zinenko MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
82830d61893SAlex Zinenko MlirOperation operation) {
829120591e1SRiver Riddle return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
83030d61893SAlex Zinenko }
83130d61893SAlex Zinenko
mlirSymbolTableErase(MlirSymbolTable symbolTable,MlirOperation operation)83230d61893SAlex Zinenko void mlirSymbolTableErase(MlirSymbolTable symbolTable,
83330d61893SAlex Zinenko MlirOperation operation) {
83430d61893SAlex Zinenko unwrap(symbolTable)->erase(unwrap(operation));
83530d61893SAlex Zinenko }
836bdc31837SStella Laurenzo
mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol,MlirStringRef newSymbol,MlirOperation from)837bdc31837SStella Laurenzo MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol,
838bdc31837SStella Laurenzo MlirStringRef newSymbol,
839bdc31837SStella Laurenzo MlirOperation from) {
84002b6fb21SMehdi Amini auto *cppFrom = unwrap(from);
841bdc31837SStella Laurenzo auto *context = cppFrom->getContext();
84256f62fbfSRiver Riddle auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
84356f62fbfSRiver Riddle auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
844bdc31837SStella Laurenzo return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
845bdc31837SStella Laurenzo unwrap(from)));
846bdc31837SStella Laurenzo }
847bdc31837SStella Laurenzo
mlirSymbolTableWalkSymbolTables(MlirOperation from,bool allSymUsesVisible,void (* callback)(MlirOperation,bool,void * userData),void * userData)848bdc31837SStella Laurenzo void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
849bdc31837SStella Laurenzo void (*callback)(MlirOperation, bool,
850bdc31837SStella Laurenzo void *userData),
851bdc31837SStella Laurenzo void *userData) {
852bdc31837SStella Laurenzo SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
853bdc31837SStella Laurenzo [&](Operation *foundOpCpp, bool isVisible) {
854bdc31837SStella Laurenzo callback(wrap(foundOpCpp), isVisible,
855bdc31837SStella Laurenzo userData);
856bdc31837SStella Laurenzo });
857bdc31837SStella Laurenzo }
858