Skip to content

Commit f264104

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 2cd19df commit f264104

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
@@ -230,6 +230,25 @@ addUseDeviceClause(Fortran::lower::AbstractConverter &converter,
230230
}
231231
}
232232

233+
static void convertLoopBounds(Fortran::lower::AbstractConverter &converter,
234+
mlir::Location loc,
235+
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
236+
llvm::SmallVectorImpl<mlir::Value> &upperBound,
237+
llvm::SmallVectorImpl<mlir::Value> &step,
238+
std::size_t loopVarTypeSize) {
239+
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
240+
// The types of lower bound, upper bound, and step are converted into the
241+
// type of the loop variable if necessary.
242+
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
243+
for (unsigned it = 0; it < (unsigned)lowerBound.size(); it++) {
244+
lowerBound[it] =
245+
firOpBuilder.createConvert(loc, loopVarType, lowerBound[it]);
246+
upperBound[it] =
247+
firOpBuilder.createConvert(loc, loopVarType, upperBound[it]);
248+
step[it] = firOpBuilder.createConvert(loc, loopVarType, step[it]);
249+
}
250+
}
251+
233252
//===----------------------------------------------------------------------===//
234253
// ClauseProcessor unique clauses
235254
//===----------------------------------------------------------------------===//
@@ -239,8 +258,7 @@ bool ClauseProcessor::processCollapse(
239258
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
240259
llvm::SmallVectorImpl<mlir::Value> &upperBound,
241260
llvm::SmallVectorImpl<mlir::Value> &step,
242-
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
243-
std::size_t &loopVarTypeSize) const {
261+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv) const {
244262
bool found = false;
245263
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
246264

@@ -259,7 +277,7 @@ bool ClauseProcessor::processCollapse(
259277
found = true;
260278
}
261279

262-
loopVarTypeSize = 0;
280+
std::size_t loopVarTypeSize = 0;
263281
do {
264282
Fortran::lower::pft::Evaluation *doLoop =
265283
&doConstructEval->getFirstNestedEvaluation();
@@ -290,6 +308,9 @@ bool ClauseProcessor::processCollapse(
290308
&*std::next(doConstructEval->getNestedEvaluations().begin());
291309
} while (collapseValue > 0);
292310

311+
convertLoopBounds(converter, currentLocation, lowerBound, upperBound, step,
312+
loopVarTypeSize);
313+
293314
return found;
294315
}
295316

@@ -961,6 +982,7 @@ bool ClauseProcessor::processMap(
961982
bool ClauseProcessor::processReduction(
962983
mlir::Location currentLocation,
963984
llvm::SmallVectorImpl<mlir::Value> &reductionVars,
985+
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
964986
llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
965987
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> *reductionSymbols)
966988
const {
@@ -971,6 +993,9 @@ bool ClauseProcessor::processReduction(
971993
rp.addReductionDecl(currentLocation, converter, reductionClause->v,
972994
reductionVars, reductionDeclSymbols,
973995
reductionSymbols);
996+
reductionTypes.reserve(reductionVars.size());
997+
llvm::transform(reductionVars, std::back_inserter(reductionTypes),
998+
[](mlir::Value v) { return v.getType(); });
974999
});
9751000
}
9761001

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ class ClauseProcessor {
5454
: converter(converter), semaCtx(semaCtx), clauses(clauses) {}
5555

5656
// 'Unique' clauses: They can appear at most once in the clause list.
57-
bool
58-
processCollapse(mlir::Location currentLocation,
59-
Fortran::lower::pft::Evaluation &eval,
60-
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
61-
llvm::SmallVectorImpl<mlir::Value> &upperBound,
62-
llvm::SmallVectorImpl<mlir::Value> &step,
63-
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
64-
std::size_t &loopVarTypeSize) const;
57+
bool processCollapse(
58+
mlir::Location currentLocation, Fortran::lower::pft::Evaluation &eval,
59+
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
60+
llvm::SmallVectorImpl<mlir::Value> &upperBound,
61+
llvm::SmallVectorImpl<mlir::Value> &step,
62+
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv) const;
6563
bool processDefault() const;
6664
bool processDevice(Fortran::lower::StatementContext &stmtCtx,
6765
mlir::Value &result) const;
@@ -125,6 +123,7 @@ class ClauseProcessor {
125123
bool
126124
processReduction(mlir::Location currentLocation,
127125
llvm::SmallVectorImpl<mlir::Value> &reductionVars,
126+
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
128127
llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
129128
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
130129
*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

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

592570
auto reductionCallback = [&](mlir::Operation *op) {
593571
llvm::SmallVector<mlir::Location> locs(reductionVars.size(),
@@ -1476,25 +1454,6 @@ genOMP(Fortran::lower::AbstractConverter &converter,
14761454
standaloneConstruct.u);
14771455
}
14781456

1479-
static void convertLoopBounds(Fortran::lower::AbstractConverter &converter,
1480-
mlir::Location loc,
1481-
llvm::SmallVectorImpl<mlir::Value> &lowerBound,
1482-
llvm::SmallVectorImpl<mlir::Value> &upperBound,
1483-
llvm::SmallVectorImpl<mlir::Value> &step,
1484-
std::size_t loopVarTypeSize) {
1485-
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
1486-
// The types of lower bound, upper bound, and step are converted into the
1487-
// type of the loop variable if necessary.
1488-
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
1489-
for (unsigned it = 0; it < (unsigned)lowerBound.size(); it++) {
1490-
lowerBound[it] =
1491-
firOpBuilder.createConvert(loc, loopVarType, lowerBound[it]);
1492-
upperBound[it] =
1493-
firOpBuilder.createConvert(loc, loopVarType, upperBound[it]);
1494-
step[it] = firOpBuilder.createConvert(loc, loopVarType, step[it]);
1495-
}
1496-
}
1497-
14981457
static llvm::SmallVector<const Fortran::semantics::Symbol *>
14991458
genLoopVars(mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
15001459
mlir::Location &loc,
@@ -1590,16 +1549,15 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
15901549
llvm::SmallVector<mlir::Value> lowerBound, upperBound, step, reductionVars;
15911550
llvm::SmallVector<mlir::Value> alignedVars, nontemporalVars;
15921551
llvm::SmallVector<const Fortran::semantics::Symbol *> iv;
1552+
llvm::SmallVector<mlir::Type> reductionTypes;
15931553
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
15941554
mlir::omp::ClauseOrderKindAttr orderClauseOperand;
15951555
mlir::IntegerAttr simdlenClauseOperand, safelenClauseOperand;
1596-
std::size_t loopVarTypeSize;
15971556

15981557
ClauseProcessor cp(converter, semaCtx, loopOpClauseList);
1599-
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv,
1600-
loopVarTypeSize);
1558+
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv);
16011559
cp.processScheduleChunk(stmtCtx, scheduleChunkClauseOperand);
1602-
cp.processReduction(loc, reductionVars, reductionDeclSymbols);
1560+
cp.processReduction(loc, reductionVars, reductionTypes, reductionDeclSymbols);
16031561
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Simd,
16041562
ifClauseOperand);
16051563
cp.processSimdlen(simdlenClauseOperand);
@@ -1610,9 +1568,6 @@ createSimdLoop(Fortran::lower::AbstractConverter &converter,
16101568
Fortran::parser::OmpClause::Nontemporal,
16111569
Fortran::parser::OmpClause::Order>(loc, ompDirective);
16121570

1613-
convertLoopBounds(converter, loc, lowerBound, upperBound, step,
1614-
loopVarTypeSize);
1615-
16161571
mlir::TypeRange resultType;
16171572
auto simdLoopOp = firOpBuilder.create<mlir::omp::SimdLoopOp>(
16181573
loc, resultType, lowerBound, upperBound, step, alignedVars,
@@ -1650,27 +1605,23 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
16501605
llvm::SmallVector<mlir::Value> lowerBound, upperBound, step, reductionVars;
16511606
llvm::SmallVector<mlir::Value> linearVars, linearStepVars;
16521607
llvm::SmallVector<const Fortran::semantics::Symbol *> iv;
1608+
llvm::SmallVector<mlir::Type> reductionTypes;
16531609
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
16541610
llvm::SmallVector<const Fortran::semantics::Symbol *> reductionSymbols;
16551611
mlir::omp::ClauseOrderKindAttr orderClauseOperand;
16561612
mlir::omp::ClauseScheduleKindAttr scheduleValClauseOperand;
16571613
mlir::UnitAttr nowaitClauseOperand, byrefOperand, scheduleSimdClauseOperand;
16581614
mlir::IntegerAttr orderedClauseOperand;
16591615
mlir::omp::ScheduleModifierAttr scheduleModClauseOperand;
1660-
std::size_t loopVarTypeSize;
16611616

16621617
ClauseProcessor cp(converter, semaCtx, beginClauseList);
1663-
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv,
1664-
loopVarTypeSize);
1618+
cp.processCollapse(loc, eval, lowerBound, upperBound, step, iv);
16651619
cp.processScheduleChunk(stmtCtx, scheduleChunkClauseOperand);
1666-
cp.processReduction(loc, reductionVars, reductionDeclSymbols,
1620+
cp.processReduction(loc, reductionVars, reductionTypes, reductionDeclSymbols,
16671621
&reductionSymbols);
16681622
cp.processTODO<Fortran::parser::OmpClause::Linear,
16691623
Fortran::parser::OmpClause::Order>(loc, ompDirective);
16701624

1671-
convertLoopBounds(converter, loc, lowerBound, upperBound, step,
1672-
loopVarTypeSize);
1673-
16741625
if (ReductionProcessor::doReductionByRef(reductionVars))
16751626
byrefOperand = firOpBuilder.getUnitAttr();
16761627

@@ -1711,11 +1662,6 @@ static void createWsLoop(Fortran::lower::AbstractConverter &converter,
17111662
auto *nestedEval = getCollapsedLoopEval(
17121663
eval, Fortran::lower::getCollapseValue(beginClauseList));
17131664

1714-
llvm::SmallVector<mlir::Type> reductionTypes;
1715-
reductionTypes.reserve(reductionVars.size());
1716-
llvm::transform(reductionVars, std::back_inserter(reductionTypes),
1717-
[](mlir::Value v) { return v.getType(); });
1718-
17191665
auto ivCallback = [&](mlir::Operation *op) {
17201666
return genLoopAndReductionVars(op, converter, loc, iv, reductionSymbols,
17211667
reductionTypes);

flang/lib/Lower/OpenMP/Utils.cpp

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

1515
#include <flang/Lower/AbstractConverter.h>
1616
#include <flang/Lower/ConvertType.h>
17+
#include <flang/Optimizer/Builder/FIRBuilder.h>
1718
#include <flang/Parser/parse-tree.h>
1819
#include <flang/Parser/tools.h>
1920
#include <flang/Semantics/tools.h>
@@ -55,6 +56,24 @@ void genObjectList(const Fortran::parser::OmpObjectList &objectList,
5556
}
5657
}
5758

59+
mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
60+
std::size_t loopVarTypeSize) {
61+
// OpenMP runtime requires 32-bit or 64-bit loop variables.
62+
loopVarTypeSize = loopVarTypeSize * 8;
63+
if (loopVarTypeSize < 32) {
64+
loopVarTypeSize = 32;
65+
} else if (loopVarTypeSize > 64) {
66+
loopVarTypeSize = 64;
67+
mlir::emitWarning(converter.getCurrentLocation(),
68+
"OpenMP loop iteration variable cannot have more than 64 "
69+
"bits size and will be narrowed into 64 bits.");
70+
}
71+
assert((loopVarTypeSize == 32 || loopVarTypeSize == 64) &&
72+
"OpenMP loop iteration variable size must be transformed into 32-bit "
73+
"or 64-bit");
74+
return converter.getFirOpBuilder().getIntegerType(loopVarTypeSize);
75+
}
76+
5877
void gatherFuncAndVarSyms(
5978
const Fortran::parser::OmpObjectList &objList,
6079
mlir::omp::DeclareTargetCaptureClause clause,

flang/lib/Lower/OpenMP/Utils.h

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

53+
mlir::Type getLoopVarType(Fortran::lower::AbstractConverter &converter,
54+
std::size_t loopVarTypeSize);
55+
5356
void gatherFuncAndVarSyms(
5457
const Fortran::parser::OmpObjectList &objList,
5558
mlir::omp::DeclareTargetCaptureClause clause,

0 commit comments

Comments
 (0)