Skip to content

Commit 7620ee6

Browse files
committed
[Flang][OpenMP][Lower] NFC: Move clause processing helpers into the ClauseProcessor
This patch moves some code in PFT to MLIR OpenMP lowering to the `ClauseProcessor` class. This is so that some behavior that is related to certain clauses stays within the `ClauseProcessor` and it's not the caller the one responsible for always doing this when the clause is present.
1 parent 9214e51 commit 7620ee6

File tree

5 files changed

+66
-74
lines changed

5 files changed

+66
-74
lines changed

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ addUseDeviceClause(Fortran::lower::AbstractConverter &converter,
208208
useDeviceSymbols.push_back(object.id());
209209
}
210210

211+
static void convertLoopBounds(Fortran::lower::AbstractConverter &converter,
212+
mlir::Location loc,
213+
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
214+
llvm::SmallVectorImpl<mlir::Value> &upperBound,
215+
llvm::SmallVectorImpl<mlir::Value> &step,
216+
std::size_t loopVarTypeSize) {
217+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
218+
// The types of lower bound, upper bound, and step are converted into the
219+
// type of the loop variable if necessary.
220+
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
221+
for (unsigned it = 0; it < (unsigned)lowerBound.size(); it++) {
222+
lowerBound[it] =
223+
firOpBuilder.createConvert(loc, loopVarType, lowerBound[it]);
224+
upperBound[it] =
225+
firOpBuilder.createConvert(loc, loopVarType, upperBound[it]);
226+
step[it] = firOpBuilder.createConvert(loc, loopVarType, step[it]);
227+
}
228+
}
229+
211230
//===----------------------------------------------------------------------===//
212231
// ClauseProcessor unique clauses
213232
//===----------------------------------------------------------------------===//
@@ -217,8 +236,7 @@ bool ClauseProcessor::processCollapse(
217236
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
218237
llvm::SmallVectorImpl<mlir::Value> &upperBound,
219238
llvm::SmallVectorImpl<mlir::Value> &step,
220-
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
221-
std::size_t &loopVarTypeSize) const {
239+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv) const {
222240
bool found = false;
223241
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
224242

@@ -236,7 +254,7 @@ bool ClauseProcessor::processCollapse(
236254
found = true;
237255
}
238256

239-
loopVarTypeSize = 0;
257+
std::size_t loopVarTypeSize = 0;
240258
do {
241259
Fortran::lower::pft::Evaluation *doLoop =
242260
&doConstructEval->getFirstNestedEvaluation();
@@ -267,6 +285,9 @@ bool ClauseProcessor::processCollapse(
267285
&*std::next(doConstructEval->getNestedEvaluations().begin());
268286
} while (collapseValue > 0);
269287

288+
convertLoopBounds(converter, currentLocation, lowerBound, upperBound, step,
289+
loopVarTypeSize);
290+
270291
return found;
271292
}
272293

@@ -907,6 +928,7 @@ bool ClauseProcessor::processMap(
907928
bool ClauseProcessor::processReduction(
908929
mlir::Location currentLocation,
909930
llvm::SmallVectorImpl<mlir::Value> &reductionVars,
931+
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
910932
llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
911933
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> *reductionSymbols)
912934
const {
@@ -916,6 +938,9 @@ bool ClauseProcessor::processReduction(
916938
ReductionProcessor rp;
917939
rp.addReductionDecl(currentLocation, converter, clause, reductionVars,
918940
reductionDeclSymbols, reductionSymbols);
941+
reductionTypes.reserve(reductionVars.size());
942+
llvm::transform(reductionVars, std::back_inserter(reductionTypes),
943+
[](mlir::Value v) { return v.getType(); });
919944
});
920945
}
921946

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,12 @@ class ClauseProcessor {
5656
clauses(makeList(clauses, semaCtx)) {}
5757

5858
// 'Unique' clauses: They can appear at most once in the clause list.
59-
bool
60-
processCollapse(mlir::Location currentLocation,
61-
Fortran::lower::pft::Evaluation &eval,
62-
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
63-
llvm::SmallVectorImpl<mlir::Value> &upperBound,
64-
llvm::SmallVectorImpl<mlir::Value> &step,
65-
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
66-
std::size_t &loopVarTypeSize) const;
59+
bool processCollapse(
60+
mlir::Location currentLocation, Fortran::lower::pft::Evaluation &eval,
61+
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
62+
llvm::SmallVectorImpl<mlir::Value> &upperBound,
63+
llvm::SmallVectorImpl<mlir::Value> &step,
64+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv) const;
6765
bool processDefault() const;
6866
bool processDevice(Fortran::lower::StatementContext &stmtCtx,
6967
mlir::Value &result) const;
@@ -126,6 +124,7 @@ class ClauseProcessor {
126124
bool
127125
processReduction(mlir::Location currentLocation,
128126
llvm::SmallVectorImpl<mlir::Value> &reductionVars,
127+
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
129128
llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
130129
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
131130
*reductionSymbols = nullptr) const;

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -214,24 +214,6 @@ static void threadPrivatizeVars(Fortran::lower::AbstractConverter &converter,
214214
firOpBuilder.restoreInsertionPoint(insPt);
215215
}
216216

217-
static mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
218-
std::size_t loopVarTypeSize) {
219-
// OpenMP runtime requires 32-bit or 64-bit loop variables.
220-
loopVarTypeSize = loopVarTypeSize * 8;
221-
if (loopVarTypeSize < 32) {
222-
loopVarTypeSize = 32;
223-
} else if (loopVarTypeSize > 64) {
224-
loopVarTypeSize = 64;
225-
mlir::emitWarning(converter.getCurrentLocation(),
226-
"OpenMP loop iteration variable cannot have more than 64 "
227-
"bits size and will be narrowed into 64 bits.");
228-
}
229-
assert((loopVarTypeSize == 32 || loopVarTypeSize == 64) &&
230-
"OpenMP loop iteration variable size must be transformed into 32-bit "
231-
"or 64-bit");
232-
return converter.getFirOpBuilder().getIntegerType(loopVarTypeSize);
233-
}
234-
235217
static mlir::Operation *
236218
createAndSetPrivatizedLoopVar(Fortran::lower::AbstractConverter &converter,
237219
mlir::Location loc, mlir::Value indexVal,
@@ -570,6 +552,7 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
570552
mlir::omp::ClauseProcBindKindAttr procBindKindAttr;
571553
llvm::SmallVector<mlir::Value> allocateOperands, allocatorOperands,
572554
reductionVars;
555+
llvm::SmallVector<mlir::Type> reductionTypes;
573556
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
574557
llvm::SmallVector<const Fortran::semantics::Symbol *> reductionSymbols;
575558

@@ -580,13 +563,8 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
580563
cp.processDefault();
581564
cp.processAllocate(allocatorOperands, allocateOperands);
582565
if (!outerCombined)
583-
cp.processReduction(currentLocation, reductionVars, reductionDeclSymbols,
584-
&reductionSymbols);
585-
586-
llvm::SmallVector<mlir::Type> reductionTypes;
587-
reductionTypes.reserve(reductionVars.size());
588-
llvm::transform(reductionVars, std::back_inserter(reductionTypes),
589-
[](mlir::Value v) { return v.getType(); });
566+
cp.processReduction(currentLocation, reductionVars, reductionTypes,
567+
reductionDeclSymbols, &reductionSymbols);
590568

591569
auto reductionCallback = [&](mlir::Operation *op) {
592570
llvm::SmallVector<mlir::Location> locs(reductionVars.size(),
@@ -1469,25 +1447,6 @@ genOMP(Fortran::lower::AbstractConverter &converter,
14691447
standaloneConstruct.u);
14701448
}
14711449

1472-
static void convertLoopBounds(Fortran::lower::AbstractConverter &converter,
1473-
mlir::Location loc,
1474-
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
1475-
llvm::SmallVectorImpl<mlir::Value> &upperBound,
1476-
llvm::SmallVectorImpl<mlir::Value> &step,
1477-
std::size_t loopVarTypeSize) {
1478-
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
1479-
// The types of lower bound, upper bound, and step are converted into the
1480-
// type of the loop variable if necessary.
1481-
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
1482-
for (unsigned it = 0; it < (unsigned)lowerBound.size(); it++) {
1483-
lowerBound[it] =
1484-
firOpBuilder.createConvert(loc, loopVarType, lowerBound[it]);
1485-
upperBound[it] =
1486-
firOpBuilder.createConvert(loc, loopVarType, upperBound[it]);
1487-
step[it] = firOpBuilder.createConvert(loc, loopVarType, step[it]);
1488-
}
1489-
}
1490-
14911450
static llvm::SmallVector<const Fortran::semantics::Symbol *>
14921451
genLoopVars(mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
14931452
mlir::Location &loc,
@@ -1583,16 +1542,15 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
15831542
llvm::SmallVector<mlir::Value> lowerBound, upperBound, step, reductionVars;
15841543
llvm::SmallVector<mlir::Value> alignedVars, nontemporalVars;
15851544
llvm::SmallVector<const Fortran::semantics::Symbol *> iv;
1545+
llvm::SmallVector<mlir::Type> reductionTypes;
15861546
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
15871547
mlir::omp::ClauseOrderKindAttr orderClauseOperand;
15881548
mlir::IntegerAttr simdlenClauseOperand, safelenClauseOperand;
1589-
std::size_t loopVarTypeSize;
15901549

15911550
ClauseProcessor cp(converter, semaCtx, loopOpClauseList);
1592-
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv,
1593-
loopVarTypeSize);
1551+
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv);
15941552
cp.processScheduleChunk(stmtCtx, scheduleChunkClauseOperand);
1595-
cp.processReduction(loc, reductionVars, reductionDeclSymbols);
1553+
cp.processReduction(loc, reductionVars, reductionTypes, reductionDeclSymbols);
15961554
cp.processIf(clause::If::DirectiveNameModifier::Simd, ifClauseOperand);
15971555
cp.processSimdlen(simdlenClauseOperand);
15981556
cp.processSafelen(safelenClauseOperand);
@@ -1602,9 +1560,6 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
16021560
Fortran::parser::OmpClause::Nontemporal,
16031561
Fortran::parser::OmpClause::Order>(loc, ompDirective);
16041562

1605-
convertLoopBounds(converter, loc, lowerBound, upperBound, step,
1606-
loopVarTypeSize);
1607-
16081563
mlir::TypeRange resultType;
16091564
auto simdLoopOp = firOpBuilder.create<mlir::omp::SimdLoopOp>(
16101565
loc, resultType, lowerBound, upperBound, step, alignedVars,
@@ -1642,27 +1597,23 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
16421597
llvm::SmallVector<mlir::Value> lowerBound, upperBound, step, reductionVars;
16431598
llvm::SmallVector<mlir::Value> linearVars, linearStepVars;
16441599
llvm::SmallVector<const Fortran::semantics::Symbol *> iv;
1600+
llvm::SmallVector<mlir::Type> reductionTypes;
16451601
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
16461602
llvm::SmallVector<const Fortran::semantics::Symbol *> reductionSymbols;
16471603
mlir::omp::ClauseOrderKindAttr orderClauseOperand;
16481604
mlir::omp::ClauseScheduleKindAttr scheduleValClauseOperand;
16491605
mlir::UnitAttr nowaitClauseOperand, byrefOperand, scheduleSimdClauseOperand;
16501606
mlir::IntegerAttr orderedClauseOperand;
16511607
mlir::omp::ScheduleModifierAttr scheduleModClauseOperand;
1652-
std::size_t loopVarTypeSize;
16531608

16541609
ClauseProcessor cp(converter, semaCtx, beginClauseList);
1655-
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv,
1656-
loopVarTypeSize);
1610+
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv);
16571611
cp.processScheduleChunk(stmtCtx, scheduleChunkClauseOperand);
1658-
cp.processReduction(loc, reductionVars, reductionDeclSymbols,
1612+
cp.processReduction(loc, reductionVars, reductionTypes, reductionDeclSymbols,
16591613
&reductionSymbols);
16601614
cp.processTODO<Fortran::parser::OmpClause::Linear,
16611615
Fortran::parser::OmpClause::Order>(loc, ompDirective);
16621616

1663-
convertLoopBounds(converter, loc, lowerBound, upperBound, step,
1664-
loopVarTypeSize);
1665-
16661617
if (ReductionProcessor::doReductionByRef(reductionVars))
16671618
byrefOperand = firOpBuilder.getUnitAttr();
16681619

@@ -1703,11 +1654,6 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
17031654
auto *nestedEval = getCollapsedLoopEval(
17041655
eval, Fortran::lower::getCollapseValue(beginClauseList));
17051656

1706-
llvm::SmallVector<mlir::Type> reductionTypes;
1707-
reductionTypes.reserve(reductionVars.size());
1708-
llvm::transform(reductionVars, std::back_inserter(reductionTypes),
1709-
[](mlir::Value v) { return v.getType(); });
1710-
17111657
auto ivCallback = [&](mlir::Operation *op) {
17121658
return genLoopAndReductionVars(op, converter, loc, iv, reductionSymbols,
17131659
reductionTypes);

flang/lib/Lower/OpenMP/Utils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <flang/Lower/AbstractConverter.h>
1717
#include <flang/Lower/ConvertType.h>
18+
#include <flang/Optimizer/Builder/FIRBuilder.h>
1819
#include <flang/Parser/parse-tree.h>
1920
#include <flang/Parser/tools.h>
2021
#include <flang/Semantics/tools.h>
@@ -70,6 +71,24 @@ void genObjectList2(const Fortran::parser::OmpObjectList &objectList,
7071
}
7172
}
7273

74+
mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
75+
std::size_t loopVarTypeSize) {
76+
// OpenMP runtime requires 32-bit or 64-bit loop variables.
77+
loopVarTypeSize = loopVarTypeSize * 8;
78+
if (loopVarTypeSize < 32) {
79+
loopVarTypeSize = 32;
80+
} else if (loopVarTypeSize > 64) {
81+
loopVarTypeSize = 64;
82+
mlir::emitWarning(converter.getCurrentLocation(),
83+
"OpenMP loop iteration variable cannot have more than 64 "
84+
"bits size and will be narrowed into 64 bits.");
85+
}
86+
assert((loopVarTypeSize == 32 || loopVarTypeSize == 64) &&
87+
"OpenMP loop iteration variable size must be transformed into 32-bit "
88+
"or 64-bit");
89+
return converter.getFirOpBuilder().getIntegerType(loopVarTypeSize);
90+
}
91+
7392
void gatherFuncAndVarSyms(
7493
const ObjectList &objects, mlir::omp::DeclareTargetCaptureClause clause,
7594
llvm::SmallVectorImpl<DeclareTargetCapturePair> &symbolAndClause) {

flang/lib/Lower/OpenMP/Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
5151
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
5252
bool isVal = false);
5353

54+
mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
55+
std::size_t loopVarTypeSize);
56+
5457
void gatherFuncAndVarSyms(
5558
const ObjectList &objects, mlir::omp::DeclareTargetCaptureClause clause,
5659
llvm::SmallVectorImpl<DeclareTargetCapturePair> &symbolAndClause);

0 commit comments

Comments
 (0)