Skip to content

Commit 2f58864

Browse files
authored
[flang][OpenMP] Extend do concurrent mapping to device. #50
For simple loops, we can now choose to map `do concurrent` to either the host (i.e. `omp parallel do`) or the device (i.e. `omp target teams distribute parallel do`). In order to use this from `flang-new`, you can pass: `-fdo-concurrent-parallel=[none|host|device]`.
2 parents a92e557 + 3bb1152 commit 2f58864

File tree

20 files changed

+670
-248
lines changed

20 files changed

+670
-248
lines changed

flang/lib/Lower/OpenMP/Utils.h renamed to flang/include/flang/Lower/OpenMP/Utils.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct OmpMapMemberIndicesData {
5959
};
6060

6161
mlir::omp::MapInfoOp
62-
createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
62+
createMapInfoOp(mlir::OpBuilder &builder, mlir::Location loc,
6363
mlir::Value baseAddr, mlir::Value varPtrPtr, std::string name,
6464
mlir::ArrayRef<mlir::Value> bounds,
6565
mlir::ArrayRef<mlir::Value> members,
@@ -102,6 +102,15 @@ void genObjectList(const ObjectList &objects,
102102
Fortran::lower::AbstractConverter &converter,
103103
llvm::SmallVectorImpl<mlir::Value> &operands);
104104

105+
// TODO: consider moving this to the `omp.loop_nest` op. Would be something like
106+
// this:
107+
//
108+
// ```
109+
// mlir::Value LoopNestOp::calculateTripCount(mlir::OpBuilder &builder,
110+
// mlir::OpBuilder::InsertPoint ip)
111+
// ```
112+
mlir::Value calculateTripCount(fir::FirOpBuilder &builder, mlir::Location loc,
113+
const mlir::omp::CollapseClauseOps &ops);
105114
} // namespace omp
106115
} // namespace lower
107116
} // namespace Fortran

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace fir {
3838
#define GEN_PASS_DECL_ARRAYVALUECOPY
3939
#define GEN_PASS_DECL_CHARACTERCONVERSION
4040
#define GEN_PASS_DECL_CFGCONVERSION
41+
#define GEN_PASS_DECL_DOCONCURRENTCONVERSIONPASS
4142
#define GEN_PASS_DECL_EXTERNALNAMECONVERSION
4243
#define GEN_PASS_DECL_MEMREFDATAFLOWOPT
4344
#define GEN_PASS_DECL_SIMPLIFYINTRINSICS
@@ -88,7 +89,7 @@ createFunctionAttrPass(FunctionAttrTypes &functionAttr, bool noInfsFPMath,
8889
bool noNaNsFPMath, bool approxFuncFPMath,
8990
bool noSignedZerosFPMath, bool unsafeFPMath);
9091

91-
std::unique_ptr<mlir::Pass> createDoConcurrentConversionPass();
92+
std::unique_ptr<mlir::Pass> createDoConcurrentConversionPass(bool mapToDevice);
9293

9394
void populateCfgConversionRewrites(mlir::RewritePatternSet &patterns,
9495
bool forceLoopToExecuteOnce = false);

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,12 @@ def DoConcurrentConversionPass : Pass<"fopenmp-do-concurrent-conversion", "mlir:
416416
target.
417417
}];
418418

419-
let constructor = "::fir::createDoConcurrentConversionPass()";
420419
let dependentDialects = ["mlir::omp::OpenMPDialect"];
420+
421+
let options = [
422+
Option<"mapTo", "map-to", "std::string", "",
423+
"Try to map `do concurrent` loops to OpenMP (on host or device)">,
424+
];
421425
}
422426

423427
#endif // FLANG_OPTIMIZER_TRANSFORMS_PASSES

flang/include/flang/Tools/CLOptions.inc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ inline void createHLFIRToFIRPassPipeline(
324324
pm.addPass(hlfir::createConvertHLFIRtoFIRPass());
325325
}
326326

