Skip to content

Commit cab5085

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 3f5fcb5 commit cab5085

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
@@ -568,6 +568,9 @@ class ClauseProcessor {
568568
bool processDevice(Fortran::lower::StatementContext &stmtCtx,
569569
mlir::Value &result) const;
570570
bool processDeviceType(mlir::omp::DeclareTargetDeviceType &result) const;
571+
bool processDistSchedule(Fortran::lower::StatementContext &stmtCtx,
572+
mlir::UnitAttr &scheduleStatic,
573+
mlir::Value &chunkSize) const;
571574
bool processFinal(Fortran::lower::StatementContext &stmtCtx,
572575
mlir::Value &result) const;
573576
bool processHint(mlir::IntegerAttr &result) const;
@@ -1493,6 +1496,19 @@ bool ClauseProcessor::processDeviceType(
14931496
return false;
14941497
}
14951498

1499+
bool ClauseProcessor::processDistSchedule(
1500+
Fortran::lower::StatementContext &stmtCtx, mlir::UnitAttr &scheduleStatic,
1501+
mlir::Value &chunkSize) const {
1502+
if (auto *distScheduleClause = findUniqueClause<ClauseTy::DistSchedule>()) {
1503+
scheduleStatic = converter.getFirOpBuilder().getUnitAttr();
1504+
if (const auto *expr = Fortran::semantics::GetExpr(distScheduleClause->v)) {
1505+
chunkSize = fir::getBase(converter.genExprValue(*expr, stmtCtx));
1506+
}
1507+
return true;
1508+
}
1509+
return false;
1510+
}
1511+
14961512
bool ClauseProcessor::processFinal(Fortran::lower::StatementContext &stmtCtx,
14971513
mlir::Value &result) const {
14981514
const Fortran::parser::CharBlock *source = nullptr;
@@ -3003,6 +3019,27 @@ genTeamsOp(Fortran::lower::AbstractConverter &converter,
30033019
reductionDeclSymbols));
30043020
}
30053021

3022+
static mlir::omp::DistributeOp
3023+
genDistributeOp(Fortran::lower::AbstractConverter &converter,
3024+
Fortran::lower::pft::Evaluation &eval, bool genNested,
3025+
mlir::Location currentLocation,
3026+
const Fortran::parser::OmpClauseList &clauseList,
3027+
bool outerCombined = false) {
3028+
Fortran::lower::StatementContext stmtCtx;
3029+
mlir::UnitAttr scheduleStatic;
3030+
mlir::Value chunkSize;
3031+
llvm::SmallVector<mlir::Value> allocateOperands, allocatorOperands;
3032+
3033+
ClauseProcessor cp(converter, clauseList);
3034+
cp.processDistSchedule(stmtCtx, scheduleStatic, chunkSize);
3035+
cp.processAllocate(allocatorOperands, allocateOperands);
3036+
3037+
return genOpWithBody<mlir::omp::DistributeOp>(
3038+
converter, eval, genNested, currentLocation, outerCombined, &clauseList,
3039+
scheduleStatic, chunkSize, allocateOperands, allocatorOperands,
3040+
/*order_val=*/nullptr);
3041+
}
3042+
30063043
/// Extract the list of function and variable symbols affected by the given
30073044
/// 'declare target' directive and return the intended device type for them.
30083045
static mlir::omp::DeclareTargetDeviceType getDeclareTargetInfo(
@@ -3387,7 +3424,9 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
33873424
}
33883425
if (llvm::omp::allDistributeSet.test(ompDirective)) {
33893426
validDirective = true;
3390-
TODO(currentLocation, "Distribute construct");
3427+
bool outerCombined = llvm::omp::topDistributeSet.test(ompDirective);
3428+
genDistributeOp(converter, eval, /*genNested=*/false, currentLocation,
3429+
loopOpClauseList, outerCombined);
33913430
}
33923431
if ((llvm::omp::allParallelSet & llvm::omp::loopConstructSet)
33933432
.test(ompDirective)) {

0 commit comments

Comments
 (0)