Skip to content

Commit aeb4821

Browse files
authored
[flang][OpenMP] Move nested eval conversion to OpenMP.cpp, NFC (#75502)
This is the first step towards exploiting `genEval` functionality from inside of OpenMP-generating functions. This follows discourse discussion: https://discourse.llvm.org/t/openmp-lowering-from-pft-to-fir/75263
1 parent 57fcc23 commit aeb4821

File tree

3 files changed

+90
-71
lines changed

3 files changed

+90
-71
lines changed

flang/include/flang/Lower/OpenMP.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SemanticsContext;
4242
namespace lower {
4343

4444
class AbstractConverter;
45+
class SymMap;
4546

4647
namespace pft {
4748
struct Evaluation;
@@ -52,8 +53,9 @@ struct Variable;
5253
void genOpenMPTerminator(fir::FirOpBuilder &, mlir::Operation *,
5354
mlir::Location);
5455

55-
void genOpenMPConstruct(AbstractConverter &, semantics::SemanticsContext &,
56-
pft::Evaluation &, const parser::OpenMPConstruct &);
56+
void genOpenMPConstruct(AbstractConverter &, Fortran::lower::SymMap &,
57+
semantics::SemanticsContext &, pft::Evaluation &,
58+
const parser::OpenMPConstruct &);
5759
void genOpenMPDeclarativeConstruct(AbstractConverter &, pft::Evaluation &,
5860
const parser::OpenMPDeclarativeConstruct &);
5961
int64_t getCollapseValue(const Fortran::parser::OmpClauseList &clauseList);

flang/lib/Lower/Bridge.cpp

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,48 +2421,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
24212421

24222422
void genFIR(const Fortran::parser::OpenMPConstruct &omp) {
24232423
mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
2424-
localSymbols.pushScope();
2425-
genOpenMPConstruct(*this, bridge.getSemanticsContext(), getEval(), omp);
2426-
2427-
const Fortran::parser::OpenMPLoopConstruct *ompLoop =
2428-
std::get_if<Fortran::parser::OpenMPLoopConstruct>(&omp.u);
2429-
const Fortran::parser::OpenMPBlockConstruct *ompBlock =
2430-
std::get_if<Fortran::parser::OpenMPBlockConstruct>(&omp.u);
2431-
2432-
// If loop is part of an OpenMP Construct then the OpenMP dialect
2433-
// workshare loop operation has already been created. Only the
2434-
// body needs to be created here and the do_loop can be skipped.
2435-
// Skip the number of collapsed loops, which is 1 when there is a
2436-
// no collapse requested.
2437-
2438-
Fortran::lower::pft::Evaluation *curEval = &getEval();
2439-
const Fortran::parser::OmpClauseList *loopOpClauseList = nullptr;
2440-
if (ompLoop) {
2441-
loopOpClauseList = &std::get<Fortran::parser::OmpClauseList>(
2442-
std::get<Fortran::parser::OmpBeginLoopDirective>(ompLoop->t).t);
2443-
int64_t collapseValue =
2444-
Fortran::lower::getCollapseValue(*loopOpClauseList);
2445-
2446-
curEval = &curEval->getFirstNestedEvaluation();
2447-
for (int64_t i = 1; i < collapseValue; i++) {
2448-
curEval = &*std::next(curEval->getNestedEvaluations().begin());
2449-
}
2450-
}
2451-
2452-
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
2453-
genFIR(e);
2454-
2455-
if (ompLoop) {
2456-
genOpenMPReduction(*this, *loopOpClauseList);
2457-
} else if (ompBlock) {
2458-
const auto &blockStart =
2459-
std::get<Fortran::parser::OmpBeginBlockDirective>(ompBlock->t);
2460-
const auto &blockClauses =
2461-
std::get<Fortran::parser::OmpClauseList>(blockStart.t);
2462-
genOpenMPReduction(*this, blockClauses);
2463-
}
2464-
2465-
localSymbols.popScope();
2424+
genOpenMPConstruct(*this, localSymbols, bridge.getSemanticsContext(),
2425+
getEval(), omp);
24662426
builder->restoreInsertionPoint(insertPt);
24672427

24682428
// Register if a target region was found
@@ -2478,8 +2438,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
24782438
ompDeviceCodeFound ||
24792439
Fortran::lower::isOpenMPDeviceDeclareTarget(*this, getEval(), ompDecl);
24802440
genOpenMPDeclarativeConstruct(*this, getEval(), ompDecl);
2481-
for (Fortran::lower::pft::Evaluation &e : getEval().getNestedEvaluations())
2482-
genFIR(e);
24832441
builder->restoreInsertionPoint(insertPt);
24842442
}
24852443

flang/lib/Lower/OpenMP.cpp

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "flang/Lower/ConvertVariable.h"
1919
#include "flang/Lower/PFTBuilder.h"
2020
#include "flang/Lower/StatementContext.h"
21+
#include "flang/Lower/SymbolMap.h"
2122
#include "flang/Optimizer/Builder/BoxValue.h"
2223
#include "flang/Optimizer/Builder/FIRBuilder.h"
2324
#include "flang/Optimizer/Builder/Todo.h"
@@ -3384,27 +3385,12 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
33843385
}
33853386
}
33863387

3387-
//===----------------------------------------------------------------------===//
3388-
// Public functions
3389-
//===----------------------------------------------------------------------===//
3390-
3391-
void Fortran::lower::genOpenMPTerminator(fir::FirOpBuilder &builder,
3392-
mlir::Operation *op,
3393-
mlir::Location loc) {
3394-
if (mlir::isa<mlir::omp::WsLoopOp, mlir::omp::ReductionDeclareOp,
3395-
mlir::omp::AtomicUpdateOp, mlir::omp::SimdLoopOp>(op))
3396-
builder.create<mlir::omp::YieldOp>(loc);
3397-
else
3398-
builder.create<mlir::omp::TerminatorOp>(loc);
3399-
}
3400-
3401-
void Fortran::lower::genOpenMPConstruct(
3402-
Fortran::lower::AbstractConverter &converter,
3403-
Fortran::semantics::SemanticsContext &semanticsContext,
3404-
Fortran::lower::pft::Evaluation &eval,
3405-
const Fortran::parser::OpenMPConstruct &ompConstruct) {
3388+
static void genOMP(Fortran::lower::AbstractConverter &converter,
3389+
Fortran::semantics::SemanticsContext &semanticsContext,
3390+
Fortran::lower::pft::Evaluation &eval,
3391+
const Fortran::parser::OpenMPConstruct &ompConstruct) {
34063392
std::visit(
3407-
common::visitors{
3393+
Fortran::common::visitors{
34083394
[&](const Fortran::parser::OpenMPStandaloneConstruct
34093395
&standaloneConstruct) {
34103396
genOMP(converter, eval, semanticsContext, standaloneConstruct);
@@ -3445,12 +3431,12 @@ void Fortran::lower::genOpenMPConstruct(
34453431
ompConstruct.u);
34463432
}
34473433

3448-
void Fortran::lower::genOpenMPDeclarativeConstruct(
3449-
Fortran::lower::AbstractConverter &converter,
3450-
Fortran::lower::pft::Evaluation &eval,
3451-
const Fortran::parser::OpenMPDeclarativeConstruct &ompDeclConstruct) {
3434+
static void
3435+
genOMP(Fortran::lower::AbstractConverter &converter,
3436+
Fortran::lower::pft::Evaluation &eval,
3437+
const Fortran::parser::OpenMPDeclarativeConstruct &ompDeclConstruct) {
34523438
std::visit(
3453-
common::visitors{
3439+
Fortran::common::visitors{
34543440
[&](const Fortran::parser::OpenMPDeclarativeAllocate
34553441
&declarativeAllocate) {
34563442
TODO(converter.getCurrentLocation(), "OpenMPDeclarativeAllocate");
@@ -3483,6 +3469,79 @@ void Fortran::lower::genOpenMPDeclarativeConstruct(
34833469
ompDeclConstruct.u);
34843470
}
34853471

3472+
//===----------------------------------------------------------------------===//
3473+
// Public functions
3474+
//===----------------------------------------------------------------------===//
3475+
3476+
void Fortran::lower::genOpenMPTerminator(fir::FirOpBuilder &builder,
3477+
mlir::Operation *op,
3478+
mlir::Location loc) {
3479+
if (mlir::isa<mlir::omp::WsLoopOp, mlir::omp::ReductionDeclareOp,
3480+
mlir::omp::AtomicUpdateOp, mlir::omp::SimdLoopOp>(op))
3481+
builder.create<mlir::omp::YieldOp>(loc);
3482+
else
3483+
builder.create<mlir::omp::TerminatorOp>(loc);
3484+
}
3485+
3486+
void Fortran::lower::genOpenMPConstruct(
3487+
Fortran::lower::AbstractConverter &converter,
3488+
Fortran::lower::SymMap &symTable,
3489+
Fortran::semantics::SemanticsContext &semanticsContext,
3490+
Fortran::lower::pft::Evaluation &eval,
3491+
const Fortran::parser::OpenMPConstruct &omp) {
3492+
3493+
symTable.pushScope();
3494+
genOMP(converter, semanticsContext, eval, omp);
3495+
3496+
const Fortran::parser::OpenMPLoopConstruct *ompLoop =
3497+
std::get_if<Fortran::parser::OpenMPLoopConstruct>(&omp.u);
3498+
const Fortran::parser::OpenMPBlockConstruct *ompBlock =
3499+
std::get_if<Fortran::parser::OpenMPBlockConstruct>(&omp.u);
3500+
3501+
// If loop is part of an OpenMP Construct then the OpenMP dialect
3502+
// workshare loop operation has already been created. Only the
3503+
// body needs to be created here and the do_loop can be skipped.
3504+
// Skip the number of collapsed loops, which is 1 when there is a
3505+
// no collapse requested.
3506+
3507+
Fortran::lower::pft::Evaluation *curEval = &eval;
3508+
const Fortran::parser::OmpClauseList *loopOpClauseList = nullptr;
3509+
if (ompLoop) {
3510+
loopOpClauseList = &std::get<Fortran::parser::OmpClauseList>(
3511+
std::get<Fortran::parser::OmpBeginLoopDirective>(ompLoop->t).t);
3512+
int64_t collapseValue = Fortran::lower::getCollapseValue(*loopOpClauseList);
3513+
3514+
curEval = &curEval->getFirstNestedEvaluation();
3515+
for (int64_t i = 1; i < collapseValue; i++) {
3516+
curEval = &*std::next(curEval->getNestedEvaluations().begin());
3517+
}
3518+
}
3519+
3520+
for (Fortran::lower::pft::Evaluation &e : curEval->getNestedEvaluations())
3521+
converter.genEval(e);
3522+
3523+
if (ompLoop) {
3524+
genOpenMPReduction(converter, *loopOpClauseList);
3525+
} else if (ompBlock) {
3526+
const auto &blockStart =
3527+
std::get<Fortran::parser::OmpBeginBlockDirective>(ompBlock->t);
3528+
const auto &blockClauses =
3529+
std::get<Fortran::parser::OmpClauseList>(blockStart.t);
3530+
genOpenMPReduction(converter, blockClauses);
3531+
}
3532+
3533+
symTable.popScope();
3534+
}
3535+
3536+
void Fortran::lower::genOpenMPDeclarativeConstruct(
3537+
Fortran::lower::AbstractConverter &converter,
3538+
Fortran::lower::pft::Evaluation &eval,
3539+
const Fortran::parser::OpenMPDeclarativeConstruct &omp) {
3540+
genOMP(converter, eval, omp);
3541+
for (Fortran::lower::pft::Evaluation &e : eval.getNestedEvaluations())
3542+
converter.genEval(e);
3543+
}
3544+
34863545
int64_t Fortran::lower::getCollapseValue(
34873546
const Fortran::parser::OmpClauseList &clauseList) {
34883547
for (const Fortran::parser::OmpClause &clause : clauseList.v) {

0 commit comments

Comments
 (0)