1 //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// 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 contains code to emit OpenMP nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CGOpenMPRuntime.h" 15 #include "CodeGenFunction.h" 16 #include "CodeGenModule.h" 17 #include "TargetInfo.h" 18 #include "clang/AST/Stmt.h" 19 #include "clang/AST/StmtOpenMP.h" 20 using namespace clang; 21 using namespace CodeGen; 22 23 //===----------------------------------------------------------------------===// 24 // OpenMP Directive Emission 25 //===----------------------------------------------------------------------===// 26 namespace { 27 /// \brief RAII for inlined OpenMP regions (like 'omp for', 'omp simd', 'omp 28 /// critical' etc.). Helps to generate proper debug info and provides correct 29 /// code generation for such constructs. 30 class InlinedOpenMPRegionScopeRAII { 31 InlinedOpenMPRegionRAII Region; 32 CodeGenFunction::LexicalScope DirectiveScope; 33 34 public: 35 InlinedOpenMPRegionScopeRAII(CodeGenFunction &CGF, 36 const OMPExecutableDirective &D) 37 : Region(CGF, D), DirectiveScope(CGF, D.getSourceRange()) {} 38 }; 39 } // namespace 40 41 /// \brief Emits code for OpenMP 'if' clause using specified \a CodeGen 42 /// function. Here is the logic: 43 /// if (Cond) { 44 /// CodeGen(true); 45 /// } else { 46 /// CodeGen(false); 47 /// } 48 static void EmitOMPIfClause(CodeGenFunction &CGF, const Expr *Cond, 49 const std::function<void(bool)> &CodeGen) { 50 CodeGenFunction::LexicalScope ConditionScope(CGF, Cond->getSourceRange()); 51 52 // If the condition constant folds and can be elided, try to avoid emitting 53 // the condition and the dead arm of the if/else. 54 bool CondConstant; 55 if (CGF.ConstantFoldsToSimpleInteger(Cond, CondConstant)) { 56 CodeGen(CondConstant); 57 return; 58 } 59 60 // Otherwise, the condition did not fold, or we couldn't elide it. Just 61 // emit the conditional branch. 62 auto ThenBlock = CGF.createBasicBlock(/*name*/ "omp_if.then"); 63 auto ElseBlock = CGF.createBasicBlock(/*name*/ "omp_if.else"); 64 auto ContBlock = CGF.createBasicBlock(/*name*/ "omp_if.end"); 65 CGF.EmitBranchOnBoolExpr(Cond, ThenBlock, ElseBlock, /*TrueCount*/ 0); 66 67 // Emit the 'then' code. 68 CGF.EmitBlock(ThenBlock); 69 CodeGen(/*ThenBlock*/ true); 70 CGF.EmitBranch(ContBlock); 71 // Emit the 'else' code if present. 72 { 73 // There is no need to emit line number for unconditional branch. 74 auto NL = ApplyDebugLocation::CreateEmpty(CGF); 75 CGF.EmitBlock(ElseBlock); 76 } 77 CodeGen(/*ThenBlock*/ false); 78 { 79 // There is no need to emit line number for unconditional branch. 80 auto NL = ApplyDebugLocation::CreateEmpty(CGF); 81 CGF.EmitBranch(ContBlock); 82 } 83 // Emit the continuation block for code after the if. 84 CGF.EmitBlock(ContBlock, /*IsFinished*/ true); 85 } 86 87 void CodeGenFunction::EmitOMPAggregateAssign(LValue OriginalAddr, 88 llvm::Value *PrivateAddr, 89 const Expr *AssignExpr, 90 QualType OriginalType, 91 const VarDecl *VDInit) { 92 EmitBlock(createBasicBlock(".omp.assign.begin.")); 93 if (!isa<CXXConstructExpr>(AssignExpr) || isTrivialInitializer(AssignExpr)) { 94 // Perform simple memcpy. 95 EmitAggregateAssign(PrivateAddr, OriginalAddr.getAddress(), 96 AssignExpr->getType()); 97 } else { 98 // Perform element-by-element initialization. 99 QualType ElementTy; 100 auto SrcBegin = OriginalAddr.getAddress(); 101 auto DestBegin = PrivateAddr; 102 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); 103 auto SrcNumElements = emitArrayLength(ArrayTy, ElementTy, SrcBegin); 104 auto DestNumElements = emitArrayLength(ArrayTy, ElementTy, DestBegin); 105 auto SrcEnd = Builder.CreateGEP(SrcBegin, SrcNumElements); 106 auto DestEnd = Builder.CreateGEP(DestBegin, DestNumElements); 107 // The basic structure here is a do-while loop, because we don't 108 // need to check for the zero-element case. 109 auto BodyBB = createBasicBlock("omp.arraycpy.body"); 110 auto DoneBB = createBasicBlock("omp.arraycpy.done"); 111 auto IsEmpty = 112 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); 113 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); 114 115 // Enter the loop body, making that address the current address. 116 auto EntryBB = Builder.GetInsertBlock(); 117 EmitBlock(BodyBB); 118 auto SrcElementPast = Builder.CreatePHI(SrcBegin->getType(), 2, 119 "omp.arraycpy.srcElementPast"); 120 SrcElementPast->addIncoming(SrcEnd, EntryBB); 121 auto DestElementPast = Builder.CreatePHI(DestBegin->getType(), 2, 122 "omp.arraycpy.destElementPast"); 123 DestElementPast->addIncoming(DestEnd, EntryBB); 124 125 // Shift the address back by one element. 126 auto NegativeOne = llvm::ConstantInt::get(SizeTy, -1, true); 127 auto DestElement = Builder.CreateGEP(DestElementPast, NegativeOne, 128 "omp.arraycpy.dest.element"); 129 auto SrcElement = Builder.CreateGEP(SrcElementPast, NegativeOne, 130 "omp.arraycpy.src.element"); 131 { 132 // Create RunCleanScope to cleanup possible temps. 133 CodeGenFunction::RunCleanupsScope Init(*this); 134 // Emit initialization for single element. 135 LocalDeclMap[VDInit] = SrcElement; 136 EmitAnyExprToMem(AssignExpr, DestElement, 137 AssignExpr->getType().getQualifiers(), 138 /*IsInitializer*/ false); 139 LocalDeclMap.erase(VDInit); 140 } 141 142 // Check whether we've reached the end. 143 auto Done = 144 Builder.CreateICmpEQ(DestElement, DestBegin, "omp.arraycpy.done"); 145 Builder.CreateCondBr(Done, DoneBB, BodyBB); 146 DestElementPast->addIncoming(DestElement, Builder.GetInsertBlock()); 147 SrcElementPast->addIncoming(SrcElement, Builder.GetInsertBlock()); 148 149 // Done. 150 EmitBlock(DoneBB, true); 151 } 152 EmitBlock(createBasicBlock(".omp.assign.end.")); 153 } 154 155 void CodeGenFunction::EmitOMPFirstprivateClause( 156 const OMPExecutableDirective &D, 157 CodeGenFunction::OMPPrivateScope &PrivateScope) { 158 auto PrivateFilter = [](const OMPClause *C) -> bool { 159 return C->getClauseKind() == OMPC_firstprivate; 160 }; 161 for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)> 162 I(D.clauses(), PrivateFilter); I; ++I) { 163 auto *C = cast<OMPFirstprivateClause>(*I); 164 auto IRef = C->varlist_begin(); 165 auto InitsRef = C->inits().begin(); 166 for (auto IInit : C->private_copies()) { 167 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 168 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 169 bool IsRegistered; 170 if (*InitsRef != nullptr) { 171 // Emit VarDecl with copy init for arrays. 172 auto *FD = CapturedStmtInfo->lookup(OrigVD); 173 LValue Base = MakeNaturalAlignAddrLValue( 174 CapturedStmtInfo->getContextValue(), 175 getContext().getTagDeclType(FD->getParent())); 176 auto OriginalAddr = EmitLValueForField(Base, FD); 177 auto VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); 178 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * { 179 auto Emission = EmitAutoVarAlloca(*VD); 180 // Emit initialization of aggregate firstprivate vars. 181 EmitOMPAggregateAssign(OriginalAddr, Emission.getAllocatedAddress(), 182 VD->getInit(), (*IRef)->getType(), VDInit); 183 EmitAutoVarCleanups(Emission); 184 return Emission.getAllocatedAddress(); 185 }); 186 } else 187 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * { 188 // Emit private VarDecl with copy init. 189 EmitDecl(*VD); 190 return GetAddrOfLocalVar(VD); 191 }); 192 assert(IsRegistered && "counter already registered as private"); 193 // Silence the warning about unused variable. 194 (void)IsRegistered; 195 ++IRef, ++InitsRef; 196 } 197 } 198 } 199 200 void CodeGenFunction::EmitOMPPrivateClause( 201 const OMPExecutableDirective &D, 202 CodeGenFunction::OMPPrivateScope &PrivateScope) { 203 auto PrivateFilter = [](const OMPClause *C) -> bool { 204 return C->getClauseKind() == OMPC_private; 205 }; 206 for (OMPExecutableDirective::filtered_clause_iterator<decltype(PrivateFilter)> 207 I(D.clauses(), PrivateFilter); I; ++I) { 208 auto *C = cast<OMPPrivateClause>(*I); 209 auto IRef = C->varlist_begin(); 210 for (auto IInit : C->private_copies()) { 211 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 212 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 213 bool IsRegistered = 214 PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value * { 215 // Emit private VarDecl with copy init. 216 EmitDecl(*VD); 217 return GetAddrOfLocalVar(VD); 218 }); 219 assert(IsRegistered && "counter already registered as private"); 220 // Silence the warning about unused variable. 221 (void)IsRegistered; 222 ++IRef; 223 } 224 } 225 } 226 227 /// \brief Emits code for OpenMP parallel directive in the parallel region. 228 static void EmitOMPParallelCall(CodeGenFunction &CGF, 229 const OMPParallelDirective &S, 230 llvm::Value *OutlinedFn, 231 llvm::Value *CapturedStruct) { 232 if (auto C = S.getSingleClause(/*K*/ OMPC_num_threads)) { 233 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); 234 auto NumThreadsClause = cast<OMPNumThreadsClause>(C); 235 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), 236 /*IgnoreResultAssign*/ true); 237 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( 238 CGF, NumThreads, NumThreadsClause->getLocStart()); 239 } 240 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, 241 CapturedStruct); 242 } 243 244 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { 245 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 246 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 247 auto OutlinedFn = CGM.getOpenMPRuntime().emitOutlinedFunction( 248 S, *CS->getCapturedDecl()->param_begin()); 249 if (auto C = S.getSingleClause(/*K*/ OMPC_if)) { 250 auto Cond = cast<OMPIfClause>(C)->getCondition(); 251 EmitOMPIfClause(*this, Cond, [&](bool ThenBlock) { 252 if (ThenBlock) 253 EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct); 254 else 255 CGM.getOpenMPRuntime().emitSerialCall(*this, S.getLocStart(), 256 OutlinedFn, CapturedStruct); 257 }); 258 } else 259 EmitOMPParallelCall(*this, S, OutlinedFn, CapturedStruct); 260 } 261 262 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &S, 263 bool SeparateIter) { 264 RunCleanupsScope BodyScope(*this); 265 // Update counters values on current iteration. 266 for (auto I : S.updates()) { 267 EmitIgnoredExpr(I); 268 } 269 // On a continue in the body, jump to the end. 270 auto Continue = getJumpDestInCurrentScope("omp.body.continue"); 271 BreakContinueStack.push_back(BreakContinue(JumpDest(), Continue)); 272 // Emit loop body. 273 EmitStmt(S.getBody()); 274 // The end (updates/cleanups). 275 EmitBlock(Continue.getBlock()); 276 BreakContinueStack.pop_back(); 277 if (SeparateIter) { 278 // TODO: Update lastprivates if the SeparateIter flag is true. 279 // This will be implemented in a follow-up OMPLastprivateClause patch, but 280 // result should be still correct without it, as we do not make these 281 // variables private yet. 282 } 283 } 284 285 void CodeGenFunction::EmitOMPInnerLoop(const Stmt &S, bool RequiresCleanup, 286 const Expr *LoopCond, 287 const Expr *IncExpr, 288 const std::function<void()> &BodyGen) { 289 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); 290 auto Cnt = getPGORegionCounter(&S); 291 292 // Start the loop with a block that tests the condition. 293 auto CondBlock = createBasicBlock("omp.inner.for.cond"); 294 EmitBlock(CondBlock); 295 LoopStack.push(CondBlock); 296 297 // If there are any cleanups between here and the loop-exit scope, 298 // create a block to stage a loop exit along. 299 auto ExitBlock = LoopExit.getBlock(); 300 if (RequiresCleanup) 301 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); 302 303 auto LoopBody = createBasicBlock("omp.inner.for.body"); 304 305 // Emit condition. 306 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, Cnt.getCount()); 307 if (ExitBlock != LoopExit.getBlock()) { 308 EmitBlock(ExitBlock); 309 EmitBranchThroughCleanup(LoopExit); 310 } 311 312 EmitBlock(LoopBody); 313 Cnt.beginRegion(Builder); 314 315 // Create a block for the increment. 316 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); 317 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 318 319 BodyGen(); 320 321 // Emit "IV = IV + 1" and a back-edge to the condition block. 322 EmitBlock(Continue.getBlock()); 323 EmitIgnoredExpr(IncExpr); 324 BreakContinueStack.pop_back(); 325 EmitBranch(CondBlock); 326 LoopStack.pop(); 327 // Emit the fall-through block. 328 EmitBlock(LoopExit.getBlock()); 329 } 330 331 void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &S) { 332 auto IC = S.counters().begin(); 333 for (auto F : S.finals()) { 334 if (LocalDeclMap.lookup(cast<DeclRefExpr>((*IC))->getDecl())) { 335 EmitIgnoredExpr(F); 336 } 337 ++IC; 338 } 339 } 340 341 static void EmitOMPAlignedClause(CodeGenFunction &CGF, CodeGenModule &CGM, 342 const OMPAlignedClause &Clause) { 343 unsigned ClauseAlignment = 0; 344 if (auto AlignmentExpr = Clause.getAlignment()) { 345 auto AlignmentCI = 346 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); 347 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); 348 } 349 for (auto E : Clause.varlists()) { 350 unsigned Alignment = ClauseAlignment; 351 if (Alignment == 0) { 352 // OpenMP [2.8.1, Description] 353 // If no optional parameter is specified, implementation-defined default 354 // alignments for SIMD instructions on the target platforms are assumed. 355 Alignment = CGM.getTargetCodeGenInfo().getOpenMPSimdDefaultAlignment( 356 E->getType()); 357 } 358 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && 359 "alignment is not power of 2"); 360 if (Alignment != 0) { 361 llvm::Value *PtrValue = CGF.EmitScalarExpr(E); 362 CGF.EmitAlignmentAssumption(PtrValue, Alignment); 363 } 364 } 365 } 366 367 static void EmitPrivateLoopCounters(CodeGenFunction &CGF, 368 CodeGenFunction::OMPPrivateScope &LoopScope, 369 ArrayRef<Expr *> Counters) { 370 for (auto *E : Counters) { 371 auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 372 bool IsRegistered = LoopScope.addPrivate(VD, [&]() -> llvm::Value * { 373 // Emit var without initialization. 374 auto VarEmission = CGF.EmitAutoVarAlloca(*VD); 375 CGF.EmitAutoVarCleanups(VarEmission); 376 return VarEmission.getAllocatedAddress(); 377 }); 378 assert(IsRegistered && "counter already registered as private"); 379 // Silence the warning about unused variable. 380 (void)IsRegistered; 381 } 382 (void)LoopScope.Privatize(); 383 } 384 385 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { 386 // Pragma 'simd' code depends on presence of 'lastprivate'. 387 // If present, we have to separate last iteration of the loop: 388 // 389 // if (LastIteration != 0) { 390 // for (IV in 0..LastIteration-1) BODY; 391 // BODY with updates of lastprivate vars; 392 // <Final counter/linear vars updates>; 393 // } 394 // 395 // otherwise (when there's no lastprivate): 396 // 397 // for (IV in 0..LastIteration) BODY; 398 // <Final counter/linear vars updates>; 399 // 400 401 // Walk clauses and process safelen/lastprivate. 402 bool SeparateIter = false; 403 LoopStack.setParallel(); 404 LoopStack.setVectorizerEnable(true); 405 for (auto C : S.clauses()) { 406 switch (C->getClauseKind()) { 407 case OMPC_safelen: { 408 RValue Len = EmitAnyExpr(cast<OMPSafelenClause>(C)->getSafelen(), 409 AggValueSlot::ignored(), true); 410 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 411 LoopStack.setVectorizerWidth(Val->getZExtValue()); 412 // In presence of finite 'safelen', it may be unsafe to mark all 413 // the memory instructions parallel, because loop-carried 414 // dependences of 'safelen' iterations are possible. 415 LoopStack.setParallel(false); 416 break; 417 } 418 case OMPC_aligned: 419 EmitOMPAlignedClause(*this, CGM, cast<OMPAlignedClause>(*C)); 420 break; 421 case OMPC_lastprivate: 422 SeparateIter = true; 423 break; 424 default: 425 // Not handled yet 426 ; 427 } 428 } 429 430 InlinedOpenMPRegionScopeRAII Region(*this, S); 431 432 // Emit the loop iteration variable. 433 const Expr *IVExpr = S.getIterationVariable(); 434 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 435 EmitVarDecl(*IVDecl); 436 EmitIgnoredExpr(S.getInit()); 437 438 // Emit the iterations count variable. 439 // If it is not a variable, Sema decided to calculate iterations count on each 440 // iteration (e.g., it is foldable into a constant). 441 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 442 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 443 // Emit calculation of the iterations count. 444 EmitIgnoredExpr(S.getCalcLastIteration()); 445 } 446 447 if (SeparateIter) { 448 // Emit: if (LastIteration > 0) - begin. 449 RegionCounter Cnt = getPGORegionCounter(&S); 450 auto ThenBlock = createBasicBlock("simd.if.then"); 451 auto ContBlock = createBasicBlock("simd.if.end"); 452 EmitBranchOnBoolExpr(S.getPreCond(), ThenBlock, ContBlock, Cnt.getCount()); 453 EmitBlock(ThenBlock); 454 Cnt.beginRegion(Builder); 455 // Emit 'then' code. 456 { 457 OMPPrivateScope LoopScope(*this); 458 EmitPrivateLoopCounters(*this, LoopScope, S.counters()); 459 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), 460 S.getCond(/*SeparateIter=*/true), S.getInc(), 461 [&S, this]() { 462 EmitOMPLoopBody(S); 463 EmitStopPoint(&S); 464 }); 465 EmitOMPLoopBody(S, /* SeparateIter */ true); 466 } 467 EmitOMPSimdFinal(S); 468 // Emit: if (LastIteration != 0) - end. 469 EmitBranch(ContBlock); 470 EmitBlock(ContBlock, true); 471 } else { 472 { 473 OMPPrivateScope LoopScope(*this); 474 EmitPrivateLoopCounters(*this, LoopScope, S.counters()); 475 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), 476 S.getCond(/*SeparateIter=*/false), S.getInc(), 477 [&S, this]() { 478 EmitOMPLoopBody(S); 479 EmitStopPoint(&S); 480 }); 481 } 482 EmitOMPSimdFinal(S); 483 } 484 } 485 486 void CodeGenFunction::EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind, 487 const OMPLoopDirective &S, 488 OMPPrivateScope &LoopScope, 489 llvm::Value *LB, llvm::Value *UB, 490 llvm::Value *ST, llvm::Value *IL, 491 llvm::Value *Chunk) { 492 auto &RT = CGM.getOpenMPRuntime(); 493 494 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). 495 const bool Dynamic = RT.isDynamic(ScheduleKind); 496 497 assert(!RT.isStaticNonchunked(ScheduleKind, /* Chunked */ Chunk != nullptr) && 498 "static non-chunked schedule does not need outer loop"); 499 500 // Emit outer loop. 501 // 502 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 503 // When schedule(dynamic,chunk_size) is specified, the iterations are 504 // distributed to threads in the team in chunks as the threads request them. 505 // Each thread executes a chunk of iterations, then requests another chunk, 506 // until no chunks remain to be distributed. Each chunk contains chunk_size 507 // iterations, except for the last chunk to be distributed, which may have 508 // fewer iterations. When no chunk_size is specified, it defaults to 1. 509 // 510 // When schedule(guided,chunk_size) is specified, the iterations are assigned 511 // to threads in the team in chunks as the executing threads request them. 512 // Each thread executes a chunk of iterations, then requests another chunk, 513 // until no chunks remain to be assigned. For a chunk_size of 1, the size of 514 // each chunk is proportional to the number of unassigned iterations divided 515 // by the number of threads in the team, decreasing to 1. For a chunk_size 516 // with value k (greater than 1), the size of each chunk is determined in the 517 // same way, with the restriction that the chunks do not contain fewer than k 518 // iterations (except for the last chunk to be assigned, which may have fewer 519 // than k iterations). 520 // 521 // When schedule(auto) is specified, the decision regarding scheduling is 522 // delegated to the compiler and/or runtime system. The programmer gives the 523 // implementation the freedom to choose any possible mapping of iterations to 524 // threads in the team. 525 // 526 // When schedule(runtime) is specified, the decision regarding scheduling is 527 // deferred until run time, and the schedule and chunk size are taken from the 528 // run-sched-var ICV. If the ICV is set to auto, the schedule is 529 // implementation defined 530 // 531 // while(__kmpc_dispatch_next(&LB, &UB)) { 532 // idx = LB; 533 // while (idx <= UB) { BODY; ++idx; } // inner loop 534 // } 535 // 536 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 537 // When schedule(static, chunk_size) is specified, iterations are divided into 538 // chunks of size chunk_size, and the chunks are assigned to the threads in 539 // the team in a round-robin fashion in the order of the thread number. 540 // 541 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { 542 // while (idx <= UB) { BODY; ++idx; } // inner loop 543 // LB = LB + ST; 544 // UB = UB + ST; 545 // } 546 // 547 548 const Expr *IVExpr = S.getIterationVariable(); 549 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 550 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 551 552 RT.emitForInit( 553 *this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, IL, LB, 554 (Dynamic ? EmitAnyExpr(S.getLastIteration()).getScalarVal() : UB), ST, 555 Chunk); 556 557 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); 558 559 // Start the loop with a block that tests the condition. 560 auto CondBlock = createBasicBlock("omp.dispatch.cond"); 561 EmitBlock(CondBlock); 562 LoopStack.push(CondBlock); 563 564 llvm::Value *BoolCondVal = nullptr; 565 if (!Dynamic) { 566 // UB = min(UB, GlobalUB) 567 EmitIgnoredExpr(S.getEnsureUpperBound()); 568 // IV = LB 569 EmitIgnoredExpr(S.getInit()); 570 // IV < UB 571 BoolCondVal = EvaluateExprAsBool(S.getCond(false)); 572 } else { 573 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, 574 IL, LB, UB, ST); 575 } 576 577 // If there are any cleanups between here and the loop-exit scope, 578 // create a block to stage a loop exit along. 579 auto ExitBlock = LoopExit.getBlock(); 580 if (LoopScope.requiresCleanups()) 581 ExitBlock = createBasicBlock("omp.dispatch.cleanup"); 582 583 auto LoopBody = createBasicBlock("omp.dispatch.body"); 584 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); 585 if (ExitBlock != LoopExit.getBlock()) { 586 EmitBlock(ExitBlock); 587 EmitBranchThroughCleanup(LoopExit); 588 } 589 EmitBlock(LoopBody); 590 591 // Emit "IV = LB" (in case of static schedule, we have already calculated new 592 // LB for loop condition and emitted it above). 593 if (Dynamic) 594 EmitIgnoredExpr(S.getInit()); 595 596 // Create a block for the increment. 597 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); 598 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 599 600 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), 601 S.getCond(/*SeparateIter=*/false), S.getInc(), [&S, this]() { 602 EmitOMPLoopBody(S); 603 EmitStopPoint(&S); 604 }); 605 606 EmitBlock(Continue.getBlock()); 607 BreakContinueStack.pop_back(); 608 if (!Dynamic) { 609 // Emit "LB = LB + Stride", "UB = UB + Stride". 610 EmitIgnoredExpr(S.getNextLowerBound()); 611 EmitIgnoredExpr(S.getNextUpperBound()); 612 } 613 614 EmitBranch(CondBlock); 615 LoopStack.pop(); 616 // Emit the fall-through block. 617 EmitBlock(LoopExit.getBlock()); 618 619 // Tell the runtime we are done. 620 // FIXME: Also call fini for ordered loops with dynamic scheduling. 621 if (!Dynamic) 622 RT.emitForFinish(*this, S.getLocStart(), ScheduleKind); 623 } 624 625 /// \brief Emit a helper variable and return corresponding lvalue. 626 static LValue EmitOMPHelperVar(CodeGenFunction &CGF, 627 const DeclRefExpr *Helper) { 628 auto VDecl = cast<VarDecl>(Helper->getDecl()); 629 CGF.EmitVarDecl(*VDecl); 630 return CGF.EmitLValue(Helper); 631 } 632 633 void CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { 634 // Emit the loop iteration variable. 635 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 636 auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); 637 EmitVarDecl(*IVDecl); 638 639 // Emit the iterations count variable. 640 // If it is not a variable, Sema decided to calculate iterations count on each 641 // iteration (e.g., it is foldable into a constant). 642 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 643 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 644 // Emit calculation of the iterations count. 645 EmitIgnoredExpr(S.getCalcLastIteration()); 646 } 647 648 auto &RT = CGM.getOpenMPRuntime(); 649 650 // Check pre-condition. 651 { 652 // Skip the entire loop if we don't meet the precondition. 653 RegionCounter Cnt = getPGORegionCounter(&S); 654 auto ThenBlock = createBasicBlock("omp.precond.then"); 655 auto ContBlock = createBasicBlock("omp.precond.end"); 656 EmitBranchOnBoolExpr(S.getPreCond(), ThenBlock, ContBlock, Cnt.getCount()); 657 EmitBlock(ThenBlock); 658 Cnt.beginRegion(Builder); 659 // Emit 'then' code. 660 { 661 // Emit helper vars inits. 662 LValue LB = 663 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); 664 LValue UB = 665 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); 666 LValue ST = 667 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 668 LValue IL = 669 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 670 671 OMPPrivateScope LoopScope(*this); 672 EmitPrivateLoopCounters(*this, LoopScope, S.counters()); 673 674 // Detect the loop schedule kind and chunk. 675 auto ScheduleKind = OMPC_SCHEDULE_unknown; 676 llvm::Value *Chunk = nullptr; 677 if (auto C = cast_or_null<OMPScheduleClause>( 678 S.getSingleClause(OMPC_schedule))) { 679 ScheduleKind = C->getScheduleKind(); 680 if (auto Ch = C->getChunkSize()) { 681 Chunk = EmitScalarExpr(Ch); 682 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 683 S.getIterationVariable()->getType()); 684 } 685 } 686 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 687 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 688 if (RT.isStaticNonchunked(ScheduleKind, 689 /* Chunked */ Chunk != nullptr)) { 690 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 691 // When no chunk_size is specified, the iteration space is divided into 692 // chunks that are approximately equal in size, and at most one chunk is 693 // distributed to each thread. Note that the size of the chunks is 694 // unspecified in this case. 695 RT.emitForInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, 696 IL.getAddress(), LB.getAddress(), UB.getAddress(), 697 ST.getAddress()); 698 // UB = min(UB, GlobalUB); 699 EmitIgnoredExpr(S.getEnsureUpperBound()); 700 // IV = LB; 701 EmitIgnoredExpr(S.getInit()); 702 // while (idx <= UB) { BODY; ++idx; } 703 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), 704 S.getCond(/*SeparateIter=*/false), S.getInc(), 705 [&S, this]() { 706 EmitOMPLoopBody(S); 707 EmitStopPoint(&S); 708 }); 709 // Tell the runtime we are done. 710 RT.emitForFinish(*this, S.getLocStart(), ScheduleKind); 711 } else { 712 // Emit the outer loop, which requests its work chunk [LB..UB] from 713 // runtime and runs the inner loop to process it. 714 EmitOMPForOuterLoop(ScheduleKind, S, LoopScope, LB.getAddress(), 715 UB.getAddress(), ST.getAddress(), IL.getAddress(), 716 Chunk); 717 } 718 } 719 // We're now done with the loop, so jump to the continuation block. 720 EmitBranch(ContBlock); 721 EmitBlock(ContBlock, true); 722 } 723 } 724 725 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { 726 InlinedOpenMPRegionScopeRAII Region(*this, S); 727 728 EmitOMPWorksharingLoop(S); 729 730 // Emit an implicit barrier at the end. 731 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 732 /*IsExplicit*/ false); 733 } 734 735 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &) { 736 llvm_unreachable("CodeGen for 'omp for simd' is not supported yet."); 737 } 738 739 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, 740 const Twine &Name, 741 llvm::Value *Init = nullptr) { 742 auto LVal = CGF.MakeNaturalAlignAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); 743 if (Init) 744 CGF.EmitScalarInit(Init, LVal); 745 return LVal; 746 } 747 748 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { 749 InlinedOpenMPRegionScopeRAII Region(*this, S); 750 751 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); 752 auto *CS = dyn_cast<CompoundStmt>(Stmt); 753 if (CS && CS->size() > 1) { 754 auto &C = CGM.getContext(); 755 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 756 // Emit helper vars inits. 757 LValue LB = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.lb.", 758 Builder.getInt32(0)); 759 auto *GlobalUBVal = Builder.getInt32(CS->size() - 1); 760 LValue UB = 761 createSectionLVal(*this, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); 762 LValue ST = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.st.", 763 Builder.getInt32(1)); 764 LValue IL = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.il.", 765 Builder.getInt32(0)); 766 // Loop counter. 767 LValue IV = createSectionLVal(*this, KmpInt32Ty, ".omp.sections.iv."); 768 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 769 OpaqueValueMapping OpaqueIV(*this, &IVRefExpr, IV); 770 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 771 OpaqueValueMapping OpaqueUB(*this, &UBRefExpr, UB); 772 // Generate condition for loop. 773 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, 774 OK_Ordinary, S.getLocStart(), /*fpContractable=*/false); 775 // Increment for loop counter. 776 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, 777 S.getLocStart()); 778 auto BodyGen = [this, CS, &S, &IV]() { 779 // Iterate through all sections and emit a switch construct: 780 // switch (IV) { 781 // case 0: 782 // <SectionStmt[0]>; 783 // break; 784 // ... 785 // case <NumSection> - 1: 786 // <SectionStmt[<NumSection> - 1]>; 787 // break; 788 // } 789 // .omp.sections.exit: 790 auto *ExitBB = createBasicBlock(".omp.sections.exit"); 791 auto *SwitchStmt = Builder.CreateSwitch( 792 EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, 793 CS->size()); 794 unsigned CaseNumber = 0; 795 for (auto C = CS->children(); C; ++C, ++CaseNumber) { 796 auto CaseBB = createBasicBlock(".omp.sections.case"); 797 EmitBlock(CaseBB); 798 SwitchStmt->addCase(Builder.getInt32(CaseNumber), CaseBB); 799 EmitStmt(*C); 800 EmitBranch(ExitBB); 801 } 802 EmitBlock(ExitBB, /*IsFinished=*/true); 803 }; 804 // Emit static non-chunked loop. 805 CGM.getOpenMPRuntime().emitForInit( 806 *this, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32, 807 /*IVSigned=*/true, IL.getAddress(), LB.getAddress(), UB.getAddress(), 808 ST.getAddress()); 809 // UB = min(UB, GlobalUB); 810 auto *UBVal = EmitLoadOfScalar(UB, S.getLocStart()); 811 auto *MinUBGlobalUB = Builder.CreateSelect( 812 Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); 813 EmitStoreOfScalar(MinUBGlobalUB, UB); 814 // IV = LB; 815 EmitStoreOfScalar(EmitLoadOfScalar(LB, S.getLocStart()), IV); 816 // while (idx <= UB) { BODY; ++idx; } 817 EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen); 818 // Tell the runtime we are done. 819 CGM.getOpenMPRuntime().emitForFinish(*this, S.getLocStart(), 820 OMPC_SCHEDULE_static); 821 } else { 822 // If only one section is found - no need to generate loop, emit as a single 823 // region. 824 CGM.getOpenMPRuntime().emitSingleRegion(*this, [&]() -> void { 825 InlinedOpenMPRegionScopeRAII Region(*this, S); 826 EmitStmt(Stmt); 827 EnsureInsertPoint(); 828 }, S.getLocStart()); 829 } 830 831 // Emit an implicit barrier at the end. 832 if (!S.getSingleClause(OMPC_nowait)) 833 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 834 /*IsExplicit=*/false); 835 } 836 837 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { 838 InlinedOpenMPRegionScopeRAII Region(*this, S); 839 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 840 EnsureInsertPoint(); 841 } 842 843 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { 844 CGM.getOpenMPRuntime().emitSingleRegion(*this, [&]() -> void { 845 InlinedOpenMPRegionScopeRAII Region(*this, S); 846 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 847 EnsureInsertPoint(); 848 }, S.getLocStart()); 849 } 850 851 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { 852 CGM.getOpenMPRuntime().emitMasterRegion(*this, [&]() -> void { 853 InlinedOpenMPRegionScopeRAII Region(*this, S); 854 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 855 EnsureInsertPoint(); 856 }, S.getLocStart()); 857 } 858 859 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { 860 CGM.getOpenMPRuntime().emitCriticalRegion( 861 *this, S.getDirectiveName().getAsString(), [&]() -> void { 862 InlinedOpenMPRegionScopeRAII Region(*this, S); 863 EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 864 EnsureInsertPoint(); 865 }, S.getLocStart()); 866 } 867 868 void 869 CodeGenFunction::EmitOMPParallelForDirective(const OMPParallelForDirective &) { 870 llvm_unreachable("CodeGen for 'omp parallel for' is not supported yet."); 871 } 872 873 void CodeGenFunction::EmitOMPParallelForSimdDirective( 874 const OMPParallelForSimdDirective &) { 875 llvm_unreachable("CodeGen for 'omp parallel for simd' is not supported yet."); 876 } 877 878 void CodeGenFunction::EmitOMPParallelSectionsDirective( 879 const OMPParallelSectionsDirective &) { 880 llvm_unreachable("CodeGen for 'omp parallel sections' is not supported yet."); 881 } 882 883 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { 884 // Emit outlined function for task construct. 885 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 886 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 887 auto *I = CS->getCapturedDecl()->param_begin(); 888 // The first function argument for tasks is a thread id, the second one is a 889 // part id (0 for tied tasks, >=0 for untied task). 890 auto OutlinedFn = 891 CGM.getOpenMPRuntime().emitTaskOutlinedFunction(S, *I, *std::next(I)); 892 // Check if we should emit tied or untied task. 893 bool Tied = !S.getSingleClause(OMPC_untied); 894 // Check if the task is final 895 llvm::PointerIntPair<llvm::Value *, 1, bool> Final; 896 if (auto *Clause = S.getSingleClause(OMPC_final)) { 897 // If the condition constant folds and can be elided, try to avoid emitting 898 // the condition and the dead arm of the if/else. 899 auto *Cond = cast<OMPFinalClause>(Clause)->getCondition(); 900 bool CondConstant; 901 if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) 902 Final.setInt(CondConstant); 903 else 904 Final.setPointer(EvaluateExprAsBool(Cond)); 905 } else { 906 // By default the task is not final. 907 Final.setInt(/*IntVal=*/false); 908 } 909 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 910 CGM.getOpenMPRuntime().emitTaskCall(*this, S.getLocStart(), Tied, Final, 911 OutlinedFn, SharedsTy, CapturedStruct); 912 } 913 914 void CodeGenFunction::EmitOMPTaskyieldDirective( 915 const OMPTaskyieldDirective &S) { 916 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); 917 } 918 919 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { 920 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart()); 921 } 922 923 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &) { 924 llvm_unreachable("CodeGen for 'omp taskwait' is not supported yet."); 925 } 926 927 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { 928 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { 929 if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) { 930 auto FlushClause = cast<OMPFlushClause>(C); 931 return llvm::makeArrayRef(FlushClause->varlist_begin(), 932 FlushClause->varlist_end()); 933 } 934 return llvm::None; 935 }(), S.getLocStart()); 936 } 937 938 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &) { 939 llvm_unreachable("CodeGen for 'omp ordered' is not supported yet."); 940 } 941 942 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, 943 QualType SrcType, QualType DestType) { 944 assert(CGF.hasScalarEvaluationKind(DestType) && 945 "DestType must have scalar evaluation kind."); 946 assert(!Val.isAggregate() && "Must be a scalar or complex."); 947 return Val.isScalar() 948 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType) 949 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, 950 DestType); 951 } 952 953 static CodeGenFunction::ComplexPairTy 954 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, 955 QualType DestType) { 956 assert(CGF.getEvaluationKind(DestType) == TEK_Complex && 957 "DestType must have complex evaluation kind."); 958 CodeGenFunction::ComplexPairTy ComplexVal; 959 if (Val.isScalar()) { 960 // Convert the input element to the element type of the complex. 961 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 962 auto ScalarVal = 963 CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestElementType); 964 ComplexVal = CodeGenFunction::ComplexPairTy( 965 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); 966 } else { 967 assert(Val.isComplex() && "Must be a scalar or complex."); 968 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); 969 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 970 ComplexVal.first = CGF.EmitScalarConversion( 971 Val.getComplexVal().first, SrcElementType, DestElementType); 972 ComplexVal.second = CGF.EmitScalarConversion( 973 Val.getComplexVal().second, SrcElementType, DestElementType); 974 } 975 return ComplexVal; 976 } 977 978 static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, 979 const Expr *X, const Expr *V, 980 SourceLocation Loc) { 981 // v = x; 982 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); 983 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); 984 LValue XLValue = CGF.EmitLValue(X); 985 LValue VLValue = CGF.EmitLValue(V); 986 RValue Res = XLValue.isGlobalReg() 987 ? CGF.EmitLoadOfLValue(XLValue, Loc) 988 : CGF.EmitAtomicLoad(XLValue, Loc, 989 IsSeqCst ? llvm::SequentiallyConsistent 990 : llvm::Monotonic, 991 XLValue.isVolatile()); 992 // OpenMP, 2.12.6, atomic Construct 993 // Any atomic construct with a seq_cst clause forces the atomically 994 // performed operation to include an implicit flush operation without a 995 // list. 996 if (IsSeqCst) 997 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 998 switch (CGF.getEvaluationKind(V->getType())) { 999 case TEK_Scalar: 1000 CGF.EmitStoreOfScalar( 1001 convertToScalarValue(CGF, Res, X->getType(), V->getType()), VLValue); 1002 break; 1003 case TEK_Complex: 1004 CGF.EmitStoreOfComplex( 1005 convertToComplexValue(CGF, Res, X->getType(), V->getType()), VLValue, 1006 /*isInit=*/false); 1007 break; 1008 case TEK_Aggregate: 1009 llvm_unreachable("Must be a scalar or complex."); 1010 } 1011 } 1012 1013 static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, 1014 const Expr *X, const Expr *E, 1015 SourceLocation Loc) { 1016 // x = expr; 1017 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); 1018 LValue XLValue = CGF.EmitLValue(X); 1019 RValue ExprRValue = CGF.EmitAnyExpr(E); 1020 if (XLValue.isGlobalReg()) 1021 CGF.EmitStoreThroughGlobalRegLValue(ExprRValue, XLValue); 1022 else 1023 CGF.EmitAtomicStore(ExprRValue, XLValue, 1024 IsSeqCst ? llvm::SequentiallyConsistent 1025 : llvm::Monotonic, 1026 XLValue.isVolatile(), /*IsInit=*/false); 1027 // OpenMP, 2.12.6, atomic Construct 1028 // Any atomic construct with a seq_cst clause forces the atomically 1029 // performed operation to include an implicit flush operation without a 1030 // list. 1031 if (IsSeqCst) 1032 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 1033 } 1034 1035 static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, 1036 bool IsSeqCst, const Expr *X, const Expr *V, 1037 const Expr *E, SourceLocation Loc) { 1038 switch (Kind) { 1039 case OMPC_read: 1040 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); 1041 break; 1042 case OMPC_write: 1043 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); 1044 break; 1045 case OMPC_update: 1046 case OMPC_capture: 1047 llvm_unreachable("CodeGen for 'omp atomic clause' is not supported yet."); 1048 case OMPC_if: 1049 case OMPC_final: 1050 case OMPC_num_threads: 1051 case OMPC_private: 1052 case OMPC_firstprivate: 1053 case OMPC_lastprivate: 1054 case OMPC_reduction: 1055 case OMPC_safelen: 1056 case OMPC_collapse: 1057 case OMPC_default: 1058 case OMPC_seq_cst: 1059 case OMPC_shared: 1060 case OMPC_linear: 1061 case OMPC_aligned: 1062 case OMPC_copyin: 1063 case OMPC_copyprivate: 1064 case OMPC_flush: 1065 case OMPC_proc_bind: 1066 case OMPC_schedule: 1067 case OMPC_ordered: 1068 case OMPC_nowait: 1069 case OMPC_untied: 1070 case OMPC_threadprivate: 1071 case OMPC_mergeable: 1072 case OMPC_unknown: 1073 llvm_unreachable("Clause is not allowed in 'omp atomic'."); 1074 } 1075 } 1076 1077 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { 1078 bool IsSeqCst = S.getSingleClause(/*K=*/OMPC_seq_cst); 1079 OpenMPClauseKind Kind = OMPC_unknown; 1080 for (auto *C : S.clauses()) { 1081 // Find first clause (skip seq_cst clause, if it is first). 1082 if (C->getClauseKind() != OMPC_seq_cst) { 1083 Kind = C->getClauseKind(); 1084 break; 1085 } 1086 } 1087 1088 const auto *CS = 1089 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); 1090 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) 1091 enterFullExpression(EWC); 1092 InlinedOpenMPRegionScopeRAII Region(*this, S); 1093 1094 EmitOMPAtomicExpr(*this, Kind, IsSeqCst, S.getX(), S.getV(), S.getExpr(), 1095 S.getLocStart()); 1096 } 1097 1098 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) { 1099 llvm_unreachable("CodeGen for 'omp target' is not supported yet."); 1100 } 1101 1102 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) { 1103 llvm_unreachable("CodeGen for 'omp teams' is not supported yet."); 1104 } 1105 1106