1 //===- Pass.cpp - C Interface for General Pass Management APIs ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "mlir-c/Pass.h"
10
11 #include "mlir/CAPI/IR.h"
12 #include "mlir/CAPI/Pass.h"
13 #include "mlir/CAPI/Support.h"
14 #include "mlir/CAPI/Utils.h"
15 #include "mlir/Pass/PassManager.h"
16
17 using namespace mlir;
18
19 //===----------------------------------------------------------------------===//
20 // PassManager/OpPassManager APIs.
21 //===----------------------------------------------------------------------===//
22
mlirPassManagerCreate(MlirContext ctx)23 MlirPassManager mlirPassManagerCreate(MlirContext ctx) {
24 return wrap(new PassManager(unwrap(ctx)));
25 }
26
mlirPassManagerDestroy(MlirPassManager passManager)27 void mlirPassManagerDestroy(MlirPassManager passManager) {
28 delete unwrap(passManager);
29 }
30
31 MlirOpPassManager
mlirPassManagerGetAsOpPassManager(MlirPassManager passManager)32 mlirPassManagerGetAsOpPassManager(MlirPassManager passManager) {
33 return wrap(static_cast<OpPassManager *>(unwrap(passManager)));
34 }
35
mlirPassManagerRun(MlirPassManager passManager,MlirModule module)36 MlirLogicalResult mlirPassManagerRun(MlirPassManager passManager,
37 MlirModule module) {
38 return wrap(unwrap(passManager)->run(unwrap(module)));
39 }
40
mlirPassManagerGetNestedUnder(MlirPassManager passManager,MlirStringRef operationName)41 MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager,
42 MlirStringRef operationName) {
43 return wrap(&unwrap(passManager)->nest(unwrap(operationName)));
44 }
45
mlirOpPassManagerGetNestedUnder(MlirOpPassManager passManager,MlirStringRef operationName)46 MlirOpPassManager mlirOpPassManagerGetNestedUnder(MlirOpPassManager passManager,
47 MlirStringRef operationName) {
48 return wrap(&unwrap(passManager)->nest(unwrap(operationName)));
49 }
50
mlirPassManagerAddOwnedPass(MlirPassManager passManager,MlirPass pass)51 void mlirPassManagerAddOwnedPass(MlirPassManager passManager, MlirPass pass) {
52 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass)));
53 }
54
mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager,MlirPass pass)55 void mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager,
56 MlirPass pass) {
57 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass)));
58 }
59
mlirPrintPassPipeline(MlirOpPassManager passManager,MlirStringCallback callback,void * userData)60 void mlirPrintPassPipeline(MlirOpPassManager passManager,
61 MlirStringCallback callback, void *userData) {
62 detail::CallbackOstream stream(callback, userData);
63 unwrap(passManager)->printAsTextualPipeline(stream);
64 }
65
mlirParsePassPipeline(MlirOpPassManager passManager,MlirStringRef pipeline)66 MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager,
67 MlirStringRef pipeline) {
68 // TODO: errors are sent to std::errs() at the moment, we should pass in a
69 // stream and redirect to a diagnostic.
70 return wrap(mlir::parsePassPipeline(unwrap(pipeline), *unwrap(passManager)));
71 }
72