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 23 MlirPassManager mlirPassManagerCreate(MlirContext ctx) { 24 return wrap(new PassManager(unwrap(ctx))); 25 } 26 27 void mlirPassManagerDestroy(MlirPassManager passManager) { 28 delete unwrap(passManager); 29 } 30 31 MlirOpPassManager 32 mlirPassManagerGetAsOpPassManager(MlirPassManager passManager) { 33 return wrap(static_cast<OpPassManager *>(unwrap(passManager))); 34 } 35 36 MlirLogicalResult mlirPassManagerRun(MlirPassManager passManager, 37 MlirModule module) { 38 return wrap(unwrap(passManager)->run(unwrap(module))); 39 } 40 41 MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager, 42 MlirStringRef operationName) { 43 return wrap(&unwrap(passManager)->nest(unwrap(operationName))); 44 } 45 46 MlirOpPassManager mlirOpPassManagerGetNestedUnder(MlirOpPassManager passManager, 47 MlirStringRef operationName) { 48 return wrap(&unwrap(passManager)->nest(unwrap(operationName))); 49 } 50 51 void mlirPassManagerAddOwnedPass(MlirPassManager passManager, MlirPass pass) { 52 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass))); 53 } 54 55 void mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager, 56 MlirPass pass) { 57 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass))); 58 } 59 60 void mlirPrintPassPipeline(MlirOpPassManager passManager, 61 MlirStringCallback callback, void *userData) { 62 detail::CallbackOstream stream(callback, userData); 63 unwrap(passManager)->printAsTextualPipeline(stream); 64 } 65 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