Skip to content

Commit c5a9e35

Browse files
authored
[Flang][OpenMP] Push genEval calls to individual operations, NFC (#77758)
Introduce `genNestedEvaluations` that will lower all evaluations nested in the given, accouting for a potential COLLAPSE directive. Recursive lowering [2/5]
1 parent 5723fce commit c5a9e35

File tree

3 files changed

+89
-56
lines changed

3 files changed

+89
-56
lines changed

flang/include/flang/Lower/OpenMP.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ void genOpenMPTerminator(fir::FirOpBuilder &, mlir::Operation *,
5656
void genOpenMPConstruct(AbstractConverter &, Fortran::lower::SymMap &,
5757
semantics::SemanticsContext &, pft::Evaluation &,
5858
const parser::OpenMPConstruct &);
59-
void genOpenMPDeclarativeConstruct(AbstractConverter &, pft::Evaluation &,
59+
void genOpenMPDeclarativeConstruct(AbstractConverter &,
60+
Fortran::lower::SymMap &,
61+
semantics::SemanticsContext &,
62+
pft::Evaluation &,
6063
const parser::OpenMPDeclarativeConstruct &);
6164
/// Symbols in OpenMP code can have flags (e.g. threadprivate directive)
6265
/// that require additional handling when lowering the corresponding

flang/lib/Lower/Bridge.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
24402440
ompDeviceCodeFound =
24412441
ompDeviceCodeFound ||
24422442
Fortran::lower::isOpenMPDeviceDeclareTarget(*this, getEval(), ompDecl);
2443-
genOpenMPDeclarativeConstruct(*this, getEval(), ompDecl);
2443+
genOpenMPDeclarativeConstruct(
2444+
*this, localSymbols, bridge.getSemanticsContext(), getEval(), ompDecl);
24442445
builder->restoreInsertionPoint(insertPt);
24452446
}
24462447

flang/lib/Lower/OpenMP.cpp

Lines changed: 83 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ static void gatherFuncAndVarSyms(
110110
}
111111
}
112112

113+
static Fortran::lower::pft::Evaluation *
114+
getCollapsedEval(Fortran::lower::pft::Evaluation &eval, int collapseValue) {
115+
// Return the Evaluation of the innermost collapsed loop, or the current
116+
// evaluation, if there is nothing to collapse.
117+
if (collapseValue == 0)
118+
return &eval;
119+
120+
Fortran::lower::pft::Evaluation *curEval = &eval.getFirstNestedEvaluation();
121+
for (int i = 1; i < collapseValue; i++) {
122+
// The nested evaluations should be DoConstructs (i.e. they should form
123+
// a loop nest). Each DoConstruct is a tuple <NonLabelDoStmt, Block,
124+
// EndDoStmt>.
125+
assert(curEval->isA<Fortran::parser::DoConstruct>());
126+
curEval = &*std::next(curEval->getNestedEvaluations().begin());
127+
}
128+
return curEval;
129+
}
130+
131+
static void genNestedEvaluations(Fortran::lower::AbstractConverter &converter,
132+
Fortran::lower::pft::Evaluation &eval,
133+
int collapseValue = 0) {
134+
Fortran::lower::pft::Evaluation *curEval =
135+
getCollapsedEval(eval, collapseValue);
136+
137+
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
138+
converter.genEval(e);
139+
}
140+
113141
//===----------------------------------------------------------------------===//
114142
// DataSharingProcessor
115143
//===----------------------------------------------------------------------===//
@@ -2944,8 +2972,9 @@ genOmpFlush(Fortran::lower::AbstractConverter &converter,
29442972

29452973
static void
29462974
genOMP(Fortran::lower::AbstractConverter &converter,
2947-
Fortran::lower::pft::Evaluation &eval,
2975+
Fortran::lower::SymMap &symTable,
29482976
Fortran::semantics::SemanticsContext &semanticsContext,
2977+
Fortran::lower::pft::Evaluation &eval,
29492978
const Fortran::parser::OpenMPStandaloneConstruct &standaloneConstruct) {
29502979
std::visit(
29512980
Fortran::common::visitors{
@@ -3034,6 +3063,9 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
30343063
createBodyOfOp<mlir::omp::SimdLoopOp>(simdLoopOp, converter, loc, eval,
30353064
&loopOpClauseList, iv,
30363065
/*outer=*/false, &dsp);
3066+
3067+
genNestedEvaluations(converter, eval,
3068+
Fortran::lower::getCollapseValue(loopOpClauseList));
30373069
}
30383070

30393071
static void createWsLoop(Fortran::lower::AbstractConverter &converter,
@@ -3107,11 +3139,15 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
31073139
createBodyOfOp<mlir::omp::WsLoopOp>(wsLoopOp, converter, loc, eval,
31083140
&beginClauseList, iv,
31093141
/*outer=*/false, &dsp);
3142+
3143+
genNestedEvaluations(converter, eval,
3144+
Fortran::lower::getCollapseValue(beginClauseList));
31103145
}
31113146

31123147
static void genOMP(Fortran::lower::AbstractConverter &converter,
3113-
Fortran::lower::pft::Evaluation &eval,
3148+
Fortran::lower::SymMap &symTable,
31143149
Fortran::semantics::SemanticsContext &semanticsContext,
3150+
Fortran::lower::pft::Evaluation &eval,
31153151
const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
31163152
const auto &beginLoopDirective =
31173153
std::get<Fortran::parser::OmpBeginLoopDirective>(loopConstruct.t);
@@ -3179,12 +3215,15 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
31793215
createWsLoop(converter, eval, ompDirective, loopOpClauseList, endClauseList,
31803216
currentLocation);
31813217
}
3218+
3219+
genOpenMPReduction(converter, loopOpClauseList);
31823220
}
31833221

31843222
static void
31853223
genOMP(Fortran::lower::AbstractConverter &converter,
3186-
Fortran::lower::pft::Evaluation &eval,
3224+
Fortran::lower::SymMap &symTable,
31873225
Fortran::semantics::SemanticsContext &semanticsContext,
3226+
Fortran::lower::pft::Evaluation &eval,
31883227
const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
31893228
const auto &beginBlockDirective =
31903229
std::get<Fortran::parser::OmpBeginBlockDirective>(blockConstruct.t);
@@ -3298,10 +3337,15 @@ genOMP(Fortran::lower::AbstractConverter &converter,
32983337
break;
32993338
}
33003339
}
3340+
3341+
genNestedEvaluations(converter, eval);
3342+
genOpenMPReduction(converter, beginClauseList);
33013343
}
33023344

33033345
static void
33043346
genOMP(Fortran::lower::AbstractConverter &converter,
3347+
Fortran::lower::SymMap &symTable,
3348+
Fortran::semantics::SemanticsContext &semanticsContext,
33053349
Fortran::lower::pft::Evaluation &eval,
33063350
const Fortran::parser::OpenMPCriticalConstruct &criticalConstruct) {
33073351
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
@@ -3336,10 +3380,13 @@ genOMP(Fortran::lower::AbstractConverter &converter,
33363380
}();
33373381
createBodyOfOp<mlir::omp::CriticalOp>(criticalOp, converter, currentLocation,
33383382
eval);
3383+
genNestedEvaluations(converter, eval);
33393384
}
33403385

33413386
static void
33423387
genOMP(Fortran::lower::AbstractConverter &converter,
3388+
Fortran::lower::SymMap &symTable,
3389+
Fortran::semantics::SemanticsContext &semanticsContext,
33433390
Fortran::lower::pft::Evaluation &eval,
33443391
const Fortran::parser::OpenMPSectionConstruct &sectionConstruct) {
33453392
mlir::Location currentLocation = converter.getCurrentLocation();
@@ -3359,13 +3406,18 @@ genOMP(Fortran::lower::AbstractConverter &converter,
33593406
.t);
33603407
// Currently only private/firstprivate clause is handled, and
33613408
// all privatization is done within `omp.section` operations.
3409+
symTable.pushScope();
33623410
genOpWithBody<mlir::omp::SectionOp>(converter, eval, currentLocation,
33633411
/*outerCombined=*/false,
33643412
&sectionsClauseList);
3413+
genNestedEvaluations(converter, eval);
3414+
symTable.popScope();
33653415
}
33663416

33673417
static void
33683418
genOMP(Fortran::lower::AbstractConverter &converter,
3419+
Fortran::lower::SymMap &symTable,
3420+
Fortran::semantics::SemanticsContext &semanticsContext,
33693421
Fortran::lower::pft::Evaluation &eval,
33703422
const Fortran::parser::OpenMPSectionsConstruct &sectionsConstruct) {
33713423
mlir::Location currentLocation = converter.getCurrentLocation();
@@ -3406,10 +3458,14 @@ genOMP(Fortran::lower::AbstractConverter &converter,
34063458
/*reduction_vars=*/mlir::ValueRange(),
34073459
/*reductions=*/nullptr, allocateOperands,
34083460
allocatorOperands, nowaitClauseOperand);
3461+
3462+
genNestedEvaluations(converter, eval);
34093463
}
34103464

34113465
static void
34123466
genOMP(Fortran::lower::AbstractConverter &converter,
3467+
Fortran::lower::SymMap &symTable,
3468+
Fortran::semantics::SemanticsContext &semanticsContext,
34133469
Fortran::lower::pft::Evaluation &eval,
34143470
const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
34153471
std::visit(
@@ -3453,6 +3509,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
34533509
}
34543510

34553511
static void genOMP(Fortran::lower::AbstractConverter &converter,
3512+
Fortran::lower::SymMap &symTable,
3513+
Fortran::semantics::SemanticsContext &semanticsContext,
34563514
Fortran::lower::pft::Evaluation &eval,
34573515
const Fortran::parser::OpenMPDeclareTargetConstruct
34583516
&declareTargetConstruct) {
@@ -3504,24 +3562,28 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
35043562
}
35053563

35063564
static void genOMP(Fortran::lower::AbstractConverter &converter,
3565+
Fortran::lower::SymMap &symTable,
35073566
Fortran::semantics::SemanticsContext &semanticsContext,
35083567
Fortran::lower::pft::Evaluation &eval,
35093568
const Fortran::parser::OpenMPConstruct &ompConstruct) {
35103569
std::visit(
35113570
Fortran::common::visitors{
35123571
[&](const Fortran::parser::OpenMPStandaloneConstruct
35133572
&standaloneConstruct) {
3514-
genOMP(converter, eval, semanticsContext, standaloneConstruct);
3573+
genOMP(converter, symTable, semanticsContext, eval,
3574+
standaloneConstruct);
35153575
},
35163576
[&](const Fortran::parser::OpenMPSectionsConstruct
35173577
&sectionsConstruct) {
3518-
genOMP(converter, eval, sectionsConstruct);
3578+
genOMP(converter, symTable, semanticsContext, eval,
3579+
sectionsConstruct);
35193580
},
35203581
[&](const Fortran::parser::OpenMPSectionConstruct &sectionConstruct) {
3521-
genOMP(converter, eval, sectionConstruct);
3582+
genOMP(converter, symTable, semanticsContext, eval,
3583+
sectionConstruct);
35223584
},
35233585
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
3524-
genOMP(converter, eval, semanticsContext, loopConstruct);
3586+
genOMP(converter, symTable, semanticsContext, eval, loopConstruct);
35253587
},
35263588
[&](const Fortran::parser::OpenMPDeclarativeAllocate
35273589
&execAllocConstruct) {
@@ -3536,21 +3598,25 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
35363598
TODO(converter.getCurrentLocation(), "OpenMPAllocatorsConstruct");
35373599
},
35383600
[&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
3539-
genOMP(converter, eval, semanticsContext, blockConstruct);
3601+
genOMP(converter, symTable, semanticsContext, eval, blockConstruct);
35403602
},
35413603
[&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
3542-
genOMP(converter, eval, atomicConstruct);
3604+
genOMP(converter, symTable, semanticsContext, eval,
3605+
atomicConstruct);
35433606
},
35443607
[&](const Fortran::parser::OpenMPCriticalConstruct
35453608
&criticalConstruct) {
3546-
genOMP(converter, eval, criticalConstruct);
3609+
genOMP(converter, symTable, semanticsContext, eval,
3610+
criticalConstruct);
35473611
},
35483612
},
35493613
ompConstruct.u);
35503614
}
35513615

35523616
static void
35533617
genOMP(Fortran::lower::AbstractConverter &converter,
3618+
Fortran::lower::SymMap &symTable,
3619+
Fortran::semantics::SemanticsContext &semanticsContext,
35543620
Fortran::lower::pft::Evaluation &eval,
35553621
const Fortran::parser::OpenMPDeclarativeConstruct &ompDeclConstruct) {
35563622
std::visit(
@@ -3570,7 +3636,8 @@ genOMP(Fortran::lower::AbstractConverter &converter,
35703636
},
35713637
[&](const Fortran::parser::OpenMPDeclareTargetConstruct
35723638
&declareTargetConstruct) {
3573-
genOMP(converter, eval, declareTargetConstruct);
3639+
genOMP(converter, symTable, semanticsContext, eval,
3640+
declareTargetConstruct);
35743641
},
35753642
[&](const Fortran::parser::OpenMPRequiresConstruct
35763643
&requiresConstruct) {
@@ -3607,57 +3674,19 @@ void Fortran::lower::genOpenMPConstruct(
36073674
Fortran::semantics::SemanticsContext &semanticsContext,
36083675
Fortran::lower::pft::Evaluation &eval,
36093676
const Fortran::parser::OpenMPConstruct &omp) {
3610-
36113677
symTable.pushScope();
3612-
genOMP(converter, semanticsContext, eval, omp);
3613-
3614-
const Fortran::parser::OpenMPLoopConstruct *ompLoop =
3615-
std::get_if<Fortran::parser::OpenMPLoopConstruct>(&omp.u);
3616-
const Fortran::parser::OpenMPBlockConstruct *ompBlock =
3617-
std::get_if<Fortran::parser::OpenMPBlockConstruct>(&omp.u);
3618-
3619-
// If loop is part of an OpenMP Construct then the OpenMP dialect
3620-
// workshare loop operation has already been created. Only the
3621-
// body needs to be created here and the do_loop can be skipped.
3622-
// Skip the number of collapsed loops, which is 1 when there is a
3623-
// no collapse requested.
3624-
3625-
Fortran::lower::pft::Evaluation *curEval = &eval;
3626-
const Fortran::parser::OmpClauseList *loopOpClauseList = nullptr;
3627-
if (ompLoop) {
3628-
loopOpClauseList = &std::get<Fortran::parser::OmpClauseList>(
3629-
std::get<Fortran::parser::OmpBeginLoopDirective>(ompLoop->t).t);
3630-
int64_t collapseValue = Fortran::lower::getCollapseValue(*loopOpClauseList);
3631-
3632-
curEval = &curEval->getFirstNestedEvaluation();
3633-
for (int64_t i = 1; i < collapseValue; i++) {
3634-
curEval = &*std::next(curEval->getNestedEvaluations().begin());
3635-
}
3636-
}
3637-
3638-
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
3639-
converter.genEval(e);
3640-
3641-
if (ompLoop) {
3642-
genOpenMPReduction(converter, *loopOpClauseList);
3643-
} else if (ompBlock) {
3644-
const auto &blockStart =
3645-
std::get<Fortran::parser::OmpBeginBlockDirective>(ompBlock->t);
3646-
const auto &blockClauses =
3647-
std::get<Fortran::parser::OmpClauseList>(blockStart.t);
3648-
genOpenMPReduction(converter, blockClauses);
3649-
}
3650-
3678+
genOMP(converter, symTable, semanticsContext, eval, omp);
36513679
symTable.popScope();
36523680
}
36533681

36543682
void Fortran::lower::genOpenMPDeclarativeConstruct(
36553683
Fortran::lower::AbstractConverter &converter,
3684+
Fortran::lower::SymMap &symTable,
3685+
Fortran::semantics::SemanticsContext &semanticsContext,
36563686
Fortran::lower::pft::Evaluation &eval,
36573687
const Fortran::parser::OpenMPDeclarativeConstruct &omp) {
3658-
genOMP(converter, eval, omp);
3659-
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations())
3660-
converter.genEval(e);
3688+
genOMP(converter, symTable, semanticsContext, eval, omp);
3689+
genNestedEvaluations(converter, eval);
36613690
}
36623691

36633692
void Fortran::lower::genOpenMPSymbolProperties(

0 commit comments

Comments
 (0)