2020-08-05 14:36:16 +02:00
|
|
|
//===- 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"
|
2020-09-29 16:23:02 +02:00
|
|
|
#include "mlir-c/Support.h"
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-08-18 10:26:30 +02:00
|
|
|
#include "mlir/CAPI/IR.h"
|
2020-09-29 16:23:02 +02:00
|
|
|
#include "mlir/CAPI/Support.h"
|
2020-09-14 22:52:22 +08:00
|
|
|
#include "mlir/CAPI/Utils.h"
|
2020-08-05 14:36:16 +02:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2020-11-19 10:43:12 -08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-18 20:01:19 +00:00
|
|
|
#include "mlir/IR/Dialect.h"
|
2020-08-05 14:36:16 +02:00
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
#include "mlir/IR/Types.h"
|
2020-12-03 17:45:28 +00:00
|
|
|
#include "mlir/IR/Verifier.h"
|
2021-01-22 18:43:50 -08:00
|
|
|
#include "mlir/Interfaces/InferTypeOpInterface.h"
|
2020-08-05 14:36:16 +02:00
|
|
|
#include "mlir/Parser.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Context API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
|
|
|
MlirContext mlirContextCreate() {
|
2020-10-23 20:19:35 +00:00
|
|
|
auto *context = new MLIRContext;
|
2020-08-05 14:36:16 +02:00
|
|
|
return wrap(context);
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
|
2020-08-26 23:48:42 -07:00
|
|
|
return unwrap(ctx1) == unwrap(ctx2);
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirContextDestroy(MlirContext context) { delete unwrap(context); }
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {
|
2020-09-19 22:02:32 -07:00
|
|
|
unwrap(context)->allowUnregisteredDialects(allow);
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {
|
2020-09-19 22:02:32 -07:00
|
|
|
return unwrap(context)->allowsUnregisteredDialects();
|
|
|
|
}
|
2020-09-29 16:23:02 +02:00
|
|
|
intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) {
|
|
|
|
return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: expose a cheaper way than constructing + sorting a vector only to take
|
|
|
|
// its size.
|
|
|
|
intptr_t mlirContextGetNumLoadedDialects(MlirContext context) {
|
|
|
|
return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
|
|
|
|
MlirStringRef name) {
|
|
|
|
return wrap(unwrap(context)->getOrLoadDialect(unwrap(name)));
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dialect API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-09-29 16:23:02 +02:00
|
|
|
|
|
|
|
MlirContext mlirDialectGetContext(MlirDialect dialect) {
|
|
|
|
return wrap(unwrap(dialect)->getContext());
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
|
2020-09-29 16:23:02 +02:00
|
|
|
return unwrap(dialect1) == unwrap(dialect2);
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) {
|
|
|
|
return wrap(unwrap(dialect)->getNamespace());
|
|
|
|
}
|
2020-09-19 22:02:32 -07:00
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Printing flags API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-10-20 23:20:04 -07:00
|
|
|
|
|
|
|
MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {
|
|
|
|
return wrap(new OpPrintingFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {
|
|
|
|
delete unwrap(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
|
|
|
|
intptr_t largeElementLimit) {
|
|
|
|
unwrap(flags)->elideLargeElementsAttrs(largeElementLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags,
|
2020-11-29 18:31:11 -08:00
|
|
|
bool prettyForm) {
|
2020-10-20 23:20:04 -07:00
|
|
|
unwrap(flags)->enableDebugInfo(/*prettyForm=*/prettyForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
|
|
|
|
unwrap(flags)->printGenericOpForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
|
|
|
|
unwrap(flags)->useLocalScope();
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Location API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
|
|
|
MlirLocation mlirLocationFileLineColGet(MlirContext context,
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirStringRef filename, unsigned line,
|
2020-08-05 14:36:16 +02:00
|
|
|
unsigned col) {
|
2021-03-08 14:25:38 -08:00
|
|
|
return wrap(Location(
|
|
|
|
FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-12-17 09:27:17 -08:00
|
|
|
MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
|
2021-03-08 14:25:38 -08:00
|
|
|
return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
|
2020-12-17 09:27:17 -08:00
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirLocation mlirLocationUnknownGet(MlirContext context) {
|
2021-03-08 14:25:38 -08:00
|
|
|
return wrap(Location(UnknownLoc::get(unwrap(context))));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
|
2020-11-29 13:30:23 -08:00
|
|
|
return unwrap(l1) == unwrap(l2);
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
MlirContext mlirLocationGetContext(MlirLocation location) {
|
|
|
|
return wrap(unwrap(location).getContext());
|
|
|
|
}
|
|
|
|
|
2020-08-19 18:38:56 +02:00
|
|
|
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
|
2020-08-11 18:25:09 +02:00
|
|
|
void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(location).print(stream);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Module API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
|
|
|
MlirModule mlirModuleCreateEmpty(MlirLocation location) {
|
|
|
|
return wrap(ModuleOp::create(unwrap(location)));
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
|
|
|
|
OwningModuleRef owning = parseSourceString(unwrap(module), unwrap(context));
|
2020-08-16 20:53:45 -07:00
|
|
|
if (!owning)
|
|
|
|
return MlirModule{nullptr};
|
2020-08-05 14:36:16 +02:00
|
|
|
return MlirModule{owning.release().getOperation()};
|
|
|
|
}
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
MlirContext mlirModuleGetContext(MlirModule module) {
|
|
|
|
return wrap(unwrap(module).getContext());
|
|
|
|
}
|
|
|
|
|
2020-10-28 05:57:17 +00:00
|
|
|
MlirBlock mlirModuleGetBody(MlirModule module) {
|
|
|
|
return wrap(unwrap(module).getBody());
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirModuleDestroy(MlirModule module) {
|
|
|
|
// Transfer ownership to an OwningModuleRef so that its destructor is called.
|
|
|
|
OwningModuleRef(unwrap(module));
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirModuleGetOperation(MlirModule module) {
|
|
|
|
return wrap(unwrap(module).getOperation());
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operation state API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirOperationState state;
|
|
|
|
state.name = name;
|
|
|
|
state.location = loc;
|
|
|
|
state.nResults = 0;
|
|
|
|
state.results = nullptr;
|
|
|
|
state.nOperands = 0;
|
|
|
|
state.operands = nullptr;
|
|
|
|
state.nRegions = 0;
|
|
|
|
state.regions = nullptr;
|
|
|
|
state.nSuccessors = 0;
|
|
|
|
state.successors = nullptr;
|
|
|
|
state.nAttributes = 0;
|
|
|
|
state.attributes = nullptr;
|
2021-01-22 18:43:50 -08:00
|
|
|
state.enableResultTypeInference = false;
|
2020-08-05 14:36:16 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define APPEND_ELEMS(type, sizeName, elemName) \
|
|
|
|
state->elemName = \
|
|
|
|
(type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
|
|
|
|
memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
|
|
|
|
state->sizeName += n;
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirType const *results) {
|
2020-08-05 14:36:16 +02:00
|
|
|
APPEND_ELEMS(MlirType, nResults, results);
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirValue const *operands) {
|
2020-08-05 14:36:16 +02:00
|
|
|
APPEND_ELEMS(MlirValue, nOperands, operands);
|
|
|
|
}
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirRegion const *regions) {
|
2020-08-05 14:36:16 +02:00
|
|
|
APPEND_ELEMS(MlirRegion, nRegions, regions);
|
|
|
|
}
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirBlock const *successors) {
|
2020-08-05 14:36:16 +02:00
|
|
|
APPEND_ELEMS(MlirBlock, nSuccessors, successors);
|
|
|
|
}
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirNamedAttribute const *attributes) {
|
2020-08-05 14:36:16 +02:00
|
|
|
APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:43:50 -08:00
|
|
|
void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {
|
|
|
|
state->enableResultTypeInference = true;
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operation API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2021-01-22 18:43:50 -08:00
|
|
|
static LogicalResult inferOperationTypes(OperationState &state) {
|
|
|
|
MLIRContext *context = state.getContext();
|
|
|
|
const AbstractOperation *abstractOp =
|
|
|
|
AbstractOperation::lookup(state.name.getStringRef(), context);
|
|
|
|
if (!abstractOp) {
|
|
|
|
emitError(state.location)
|
|
|
|
<< "type inference was requested for the operation " << state.name
|
|
|
|
<< ", but the operation was not registered. Ensure that the dialect "
|
|
|
|
"containing the operation is linked into MLIR and registered with "
|
|
|
|
"the context";
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to inference via an op interface.
|
|
|
|
auto *inferInterface = abstractOp->getInterface<InferTypeOpInterface>();
|
|
|
|
if (!inferInterface) {
|
|
|
|
emitError(state.location)
|
|
|
|
<< "type inference was requested for the operation " << state.name
|
|
|
|
<< ", but the operation does not support type inference. Result "
|
|
|
|
"types must be specified explicitly.";
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (succeeded(inferInterface->inferReturnTypes(
|
|
|
|
context, state.location, state.operands,
|
|
|
|
state.attributes.getDictionary(context), state.regions, state.types)))
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// Diagnostic emitted by interface.
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirOperationCreate(MlirOperationState *state) {
|
2020-08-05 14:36:16 +02:00
|
|
|
assert(state);
|
2020-11-23 13:08:00 -08:00
|
|
|
OperationState cppState(unwrap(state->location), unwrap(state->name));
|
2020-08-05 14:36:16 +02:00
|
|
|
SmallVector<Type, 4> resultStorage;
|
|
|
|
SmallVector<Value, 8> operandStorage;
|
|
|
|
SmallVector<Block *, 2> successorStorage;
|
|
|
|
cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
|
|
|
|
cppState.addOperands(
|
|
|
|
unwrapList(state->nOperands, state->operands, operandStorage));
|
|
|
|
cppState.addSuccessors(
|
|
|
|
unwrapList(state->nSuccessors, state->successors, successorStorage));
|
|
|
|
|
|
|
|
cppState.attributes.reserve(state->nAttributes);
|
2020-08-11 18:34:32 +02:00
|
|
|
for (intptr_t i = 0; i < state->nAttributes; ++i)
|
2020-11-23 13:08:00 -08:00
|
|
|
cppState.addAttribute(unwrap(state->attributes[i].name),
|
2020-08-05 14:36:16 +02:00
|
|
|
unwrap(state->attributes[i].attribute));
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
for (intptr_t i = 0; i < state->nRegions; ++i)
|
2020-08-05 14:36:16 +02:00
|
|
|
cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
|
|
|
|
|
2020-08-10 10:32:24 +02:00
|
|
|
free(state->results);
|
|
|
|
free(state->operands);
|
|
|
|
free(state->successors);
|
2020-08-11 18:34:32 +02:00
|
|
|
free(state->regions);
|
2020-08-10 10:32:24 +02:00
|
|
|
free(state->attributes);
|
2021-01-22 18:43:50 -08:00
|
|
|
|
|
|
|
// Infer result types.
|
|
|
|
if (state->enableResultTypeInference) {
|
|
|
|
assert(cppState.types.empty() &&
|
|
|
|
"result type inference enabled and result types provided");
|
|
|
|
if (failed(inferOperationTypes(cppState)))
|
|
|
|
return {nullptr};
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation result = wrap(Operation::create(cppState));
|
2020-08-11 18:34:32 +02:00
|
|
|
return result;
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
|
2020-10-19 19:17:51 +02:00
|
|
|
return unwrap(op) == unwrap(other);
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:54:19 -08:00
|
|
|
MlirContext mlirOperationGetContext(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getContext());
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:26:38 +00:00
|
|
|
MlirIdentifier mlirOperationGetName(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getName().getIdentifier());
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
MlirBlock mlirOperationGetBlock(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getParentOp());
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirOperationGetNumRegions(MlirOperation op) {
|
|
|
|
return static_cast<intptr_t>(unwrap(op)->getNumRegions());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
|
|
|
|
return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getNextNode());
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirOperationGetNumOperands(MlirOperation op) {
|
|
|
|
return static_cast<intptr_t>(unwrap(op)->getNumOperands());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
|
|
|
|
return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirOperationGetNumResults(MlirOperation op) {
|
|
|
|
return static_cast<intptr_t>(unwrap(op)->getNumResults());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
|
|
|
|
return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
|
|
|
|
return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
|
|
|
|
return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
|
|
|
|
return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {
|
2020-08-05 14:36:16 +02:00
|
|
|
NamedAttribute attr = unwrap(op)->getAttrs()[pos];
|
2020-12-11 18:50:04 +00:00
|
|
|
return MlirNamedAttribute{wrap(attr.first), wrap(attr.second)};
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirStringRef name) {
|
|
|
|
return wrap(unwrap(op)->getAttr(unwrap(name)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 13:08:00 -08:00
|
|
|
void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
|
2020-10-06 23:01:20 -07:00
|
|
|
MlirAttribute attr) {
|
2020-11-23 13:08:00 -08:00
|
|
|
unwrap(op)->setAttr(unwrap(name), unwrap(attr));
|
2020-10-06 23:01:20 -07:00
|
|
|
}
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {
|
2020-12-17 17:10:12 -08:00
|
|
|
return !!unwrap(op)->removeAttr(unwrap(name));
|
2020-10-06 23:01:20 -07:00
|
|
|
}
|
|
|
|
|
2020-08-19 18:38:56 +02:00
|
|
|
void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
|
2020-08-11 18:25:09 +02:00
|
|
|
void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(op)->print(stream);
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:20:04 -07:00
|
|
|
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
|
|
|
|
MlirStringCallback callback, void *userData) {
|
|
|
|
detail::CallbackOstream stream(callback, userData);
|
|
|
|
unwrap(op)->print(stream, *unwrap(flags));
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
|
|
|
|
|
2020-12-03 17:45:28 +00:00
|
|
|
bool mlirOperationVerify(MlirOperation op) {
|
|
|
|
return succeeded(verify(unwrap(op)));
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
|
|
|
MlirRegion mlirRegionCreate() { return wrap(new Region); }
|
|
|
|
|
|
|
|
MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
|
|
|
|
Region *cppRegion = unwrap(region);
|
|
|
|
if (cppRegion->empty())
|
|
|
|
return wrap(static_cast<Block *>(nullptr));
|
|
|
|
return wrap(&cppRegion->front());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
|
|
|
|
unwrap(region)->push_back(unwrap(block));
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirBlock block) {
|
|
|
|
auto &blockList = unwrap(region)->getBlocks();
|
|
|
|
blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:02:47 +02:00
|
|
|
void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
|
|
|
|
MlirBlock block) {
|
|
|
|
Region *cppRegion = unwrap(region);
|
|
|
|
if (mlirBlockIsNull(reference)) {
|
|
|
|
cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(unwrap(reference)->getParent() == unwrap(region) &&
|
|
|
|
"expected reference block to belong to the region");
|
|
|
|
cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
|
|
|
|
unwrap(block));
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
|
|
|
|
MlirBlock block) {
|
|
|
|
if (mlirBlockIsNull(reference))
|
|
|
|
return mlirRegionAppendOwnedBlock(region, block);
|
|
|
|
|
|
|
|
assert(unwrap(reference)->getParent() == unwrap(region) &&
|
|
|
|
"expected reference block to belong to the region");
|
|
|
|
unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
|
|
|
|
unwrap(block));
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirRegionDestroy(MlirRegion region) {
|
|
|
|
delete static_cast<Region *>(region.ptr);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Block API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-11-19 10:45:43 -08:00
|
|
|
MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) {
|
2020-08-05 14:36:16 +02:00
|
|
|
Block *b = new Block;
|
2020-08-11 18:34:32 +02:00
|
|
|
for (intptr_t i = 0; i < nArgs; ++i)
|
2020-08-05 14:36:16 +02:00
|
|
|
b->addArgument(unwrap(args[i]));
|
|
|
|
return wrap(b);
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
|
2020-10-19 19:17:51 +02:00
|
|
|
return unwrap(block) == unwrap(other);
|
|
|
|
}
|
|
|
|
|
2021-02-08 19:54:19 -08:00
|
|
|
MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
|
|
|
|
return wrap(unwrap(block)->getParentOp());
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
|
|
|
|
return wrap(unwrap(block)->getNextNode());
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
|
|
|
|
Block *cppBlock = unwrap(block);
|
|
|
|
if (cppBlock->empty())
|
|
|
|
return wrap(static_cast<Operation *>(nullptr));
|
|
|
|
return wrap(&cppBlock->front());
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
MlirOperation mlirBlockGetTerminator(MlirBlock block) {
|
|
|
|
Block *cppBlock = unwrap(block);
|
|
|
|
if (cppBlock->empty())
|
|
|
|
return wrap(static_cast<Operation *>(nullptr));
|
|
|
|
Operation &back = cppBlock->back();
|
2021-02-09 11:41:10 -08:00
|
|
|
if (!back.hasTrait<OpTrait::IsTerminator>())
|
2020-10-28 23:16:36 -07:00
|
|
|
return wrap(static_cast<Operation *>(nullptr));
|
|
|
|
return wrap(&back);
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
|
|
|
|
unwrap(block)->push_back(unwrap(operation));
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirOperation operation) {
|
|
|
|
auto &opList = unwrap(block)->getOperations();
|
|
|
|
opList.insert(std::next(opList.begin(), pos), unwrap(operation));
|
|
|
|
}
|
|
|
|
|
2020-09-23 15:02:47 +02:00
|
|
|
void mlirBlockInsertOwnedOperationAfter(MlirBlock block,
|
|
|
|
MlirOperation reference,
|
|
|
|
MlirOperation operation) {
|
|
|
|
Block *cppBlock = unwrap(block);
|
|
|
|
if (mlirOperationIsNull(reference)) {
|
|
|
|
cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(unwrap(reference)->getBlock() == unwrap(block) &&
|
|
|
|
"expected reference operation to belong to the block");
|
|
|
|
cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
|
|
|
|
unwrap(operation));
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirBlockInsertOwnedOperationBefore(MlirBlock block,
|
|
|
|
MlirOperation reference,
|
|
|
|
MlirOperation operation) {
|
|
|
|
if (mlirOperationIsNull(reference))
|
|
|
|
return mlirBlockAppendOwnedOperation(block, operation);
|
|
|
|
|
|
|
|
assert(unwrap(reference)->getBlock() == unwrap(block) &&
|
|
|
|
"expected reference operation to belong to the block");
|
|
|
|
unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
|
|
|
|
unwrap(operation));
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
intptr_t mlirBlockGetNumArguments(MlirBlock block) {
|
|
|
|
return static_cast<intptr_t>(unwrap(block)->getNumArguments());
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 12:34:29 -08:00
|
|
|
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type) {
|
|
|
|
return wrap(unwrap(block)->addArgument(unwrap(type)));
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:34:32 +02:00
|
|
|
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
|
|
|
|
return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 18:38:56 +02:00
|
|
|
void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
|
2020-08-11 18:25:09 +02:00
|
|
|
void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(block)->print(stream);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Value API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirValueEqual(MlirValue value1, MlirValue value2) {
|
2020-11-06 15:09:54 +01:00
|
|
|
return unwrap(value1) == unwrap(value2);
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
bool mlirValueIsABlockArgument(MlirValue value) {
|
2020-10-19 19:17:51 +02:00
|
|
|
return unwrap(value).isa<BlockArgument>();
|
|
|
|
}
|
|
|
|
|
2020-11-29 18:31:11 -08:00
|
|
|
bool mlirValueIsAOpResult(MlirValue value) {
|
2020-10-19 19:17:51 +02:00
|
|
|
return unwrap(value).isa<OpResult>();
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
|
|
|
|
return wrap(unwrap(value).cast<BlockArgument>().getOwner());
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {
|
|
|
|
return static_cast<intptr_t>(
|
|
|
|
unwrap(value).cast<BlockArgument>().getArgNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
|
|
|
|
unwrap(value).cast<BlockArgument>().setType(unwrap(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirOpResultGetOwner(MlirValue value) {
|
|
|
|
return wrap(unwrap(value).cast<OpResult>().getOwner());
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t mlirOpResultGetResultNumber(MlirValue value) {
|
|
|
|
return static_cast<intptr_t>(
|
|
|
|
unwrap(value).cast<OpResult>().getResultNumber());
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
MlirType mlirValueGetType(MlirValue value) {
|
|
|
|
return wrap(unwrap(value).getType());
|
|
|
|
}
|
|
|
|
|
2020-10-20 11:21:05 +02:00
|
|
|
void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
|
|
|
|
|
2020-08-19 18:38:56 +02:00
|
|
|
void mlirValuePrint(MlirValue value, MlirStringCallback callback,
|
2020-08-11 18:25:09 +02:00
|
|
|
void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(value).print(stream);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
|
|
|
|
return wrap(mlir::parseType(unwrap(type), unwrap(context)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-08-26 23:48:42 -07:00
|
|
|
MlirContext mlirTypeGetContext(MlirType type) {
|
|
|
|
return wrap(unwrap(type).getContext());
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirTypeEqual(MlirType t1, MlirType t2) {
|
|
|
|
return unwrap(t1) == unwrap(t2);
|
|
|
|
}
|
2020-08-18 10:26:30 +02:00
|
|
|
|
2020-08-19 18:38:56 +02:00
|
|
|
void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(type).print(stream);
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
|
|
|
|
return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
|
|
|
|
return wrap(unwrap(attribute).getContext());
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:31:31 -07:00
|
|
|
MlirType mlirAttributeGetType(MlirAttribute attribute) {
|
|
|
|
return wrap(unwrap(attribute).getType());
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
|
2020-08-19 18:38:56 +02:00
|
|
|
return unwrap(a1) == unwrap(a2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
|
2020-08-11 18:25:09 +02:00
|
|
|
void *userData) {
|
2020-09-14 22:52:22 +08:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2020-08-11 18:25:09 +02:00
|
|
|
unwrap(attr).print(stream);
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
|
|
|
|
|
2020-12-11 18:50:04 +00:00
|
|
|
MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name,
|
2020-11-23 13:08:00 -08:00
|
|
|
MlirAttribute attr) {
|
2020-08-05 14:36:16 +02:00
|
|
|
return MlirNamedAttribute{name, attr};
|
|
|
|
}
|
2020-11-02 18:26:38 +00:00
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Identifier API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-11-02 18:26:38 +00:00
|
|
|
|
|
|
|
MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
|
|
|
|
return wrap(Identifier::get(unwrap(str), unwrap(context)));
|
|
|
|
}
|
|
|
|
|
2021-02-09 12:59:52 -08:00
|
|
|
MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
|
|
|
|
return wrap(unwrap(ident).getContext());
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:40:03 -08:00
|
|
|
bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
|
2020-11-02 18:26:38 +00:00
|
|
|
return unwrap(ident) == unwrap(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
|
|
|
|
return wrap(unwrap(ident).strref());
|
|
|
|
}
|