| 1 | //===----------------------------------------------------------------------===// |
| 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 | // Similar to MLIR/LLVM's "opt" tools but also deals with analysis and custom |
| 10 | // arguments. TODO: this is basically a copy from MlirOptMain.cpp, but capable |
| 11 | // of module emission as specified by the user. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" |
| 16 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 17 | #include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 18 | #include "mlir/Dialect/OpenMP/Transforms/Passes.h" |
| 19 | #include "mlir/Pass/PassManager.h" |
| 20 | #include "mlir/Pass/PassOptions.h" |
| 21 | #include "mlir/Pass/PassRegistry.h" |
| 22 | #include "mlir/Tools/mlir-opt/MlirOptMain.h" |
| 23 | #include "mlir/Transforms/Passes.h" |
| 24 | #include "clang/CIR/Dialect/Passes.h" |
| 25 | #include "clang/CIR/InitAllDialects.h" |
| 26 | #include "clang/CIR/Passes.h" |
| 27 | |
| 28 | #ifdef CLANG_INCLUDE_TESTS |
| 29 | namespace cir::test { |
| 30 | void registerTestCIRAliasAnalysisPass(); |
| 31 | } // namespace cir::test |
| 32 | #endif |
| 33 | |
| 34 | struct CIRToLLVMPipelineOptions |
| 35 | : public mlir::PassPipelineOptions<CIRToLLVMPipelineOptions> { |
| 36 | Option<bool> enableOpenMP{ |
| 37 | *this, "enable-openmp" , |
| 38 | llvm::cl::desc("Add OpenMP-specific CIR-to-LLVM lowering passes" ), |
| 39 | llvm::cl::init(Val: false)}; |
| 40 | }; |
| 41 | |
| 42 | int main(int argc, char **argv) { |
| 43 | // TODO: register needed MLIR passes for CIR? |
| 44 | mlir::DialectRegistry registry; |
| 45 | cir::registerAllDialects(registry); |
| 46 | |
| 47 | #ifdef CLANG_INCLUDE_TESTS |
| 48 | cir::test::registerTestCIRAliasAnalysisPass(); |
| 49 | #endif |
| 50 | registry.insert<mlir::memref::MemRefDialect, mlir::LLVM::LLVMDialect>(); |
| 51 | |
| 52 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 53 | return mlir::createCIRCanonicalizePass(); |
| 54 | }); |
| 55 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 56 | return mlir::createCIRFoldEmptyBranchesPass(); |
| 57 | }); |
| 58 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 59 | return mlir::createCIRSimplifyPass(); |
| 60 | }); |
| 61 | |
| 62 | mlir::PassPipelineRegistration<CIRToLLVMPipelineOptions> pipeline( |
| 63 | "cir-to-llvm" , "" , |
| 64 | [](mlir::OpPassManager &pm, const CIRToLLVMPipelineOptions &options) { |
| 65 | cir::direct::populateCIRToLLVMPasses(pm, enableOpenMP: options.enableOpenMP); |
| 66 | }); |
| 67 | |
| 68 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 69 | return mlir::createCIRFlattenCFGPass(); |
| 70 | }); |
| 71 | |
| 72 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 73 | return mlir::createCIREHABILoweringPass(); |
| 74 | }); |
| 75 | |
| 76 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 77 | return mlir::createHoistAllocasPass(); |
| 78 | }); |
| 79 | |
| 80 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 81 | return mlir::createGotoSolverPass(); |
| 82 | }); |
| 83 | |
| 84 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 85 | return mlir::createCXXABILoweringPass(); |
| 86 | }); |
| 87 | |
| 88 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 89 | return mlir::createTargetLoweringPass(); |
| 90 | }); |
| 91 | |
| 92 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 93 | return mlir::createLoweringPreparePass(); |
| 94 | }); |
| 95 | |
| 96 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 97 | return mlir::createLibOptPass(); |
| 98 | }); |
| 99 | |
| 100 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 101 | return mlir::createIdiomRecognizerPass(); |
| 102 | }); |
| 103 | |
| 104 | ::mlir::registerPass(function: []() -> std::unique_ptr<::mlir::Pass> { |
| 105 | return mlir::createCallConvLoweringPass(); |
| 106 | }); |
| 107 | |
| 108 | mlir::omp::registerOpenMPPasses(); |
| 109 | mlir::registerTransformsPasses(); |
| 110 | |
| 111 | return mlir::asMainReturnCode(r: MlirOptMain( |
| 112 | argc, argv, toolName: "Clang IR analysis and optimization tool\n" , registry)); |
| 113 | } |
| 114 | |