Skip to content

Commit a1a5e7d

Browse files
committed
[Flang][Lower] Add lowering support of OpenMP distribute to MLIR
This patch adds support for lowering the OpenMP distribute directive from PFT to MLIR. This in turn unlocks support for several related combined loop constructs as well.
1 parent 223d3da commit a1a5e7d

File tree

6 files changed

+2181
-105
lines changed

6 files changed

+2181
-105
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ class ClauseProcessor {
565565
bool processDevice(Fortran::lower::StatementContext &stmtCtx,
566566
mlir::Value &result) const;
567567
bool processDeviceType(mlir::omp::DeclareTargetDeviceType &result) const;
568+
bool processDistSchedule(Fortran::lower::StatementContext &stmtCtx,
569+
mlir::UnitAttr &scheduleStatic,
570+
mlir::Value &chunkSize) const;
568571
bool processFinal(Fortran::lower::StatementContext &stmtCtx,
569572
mlir::Value &result) const;
570573
bool processHint(mlir::IntegerAttr &result) const;
@@ -1490,6 +1493,19 @@ bool ClauseProcessor::processDeviceType(
14901493
return false;
14911494
}
14921495

1496+
bool ClauseProcessor::processDistSchedule(
1497+
Fortran::lower::StatementContext &stmtCtx, mlir::UnitAttr &scheduleStatic,
1498+
mlir::Value &chunkSize) const {
1499+
if (auto *distScheduleClause = findUniqueClause<ClauseTy::DistSchedule>()) {
1500+
scheduleStatic = converter.getFirOpBuilder().getUnitAttr();
1501+
if (const auto *expr = Fortran::semantics::GetExpr(distScheduleClause->v)) {
1502+
chunkSize = fir::getBase(converter.genExprValue(*expr, stmtCtx));
1503+
}
1504+
return true;
1505+
}
1506+
return false;
1507+
}
1508+
14931509
bool ClauseProcessor::processFinal(Fortran::lower::StatementContext &stmtCtx,
14941510
mlir::Value &result) const {
14951511
const Fortran::parser::CharBlock *source = nullptr;
@@ -2997,6 +3013,27 @@ genTeamsOp(Fortran::lower::AbstractConverter &converter,
29973013
reductionDeclSymbols));
29983014
}
29993015

3016+
static mlir::omp::DistributeOp
3017+
genDistributeOp(Fortran::lower::AbstractConverter &converter,
3018+
Fortran::lower::pft::Evaluation &eval, bool genNested,
3019+
mlir::Location currentLocation,
3020+
const Fortran::parser::OmpClauseList &clauseList,
3021+
bool outerCombined = false) {
3022+
Fortran::lower::StatementContext stmtCtx;
3023+
mlir::UnitAttr scheduleStatic;
3024+
mlir::Value chunkSize;
3025+
llvm::SmallVector<mlir::Value> allocateOperands, allocatorOperands;
3026+
3027+
ClauseProcessor cp(converter, clauseList);
3028+
cp.processDistSchedule(stmtCtx, scheduleStatic, chunkSize);
3029+
cp.processAllocate(allocatorOperands, allocateOperands);
3030+
3031+
return genOpWithBody<mlir::omp::DistributeOp>(
3032+
converter, eval, genNested, currentLocation, outerCombined, &clauseList,
3033+
scheduleStatic, chunkSize, allocateOperands, allocatorOperands,
3034+
/*order_val=*/nullptr);
3035+
}
3036+
30003037
/// Extract the list of function and variable symbols affected by the given
30013038
/// 'declare target' directive and return the intended device type for them.
30023039
static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
@@ -3358,7 +3395,9 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
33583395
}
33593396
if (llvm::omp::allDistributeSet.test(ompDirective)) {
33603397
validDirective = true;
3361-
TODO(currentLocation, "Distribute construct");
3398+
bool outerCombined = llvm::omp::topDistributeSet.test(ompDirective);
3399+
genDistributeOp(converter, eval, /*genNested=*/false, currentLocation,
3400+
loopOpClauseList, outerCombined);
33623401
}
33633402
if ((llvm::omp::allParallelSet & llvm::omp::loopConstructSet)
33643403
.test(ompDirective)) {

0 commit comments

Comments
 (0)