Skip to content

Commit e56dbd6

Browse files
committed
[flang] Lower omp.workshare to other omp constructs
Change to workshare loop wrapper op Move single op declaration Schedule pass properly Correctly handle nested nested loop nests to be parallelized by workshare Leave comments for shouldUseWorkshareLowering Use copyprivate to scatter val from omp.single TODO still need to implement copy function TODO transitive check for usage outside of omp.single not imiplemented yet Transitively check for users outisde of single op TODO need to implement copy func TODO need to hoist allocas outside of single regions Add tests Hoist allocas More tests Emit body for copy func Test the tmp storing logic Clean up trivially dead ops Only handle single-block regions for now Fix tests for custom assembly for loop wrapper Only run the lower workshare pass if openmp is enabled Implement some missing functionality Fix tests Fix test Iterate backwards to find all trivially dead ops Add expalanation comment for createCopyFun Update test
1 parent d5fbe9c commit e56dbd6

File tree

16 files changed

+915
-4
lines changed

16 files changed

+915
-4
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
@@ -37,4 +37,9 @@ def FunctionFiltering : Pass<"omp-function-filtering"> {
3737
];
3838
}
3939

40+
// Needs to be scheduled on Module as we create functions in it
41+
def LowerWorkshare : Pass<"lower-workshare", "::mlir::ModuleOp"> {
42+
let summary = "Lower workshare construct";
43+
}
44+
4045
#endif //FORTRAN_OPTIMIZER_OPENMP_PASSES

flang/include/flang/Tools/CLOptions.inc

Lines changed: 4 additions & 2 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,6 +354,8 @@ inline void createHLFIRToFIRPassPipeline(
354354
pm.addPass(hlfir::createLowerHLFIRIntrinsics());
355355
pm.addPass(hlfir::createBufferizeHLFIR());
356356
pm.addPass(hlfir::createConvertHLFIRtoFIR());
357+
if (enableOpenMP)
358+
pm.addPass(flangomp::createLowerWorkshare());
357359
}
358360

359361
/// Create a pass pipeline for handling certain OpenMP transformations needed
@@ -425,7 +427,7 @@ inline void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
425427
/// passes pipeline
426428
inline void createMLIRToLLVMPassPipeline(mlir::PassManager &pm,
427429
MLIRToLLVMPassPipelineConfig &config, llvm::StringRef inputFilename = {}) {
428-
fir::createHLFIRToFIRPassPipeline(pm, config.OptLevel);
430+
fir::createHLFIRToFIRPassPipeline(pm, config.EnableOpenMP, config.OptLevel);
429431

430432
// Add default optimizer pass pipeline.
431433
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/lib/Optimizer/OpenMP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_flang_library(FlangOpenMPTransforms
44
FunctionFiltering.cpp
55
MapInfoFinalization.cpp
66
MarkDeclareTarget.cpp
7+
LowerWorkshare.cpp
78

89
DEPENDS
910
FIRDialect

0 commit comments

Comments
 (0)