Skip to content

[flang][NFCI]Use config structure for MLIR to LLVM pass creation #67792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions flang/include/flang/Tools/CLOptions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "flang/Optimizer/CodeGen/CodeGen.h"
#include "flang/Optimizer/HLFIR/Passes.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Passes/OptimizationLevel.h"
#include "llvm/Support/CommandLine.h"

Expand Down Expand Up @@ -184,29 +183,28 @@ inline void addExternalNameConversionPass(
/// incremental conversion of FIR.
///
/// \param pm - MLIR pass manager that will hold the pipeline definition
inline void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
llvm::OptimizationLevel optLevel = defaultOptLevel,
bool stackArrays = false, bool loopVersioning = false) {
inline void createDefaultFIROptimizerPassPipeline(
mlir::PassManager &pm, const MLIRToLLVMPassPipelineConfig &pc) {
// simplify the IR
mlir::GreedyRewriteConfig config;
config.enableRegionSimplification = false;
pm.addPass(mlir::createCSEPass());
fir::addAVC(pm, optLevel);
fir::addAVC(pm, pc.OptLevel);
pm.addNestedPass<mlir::func::FuncOp>(fir::createCharacterConversionPass());
pm.addPass(mlir::createCanonicalizerPass(config));
pm.addPass(fir::createSimplifyRegionLitePass());
if (optLevel.isOptimizingForSpeed()) {
if (pc.OptLevel.isOptimizingForSpeed()) {
// These passes may increase code size.
pm.addPass(fir::createSimplifyIntrinsicsPass());
pm.addPass(fir::createAlgebraicSimplificationPass(config));
}

if (loopVersioning)
if (pc.LoopVersioning)
pm.addPass(fir::createLoopVersioningPass());

pm.addPass(mlir::createCSEPass());

if (stackArrays)
if (pc.StackArrays)
pm.addPass(fir::createStackArraysPass());
else
fir::addMemoryAllocationOpt(pm);
Expand Down Expand Up @@ -291,40 +289,33 @@ inline void createDebugPasses(
}
}

inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
llvm::OptimizationLevel optLevel = defaultOptLevel,
bool underscoring = true,
llvm::codegenoptions::DebugInfoKind debugInfo = NoDebugInfo) {
inline void createDefaultFIRCodeGenPassPipeline(
mlir::PassManager &pm, MLIRToLLVMPassPipelineConfig config) {
fir::addBoxedProcedurePass(pm);
pm.addNestedPass<mlir::func::FuncOp>(
fir::createAbstractResultOnFuncOptPass());
pm.addNestedPass<fir::GlobalOp>(fir::createAbstractResultOnGlobalOptPass());
fir::addCodeGenRewritePass(pm);
fir::addTargetRewritePass(pm);
fir::addExternalNameConversionPass(pm, underscoring);
fir::createDebugPasses(pm, debugInfo);
fir::addFIRToLLVMPass(pm, optLevel);
fir::addExternalNameConversionPass(pm, config.Underscoring);
fir::createDebugPasses(pm, config.DebugInfo);
fir::addFIRToLLVMPass(pm, config.OptLevel);
}

/// Create a pass pipeline for lowering from MLIR to LLVM IR
///
/// \param pm - MLIR pass manager that will hold the pipeline definition
/// \param optLevel - optimization level used for creating FIR optimization
/// passes pipeline
inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
llvm::OptimizationLevel optLevel = defaultOptLevel,
bool stackArrays = false, bool underscoring = true,
bool loopVersioning = false,
llvm::codegenoptions::DebugInfoKind debugInfo = NoDebugInfo) {
fir::createHLFIRToFIRPassPipeline(pm, optLevel);
inline void createMLIRToLLVMPassPipeline(
mlir::PassManager &pm, const MLIRToLLVMPassPipelineConfig &config) {
fir::createHLFIRToFIRPassPipeline(pm, config.OptLevel);

// Add default optimizer pass pipeline.
fir::createDefaultFIROptimizerPassPipeline(
pm, optLevel, stackArrays, loopVersioning);
fir::createDefaultFIROptimizerPassPipeline(pm, config);

// Add codegen pass pipeline.
fir::createDefaultFIRCodeGenPassPipeline(
pm, optLevel, underscoring, debugInfo);
fir::createDefaultFIRCodeGenPassPipeline(pm, config);
}
#undef FLANG_EXCLUDE_CODEGEN
#endif
Expand Down
25 changes: 25 additions & 0 deletions flang/include/flang/Tools/CrossToolHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,36 @@
#ifndef FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H
#define FORTRAN_TOOLS_CROSS_TOOL_HELPERS_H

#include "flang/Frontend/CodeGenOptions.h"
#include "flang/Frontend/LangOptions.h"
#include <cstdint>

#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/IR/BuiltinOps.h"
#include "llvm/Frontend/Debug/Options.h"
#include "llvm/Passes/OptimizationLevel.h"

/// Configuriation for the MLIR to LLVM pass pipeline.
struct MLIRToLLVMPassPipelineConfig {
explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level) {
OptLevel = level;
}
explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level,
const Fortran::frontend::CodeGenOptions &opts) {
OptLevel = level;
StackArrays = opts.StackArrays;
Underscoring = opts.Underscoring;
LoopVersioning = opts.LoopVersioning;
DebugInfo = opts.getDebugInfo();
}

llvm::OptimizationLevel OptLevel; ///< optimisation level
bool StackArrays = false; ///< convert memory allocations to alloca.
bool Underscoring = true; ///< add underscores to function names.
bool LoopVersioning = false; ///< Run the version loop pass.
llvm::codegenoptions::DebugInfoKind DebugInfo =
llvm::codegenoptions::NoDebugInfo; ///< Debug info generation.
};

