1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//===- Pass.cpp - C Interface for General Pass Management 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/Pass.h"
#include "mlir/CAPI/IR.h"
#include "mlir/CAPI/Pass.h"
#include "mlir/CAPI/Support.h"
#include "mlir/CAPI/Utils.h"
#include "mlir/Pass/PassManager.h"
#include <optional>
using namespace mlir;
//===----------------------------------------------------------------------===//
// PassManager/OpPassManager APIs.
//===----------------------------------------------------------------------===//
MlirPassManager mlirPassManagerCreate(MlirContext ctx) {
return wrap(new PassManager(unwrap(ctx)));
}
MlirPassManager mlirPassManagerCreateOnOperation(MlirContext ctx,
MlirStringRef anchorOp) {
return wrap(new PassManager(unwrap(ctx), unwrap(anchorOp)));
}
void mlirPassManagerDestroy(MlirPassManager passManager) {
delete unwrap(passManager);
}
MlirOpPassManager
mlirPassManagerGetAsOpPassManager(MlirPassManager passManager) {
return wrap(static_cast<OpPassManager *>(unwrap(passManager)));
}
MlirLogicalResult mlirPassManagerRunOnOp(MlirPassManager passManager,
MlirOperation op) {
return wrap(unwrap(passManager)->run(unwrap(op)));
}
void mlirPassManagerEnableIRPrinting(MlirPassManager passManager) {
return unwrap(passManager)->enableIRPrinting();
}
void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable) {
unwrap(passManager)->enableVerifier(enable);
}
MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager,
MlirStringRef operationName) {
return wrap(&unwrap(passManager)->nest(unwrap(operationName)));
}
MlirOpPassManager mlirOpPassManagerGetNestedUnder(MlirOpPassManager passManager,
MlirStringRef operationName) {
return wrap(&unwrap(passManager)->nest(unwrap(operationName)));
}
void mlirPassManagerAddOwnedPass(MlirPassManager passManager, MlirPass pass) {
unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass)));
}
void mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager,
MlirPass pass) {
unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass)));
}
MlirLogicalResult mlirOpPassManagerAddPipeline(MlirOpPassManager passManager,
MlirStringRef pipelineElements,
MlirStringCallback callback,
void *userData) {
detail::CallbackOstream stream(callback, userData);
return wrap(parsePassPipeline(unwrap(pipelineElements), *unwrap(passManager),
stream));
}
void mlirPrintPassPipeline(MlirOpPassManager passManager,
MlirStringCallback callback, void *userData) {
detail::CallbackOstream stream(callback, userData);
unwrap(passManager)->printAsTextualPipeline(stream);
}
MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager,
MlirStringRef pipeline,
MlirStringCallback callback,
void *userData) {
detail::CallbackOstream stream(callback, userData);
FailureOr<OpPassManager> pm = parsePassPipeline(unwrap(pipeline), stream);
if (succeeded(pm))
*unwrap(passManager) = std::move(*pm);
return wrap(pm);
}
//===----------------------------------------------------------------------===//
// External Pass API.
//===----------------------------------------------------------------------===//
namespace mlir {
class ExternalPass;
} // namespace mlir
DEFINE_C_API_PTR_METHODS(MlirExternalPass, mlir::ExternalPass)
namespace mlir {
/// This pass class wraps external passes defined in other languages using the
/// MLIR C-interface
class ExternalPass : public Pass {
public:
ExternalPass(TypeID passID, StringRef name, StringRef argument,
StringRef description, std::optional<StringRef> opName,
ArrayRef<MlirDialectHandle> dependentDialects,
MlirExternalPassCallbacks callbacks, void *userData)
: Pass(passID, opName), id(passID), name(name), argument(argument),
description(description), dependentDialects(dependentDialects),
callbacks(callbacks), userData(userData) {
callbacks.construct(userData);
}
~ExternalPass() override { callbacks.destruct(userData); }
StringRef getName() const override { return name; }
StringRef getArgument() const override { return argument; }
StringRef getDescription() const override { return description; }
void getDependentDialects(DialectRegistry ®istry) const override {
MlirDialectRegistry cRegistry = wrap(®istry);
for (MlirDialectHandle dialect : dependentDialects)
mlirDialectHandleInsertDialect(dialect, cRegistry);
}
void signalPassFailure() { Pass::signalPassFailure(); }
protected:
LogicalResult initialize(MLIRContext *ctx) override {
if (callbacks.initialize)
return unwrap(callbacks.initialize(wrap(ctx), userData));
return success();
}
bool canScheduleOn(RegisteredOperationName opName) const override {
if (std::optional<StringRef> specifiedOpName = getOpName())
return opName.getStringRef() == specifiedOpName;
return true;
}
void runOnOperation() override {
callbacks.run(wrap(getOperation()), wrap(this), userData);
}
std::unique_ptr<Pass> clonePass() const override {
void *clonedUserData = callbacks.clone(userData);
return std::make_unique<ExternalPass>(id, name, argument, description,
getOpName(), dependentDialects,
callbacks, clonedUserData);
}
private:
TypeID id;
std::string name;
std::string argument;
std::string description;
std::vector<MlirDialectHandle> dependentDialects;
MlirExternalPassCallbacks callbacks;
void *userData;
};
} // namespace mlir
MlirPass mlirCreateExternalPass(MlirTypeID passID, MlirStringRef name,
MlirStringRef argument,
MlirStringRef description, MlirStringRef opName,
intptr_t nDependentDialects,
MlirDialectHandle *dependentDialects,
MlirExternalPassCallbacks callbacks,
void *userData) {
return wrap(static_cast<mlir::Pass *>(new mlir::ExternalPass(
unwrap(passID), unwrap(name), unwrap(argument), unwrap(description),
opName.length > 0 ? std::optional<StringRef>(unwrap(opName))
: std::nullopt,
{dependentDialects, static_cast<size_t>(nDependentDialects)}, callbacks,
userData)));
}
void mlirExternalPassSignalFailure(MlirExternalPass pass) {
unwrap(pass)->signalPassFailure();
}
|