Skip to content

[Flang][OpenMP][Lower] Update workshare-loop lowering (5/5) #89215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c3962aa
[MLIR][OpenMP] Make omp.wsloop into a loop wrapper (1/5)
skatrak Apr 17, 2024
f9b14e3
[MLIR][OpenMP] Update op verifiers dependent on omp.wsloop (2/5)
skatrak Apr 17, 2024
fdee8cf
[MLIR][SCF] Update scf.parallel lowering to OpenMP (3/5)
skatrak Apr 18, 2024
25dc3a4
[MLIR][OpenMP] Update omp.wsloop translation to LLVM IR (4/5)
skatrak Apr 18, 2024
da2675d
[Flang][OpenMP][Lower] Update workshare-loop lowering (5/5)
skatrak Apr 18, 2024
18c8bda
Address review comment, improve tests
skatrak Apr 19, 2024
f7254bf
Merge branch 'users/skatrak/spr/wsloop-wrapper-02-dependent-ops' into…
skatrak Apr 19, 2024
f8c0897
Address review comments
skatrak Apr 19, 2024
2d53695
Merge branch 'users/skatrak/spr/wsloop-wrapper-03-scf-parallel' into …
skatrak Apr 19, 2024
6fd45da
Merge branch 'users/skatrak/spr/wsloop-wrapper-04-llvm-ir' into users…
skatrak Apr 19, 2024
6401482
Address review comments
skatrak Apr 19, 2024
2682417
Merge branch 'users/skatrak/spr/wsloop-wrapper-01-mlir' into users/sk…
skatrak Apr 19, 2024
e2a8386
Merge branch 'users/skatrak/spr/wsloop-wrapper-02-dependent-ops' into…
skatrak Apr 19, 2024
996a7cd
Merge branch 'users/skatrak/spr/wsloop-wrapper-03-scf-parallel' into …
skatrak Apr 19, 2024
bff4557
Add documentation and simplify genLoopVars
skatrak Apr 19, 2024
f833127
Merge branch 'main' into users/skatrak/spr/wsloop-wrapper-01-mlir
skatrak Apr 24, 2024
48e93f9
Improve documentation
skatrak Apr 24, 2024
fc21155
Merge branch 'users/skatrak/spr/wsloop-wrapper-01-mlir' into users/sk…
skatrak Apr 24, 2024
e9f960a
Merge branch 'users/skatrak/spr/wsloop-wrapper-02-dependent-ops' into…
skatrak Apr 24, 2024
077c430
Merge branch 'users/skatrak/spr/wsloop-wrapper-03-scf-parallel' into …
skatrak Apr 24, 2024
d2a4c8f
Merge branch 'users/skatrak/spr/wsloop-wrapper-04-llvm-ir' into users…
skatrak Apr 24, 2024
196837c
Fix unit tests
skatrak Apr 24, 2024
75df239
Merge branch 'users/skatrak/spr/wsloop-wrapper-01-mlir' into users/sk…
skatrak Apr 24, 2024
b720026
Merge branch 'users/skatrak/spr/wsloop-wrapper-02-dependent-ops' into…
skatrak Apr 24, 2024
00bb71a
Merge branch 'users/skatrak/spr/wsloop-wrapper-03-scf-parallel' into …
skatrak Apr 24, 2024
2db3c61
Merge branch 'users/skatrak/spr/wsloop-wrapper-04-llvm-ir' into users…
skatrak Apr 24, 2024
29362bb
Fix unit tests
skatrak Apr 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ void DataSharingProcessor::insertBarrier() {
}

void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
mlir::omp::LoopNestOp loopOp;
if (auto wrapper = mlir::dyn_cast<mlir::omp::LoopWrapperInterface>(op))
loopOp = wrapper.isWrapper()
? mlir::cast<mlir::omp::LoopNestOp>(wrapper.getWrappedLoop())
: nullptr;

