Skip to content

Commit 841ae5f

Browse files
committed
[Flang][OpenMP] Push genEval calls to individual operations, NFC
Introduce `genNestedEvaluations` that will lower all evaluations nested in the given, accouting for a potential COLLAPSE directive. Recursive lowering [2/5]
1 parent 62f3165 commit 841ae5f

File tree

1 file changed

+60
-55
lines changed

1 file changed

+60
-55
lines changed

flang/lib/Lower/OpenMP.cpp

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

113+
static Fortran::lower::pft::Evaluation *
114+
getEvalPastCollapse(Fortran::lower::pft::Evaluation &eval, int collapseValue) {
115+
if (collapseValue == 0)
116+
return &eval;
117+
118+
Fortran::lower::pft::Evaluation *curEval = &eval.getFirstNestedEvaluation();
119+
for (int i = 1; i < collapseValue; i++) {
120+
// The nested evaluations should be DoConstructs (i.e. they should form
121+
// a loop nest). Each DoConstruct is a tuple <NonLabelDoStmt, Block,
122+
// EndDoStmt>.
123+
assert(curEval->isA<Fortran::parser::DoConstruct>());
124+
curEval = &*std::next(curEval->getNestedEvaluations().begin());
125+
}
126+
return curEval;
127+
}
128+
129+
static void genNestedEvaluations(Fortran::lower::AbstractConverter &converter,
130+
Fortran::lower::pft::Evaluation &eval,
131+
int collapseValue = 0) {
132+
Fortran::lower::pft::Evaluation *curEval =
133+
getEvalPastCollapse(eval, collapseValue);
134+
135+
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
136+
converter.genEval(e);
137+
}
138+
113139
//===----------------------------------------------------------------------===//
114140
// DataSharingProcessor
115141
//===----------------------------------------------------------------------===//
@@ -2944,7 +2970,7 @@ genOmpFlush(Fortran::lower::AbstractConverter &converter,
29442970

29452971
static void
29462972
genOMP(Fortran::lower::AbstractConverter &converter,
2947-
Fortran::lower::pft::Evaluation &eval,
2973+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
29482974
Fortran::semantics::SemanticsContext &semanticsContext,
29492975
const Fortran::parser::OpenMPStandaloneConstruct &standaloneConstruct) {
29502976
std::visit(
@@ -3025,6 +3051,9 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
30253051
createBodyOfOp<mlir::omp::SimdLoopOp>(simdLoopOp, converter, loc, eval,
30263052
&loopOpClauseList, iv,
30273053
/*outer=*/false, &dsp);
3054+
3055+
genNestedEvaluations(converter, eval,
3056+
Fortran::lower::getCollapseValue(loopOpClauseList));
30283057
}
30293058

30303059
static void createWsLoop(Fortran::lower::AbstractConverter &converter,
@@ -3106,9 +3135,13 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
31063135
createBodyOfOp<mlir::omp::WsLoopOp>(wsLoopOp, converter, loc, eval,
31073136
&beginClauseList, iv,
31083137
/*outer=*/false, &dsp);
3138+
3139+
genNestedEvaluations(converter, eval,
3140+
Fortran::lower::getCollapseValue(beginClauseList));
31093141
}
31103142

31113143
static void genOMP(Fortran::lower::AbstractConverter &converter,
3144+
Fortran::lower::SymMap &symTable,
31123145
Fortran::lower::pft::Evaluation &eval,
31133146
Fortran::semantics::SemanticsContext &semanticsContext,
31143147
const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
@@ -3178,11 +3211,12 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
31783211
createWsLoop(converter, eval, ompDirective, loopOpClauseList, endClauseList,
31793212
currentLocation);
31803213
}
3214+
genOpenMPReduction(converter, loopOpClauseList);
31813215
}
31823216

31833217
static void
31843218
genOMP(Fortran::lower::AbstractConverter &converter,
3185-
Fortran::lower::pft::Evaluation &eval,
3219+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
31863220
Fortran::semantics::SemanticsContext &semanticsContext,
31873221
const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
31883222
const auto &beginBlockDirective =
@@ -3297,11 +3331,14 @@ genOMP(Fortran::lower::AbstractConverter &converter,
32973331
break;
32983332
}
32993333
}
3334+
3335+
genNestedEvaluations(converter, eval);
3336+
genOpenMPReduction(converter, beginClauseList);
33003337
}
33013338

