-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-flang-driver ChangesThe CreateMLIRToLLVMPassPipeline function has quite a few arguments, all of which has default values. Create a struct, with a constructor for the default values, and pass that struct instead. Re-arrange a few include files to make everything available. No functional change intended. Full diff: https://github.com/llvm/llvm-project/pull/67792.diff 4 Files Affected:
diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc
index 616d9ddc066a75d..0b210bec9f785c8 100644
--- a/flang/include/flang/Tools/CLOptions.inc
+++ b/flang/include/flang/Tools/CLOptions.inc
@@ -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"
@@ -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);
@@ -291,19 +289,17 @@ 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
@@ -311,20 +307,15 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
/// \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
diff --git a/flang/include/flang/Tools/CrossToolHelpers.h b/flang/include/flang/Tools/CrossToolHelpers.h
index d43a1fccccb62fd..b16fd14d1d7f34d 100644
--- a/flang/include/flang/Tools/CrossToolHelpers.h
+++ b/flang/include/flang/Tools/CrossToolHelpers.h
@@ -18,6 +18,24 @@
#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 {
+ MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level) {
+ OptLevel = level;
+ StackArrays = false;
+ Underscoring = true;
+ LoopVersioning = false;
+ DebugInfo = llvm::codegenoptions::NoDebugInfo;
+ }
+ llvm::OptimizationLevel OptLevel; ///< optimisation level
+ bool StackArrays; ///< convert memory allocations to alloca.
+ bool Underscoring; ///< add underscores to function names.
+ bool LoopVersioning; ///< Run the version loop pass.
+ llvm::codegenoptions::DebugInfoKind DebugInfo; ///< Debug info generation.
+};
struct OffloadModuleOpts {
OffloadModuleOpts() {}
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 6cc7808a30d8a0f..5069e429a42d3b5 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -713,10 +713,18 @@ void CodeGenAction::generateLLVMIR() {
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
pm.enableVerifier(/*verifyPasses=*/true);
+ const auto targetOpts = ci.getInvocation().getTargetOpts();
+ const llvm::Triple triple(targetOpts.triple);
+
+ MLIRToLLVMPassPipelineConfig config(level);
+
+ config.StackArrays = opts.StackArrays;
+ config.Underscoring = opts.Underscoring;
+ config.LoopVersioning = opts.LoopVersioning;
+ config.DebugInfo = opts.getDebugInfo();
+
// 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
diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp
index 9552d5c9af9e30b..31d6bac142dc421 100644
--- a/flang/tools/tco/tco.cpp
+++ b/flang/tools/tco/tco.cpp
@@ -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"
@@ -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());
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice to see this, thanks! One minor nit: feel free to ignore if you don't like it.
@@ -713,10 +713,18 @@ void CodeGenAction::generateLLVMIR() { | |||
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>()); | |||
pm.enableVerifier(/*verifyPasses=*/true); | |||
|
|||
const auto targetOpts = ci.getInvocation().getTargetOpts(); | |||
const llvm::Triple triple(targetOpts.triple); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, it's from an unrelated change - should be moved out of this patch. [It is needed in the other change!]
struct MLIRToLLVMPassPipelineConfig { | ||
explicit MLIRToLLVMPassPipelineConfig(llvm::OptimizationLevel level) { | ||
OptLevel = level; | ||
} | ||
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. | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally you'd re-use https://github.com/llvm/llvm-project/blob/c4e2fcff788025415b523486efdbdac4f2b08c1e/flang/include/flang/Frontend/CodeGenOptions.def for this. But perhaps that would be too much refactoring 🤔 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a nice suggestion, but there's quite a lot of work to do that , and not something I think is worth doing right now. [and there's a whole load more fields in these options than we currently support in this structure].
3404ad0
to
6a8080e
Compare
The CreateMLIRToLLVMPassPipeline function has quite a few arguments, all of which has debug values. Create a struct, with a constructor for the default values, and pass that struct instead. Re-arrange a few include files to make everything available. No functional change intended.
6a8080e
to
71d9759
Compare
The CreateMLIRToLLVMPassPipeline function has quite a few arguments, all of which has default values. Create a struct, with a constructor for the default values, and pass that struct instead.
Re-arrange a few include files to make everything available.
No functional change intended.