|
| 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/Optimizer/CodeGen/CodeGen.h" |
| 16 | +#include "flang/Optimizer/HLFIR/Passes.h" |
| 17 | +#include "flang/Optimizer/OpenMP/Passes.h" |
| 18 | +#include "flang/Optimizer/Passes/CommandLineOpts.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 fir { |
| 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 | +void addNestedPassToAllTopLevelOperations(mlir::PassManager &pm, |
| 63 | + PassConstructor ctor); |
| 64 | + |
| 65 | +void addNestedPassToAllTopLevelOperationsConditionally( |
| 66 | + mlir::PassManager &pm, llvm::cl::opt<bool> &disabled, PassConstructor ctor); |
| 67 | + |
| 68 | +/// Add MLIR Canonicalizer pass with region simplification disabled. |
| 69 | +/// FIR does not support the promotion of some SSA value to block arguments (or |
| 70 | +/// into arith.select operands) that may be done by mlir block merging in the |
| 71 | +/// region simplification (e.g., !fir.shape<> SSA values are not supported as |
| 72 | +/// block arguments). |
| 73 | +/// Aside from the fir.shape issue, moving some abstract SSA value into block |
| 74 | +/// arguments may have a heavy cost since it forces their code generation that |
| 75 | +/// may be expensive (array temporary). The MLIR pass does not take these |
| 76 | +/// extra costs into account when doing block merging. |
| 77 | +void addCanonicalizerPassWithoutRegionSimplification(mlir::OpPassManager &pm); |
| 78 | + |
| 79 | +void addCfgConversionPass(mlir::PassManager &pm, |
| 80 | + const MLIRToLLVMPassPipelineConfig &config); |
| 81 | + |
| 82 | +void addAVC(mlir::PassManager &pm, const llvm::OptimizationLevel &optLevel); |
| 83 | + |
| 84 | +void addMemoryAllocationOpt(mlir::PassManager &pm); |
| 85 | + |
| 86 | +void addCodeGenRewritePass(mlir::PassManager &pm, bool preserveDeclare); |
| 87 | + |
| 88 | +void addTargetRewritePass(mlir::PassManager &pm); |
| 89 | + |
| 90 | +mlir::LLVM::DIEmissionKind |
| 91 | +getEmissionKind(llvm::codegenoptions::DebugInfoKind kind); |
| 92 | + |
| 93 | +void addBoxedProcedurePass(mlir::PassManager &pm); |
| 94 | + |
| 95 | +void addExternalNameConversionPass(mlir::PassManager &pm, |
| 96 | + bool appendUnderscore = true); |
| 97 | + |
| 98 | +void addCompilerGeneratedNamesConversionPass(mlir::PassManager &pm); |
| 99 | + |
| 100 | +void addDebugInfoPass(mlir::PassManager &pm, |
| 101 | + llvm::codegenoptions::DebugInfoKind debugLevel, |
| 102 | + llvm::OptimizationLevel optLevel, |
| 103 | + llvm::StringRef inputFilename); |
| 104 | + |
| 105 | +void addFIRToLLVMPass(mlir::PassManager &pm, |
| 106 | + const MLIRToLLVMPassPipelineConfig &config); |
| 107 | + |
| 108 | +void addLLVMDialectToLLVMPass(mlir::PassManager &pm, llvm::raw_ostream &output); |
| 109 | + |
| 110 | +/// Use inliner extension point callback to register the default inliner pass. |
| 111 | +void registerDefaultInlinerPass(MLIRToLLVMPassPipelineConfig &config); |
| 112 | + |
| 113 | +/// Create a pass pipeline for running default optimization passes for |
| 114 | +/// incremental conversion of FIR. |
| 115 | +/// |
| 116 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 117 | +void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm, |
| 118 | + MLIRToLLVMPassPipelineConfig &pc); |
| 119 | + |
| 120 | +/// Create a pass pipeline for lowering from HLFIR to FIR |
| 121 | +/// |
| 122 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 123 | +/// \param optLevel - optimization level used for creating FIR optimization |
| 124 | +/// passes pipeline |
| 125 | +void createHLFIRToFIRPassPipeline( |
| 126 | + mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel); |
| 127 | + |
| 128 | +/// Create a pass pipeline for handling certain OpenMP transformations needed |
| 129 | +/// prior to FIR lowering. |
| 130 | +/// |
| 131 | +/// WARNING: These passes must be run immediately after the lowering to ensure |
| 132 | +/// that the FIR is correct with respect to OpenMP operations/attributes. |
| 133 | +/// |
| 134 | +/// \param pm - MLIR pass manager that will hold the pipeline definition. |
| 135 | +/// \param isTargetDevice - Whether code is being generated for a target device |
| 136 | +/// rather than the host device. |
| 137 | +void createOpenMPFIRPassPipeline(mlir::PassManager &pm, bool isTargetDevice); |
| 138 | + |
| 139 | +#if !defined(FLANG_EXCLUDE_CODEGEN) |
| 140 | +void createDebugPasses(mlir::PassManager &pm, |
| 141 | + llvm::codegenoptions::DebugInfoKind debugLevel, |
| 142 | + llvm::OptimizationLevel OptLevel, |
| 143 | + llvm::StringRef inputFilename); |
| 144 | + |
| 145 | +void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm, |
| 146 | + MLIRToLLVMPassPipelineConfig config, |
| 147 | + llvm::StringRef inputFilename = {}); |
| 148 | + |
| 149 | +/// Create a pass pipeline for lowering from MLIR to LLVM IR |
| 150 | +/// |
| 151 | +/// \param pm - MLIR pass manager that will hold the pipeline definition |
| 152 | +/// \param optLevel - optimization level used for creating FIR optimization |
| 153 | +/// passes pipeline |
| 154 | +void createMLIRToLLVMPassPipeline(mlir::PassManager &pm, |
| 155 | + MLIRToLLVMPassPipelineConfig &config, |
| 156 | + llvm::StringRef inputFilename = {}); |
| 157 | +#undef FLANG_EXCLUDE_CODEGEN |
| 158 | +#endif |
| 159 | + |
| 160 | +} // namespace fir |
| 161 | + |
| 162 | +#endif // FORTRAN_OPTIMIZER_PASSES_PIPELINES_H |
0 commit comments