33023339
static void
33033340
genOMP(Fortran::lower::AbstractConverter &converter,
3304-
Fortran::lower::pft::Evaluation &eval,
3341+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
33053342
const Fortran::parser::OpenMPCriticalConstruct &criticalConstruct) {
33063343
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
33073344
mlir::Location currentLocation = converter.getCurrentLocation();
@@ -3335,11 +3372,12 @@ genOMP(Fortran::lower::AbstractConverter &converter,
33353372
}();
33363373
createBodyOfOp<mlir::omp::CriticalOp>(criticalOp, converter, currentLocation,
33373374
eval);
3375+
genNestedEvaluations(converter, eval);
33383376
}
33393377

33403378
static void
33413379
genOMP(Fortran::lower::AbstractConverter &converter,
3342-
Fortran::lower::pft::Evaluation &eval,
3380+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
33433381
const Fortran::parser::OpenMPSectionConstruct &sectionConstruct) {
33443382
mlir::Location currentLocation = converter.getCurrentLocation();
33453383
const Fortran::parser::OpenMPConstruct *parentOmpConstruct =
@@ -3358,14 +3396,17 @@ genOMP(Fortran::lower::AbstractConverter &converter,
33583396
.t);
33593397
// Currently only private/firstprivate clause is handled, and
33603398
// all privatization is done within `omp.section` operations.
3399+
symTable.pushScope();
33613400
genOpWithBody<mlir::omp::SectionOp>(converter, eval, currentLocation,
33623401
/*outerCombined=*/false,
33633402
&sectionsClauseList);
3403+
genNestedEvaluations(converter, eval);
3404+
symTable.popScope();
33643405
}
33653406

33663407
static void
33673408
genOMP(Fortran::lower::AbstractConverter &converter,
3368-
Fortran::lower::pft::Evaluation &eval,
3409+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
33693410
const Fortran::parser::OpenMPSectionsConstruct &sectionsConstruct) {
33703411
mlir::Location currentLocation = converter.getCurrentLocation();
33713412
llvm::SmallVector<mlir::Value> allocateOperands, allocatorOperands;
@@ -3405,11 +3446,13 @@ genOMP(Fortran::lower::AbstractConverter &converter,
34053446
/*reduction_vars=*/mlir::ValueRange(),
34063447
/*reductions=*/nullptr, allocateOperands,
34073448
allocatorOperands, nowaitClauseOperand);
3449+
3450+
genNestedEvaluations(converter, eval);
34083451
}
34093452