bool cmpCreated = false;
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
for (const omp::Clause &clause : clauses) {
Expand Down Expand Up @@ -214,33 +220,34 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
// Update the original variable just before exiting the worksharing
// loop. Conversion as follows:
//
// omp.wsloop {
// omp.wsloop { ...
// ... store
// store ===> %v = arith.addi %iv, %step
// omp.yield %cmp = %step < 0 ? %v < %ub : %v > %ub
// } fir.if %cmp {
// fir.store %v to %loopIV
// ^%lpv_update_blk:
// }
// omp.yield
// }
//
// omp.wsloop { omp.wsloop {
// omp.loop_nest { omp.loop_nest {
// ... ...
// store ===> store
// omp.yield %v = arith.addi %iv, %step
// } %cmp = %step < 0 ? %v < %ub : %v > %ub
// omp.terminator fir.if %cmp {
// } fir.store %v to %loopIV
// ^%lpv_update_blk:
// }
// omp.yield
// }
// omp.terminator
// }

// Only generate the compare once in presence of multiple LastPrivate
// clauses.
if (cmpCreated)
continue;
cmpCreated = true;

mlir::Location loc = op->getLoc();
mlir::Operation *lastOper = op->getRegion(0).back().getTerminator();
mlir::Location loc = loopOp.getLoc();
mlir::Operation *lastOper = loopOp.getRegion().back().getTerminator();
firOpBuilder.setInsertionPoint(lastOper);

mlir::Value iv = op->getRegion(0).front().getArguments()[0];
mlir::Value ub =
mlir::dyn_cast<mlir::omp::WsloopOp>(op).getUpperBound()[0];
mlir::Value step = mlir::dyn_cast<mlir::omp::WsloopOp>(op).getStep()[0];
mlir::Value iv = loopOp.getIVs()[0];
mlir::Value ub = loopOp.getUpperBound()[0];
mlir::Value step = loopOp.getStep()[0];

// v = iv + step
// cmp = step < 0 ? v < ub : v > ub
Expand All @@ -259,7 +266,7 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
auto ifOp = firOpBuilder.create<fir::IfOp>(loc, cmpOp, /*else*/ false);
firOpBuilder.setInsertionPointToStart(&ifOp.getThenRegion().front());
assert(loopIV && "loopIV was not set");
firOpBuilder.create<fir::StoreOp>(op->getLoc(), v, loopIV);
firOpBuilder.create<fir::StoreOp>(loopOp.getLoc(), v, loopIV);
lastPrivIP = firOpBuilder.saveInsertionPoint();
} else {
TODO(converter.getCurrentLocation(),
Expand Down
137 changes: 61 additions & 76 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,29 @@ getDeclareTargetFunctionDevice(
return std::nullopt;
}

static llvm::SmallVector<const Fortran::semantics::Symbol *>
/// Set up the entry block of the given `omp.loop_nest` operation, adding a
/// block argument for each loop induction variable and allocating and
/// initializing a private value to hold each of them.
///
/// This function can also bind the symbols of any variables that should match
/// block arguments on parent loop wrapper operations attached to the same
/// loop. This allows the introduction of any necessary `hlfir.declare`
/// operations inside of the entry block of the `omp.loop_nest` operation and
/// not directly under any of the wrappers, which would invalidate them.
///
/// \param [in] op - the loop nest operation.
/// \param [in] converter - PFT to MLIR conversion interface.
/// \param [in] loc - location.
/// \param [in] args - symbols of induction variables.
/// \param [in] wrapperSyms - symbols of variables to be mapped to loop wrapper
/// entry block arguments.
/// \param [in] wrapperArgs - entry block arguments of parent loop wrappers.
static void
genLoopVars(mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
mlir::Location &loc,
llvm::ArrayRef<const Fortran::semantics::Symbol *> args) {
llvm::ArrayRef<const Fortran::semantics::Symbol *> args,
llvm::ArrayRef<const Fortran::semantics::Symbol *> wrapperSyms = {},
llvm::ArrayRef<mlir::BlockArgument> wrapperArgs = {}) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
auto &region = op->getRegion(0);

Expand All @@ -380,6 +399,12 @@ genLoopVars(mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<mlir::Type> tiv(args.size(), loopVarType);
llvm::SmallVector<mlir::Location> locs(args.size(), loc);
firOpBuilder.createBlock(&region, {}, tiv, locs);

// Bind the entry block arguments of parent wrappers to the corresponding
// symbols.
for (auto [arg, prv] : llvm::zip_equal(wrapperSyms, wrapperArgs))
converter.bindSymbol(*arg, prv);

// The argument is not currently in memory, so make a temporary for the
// argument, and store it there, then bind that location to the argument.
mlir::Operation *storeOp = nullptr;
Expand All @@ -389,7 +414,6 @@ genLoopVars(mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
createAndSetPrivatizedLoopVar(converter, loc, indexVal, argSymbol);
}
firOpBuilder.setInsertionPointAfter(storeOp);
return llvm::SmallVector<const Fortran::semantics::Symbol *>(args);
}

static void genReductionVars(
Expand All @@ -410,58 +434,6 @@ static void genReductionVars(
}
}

static llvm::SmallVector<const Fortran::semantics::Symbol *>
genLoopAndReductionVars(
mlir::Operation *op, Fortran::lower::AbstractConverter &converter,
mlir::Location &loc,
llvm::ArrayRef<const Fortran::semantics::Symbol *> loopArgs,
llvm::ArrayRef<const Fortran::semantics::Symbol *> reductionArgs,
llvm::ArrayRef<mlir::Type> reductionTypes) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();

llvm::SmallVector<mlir::Type> blockArgTypes;
llvm::SmallVector<mlir::Location> blockArgLocs;
blockArgTypes.reserve(loopArgs.size() + reductionArgs.size());
blockArgLocs.reserve(blockArgTypes.size());
mlir::Block *entryBlock;

if (loopArgs.size()) {
std::size_t loopVarTypeSize = 0;
for (const Fortran::semantics::Symbol *arg : loopArgs)
loopVarTypeSize = std::max(loopVarTypeSize, arg->GetUltimate().size());
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize);
std::fill_n(std::back_inserter(blockArgTypes), loopArgs.size(),
loopVarType);
std::fill_n(std::back_inserter(blockArgLocs), loopArgs.size(), loc);
}
if (reductionArgs.size()) {
llvm::copy(reductionTypes, std::back_inserter(blockArgTypes));
std::fill_n(std::back_inserter(blockArgLocs), reductionArgs.size(), loc);
}
entryBlock = firOpBuilder.createBlock(&op->getRegion(0), {}, blockArgTypes,
blockArgLocs);
// The argument is not currently in memory, so make a temporary for the
// argument, and store it there, then bind that location to the argument.
if (loopArgs.size()) {
mlir::Operation *storeOp = nullptr;
for (auto [argIndex, argSymbol] : llvm::enumerate(loopArgs)) {
mlir::Value indexVal =
fir::getBase(op->getRegion(0).front().getArgument(argIndex));
storeOp =
createAndSetPrivatizedLoopVar(converter, loc, indexVal, argSymbol);
}
firOpBuilder.setInsertionPointAfter(storeOp);
}
// Bind the reduction arguments to their block arguments
for (auto [arg, prv] : llvm::zip_equal(
reductionArgs,
llvm::drop_begin(entryBlock->getArguments(), loopArgs.size()))) {
converter.bindSymbol(*arg, prv);
}

return llvm::SmallVector<const Fortran::semantics::Symbol *>(loopArgs);
}

