Skip to content

Commit

Permalink
[Flang][OpenMP] NFC: Share DataSharingProcessor creation logic for al…
Browse files Browse the repository at this point in the history
…l loop directives (llvm#97565)

This patch moves the logic associated with the creation of a
`DataSharingProcessor` instance for loop-associated OpenMP leaf
constructs to the `genOMPDispatch` function, avoiding code duplication
for standalone and composite loop constructs.

This also prevents privatization-related allocations to be later made
inside of loop wrappers when support for composite constructs is
implemented.
  • Loading branch information
skatrak committed Jul 5, 2024
1 parent 0fbb320 commit 720b958
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,12 +1316,9 @@ static mlir::omp::DistributeOp
genDistributeOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval, mlir::Location loc,
const ConstructQueue &queue, ConstructQueue::iterator item) {
const ConstructQueue &queue, ConstructQueue::iterator item,
DataSharingProcessor &dsp) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
symTable.pushScope();
DataSharingProcessor dsp(converter, semaCtx, item->clauses, eval,
lower::omp::isLastItemInQueue(item, queue));
dsp.processStep1();

lower::StatementContext stmtCtx;
mlir::omp::LoopNestClauseOps loopClauseOps;
Expand Down Expand Up @@ -1359,7 +1356,6 @@ genDistributeOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
.setGenRegionEntryCb(ivCallback),
queue, item);

symTable.popScope();
return distributeOp;
}

Expand Down Expand Up @@ -1580,12 +1576,8 @@ static mlir::omp::SimdOp
genSimdOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item) {
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
symTable.pushScope();
DataSharingProcessor dsp(converter, semaCtx, item->clauses, eval,
lower::omp::isLastItemInQueue(item, queue));
dsp.processStep1();

lower::StatementContext stmtCtx;
mlir::omp::LoopNestClauseOps loopClauseOps;
Expand Down Expand Up @@ -1622,7 +1614,6 @@ genSimdOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
.setGenRegionEntryCb(ivCallback),
queue, item);

symTable.popScope();
return simdOp;
}

Expand Down Expand Up @@ -1861,7 +1852,8 @@ static mlir::omp::TaskloopOp
genTaskloopOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval, mlir::Location loc,
const ConstructQueue &queue, ConstructQueue::iterator item) {
const ConstructQueue &queue, ConstructQueue::iterator item,
DataSharingProcessor &dsp) {
TODO(loc, "Taskloop construct");
}

Expand Down Expand Up @@ -1904,12 +1896,8 @@ static mlir::omp::WsloopOp
genWsloopOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item) {
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
symTable.pushScope();
DataSharingProcessor dsp(converter, semaCtx, item->clauses, eval,
lower::omp::isLastItemInQueue(item, queue));
dsp.processStep1();

lower::StatementContext stmtCtx;
mlir::omp::LoopNestClauseOps loopClauseOps;
Expand Down Expand Up @@ -1950,7 +1938,7 @@ genWsloopOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
.setDataSharingProcessor(&dsp)
.setGenRegionEntryCb(ivCallback),
queue, item);
symTable.popScope();

return wsloopOp;
}

Expand All @@ -1962,25 +1950,23 @@ static void genCompositeDistributeParallelDo(
lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item) {
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
TODO(loc, "Composite DISTRIBUTE PARALLEL DO");
}

static void genCompositeDistributeParallelDoSimd(
lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item) {
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
TODO(loc, "Composite DISTRIBUTE PARALLEL DO SIMD");
}

