1 //===--- OpenMPClause.cpp - Classes for OpenMP clauses --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the subclesses of Stmt class declared in OpenMPClause.h 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/OpenMPClause.h" 15 16 #include "clang/AST/ASTContext.h" 17 18 using namespace clang; 19 20 OMPClause::child_range OMPClause::children() { 21 switch (getClauseKind()) { 22 default: 23 break; 24 #define OPENMP_CLAUSE(Name, Class) \ 25 case OMPC_##Name: \ 26 return static_cast<Class *>(this)->children(); 27 #include "clang/Basic/OpenMPKinds.def" 28 } 29 llvm_unreachable("unknown OMPClause"); 30 } 31 32 OMPClauseWithPreInit *OMPClauseWithPreInit::get(OMPClause *C) { 33 auto *Res = OMPClauseWithPreInit::get(const_cast<const OMPClause *>(C)); 34 return Res ? const_cast<OMPClauseWithPreInit *>(Res) : nullptr; 35 } 36 37 const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) { 38 switch (C->getClauseKind()) { 39 case OMPC_schedule: 40 return static_cast<const OMPScheduleClause *>(C); 41 case OMPC_dist_schedule: 42 return static_cast<const OMPDistScheduleClause *>(C); 43 case OMPC_firstprivate: 44 return static_cast<const OMPFirstprivateClause *>(C); 45 case OMPC_lastprivate: 46 return static_cast<const OMPLastprivateClause *>(C); 47 case OMPC_reduction: 48 return static_cast<const OMPReductionClause *>(C); 49 case OMPC_task_reduction: 50 return static_cast<const OMPTaskReductionClause *>(C); 51 case OMPC_linear: 52 return static_cast<const OMPLinearClause *>(C); 53 case OMPC_if: 54 return static_cast<const OMPIfClause *>(C); 55 case OMPC_num_threads: 56 return static_cast<const OMPNumThreadsClause *>(C); 57 case OMPC_num_teams: 58 return static_cast<const OMPNumTeamsClause *>(C); 59 case OMPC_thread_limit: 60 return static_cast<const OMPThreadLimitClause *>(C); 61 case OMPC_default: 62 case OMPC_proc_bind: 63 case OMPC_final: 64 case OMPC_safelen: 65 case OMPC_simdlen: 66 case OMPC_collapse: 67 case OMPC_private: 68 case OMPC_shared: 69 case OMPC_aligned: 70 case OMPC_copyin: 71 case OMPC_copyprivate: 72 case OMPC_ordered: 73 case OMPC_nowait: 74 case OMPC_untied: 75 case OMPC_mergeable: 76 case OMPC_threadprivate: 77 case OMPC_flush: 78 case OMPC_read: 79 case OMPC_write: 80 case OMPC_update: 81 case OMPC_capture: 82 case OMPC_seq_cst: 83 case OMPC_depend: 84 case OMPC_device: 85 case OMPC_threads: 86 case OMPC_simd: 87 case OMPC_map: 88 case OMPC_priority: 89 case OMPC_grainsize: 90 case OMPC_nogroup: 91 case OMPC_num_tasks: 92 case OMPC_hint: 93 case OMPC_defaultmap: 94 case OMPC_unknown: 95 case OMPC_uniform: 96 case OMPC_to: 97 case OMPC_from: 98 case OMPC_use_device_ptr: 99 case OMPC_is_device_ptr: 100 break; 101 } 102 103 return nullptr; 104 } 105 106 OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(OMPClause *C) { 107 auto *Res = OMPClauseWithPostUpdate::get(const_cast<const OMPClause *>(C)); 108 return Res ? const_cast<OMPClauseWithPostUpdate *>(Res) : nullptr; 109 } 110 111 const OMPClauseWithPostUpdate *OMPClauseWithPostUpdate::get(const OMPClause *C) { 112 switch (C->getClauseKind()) { 113 case OMPC_lastprivate: 114 return static_cast<const OMPLastprivateClause *>(C); 115 case OMPC_reduction: 116 return static_cast<const OMPReductionClause *>(C); 117 case OMPC_task_reduction: 118 return static_cast<const OMPTaskReductionClause *>(C); 119 case OMPC_linear: 120 return static_cast<const OMPLinearClause *>(C); 121 case OMPC_schedule: 122 case OMPC_dist_schedule: 123 case OMPC_firstprivate: 124 case OMPC_default: 125 case OMPC_proc_bind: 126 case OMPC_if: 127 case OMPC_final: 128 case OMPC_num_threads: 129 case OMPC_safelen: 130 case OMPC_simdlen: 131 case OMPC_collapse: 132 case OMPC_private: 133 case OMPC_shared: 134 case OMPC_aligned: 135 case OMPC_copyin: 136 case OMPC_copyprivate: 137 case OMPC_ordered: 138 case OMPC_nowait: 139 case OMPC_untied: 140 case OMPC_mergeable: 141 case OMPC_threadprivate: 142 case OMPC_flush: 143 case OMPC_read: 144 case OMPC_write: 145 case OMPC_update: 146 case OMPC_capture: 147 case OMPC_seq_cst: 148 case OMPC_depend: 149 case OMPC_device: 150 case OMPC_threads: 151 case OMPC_simd: 152 case OMPC_map: 153 case OMPC_num_teams: 154 case OMPC_thread_limit: 155 case OMPC_priority: 156 case OMPC_grainsize: 157 case OMPC_nogroup: 158 case OMPC_num_tasks: 159 case OMPC_hint: 160 case OMPC_defaultmap: 161 case OMPC_unknown: 162 case OMPC_uniform: 163 case OMPC_to: 164 case OMPC_from: 165 case OMPC_use_device_ptr: 166 case OMPC_is_device_ptr: 167 break; 168 } 169 170 return nullptr; 171 } 172 173 void OMPPrivateClause::setPrivateCopies(ArrayRef<Expr *> VL) { 174 assert(VL.size() == varlist_size() && 175 "Number of private copies is not the same as the preallocated buffer"); 176 std::copy(VL.begin(), VL.end(), varlist_end()); 177 } 178 179 OMPPrivateClause * 180 OMPPrivateClause::Create(const ASTContext &C, SourceLocation StartLoc, 181 SourceLocation LParenLoc, SourceLocation EndLoc, 182 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL) { 183 // Allocate space for private variables and initializer expressions. 184 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * VL.size())); 185 OMPPrivateClause *Clause = 186 new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 187 Clause->setVarRefs(VL); 188 Clause->setPrivateCopies(PrivateVL); 189 return Clause; 190 } 191 192 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C, 193 unsigned N) { 194 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(2 * N)); 195 return new (Mem) OMPPrivateClause(N); 196 } 197 198 void OMPFirstprivateClause::setPrivateCopies(ArrayRef<Expr *> VL) { 199 assert(VL.size() == varlist_size() && 200 "Number of private copies is not the same as the preallocated buffer"); 201 std::copy(VL.begin(), VL.end(), varlist_end()); 202 } 203 204 void OMPFirstprivateClause::setInits(ArrayRef<Expr *> VL) { 205 assert(VL.size() == varlist_size() && 206 "Number of inits is not the same as the preallocated buffer"); 207 std::copy(VL.begin(), VL.end(), getPrivateCopies().end()); 208 } 209 210 OMPFirstprivateClause * 211 OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation StartLoc, 212 SourceLocation LParenLoc, SourceLocation EndLoc, 213 ArrayRef<Expr *> VL, ArrayRef<Expr *> PrivateVL, 214 ArrayRef<Expr *> InitVL, Stmt *PreInit) { 215 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * VL.size())); 216 OMPFirstprivateClause *Clause = 217 new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 218 Clause->setVarRefs(VL); 219 Clause->setPrivateCopies(PrivateVL); 220 Clause->setInits(InitVL); 221 Clause->setPreInitStmt(PreInit); 222 return Clause; 223 } 224 225 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C, 226 unsigned N) { 227 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(3 * N)); 228 return new (Mem) OMPFirstprivateClause(N); 229 } 230 231 void OMPLastprivateClause::setPrivateCopies(ArrayRef<Expr *> PrivateCopies) { 232 assert(PrivateCopies.size() == varlist_size() && 233 "Number of private copies is not the same as the preallocated buffer"); 234 std::copy(PrivateCopies.begin(), PrivateCopies.end(), varlist_end()); 235 } 236 237 void OMPLastprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 238 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 239 "not the same as the " 240 "preallocated buffer"); 241 std::copy(SrcExprs.begin(), SrcExprs.end(), getPrivateCopies().end()); 242 } 243 244 void OMPLastprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 245 assert(DstExprs.size() == varlist_size() && "Number of destination " 246 "expressions is not the same as " 247 "the preallocated buffer"); 248 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 249 } 250 251 void OMPLastprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 252 assert(AssignmentOps.size() == varlist_size() && 253 "Number of assignment expressions is not the same as the preallocated " 254 "buffer"); 255 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 256 getDestinationExprs().end()); 257 } 258 259 OMPLastprivateClause *OMPLastprivateClause::Create( 260 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 261 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 262 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps, Stmt *PreInit, 263 Expr *PostUpdate) { 264 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size())); 265 OMPLastprivateClause *Clause = 266 new (Mem) OMPLastprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 267 Clause->setVarRefs(VL); 268 Clause->setSourceExprs(SrcExprs); 269 Clause->setDestinationExprs(DstExprs); 270 Clause->setAssignmentOps(AssignmentOps); 271 Clause->setPreInitStmt(PreInit); 272 Clause->setPostUpdateExpr(PostUpdate); 273 return Clause; 274 } 275 276 OMPLastprivateClause *OMPLastprivateClause::CreateEmpty(const ASTContext &C, 277 unsigned N) { 278 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N)); 279 return new (Mem) OMPLastprivateClause(N); 280 } 281 282 OMPSharedClause *OMPSharedClause::Create(const ASTContext &C, 283 SourceLocation StartLoc, 284 SourceLocation LParenLoc, 285 SourceLocation EndLoc, 286 ArrayRef<Expr *> VL) { 287 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size())); 288 OMPSharedClause *Clause = 289 new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size()); 290 Clause->setVarRefs(VL); 291 return Clause; 292 } 293 294 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) { 295 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 296 return new (Mem) OMPSharedClause(N); 297 } 298 299 void OMPLinearClause::setPrivates(ArrayRef<Expr *> PL) { 300 assert(PL.size() == varlist_size() && 301 "Number of privates is not the same as the preallocated buffer"); 302 std::copy(PL.begin(), PL.end(), varlist_end()); 303 } 304 305 void OMPLinearClause::setInits(ArrayRef<Expr *> IL) { 306 assert(IL.size() == varlist_size() && 307 "Number of inits is not the same as the preallocated buffer"); 308 std::copy(IL.begin(), IL.end(), getPrivates().end()); 309 } 310 311 void OMPLinearClause::setUpdates(ArrayRef<Expr *> UL) { 312 assert(UL.size() == varlist_size() && 313 "Number of updates is not the same as the preallocated buffer"); 314 std::copy(UL.begin(), UL.end(), getInits().end()); 315 } 316 317 void OMPLinearClause::setFinals(ArrayRef<Expr *> FL) { 318 assert(FL.size() == varlist_size() && 319 "Number of final updates is not the same as the preallocated buffer"); 320 std::copy(FL.begin(), FL.end(), getUpdates().end()); 321 } 322 323 OMPLinearClause *OMPLinearClause::Create( 324 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 325 OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, 326 SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef<Expr *> VL, 327 ArrayRef<Expr *> PL, ArrayRef<Expr *> IL, Expr *Step, Expr *CalcStep, 328 Stmt *PreInit, Expr *PostUpdate) { 329 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions 330 // (Step and CalcStep). 331 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size() + 2)); 332 OMPLinearClause *Clause = new (Mem) OMPLinearClause( 333 StartLoc, LParenLoc, Modifier, ModifierLoc, ColonLoc, EndLoc, VL.size()); 334 Clause->setVarRefs(VL); 335 Clause->setPrivates(PL); 336 Clause->setInits(IL); 337 // Fill update and final expressions with zeroes, they are provided later, 338 // after the directive construction. 339 std::fill(Clause->getInits().end(), Clause->getInits().end() + VL.size(), 340 nullptr); 341 std::fill(Clause->getUpdates().end(), Clause->getUpdates().end() + VL.size(), 342 nullptr); 343 Clause->setStep(Step); 344 Clause->setCalcStep(CalcStep); 345 Clause->setPreInitStmt(PreInit); 346 Clause->setPostUpdateExpr(PostUpdate); 347 return Clause; 348 } 349 350 OMPLinearClause *OMPLinearClause::CreateEmpty(const ASTContext &C, 351 unsigned NumVars) { 352 // Allocate space for 4 lists (Vars, Inits, Updates, Finals) and 2 expressions 353 // (Step and CalcStep). 354 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * NumVars + 2)); 355 return new (Mem) OMPLinearClause(NumVars); 356 } 357 358 OMPAlignedClause * 359 OMPAlignedClause::Create(const ASTContext &C, SourceLocation StartLoc, 360 SourceLocation LParenLoc, SourceLocation ColonLoc, 361 SourceLocation EndLoc, ArrayRef<Expr *> VL, Expr *A) { 362 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1)); 363 OMPAlignedClause *Clause = new (Mem) 364 OMPAlignedClause(StartLoc, LParenLoc, ColonLoc, EndLoc, VL.size()); 365 Clause->setVarRefs(VL); 366 Clause->setAlignment(A); 367 return Clause; 368 } 369 370 OMPAlignedClause *OMPAlignedClause::CreateEmpty(const ASTContext &C, 371 unsigned NumVars) { 372 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumVars + 1)); 373 return new (Mem) OMPAlignedClause(NumVars); 374 } 375 376 void OMPCopyinClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 377 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 378 "not the same as the " 379 "preallocated buffer"); 380 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end()); 381 } 382 383 void OMPCopyinClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 384 assert(DstExprs.size() == varlist_size() && "Number of destination " 385 "expressions is not the same as " 386 "the preallocated buffer"); 387 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 388 } 389 390 void OMPCopyinClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 391 assert(AssignmentOps.size() == varlist_size() && 392 "Number of assignment expressions is not the same as the preallocated " 393 "buffer"); 394 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 395 getDestinationExprs().end()); 396 } 397 398 OMPCopyinClause *OMPCopyinClause::Create( 399 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 400 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 401 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) { 402 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size())); 403 OMPCopyinClause *Clause = 404 new (Mem) OMPCopyinClause(StartLoc, LParenLoc, EndLoc, VL.size()); 405 Clause->setVarRefs(VL); 406 Clause->setSourceExprs(SrcExprs); 407 Clause->setDestinationExprs(DstExprs); 408 Clause->setAssignmentOps(AssignmentOps); 409 return Clause; 410 } 411 412 OMPCopyinClause *OMPCopyinClause::CreateEmpty(const ASTContext &C, unsigned N) { 413 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N)); 414 return new (Mem) OMPCopyinClause(N); 415 } 416 417 void OMPCopyprivateClause::setSourceExprs(ArrayRef<Expr *> SrcExprs) { 418 assert(SrcExprs.size() == varlist_size() && "Number of source expressions is " 419 "not the same as the " 420 "preallocated buffer"); 421 std::copy(SrcExprs.begin(), SrcExprs.end(), varlist_end()); 422 } 423 424 void OMPCopyprivateClause::setDestinationExprs(ArrayRef<Expr *> DstExprs) { 425 assert(DstExprs.size() == varlist_size() && "Number of destination " 426 "expressions is not the same as " 427 "the preallocated buffer"); 428 std::copy(DstExprs.begin(), DstExprs.end(), getSourceExprs().end()); 429 } 430 431 void OMPCopyprivateClause::setAssignmentOps(ArrayRef<Expr *> AssignmentOps) { 432 assert(AssignmentOps.size() == varlist_size() && 433 "Number of assignment expressions is not the same as the preallocated " 434 "buffer"); 435 std::copy(AssignmentOps.begin(), AssignmentOps.end(), 436 getDestinationExprs().end()); 437 } 438 439 OMPCopyprivateClause *OMPCopyprivateClause::Create( 440 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 441 SourceLocation EndLoc, ArrayRef<Expr *> VL, ArrayRef<Expr *> SrcExprs, 442 ArrayRef<Expr *> DstExprs, ArrayRef<Expr *> AssignmentOps) { 443 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * VL.size())); 444 OMPCopyprivateClause *Clause = 445 new (Mem) OMPCopyprivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); 446 Clause->setVarRefs(VL); 447 Clause->setSourceExprs(SrcExprs); 448 Clause->setDestinationExprs(DstExprs); 449 Clause->setAssignmentOps(AssignmentOps); 450 return Clause; 451 } 452 453 OMPCopyprivateClause *OMPCopyprivateClause::CreateEmpty(const ASTContext &C, 454 unsigned N) { 455 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(4 * N)); 456 return new (Mem) OMPCopyprivateClause(N); 457 } 458 459 void OMPReductionClause::setPrivates(ArrayRef<Expr *> Privates) { 460 assert(Privates.size() == varlist_size() && 461 "Number of private copies is not the same as the preallocated buffer"); 462 std::copy(Privates.begin(), Privates.end(), varlist_end()); 463 } 464 465 void OMPReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) { 466 assert( 467 LHSExprs.size() == varlist_size() && 468 "Number of LHS expressions is not the same as the preallocated buffer"); 469 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end()); 470 } 471 472 void OMPReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) { 473 assert( 474 RHSExprs.size() == varlist_size() && 475 "Number of RHS expressions is not the same as the preallocated buffer"); 476 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end()); 477 } 478 479 void OMPReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) { 480 assert(ReductionOps.size() == varlist_size() && "Number of reduction " 481 "expressions is not the same " 482 "as the preallocated buffer"); 483 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end()); 484 } 485 486 OMPReductionClause *OMPReductionClause::Create( 487 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 488 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL, 489 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo, 490 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs, 491 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit, 492 Expr *PostUpdate) { 493 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size())); 494 OMPReductionClause *Clause = new (Mem) OMPReductionClause( 495 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo); 496 Clause->setVarRefs(VL); 497 Clause->setPrivates(Privates); 498 Clause->setLHSExprs(LHSExprs); 499 Clause->setRHSExprs(RHSExprs); 500 Clause->setReductionOps(ReductionOps); 501 Clause->setPreInitStmt(PreInit); 502 Clause->setPostUpdateExpr(PostUpdate); 503 return Clause; 504 } 505 506 OMPReductionClause *OMPReductionClause::CreateEmpty(const ASTContext &C, 507 unsigned N) { 508 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N)); 509 return new (Mem) OMPReductionClause(N); 510 } 511 512 void OMPTaskReductionClause::setPrivates(ArrayRef<Expr *> Privates) { 513 assert(Privates.size() == varlist_size() && 514 "Number of private copies is not the same as the preallocated buffer"); 515 std::copy(Privates.begin(), Privates.end(), varlist_end()); 516 } 517 518 void OMPTaskReductionClause::setLHSExprs(ArrayRef<Expr *> LHSExprs) { 519 assert( 520 LHSExprs.size() == varlist_size() && 521 "Number of LHS expressions is not the same as the preallocated buffer"); 522 std::copy(LHSExprs.begin(), LHSExprs.end(), getPrivates().end()); 523 } 524 525 void OMPTaskReductionClause::setRHSExprs(ArrayRef<Expr *> RHSExprs) { 526 assert( 527 RHSExprs.size() == varlist_size() && 528 "Number of RHS expressions is not the same as the preallocated buffer"); 529 std::copy(RHSExprs.begin(), RHSExprs.end(), getLHSExprs().end()); 530 } 531 532 void OMPTaskReductionClause::setReductionOps(ArrayRef<Expr *> ReductionOps) { 533 assert(ReductionOps.size() == varlist_size() && "Number of task reduction " 534 "expressions is not the same " 535 "as the preallocated buffer"); 536 std::copy(ReductionOps.begin(), ReductionOps.end(), getRHSExprs().end()); 537 } 538 539 OMPTaskReductionClause *OMPTaskReductionClause::Create( 540 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 541 SourceLocation EndLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL, 542 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo, 543 ArrayRef<Expr *> Privates, ArrayRef<Expr *> LHSExprs, 544 ArrayRef<Expr *> RHSExprs, ArrayRef<Expr *> ReductionOps, Stmt *PreInit, 545 Expr *PostUpdate) { 546 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * VL.size())); 547 OMPTaskReductionClause *Clause = new (Mem) OMPTaskReductionClause( 548 StartLoc, LParenLoc, EndLoc, ColonLoc, VL.size(), QualifierLoc, NameInfo); 549 Clause->setVarRefs(VL); 550 Clause->setPrivates(Privates); 551 Clause->setLHSExprs(LHSExprs); 552 Clause->setRHSExprs(RHSExprs); 553 Clause->setReductionOps(ReductionOps); 554 Clause->setPreInitStmt(PreInit); 555 Clause->setPostUpdateExpr(PostUpdate); 556 return Clause; 557 } 558 559 OMPTaskReductionClause *OMPTaskReductionClause::CreateEmpty(const ASTContext &C, 560 unsigned N) { 561 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(5 * N)); 562 return new (Mem) OMPTaskReductionClause(N); 563 } 564 565 OMPFlushClause *OMPFlushClause::Create(const ASTContext &C, 566 SourceLocation StartLoc, 567 SourceLocation LParenLoc, 568 SourceLocation EndLoc, 569 ArrayRef<Expr *> VL) { 570 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1)); 571 OMPFlushClause *Clause = 572 new (Mem) OMPFlushClause(StartLoc, LParenLoc, EndLoc, VL.size()); 573 Clause->setVarRefs(VL); 574 return Clause; 575 } 576 577 OMPFlushClause *OMPFlushClause::CreateEmpty(const ASTContext &C, unsigned N) { 578 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N)); 579 return new (Mem) OMPFlushClause(N); 580 } 581 582 OMPDependClause *OMPDependClause::Create( 583 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 584 SourceLocation EndLoc, OpenMPDependClauseKind DepKind, 585 SourceLocation DepLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VL) { 586 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(VL.size() + 1)); 587 OMPDependClause *Clause = 588 new (Mem) OMPDependClause(StartLoc, LParenLoc, EndLoc, VL.size()); 589 Clause->setVarRefs(VL); 590 Clause->setDependencyKind(DepKind); 591 Clause->setDependencyLoc(DepLoc); 592 Clause->setColonLoc(ColonLoc); 593 Clause->setCounterValue(nullptr); 594 return Clause; 595 } 596 597 OMPDependClause *OMPDependClause::CreateEmpty(const ASTContext &C, unsigned N) { 598 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(N + 1)); 599 return new (Mem) OMPDependClause(N); 600 } 601 602 void OMPDependClause::setCounterValue(Expr *V) { 603 assert(getDependencyKind() == OMPC_DEPEND_sink || 604 getDependencyKind() == OMPC_DEPEND_source || V == nullptr); 605 *getVarRefs().end() = V; 606 } 607 608 const Expr *OMPDependClause::getCounterValue() const { 609 auto *V = *getVarRefs().end(); 610 assert(getDependencyKind() == OMPC_DEPEND_sink || 611 getDependencyKind() == OMPC_DEPEND_source || V == nullptr); 612 return V; 613 } 614 615 Expr *OMPDependClause::getCounterValue() { 616 auto *V = *getVarRefs().end(); 617 assert(getDependencyKind() == OMPC_DEPEND_sink || 618 getDependencyKind() == OMPC_DEPEND_source || V == nullptr); 619 return V; 620 } 621 622 unsigned OMPClauseMappableExprCommon::getComponentsTotalNumber( 623 MappableExprComponentListsRef ComponentLists) { 624 unsigned TotalNum = 0u; 625 for (auto &C : ComponentLists) 626 TotalNum += C.size(); 627 return TotalNum; 628 } 629 630 unsigned OMPClauseMappableExprCommon::getUniqueDeclarationsTotalNumber( 631 ArrayRef<ValueDecl *> Declarations) { 632 unsigned TotalNum = 0u; 633 llvm::SmallPtrSet<const ValueDecl *, 8> Cache; 634 for (auto *D : Declarations) { 635 const ValueDecl *VD = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; 636 if (Cache.count(VD)) 637 continue; 638 ++TotalNum; 639 Cache.insert(VD); 640 } 641 return TotalNum; 642 } 643 644 OMPMapClause * 645 OMPMapClause::Create(const ASTContext &C, SourceLocation StartLoc, 646 SourceLocation LParenLoc, SourceLocation EndLoc, 647 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations, 648 MappableExprComponentListsRef ComponentLists, 649 OpenMPMapClauseKind TypeModifier, OpenMPMapClauseKind Type, 650 bool TypeIsImplicit, SourceLocation TypeLoc) { 651 652 unsigned NumVars = Vars.size(); 653 unsigned NumUniqueDeclarations = 654 getUniqueDeclarationsTotalNumber(Declarations); 655 unsigned NumComponentLists = ComponentLists.size(); 656 unsigned NumComponents = getComponentsTotalNumber(ComponentLists); 657 658 // We need to allocate: 659 // NumVars x Expr* - we have an original list expression for each clause list 660 // entry. 661 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated 662 // with each component list. 663 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the 664 // number of lists for each unique declaration and the size of each component 665 // list. 666 // NumComponents x MappableComponent - the total of all the components in all 667 // the lists. 668 void *Mem = C.Allocate( 669 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 670 OMPClauseMappableExprCommon::MappableComponent>( 671 NumVars, NumUniqueDeclarations, 672 NumUniqueDeclarations + NumComponentLists, NumComponents)); 673 OMPMapClause *Clause = new (Mem) OMPMapClause( 674 TypeModifier, Type, TypeIsImplicit, TypeLoc, StartLoc, LParenLoc, EndLoc, 675 NumVars, NumUniqueDeclarations, NumComponentLists, NumComponents); 676 677 Clause->setVarRefs(Vars); 678 Clause->setClauseInfo(Declarations, ComponentLists); 679 Clause->setMapTypeModifier(TypeModifier); 680 Clause->setMapType(Type); 681 Clause->setMapLoc(TypeLoc); 682 return Clause; 683 } 684 685 OMPMapClause *OMPMapClause::CreateEmpty(const ASTContext &C, unsigned NumVars, 686 unsigned NumUniqueDeclarations, 687 unsigned NumComponentLists, 688 unsigned NumComponents) { 689 void *Mem = C.Allocate( 690 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 691 OMPClauseMappableExprCommon::MappableComponent>( 692 NumVars, NumUniqueDeclarations, 693 NumUniqueDeclarations + NumComponentLists, NumComponents)); 694 return new (Mem) OMPMapClause(NumVars, NumUniqueDeclarations, 695 NumComponentLists, NumComponents); 696 } 697 698 OMPToClause *OMPToClause::Create(const ASTContext &C, SourceLocation StartLoc, 699 SourceLocation LParenLoc, 700 SourceLocation EndLoc, ArrayRef<Expr *> Vars, 701 ArrayRef<ValueDecl *> Declarations, 702 MappableExprComponentListsRef ComponentLists) { 703 unsigned NumVars = Vars.size(); 704 unsigned NumUniqueDeclarations = 705 getUniqueDeclarationsTotalNumber(Declarations); 706 unsigned NumComponentLists = ComponentLists.size(); 707 unsigned NumComponents = getComponentsTotalNumber(ComponentLists); 708 709 // We need to allocate: 710 // NumVars x Expr* - we have an original list expression for each clause list 711 // entry. 712 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated 713 // with each component list. 714 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the 715 // number of lists for each unique declaration and the size of each component 716 // list. 717 // NumComponents x MappableComponent - the total of all the components in all 718 // the lists. 719 void *Mem = C.Allocate( 720 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 721 OMPClauseMappableExprCommon::MappableComponent>( 722 NumVars, NumUniqueDeclarations, 723 NumUniqueDeclarations + NumComponentLists, NumComponents)); 724 725 OMPToClause *Clause = new (Mem) 726 OMPToClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations, 727 NumComponentLists, NumComponents); 728 729 Clause->setVarRefs(Vars); 730 Clause->setClauseInfo(Declarations, ComponentLists); 731 return Clause; 732 } 733 734 OMPToClause *OMPToClause::CreateEmpty(const ASTContext &C, unsigned NumVars, 735 unsigned NumUniqueDeclarations, 736 unsigned NumComponentLists, 737 unsigned NumComponents) { 738 void *Mem = C.Allocate( 739 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 740 OMPClauseMappableExprCommon::MappableComponent>( 741 NumVars, NumUniqueDeclarations, 742 NumUniqueDeclarations + NumComponentLists, NumComponents)); 743 return new (Mem) OMPToClause(NumVars, NumUniqueDeclarations, 744 NumComponentLists, NumComponents); 745 } 746 747 OMPFromClause * 748 OMPFromClause::Create(const ASTContext &C, SourceLocation StartLoc, 749 SourceLocation LParenLoc, SourceLocation EndLoc, 750 ArrayRef<Expr *> Vars, ArrayRef<ValueDecl *> Declarations, 751 MappableExprComponentListsRef ComponentLists) { 752 unsigned NumVars = Vars.size(); 753 unsigned NumUniqueDeclarations = 754 getUniqueDeclarationsTotalNumber(Declarations); 755 unsigned NumComponentLists = ComponentLists.size(); 756 unsigned NumComponents = getComponentsTotalNumber(ComponentLists); 757 758 // We need to allocate: 759 // NumVars x Expr* - we have an original list expression for each clause list 760 // entry. 761 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated 762 // with each component list. 763 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the 764 // number of lists for each unique declaration and the size of each component 765 // list. 766 // NumComponents x MappableComponent - the total of all the components in all 767 // the lists. 768 void *Mem = C.Allocate( 769 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 770 OMPClauseMappableExprCommon::MappableComponent>( 771 NumVars, NumUniqueDeclarations, 772 NumUniqueDeclarations + NumComponentLists, NumComponents)); 773 774 OMPFromClause *Clause = new (Mem) 775 OMPFromClause(StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations, 776 NumComponentLists, NumComponents); 777 778 Clause->setVarRefs(Vars); 779 Clause->setClauseInfo(Declarations, ComponentLists); 780 return Clause; 781 } 782 783 OMPFromClause *OMPFromClause::CreateEmpty(const ASTContext &C, unsigned NumVars, 784 unsigned NumUniqueDeclarations, 785 unsigned NumComponentLists, 786 unsigned NumComponents) { 787 void *Mem = C.Allocate( 788 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 789 OMPClauseMappableExprCommon::MappableComponent>( 790 NumVars, NumUniqueDeclarations, 791 NumUniqueDeclarations + NumComponentLists, NumComponents)); 792 return new (Mem) OMPFromClause(NumVars, NumUniqueDeclarations, 793 NumComponentLists, NumComponents); 794 } 795 796 void OMPUseDevicePtrClause::setPrivateCopies(ArrayRef<Expr *> VL) { 797 assert(VL.size() == varlist_size() && 798 "Number of private copies is not the same as the preallocated buffer"); 799 std::copy(VL.begin(), VL.end(), varlist_end()); 800 } 801 802 void OMPUseDevicePtrClause::setInits(ArrayRef<Expr *> VL) { 803 assert(VL.size() == varlist_size() && 804 "Number of inits is not the same as the preallocated buffer"); 805 std::copy(VL.begin(), VL.end(), getPrivateCopies().end()); 806 } 807 808 OMPUseDevicePtrClause *OMPUseDevicePtrClause::Create( 809 const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, 810 SourceLocation EndLoc, ArrayRef<Expr *> Vars, ArrayRef<Expr *> PrivateVars, 811 ArrayRef<Expr *> Inits, ArrayRef<ValueDecl *> Declarations, 812 MappableExprComponentListsRef ComponentLists) { 813 unsigned NumVars = Vars.size(); 814 unsigned NumUniqueDeclarations = 815 getUniqueDeclarationsTotalNumber(Declarations); 816 unsigned NumComponentLists = ComponentLists.size(); 817 unsigned NumComponents = getComponentsTotalNumber(ComponentLists); 818 819 // We need to allocate: 820 // 3 x NumVars x Expr* - we have an original list expression for each clause 821 // list entry and an equal number of private copies and inits. 822 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated 823 // with each component list. 824 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the 825 // number of lists for each unique declaration and the size of each component 826 // list. 827 // NumComponents x MappableComponent - the total of all the components in all 828 // the lists. 829 void *Mem = C.Allocate( 830 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 831 OMPClauseMappableExprCommon::MappableComponent>( 832 3 * NumVars, NumUniqueDeclarations, 833 NumUniqueDeclarations + NumComponentLists, NumComponents)); 834 835 OMPUseDevicePtrClause *Clause = new (Mem) OMPUseDevicePtrClause( 836 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations, 837 NumComponentLists, NumComponents); 838 839 Clause->setVarRefs(Vars); 840 Clause->setPrivateCopies(PrivateVars); 841 Clause->setInits(Inits); 842 Clause->setClauseInfo(Declarations, ComponentLists); 843 return Clause; 844 } 845 846 OMPUseDevicePtrClause *OMPUseDevicePtrClause::CreateEmpty( 847 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations, 848 unsigned NumComponentLists, unsigned NumComponents) { 849 void *Mem = C.Allocate( 850 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 851 OMPClauseMappableExprCommon::MappableComponent>( 852 3 * NumVars, NumUniqueDeclarations, 853 NumUniqueDeclarations + NumComponentLists, NumComponents)); 854 return new (Mem) OMPUseDevicePtrClause(NumVars, NumUniqueDeclarations, 855 NumComponentLists, NumComponents); 856 } 857 858 OMPIsDevicePtrClause * 859 OMPIsDevicePtrClause::Create(const ASTContext &C, SourceLocation StartLoc, 860 SourceLocation LParenLoc, SourceLocation EndLoc, 861 ArrayRef<Expr *> Vars, 862 ArrayRef<ValueDecl *> Declarations, 863 MappableExprComponentListsRef ComponentLists) { 864 unsigned NumVars = Vars.size(); 865 unsigned NumUniqueDeclarations = 866 getUniqueDeclarationsTotalNumber(Declarations); 867 unsigned NumComponentLists = ComponentLists.size(); 868 unsigned NumComponents = getComponentsTotalNumber(ComponentLists); 869 870 // We need to allocate: 871 // NumVars x Expr* - we have an original list expression for each clause list 872 // entry. 873 // NumUniqueDeclarations x ValueDecl* - unique base declarations associated 874 // with each component list. 875 // (NumUniqueDeclarations + NumComponentLists) x unsigned - we specify the 876 // number of lists for each unique declaration and the size of each component 877 // list. 878 // NumComponents x MappableComponent - the total of all the components in all 879 // the lists. 880 void *Mem = C.Allocate( 881 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 882 OMPClauseMappableExprCommon::MappableComponent>( 883 NumVars, NumUniqueDeclarations, 884 NumUniqueDeclarations + NumComponentLists, NumComponents)); 885 886 OMPIsDevicePtrClause *Clause = new (Mem) OMPIsDevicePtrClause( 887 StartLoc, LParenLoc, EndLoc, NumVars, NumUniqueDeclarations, 888 NumComponentLists, NumComponents); 889 890 Clause->setVarRefs(Vars); 891 Clause->setClauseInfo(Declarations, ComponentLists); 892 return Clause; 893 } 894 895 OMPIsDevicePtrClause *OMPIsDevicePtrClause::CreateEmpty( 896 const ASTContext &C, unsigned NumVars, unsigned NumUniqueDeclarations, 897 unsigned NumComponentLists, unsigned NumComponents) { 898 void *Mem = C.Allocate( 899 totalSizeToAlloc<Expr *, ValueDecl *, unsigned, 900 OMPClauseMappableExprCommon::MappableComponent>( 901 NumVars, NumUniqueDeclarations, 902 NumUniqueDeclarations + NumComponentLists, NumComponents)); 903 return new (Mem) OMPIsDevicePtrClause(NumVars, NumUniqueDeclarations, 904 NumComponentLists, NumComponents); 905 } 906