Skip to content

Commit e7e5541

Browse files
authored
[flang] Lower omp.workshare to other omp constructs (llvm#101446)
Add a new pass that lowers an `omp.workshare` with its binding `omp.workshare.loop_wrapper` loop nests into other OpenMP constructs that can be lowered to LLVM. More specifically, in order to preserve the sequential execution semantics of the code contained, it wraps portions that needs to be executed on a single thread in `omp.single` blocks, converts code that must be parallelized into `omp.wsloop` nests and inserts the appropriate synchronization.
1 parent 8bb21ae commit e7e5541

20 files changed

+908
-5
lines changed

flang/include/flang/Optimizer/OpenMP/Passes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ namespace flangomp {
2525
#define GEN_PASS_REGISTRATION
2626
#include "flang/Optimizer/OpenMP/Passes.h.inc"
2727

28+
/// Impelements the logic specified in the 2.8.3 workshare Construct section of
29+
/// the OpenMP standard which specifies what statements or constructs shall be
30+
/// divided into units of work.
31+
bool shouldUseWorkshareLowering(mlir::Operation *op);
32+
2833
} // namespace flangomp
2934

3035
#endif // FORTRAN_OPTIMIZER_OPENMP_PASSES_H

flang/include/flang/Optimizer/OpenMP/Passes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ def FunctionFilteringPass : Pass<"omp-function-filtering"> {
5050
];
5151
}
5252

53+
// Needs to be scheduled on Module as we create functions in it
54+
def LowerWorkshare : Pass<"lower-workshare", "::mlir::ModuleOp"> {
55+
let summary = "Lower workshare construct";
56+
}
57+
5358
#endif //FORTRAN_OPTIMIZER_OPENMP_PASSES

flang/include/flang/Optimizer/Passes/Pipelines.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
123123
/// \param optLevel - optimization level used for creating FIR optimization
124124
/// passes pipeline
125125
void createHLFIRToFIRPassPipeline(
126-
mlir::PassManager &pm, llvm::OptimizationLevel optLevel = defaultOptLevel);
126+
mlir::PassManager &pm, bool enableOpenMP,
127+
llvm::OptimizationLevel optLevel = defaultOptLevel);
127128

128129
/// Create a pass pipeline for handling certain OpenMP transformations needed
129130
/// prior to FIR lowering.

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
@@ -715,7 +715,11 @@ void CodeGenAction::lowerHLFIRToFIR() {
715715
pm.enableVerifier(/*verifyPasses=*/true);
716716

717717
// Create the pass pipeline
718-
fir::createHLFIRToFIRPassPipeline(pm, level);
718+
fir::createHLFIRToFIRPassPipeline(
719+
pm,
720+
ci.getInvocation().getFrontendOpts().features.IsEnabled(
721+
Fortran::common::LanguageFeature::OpenMP),
722+
level);
719723
(void)mlir::applyPassManagerCLOptions(pm);
720724

721725
if (!mlir::succeeded(pm.run(*mlirModule))) {
@@ -828,6 +832,10 @@ void CodeGenAction::generateLLVMIR() {
828832
config.VScaleMax = vsr->second;
829833
}
830834

835+
if (ci.getInvocation().getFrontendOpts().features.IsEnabled(
836+
Fortran::common::LanguageFeature::OpenMP))
837+
config.EnableOpenMP = true;
838+
831839
if (ci.getInvocation().getLoweringOpts().getNSWOnLoopVarInc())
832840
config.NSWOnLoopVarInc = true;
833841

flang/lib/Optimizer/OpenMP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_flang_library(FlangOpenMPTransforms
55
MapsForPrivatizedSymbols.cpp
66
MapInfoFinalization.cpp
77
MarkDeclareTarget.cpp
8+
LowerWorkshare.cpp
89

910
DEPENDS
1011
FIRDialect

0 commit comments

Comments
 (0)