-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang][OpenMP] Rewrite omp.loop
to semantically equivalent ops
#115443
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===-- include/flang/Common/OpenMP-utils.h --------------------*- C++ -*-====// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef FORTRAN_COMMON_OPENMP_UTILS_H_ | ||
#define FORTRAN_COMMON_OPENMP_UTILS_H_ | ||
|
||
#include "flang/Semantics/symbol.h" | ||
|
||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/Value.h" | ||
|
||
#include "llvm/ADT/ArrayRef.h" | ||
|
||
namespace Fortran::common::openmp { | ||
/// Structure holding the information needed to create and bind entry block | ||
/// arguments associated to a single clause. | ||
struct EntryBlockArgsEntry { | ||
llvm::ArrayRef<const Fortran::semantics::Symbol *> syms; | ||
llvm::ArrayRef<mlir::Value> vars; | ||
|
||
bool isValid() const { | ||
// This check allows specifying a smaller number of symbols than values | ||
// because in some case cases a single symbol generates multiple block | ||
// arguments. | ||
return syms.size() <= vars.size(); | ||
} | ||
}; | ||
|
||
/// Structure holding the information needed to create and bind entry block | ||
/// arguments associated to all clauses that can define them. | ||
struct EntryBlockArgs { | ||
EntryBlockArgsEntry inReduction; | ||
EntryBlockArgsEntry map; | ||
EntryBlockArgsEntry priv; | ||
EntryBlockArgsEntry reduction; | ||
EntryBlockArgsEntry taskReduction; | ||
EntryBlockArgsEntry useDeviceAddr; | ||
EntryBlockArgsEntry useDevicePtr; | ||
|
||
bool isValid() const { | ||
return inReduction.isValid() && map.isValid() && priv.isValid() && | ||
reduction.isValid() && taskReduction.isValid() && | ||
useDeviceAddr.isValid() && useDevicePtr.isValid(); | ||
} | ||
|
||
auto getSyms() const { | ||
return llvm::concat<const Fortran::semantics::Symbol *const>( | ||
inReduction.syms, map.syms, priv.syms, reduction.syms, | ||
taskReduction.syms, useDeviceAddr.syms, useDevicePtr.syms); | ||
} | ||
|
||
auto getVars() const { | ||
return llvm::concat<const mlir::Value>(inReduction.vars, map.vars, | ||
priv.vars, reduction.vars, taskReduction.vars, useDeviceAddr.vars, | ||
useDevicePtr.vars); | ||
} | ||
}; | ||
|
||
mlir::Block *genEntryBlock( | ||
mlir::OpBuilder &builder, const EntryBlockArgs &args, mlir::Region ®ion); | ||
} // namespace Fortran::common::openmp | ||
|
||
#endif // FORTRAN_COMMON_OPENMP_UTILS_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//===-- include/flang/Common/OpenMP-utils.cpp ------------------*- C++ -*-====// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "flang/Common/OpenMP-utils.h" | ||
|
||
#include "mlir/IR/OpDefinition.h" | ||
|
||
namespace Fortran::common::openmp { | ||
mlir::Block *genEntryBlock(mlir::OpBuilder &builder, const EntryBlockArgs &args, | ||
mlir::Region ®ion) { | ||
assert(args.isValid() && "invalid args"); | ||
assert(region.empty() && "non-empty region"); | ||
|
||
llvm::SmallVector<mlir::Type> types; | ||
llvm::SmallVector<mlir::Location> locs; | ||
unsigned numVars = args.inReduction.vars.size() + args.map.vars.size() + | ||
args.priv.vars.size() + args.reduction.vars.size() + | ||
args.taskReduction.vars.size() + args.useDeviceAddr.vars.size() + | ||
args.useDevicePtr.vars.size(); | ||
types.reserve(numVars); | ||
locs.reserve(numVars); | ||
|
||
auto extractTypeLoc = [&types, &locs](llvm::ArrayRef<mlir::Value> vals) { | ||
llvm::transform(vals, std::back_inserter(types), | ||
[](mlir::Value v) { return v.getType(); }); | ||
llvm::transform(vals, std::back_inserter(locs), | ||
[](mlir::Value v) { return v.getLoc(); }); | ||
}; | ||
|
||
// Populate block arguments in clause name alphabetical order to match | ||
// expected order by the BlockArgOpenMPOpInterface. | ||
extractTypeLoc(args.inReduction.vars); | ||
extractTypeLoc(args.map.vars); | ||
extractTypeLoc(args.priv.vars); | ||
extractTypeLoc(args.reduction.vars); | ||
extractTypeLoc(args.taskReduction.vars); | ||
extractTypeLoc(args.useDeviceAddr.vars); | ||
extractTypeLoc(args.useDevicePtr.vars); | ||
|
||
return builder.createBlock(®ion, {}, types, locs); | ||
} | ||
} // namespace Fortran::common::openmp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
#include "DirectivesCommon.h" | ||
#include "ReductionProcessor.h" | ||
#include "Utils.h" | ||
#include "flang/Common/OpenMP-utils.h" | ||
#include "flang/Common/idioms.h" | ||
#include "flang/Lower/Bridge.h" | ||
#include "flang/Lower/ConvertExpr.h" | ||
|
@@ -41,57 +42,12 @@ | |
#include "llvm/Frontend/OpenMP/OMPConstants.h" | ||
|
||
using namespace Fortran::lower::omp; | ||
using namespace Fortran::common::openmp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: For consistency, it looks like |
||
|
||
//===----------------------------------------------------------------------===// | ||
// Code generation helper functions | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
/// Structure holding the information needed to create and bind entry block | ||
/// arguments associated to a single clause. | ||
struct EntryBlockArgsEntry { | ||
llvm::ArrayRef<const semantics::Symbol *> syms; | ||
llvm::ArrayRef<mlir::Value> vars; | ||
|
||
bool isValid() const { | ||
// This check allows specifying a smaller number of symbols than values | ||
// because in some case cases a single symbol generates multiple block | ||
// arguments. | ||
return syms.size() <= vars.size(); | ||
} | ||
}; | ||
|
||
/// Structure holding the information needed to create and bind entry block | ||
/// arguments associated to all clauses that can define them. | ||
struct EntryBlockArgs { | ||
EntryBlockArgsEntry inReduction; | ||
EntryBlockArgsEntry map; | ||
EntryBlockArgsEntry priv; | ||
EntryBlockArgsEntry reduction; | ||
EntryBlockArgsEntry taskReduction; | ||
EntryBlockArgsEntry useDeviceAddr; | ||
EntryBlockArgsEntry useDevicePtr; | ||
|
||
bool isValid() const { | ||
return inReduction.isValid() && map.isValid() && priv.isValid() && | ||
reduction.isValid() && taskReduction.isValid() && | ||
useDeviceAddr.isValid() && useDevicePtr.isValid(); | ||
} | ||
|
||
auto getSyms() const { | ||
return llvm::concat<const semantics::Symbol *const>( | ||
inReduction.syms, map.syms, priv.syms, reduction.syms, | ||
taskReduction.syms, useDeviceAddr.syms, useDevicePtr.syms); | ||
} | ||
|
||
auto getVars() const { | ||
return llvm::concat<const mlir::Value>( | ||
inReduction.vars, map.vars, priv.vars, reduction.vars, | ||
taskReduction.vars, useDeviceAddr.vars, useDevicePtr.vars); | ||
} | ||
}; | ||
} // namespace | ||
|
||
static void genOMPDispatch(lower::AbstractConverter &converter, | ||
lower::SymMap &symTable, | ||
semantics::SemanticsContext &semaCtx, | ||
|
@@ -623,50 +579,6 @@ static void genLoopVars( | |
firOpBuilder.setInsertionPointAfter(storeOp); | ||
} | ||
|
||
/// Create an entry block for the given region, including the clause-defined | ||
/// arguments specified. | ||
/// | ||
/// \param [in] converter - PFT to MLIR conversion interface. | ||
/// \param [in] args - entry block arguments information for the given | ||
/// operation. | ||
/// \param [in] region - Empty region in which to create the entry block. | ||
static mlir::Block *genEntryBlock(lower::AbstractConverter &converter, | ||
const EntryBlockArgs &args, | ||
mlir::Region ®ion) { | ||
assert(args.isValid() && "invalid args"); | ||
assert(region.empty() && "non-empty region"); | ||
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); | ||
|
||
llvm::SmallVector<mlir::Type> types; | ||
llvm::SmallVector<mlir::Location> locs; | ||
unsigned numVars = args.inReduction.vars.size() + args.map.vars.size() + | ||
args.priv.vars.size() + args.reduction.vars.size() + | ||
args.taskReduction.vars.size() + | ||
args.useDeviceAddr.vars.size() + | ||
args.useDevicePtr.vars.size(); | ||
types.reserve(numVars); | ||
locs.reserve(numVars); | ||
|
||
auto extractTypeLoc = [&types, &locs](llvm::ArrayRef<mlir::Value> vals) { | ||
llvm::transform(vals, std::back_inserter(types), | ||
[](mlir::Value v) { return v.getType(); }); | ||
llvm::transform(vals, std::back_inserter(locs), | ||
[](mlir::Value v) { return v.getLoc(); }); | ||
}; | ||
|
||
// Populate block arguments in clause name alphabetical order to match | ||
// expected order by the BlockArgOpenMPOpInterface. | ||
extractTypeLoc(args.inReduction.vars); | ||
extractTypeLoc(args.map.vars); | ||
extractTypeLoc(args.priv.vars); | ||
extractTypeLoc(args.reduction.vars); | ||
extractTypeLoc(args.taskReduction.vars); | ||
extractTypeLoc(args.useDeviceAddr.vars); | ||
extractTypeLoc(args.useDevicePtr.vars); | ||
|
||
return firOpBuilder.createBlock(®ion, {}, types, locs); | ||
} | ||
|
||
static void | ||
markDeclareTarget(mlir::Operation *op, lower::AbstractConverter &converter, | ||
mlir::omp::DeclareTargetCaptureClause captureClause, | ||
|
@@ -919,7 +831,7 @@ static void genBodyOfTargetDataOp( | |
ConstructQueue::const_iterator item) { | ||
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); | ||
|
||
genEntryBlock(converter, args, dataOp.getRegion()); | ||
genEntryBlock(firOpBuilder, args, dataOp.getRegion()); | ||
bindEntryBlockArgs(converter, dataOp, args); | ||
|
||
// Insert dummy instruction to remember the insertion position. The | ||
|
@@ -996,7 +908,7 @@ static void genBodyOfTargetOp( | |
auto argIface = llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(*targetOp); | ||
|
||
mlir::Region ®ion = targetOp.getRegion(); | ||
mlir::Block *entryBlock = genEntryBlock(converter, args, region); | ||
mlir::Block *entryBlock = genEntryBlock(firOpBuilder, args, region); | ||
bindEntryBlockArgs(converter, targetOp, args); | ||
|
||
// Check if cloning the bounds introduced any dependency on the outer region. | ||
|
@@ -1122,7 +1034,7 @@ static OpTy genWrapperOp(lower::AbstractConverter &converter, | |
auto op = firOpBuilder.create<OpTy>(loc, clauseOps); | ||
|
||
// Create entry block with arguments. | ||
genEntryBlock(converter, args, op.getRegion()); | ||
genEntryBlock(firOpBuilder, args, op.getRegion()); | ||
|
||
return op; | ||
} | ||
|
@@ -1588,7 +1500,7 @@ genParallelOp(lower::AbstractConverter &converter, lower::SymMap &symTable, | |
const EntryBlockArgs &args, DataSharingProcessor *dsp, | ||
bool isComposite = false) { | ||
auto genRegionEntryCB = [&](mlir::Operation *op) { | ||
genEntryBlock(converter, args, op->getRegion(0)); | ||
genEntryBlock(converter.getFirOpBuilder(), args, op->getRegion(0)); | ||
bindEntryBlockArgs( | ||
converter, llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(op), args); | ||
return llvm::to_vector(args.getSyms()); | ||
|
@@ -1661,12 +1573,12 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable, | |
args.reduction.syms = reductionSyms; | ||
args.reduction.vars = clauseOps.reductionVars; | ||
|
||
genEntryBlock(converter, args, sectionsOp.getRegion()); | ||
genEntryBlock(builder, args, sectionsOp.getRegion()); | ||
mlir::Operation *terminator = | ||
lower::genOpenMPTerminator(builder, sectionsOp, loc); | ||
|
||
auto genRegionEntryCB = [&](mlir::Operation *op) { | ||
genEntryBlock(converter, args, op->getRegion(0)); | ||
genEntryBlock(builder, args, op->getRegion(0)); | ||
bindEntryBlockArgs( | ||
converter, llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(op), args); | ||
return llvm::to_vector(args.getSyms()); | ||
|
@@ -1989,7 +1901,7 @@ genTaskOp(lower::AbstractConverter &converter, lower::SymMap &symTable, | |
taskArgs.priv.vars = clauseOps.privateVars; | ||
|
||
auto genRegionEntryCB = [&](mlir::Operation *op) { | ||
genEntryBlock(converter, taskArgs, op->getRegion(0)); | ||
genEntryBlock(converter.getFirOpBuilder(), taskArgs, op->getRegion(0)); | ||
bindEntryBlockArgs(converter, | ||
llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(op), | ||
taskArgs); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.