| 1 |
//===-- ClauseProcessor.cpp -------------------------------------*- 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 |
|
| 13 |
#include "ClauseProcessor.h" |
| 14 |
#include "Clauses.h" |
| 15 |
|
| 16 |
#include "flang/Lower/PFTBuilder.h" |
| 17 |
#include "flang/Parser/tools.h" |
| 18 |
#include "flang/Semantics/tools.h" |
| 19 |
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h" |
| 20 |
|
| 21 |
namespace Fortran { |
| 22 |
namespace lower { |
| 23 |
namespace omp { |
| 24 |
|
| 25 |
/// Check for unsupported map operand types. |
| 26 |
static void checkMapType(mlir::Location location, mlir::Type type) { |
| 27 |
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(type)) |
| 28 |
type = refType.getElementType(); |
| 29 |
if (auto boxType = mlir::dyn_cast_or_null<fir::BoxType>(type)) |
| 30 |
if (!mlir::isa<fir::PointerType>(boxType.getElementType())) |
| 31 |
TODO(location, "OMPD_target_data MapOperand BoxType"); |
| 32 |
} |
| 33 |
|
| 34 |
static mlir::omp::ScheduleModifier |
| 35 |
translateScheduleModifier(const omp::clause::Schedule::OrderingModifier &m) { |
| 36 |
switch (m) { |
| 37 |
case omp::clause::Schedule::OrderingModifier::Monotonic: |
| 38 |
return mlir::omp::ScheduleModifier::monotonic; |
| 39 |
case omp::clause::Schedule::OrderingModifier::Nonmonotonic: |
| 40 |
return mlir::omp::ScheduleModifier::nonmonotonic; |
| 41 |
} |
| 42 |
return mlir::omp::ScheduleModifier::none; |
| 43 |
} |
| 44 |
|
| 45 |
static mlir::omp::ScheduleModifier |
| 46 |
getScheduleModifier(const omp::clause::Schedule &clause) { |
| 47 |
using Schedule = omp::clause::Schedule; |
| 48 |
const auto &modifier = |
| 49 |
std::get<std::optional<Schedule::OrderingModifier>>(clause.t); |
| 50 |
if (modifier) |
| 51 |
return translateScheduleModifier(*modifier); |
| 52 |
return mlir::omp::ScheduleModifier::none; |
| 53 |
} |
| 54 |
|
| 55 |
static mlir::omp::ScheduleModifier |
| 56 |
getSimdModifier(const omp::clause::Schedule &clause) { |
| 57 |
using Schedule = omp::clause::Schedule; |
| 58 |
const auto &modifier = |
| 59 |
std::get<std::optional<Schedule::ChunkModifier>>(clause.t); |
| 60 |
if (modifier && *modifier == Schedule::ChunkModifier::Simd) |
| 61 |
return mlir::omp::ScheduleModifier::simd; |
| 62 |
return mlir::omp::ScheduleModifier::none; |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
genAllocateClause(lower::AbstractConverter &converter, |
| 67 |
const omp::clause::Allocate &clause, |
| 68 |
llvm::SmallVectorImpl<mlir::Value> &allocatorOperands, |
| 69 |
llvm::SmallVectorImpl<mlir::Value> &allocateOperands) { |
| 70 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 71 |
mlir::Location currentLocation = converter.getCurrentLocation(); |
| 72 |
lower::StatementContext stmtCtx; |
| 73 |
|
| 74 |
auto &objects = std::get<omp::ObjectList>(clause.t); |
| 75 |
|
| 76 |
using Allocate = omp::clause::Allocate; |
| 77 |
// ALIGN in this context is unimplemented |
| 78 |
if (std::get<std::optional<Allocate::AlignModifier>>(clause.t)) |
| 79 |
TODO(currentLocation, "OmpAllocateClause ALIGN modifier"); |
| 80 |
|
| 81 |
// Check if allocate clause has allocator specified. If so, add it |
| 82 |
// to list of allocators, otherwise, add default allocator to |
| 83 |
// list of allocators. |
| 84 |
using SimpleModifier = Allocate::AllocatorSimpleModifier; |
| 85 |
using ComplexModifier = Allocate::AllocatorComplexModifier; |
| 86 |
if (auto &mod = std::get<std::optional<SimpleModifier>>(clause.t)) { |
| 87 |
mlir::Value operand = fir::getBase(converter.genExprValue(*mod, stmtCtx)); |
| 88 |
allocatorOperands.append(objects.size(), operand); |
| 89 |
} else if (auto &mod = std::get<std::optional<ComplexModifier>>(clause.t)) { |
| 90 |
mlir::Value operand = fir::getBase(converter.genExprValue(mod->v, stmtCtx)); |
| 91 |
allocatorOperands.append(objects.size(), operand); |
| 92 |
} else { |
| 93 |
mlir::Value operand = firOpBuilder.createIntegerConstant( |
| 94 |
currentLocation, firOpBuilder.getI32Type(), 1); |
| 95 |
allocatorOperands.append(objects.size(), operand); |
| 96 |
} |
| 97 |
|
| 98 |
genObjectList(objects, converter, allocateOperands); |
| 99 |
} |
| 100 |
|
| 101 |
static mlir::omp::ClauseProcBindKindAttr |
| 102 |
genProcBindKindAttr(fir::FirOpBuilder &firOpBuilder, |
| 103 |
const omp::clause::ProcBind &clause) { |
| 104 |
mlir::omp::ClauseProcBindKind procBindKind; |
| 105 |
switch (clause.v) { |
| 106 |
case omp::clause::ProcBind::AffinityPolicy::Master: |
| 107 |
procBindKind = mlir::omp::ClauseProcBindKind::Master; |
| 108 |
break; |
| 109 |
case omp::clause::ProcBind::AffinityPolicy::Close: |
| 110 |
procBindKind = mlir::omp::ClauseProcBindKind::Close; |
| 111 |
break; |
| 112 |
case omp::clause::ProcBind::AffinityPolicy::Spread: |
| 113 |
procBindKind = mlir::omp::ClauseProcBindKind::Spread; |
| 114 |
break; |
| 115 |
case omp::clause::ProcBind::AffinityPolicy::Primary: |
| 116 |
procBindKind = mlir::omp::ClauseProcBindKind::Primary; |
| 117 |
break; |
| 118 |
} |
| 119 |
return mlir::omp::ClauseProcBindKindAttr::get(firOpBuilder.getContext(), |
| 120 |
procBindKind); |
| 121 |
} |
| 122 |
|
| 123 |
static mlir::omp::ClauseTaskDependAttr |
| 124 |
genDependKindAttr(fir::FirOpBuilder &firOpBuilder, |
| 125 |
const omp::clause::Depend::TaskDependenceType kind) { |
| 126 |
mlir::omp::ClauseTaskDepend pbKind; |
| 127 |
switch (kind) { |
| 128 |
case omp::clause::Depend::TaskDependenceType::In: |
| 129 |
pbKind = mlir::omp::ClauseTaskDepend::taskdependin; |
| 130 |
break; |
| 131 |
case omp::clause::Depend::TaskDependenceType::Out: |
| 132 |
pbKind = mlir::omp::ClauseTaskDepend::taskdependout; |
| 133 |
break; |
| 134 |
case omp::clause::Depend::TaskDependenceType::Inout: |
| 135 |
pbKind = mlir::omp::ClauseTaskDepend::taskdependinout; |
| 136 |
break; |
| 137 |
case omp::clause::Depend::TaskDependenceType::Mutexinoutset: |
| 138 |
case omp::clause::Depend::TaskDependenceType::Inoutset: |
| 139 |
case omp::clause::Depend::TaskDependenceType::Depobj: |
| 140 |
llvm_unreachable("unhandled parser task dependence type"); |
| 141 |
break; |
| 142 |
} |
| 143 |
return mlir::omp::ClauseTaskDependAttr::get(firOpBuilder.getContext(), |
| 144 |
pbKind); |
| 145 |
} |
| 146 |
|
| 147 |
static mlir::Value |
| 148 |
getIfClauseOperand(lower::AbstractConverter &converter, |
| 149 |
const omp::clause::If &clause, |
| 150 |
omp::clause::If::DirectiveNameModifier directiveName, |
| 151 |
mlir::Location clauseLocation) { |
| 152 |
// Only consider the clause if it's intended for the given directive. |
| 153 |
auto &directive = |
| 154 |
std::get<std::optional<omp::clause::If::DirectiveNameModifier>>(clause.t); |
| 155 |
if (directive && directive.value() != directiveName) |
| 156 |
return nullptr; |
| 157 |
|
| 158 |
lower::StatementContext stmtCtx; |
| 159 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 160 |
mlir::Value ifVal = fir::getBase( |
| 161 |
converter.genExprValue(std::get<omp::SomeExpr>(clause.t), stmtCtx)); |
| 162 |
return firOpBuilder.createConvert(clauseLocation, firOpBuilder.getI1Type(), |
| 163 |
ifVal); |
| 164 |
} |
| 165 |
|
| 166 |
static void addUseDeviceClause( |
| 167 |
lower::AbstractConverter &converter, const omp::ObjectList &objects, |
| 168 |
llvm::SmallVectorImpl<mlir::Value> &operands, |
| 169 |
llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes, |
| 170 |
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs, |
| 171 |
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) { |
| 172 |
genObjectList(objects, converter, operands); |
| 173 |
for (mlir::Value &operand : operands) { |
| 174 |
checkMapType(operand.getLoc(), operand.getType()); |
| 175 |
useDeviceTypes.push_back(operand.getType()); |
| 176 |
useDeviceLocs.push_back(operand.getLoc()); |
| 177 |
} |
| 178 |
for (const omp::Object &object : objects) |
| 179 |
useDeviceSyms.push_back(object.sym()); |
| 180 |
} |
| 181 |
|
| 182 |
static void convertLoopBounds(lower::AbstractConverter &converter, |
| 183 |
mlir::Location loc, |
| 184 |
mlir::omp::CollapseClauseOps &result, |
| 185 |
std::size_t loopVarTypeSize) { |
| 186 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 187 |
// The types of lower bound, upper bound, and step are converted into the |
| 188 |
// type of the loop variable if necessary. |
| 189 |
mlir::Type loopVarType = getLoopVarType(converter, loopVarTypeSize); |
| 190 |
for (unsigned it = 0; it < (unsigned)result.loopLBVar.size(); it++) { |
| 191 |
result.loopLBVar[it] = |
| 192 |
firOpBuilder.createConvert(loc, loopVarType, result.loopLBVar[it]); |
| 193 |
result.loopUBVar[it] = |
| 194 |
firOpBuilder.createConvert(loc, loopVarType, result.loopUBVar[it]); |
| 195 |
result.loopStepVar[it] = |
| 196 |
firOpBuilder.createConvert(loc, loopVarType, result.loopStepVar[it]); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
//===----------------------------------------------------------------------===// |
| 201 |
// ClauseProcessor unique clauses |
| 202 |
//===----------------------------------------------------------------------===// |
| 203 |
|
| 204 |
bool ClauseProcessor::processCollapse( |
| 205 |
mlir::Location currentLocation, lower::pft::Evaluation &eval, |
| 206 |
mlir::omp::CollapseClauseOps &result, |
| 207 |
llvm::SmallVectorImpl<const semantics::Symbol *> &iv) const { |
| 208 |
bool found = false; |
| 209 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 210 |
|
| 211 |
// Collect the loops to collapse. |
| 212 |
lower::pft::Evaluation *doConstructEval = &eval.getFirstNestedEvaluation(); |
| 213 |
if (doConstructEval->getIf<parser::DoConstruct>()->IsDoConcurrent()) { |
| 214 |
TODO(currentLocation, "Do Concurrent in Worksharing loop construct"); |
| 215 |
} |
| 216 |
|
| 217 |
std::int64_t collapseValue = 1l; |
| 218 |
if (auto *clause = findUniqueClause<omp::clause::Collapse>()) { |
| 219 |
collapseValue = evaluate::ToInt64(clause->v).value(); |
| 220 |
found = true; |
| 221 |
} |
| 222 |
|
| 223 |
std::size_t loopVarTypeSize = 0; |
| 224 |
do { |
| 225 |
lower::pft::Evaluation *doLoop = |
| 226 |
&doConstructEval->getFirstNestedEvaluation(); |
| 227 |
auto *doStmt = doLoop->getIf<parser::NonLabelDoStmt>(); |
| 228 |
assert(doStmt && "Expected do loop to be in the nested evaluation"); |
| 229 |
const auto &loopControl = |
| 230 |
std::get<std::optional<parser::LoopControl>>(doStmt->t); |
| 231 |
const parser::LoopControl::Bounds *bounds = |
| 232 |
std::get_if<parser::LoopControl::Bounds>(&loopControl->u); |
| 233 |
assert(bounds && "Expected bounds for worksharing do loop"); |
| 234 |
lower::StatementContext stmtCtx; |
| 235 |
result.loopLBVar.push_back(fir::getBase( |
| 236 |
converter.genExprValue(*semantics::GetExpr(bounds->lower), stmtCtx))); |
| 237 |
result.loopUBVar.push_back(fir::getBase( |
| 238 |
converter.genExprValue(*semantics::GetExpr(bounds->upper), stmtCtx))); |
| 239 |
if (bounds->step) { |
| 240 |
result.loopStepVar.push_back(fir::getBase( |
| 241 |
converter.genExprValue(*semantics::GetExpr(bounds->step), stmtCtx))); |
| 242 |
} else { // If `step` is not present, assume it as `1`. |
| 243 |
result.loopStepVar.push_back(firOpBuilder.createIntegerConstant( |
| 244 |
currentLocation, firOpBuilder.getIntegerType(32), 1)); |
| 245 |
} |
| 246 |
iv.push_back(bounds->name.thing.symbol); |
| 247 |
loopVarTypeSize = std::max(loopVarTypeSize, |
| 248 |
bounds->name.thing.symbol->GetUltimate().size()); |
| 249 |
collapseValue--; |
| 250 |
doConstructEval = |
| 251 |
&*std::next(doConstructEval->getNestedEvaluations().begin()); |
| 252 |
} while (collapseValue > 0); |
| 253 |
|
| 254 |
convertLoopBounds(converter, currentLocation, result, loopVarTypeSize); |
| 255 |
|
| 256 |
return found; |
| 257 |
} |
| 258 |
|
| 259 |
bool ClauseProcessor::processDefault() const { |
| 260 |
if (auto *clause = findUniqueClause<omp::clause::Default>()) { |
| 261 |
// Private, Firstprivate, Shared, None |
| 262 |
switch (clause->v) { |
| 263 |
case omp::clause::Default::DataSharingAttribute::Shared: |
| 264 |
case omp::clause::Default::DataSharingAttribute::None: |
| 265 |
// Default clause with shared or none do not require any handling since |
| 266 |
// Shared is the default behavior in the IR and None is only required |
| 267 |
// for semantic checks. |
| 268 |
break; |
| 269 |
case omp::clause::Default::DataSharingAttribute::Private: |
| 270 |
// TODO Support default(private) |
| 271 |
break; |
| 272 |
case omp::clause::Default::DataSharingAttribute::Firstprivate: |
| 273 |
// TODO Support default(firstprivate) |
| 274 |
break; |
| 275 |
} |
| 276 |
return true; |
| 277 |
} |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
bool ClauseProcessor::processDevice(lower::StatementContext &stmtCtx, |
| 282 |
mlir::omp::DeviceClauseOps &result) const { |
| 283 |
const parser::CharBlock *source = nullptr; |
| 284 |
if (auto *clause = findUniqueClause<omp::clause::Device>(&source)) { |
| 285 |
mlir::Location clauseLocation = converter.genLocation(*source); |
| 286 |
if (auto deviceModifier = |
| 287 |
std::get<std::optional<omp::clause::Device::DeviceModifier>>( |
| 288 |
clause->t)) { |
| 289 |
if (deviceModifier == omp::clause::Device::DeviceModifier::Ancestor) { |
| 290 |
TODO(clauseLocation, "OMPD_target Device Modifier Ancestor"); |
| 291 |
} |
| 292 |
} |
| 293 |
const auto &deviceExpr = std::get<omp::SomeExpr>(clause->t); |
| 294 |
result.deviceVar = |
| 295 |
fir::getBase(converter.genExprValue(deviceExpr, stmtCtx)); |
| 296 |
return true; |
| 297 |
} |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
bool ClauseProcessor::processDeviceType( |
| 302 |
mlir::omp::DeviceTypeClauseOps &result) const { |
| 303 |
if (auto *clause = findUniqueClause<omp::clause::DeviceType>()) { |
| 304 |
// Case: declare target ... device_type(any | host | nohost) |
| 305 |
switch (clause->v) { |
| 306 |
case omp::clause::DeviceType::DeviceTypeDescription::Nohost: |
| 307 |
result.deviceType = mlir::omp::DeclareTargetDeviceType::nohost; |
| 308 |
break; |
| 309 |
case omp::clause::DeviceType::DeviceTypeDescription::Host: |
| 310 |
result.deviceType = mlir::omp::DeclareTargetDeviceType::host; |
| 311 |
break; |
| 312 |
case omp::clause::DeviceType::DeviceTypeDescription::Any: |
| 313 |
result.deviceType = mlir::omp::DeclareTargetDeviceType::any; |
| 314 |
break; |
| 315 |
} |
| 316 |
return true; |
| 317 |
} |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
bool ClauseProcessor::processDistSchedule( |
| 322 |
lower::StatementContext &stmtCtx, |
| 323 |
mlir::omp::DistScheduleClauseOps &result) const { |
| 324 |
if (auto *clause = findUniqueClause<omp::clause::DistSchedule>()) { |
| 325 |
result.distScheduleStaticAttr = converter.getFirOpBuilder().getUnitAttr(); |
| 326 |
const auto &chunkSize = std::get<std::optional<ExprTy>>(clause->t); |
| 327 |
if (chunkSize) |
| 328 |
result.distScheduleChunkSizeVar = |
| 329 |
fir::getBase(converter.genExprValue(*chunkSize, stmtCtx)); |
| 330 |
return true; |
| 331 |
} |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
bool ClauseProcessor::processFilter(lower::StatementContext &stmtCtx, |
| 336 |
mlir::omp::FilterClauseOps &result) const { |
| 337 |
if (auto *clause = findUniqueClause<omp::clause::Filter>()) { |
| 338 |
result.filteredThreadIdVar = |
| 339 |
fir::getBase(converter.genExprValue(clause->v, stmtCtx)); |
| 340 |
return true; |
| 341 |
} |
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
bool ClauseProcessor::processFinal(lower::StatementContext &stmtCtx, |
| 346 |
mlir::omp::FinalClauseOps &result) const { |
| 347 |
const parser::CharBlock *source = nullptr; |
| 348 |
if (auto *clause = findUniqueClause<omp::clause::Final>(&source)) { |
| 349 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 350 |
mlir::Location clauseLocation = converter.genLocation(*source); |
| 351 |
|
| 352 |
mlir::Value finalVal = |
| 353 |
fir::getBase(converter.genExprValue(clause->v, stmtCtx)); |
| 354 |
result.finalVar = firOpBuilder.createConvert( |
| 355 |
clauseLocation, firOpBuilder.getI1Type(), finalVal); |
| 356 |
return true; |
| 357 |
} |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
bool ClauseProcessor::processHint(mlir::omp::HintClauseOps &result) const { |
| 362 |
if (auto *clause = findUniqueClause<omp::clause::Hint>()) { |
| 363 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 364 |
int64_t hintValue = *evaluate::ToInt64(clause->v); |
| 365 |
result.hintAttr = firOpBuilder.getI64IntegerAttr(hintValue); |
| 366 |
return true; |
| 367 |
} |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
bool ClauseProcessor::processMergeable( |
| 372 |
mlir::omp::MergeableClauseOps &result) const { |
| 373 |
return markClauseOccurrence<omp::clause::Mergeable>(result.mergeableAttr); |
| 374 |
} |
| 375 |
|
| 376 |
bool ClauseProcessor::processNowait(mlir::omp::NowaitClauseOps &result) const { |
| 377 |
return markClauseOccurrence<omp::clause::Nowait>(result.nowaitAttr); |
| 378 |
} |
| 379 |
|
| 380 |
bool ClauseProcessor::processNumTeams( |
| 381 |
lower::StatementContext &stmtCtx, |
| 382 |
mlir::omp::NumTeamsClauseOps &result) const { |
| 383 |
// TODO Get lower and upper bounds for num_teams when parser is updated to |
| 384 |
// accept both. |
| 385 |
if (auto *clause = findUniqueClause<omp::clause::NumTeams>()) { |
| 386 |
// auto lowerBound = std::get<std::optional<ExprTy>>(clause->t); |
| 387 |
auto &upperBound = std::get<ExprTy>(clause->t); |
| 388 |
result.numTeamsUpperVar = |
| 389 |
fir::getBase(converter.genExprValue(upperBound, stmtCtx)); |
| 390 |
return true; |
| 391 |
} |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
bool ClauseProcessor::processNumThreads( |
| 396 |
lower::StatementContext &stmtCtx, |
| 397 |
mlir::omp::NumThreadsClauseOps &result) const { |
| 398 |
if (auto *clause = findUniqueClause<omp::clause::NumThreads>()) { |
| 399 |
// OMPIRBuilder expects `NUM_THREADS` clause as a `Value`. |
| 400 |
result.numThreadsVar = |
| 401 |
fir::getBase(converter.genExprValue(clause->v, stmtCtx)); |
| 402 |
return true; |
| 403 |
} |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
bool ClauseProcessor::processOrder(mlir::omp::OrderClauseOps &result) const { |
| 408 |
using Order = omp::clause::Order; |
| 409 |
if (auto *clause = findUniqueClause<Order>()) { |
| 410 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 411 |
result.orderAttr = mlir::omp::ClauseOrderKindAttr::get( |
| 412 |
firOpBuilder.getContext(), mlir::omp::ClauseOrderKind::Concurrent); |
| 413 |
const auto &modifier = |
| 414 |
std::get<std::optional<Order::OrderModifier>>(clause->t); |
| 415 |
if (modifier && *modifier == Order::OrderModifier::Unconstrained) { |
| 416 |
result.orderModAttr = mlir::omp::OrderModifierAttr::get( |
| 417 |
firOpBuilder.getContext(), mlir::omp::OrderModifier::unconstrained); |
| 418 |
} else { |
| 419 |
// "If order-modifier is not unconstrained, the behavior is as if the |
| 420 |
// reproducible modifier is present." |
| 421 |
result.orderModAttr = mlir::omp::OrderModifierAttr::get( |
| 422 |
firOpBuilder.getContext(), mlir::omp::OrderModifier::reproducible); |
| 423 |
} |
| 424 |
return true; |
| 425 |
} |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
bool ClauseProcessor::processOrdered( |
| 430 |
mlir::omp::OrderedClauseOps &result) const { |
| 431 |
if (auto *clause = findUniqueClause<omp::clause::Ordered>()) { |
| 432 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 433 |
int64_t orderedClauseValue = 0l; |
| 434 |
if (clause->v.has_value()) |
| 435 |
orderedClauseValue = *evaluate::ToInt64(*clause->v); |
| 436 |
result.orderedAttr = firOpBuilder.getI64IntegerAttr(orderedClauseValue); |
| 437 |
return true; |
| 438 |
} |
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
bool ClauseProcessor::processPriority( |
| 443 |
lower::StatementContext &stmtCtx, |
| 444 |
mlir::omp::PriorityClauseOps &result) const { |
| 445 |
if (auto *clause = findUniqueClause<omp::clause::Priority>()) { |
| 446 |
result.priorityVar = |
| 447 |
fir::getBase(converter.genExprValue(clause->v, stmtCtx)); |
| 448 |
return true; |
| 449 |
} |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
bool ClauseProcessor::processProcBind( |
| 454 |
mlir::omp::ProcBindClauseOps &result) const { |
| 455 |
if (auto *clause = findUniqueClause<omp::clause::ProcBind>()) { |
| 456 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 457 |
result.procBindKindAttr = genProcBindKindAttr(firOpBuilder, *clause); |
| 458 |
return true; |
| 459 |
} |
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
bool ClauseProcessor::processSafelen( |
| 464 |
mlir::omp::SafelenClauseOps &result) const { |
| 465 |
if (auto *clause = findUniqueClause<omp::clause::Safelen>()) { |
| 466 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 467 |
const std::optional<std::int64_t> safelenVal = evaluate::ToInt64(clause->v); |
| 468 |
result.safelenAttr = firOpBuilder.getI64IntegerAttr(*safelenVal); |
| 469 |
return true; |
| 470 |
} |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
bool ClauseProcessor::processSchedule( |
| 475 |
lower::StatementContext &stmtCtx, |
| 476 |
mlir::omp::ScheduleClauseOps &result) const { |
| 477 |
if (auto *clause = findUniqueClause<omp::clause::Schedule>()) { |
| 478 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 479 |
mlir::MLIRContext *context = firOpBuilder.getContext(); |
| 480 |
const auto &scheduleType = std::get<omp::clause::Schedule::Kind>(clause->t); |
| 481 |
|
| 482 |
mlir::omp::ClauseScheduleKind scheduleKind; |
| 483 |
switch (scheduleType) { |
| 484 |
case omp::clause::Schedule::Kind::Static: |
| 485 |
scheduleKind = mlir::omp::ClauseScheduleKind::Static; |
| 486 |
break; |
| 487 |
case omp::clause::Schedule::Kind::Dynamic: |
| 488 |
scheduleKind = mlir::omp::ClauseScheduleKind::Dynamic; |
| 489 |
break; |
| 490 |
case omp::clause::Schedule::Kind::Guided: |
| 491 |
scheduleKind = mlir::omp::ClauseScheduleKind::Guided; |
| 492 |
break; |
| 493 |
case omp::clause::Schedule::Kind::Auto: |
| 494 |
scheduleKind = mlir::omp::ClauseScheduleKind::Auto; |
| 495 |
break; |
| 496 |
case omp::clause::Schedule::Kind::Runtime: |
| 497 |
scheduleKind = mlir::omp::ClauseScheduleKind::Runtime; |
| 498 |
break; |
| 499 |
} |
| 500 |
|
| 501 |
result.scheduleValAttr = |
| 502 |
mlir::omp::ClauseScheduleKindAttr::get(context, scheduleKind); |
| 503 |
|
| 504 |
mlir::omp::ScheduleModifier scheduleModifier = getScheduleModifier(*clause); |
| 505 |
if (scheduleModifier != mlir::omp::ScheduleModifier::none) |
| 506 |
result.scheduleModAttr = |
| 507 |
mlir::omp::ScheduleModifierAttr::get(context, scheduleModifier); |
| 508 |
|
| 509 |
if (getSimdModifier(*clause) != mlir::omp::ScheduleModifier::none) |
| 510 |
result.scheduleSimdAttr = firOpBuilder.getUnitAttr(); |
| 511 |
|
| 512 |
if (const auto &chunkExpr = std::get<omp::MaybeExpr>(clause->t)) |
| 513 |
result.scheduleChunkVar = |
| 514 |
fir::getBase(converter.genExprValue(*chunkExpr, stmtCtx)); |
| 515 |
|
| 516 |
return true; |
| 517 |
} |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
bool ClauseProcessor::processSimdlen( |
| 522 |
mlir::omp::SimdlenClauseOps &result) const { |
| 523 |
if (auto *clause = findUniqueClause<omp::clause::Simdlen>()) { |
| 524 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 525 |
const std::optional<std::int64_t> simdlenVal = evaluate::ToInt64(clause->v); |
| 526 |
result.simdlenAttr = firOpBuilder.getI64IntegerAttr(*simdlenVal); |
| 527 |
return true; |
| 528 |
} |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
bool ClauseProcessor::processThreadLimit( |
| 533 |
lower::StatementContext &stmtCtx, |
| 534 |
mlir::omp::ThreadLimitClauseOps &result) const { |
| 535 |
if (auto *clause = findUniqueClause<omp::clause::ThreadLimit>()) { |
| 536 |
result.threadLimitVar = |
| 537 |
fir::getBase(converter.genExprValue(clause->v, stmtCtx)); |
| 538 |
return true; |
| 539 |
} |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
bool ClauseProcessor::processUntied(mlir::omp::UntiedClauseOps &result) const { |
| 544 |
return markClauseOccurrence<omp::clause::Untied>(result.untiedAttr); |
| 545 |
} |
| 546 |
|
| 547 |
//===----------------------------------------------------------------------===// |
| 548 |
// ClauseProcessor repeatable clauses |
| 549 |
//===----------------------------------------------------------------------===// |
| 550 |
static llvm::StringMap<bool> getTargetFeatures(mlir::ModuleOp module) { |
| 551 |
llvm::StringMap<bool> featuresMap; |
| 552 |
llvm::SmallVector<llvm::StringRef> targetFeaturesVec; |
| 553 |
if (mlir::LLVM::TargetFeaturesAttr features = |
| 554 |
fir::getTargetFeatures(module)) { |
| 555 |
llvm::ArrayRef<mlir::StringAttr> featureAttrs = features.getFeatures(); |
| 556 |
for (auto &featureAttr : featureAttrs) { |
| 557 |
llvm::StringRef featureKeyString = featureAttr.strref(); |
| 558 |
featuresMap[featureKeyString.substr(1)] = (featureKeyString[0] == '+'); |
| 559 |
} |
| 560 |
} |
| 561 |
return featuresMap; |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
addAlignedClause(lower::AbstractConverter &converter, |
| 566 |
const omp::clause::Aligned &clause, |
| 567 |
llvm::SmallVectorImpl<mlir::Value> &alignedVars, |
| 568 |
llvm::SmallVectorImpl<mlir::Attribute> &alignmentAttrs) { |
| 569 |
using Aligned = omp::clause::Aligned; |
| 570 |
lower::StatementContext stmtCtx; |
| 571 |
mlir::IntegerAttr alignmentValueAttr; |
| 572 |
int64_t alignment = 0; |
| 573 |
fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 574 |
|
| 575 |
if (auto &alignmentValueParserExpr = |
| 576 |
std::get<std::optional<Aligned::Alignment>>(clause.t)) { |
| 577 |
mlir::Value operand = fir::getBase( |
| 578 |
converter.genExprValue(*alignmentValueParserExpr, stmtCtx)); |
| 579 |
alignment = *fir::getIntIfConstant(operand); |
| 580 |
} else { |
| 581 |
llvm::StringMap<bool> featuresMap = getTargetFeatures(builder.getModule()); |
| 582 |
llvm::Triple triple = fir::getTargetTriple(builder.getModule()); |
| 583 |
alignment = |
| 584 |
llvm::OpenMPIRBuilder::getOpenMPDefaultSimdAlign(triple, featuresMap); |
| 585 |
} |
| 586 |
|
| 587 |
// The default alignment for some targets is equal to 0. |
| 588 |
// Do not generate alignment assumption if alignment is less than or equal to |
| 589 |
// 0. |
| 590 |
if (alignment > 0) { |
| 591 |
auto &objects = std::get<omp::ObjectList>(clause.t); |
| 592 |
if (!objects.empty()) |
| 593 |
genObjectList(objects, converter, alignedVars); |
| 594 |
alignmentValueAttr = builder.getI64IntegerAttr(alignment); |
| 595 |
// All the list items in a aligned clause will have same alignment |
| 596 |
for (std::size_t i = 0; i < objects.size(); i++) |
| 597 |
alignmentAttrs.push_back(alignmentValueAttr); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
bool ClauseProcessor::processAligned( |
| 602 |
mlir::omp::AlignedClauseOps &result) const { |
| 603 |
return findRepeatableClause<omp::clause::Aligned>( |
| 604 |
[&](const omp::clause::Aligned &clause, const parser::CharBlock &) { |
| 605 |
addAlignedClause(converter, clause, result.alignedVars, |
| 606 |
result.alignmentAttrs); |
| 607 |
}); |
| 608 |
} |
| 609 |
|
| 610 |
bool ClauseProcessor::processAllocate( |
| 611 |
mlir::omp::AllocateClauseOps &result) const { |
| 612 |
return findRepeatableClause<omp::clause::Allocate>( |
| 613 |
[&](const omp::clause::Allocate &clause, const parser::CharBlock &) { |
| 614 |
genAllocateClause(converter, clause, result.allocatorVars, |
| 615 |
result.allocateVars); |
| 616 |
}); |
| 617 |
} |
| 618 |
|
| 619 |
bool ClauseProcessor::processCopyin() const { |
| 620 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 621 |
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint(); |
| 622 |
firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock()); |
| 623 |
auto checkAndCopyHostAssociateVar = |
| 624 |
[&](semantics::Symbol *sym, |
| 625 |
mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr) { |
| 626 |
assert(sym->has<semantics::HostAssocDetails>() && |
| 627 |
"No host-association found"); |
| 628 |
if (converter.isPresentShallowLookup(*sym)) |
| 629 |
converter.copyHostAssociateVar(*sym, copyAssignIP); |
| 630 |
}; |
| 631 |
bool hasCopyin = findRepeatableClause<omp::clause::Copyin>( |
| 632 |
[&](const omp::clause::Copyin &clause, const parser::CharBlock &) { |
| 633 |
for (const omp::Object &object : clause.v) { |
| 634 |
semantics::Symbol *sym = object.sym(); |
| 635 |
assert(sym && "Expecting symbol"); |
| 636 |
if (const auto *commonDetails = |
| 637 |
sym->detailsIf<semantics::CommonBlockDetails>()) { |
| 638 |
for (const auto &mem : commonDetails->objects()) |
| 639 |
checkAndCopyHostAssociateVar(&*mem, &insPt); |
| 640 |
break; |
| 641 |
} |
| 642 |
if (semantics::IsAllocatableOrObjectPointer(&sym->GetUltimate())) |
| 643 |
TODO(converter.getCurrentLocation(), |
| 644 |
"pointer or allocatable variables in Copyin clause"); |
| 645 |
assert(sym->has<semantics::HostAssocDetails>() && |
| 646 |
"No host-association found"); |
| 647 |
checkAndCopyHostAssociateVar(sym); |
| 648 |
} |
| 649 |
}); |
| 650 |
|
| 651 |
// [OMP 5.0, 2.19.6.1] The copy is done after the team is formed and prior to |
| 652 |
// the execution of the associated structured block. Emit implicit barrier to |
| 653 |
// synchronize threads and avoid data races on propagation master's thread |
| 654 |
// values of threadprivate variables to local instances of that variables of |
| 655 |
// all other implicit threads. |
| 656 |
|
| 657 |
// All copies are inserted at either "insPt" (i.e. immediately before it), |
| 658 |
// or at some earlier point (as determined by "copyHostAssociateVar"). |
| 659 |
// Unless the insertion point is given to "copyHostAssociateVar" explicitly, |
| 660 |
// it will not restore the builder's insertion point. Since the copies may be |
| 661 |
// inserted in any order (not following the execution order), make sure the |
| 662 |
// barrier is inserted following all of them. |
| 663 |
firOpBuilder.restoreInsertionPoint(insPt); |
| 664 |
if (hasCopyin) |
| 665 |
firOpBuilder.create<mlir::omp::BarrierOp>(converter.getCurrentLocation()); |
| 666 |
return hasCopyin; |
| 667 |
} |
| 668 |
|
| 669 |
/// Class that extracts information from the specified type. |
| 670 |
class TypeInfo { |
| 671 |
public: |
| 672 |
TypeInfo(mlir::Type ty) { typeScan(ty); } |
| 673 |
|
| 674 |
// Returns the length of character types. |
| 675 |
std::optional<fir::CharacterType::LenType> getCharLength() const { |
| 676 |
return charLen; |
| 677 |
} |
| 678 |
|
| 679 |
// Returns the shape of array types. |
| 680 |
llvm::ArrayRef<int64_t> getShape() const { return shape; } |
| 681 |
|
| 682 |
// Is the type inside a box? |
| 683 |
bool isBox() const { return inBox; } |
| 684 |
|
| 685 |
private: |
| 686 |
void typeScan(mlir::Type type); |
| 687 |
|
| 688 |
std::optional<fir::CharacterType::LenType> charLen; |
| 689 |
llvm::SmallVector<int64_t> shape; |
| 690 |
bool inBox = false; |
| 691 |
}; |
| 692 |
|
| 693 |
void TypeInfo::typeScan(mlir::Type ty) { |
| 694 |
if (auto sty = mlir::dyn_cast<fir::SequenceType>(ty)) { |
| 695 |
assert(shape.empty() && !sty.getShape().empty()); |
| 696 |
shape = llvm::SmallVector<int64_t>(sty.getShape()); |
| 697 |
typeScan(sty.getEleTy()); |
| 698 |
} else if (auto bty = mlir::dyn_cast<fir::BoxType>(ty)) { |
| 699 |
inBox = true; |
| 700 |
typeScan(bty.getEleTy()); |
| 701 |
} else if (auto cty = mlir::dyn_cast<fir::CharacterType>(ty)) { |
| 702 |
charLen = cty.getLen(); |
| 703 |
} else if (auto hty = mlir::dyn_cast<fir::HeapType>(ty)) { |
| 704 |
typeScan(hty.getEleTy()); |
| 705 |
} else if (auto pty = mlir::dyn_cast<fir::PointerType>(ty)) { |
| 706 |
typeScan(pty.getEleTy()); |
| 707 |
} else { |
| 708 |
// The scan ends when reaching any built-in or record type. |
| 709 |
assert(ty.isIntOrIndexOrFloat() || mlir::isa<fir::ComplexType>(ty) || |
| 710 |
mlir::isa<fir::LogicalType>(ty) || mlir::isa<fir::RecordType>(ty)); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
// Create a function that performs a copy between two variables, compatible |
| 715 |
// with their types and attributes. |
| 716 |
static mlir::func::FuncOp |
| 717 |
createCopyFunc(mlir::Location loc, lower::AbstractConverter &converter, |
| 718 |
mlir::Type varType, fir::FortranVariableFlagsEnum varAttrs) { |
| 719 |
fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 720 |
mlir::ModuleOp module = builder.getModule(); |
| 721 |
mlir::Type eleTy = mlir::cast<fir::ReferenceType>(varType).getEleTy(); |
| 722 |
TypeInfo typeInfo(eleTy); |
| 723 |
std::string copyFuncName = |
| 724 |
fir::getTypeAsString(eleTy, builder.getKindMap(), "_copy"); |
| 725 |
|
| 726 |
if (auto decl = module.lookupSymbol<mlir::func::FuncOp>(copyFuncName)) |
| 727 |
return decl; |
| 728 |
|
| 729 |
// create function |
| 730 |
mlir::OpBuilder::InsertionGuard guard(builder); |
| 731 |
mlir::OpBuilder modBuilder(module.getBodyRegion()); |
| 732 |
llvm::SmallVector<mlir::Type> argsTy = {varType, varType}; |
| 733 |
auto funcType = mlir::FunctionType::get(builder.getContext(), argsTy, {}); |
| 734 |
mlir::func::FuncOp funcOp = |
| 735 |
modBuilder.create<mlir::func::FuncOp>(loc, copyFuncName, funcType); |
| 736 |
funcOp.setVisibility(mlir::SymbolTable::Visibility::Private); |
| 737 |
builder.createBlock(&funcOp.getRegion(), funcOp.getRegion().end(), argsTy, |
| 738 |
{loc, loc}); |
| 739 |
builder.setInsertionPointToStart(&funcOp.getRegion().back()); |
| 740 |
// generate body |
| 741 |
fir::FortranVariableFlagsAttr attrs; |
| 742 |
if (varAttrs != fir::FortranVariableFlagsEnum::None) |
| 743 |
attrs = fir::FortranVariableFlagsAttr::get(builder.getContext(), varAttrs); |
| 744 |
llvm::SmallVector<mlir::Value> typeparams; |
| 745 |
if (typeInfo.getCharLength().has_value()) { |
| 746 |
mlir::Value charLen = builder.createIntegerConstant( |
| 747 |
loc, builder.getCharacterLengthType(), *typeInfo.getCharLength()); |
| 748 |
typeparams.push_back(charLen); |
| 749 |
} |
| 750 |
mlir::Value shape; |
| 751 |
if (!typeInfo.isBox() && !typeInfo.getShape().empty()) { |
| 752 |
llvm::SmallVector<mlir::Value> extents; |
| 753 |
for (auto extent : typeInfo.getShape()) |
| 754 |
extents.push_back( |
| 755 |
builder.createIntegerConstant(loc, builder.getIndexType(), extent)); |
| 756 |
shape = builder.create<fir::ShapeOp>(loc, extents); |
| 757 |
} |
| 758 |
auto declDst = builder.create<hlfir::DeclareOp>( |
| 759 |
loc, funcOp.getArgument(0), copyFuncName + "_dst", shape, typeparams, |
| 760 |
/*dummy_scope=*/nullptr, attrs); |
| 761 |
auto declSrc = builder.create<hlfir::DeclareOp>( |
| 762 |
loc, funcOp.getArgument(1), copyFuncName + "_src", shape, typeparams, |
| 763 |
/*dummy_scope=*/nullptr, attrs); |
| 764 |
converter.copyVar(loc, declDst.getBase(), declSrc.getBase(), varAttrs); |
| 765 |
builder.create<mlir::func::ReturnOp>(loc); |
| 766 |
return funcOp; |
| 767 |
} |
| 768 |
|
| 769 |
bool ClauseProcessor::processCopyprivate( |
| 770 |
mlir::Location currentLocation, |
| 771 |
mlir::omp::CopyprivateClauseOps &result) const { |
| 772 |
auto addCopyPrivateVar = [&](semantics::Symbol *sym) { |
| 773 |
mlir::Value symVal = converter.getSymbolAddress(*sym); |
| 774 |
auto declOp = symVal.getDefiningOp<hlfir::DeclareOp>(); |
| 775 |
if (!declOp) |
| 776 |
fir::emitFatalError(currentLocation, |
| 777 |
"COPYPRIVATE is supported only in HLFIR mode"); |
| 778 |
symVal = declOp.getBase(); |
| 779 |
mlir::Type symType = symVal.getType(); |
| 780 |
fir::FortranVariableFlagsEnum attrs = |
| 781 |
declOp.getFortranAttrs().has_value() |
| 782 |
? *declOp.getFortranAttrs() |
| 783 |
: fir::FortranVariableFlagsEnum::None; |
| 784 |
mlir::Value cpVar = symVal; |
| 785 |
|
| 786 |
// CopyPrivate variables must be passed by reference. However, in the case |
| 787 |
// of assumed shapes/vla the type is not a !fir.ref, but a !fir.box. |
| 788 |
// In these cases to retrieve the appropriate !fir.ref<!fir.box<...>> to |
| 789 |
// access the data we need we must perform an alloca and then store to it |
| 790 |
// and retrieve the data from the new alloca. |
| 791 |
if (mlir::isa<fir::BaseBoxType>(symType)) { |
| 792 |
fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 793 |
auto alloca = builder.create<fir::AllocaOp>(currentLocation, symType); |
| 794 |
builder.create<fir::StoreOp>(currentLocation, symVal, alloca); |
| 795 |
cpVar = alloca; |
| 796 |
} |
| 797 |
|
| 798 |
result.copyprivateVars.push_back(cpVar); |
| 799 |
mlir::func::FuncOp funcOp = |
| 800 |
createCopyFunc(currentLocation, converter, cpVar.getType(), attrs); |
| 801 |
result.copyprivateFuncs.push_back(mlir::SymbolRefAttr::get(funcOp)); |
| 802 |
}; |
| 803 |
|
| 804 |
bool hasCopyPrivate = findRepeatableClause<clause::Copyprivate>( |
| 805 |
[&](const clause::Copyprivate &clause, const parser::CharBlock &) { |
| 806 |
for (const Object &object : clause.v) { |
| 807 |
semantics::Symbol *sym = object.sym(); |
| 808 |
if (const auto *commonDetails = |
| 809 |
sym->detailsIf<semantics::CommonBlockDetails>()) { |
| 810 |
for (const auto &mem : commonDetails->objects()) |
| 811 |
addCopyPrivateVar(&*mem); |
| 812 |
break; |
| 813 |
} |
| 814 |
addCopyPrivateVar(sym); |
| 815 |
} |
| 816 |
}); |
| 817 |
|
| 818 |
return hasCopyPrivate; |
| 819 |
} |
| 820 |
|
| 821 |
bool ClauseProcessor::processDepend(mlir::omp::DependClauseOps &result) const { |
| 822 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 823 |
|
| 824 |
return findRepeatableClause<omp::clause::Depend>( |
| 825 |
[&](const omp::clause::Depend &clause, const parser::CharBlock &) { |
| 826 |
using Depend = omp::clause::Depend; |
| 827 |
assert(std::holds_alternative<Depend::WithLocators>(clause.u) && |
| 828 |
"Only the modern form is handled at the moment"); |
| 829 |
auto &modern = std::get<Depend::WithLocators>(clause.u); |
| 830 |
auto kind = std::get<Depend::TaskDependenceType>(modern.t); |
| 831 |
auto &objects = std::get<omp::ObjectList>(modern.t); |
| 832 |
|
| 833 |
mlir::omp::ClauseTaskDependAttr dependTypeOperand = |
| 834 |
genDependKindAttr(firOpBuilder, kind); |
| 835 |
result.dependTypeAttrs.append(objects.size(), dependTypeOperand); |
| 836 |
|
| 837 |
for (const omp::Object &object : objects) { |
| 838 |
assert(object.ref() && "Expecting designator"); |
| 839 |
|
| 840 |
if (evaluate::ExtractSubstring(*object.ref())) { |
| 841 |
TODO(converter.getCurrentLocation(), |
| 842 |
"substring not supported for task depend"); |
| 843 |
} else if (evaluate::IsArrayElement(*object.ref())) { |
| 844 |
TODO(converter.getCurrentLocation(), |
| 845 |
"array sections not supported for task depend"); |
| 846 |
} |
| 847 |
|
| 848 |
semantics::Symbol *sym = object.sym(); |
| 849 |
const mlir::Value variable = converter.getSymbolAddress(*sym); |
| 850 |
result.dependVars.push_back(variable); |
| 851 |
} |
| 852 |
}); |
| 853 |
} |
| 854 |
|
| 855 |
bool ClauseProcessor::processHasDeviceAddr( |
| 856 |
mlir::omp::HasDeviceAddrClauseOps &result, |
| 857 |
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes, |
| 858 |
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs, |
| 859 |
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSymbols) const { |
| 860 |
return findRepeatableClause<omp::clause::HasDeviceAddr>( |
| 861 |
[&](const omp::clause::HasDeviceAddr &devAddrClause, |
| 862 |
const parser::CharBlock &) { |
| 863 |
addUseDeviceClause(converter, devAddrClause.v, result.hasDeviceAddrVars, |
| 864 |
isDeviceTypes, isDeviceLocs, isDeviceSymbols); |
| 865 |
}); |
| 866 |
} |
| 867 |
|
| 868 |
bool ClauseProcessor::processIf( |
| 869 |
omp::clause::If::DirectiveNameModifier directiveName, |
| 870 |
mlir::omp::IfClauseOps &result) const { |
| 871 |
bool found = false; |
| 872 |
findRepeatableClause<omp::clause::If>([&](const omp::clause::If &clause, |
| 873 |
const parser::CharBlock &source) { |
| 874 |
mlir::Location clauseLocation = converter.genLocation(source); |
| 875 |
mlir::Value operand = |
| 876 |
getIfClauseOperand(converter, clause, directiveName, clauseLocation); |
| 877 |
// Assume that, at most, a single 'if' clause will be applicable to the |
| 878 |
// given directive. |
| 879 |
if (operand) { |
| 880 |
result.ifVar = operand; |
| 881 |
found = true; |
| 882 |
} |
| 883 |
}); |
| 884 |
return found; |
| 885 |
} |
| 886 |
|
| 887 |
bool ClauseProcessor::processIsDevicePtr( |
| 888 |
mlir::omp::IsDevicePtrClauseOps &result, |
| 889 |
llvm::SmallVectorImpl<mlir::Type> &isDeviceTypes, |
| 890 |
llvm::SmallVectorImpl<mlir::Location> &isDeviceLocs, |
| 891 |
llvm::SmallVectorImpl<const semantics::Symbol *> &isDeviceSymbols) const { |
| 892 |
return findRepeatableClause<omp::clause::IsDevicePtr>( |
| 893 |
[&](const omp::clause::IsDevicePtr &devPtrClause, |
| 894 |
const parser::CharBlock &) { |
| 895 |
addUseDeviceClause(converter, devPtrClause.v, result.isDevicePtrVars, |
| 896 |
isDeviceTypes, isDeviceLocs, isDeviceSymbols); |
| 897 |
}); |
| 898 |
} |
| 899 |
|
| 900 |
bool ClauseProcessor::processLink( |
| 901 |
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const { |
| 902 |
return findRepeatableClause<omp::clause::Link>( |
| 903 |
[&](const omp::clause::Link &clause, const parser::CharBlock &) { |
| 904 |
// Case: declare target link(var1, var2)... |
| 905 |
gatherFuncAndVarSyms( |
| 906 |
clause.v, mlir::omp::DeclareTargetCaptureClause::link, result); |
| 907 |
}); |
| 908 |
} |
| 909 |
|
| 910 |
bool ClauseProcessor::processMap( |
| 911 |
mlir::Location currentLocation, lower::StatementContext &stmtCtx, |
| 912 |
mlir::omp::MapClauseOps &result, |
| 913 |
llvm::SmallVectorImpl<const semantics::Symbol *> *mapSyms, |
| 914 |
llvm::SmallVectorImpl<mlir::Location> *mapSymLocs, |
| 915 |
llvm::SmallVectorImpl<mlir::Type> *mapSymTypes) const { |
| 916 |
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder(); |
| 917 |
// We always require tracking of symbols, even if the caller does not, |
| 918 |
// so we create an optionally used local set of symbols when the mapSyms |
| 919 |
// argument is not present. |
| 920 |
llvm::SmallVector<const semantics::Symbol *> localMapSyms; |
| 921 |
llvm::SmallVectorImpl<const semantics::Symbol *> *ptrMapSyms = |
| 922 |
mapSyms ? mapSyms : &localMapSyms; |
| 923 |
std::map<const semantics::Symbol *, |
| 924 |
llvm::SmallVector<OmpMapMemberIndicesData>> |
| 925 |
parentMemberIndices; |
| 926 |
|
| 927 |
bool clauseFound = findRepeatableClause<omp::clause::Map>( |
| 928 |
[&](const omp::clause::Map &clause, const parser::CharBlock &source) { |
| 929 |
using Map = omp::clause::Map; |
| 930 |
mlir::Location clauseLocation = converter.genLocation(source); |
| 931 |
const auto &mapType = std::get<std::optional<Map::MapType>>(clause.t); |
| 932 |
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits = |
| 933 |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE; |
| 934 |
// If the map type is specified, then process it else Tofrom is the |
| 935 |
// default. |
| 936 |
if (mapType) { |
| 937 |
switch (*mapType) { |
| 938 |
case Map::MapType::To: |
| 939 |
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO; |
| 940 |
break; |
| 941 |
case Map::MapType::From: |
| 942 |
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_FROM; |
| 943 |
break; |
| 944 |
case Map::MapType::Tofrom: |
| 945 |
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO | |
| 946 |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_FROM; |
| 947 |
break; |
| 948 |
case Map::MapType::Alloc: |
| 949 |
case Map::MapType::Release: |
| 950 |
// alloc and release is the default map_type for the Target Data |
| 951 |
// Ops, i.e. if no bits for map_type is supplied then alloc/release |
| 952 |
// is implicitly assumed based on the target directive. Default |
| 953 |
// value for Target Data and Enter Data is alloc and for Exit Data |
| 954 |
// it is release. |
| 955 |
break; |
| 956 |
case Map::MapType::Delete: |
| 957 |
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE; |
| 958 |
} |
| 959 |
|
| 960 |
auto &modTypeMods = |
| 961 |
std::get<std::optional<Map::MapTypeModifiers>>(clause.t); |
| 962 |
if (modTypeMods) { |
| 963 |
if (llvm::is_contained(*modTypeMods, Map::MapTypeModifier::Always)) |
| 964 |
mapTypeBits |= |
| 965 |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_ALWAYS; |
| 966 |
} |
| 967 |
} else { |
| 968 |
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO | |
| 969 |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_FROM; |
| 970 |
} |
| 971 |
|
| 972 |
for (const omp::Object &object : std::get<omp::ObjectList>(clause.t)) { |
| 973 |
llvm::SmallVector<mlir::Value> bounds; |
| 974 |
std::stringstream asFortran; |
| 975 |
|
| 976 |
lower::AddrAndBoundsInfo info = |
| 977 |
lower::gatherDataOperandAddrAndBounds<mlir::omp::MapBoundsOp, |
| 978 |
mlir::omp::MapBoundsType>( |
| 979 |
converter, firOpBuilder, semaCtx, stmtCtx, *object.sym(), |
| 980 |
object.ref(), clauseLocation, asFortran, bounds, |
| 981 |
treatIndexAsSection); |
| 982 |
|
| 983 |
auto origSymbol = converter.getSymbolAddress(*object.sym()); |
| 984 |
mlir::Value symAddr = info.addr; |
| 985 |
if (origSymbol && fir::isTypeWithDescriptor(origSymbol.getType())) |
| 986 |
symAddr = origSymbol; |
| 987 |
|
| 988 |
// Explicit map captures are captured ByRef by default, |
| 989 |
// optimisation passes may alter this to ByCopy or other capture |
| 990 |
// types to optimise |
| 991 |
auto location = mlir::NameLoc::get( |
| 992 |
mlir::StringAttr::get(firOpBuilder.getContext(), asFortran.str()), |
| 993 |
symAddr.getLoc()); |
| 994 |
mlir::omp::MapInfoOp mapOp = createMapInfoOp( |
| 995 |
firOpBuilder, location, symAddr, |
| 996 |
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds, |
| 997 |
/*members=*/{}, /*membersIndex=*/mlir::DenseIntElementsAttr{}, |
| 998 |
static_cast< |
| 999 |
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>( |
| 1000 |
mapTypeBits), |
| 1001 |
mlir::omp::VariableCaptureKind::ByRef, symAddr.getType()); |
| 1002 |
|
| 1003 |
if (object.sym()->owner().IsDerivedType()) { |
| 1004 |
addChildIndexAndMapToParent(object, parentMemberIndices, mapOp, |
| 1005 |
semaCtx); |
| 1006 |
} else { |
| 1007 |
result.mapVars.push_back(mapOp); |
| 1008 |
ptrMapSyms->push_back(object.sym()); |
| 1009 |
if (mapSymTypes) |
| 1010 |
mapSymTypes->push_back(symAddr.getType()); |
| 1011 |
if (mapSymLocs) |
| 1012 |
mapSymLocs->push_back(symAddr.getLoc()); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
}); |
| 1016 |
|
| 1017 |
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars, |
| 1018 |
*ptrMapSyms, mapSymTypes, mapSymLocs); |
| 1019 |
|
| 1020 |
return clauseFound; |
| 1021 |
} |
| 1022 |
|
| 1023 |
bool ClauseProcessor::processReduction( |
| 1024 |
mlir::Location currentLocation, mlir::omp::ReductionClauseOps &result, |
| 1025 |
llvm::SmallVectorImpl<mlir::Type> *outReductionTypes, |
| 1026 |
llvm::SmallVectorImpl<const semantics::Symbol *> *outReductionSyms) const { |
| 1027 |
return findRepeatableClause<omp::clause::Reduction>( |
| 1028 |
[&](const omp::clause::Reduction &clause, const parser::CharBlock &) { |
| 1029 |
llvm::SmallVector<mlir::Value> reductionVars; |
| 1030 |
llvm::SmallVector<bool> reduceVarByRef; |
| 1031 |
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols; |
| 1032 |
llvm::SmallVector<const semantics::Symbol *> reductionSyms; |
| 1033 |
ReductionProcessor rp; |
| 1034 |
rp.addDeclareReduction( |
| 1035 |
currentLocation, converter, clause, reductionVars, reduceVarByRef, |
| 1036 |
reductionDeclSymbols, outReductionSyms ? &reductionSyms : nullptr); |
| 1037 |
|
| 1038 |
// Copy local lists into the output. |
| 1039 |
llvm::copy(reductionVars, std::back_inserter(result.reductionVars)); |
| 1040 |
llvm::copy(reduceVarByRef, |
| 1041 |
std::back_inserter(result.reductionVarsByRef)); |
| 1042 |
llvm::copy(reductionDeclSymbols, |
| 1043 |
std::back_inserter(result.reductionDeclSymbols)); |
| 1044 |
|
| 1045 |
if (outReductionTypes) { |
| 1046 |
outReductionTypes->reserve(outReductionTypes->size() + |
| 1047 |
reductionVars.size()); |
| 1048 |
llvm::transform(reductionVars, std::back_inserter(*outReductionTypes), |
| 1049 |
[](mlir::Value v) { return v.getType(); }); |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (outReductionSyms) |
| 1053 |
llvm::copy(reductionSyms, std::back_inserter(*outReductionSyms)); |
| 1054 |
}); |
| 1055 |
} |
| 1056 |
|
| 1057 |
bool ClauseProcessor::processTo( |
| 1058 |
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const { |
| 1059 |
return findRepeatableClause<omp::clause::To>( |
| 1060 |
[&](const omp::clause::To &clause, const parser::CharBlock &) { |
| 1061 |
// Case: declare target to(func, var1, var2)... |
| 1062 |
gatherFuncAndVarSyms(std::get<ObjectList>(clause.t), |
| 1063 |
mlir::omp::DeclareTargetCaptureClause::to, result); |
| 1064 |
}); |
| 1065 |
} |
| 1066 |
|
| 1067 |
bool ClauseProcessor::processEnter( |
| 1068 |
llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const { |
| 1069 |
return findRepeatableClause<omp::clause::Enter>( |
| 1070 |
[&](const omp::clause::Enter &clause, const parser::CharBlock &) { |
| 1071 |
// Case: declare target enter(func, var1, var2)... |
| 1072 |
gatherFuncAndVarSyms( |
| 1073 |
clause.v, mlir::omp::DeclareTargetCaptureClause::enter, result); |
| 1074 |
}); |
| 1075 |
} |
| 1076 |
|
| 1077 |
bool ClauseProcessor::processUseDeviceAddr( |
| 1078 |
mlir::omp::UseDeviceAddrClauseOps &result, |
| 1079 |
llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes, |
| 1080 |
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs, |
| 1081 |
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const { |
| 1082 |
return findRepeatableClause<omp::clause::UseDeviceAddr>( |
| 1083 |
[&](const omp::clause::UseDeviceAddr &clause, const parser::CharBlock &) { |
| 1084 |
addUseDeviceClause(converter, clause.v, result.useDeviceAddrVars, |
| 1085 |
useDeviceTypes, useDeviceLocs, useDeviceSyms); |
| 1086 |
}); |
| 1087 |
} |
| 1088 |
|
| 1089 |
bool ClauseProcessor::processUseDevicePtr( |
| 1090 |
mlir::omp::UseDevicePtrClauseOps &result, |
| 1091 |
llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes, |
| 1092 |
llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs, |
| 1093 |
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const { |
| 1094 |
return findRepeatableClause<omp::clause::UseDevicePtr>( |
| 1095 |
[&](const omp::clause::UseDevicePtr &clause, const parser::CharBlock &) { |
| 1096 |
addUseDeviceClause(converter, clause.v, result.useDevicePtrVars, |
| 1097 |
useDeviceTypes, useDeviceLocs, useDeviceSyms); |
| 1098 |
}); |
| 1099 |
} |
| 1100 |
|
| 1101 |
} // namespace omp |
| 1102 |
} // namespace lower |
| 1103 |
} // namespace Fortran |