|
| 1 | +//===-- Pipelines.h -- FIR pass pipelines -----------------------*- C++ -*-===// |
| 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 | +/// This file declares some utilties to setup FIR pass pipelines. These are |
| 10 | +/// common to flang and the test tools. |
| 11 | + |
| 12 | +#ifndef FORTRAN_OPTIMIZER_PASSES_PIPELINES_H |
| 13 | +#define FORTRAN_OPTIMIZER_PASSES_PIPELINES_H |
| 14 | + |
| 15 | +#include "flang/Common/CommandLineOpts.h" |
| 16 | +#include "flang/Optimizer/CodeGen/CodeGen.h" |
| 17 | +#include "flang/Optimizer/HLFIR/Passes.h" |
| 18 | +#include "flang/Optimizer/OpenMP/Passes.h" |
| 19 | +#include "flang/Optimizer/Transforms/Passes.h" |
| 20 | +#include "flang/Tools/CrossToolHelpers.h" |
| 21 | +#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" |
| 22 | +#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" |
| 23 | +#include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 24 | +#include "mlir/Pass/PassManager.h" |
| 25 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 26 | +#include "mlir/Transforms/Passes.h" |
| 27 | +#include "llvm/Frontend/Debug/Options.h" |
| 28 | +#include "llvm/Passes/OptimizationLevel.h" |
| 29 | +#include "llvm/Support/CommandLine.h" |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +using PassConstructor = std::unique_ptr<mlir::Pass>(); |
| 34 | + |
| 35 | +template <typename OP> |
| 36 | +void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) { |
| 37 | + pm.addNestedPass<OP>(ctor()); |
| 38 | +} |
| 39 | + |
| 40 | +template <typename OP, typename... OPS, |
| 41 | + typename = std::enable_if_t<sizeof...(OPS) != 0>> |
| 42 | +void addNestedPassToOps(mlir::PassManager &pm, PassConstructor ctor) { |
| 43 | + addNestedPassToOps<OP>(pm, ctor); |
| 44 | + addNestedPassToOps<OPS...>(pm, ctor); |
| 45 | +} |
| 46 | + |
| 47 | +/// Generic for adding a pass to the pass manager if it is not disabled. |
| 48 | +template <typename F> |
| 49 | +void addPassConditionally(mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, |
| 50 | + F ctor) { |
| 51 | + if (!disabled) |
| 52 | + pm.addPass(ctor()); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename OP, typename F> |
| 56 | +void addNestedPassConditionally(mlir::PassManager &pm, |
| 57 | + llvm::cl::opt<bool> &disabled, F ctor) { |
| 58 | + if (!disabled) |
| 59 | + pm.addNestedPass<OP>(ctor()); |
| 60 | +} |
| 61 | + |
| 62 | +} // namespace |
| 63 | + |
| 64 | +namespace fir { |
| 65 | + |
| 66 | +void addDebugInfoPass(mlir::PassManager &pm, |
| 67 | + llvm::codegenoptions::DebugInfoKind debugLevel, |
| 68 | + llvm::OptimizationLevel optLevel, |
| 69 | + llvm::StringRef inputFilename); |
| 70 | + |
| 71 | +void addFIRToLLVMPass(mlir::PassManager &pm, |
| 72 | + const MLIRToLLVMPassPipelineConfig &config); |
| 73 | + |
| 74 | +void addLLVMDialectToLLVMPass(mlir::PassManager &pm, llvm::raw_ostream &output); |
| 75 | + |
| 76 | +/// Use inliner extension point callback to register the default inliner pass. |
| 77 | +void registerDefaultInlinerPass(MLIRToLLVMPassPipelineConfig &config); |
| 78 | + |
| 79 | +/// Create a pass pipeline for running default optimization passes for |
| 80 | +/// incremental conversion of FIR. |
| 81 | +/// |
| 82 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 83 | +void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm, |
| 84 | + MLIRToLLVMPassPipelineConfig &pc); |
| 85 | + |
| 86 | +/// Create a pass pipeline for lowering from HLFIR to FIR |
| 87 | +/// |
| 88 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 89 | +/// \param optLevel - optimization level used for creating FIR optimization |
| 90 | +/// passes pipeline |
| 91 | +void createHLFIRToFIRPassPipeline( |
| 92 | + mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel); |
| 93 | + |
| 94 | +/// Create a pass pipeline for handling certain OpenMP transformations needed |
| 95 | +/// prior to FIR lowering. |
| 96 | +/// |
| 97 | +/// WARNING: These passes must be run immediately after the lowering to ensure |
| 98 | +/// that the FIR is correct with respect to OpenMP operations/attributes. |
| 99 | +/// |
| 100 | +/// \param pm - MLIR pass manager that will hold the pipeline definition. |
| 101 | +/// \param isTargetDevice - Whether code is being generated for a target device |
| 102 | +/// rather than the host device. |
| 103 | +void createOpenMPFIRPassPipeline(mlir::PassManager &pm, bool isTargetDevice); |
| 104 | + |
| 105 | +#if !defined(FLANG_EXCLUDE_CODEGEN) |
| 106 | +void createDebugPasses(mlir::PassManager &pm, |
| 107 | + llvm::codegenoptions::DebugInfoKind debugLevel, |
| 108 | + llvm::OptimizationLevel OptLevel, |
| 109 | + llvm::StringRef inputFilename); |
| 110 | + |
| 111 | +void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm, |
| 112 | + MLIRToLLVMPassPipelineConfig config, |
| 113 | + llvm::StringRef inputFilename = {}); |
| 114 | + |
| 115 | +/// Create a pass pipeline for lowering from MLIR to LLVM IR |
| 116 | +/// |
| 117 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 118 | +/// \param optLevel - optimization level used for creating FIR optimization |
| 119 | +/// passes pipeline |
| 120 | +void createMLIRToLLVMPassPipeline(mlir::PassManager &pm, |
| 121 | + MLIRToLLVMPassPipelineConfig &config, |
| 122 | + llvm::StringRef inputFilename = {}); |
| 123 | +#undef FLANG_EXCLUDE_CODEGEN |
| 124 | +#endif |
| 125 | + |
| 126 | +} // namespace fir |
| 127 | + |
| 128 | +#endif // FORTRAN_OPTIMIZER_PASSES_PIPELINES_H |
0 commit comments