34103453
static void
34113454
genOMP(Fortran::lower::AbstractConverter &converter,
3412-
Fortran::lower::pft::Evaluation &eval,
3455+
Fortran::lower::SymMap &symTable, Fortran::lower::pft::Evaluation &eval,
34133456
const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
34143457
std::visit(
34153458
Fortran::common::visitors{
@@ -3503,24 +3546,26 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
35033546
}
35043547

35053548
static void genOMP(Fortran::lower::AbstractConverter &converter,
3549+
Fortran::lower::SymMap &symTable,
35063550
Fortran::semantics::SemanticsContext &semanticsContext,
35073551
Fortran::lower::pft::Evaluation &eval,
35083552
const Fortran::parser::OpenMPConstruct &ompConstruct) {
35093553
std::visit(
35103554
Fortran::common::visitors{
35113555
[&](const Fortran::parser::OpenMPStandaloneConstruct
35123556
&standaloneConstruct) {
3513-
genOMP(converter, eval, semanticsContext, standaloneConstruct);
3557+
genOMP(converter, symTable, eval, semanticsContext,
3558+
standaloneConstruct);
35143559
},
35153560
[&](const Fortran::parser::OpenMPSectionsConstruct
35163561
&sectionsConstruct) {
3517-
genOMP(converter, eval, sectionsConstruct);
3562+
genOMP(converter, symTable, eval, sectionsConstruct);
35183563
},
35193564
[&](const Fortran::parser::OpenMPSectionConstruct &sectionConstruct) {
3520-
genOMP(converter, eval, sectionConstruct);
3565+
genOMP(converter, symTable, eval, sectionConstruct);
35213566
},
35223567
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
3523-
genOMP(converter, eval, semanticsContext, loopConstruct);
3568+
genOMP(converter, symTable, eval, semanticsContext, loopConstruct);
35243569
},
35253570
[&](const Fortran::parser::OpenMPDeclarativeAllocate
35263571
&execAllocConstruct) {
@@ -3535,14 +3580,14 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
35353580
TODO(converter.getCurrentLocation(), "OpenMPAllocatorsConstruct");
35363581
},
35373582
[&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
3538-
genOMP(converter, eval, semanticsContext, blockConstruct);
3583+
genOMP(converter, symTable, eval, semanticsContext, blockConstruct);
35393584
},
35403585
[&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
3541-
genOMP(converter, eval, atomicConstruct);
3586+
genOMP(converter, symTable, eval, atomicConstruct);
35423587
},
35433588
[&](const Fortran::parser::OpenMPCriticalConstruct
35443589
&criticalConstruct) {
3545-
genOMP(converter, eval, criticalConstruct);
3590+
genOMP(converter, symTable, eval, criticalConstruct);
35463591
},
35473592
},
35483593
ompConstruct.u);
@@ -3606,47 +3651,8 @@ void Fortran::lower::genOpenMPConstruct(
36063651
Fortran::semantics::SemanticsContext &semanticsContext,
36073652
Fortran::lower::pft::Evaluation &eval,
36083653
const Fortran::parser::OpenMPConstruct &omp) {
3609-
36103654
symTable.pushScope();
3611-
genOMP(converter, semanticsContext, eval, omp);
3612-
3613-
const Fortran::parser::OpenMPLoopConstruct *ompLoop =
3614-
std::get_if<Fortran::parser::OpenMPLoopConstruct>(&omp.u);
3615-
const Fortran::parser::OpenMPBlockConstruct *ompBlock =
3616-
std::get_if<Fortran::parser::OpenMPBlockConstruct>(&omp.u);
3617-
3618-
// If loop is part of an OpenMP Construct then the OpenMP dialect
3619-
// workshare loop operation has already been created. Only the
3620-
// body needs to be created here and the do_loop can be skipped.
3621-
// Skip the number of collapsed loops, which is 1 when there is a
3622-
// no collapse requested.
3623-
3624-
Fortran::lower::pft::Evaluation *curEval = &eval;
3625-
const Fortran::parser::OmpClauseList *loopOpClauseList = nullptr;
3626-
if (ompLoop) {
3627-
loopOpClauseList = &std::get<Fortran::parser::OmpClauseList>(
3628-
std::get<Fortran::parser::OmpBeginLoopDirective>(ompLoop->t).t);
3629-
int64_t collapseValue = Fortran::lower::getCollapseValue(*loopOpClauseList);
3630-
3631-
curEval = &curEval->getFirstNestedEvaluation();
3632-
for (int64_t i = 1; i < collapseValue; i++) {
3633-
curEval = &*std::next(curEval->getNestedEvaluations().begin());
3634-
}
3635-
}
3636-
3637-
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
3638-
converter.genEval(e);
3639-
3640-
if (ompLoop) {
3641-
genOpenMPReduction(converter, *loopOpClauseList);
3642-
} else if (ompBlock) {
3643-
const auto &blockStart =
3644-
std::get<Fortran::parser::OmpBeginBlockDirective>(ompBlock->t);
3645-
const auto &blockClauses =
3646-
std::get<Fortran::parser::OmpClauseList>(blockStart.t);
3647-
genOpenMPReduction(converter, blockClauses);
3648-
}
3649-
3655+
genOMP(converter, symTable, semanticsContext, eval, omp);
36503656
symTable.popScope();
36513657
}
36523658

@@ -3655,8 +3661,7 @@ void Fortran::lower::genOpenMPDeclarativeConstruct(
36553661
Fortran::lower::pft::Evaluation &eval,
36563662
const Fortran::parser::OpenMPDeclarativeConstruct &omp) {
36573663
genOMP(converter, eval, omp);
3658-
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations())
3659-
converter.genEval(e);
3664+
genNestedEvaluations(converter, eval);
36603665
}
36613666

36623667
void Fortran::lower::genOpenMPSymbolProperties(

0 commit comments

Comments
 (0)