static void
markDeclareTarget(mlir::Operation *op,
Fortran::lower::AbstractConverter &converter,
Expand Down Expand Up @@ -1270,20 +1242,16 @@ static void genTeamsClauses(Fortran::lower::AbstractConverter &converter,
static void genWsloopClauses(
Fortran::lower::AbstractConverter &converter,
Fortran::semantics::SemanticsContext &semaCtx,
Fortran::lower::StatementContext &stmtCtx,
Fortran::lower::pft::Evaluation &eval, const List<Clause> &clauses,
Fortran::lower::StatementContext &stmtCtx, const List<Clause> &clauses,
mlir::Location loc, mlir::omp::WsloopClauseOps &clauseOps,
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
llvm::SmallVectorImpl<mlir::Type> &reductionTypes,
llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &reductionSyms) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
ClauseProcessor cp(converter, semaCtx, clauses);
cp.processCollapse(loc, eval, clauseOps, iv);
cp.processNowait(clauseOps);
cp.processOrdered(clauseOps);
cp.processReduction(loc, clauseOps, &reductionTypes, &reductionSyms);
cp.processSchedule(stmtCtx, clauseOps);
clauseOps.loopInclusiveAttr = firOpBuilder.getUnitAttr();
// TODO Support delayed privatization.

if (ReductionProcessor::doReductionByRef(clauseOps.reductionVars))
Expand Down Expand Up @@ -1526,7 +1494,8 @@ genSimdOp(Fortran::lower::AbstractConverter &converter,
auto *nestedEval = getCollapsedLoopEval(eval, getCollapseValue(clauses));

auto ivCallback = [&](mlir::Operation *op) {
return genLoopVars(op, converter, loc, iv);
genLoopVars(op, converter, loc, iv);
return iv;
};

createBodyOfOp(*loopOp,
Expand Down Expand Up @@ -1801,32 +1770,48 @@ genWsloopOp(Fortran::lower::AbstractConverter &converter,
Fortran::semantics::SemanticsContext &semaCtx,
Fortran::lower::pft::Evaluation &eval, mlir::Location loc,
const List<Clause> &clauses) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
DataSharingProcessor dsp(converter, semaCtx, clauses, eval);
dsp.processStep1();

Fortran::lower::StatementContext stmtCtx;
mlir::omp::WsloopClauseOps clauseOps;
mlir::omp::LoopNestClauseOps loopClauseOps;
mlir::omp::WsloopClauseOps wsClauseOps;
llvm::SmallVector<const Fortran::semantics::Symbol *> iv;
llvm::SmallVector<mlir::Type> reductionTypes;
llvm::SmallVector<const Fortran::semantics::Symbol *> reductionSyms;
genWsloopClauses(converter, semaCtx, stmtCtx, eval, clauses, loc, clauseOps,
iv, reductionTypes, reductionSyms);
genLoopNestClauses(converter, semaCtx, eval, clauses, loc, loopClauseOps, iv);
genWsloopClauses(converter, semaCtx, stmtCtx, clauses, loc, wsClauseOps,
reductionTypes, reductionSyms);

// Create omp.wsloop wrapper and populate entry block arguments with reduction
// variables.
auto wsloopOp = firOpBuilder.create<mlir::omp::WsloopOp>(loc, wsClauseOps);
llvm::SmallVector<mlir::Location> reductionLocs(reductionSyms.size(), loc);
mlir::Block *wsloopEntryBlock = firOpBuilder.createBlock(
&wsloopOp.getRegion(), {}, reductionTypes, reductionLocs);
firOpBuilder.setInsertionPoint(
Fortran::lower::genOpenMPTerminator(firOpBuilder, wsloopOp, loc));

// Create nested omp.loop_nest and fill body with loop contents.
auto loopOp = firOpBuilder.create<mlir::omp::LoopNestOp>(loc, loopClauseOps);

auto *nestedEval = getCollapsedLoopEval(eval, getCollapseValue(clauses));

auto ivCallback = [&](mlir::Operation *op) {
return genLoopAndReductionVars(op, converter, loc, iv, reductionSyms,
reductionTypes);
genLoopVars(op, converter, loc, iv, reductionSyms,
wsloopEntryBlock->getArguments());
return iv;
};

return genOpWithBody<mlir::omp::WsloopOp>(
OpWithBodyGenInfo(converter, semaCtx, loc, *nestedEval,
llvm::omp::Directive::OMPD_do)
.setClauses(&clauses)
.setDataSharingProcessor(&dsp)
.setReductions(&reductionSyms, &reductionTypes)
.setGenRegionEntryCb(ivCallback),
clauseOps);
createBodyOfOp(*loopOp,
OpWithBodyGenInfo(converter, semaCtx, loc, *nestedEval,
llvm::omp::Directive::OMPD_do)
.setClauses(&clauses)
.setDataSharingProcessor(&dsp)
.setReductions(&reductionSyms, &reductionTypes)
.setGenRegionEntryCb(ivCallback));
return wsloopOp;
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2482,8 +2467,8 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
mlir::Operation *Fortran::lower::genOpenMPTerminator(fir::FirOpBuilder &builder,
mlir::Operation *op,
mlir::Location loc) {
if (mlir::isa<mlir::omp::WsloopOp, mlir::omp::DeclareReductionOp,
mlir::omp::AtomicUpdateOp, mlir::omp::LoopNestOp>(op))
if (mlir::isa<mlir::omp::AtomicUpdateOp, mlir::omp::DeclareReductionOp,
mlir::omp::LoopNestOp>(op))
return builder.create<mlir::omp::YieldOp>(loc);
return builder.create<mlir::omp::TerminatorOp>(loc);
}
Expand Down
Loading