static void genCompositeDistributeSimd(lower::AbstractConverter &converter,
lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval,
mlir::Location loc,
const ConstructQueue &queue,
ConstructQueue::iterator item) {
static void genCompositeDistributeSimd(
lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
TODO(loc, "Composite DISTRIBUTE SIMD");
}

Expand All @@ -1989,7 +1975,8 @@ static void genCompositeDoSimd(lower::AbstractConverter &converter,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval, mlir::Location loc,
const ConstructQueue &queue,
ConstructQueue::iterator item) {
ConstructQueue::iterator item,
DataSharingProcessor &dsp) {
ClauseProcessor cp(converter, semaCtx, item->clauses);
cp.processTODO<clause::Aligned, clause::Allocate, clause::Linear,
clause::Safelen, clause::Simdlen>(loc,
Expand All @@ -2002,16 +1989,14 @@ static void genCompositeDoSimd(lower::AbstractConverter &converter,
// When support for vectorization is enabled, then we need to add handling of
// if clause. Currently if clause can be skipped because we always assume
// SIMD length = 1.
genWsloopOp(converter, symTable, semaCtx, eval, loc, queue, item);
genWsloopOp(converter, symTable, semaCtx, eval, loc, queue, item, dsp);
}

static void genCompositeTaskloopSimd(lower::AbstractConverter &converter,
lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval,
mlir::Location loc,
const ConstructQueue &queue,
ConstructQueue::iterator item) {
static void genCompositeTaskloopSimd(
lower::AbstractConverter &converter, lower::SymMap &symTable,
semantics::SemanticsContext &semaCtx, lower::pft::Evaluation &eval,
mlir::Location loc, const ConstructQueue &queue,
ConstructQueue::iterator item, DataSharingProcessor &dsp) {
TODO(loc, "Composite TASKLOOP SIMD");
}

Expand All @@ -2027,15 +2012,27 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
ConstructQueue::iterator item) {
assert(item != queue.end());

std::optional<DataSharingProcessor> loopDsp;
bool loopLeaf = llvm::omp::getDirectiveAssociation(item->id) ==
llvm::omp::Association::Loop;
if (loopLeaf) {
symTable.pushScope();
loopDsp.emplace(converter, semaCtx, item->clauses, eval,
/*shouldCollectPreDeterminedSymbols=*/true,
enableDelayedPrivatization, &symTable);
loopDsp->processStep1();
}

switch (llvm::omp::Directive dir = item->id) {
case llvm::omp::Directive::OMPD_barrier:
genBarrierOp(converter, symTable, semaCtx, eval, loc, queue, item);
break;
case llvm::omp::Directive::OMPD_distribute:
genDistributeOp(converter, symTable, semaCtx, eval, loc, queue, item);
genDistributeOp(converter, symTable, semaCtx, eval, loc, queue, item,
*loopDsp);
break;
case llvm::omp::Directive::OMPD_do:
genWsloopOp(converter, symTable, semaCtx, eval, loc, queue, item);
genWsloopOp(converter, symTable, semaCtx, eval, loc, queue, item, *loopDsp);
break;
case llvm::omp::Directive::OMPD_loop:
case llvm::omp::Directive::OMPD_masked:
Expand All @@ -2058,7 +2055,7 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
genSectionsOp(converter, symTable, semaCtx, eval, loc, queue, item);
break;
case llvm::omp::Directive::OMPD_simd:
genSimdOp(converter, symTable, semaCtx, eval, loc, queue, item);
genSimdOp(converter, symTable, semaCtx, eval, loc, queue, item, *loopDsp);
break;
case llvm::omp::Directive::OMPD_single:
genSingleOp(converter, symTable, semaCtx, eval, loc, queue, item);
Expand Down Expand Up @@ -2088,7 +2085,8 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
genTaskgroupOp(converter, symTable, semaCtx, eval, loc, queue, item);
break;
case llvm::omp::Directive::OMPD_taskloop:
genTaskloopOp(converter, symTable, semaCtx, eval, loc, queue, item);
genTaskloopOp(converter, symTable, semaCtx, eval, loc, queue, item,
*loopDsp);
break;
case llvm::omp::Directive::OMPD_taskwait:
genTaskwaitOp(converter, symTable, semaCtx, eval, loc, queue, item);
Expand All @@ -2114,26 +2112,30 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
// Composite constructs
case llvm::omp::Directive::OMPD_distribute_parallel_do:
genCompositeDistributeParallelDo(converter, symTable, semaCtx, eval, loc,
queue, item);
queue, item, *loopDsp);
break;
case llvm::omp::Directive::OMPD_distribute_parallel_do_simd:
genCompositeDistributeParallelDoSimd(converter, symTable, semaCtx, eval,
loc, queue, item);
loc, queue, item, *loopDsp);
break;
case llvm::omp::Directive::OMPD_distribute_simd:
genCompositeDistributeSimd(converter, symTable, semaCtx, eval, loc, queue,
item);
item, *loopDsp);
break;
case llvm::omp::Directive::OMPD_do_simd:
genCompositeDoSimd(converter, symTable, semaCtx, eval, loc, queue, item);
genCompositeDoSimd(converter, symTable, semaCtx, eval, loc, queue, item,
*loopDsp);
break;
case llvm::omp::Directive::OMPD_taskloop_simd:
genCompositeTaskloopSimd(converter, symTable, semaCtx, eval, loc, queue,
item);
item, *loopDsp);
break;
default:
break;
}

if (loopLeaf)
symTable.popScope();
}

//===----------------------------------------------------------------------===//
Expand Down

0 comments on commit 720b958

Please sign in to comment.