Skip to content

Commit 16b39fb

Browse files
committed
Only run the lower workshare pass if openmp is enabled
1 parent c03a385 commit 16b39fb

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

flang/include/flang/Tools/CLOptions.inc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ inline void createDefaultFIROptimizerPassPipeline(
337337
/// \param optLevel - optimization level used for creating FIR optimization
338338
/// passes pipeline
339339
inline void createHLFIRToFIRPassPipeline(
340-
mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel) {
340+
mlir::PassManager &pm, bool enableOpenMP, llvm::OptimizationLevel optLevel = defaultOptLevel) {
341341
if (optLevel.isOptimizingForSpeed()) {
342342
addCanonicalizerPassWithoutRegionSimplification(pm);
343343
addNestedPassToAllTopLevelOperations(
@@ -354,7 +354,8 @@ inline void createHLFIRToFIRPassPipeline(
354354
pm.addPass(hlfir::createLowerHLFIRIntrinsics());
355355
pm.addPass(hlfir::createBufferizeHLFIR());
356356
pm.addPass(hlfir::createConvertHLFIRtoFIR());
357-
pm.addPass(flangomp::createLowerWorkshare());
357+
if (enableOpenMP)
358+
pm.addPass(flangomp::createLowerWorkshare());
358359
}
359360

360361
/// Create a pass pipeline for handling certain OpenMP transformations needed
@@ -426,7 +427,7 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
426427
/// passes pipeline
427428
inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
428429
MLIRToLLVMPassPipelineConfig &config, llvm::StringRef inputFilename = {}) {
429-
fir::createHLFIRToFIRPassPipeline(pm, config.OptLevel);
430+
fir::createHLFIRToFIRPassPipeline(pm, config.EnableOpenMP, config.OptLevel);
430431

431432
// Add default optimizer pass pipeline.
432433
fir::createDefaultFIROptimizerPassPipeline(pm, config);

flang/include/flang/Tools/CrossToolHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct MLIRToLLVMPassPipelineConfig : public FlangEPCallBacks {
123123
false; ///< Set no-signed-zeros-fp-math attribute for functions.
124124
bool UnsafeFPMath = false; ///< Set unsafe-fp-math attribute for functions.
125125
bool NSWOnLoopVarInc = false; ///< Add nsw flag to loop variable increments.
126+
bool EnableOpenMP = false; ///< Enable OpenMP lowering.
126127
};
127128

128129
struct OffloadModuleOpts {

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,11 @@ void CodeGenAction::lowerHLFIRToFIR() {
711711
pm.enableVerifier(/*verifyPasses=*/true);
712712

713713
// Create the pass pipeline
714-
fir::createHLFIRToFIRPassPipeline(pm, level);
714+
fir::createHLFIRToFIRPassPipeline(
715+
pm,
716+
ci.getInvocation().getFrontendOpts().features.IsEnabled(
717+
Fortran::common::LanguageFeature::OpenMP),
718+
level);
715719
(void)mlir::applyPassManagerCLOptions(pm);
716720

717721
if (!mlir::succeeded(pm.run(*mlirModule))) {
@@ -824,6 +828,10 @@ void CodeGenAction::generateLLVMIR() {
824828
config.VScaleMax = vsr->second;
825829
}
826830

831+
if (ci.getInvocation().getFrontendOpts().features.IsEnabled(
832+
Fortran::common::LanguageFeature::OpenMP))
833+
config.EnableOpenMP = true;
834+
827835
if (ci.getInvocation().getLoweringOpts().getNSWOnLoopVarInc())
828836
config.NSWOnLoopVarInc = true;
829837

flang/tools/bbc/bbc.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
440440

441441
if (emitFIR && useHLFIR) {
442442
// lower HLFIR to FIR
443-
fir::createHLFIRToFIRPassPipeline(pm, llvm::OptimizationLevel::O2);
443+
fir::createHLFIRToFIRPassPipeline(pm, enableOpenMP,
444+
llvm::OptimizationLevel::O2);
444445
if (mlir::failed(pm.run(mlirModule))) {
445446
llvm::errs() << "FATAL: lowering from HLFIR to FIR failed";
446447
return mlir::failure();
@@ -455,6 +456,8 @@ static llvm::LogicalResult convertFortranSourceToMLIR(
455456

456457
// Add O2 optimizer pass pipeline.
457458
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
459+
if (enableOpenMP)
460+
config.EnableOpenMP = true;
458461
config.NSWOnLoopVarInc = setNSW;
459462
fir::registerDefaultInlinerPass(config);
460463
fir::createDefaultFIROptimizerPassPipeline(pm, config);

flang/tools/tco/tco.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
138138
return mlir::failure();
139139
} else {
140140
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
141+
config.EnableOpenMP = true; // assume the input contains OpenMP
141142
config.AliasAnalysis = true; // enabled when optimizing for speed
142143
if (codeGenLLVM) {
143144
// Run only CodeGen passes.

0 commit comments

Comments
 (0)