struct OffloadModuleOpts {
OffloadModuleOpts() {}
Expand Down
6 changes: 3 additions & 3 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,10 @@ void CodeGenAction::generateLLVMIR() {
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
pm.enableVerifier(/*verifyPasses=*/true);

MLIRToLLVMPassPipelineConfig config(level, opts);

// Create the pass pipeline
fir::createMLIRToLLVMPassPipeline(pm, level, opts.StackArrays,
opts.Underscoring, opts.LoopVersioning,
opts.getDebugInfo());
fir::createMLIRToLLVMPassPipeline(pm, config);
(void)mlir::applyPassManagerCLOptions(pm);

// run the pass manager
Expand Down
3 changes: 2 additions & 1 deletion flang/tools/bbc/bbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ static mlir::LogicalResult convertFortranSourceToMLIR(
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());

// Add O2 optimizer pass pipeline.
fir::createDefaultFIROptimizerPassPipeline(pm, llvm::OptimizationLevel::O2);
fir::createDefaultFIROptimizerPassPipeline(
pm, MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel::O2));
}

if (mlir::succeeded(pm.run(mlirModule))) {
Expand Down
6 changes: 4 additions & 2 deletions flang/tools/tco/tco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "flang/Optimizer/Support/InitFIR.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "flang/Tools/CrossToolHelpers.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
Expand Down Expand Up @@ -118,12 +119,13 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
if (mlir::failed(passPipeline.addToPipeline(pm, errorHandler)))
return mlir::failure();
} else {
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
if (codeGenLLVM) {
// Run only CodeGen passes.
fir::createDefaultFIRCodeGenPassPipeline(pm);
fir::createDefaultFIRCodeGenPassPipeline(pm, config);
} else {
// Run tco with O2 by default.
fir::createMLIRToLLVMPassPipeline(pm, llvm::OptimizationLevel::O2);
fir::createMLIRToLLVMPassPipeline(pm, config);
}
fir::addLLVMDialectToLLVMPass(pm, out.os());
}
Expand Down