327+
using DoConcurrentMappingKind =
328+
Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind;
329+
327330
/// Create a pass pipeline for handling certain OpenMP transformations needed
328331
/// prior to FIR lowering.
329332
///
@@ -333,8 +336,12 @@ inline void createHLFIRToFIRPassPipeline(
333336
/// \param pm - MLIR pass manager that will hold the pipeline definition.
334337
/// \param isTargetDevice - Whether code is being generated for a target device
335338
/// rather than the host device.
336-
inline void createOpenMPFIRPassPipeline(
337-
mlir::PassManager &pm, bool isTargetDevice) {
339+
inline void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
340+
bool isTargetDevice, DoConcurrentMappingKind doConcurrentMappingKind) {
341+
if (doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None)
342+
pm.addPass(fir::createDoConcurrentConversionPass(
343+
doConcurrentMappingKind == DoConcurrentMappingKind::DCMK_Device));
344+
338345
pm.addPass(fir::createOMPMapInfoFinalizationPass());
339346
pm.addPass(fir::createOMPMarkDeclareTargetPass());
340347
if (isTargetDevice)

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -320,41 +320,34 @@ bool CodeGenAction::beginSourceFileAction() {
320320
// Add OpenMP-related passes
321321
// WARNING: These passes must be run immediately after the lowering to ensure
322322
// that the FIR is correct with respect to OpenMP operations/attributes.
323-
bool isOpenMPEnabled = ci.getInvocation().getFrontendOpts().features.IsEnabled(
323+
bool isOpenMPEnabled =
324+
ci.getInvocation().getFrontendOpts().features.IsEnabled(
324325
Fortran::common::LanguageFeature::OpenMP);
326+
327+
using DoConcurrentMappingKind =
328+
Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind;
329+
DoConcurrentMappingKind doConcurrentMappingKind =
330+
ci.getInvocation().getCodeGenOpts().getDoConcurrentMapping();
331+
332+
if (doConcurrentMappingKind != DoConcurrentMappingKind::DCMK_None &&
333+
!isOpenMPEnabled) {
334+
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
335+
clang::DiagnosticsEngine::Warning,
336+
"lowering `do concurrent` loops to OpenMP is only supported if "
337+
"OpenMP is enabled");
338+
ci.getDiagnostics().Report(diagID);
339+
}
340+
325341
if (isOpenMPEnabled) {
326342
bool isDevice = false;
327343
if (auto offloadMod = llvm::dyn_cast<mlir::omp::OffloadModuleInterface>(
328344
mlirModule->getOperation()))
329345
isDevice = offloadMod.getIsTargetDevice();
346+
330347
// WARNING: This pipeline must be run immediately after the lowering to
331348
// ensure that the FIR is correct with respect to OpenMP operations/
332349
// attributes.
333-
fir::createOpenMPFIRPassPipeline(pm, isDevice);
334-
}
335-
336-
using DoConcurrentMappingKind =
337-
Fortran::frontend::CodeGenOptions::DoConcurrentMappingKind;
338-
DoConcurrentMappingKind selectedKind = ci.getInvocation().getCodeGenOpts().getDoConcurrentMapping();
339-
if (selectedKind != DoConcurrentMappingKind::DCMK_None) {
340-
if (!isOpenMPEnabled) {
341-
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
342-
clang::DiagnosticsEngine::Warning,
343-
"lowering `do concurrent` loops to OpenMP is only supported if "
344-
"OpenMP is enabled");
345-
ci.getDiagnostics().Report(diagID);
346-
} else {
347-
bool mapToDevice = selectedKind == DoConcurrentMappingKind::DCMK_Device;
348-
349-
if (mapToDevice) {
350-
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
351-
clang::DiagnosticsEngine::Warning,
352-
"TODO: lowering `do concurrent` loops to OpenMP device is not "
353-
"supported yet");
354-
ci.getDiagnostics().Report(diagID);
355-
} else
356-
pm.addPass(fir::createDoConcurrentConversionPass());
357-
}
350+
fir::createOpenMPFIRPassPipeline(pm, isDevice, doConcurrentMappingKind);
358351
}
359352

360353
pm.enableVerifier(/*verifyPasses=*/true);

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ClauseProcessor.h"
14-
#include "Clauses.h"
1514

15+
#include "flang/Lower/OpenMP/Clauses.h"
1616
#include "flang/Lower/PFTBuilder.h"
1717
#include "flang/Parser/tools.h"
1818
#include "flang/Semantics/tools.h"

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#ifndef FORTRAN_LOWER_CLAUASEPROCESSOR_H
1313
#define FORTRAN_LOWER_CLAUASEPROCESSOR_H
1414

15-
#include "Clauses.h"
1615
#include "DirectivesCommon.h"
1716
#include "ReductionProcessor.h"
18-
#include "Utils.h"
1917
#include "flang/Lower/AbstractConverter.h"
2018
#include "flang/Lower/Bridge.h"
19+
#include "flang/Lower/OpenMP/Clauses.h"
20+
#include "flang/Lower/OpenMP/Utils.h"
2121
#include "flang/Optimizer/Builder/Todo.h"
2222
#include "flang/Parser/dump-parse-tree.h"
2323
#include "flang/Parser/parse-tree.h"

flang/lib/Lower/OpenMP/Clauses.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "Clauses.h"
9+
#include "flang/Lower/OpenMP/Clauses.h"
1010

1111
#include "flang/Common/idioms.h"
1212
#include "flang/Evaluate/expression.h"

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "DataSharingProcessor.h"
1414

15-
#include "Utils.h"
15+
#include "flang/Lower/OpenMP/Utils.h"
1616
#include "flang/Lower/PFTBuilder.h"
1717
#include "flang/Lower/SymbolMap.h"
1818
#include "flang/Optimizer/Builder/HLFIRTools.h"

0 commit comments

Comments
 (0)