| 1 |
//===-- Lower/OpenMP/ClauseProcessor.h --------------------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
#ifndef FORTRAN_LOWER_CLAUSEPROCESSOR_H |
| 13 |
#define FORTRAN_LOWER_CLAUSEPROCESSOR_H |
| 14 |
|
| 15 |
#include "Clauses.h" |
| 16 |
#include "DirectivesCommon.h" |
| 17 |
#include "ReductionProcessor.h" |
| 18 |
#include "Utils.h" |
| 19 |
#include "flang/Lower/AbstractConverter.h" |
| 20 |
#include "flang/Lower/Bridge.h" |
| 21 |
#include "flang/Optimizer/Builder/Todo.h" |
| 22 |
#include "flang/Parser/dump-parse-tree.h" |
| 23 |
#include "flang/Parser/parse-tree.h" |
| 24 |
#include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 25 |
|
| 26 |
namespace fir { |
| 27 |
class FirOpBuilder; |
| 28 |
} // namespace fir |
| 29 |
|
| 30 |
namespace Fortran { |
| 31 |
namespace lower { |
| 32 |
namespace omp { |
| 33 |
|
| 34 |
/// Class that handles the processing of OpenMP clauses. |
| 35 |
/// |
| 36 |
/// Its `process<ClauseName>()` methods perform MLIR code generation for their |
| 37 |
/// corresponding clause if it is present in the clause list. Otherwise, they |
| 38 |
/// will return `false` to signal that the clause was not found. |
| 39 |
/// |
| 40 |
/// The intended use of this class is to move clause processing outside of |
| 41 |
/// construct processing, since the same clauses can appear attached to |
| 42 |
/// different constructs and constructs can be combined, so that code |
| 43 |
/// duplication is minimized. |
| 44 |
/// |
| 45 |
/// Each construct-lowering function only calls the `process<ClauseName>()` |
| 46 |
/// methods that relate to clauses that can impact the lowering of that |
| 47 |
/// construct. |
| 48 |
class ClauseProcessor { |
| 49 |
public: |
| 50 |
ClauseProcessor(lower::AbstractConverter &converter, |
| 51 |
semantics::SemanticsContext &semaCtx, |
| 52 |
const List<Clause> &clauses) |
| 53 |
: converter(converter), semaCtx(semaCtx), clauses(clauses) {} |
| 54 |
|
| 55 |
// 'Unique' clauses: They can appear at most once in the clause list. |
| 56 |
bool |
| 57 |
processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval, |
| 58 |
mlir::omp::CollapseClauseOps &result, |
| 59 |
llvm::SmallVectorImpl<const semantics::Symbol *> &iv) const; |
| 60 |
bool processDefault() const; |
| 61 |
bool processDevice(lower::StatementContext &stmtCtx, |
| 62 |
mlir::omp::DeviceClauseOps &result) const; |
| 63 |
bool processDeviceType(mlir::omp::DeviceTypeClauseOps &result) const; |
| 64 |
bool processDistSchedule(lower::StatementContext &stmtCtx, |
| 65 |
mlir::omp::DistScheduleClauseOps &result) const; |
| 66 |
bool processFilter(lower::StatementContext &stmtCtx, |
| 67 |
mlir::omp::FilterClauseOps &result) const; |
| 68 |
bool processFinal(lower::StatementContext &stmtCtx, |
| 69 |
mlir::omp::FinalClauseOps &result) const; |
| 70 |
bool processHasDeviceAddr( |
| 71 |
mlir::omp::HasDeviceAddrClauseOps &result, |
| 72 |
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes, |
| 73 |
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs, |
| 74 |
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSymbols) const; |
| 75 |
bool processHint(mlir::omp::HintClauseOps &result) const; |
| 76 |
bool processMergeable(mlir::omp::MergeableClauseOps &result) const; |
| 77 |
bool processNowait(mlir::omp::NowaitClauseOps &result) const; |
| 78 |
bool processNumTeams(lower::StatementContext &stmtCtx, |
| 79 |
mlir::omp::NumTeamsClauseOps &result) const; |
| 80 |
bool processNumThreads(lower::StatementContext &stmtCtx, |
| 81 |
mlir::omp::NumThreadsClauseOps &result) const; |
| 82 |
bool processOrder(mlir::omp::OrderClauseOps &result) const; |
| 83 |
bool processOrdered(mlir::omp::OrderedClauseOps &result) const; |
| 84 |
bool processPriority(lower::StatementContext &stmtCtx, |
| 85 |
mlir::omp::PriorityClauseOps &result) const; |
| 86 |
bool processProcBind(mlir::omp::ProcBindClauseOps &result) const; |
| 87 |
bool processSafelen(mlir::omp::SafelenClauseOps &result) const; |
| 88 |
bool processSchedule(lower::StatementContext &stmtCtx, |
| 89 |
mlir::omp::ScheduleClauseOps &result) const; |
| 90 |
bool processSimdlen(mlir::omp::SimdlenClauseOps &result) const; |
| 91 |
bool processThreadLimit(lower::StatementContext &stmtCtx, |
| 92 |
mlir::omp::ThreadLimitClauseOps &result) const; |
| 93 |
bool processUntied(mlir::omp::UntiedClauseOps &result) const; |
| 94 |
|
| 95 |
// 'Repeatable' clauses: They can appear multiple times in the clause list. |
| 96 |
bool processAligned(mlir::omp::AlignedClauseOps &result) const; |
| 97 |
bool processAllocate(mlir::omp::AllocateClauseOps &result) const; |
| 98 |
bool processCopyin() const; |
| 99 |
bool processCopyprivate(mlir::Location currentLocation, |
| 100 |
mlir::omp::CopyprivateClauseOps &result) const; |
| 101 |
bool processDepend(mlir::omp::DependClauseOps &result) const; |
| 102 |
bool |
| 103 |
processEnter(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const; |
| 104 |
bool processIf(omp::clause::If::DirectiveNameModifier directiveName, |
| 105 |
mlir::omp::IfClauseOps &result) const; |
| 106 |
bool processIsDevicePtr( |
| 107 |
mlir::omp::IsDevicePtrClauseOps &result, |
| 108 |
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes, |
| 109 |
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs, |
| 110 |
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSymbols) const; |
| 111 |
bool |
| 112 |
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const; |
| 113 |
|
| 114 |
// This method is used to process a map clause. |
| 115 |
// The optional parameters - mapSymTypes, mapSymLocs & mapSyms are used to |
| 116 |
// store the original type, location and Fortran symbol for the map operands. |
| 117 |
// They may be used later on to create the block_arguments for some of the |
| 118 |
// target directives that require it. |
| 119 |
bool processMap( |
| 120 |
mlir::Location currentLocation, lower::StatementContext &stmtCtx, |
| 121 |
mlir::omp::MapClauseOps &result, |
| 122 |
llvm::SmallVectorImpl<const semantics::Symbol *> *mapSyms = nullptr, |
| 123 |
llvm::SmallVectorImpl<mlir::Location> *mapSymLocs = nullptr, |
| 124 |
llvm::SmallVectorImpl<mlir::Type> *mapSymTypes = nullptr) const; |
| 125 |
bool processReduction( |
| 126 |
mlir::Location currentLocation, mlir::omp::ReductionClauseOps &result, |
| 127 |
llvm::SmallVectorImpl<mlir::Type> *reductionTypes = nullptr, |
| 128 |
llvm::SmallVectorImpl<const semantics::Symbol *> *reductionSyms = |
| 129 |
nullptr) const; |
| 130 |
bool processTo(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const; |
| 131 |
bool processUseDeviceAddr( |
| 132 |
mlir::omp::UseDeviceAddrClauseOps &result, |
| 133 |
llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes, |
| 134 |
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs, |
| 135 |
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const; |
| 136 |
bool processUseDevicePtr( |
| 137 |
mlir::omp::UseDevicePtrClauseOps &result, |
| 138 |
llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes, |
| 139 |
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs, |
| 140 |
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const; |
| 141 |
|
| 142 |
template <typename T> |
| 143 |
bool processMotionClauses(lower::StatementContext &stmtCtx, |
| 144 |
mlir::omp::MapClauseOps &result); |
| 145 |
// Call this method for these clauses that should be supported but are not |
| 146 |
// implemented yet. It triggers a compilation error if any of the given |
| 147 |
// clauses is found. |
| 148 |
template <typename... Ts> |
| 149 |
void processTODO(mlir::Location currentLocation, |
| 150 |
llvm::omp::Directive directive) const; |
| 151 |
|
| 152 |
private: |
| 153 |
using ClauseIterator = List<Clause>::const_iterator; |
| 154 |
|
| 155 |
/// Utility to find a clause within a range in the clause list. |
| 156 |
template <typename T> |
| 157 |
static ClauseIterator findClause(ClauseIterator begin, ClauseIterator end); |
| 158 |
|
| 159 |
/// Return the first instance of the given clause found in the clause list or |
| 160 |
/// `nullptr` if not present. If more than one instance is expected, use |
| 161 |
/// `findRepeatableClause` instead. |
| 162 |
template <typename T> |
| 163 |
const T *findUniqueClause(const parser::CharBlock **source = nullptr) const; |
| 164 |
|
| 165 |
/// Call `callbackFn` for each occurrence of the given clause. Return `true` |
| 166 |
/// if at least one instance was found. |
| 167 |
template <typename T> |
| 168 |
bool findRepeatableClause( |
| 169 |
std::function<void(const T &, const parser::CharBlock &source)> |
| 170 |
callbackFn) const; |
| 171 |
|
| 172 |
/// Set the `result` to a new `mlir::UnitAttr` if the clause is present. |
| 173 |
template <typename T> |
| 174 |
bool markClauseOccurrence(mlir::UnitAttr &result) const; |
| 175 |
|
| 176 |
lower::AbstractConverter &converter; |
| 177 |
semantics::SemanticsContext &semaCtx; |
| 178 |
List<Clause> clauses; |
| 179 |
}; |
| 180 |
|
| 181 |
template <typename T> |
| 182 |
bool ClauseProcessor::processMotionClauses(lower::StatementContext &stmtCtx, |
| 183 |
mlir::omp::MapClauseOps &result) { |
| 184 |
std::map<const semantics::Symbol *, |
| 185 |
llvm::SmallVector<OmpMapMemberIndicesData>> |
| 186 |
parentMemberIndices; |
| 187 |
llvm::SmallVector<const semantics::Symbol *> mapSymbols; |
| 188 |
|
| 189 |
bool clauseFound = findRepeatableClause<T>( |
| 190 |
[&](const T &clause, const parser::CharBlock &source) { |
| 191 |
mlir::Location clauseLocation = converter.genLocation(source); |
| 192 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 193 |
|
| 194 |
static_assert(std::is_same_v<T, omp::clause::To> || |
| 195 |
std::is_same_v<T, omp::clause::From>); |
| 196 |
|
| 197 |
// TODO Support motion modifiers: present, mapper, iterator. |
| 198 |
constexpr llvm::omp::OpenMPOffloadMappingFlags mapTypeBits = |
| 199 |
std::is_same_v<T, omp::clause::To> |
| 200 |
? llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO |
| 201 |
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_FROM; |
| 202 |
|
| 203 |
auto &objects = std::get<ObjectList>(clause.t); |
| 204 |
for (const omp::Object &object : objects) { |
| 205 |
llvm::SmallVector<mlir::Value> bounds; |
| 206 |
std::stringstream asFortran; |
| 207 |
|
| 208 |
lower::AddrAndBoundsInfo info = |
| 209 |
lower::gatherDataOperandAddrAndBounds<mlir::omp::MapBoundsOp, |
| 210 |
mlir::omp::MapBoundsType>( |
| 211 |
converter, firOpBuilder, semaCtx, stmtCtx, *object.sym(), |
| 212 |
object.ref(), clauseLocation, asFortran, bounds, |
| 213 |
treatIndexAsSection); |
| 214 |
|
| 215 |
auto origSymbol = converter.getSymbolAddress(*object.sym()); |
| 216 |
mlir::Value symAddr = info.addr; |
| 217 |
if (origSymbol && fir::isTypeWithDescriptor(origSymbol.getType())) |
| 218 |
symAddr = origSymbol; |
| 219 |
|
| 220 |
// Explicit map captures are captured ByRef by default, |
| 221 |
// optimisation passes may alter this to ByCopy or other capture |
| 222 |
// types to optimise |
| 223 |
mlir::omp::MapInfoOp mapOp = createMapInfoOp( |
| 224 |
firOpBuilder, clauseLocation, symAddr, |
| 225 |
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds, |
| 226 |
/*members=*/{}, /*membersIndex=*/mlir::DenseIntElementsAttr{}, |
| 227 |
static_cast< |
| 228 |
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>( |
| 229 |
mapTypeBits), |
| 230 |
mlir::omp::VariableCaptureKind::ByRef, symAddr.getType()); |
| 231 |
|
| 232 |
if (object.sym()->owner().IsDerivedType()) { |
| 233 |
addChildIndexAndMapToParent(object, parentMemberIndices, mapOp, |
| 234 |
semaCtx); |
| 235 |
} else { |
| 236 |
result.mapVars.push_back(mapOp); |
| 237 |
mapSymbols.push_back(object.sym()); |
| 238 |
} |
| 239 |
} |
| 240 |
}); |
| 241 |
|
| 242 |
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars, |
| 243 |
mapSymbols, |
| 244 |
/*mapSymTypes=*/nullptr, /*mapSymLocs=*/nullptr); |
| 245 |
return clauseFound; |
| 246 |
} |
| 247 |
|
| 248 |
template <typename... Ts> |
| 249 |
void ClauseProcessor::processTODO(mlir::Location currentLocation, |
| 250 |
llvm::omp::Directive directive) const { |
| 251 |
auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) { |
| 252 |
if (!x) |
| 253 |
return; |
| 254 |
TODO(currentLocation, |
| 255 |
"Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() + |
| 256 |
" in " + llvm::omp::getOpenMPDirectiveName(directive).upper() + |
| 257 |
" construct"); |
| 258 |
}; |
| 259 |
|
| 260 |
for (ClauseIterator it = clauses.begin(); it != clauses.end(); ++it) |
| 261 |
(checkUnhandledClause(it->id, std::get_if<Ts>(&it->u)), ...); |
| 262 |
} |
| 263 |
|
| 264 |
template <typename T> |
| 265 |
ClauseProcessor::ClauseIterator |
| 266 |
ClauseProcessor::findClause(ClauseIterator begin, ClauseIterator end) { |
| 267 |
for (ClauseIterator it = begin; it != end; ++it) { |
| 268 |
if (std::get_if<T>(&it->u)) |
| 269 |
return it; |
| 270 |
} |
| 271 |
|
| 272 |
return end; |
| 273 |
} |
| 274 |
|
| 275 |
template <typename T> |
| 276 |
const T * |
| 277 |
ClauseProcessor::findUniqueClause(const parser::CharBlock **source) const { |
| 278 |
ClauseIterator it = findClause<T>(clauses.begin(), clauses.end()); |
| 279 |
if (it != clauses.end()) { |
| 280 |
if (source) |
| 281 |
*source = &it->source; |
| 282 |
return &std::get<T>(it->u); |
| 283 |
} |
| 284 |
return nullptr; |
| 285 |
} |
| 286 |
|
| 287 |
template <typename T> |
| 288 |
bool ClauseProcessor::findRepeatableClause( |
| 289 |
std::function<void(const T &, const parser::CharBlock &source)> callbackFn) |
| 290 |
const { |
| 291 |
bool found = false; |
| 292 |
ClauseIterator nextIt, endIt = clauses.end(); |
| 293 |
for (ClauseIterator it = clauses.begin(); it != endIt; it = nextIt) { |
| 294 |
nextIt = findClause<T>(it, endIt); |
| 295 |
|
| 296 |
if (nextIt != endIt) { |
| 297 |
callbackFn(std::get<T>(nextIt->u), nextIt->source); |
| 298 |
found = true; |
| 299 |
++nextIt; |
| 300 |
} |
| 301 |
} |
| 302 |
return found; |
| 303 |
} |
| 304 |
|
| 305 |
template <typename T> |
| 306 |
bool ClauseProcessor::markClauseOccurrence(mlir::UnitAttr &result) const { |
| 307 |
if (findUniqueClause<T>()) { |
| 308 |
result = converter.getFirOpBuilder().getUnitAttr(); |
| 309 |
return true; |
| 310 |
} |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
} // namespace omp |
| 315 |
} // namespace lower |
| 316 |
} // namespace Fortran |
| 317 |
|
| 318 |
#endif // FORTRAN_LOWER_CLAUSEPROCESSOR_H |