Skip to content

Commit 0b898b9

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 d424631 commit 0b898b9

File tree

4 files changed

+1169
-53
lines changed

4 files changed

+1169
-53
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ class ClauseProcessor {
497497
bool processDevice(Fortran::lower::StatementContext &stmtCtx,
498498
mlir::Value &result) const;
499499
bool processDeviceType(mlir::omp::DeclareTargetDeviceType &result) const;
500+
bool processDistSchedule(Fortran::lower::StatementContext &stmtCtx,
501+
mlir::UnitAttr &scheduleStatic,
502+
mlir::Value &chunkSize) const;
500503
bool processFinal(Fortran::lower::StatementContext &stmtCtx,
501504
mlir::Value &result) const;
502505
bool processHint(mlir::IntegerAttr &result) const;
@@ -1335,6 +1338,19 @@ bool ClauseProcessor::processDeviceType(
13351338
return false;
13361339
}
13371340

1341+
bool ClauseProcessor::processDistSchedule(
1342+
Fortran::lower::StatementContext &stmtCtx, mlir::UnitAttr &scheduleStatic,
1343+
mlir::Value &chunkSize) const {
1344+
if (auto *distScheduleClause = findUniqueClause<ClauseTy::DistSchedule>()) {
1345+
scheduleStatic = converter.getFirOpBuilder().getUnitAttr();
1346+
if (const auto *expr = Fortran::semantics::GetExpr(distScheduleClause->v)) {
1347+
chunkSize = fir::getBase(converter.genExprValue(*expr, stmtCtx));
1348+
}
1349+
return true;
1350+
}
1351+
return false;
1352+
}
1353+
13381354
bool ClauseProcessor::processFinal(Fortran::lower::StatementContext &stmtCtx,
13391355
mlir::Value &result) const {
13401356
const Fortran::parser::CharBlock *source = nullptr;
@@ -2473,6 +2489,27 @@ genTeamsOp(Fortran::lower::AbstractConverter &converter,
24732489
reductionDeclSymbols));
24742490
}
24752491

2492+
static mlir::omp::DistributeOp
2493+
genDistributeOp(Fortran::lower::AbstractConverter &converter,
2494+
Fortran::lower::pft::Evaluation &eval,
2495+
mlir::Location currentLocation,
2496+
const Fortran::parser::OmpClauseList &clauseList,
2497+
bool outerCombined = false) {
2498+
Fortran::lower::StatementContext stmtCtx;
2499+
mlir::UnitAttr scheduleStatic;
2500+
mlir::Value chunkSize;
2501+
llvm::SmallVector<mlir::Value> allocateOperands, allocatorOperands;
2502+
2503+
ClauseProcessor cp(converter, clauseList);
2504+
cp.processDistSchedule(stmtCtx, scheduleStatic, chunkSize);
2505+
cp.processAllocate(allocatorOperands, allocateOperands);
2506+
2507+
return genOpWithBody<mlir::omp::DistributeOp>(
2508+
converter, eval, currentLocation, outerCombined, &clauseList,
2509+
scheduleStatic, chunkSize, allocateOperands, allocatorOperands,
2510+
/*order_val=*/nullptr);
2511+
}
2512+
24762513
/// Extract the list of function and variable symbols affected by the given
24772514
/// 'declare target' directive and return the intended device type for them.
24782515
static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
@@ -2681,7 +2718,9 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
26812718
}
26822719
if (llvm::omp::allDistributeSet.test(ompDirective)) {
26832720
validDirective = true;
2684-
TODO(currentLocation, "Distribute construct");
2721+
bool outerCombined = llvm::omp::topDistributeSet.test(ompDirective);
2722+
genDistributeOp(converter, eval, currentLocation, loopOpClauseList,
2723+
outerCombined);
26852724
}
26862725
if ((llvm::omp::allParallelSet & llvm::omp::loopConstructSet)
26872726
.test(ompDirective)) {

0 commit comments

Comments
 (0)