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 void mlirPassManagerEnableIRPrinting(MlirPassManager passManager) { 42 return unwrap(passManager)->enableIRPrinting(); 43 } 44 45 void mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable) { 46 unwrap(passManager)->enableVerifier(enable); 47 } 48 49 MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager, 50 MlirStringRef operationName) { 51 return wrap(&unwrap(passManager)->nest(unwrap(operationName))); 52 } 53 54 MlirOpPassManager mlirOpPassManagerGetNestedUnder(MlirOpPassManager passManager, 55 MlirStringRef operationName) { 56 return wrap(&unwrap(passManager)->nest(unwrap(operationName))); 57 } 58 59 void mlirPassManagerAddOwnedPass(MlirPassManager passManager, MlirPass pass) { 60 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass))); 61 } 62 63 void mlirOpPassManagerAddOwnedPass(MlirOpPassManager passManager, 64 MlirPass pass) { 65 unwrap(passManager)->addPass(std::unique_ptr<Pass>(unwrap(pass))); 66 } 67 68 void mlirPrintPassPipeline(MlirOpPassManager passManager, 69 MlirStringCallback callback, void *userData) { 70 detail::CallbackOstream stream(callback, userData); 71 unwrap(passManager)->printAsTextualPipeline(stream); 72 } 73 74 MlirLogicalResult mlirParsePassPipeline(MlirOpPassManager passManager, 75 MlirStringRef pipeline) { 76 // TODO: errors are sent to std::errs() at the moment, we should pass in a 77 // stream and redirect to a diagnostic. 78 return wrap(mlir::parsePassPipeline(unwrap(pipeline), *unwrap(passManager))); 79 } 80