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
|
|
|
|
2022-07-10 01:00:21 -07:00
|
|
|
#include "mlir/AsmParser/AsmParser.h"
|
2022-09-05 11:54:19 +00:00
|
|
|
#include "mlir/Bytecode/BytecodeWriter.h"
|
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"
|
2021-09-01 16:16:35 -07:00
|
|
|
#include "mlir/IR/Location.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"
|
2022-03-04 12:53:22 -08:00
|
|
|
#include "mlir/Parser/Parser.h"
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2021-09-30 18:14:00 -06:00
|
|
|
#include <cstddef>
|
2023-01-13 21:05:06 -08:00
|
|
|
#include <optional>
|
2021-04-16 12:54:43 +00:00
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
using namespace mlir;
|
|
|
|
|
2021-04-16 12:54:43 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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());
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:13:24 -07:00
|
|
|
void mlirContextAppendDialectRegistry(MlirContext ctx,
|
|
|
|
MlirDialectRegistry registry) {
|
|
|
|
unwrap(ctx)->appendDialectRegistry(*unwrap(registry));
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:23:02 +02:00
|
|
|
// 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)));
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:19:10 -07:00
|
|
|
bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
|
|
|
|
return unwrap(context)->isOperationRegistered(unwrap(name));
|
|
|
|
}
|
|
|
|
|
2021-04-16 12:54:43 +00:00
|
|
|
void mlirContextEnableMultithreading(MlirContext context, bool enable) {
|
|
|
|
return unwrap(context)->enableMultithreading(enable);
|
|
|
|
}
|
|
|
|
|
[mlir] Overhaul C/Python registration APIs to properly scope registration/loading activities.
Since the very first commits, the Python and C MLIR APIs have had mis-placed registration/load functionality for dialects, extensions, etc. This was done pragmatically in order to get bootstrapped and then just grew in. Downstreams largely bypass and do their own thing by providing various APIs to register things they need. Meanwhile, the C++ APIs have stabilized around this and it would make sense to follow suit.
The thing we have observed in canonical usage by downstreams is that each downstream tends to have native entry points that configure its installation to its preferences with one-stop APIs. This patch leans in to this approach with `RegisterEverything.h` and `mlir._mlir_libs._mlirRegisterEverything` being the one-stop entry points for the "upstream packages". The `_mlir_libs.__init__.py` now allows customization of the environment and Context by adding "initialization modules" to the `_mlir_libs` package. If present, `_mlirRegisterEverything` is treated as such a module. Others can be added by downstreams by adding a `_site_initialize_{i}.py` module, where '{i}' is a number starting with zero. The number will be incremented and corresponding module loaded until one is not found. Initialization modules can:
* Perform load time customization to the global environment (i.e. registering passes, hooks, etc).
* Define a `register_dialects(registry: DialectRegistry)` function that can extend the `DialectRegistry` that will be used to bootstrap the `Context`.
* Define a `context_init_hook(context: Context)` function that will be added to a list of callbacks which will be invoked after dialect registration during `Context` initialization.
Note that the `MLIRPythonExtension.RegisterEverything` is not included by default when building a downstream (its corresponding behavior was prior). For downstreams which need the default MLIR initialization to take place, they must add this back in to their Python CMake build just like they add their own components (i.e. to `add_mlir_python_common_capi_library` and `add_mlir_python_modules`). It is perfectly valid to not do this, in which case, only the things explicitly depended on and initialized by downstreams will be built/packaged. If the downstream has not been set up for this, it is recommended to simply add this back for the time being and pay the build time/package size cost.
CMake changes:
* `MLIRCAPIRegistration` -> `MLIRCAPIRegisterEverything` (renamed to signify what it does and force an evaluation: a number of places were incidentally linking this very expensive target)
* `MLIRPythonSoure.Passes` removed (without replacement: just drop)
* `MLIRPythonExtension.AllPassesRegistration` removed (without replacement: just drop)
* `MLIRPythonExtension.Conversions` removed (without replacement: just drop)
* `MLIRPythonExtension.Transforms` removed (without replacement: just drop)
Header changes:
* `mlir-c/Registration.h` is deleted. Dialect registration functionality is now in `IR.h`. Registration of upstream features are in `mlir-c/RegisterEverything.h`. When updating MLIR and a couple of downstreams, I found that proper usage was commingled so required making a choice vs just blind S&R.
Python APIs removed:
* mlir.transforms and mlir.conversions (previously only had an __init__.py which indirectly triggered `mlirRegisterTransformsPasses()` and `mlirRegisterConversionPasses()` respectively). Downstream impact: Remove these imports if present (they now happen as part of default initialization).
* mlir._mlir_libs._all_passes_registration, mlir._mlir_libs._mlirTransforms, mlir._mlir_libs._mlirConversions. Downstream impact: None expected (these were internally used).
C-APIs changed:
* mlirRegisterAllDialects(MlirContext) now takes an MlirDialectRegistry instead. It also used to trigger loading of all dialects, which was already marked with a TODO to remove -- it no longer does, and for direct use, dialects must be explicitly loaded. Downstream impact: Direct C-API users must ensure that needed dialects are loaded or call `mlirContextLoadAllAvailableDialects(MlirContext)` to emulate the prior behavior. Also see the `ir.c` test case (e.g. ` mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));`).
* mlirDialectHandle* APIs were moved from Registration.h (which now is restricted to just global/upstream registration) to IR.h, arguably where it should have been. Downstream impact: include correct header (likely already doing so).
C-APIs added:
* mlirContextLoadAllAvailableDialects(MlirContext): Corresponds to C++ API with the same purpose.
Python APIs added:
* mlir.ir.DialectRegistry: Mapping for an MlirDialectRegistry.
* mlir.ir.Context.append_dialect_registry(MlirDialectRegistry)
* mlir.ir.Context.load_all_available_dialects()
* mlir._mlir_libs._mlirAllRegistration: New native extension that exposes a `register_dialects(MlirDialectRegistry)` entry point and performs all upstream pass/conversion/transforms registration on init. In this first step, we eagerly load this as part of the __init__.py and use it to monkey patch the Context to emulate prior behavior.
* Type caster and capsule support for MlirDialectRegistry
This should make it possible to build downstream Python dialects that only depend on a subset of MLIR. See: https://github.com/llvm/llvm-project/issues/56037
Here is an example PR, minimally adapting IREE to these changes: https://github.com/iree-org/iree/pull/9638/files In this situation, IREE is opting to not link everything, since it is already configuring the Context to its liking. For projects that would just like to not think about it and pull in everything, add `MLIRPythonExtension.RegisterEverything` to the list of Python sources getting built, and the old behavior will continue.
Reviewed By: mehdi_amini, ftynse
Differential Revision: https://reviews.llvm.org/D128593
2022-07-16 16:09:03 -07:00
|
|
|
void mlirContextLoadAllAvailableDialects(MlirContext context) {
|
|
|
|
unwrap(context)->loadAllAvailableDialects();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-01-26 17:13:24 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DialectRegistry API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
MlirDialectRegistry mlirDialectRegistryCreate() {
|
|
|
|
return wrap(new DialectRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirDialectRegistryDestroy(MlirDialectRegistry registry) {
|
|
|
|
delete unwrap(registry);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:44:27 -08:00
|
|
|
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable,
|
2020-11-29 18:31:11 -08:00
|
|
|
bool prettyForm) {
|
2022-11-17 20:44:27 -08:00
|
|
|
unwrap(flags)->enableDebugInfo(enable, /*prettyForm=*/prettyForm);
|
2020-10-20 23:20:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
|
|
|
|
unwrap(flags)->printGenericOpForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
|
|
|
|
unwrap(flags)->useLocalScope();
|
|
|
|
}
|
|
|
|
|
2023-02-25 03:51:31 -05:00
|
|
|
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags) {
|
|
|
|
unwrap(flags)->assumeVerified();
|
|
|
|
}
|
|
|
|
|
2023-04-29 05:35:53 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bytecode printing flags API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate() {
|
|
|
|
return wrap(new BytecodeWriterConfig());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config) {
|
|
|
|
delete unwrap(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags,
|
|
|
|
int64_t version) {
|
|
|
|
unwrap(flags)->setDesiredBytecodeVersion(version);
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Location API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
2023-01-24 23:13:20 -08:00
|
|
|
MlirAttribute mlirLocationGetAttribute(MlirLocation location) {
|
|
|
|
return wrap(LocationAttr(unwrap(location)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {
|
|
|
|
return wrap(Location(unwrap(attribute).cast<LocationAttr>()));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-18 06:57:51 -07:00
|
|
|
MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
|
|
|
|
MlirLocation const *locations,
|
|
|
|
MlirAttribute metadata) {
|
|
|
|
SmallVector<Location, 4> locs;
|
|
|
|
ArrayRef<Location> unwrappedLocs = unwrapList(nLocations, locations, locs);
|
|
|
|
return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:16:35 -07:00
|
|
|
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
|
|
|
|
MlirLocation childLoc) {
|
|
|
|
if (mlirLocationIsNull(childLoc))
|
|
|
|
return wrap(
|
2021-11-16 17:21:15 +00:00
|
|
|
Location(NameLoc::get(StringAttr::get(unwrap(context), unwrap(name)))));
|
2021-09-01 16:16:35 -07:00
|
|
|
return wrap(Location(NameLoc::get(
|
2021-11-16 17:21:15 +00:00
|
|
|
StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
|
2021-09-01 16:16:35 -07: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) {
|
2022-01-29 18:41:10 -08:00
|
|
|
OwningOpRef<ModuleOp> owning =
|
2022-03-08 12:59:42 +01:00
|
|
|
parseSourceString<ModuleOp>(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) {
|
2022-01-29 18:41:10 -08:00
|
|
|
// Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
|
|
|
|
// called.
|
|
|
|
OwningOpRef<ModuleOp>(unwrap(module));
|
2020-08-05 14:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirModuleGetOperation(MlirModule module) {
|
|
|
|
return wrap(unwrap(module).getOperation());
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:14:02 +00:00
|
|
|
MlirModule mlirModuleFromOperation(MlirOperation op) {
|
|
|
|
return wrap(dyn_cast<ModuleOp>(unwrap(op)));
|
|
|
|
}
|
|
|
|
|
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();
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
|
2021-11-17 21:50:28 +00:00
|
|
|
if (!info) {
|
2021-01-22 18:43:50 -08:00
|
|
|
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.
|
2021-11-17 21:50:28 +00:00
|
|
|
auto *inferInterface = info->getInterface<InferTypeOpInterface>();
|
2021-01-22 18:43:50 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-08 16:55:06 -05:00
|
|
|
MlirOperation mlirOperationCreateParse(MlirContext context,
|
|
|
|
MlirStringRef sourceStr,
|
|
|
|
MlirStringRef sourceName) {
|
|
|
|
|
|
|
|
return wrap(
|
|
|
|
parseSourceString(unwrap(sourceStr), unwrap(context), unwrap(sourceName))
|
|
|
|
.release());
|
|
|
|
}
|
|
|
|
|
2021-05-24 11:52:41 -07:00
|
|
|
MlirOperation mlirOperationClone(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->clone());
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
|
|
|
|
|
2021-10-31 09:37:20 +01:00
|
|
|
void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
|
|
|
|
|
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());
|
2021-10-18 16:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MlirLocation mlirOperationGetLocation(MlirOperation op) {
|
|
|
|
return wrap(unwrap(op)->getLoc());
|
2021-02-08 19:54:19 -08:00
|
|
|
}
|
|
|
|
|
2021-09-30 18:14:00 -06:00
|
|
|
MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
|
2021-11-17 21:50:28 +00:00
|
|
|
if (auto info = unwrap(op)->getRegisteredInfo())
|
|
|
|
return wrap(info->getTypeID());
|
2021-09-30 18:14:00 -06:00
|
|
|
return {nullptr};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-09 17:52:56 -08:00
|
|
|
MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
|
|
|
|
Operation *cppOp = unwrap(op);
|
|
|
|
if (cppOp->getNumRegions() == 0)
|
|
|
|
return wrap(static_cast<Region *>(nullptr));
|
|
|
|
return wrap(&cppOp->getRegion(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
|
|
|
|
Region *cppRegion = unwrap(region);
|
|
|
|
Operation *parent = cppRegion->getParentOp();
|
|
|
|
intptr_t next = cppRegion->getRegionNumber() + 1;
|
|
|
|
if (parent->getNumRegions() > next)
|
|
|
|
return wrap(&parent->getRegion(next));
|
|
|
|
return wrap(static_cast<Region *>(nullptr));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-23 20:27:43 -06:00
|
|
|
void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
|
|
|
|
MlirValue newValue) {
|
|
|
|
unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
|
|
|
|
}
|
|
|
|
|
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];
|
2021-11-18 05:23:32 +00:00
|
|
|
return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
|
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));
|
|
|
|
}
|
|
|
|
|
2023-04-29 05:35:53 -07:00
|
|
|
MlirBytecodeWriterResult mlirOperationWriteBytecode(MlirOperation op,
|
|
|
|
MlirStringCallback callback,
|
|
|
|
void *userData) {
|
|
|
|
detail::CallbackOstream stream(callback, userData);
|
|
|
|
MlirBytecodeWriterResult res;
|
|
|
|
BytecodeWriterResult r = writeBytecodeToFile(unwrap(op), stream);
|
|
|
|
res.minVersion = r.minVersion;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirBytecodeWriterResult mlirOperationWriteBytecodeWithConfig(
|
|
|
|
MlirOperation op, MlirBytecodeWriterConfig config,
|
|
|
|
MlirStringCallback callback, void *userData) {
|
2022-09-05 11:54:19 +00:00
|
|
|
detail::CallbackOstream stream(callback, userData);
|
2023-04-29 05:35:53 -07:00
|
|
|
BytecodeWriterResult r =
|
|
|
|
writeBytecodeToFile(unwrap(op), stream, *unwrap(config));
|
|
|
|
MlirBytecodeWriterResult res;
|
|
|
|
res.minVersion = r.minVersion;
|
|
|
|
return res;
|
2022-09-05 11:54:19 +00:00
|
|
|
}
|
|
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2021-10-31 09:37:20 +01:00
|
|
|
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
|
|
|
|
return unwrap(op)->moveAfter(unwrap(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
|
|
|
|
return unwrap(op)->moveBefore(unwrap(other));
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:08:34 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Region API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-08-05 14:36:16 +02:00
|
|
|
|
|
|
|
MlirRegion mlirRegionCreate() { return wrap(new Region); }
|
|
|
|
|
2021-08-28 20:15:51 -07:00
|
|
|
bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
|
|
|
|
return unwrap(region) == unwrap(other);
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:36:16 +02:00
|
|
|
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
|
|
|
|
2022-01-18 18:28:51 -08:00
|
|
|
MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
|
|
|
|
MlirLocation const *locs) {
|
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)
|
2022-01-18 18:28:51 -08:00
|
|
|
b->addArgument(unwrap(args[i]), unwrap(locs[i]));
|
2020-08-05 14:36:16 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-08-28 20:15:51 -07:00
|
|
|
MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
|
|
|
|
return wrap(unwrap(block)->getParent());
|
|
|
|
}
|
|
|
|
|
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); }
|
|
|
|
|
2022-04-06 10:06:30 -07:00
|
|
|
void mlirBlockDetach(MlirBlock block) {
|
|
|
|
Block *b = unwrap(block);
|
|
|
|
b->getParent()->getBlocks().remove(b);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-18 18:28:51 -08:00
|
|
|
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
|
|
|
|
MlirLocation loc) {
|
|
|
|
return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
|
2021-02-03 12:34:29 -08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:56:08 -07:00
|
|
|
MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
|
|
|
|
Value cppValue = unwrap(value);
|
|
|
|
if (cppValue.use_empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
OpOperand *opOperand = cppValue.use_begin().getOperand();
|
|
|
|
|
|
|
|
return wrap(opOperand);
|
|
|
|
}
|
|
|
|
|
2023-04-26 09:55:27 -05:00
|
|
|
void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
|
|
|
|
unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:56:08 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OpOperand API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
|
|
|
|
|
|
|
|
MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
|
|
|
|
return wrap(unwrap(opOperand)->getOwner());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
|
|
|
|
return unwrap(opOperand)->getOperandNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
|
|
|
|
if (mlirOpOperandIsNull(opOperand))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
OpOperand *nextOpOperand = static_cast<OpOperand *>(
|
|
|
|
unwrap(opOperand)->getNextOperandUsingThisValue());
|
|
|
|
|
|
|
|
if (!nextOpOperand)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return wrap(nextOpOperand);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-09-30 18:14:00 -06:00
|
|
|
MlirTypeID mlirTypeGetTypeID(MlirType type) {
|
|
|
|
return wrap(unwrap(type).getTypeID());
|
|
|
|
}
|
|
|
|
|
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) {
|
[mlir] Remove types from attributes
This patch removes the `type` field from `Attribute` along with the
`Attribute::getType` accessor.
Going forward, this means that attributes in MLIR will no longer have
types as a first-class concept. This patch lays the groundwork to
incrementally remove or refactor code that relies on generic attributes
being typed. The immediate impact will be on attributes that rely on
`Attribute` containing a type, such as `IntegerAttr`,
`DenseElementsAttr`, and `ml_program::ExternAttr`, which will now need
to define a type parameter on their storage classes. This will save
memory as all other attribute kinds will no longer contain a type.
Moreover, it will not be possible to generically query the type of an
attribute directly. This patch provides an attribute interface
`TypedAttr` that implements only one method, `getType`, which can be
used to generically query the types of attributes that implement the
interface. This interface can be used to retain the concept of a "typed
attribute". The ODS-generated accessor for a `type` parameter
automatically implements this method.
Next steps will be to refactor the assembly formats of certain operations
that rely on `parseAttribute(type)` and `printAttributeWithoutType` to
remove special handling of type elision until `type` can be removed from
the dialect parsing hook entirely; and incrementally remove uses of
`TypedAttr`.
Reviewed By: lattner, rriddle, jpienaar
Differential Revision: https://reviews.llvm.org/D130092
2022-07-18 21:32:38 -07:00
|
|
|
Attribute attr = unwrap(attribute);
|
|
|
|
if (auto typedAttr = attr.dyn_cast<TypedAttr>())
|
|
|
|
return wrap(typedAttr.getType());
|
|
|
|
return wrap(NoneType::get(attr.getContext()));
|
2020-10-15 17:31:31 -07:00
|
|
|
}
|
|
|
|
|
2021-09-30 18:14:00 -06:00
|
|
|
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
|
|
|
|
return wrap(unwrap(attr).getTypeID());
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-11-16 17:21:15 +00:00
|
|
|
return wrap(StringAttr::get(unwrap(context), unwrap(str)));
|
2020-11-02 18:26:38 +00:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
2021-09-30 18:14:00 -06:00
|
|
|
|
2021-11-02 12:39:36 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol and SymbolTable API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
MlirStringRef mlirSymbolTableGetSymbolAttributeName() {
|
|
|
|
return wrap(SymbolTable::getSymbolAttrName());
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:30:18 -08:00
|
|
|
MlirStringRef mlirSymbolTableGetVisibilityAttributeName() {
|
|
|
|
return wrap(SymbolTable::getVisibilityAttrName());
|
|
|
|
}
|
|
|
|
|
2021-11-02 12:39:36 +01:00
|
|
|
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
|
|
|
|
if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
|
|
|
|
return wrap(static_cast<SymbolTable *>(nullptr));
|
|
|
|
return wrap(new SymbolTable(unwrap(operation)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
|
|
|
|
delete unwrap(symbolTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
|
|
|
|
MlirStringRef name) {
|
|
|
|
return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
|
|
|
|
MlirOperation operation) {
|
2021-11-11 01:44:58 +00:00
|
|
|
return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
|
2021-11-02 12:39:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mlirSymbolTableErase(MlirSymbolTable symbolTable,
|
|
|
|
MlirOperation operation) {
|
|
|
|
unwrap(symbolTable)->erase(unwrap(operation));
|
|
|
|
}
|
2021-11-28 20:30:18 -08:00
|
|
|
|
|
|
|
MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol,
|
|
|
|
MlirStringRef newSymbol,
|
|
|
|
MlirOperation from) {
|
2021-12-20 19:45:05 +00:00
|
|
|
auto *cppFrom = unwrap(from);
|
2021-11-28 20:30:18 -08:00
|
|
|
auto *context = cppFrom->getContext();
|
2022-01-12 11:20:18 -08:00
|
|
|
auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
|
|
|
|
auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
|
2021-11-28 20:30:18 -08:00
|
|
|
return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
|
|
|
|
unwrap(from)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
|
|
|
|
void (*callback)(MlirOperation, bool,
|
|
|
|
void *userData),
|
|
|
|
void *userData) {
|
|
|
|
SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
|
|
|
|
[&](Operation *foundOpCpp, bool isVisible) {
|
|
|
|
callback(wrap(foundOpCpp), isVisible,
|
|
|
|
userData);
|
|
|
|
});
|
|
|
|
}
|