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 "CGCleanup.h" 15 #include "CGOpenMPRuntime.h" 16 #include "CodeGenFunction.h" 17 #include "CodeGenModule.h" 18 #include "TargetInfo.h" 19 #include "clang/AST/Stmt.h" 20 #include "clang/AST/StmtOpenMP.h" 21 #include "clang/AST/DeclOpenMP.h" 22 #include "llvm/IR/CallSite.h" 23 using namespace clang; 24 using namespace CodeGen; 25 26 namespace { 27 /// Lexical scope for OpenMP executable constructs, that handles correct codegen 28 /// for captured expressions. 29 class OMPLexicalScope final : public CodeGenFunction::LexicalScope { 30 void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { 31 for (const auto *C : S.clauses()) { 32 if (auto *CPI = OMPClauseWithPreInit::get(C)) { 33 if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { 34 for (const auto *I : PreInit->decls()) { 35 if (!I->hasAttr<OMPCaptureNoInitAttr>()) 36 CGF.EmitVarDecl(cast<VarDecl>(*I)); 37 else { 38 CodeGenFunction::AutoVarEmission Emission = 39 CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); 40 CGF.EmitAutoVarCleanups(Emission); 41 } 42 } 43 } 44 } 45 } 46 } 47 CodeGenFunction::OMPPrivateScope InlinedShareds; 48 49 static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { 50 return CGF.LambdaCaptureFields.lookup(VD) || 51 (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || 52 (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl)); 53 } 54 55 public: 56 OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S, 57 bool AsInlined = false) 58 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), 59 InlinedShareds(CGF) { 60 emitPreInitStmt(CGF, S); 61 if (AsInlined) { 62 if (S.hasAssociatedStmt()) { 63 auto *CS = cast<CapturedStmt>(S.getAssociatedStmt()); 64 for (auto &C : CS->captures()) { 65 if (C.capturesVariable() || C.capturesVariableByCopy()) { 66 auto *VD = C.getCapturedVar(); 67 DeclRefExpr DRE(const_cast<VarDecl *>(VD), 68 isCapturedVar(CGF, VD) || 69 (CGF.CapturedStmtInfo && 70 InlinedShareds.isGlobalVarCaptured(VD)), 71 VD->getType().getNonReferenceType(), VK_LValue, 72 SourceLocation()); 73 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { 74 return CGF.EmitLValue(&DRE).getAddress(); 75 }); 76 } 77 } 78 (void)InlinedShareds.Privatize(); 79 } 80 } 81 } 82 }; 83 84 /// Private scope for OpenMP loop-based directives, that supports capturing 85 /// of used expression from loop statement. 86 class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { 87 void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { 88 if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) { 89 if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) { 90 for (const auto *I : PreInits->decls()) 91 CGF.EmitVarDecl(cast<VarDecl>(*I)); 92 } 93 } 94 } 95 96 public: 97 OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) 98 : CodeGenFunction::RunCleanupsScope(CGF) { 99 emitPreInitStmt(CGF, S); 100 } 101 }; 102 103 } // namespace 104 105 llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { 106 auto &C = getContext(); 107 llvm::Value *Size = nullptr; 108 auto SizeInChars = C.getTypeSizeInChars(Ty); 109 if (SizeInChars.isZero()) { 110 // getTypeSizeInChars() returns 0 for a VLA. 111 while (auto *VAT = C.getAsVariableArrayType(Ty)) { 112 llvm::Value *ArraySize; 113 std::tie(ArraySize, Ty) = getVLASize(VAT); 114 Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize; 115 } 116 SizeInChars = C.getTypeSizeInChars(Ty); 117 if (SizeInChars.isZero()) 118 return llvm::ConstantInt::get(SizeTy, /*V=*/0); 119 Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); 120 } else 121 Size = CGM.getSize(SizeInChars); 122 return Size; 123 } 124 125 void CodeGenFunction::GenerateOpenMPCapturedVars( 126 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { 127 const RecordDecl *RD = S.getCapturedRecordDecl(); 128 auto CurField = RD->field_begin(); 129 auto CurCap = S.captures().begin(); 130 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), 131 E = S.capture_init_end(); 132 I != E; ++I, ++CurField, ++CurCap) { 133 if (CurField->hasCapturedVLAType()) { 134 auto VAT = CurField->getCapturedVLAType(); 135 auto *Val = VLASizeMap[VAT->getSizeExpr()]; 136 CapturedVars.push_back(Val); 137 } else if (CurCap->capturesThis()) 138 CapturedVars.push_back(CXXThisValue); 139 else if (CurCap->capturesVariableByCopy()) 140 CapturedVars.push_back( 141 EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal()); 142 else { 143 assert(CurCap->capturesVariable() && "Expected capture by reference."); 144 CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); 145 } 146 } 147 } 148 149 static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType, 150 StringRef Name, LValue AddrLV, 151 bool isReferenceType = false) { 152 ASTContext &Ctx = CGF.getContext(); 153 154 auto *CastedPtr = CGF.EmitScalarConversion( 155 AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), 156 Ctx.getPointerType(DstType), SourceLocation()); 157 auto TmpAddr = 158 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) 159 .getAddress(); 160 161 // If we are dealing with references we need to return the address of the 162 // reference instead of the reference of the value. 163 if (isReferenceType) { 164 QualType RefType = Ctx.getLValueReferenceType(DstType); 165 auto *RefVal = TmpAddr.getPointer(); 166 TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref"); 167 auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); 168 CGF.EmitScalarInit(RefVal, TmpLVal); 169 } 170 171 return TmpAddr; 172 } 173 174 llvm::Function * 175 CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { 176 assert( 177 CapturedStmtInfo && 178 "CapturedStmtInfo should be set when generating the captured function"); 179 const CapturedDecl *CD = S.getCapturedDecl(); 180 const RecordDecl *RD = S.getCapturedRecordDecl(); 181 assert(CD->hasBody() && "missing CapturedDecl body"); 182 183 // Build the argument list. 184 ASTContext &Ctx = CGM.getContext(); 185 FunctionArgList Args; 186 Args.append(CD->param_begin(), 187 std::next(CD->param_begin(), CD->getContextParamPosition())); 188 auto I = S.captures().begin(); 189 for (auto *FD : RD->fields()) { 190 QualType ArgType = FD->getType(); 191 IdentifierInfo *II = nullptr; 192 VarDecl *CapVar = nullptr; 193 194 // If this is a capture by copy and the type is not a pointer, the outlined 195 // function argument type should be uintptr and the value properly casted to 196 // uintptr. This is necessary given that the runtime library is only able to 197 // deal with pointers. We can pass in the same way the VLA type sizes to the 198 // outlined function. 199 if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || 200 I->capturesVariableArrayType()) 201 ArgType = Ctx.getUIntPtrType(); 202 203 if (I->capturesVariable() || I->capturesVariableByCopy()) { 204 CapVar = I->getCapturedVar(); 205 II = CapVar->getIdentifier(); 206 } else if (I->capturesThis()) 207 II = &getContext().Idents.get("this"); 208 else { 209 assert(I->capturesVariableArrayType()); 210 II = &getContext().Idents.get("vla"); 211 } 212 if (ArgType->isVariablyModifiedType()) 213 ArgType = getContext().getVariableArrayDecayedType(ArgType); 214 Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr, 215 FD->getLocation(), II, ArgType)); 216 ++I; 217 } 218 Args.append( 219 std::next(CD->param_begin(), CD->getContextParamPosition() + 1), 220 CD->param_end()); 221 222 // Create the function declaration. 223 FunctionType::ExtInfo ExtInfo; 224 const CGFunctionInfo &FuncInfo = 225 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args); 226 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); 227 228 llvm::Function *F = llvm::Function::Create( 229 FuncLLVMTy, llvm::GlobalValue::InternalLinkage, 230 CapturedStmtInfo->getHelperName(), &CGM.getModule()); 231 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); 232 if (CD->isNothrow()) 233 F->addFnAttr(llvm::Attribute::NoUnwind); 234 235 // Generate the function. 236 StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(), 237 CD->getBody()->getLocStart()); 238 unsigned Cnt = CD->getContextParamPosition(); 239 I = S.captures().begin(); 240 for (auto *FD : RD->fields()) { 241 // If we are capturing a pointer by copy we don't need to do anything, just 242 // use the value that we get from the arguments. 243 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { 244 setAddrOfLocalVar(I->getCapturedVar(), GetAddrOfLocalVar(Args[Cnt])); 245 ++Cnt; 246 ++I; 247 continue; 248 } 249 250 LValue ArgLVal = 251 MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(), 252 AlignmentSource::Decl); 253 if (FD->hasCapturedVLAType()) { 254 LValue CastedArgLVal = 255 MakeAddrLValue(castValueFromUintptr(*this, FD->getType(), 256 Args[Cnt]->getName(), ArgLVal), 257 FD->getType(), AlignmentSource::Decl); 258 auto *ExprArg = 259 EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal(); 260 auto VAT = FD->getCapturedVLAType(); 261 VLASizeMap[VAT->getSizeExpr()] = ExprArg; 262 } else if (I->capturesVariable()) { 263 auto *Var = I->getCapturedVar(); 264 QualType VarTy = Var->getType(); 265 Address ArgAddr = ArgLVal.getAddress(); 266 if (!VarTy->isReferenceType()) { 267 ArgAddr = EmitLoadOfReference( 268 ArgAddr, ArgLVal.getType()->castAs<ReferenceType>()); 269 } 270 setAddrOfLocalVar( 271 Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var))); 272 } else if (I->capturesVariableByCopy()) { 273 assert(!FD->getType()->isAnyPointerType() && 274 "Not expecting a captured pointer."); 275 auto *Var = I->getCapturedVar(); 276 QualType VarTy = Var->getType(); 277 setAddrOfLocalVar(I->getCapturedVar(), 278 castValueFromUintptr(*this, FD->getType(), 279 Args[Cnt]->getName(), ArgLVal, 280 VarTy->isReferenceType())); 281 } else { 282 // If 'this' is captured, load it into CXXThisValue. 283 assert(I->capturesThis()); 284 CXXThisValue = 285 EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal(); 286 } 287 ++Cnt; 288 ++I; 289 } 290 291 PGO.assignRegionCounters(GlobalDecl(CD), F); 292 CapturedStmtInfo->EmitBody(*this, CD->getBody()); 293 FinishFunction(CD->getBodyRBrace()); 294 295 return F; 296 } 297 298 //===----------------------------------------------------------------------===// 299 // OpenMP Directive Emission 300 //===----------------------------------------------------------------------===// 301 void CodeGenFunction::EmitOMPAggregateAssign( 302 Address DestAddr, Address SrcAddr, QualType OriginalType, 303 const llvm::function_ref<void(Address, Address)> &CopyGen) { 304 // Perform element-by-element initialization. 305 QualType ElementTy; 306 307 // Drill down to the base element type on both arrays. 308 auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); 309 auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); 310 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); 311 312 auto SrcBegin = SrcAddr.getPointer(); 313 auto DestBegin = DestAddr.getPointer(); 314 // Cast from pointer to array type to pointer to single element. 315 auto DestEnd = Builder.CreateGEP(DestBegin, NumElements); 316 // The basic structure here is a while-do loop. 317 auto BodyBB = createBasicBlock("omp.arraycpy.body"); 318 auto DoneBB = createBasicBlock("omp.arraycpy.done"); 319 auto IsEmpty = 320 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); 321 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); 322 323 // Enter the loop body, making that address the current address. 324 auto EntryBB = Builder.GetInsertBlock(); 325 EmitBlock(BodyBB); 326 327 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); 328 329 llvm::PHINode *SrcElementPHI = 330 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); 331 SrcElementPHI->addIncoming(SrcBegin, EntryBB); 332 Address SrcElementCurrent = 333 Address(SrcElementPHI, 334 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 335 336 llvm::PHINode *DestElementPHI = 337 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); 338 DestElementPHI->addIncoming(DestBegin, EntryBB); 339 Address DestElementCurrent = 340 Address(DestElementPHI, 341 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 342 343 // Emit copy. 344 CopyGen(DestElementCurrent, SrcElementCurrent); 345 346 // Shift the address forward by one element. 347 auto DestElementNext = Builder.CreateConstGEP1_32( 348 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); 349 auto SrcElementNext = Builder.CreateConstGEP1_32( 350 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); 351 // Check whether we've reached the end. 352 auto Done = 353 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); 354 Builder.CreateCondBr(Done, DoneBB, BodyBB); 355 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); 356 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); 357 358 // Done. 359 EmitBlock(DoneBB, /*IsFinished=*/true); 360 } 361 362 /// Check if the combiner is a call to UDR combiner and if it is so return the 363 /// UDR decl used for reduction. 364 static const OMPDeclareReductionDecl * 365 getReductionInit(const Expr *ReductionOp) { 366 if (auto *CE = dyn_cast<CallExpr>(ReductionOp)) 367 if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) 368 if (auto *DRE = 369 dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) 370 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) 371 return DRD; 372 return nullptr; 373 } 374 375 static void emitInitWithReductionInitializer(CodeGenFunction &CGF, 376 const OMPDeclareReductionDecl *DRD, 377 const Expr *InitOp, 378 Address Private, Address Original, 379 QualType Ty) { 380 if (DRD->getInitializer()) { 381 std::pair<llvm::Function *, llvm::Function *> Reduction = 382 CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); 383 auto *CE = cast<CallExpr>(InitOp); 384 auto *OVE = cast<OpaqueValueExpr>(CE->getCallee()); 385 const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); 386 const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); 387 auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr()); 388 auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr()); 389 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 390 PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), 391 [=]() -> Address { return Private; }); 392 PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), 393 [=]() -> Address { return Original; }); 394 (void)PrivateScope.Privatize(); 395 RValue Func = RValue::get(Reduction.second); 396 CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); 397 CGF.EmitIgnoredExpr(InitOp); 398 } else { 399 llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty); 400 auto *GV = new llvm::GlobalVariable( 401 CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, 402 llvm::GlobalValue::PrivateLinkage, Init, ".init"); 403 LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); 404 RValue InitRVal; 405 switch (CGF.getEvaluationKind(Ty)) { 406 case TEK_Scalar: 407 InitRVal = CGF.EmitLoadOfLValue(LV, SourceLocation()); 408 break; 409 case TEK_Complex: 410 InitRVal = 411 RValue::getComplex(CGF.EmitLoadOfComplex(LV, SourceLocation())); 412 break; 413 case TEK_Aggregate: 414 InitRVal = RValue::getAggregate(LV.getAddress()); 415 break; 416 } 417 OpaqueValueExpr OVE(SourceLocation(), Ty, VK_RValue); 418 CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); 419 CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), 420 /*IsInitializer=*/false); 421 } 422 } 423 424 /// \brief Emit initialization of arrays of complex types. 425 /// \param DestAddr Address of the array. 426 /// \param Type Type of array. 427 /// \param Init Initial expression of array. 428 /// \param SrcAddr Address of the original array. 429 static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, 430 QualType Type, const Expr *Init, 431 Address SrcAddr = Address::invalid()) { 432 auto *DRD = getReductionInit(Init); 433 // Perform element-by-element initialization. 434 QualType ElementTy; 435 436 // Drill down to the base element type on both arrays. 437 auto ArrayTy = Type->getAsArrayTypeUnsafe(); 438 auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); 439 DestAddr = 440 CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); 441 if (DRD) 442 SrcAddr = 443 CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); 444 445 llvm::Value *SrcBegin = nullptr; 446 if (DRD) 447 SrcBegin = SrcAddr.getPointer(); 448 auto DestBegin = DestAddr.getPointer(); 449 // Cast from pointer to array type to pointer to single element. 450 auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); 451 // The basic structure here is a while-do loop. 452 auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); 453 auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); 454 auto IsEmpty = 455 CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); 456 CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); 457 458 // Enter the loop body, making that address the current address. 459 auto EntryBB = CGF.Builder.GetInsertBlock(); 460 CGF.EmitBlock(BodyBB); 461 462 CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); 463 464 llvm::PHINode *SrcElementPHI = nullptr; 465 Address SrcElementCurrent = Address::invalid(); 466 if (DRD) { 467 SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, 468 "omp.arraycpy.srcElementPast"); 469 SrcElementPHI->addIncoming(SrcBegin, EntryBB); 470 SrcElementCurrent = 471 Address(SrcElementPHI, 472 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 473 } 474 llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( 475 DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); 476 DestElementPHI->addIncoming(DestBegin, EntryBB); 477 Address DestElementCurrent = 478 Address(DestElementPHI, 479 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 480 481 // Emit copy. 482 { 483 CodeGenFunction::RunCleanupsScope InitScope(CGF); 484 if (DRD && (DRD->getInitializer() || !Init)) { 485 emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, 486 SrcElementCurrent, ElementTy); 487 } else 488 CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), 489 /*IsInitializer=*/false); 490 } 491 492 if (DRD) { 493 // Shift the address forward by one element. 494 auto SrcElementNext = CGF.Builder.CreateConstGEP1_32( 495 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); 496 SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); 497 } 498 499 // Shift the address forward by one element. 500 auto DestElementNext = CGF.Builder.CreateConstGEP1_32( 501 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); 502 // Check whether we've reached the end. 503 auto Done = 504 CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); 505 CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); 506 DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); 507 508 // Done. 509 CGF.EmitBlock(DoneBB, /*IsFinished=*/true); 510 } 511 512 void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, 513 Address SrcAddr, const VarDecl *DestVD, 514 const VarDecl *SrcVD, const Expr *Copy) { 515 if (OriginalType->isArrayType()) { 516 auto *BO = dyn_cast<BinaryOperator>(Copy); 517 if (BO && BO->getOpcode() == BO_Assign) { 518 // Perform simple memcpy for simple copying. 519 EmitAggregateAssign(DestAddr, SrcAddr, OriginalType); 520 } else { 521 // For arrays with complex element types perform element by element 522 // copying. 523 EmitOMPAggregateAssign( 524 DestAddr, SrcAddr, OriginalType, 525 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { 526 // Working with the single array element, so have to remap 527 // destination and source variables to corresponding array 528 // elements. 529 CodeGenFunction::OMPPrivateScope Remap(*this); 530 Remap.addPrivate(DestVD, [DestElement]() -> Address { 531 return DestElement; 532 }); 533 Remap.addPrivate( 534 SrcVD, [SrcElement]() -> Address { return SrcElement; }); 535 (void)Remap.Privatize(); 536 EmitIgnoredExpr(Copy); 537 }); 538 } 539 } else { 540 // Remap pseudo source variable to private copy. 541 CodeGenFunction::OMPPrivateScope Remap(*this); 542 Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; }); 543 Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; }); 544 (void)Remap.Privatize(); 545 // Emit copying of the whole variable. 546 EmitIgnoredExpr(Copy); 547 } 548 } 549 550 bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, 551 OMPPrivateScope &PrivateScope) { 552 if (!HaveInsertPoint()) 553 return false; 554 bool FirstprivateIsLastprivate = false; 555 llvm::DenseSet<const VarDecl *> Lastprivates; 556 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 557 for (const auto *D : C->varlists()) 558 Lastprivates.insert( 559 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); 560 } 561 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; 562 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { 563 auto IRef = C->varlist_begin(); 564 auto InitsRef = C->inits().begin(); 565 for (auto IInit : C->private_copies()) { 566 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 567 FirstprivateIsLastprivate = 568 FirstprivateIsLastprivate || 569 (Lastprivates.count(OrigVD->getCanonicalDecl()) > 0); 570 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { 571 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 572 auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); 573 bool IsRegistered; 574 DeclRefExpr DRE( 575 const_cast<VarDecl *>(OrigVD), 576 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( 577 OrigVD) != nullptr, 578 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 579 Address OriginalAddr = EmitLValue(&DRE).getAddress(); 580 QualType Type = VD->getType(); 581 if (Type->isArrayType()) { 582 // Emit VarDecl with copy init for arrays. 583 // Get the address of the original variable captured in current 584 // captured region. 585 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { 586 auto Emission = EmitAutoVarAlloca(*VD); 587 auto *Init = VD->getInit(); 588 if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) { 589 // Perform simple memcpy. 590 EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr, 591 Type); 592 } else { 593 EmitOMPAggregateAssign( 594 Emission.getAllocatedAddress(), OriginalAddr, Type, 595 [this, VDInit, Init](Address DestElement, 596 Address SrcElement) { 597 // Clean up any temporaries needed by the initialization. 598 RunCleanupsScope InitScope(*this); 599 // Emit initialization for single element. 600 setAddrOfLocalVar(VDInit, SrcElement); 601 EmitAnyExprToMem(Init, DestElement, 602 Init->getType().getQualifiers(), 603 /*IsInitializer*/ false); 604 LocalDeclMap.erase(VDInit); 605 }); 606 } 607 EmitAutoVarCleanups(Emission); 608 return Emission.getAllocatedAddress(); 609 }); 610 } else { 611 IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { 612 // Emit private VarDecl with copy init. 613 // Remap temp VDInit variable to the address of the original 614 // variable 615 // (for proper handling of captured global variables). 616 setAddrOfLocalVar(VDInit, OriginalAddr); 617 EmitDecl(*VD); 618 LocalDeclMap.erase(VDInit); 619 return GetAddrOfLocalVar(VD); 620 }); 621 } 622 assert(IsRegistered && 623 "firstprivate var already registered as private"); 624 // Silence the warning about unused variable. 625 (void)IsRegistered; 626 } 627 ++IRef; 628 ++InitsRef; 629 } 630 } 631 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); 632 } 633 634 void CodeGenFunction::EmitOMPPrivateClause( 635 const OMPExecutableDirective &D, 636 CodeGenFunction::OMPPrivateScope &PrivateScope) { 637 if (!HaveInsertPoint()) 638 return; 639 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 640 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { 641 auto IRef = C->varlist_begin(); 642 for (auto IInit : C->private_copies()) { 643 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 644 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 645 auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 646 bool IsRegistered = 647 PrivateScope.addPrivate(OrigVD, [&]() -> Address { 648 // Emit private VarDecl with copy init. 649 EmitDecl(*VD); 650 return GetAddrOfLocalVar(VD); 651 }); 652 assert(IsRegistered && "private var already registered as private"); 653 // Silence the warning about unused variable. 654 (void)IsRegistered; 655 } 656 ++IRef; 657 } 658 } 659 } 660 661 bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { 662 if (!HaveInsertPoint()) 663 return false; 664 // threadprivate_var1 = master_threadprivate_var1; 665 // operator=(threadprivate_var2, master_threadprivate_var2); 666 // ... 667 // __kmpc_barrier(&loc, global_tid); 668 llvm::DenseSet<const VarDecl *> CopiedVars; 669 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; 670 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { 671 auto IRef = C->varlist_begin(); 672 auto ISrcRef = C->source_exprs().begin(); 673 auto IDestRef = C->destination_exprs().begin(); 674 for (auto *AssignOp : C->assignment_ops()) { 675 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 676 QualType Type = VD->getType(); 677 if (CopiedVars.insert(VD->getCanonicalDecl()).second) { 678 // Get the address of the master variable. If we are emitting code with 679 // TLS support, the address is passed from the master as field in the 680 // captured declaration. 681 Address MasterAddr = Address::invalid(); 682 if (getLangOpts().OpenMPUseTLS && 683 getContext().getTargetInfo().isTLSSupported()) { 684 assert(CapturedStmtInfo->lookup(VD) && 685 "Copyin threadprivates should have been captured!"); 686 DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), 687 VK_LValue, (*IRef)->getExprLoc()); 688 MasterAddr = EmitLValue(&DRE).getAddress(); 689 LocalDeclMap.erase(VD); 690 } else { 691 MasterAddr = 692 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) 693 : CGM.GetAddrOfGlobal(VD), 694 getContext().getDeclAlign(VD)); 695 } 696 // Get the address of the threadprivate variable. 697 Address PrivateAddr = EmitLValue(*IRef).getAddress(); 698 if (CopiedVars.size() == 1) { 699 // At first check if current thread is a master thread. If it is, no 700 // need to copy data. 701 CopyBegin = createBasicBlock("copyin.not.master"); 702 CopyEnd = createBasicBlock("copyin.not.master.end"); 703 Builder.CreateCondBr( 704 Builder.CreateICmpNE( 705 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), 706 Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)), 707 CopyBegin, CopyEnd); 708 EmitBlock(CopyBegin); 709 } 710 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); 711 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 712 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); 713 } 714 ++IRef; 715 ++ISrcRef; 716 ++IDestRef; 717 } 718 } 719 if (CopyEnd) { 720 // Exit out of copying procedure for non-master thread. 721 EmitBlock(CopyEnd, /*IsFinished=*/true); 722 return true; 723 } 724 return false; 725 } 726 727 bool CodeGenFunction::EmitOMPLastprivateClauseInit( 728 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { 729 if (!HaveInsertPoint()) 730 return false; 731 bool HasAtLeastOneLastprivate = false; 732 llvm::DenseSet<const VarDecl *> SIMDLCVs; 733 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 734 auto *LoopDirective = cast<OMPLoopDirective>(&D); 735 for (auto *C : LoopDirective->counters()) { 736 SIMDLCVs.insert( 737 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); 738 } 739 } 740 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; 741 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 742 HasAtLeastOneLastprivate = true; 743 auto IRef = C->varlist_begin(); 744 auto IDestRef = C->destination_exprs().begin(); 745 for (auto *IInit : C->private_copies()) { 746 // Keep the address of the original variable for future update at the end 747 // of the loop. 748 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 749 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { 750 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 751 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address { 752 DeclRefExpr DRE( 753 const_cast<VarDecl *>(OrigVD), 754 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( 755 OrigVD) != nullptr, 756 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 757 return EmitLValue(&DRE).getAddress(); 758 }); 759 // Check if the variable is also a firstprivate: in this case IInit is 760 // not generated. Initialization of this variable will happen in codegen 761 // for 'firstprivate' clause. 762 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { 763 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 764 bool IsRegistered = 765 PrivateScope.addPrivate(OrigVD, [&]() -> Address { 766 // Emit private VarDecl with copy init. 767 EmitDecl(*VD); 768 return GetAddrOfLocalVar(VD); 769 }); 770 assert(IsRegistered && 771 "lastprivate var already registered as private"); 772 (void)IsRegistered; 773 } 774 } 775 ++IRef; 776 ++IDestRef; 777 } 778 } 779 return HasAtLeastOneLastprivate; 780 } 781 782 void CodeGenFunction::EmitOMPLastprivateClauseFinal( 783 const OMPExecutableDirective &D, bool NoFinals, 784 llvm::Value *IsLastIterCond) { 785 if (!HaveInsertPoint()) 786 return; 787 // Emit following code: 788 // if (<IsLastIterCond>) { 789 // orig_var1 = private_orig_var1; 790 // ... 791 // orig_varn = private_orig_varn; 792 // } 793 llvm::BasicBlock *ThenBB = nullptr; 794 llvm::BasicBlock *DoneBB = nullptr; 795 if (IsLastIterCond) { 796 ThenBB = createBasicBlock(".omp.lastprivate.then"); 797 DoneBB = createBasicBlock(".omp.lastprivate.done"); 798 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); 799 EmitBlock(ThenBB); 800 } 801 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; 802 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; 803 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { 804 auto IC = LoopDirective->counters().begin(); 805 for (auto F : LoopDirective->finals()) { 806 auto *D = 807 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); 808 if (NoFinals) 809 AlreadyEmittedVars.insert(D); 810 else 811 LoopCountersAndUpdates[D] = F; 812 ++IC; 813 } 814 } 815 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 816 auto IRef = C->varlist_begin(); 817 auto ISrcRef = C->source_exprs().begin(); 818 auto IDestRef = C->destination_exprs().begin(); 819 for (auto *AssignOp : C->assignment_ops()) { 820 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 821 QualType Type = PrivateVD->getType(); 822 auto *CanonicalVD = PrivateVD->getCanonicalDecl(); 823 if (AlreadyEmittedVars.insert(CanonicalVD).second) { 824 // If lastprivate variable is a loop control variable for loop-based 825 // directive, update its value before copyin back to original 826 // variable. 827 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) 828 EmitIgnoredExpr(FinalExpr); 829 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); 830 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 831 // Get the address of the original variable. 832 Address OriginalAddr = GetAddrOfLocalVar(DestVD); 833 // Get the address of the private variable. 834 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); 835 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>()) 836 PrivateAddr = 837 Address(Builder.CreateLoad(PrivateAddr), 838 getNaturalTypeAlignment(RefTy->getPointeeType())); 839 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); 840 } 841 ++IRef; 842 ++ISrcRef; 843 ++IDestRef; 844 } 845 if (auto *PostUpdate = C->getPostUpdateExpr()) 846 EmitIgnoredExpr(PostUpdate); 847 } 848 if (IsLastIterCond) 849 EmitBlock(DoneBB, /*IsFinished=*/true); 850 } 851 852 static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, 853 LValue BaseLV, llvm::Value *Addr) { 854 Address Tmp = Address::invalid(); 855 Address TopTmp = Address::invalid(); 856 Address MostTopTmp = Address::invalid(); 857 BaseTy = BaseTy.getNonReferenceType(); 858 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && 859 !CGF.getContext().hasSameType(BaseTy, ElTy)) { 860 Tmp = CGF.CreateMemTemp(BaseTy); 861 if (TopTmp.isValid()) 862 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); 863 else 864 MostTopTmp = Tmp; 865 TopTmp = Tmp; 866 BaseTy = BaseTy->getPointeeType(); 867 } 868 llvm::Type *Ty = BaseLV.getPointer()->getType(); 869 if (Tmp.isValid()) 870 Ty = Tmp.getElementType(); 871 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); 872 if (Tmp.isValid()) { 873 CGF.Builder.CreateStore(Addr, Tmp); 874 return MostTopTmp; 875 } 876 return Address(Addr, BaseLV.getAlignment()); 877 } 878 879 static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, 880 LValue BaseLV) { 881 BaseTy = BaseTy.getNonReferenceType(); 882 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && 883 !CGF.getContext().hasSameType(BaseTy, ElTy)) { 884 if (auto *PtrTy = BaseTy->getAs<PointerType>()) 885 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); 886 else { 887 BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(), 888 BaseTy->castAs<ReferenceType>()); 889 } 890 BaseTy = BaseTy->getPointeeType(); 891 } 892 return CGF.MakeAddrLValue( 893 Address( 894 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( 895 BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()), 896 BaseLV.getAlignment()), 897 BaseLV.getType(), BaseLV.getAlignmentSource()); 898 } 899 900 void CodeGenFunction::EmitOMPReductionClauseInit( 901 const OMPExecutableDirective &D, 902 CodeGenFunction::OMPPrivateScope &PrivateScope) { 903 if (!HaveInsertPoint()) 904 return; 905 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 906 auto ILHS = C->lhs_exprs().begin(); 907 auto IRHS = C->rhs_exprs().begin(); 908 auto IPriv = C->privates().begin(); 909 auto IRed = C->reduction_ops().begin(); 910 for (auto IRef : C->varlists()) { 911 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); 912 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); 913 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); 914 auto *DRD = getReductionInit(*IRed); 915 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) { 916 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 917 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 918 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 919 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 920 Base = TempASE->getBase()->IgnoreParenImpCasts(); 921 auto *DE = cast<DeclRefExpr>(Base); 922 auto *OrigVD = cast<VarDecl>(DE->getDecl()); 923 auto OASELValueLB = EmitOMPArraySectionExpr(OASE); 924 auto OASELValueUB = 925 EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); 926 auto OriginalBaseLValue = EmitLValue(DE); 927 LValue BaseLValue = 928 loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(), 929 OriginalBaseLValue); 930 // Store the address of the original variable associated with the LHS 931 // implicit variable. 932 PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address { 933 return OASELValueLB.getAddress(); 934 }); 935 // Emit reduction copy. 936 bool IsRegistered = PrivateScope.addPrivate( 937 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB, 938 OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address { 939 // Emit VarDecl with copy init for arrays. 940 // Get the address of the original variable captured in current 941 // captured region. 942 auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(), 943 OASELValueLB.getPointer()); 944 Size = Builder.CreateNUWAdd( 945 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); 946 CodeGenFunction::OpaqueValueMapping OpaqueMap( 947 *this, cast<OpaqueValueExpr>( 948 getContext() 949 .getAsVariableArrayType(PrivateVD->getType()) 950 ->getSizeExpr()), 951 RValue::get(Size)); 952 EmitVariablyModifiedType(PrivateVD->getType()); 953 auto Emission = EmitAutoVarAlloca(*PrivateVD); 954 auto Addr = Emission.getAllocatedAddress(); 955 auto *Init = PrivateVD->getInit(); 956 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), 957 DRD ? *IRed : Init, 958 OASELValueLB.getAddress()); 959 EmitAutoVarCleanups(Emission); 960 // Emit private VarDecl with reduction init. 961 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), 962 OASELValueLB.getPointer()); 963 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); 964 return castToBase(*this, OrigVD->getType(), 965 OASELValueLB.getType(), OriginalBaseLValue, 966 Ptr); 967 }); 968 assert(IsRegistered && "private var already registered as private"); 969 // Silence the warning about unused variable. 970 (void)IsRegistered; 971 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { 972 return GetAddrOfLocalVar(PrivateVD); 973 }); 974 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) { 975 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 976 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 977 Base = TempASE->getBase()->IgnoreParenImpCasts(); 978 auto *DE = cast<DeclRefExpr>(Base); 979 auto *OrigVD = cast<VarDecl>(DE->getDecl()); 980 auto ASELValue = EmitLValue(ASE); 981 auto OriginalBaseLValue = EmitLValue(DE); 982 LValue BaseLValue = loadToBegin( 983 *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue); 984 // Store the address of the original variable associated with the LHS 985 // implicit variable. 986 PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address { 987 return ASELValue.getAddress(); 988 }); 989 // Emit reduction copy. 990 bool IsRegistered = PrivateScope.addPrivate( 991 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue, 992 OriginalBaseLValue, DRD, IRed]() -> Address { 993 // Emit private VarDecl with reduction init. 994 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); 995 auto Addr = Emission.getAllocatedAddress(); 996 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { 997 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, 998 ASELValue.getAddress(), 999 ASELValue.getType()); 1000 } else 1001 EmitAutoVarInit(Emission); 1002 EmitAutoVarCleanups(Emission); 1003 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), 1004 ASELValue.getPointer()); 1005 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); 1006 return castToBase(*this, OrigVD->getType(), ASELValue.getType(), 1007 OriginalBaseLValue, Ptr); 1008 }); 1009 assert(IsRegistered && "private var already registered as private"); 1010 // Silence the warning about unused variable. 1011 (void)IsRegistered; 1012 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { 1013 return Builder.CreateElementBitCast( 1014 GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()), 1015 "rhs.begin"); 1016 }); 1017 } else { 1018 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); 1019 QualType Type = PrivateVD->getType(); 1020 if (getContext().getAsArrayType(Type)) { 1021 // Store the address of the original variable associated with the LHS 1022 // implicit variable. 1023 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1024 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1025 IRef->getType(), VK_LValue, IRef->getExprLoc()); 1026 Address OriginalAddr = EmitLValue(&DRE).getAddress(); 1027 PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr, 1028 LHSVD]() -> Address { 1029 OriginalAddr = Builder.CreateElementBitCast( 1030 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); 1031 return OriginalAddr; 1032 }); 1033 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { 1034 if (Type->isVariablyModifiedType()) { 1035 CodeGenFunction::OpaqueValueMapping OpaqueMap( 1036 *this, cast<OpaqueValueExpr>( 1037 getContext() 1038 .getAsVariableArrayType(PrivateVD->getType()) 1039 ->getSizeExpr()), 1040 RValue::get( 1041 getTypeSize(OrigVD->getType().getNonReferenceType()))); 1042 EmitVariablyModifiedType(Type); 1043 } 1044 auto Emission = EmitAutoVarAlloca(*PrivateVD); 1045 auto Addr = Emission.getAllocatedAddress(); 1046 auto *Init = PrivateVD->getInit(); 1047 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), 1048 DRD ? *IRed : Init, OriginalAddr); 1049 EmitAutoVarCleanups(Emission); 1050 return Emission.getAllocatedAddress(); 1051 }); 1052 assert(IsRegistered && "private var already registered as private"); 1053 // Silence the warning about unused variable. 1054 (void)IsRegistered; 1055 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { 1056 return Builder.CreateElementBitCast( 1057 GetAddrOfLocalVar(PrivateVD), 1058 ConvertTypeForMem(RHSVD->getType()), "rhs.begin"); 1059 }); 1060 } else { 1061 // Store the address of the original variable associated with the LHS 1062 // implicit variable. 1063 Address OriginalAddr = Address::invalid(); 1064 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef, 1065 &OriginalAddr]() -> Address { 1066 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1067 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1068 IRef->getType(), VK_LValue, IRef->getExprLoc()); 1069 OriginalAddr = EmitLValue(&DRE).getAddress(); 1070 return OriginalAddr; 1071 }); 1072 // Emit reduction copy. 1073 bool IsRegistered = PrivateScope.addPrivate( 1074 OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address { 1075 // Emit private VarDecl with reduction init. 1076 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); 1077 auto Addr = Emission.getAllocatedAddress(); 1078 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { 1079 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, 1080 OriginalAddr, 1081 PrivateVD->getType()); 1082 } else 1083 EmitAutoVarInit(Emission); 1084 EmitAutoVarCleanups(Emission); 1085 return Addr; 1086 }); 1087 assert(IsRegistered && "private var already registered as private"); 1088 // Silence the warning about unused variable. 1089 (void)IsRegistered; 1090 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { 1091 return GetAddrOfLocalVar(PrivateVD); 1092 }); 1093 } 1094 } 1095 ++ILHS; 1096 ++IRHS; 1097 ++IPriv; 1098 ++IRed; 1099 } 1100 } 1101 } 1102 1103 void CodeGenFunction::EmitOMPReductionClauseFinal( 1104 const OMPExecutableDirective &D) { 1105 if (!HaveInsertPoint()) 1106 return; 1107 llvm::SmallVector<const Expr *, 8> Privates; 1108 llvm::SmallVector<const Expr *, 8> LHSExprs; 1109 llvm::SmallVector<const Expr *, 8> RHSExprs; 1110 llvm::SmallVector<const Expr *, 8> ReductionOps; 1111 bool HasAtLeastOneReduction = false; 1112 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1113 HasAtLeastOneReduction = true; 1114 Privates.append(C->privates().begin(), C->privates().end()); 1115 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 1116 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 1117 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); 1118 } 1119 if (HasAtLeastOneReduction) { 1120 // Emit nowait reduction if nowait clause is present or directive is a 1121 // parallel directive (it always has implicit barrier). 1122 CGM.getOpenMPRuntime().emitReduction( 1123 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, 1124 D.getSingleClause<OMPNowaitClause>() || 1125 isOpenMPParallelDirective(D.getDirectiveKind()) || 1126 D.getDirectiveKind() == OMPD_simd, 1127 D.getDirectiveKind() == OMPD_simd); 1128 } 1129 } 1130 1131 static void emitPostUpdateForReductionClause( 1132 CodeGenFunction &CGF, const OMPExecutableDirective &D, 1133 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1134 if (!CGF.HaveInsertPoint()) 1135 return; 1136 llvm::BasicBlock *DoneBB = nullptr; 1137 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1138 if (auto *PostUpdate = C->getPostUpdateExpr()) { 1139 if (!DoneBB) { 1140 if (auto *Cond = CondGen(CGF)) { 1141 // If the first post-update expression is found, emit conditional 1142 // block if it was requested. 1143 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); 1144 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); 1145 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1146 CGF.EmitBlock(ThenBB); 1147 } 1148 } 1149 CGF.EmitIgnoredExpr(PostUpdate); 1150 } 1151 } 1152 if (DoneBB) 1153 CGF.EmitBlock(DoneBB, /*IsFinished=*/true); 1154 } 1155 1156 static void emitCommonOMPParallelDirective(CodeGenFunction &CGF, 1157 const OMPExecutableDirective &S, 1158 OpenMPDirectiveKind InnermostKind, 1159 const RegionCodeGenTy &CodeGen) { 1160 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 1161 auto OutlinedFn = CGF.CGM.getOpenMPRuntime(). 1162 emitParallelOrTeamsOutlinedFunction(S, 1163 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 1164 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { 1165 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); 1166 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), 1167 /*IgnoreResultAssign*/ true); 1168 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( 1169 CGF, NumThreads, NumThreadsClause->getLocStart()); 1170 } 1171 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { 1172 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); 1173 CGF.CGM.getOpenMPRuntime().emitProcBindClause( 1174 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); 1175 } 1176 const Expr *IfCond = nullptr; 1177 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 1178 if (C->getNameModifier() == OMPD_unknown || 1179 C->getNameModifier() == OMPD_parallel) { 1180 IfCond = C->getCondition(); 1181 break; 1182 } 1183 } 1184 1185 OMPLexicalScope Scope(CGF, S); 1186 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 1187 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 1188 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, 1189 CapturedVars, IfCond); 1190 } 1191 1192 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { 1193 // Emit parallel region as a standalone region. 1194 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 1195 OMPPrivateScope PrivateScope(CGF); 1196 bool Copyins = CGF.EmitOMPCopyinClause(S); 1197 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 1198 if (Copyins) { 1199 // Emit implicit barrier to synchronize threads and avoid data races on 1200 // propagation master's thread values of threadprivate variables to local 1201 // instances of that variables of all other implicit threads. 1202 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 1203 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 1204 /*ForceSimpleCall=*/true); 1205 } 1206 CGF.EmitOMPPrivateClause(S, PrivateScope); 1207 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 1208 (void)PrivateScope.Privatize(); 1209 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 1210 CGF.EmitOMPReductionClauseFinal(S); 1211 }; 1212 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen); 1213 emitPostUpdateForReductionClause( 1214 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1215 } 1216 1217 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, 1218 JumpDest LoopExit) { 1219 RunCleanupsScope BodyScope(*this); 1220 // Update counters values on current iteration. 1221 for (auto I : D.updates()) { 1222 EmitIgnoredExpr(I); 1223 } 1224 // Update the linear variables. 1225 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1226 for (auto *U : C->updates()) 1227 EmitIgnoredExpr(U); 1228 } 1229 1230 // On a continue in the body, jump to the end. 1231 auto Continue = getJumpDestInCurrentScope("omp.body.continue"); 1232 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1233 // Emit loop body. 1234 EmitStmt(D.getBody()); 1235 // The end (updates/cleanups). 1236 EmitBlock(Continue.getBlock()); 1237 BreakContinueStack.pop_back(); 1238 } 1239 1240 void CodeGenFunction::EmitOMPInnerLoop( 1241 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, 1242 const Expr *IncExpr, 1243 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, 1244 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { 1245 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); 1246 1247 // Start the loop with a block that tests the condition. 1248 auto CondBlock = createBasicBlock("omp.inner.for.cond"); 1249 EmitBlock(CondBlock); 1250 LoopStack.push(CondBlock); 1251 1252 // If there are any cleanups between here and the loop-exit scope, 1253 // create a block to stage a loop exit along. 1254 auto ExitBlock = LoopExit.getBlock(); 1255 if (RequiresCleanup) 1256 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); 1257 1258 auto LoopBody = createBasicBlock("omp.inner.for.body"); 1259 1260 // Emit condition. 1261 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); 1262 if (ExitBlock != LoopExit.getBlock()) { 1263 EmitBlock(ExitBlock); 1264 EmitBranchThroughCleanup(LoopExit); 1265 } 1266 1267 EmitBlock(LoopBody); 1268 incrementProfileCounter(&S); 1269 1270 // Create a block for the increment. 1271 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); 1272 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1273 1274 BodyGen(*this); 1275 1276 // Emit "IV = IV + 1" and a back-edge to the condition block. 1277 EmitBlock(Continue.getBlock()); 1278 EmitIgnoredExpr(IncExpr); 1279 PostIncGen(*this); 1280 BreakContinueStack.pop_back(); 1281 EmitBranch(CondBlock); 1282 LoopStack.pop(); 1283 // Emit the fall-through block. 1284 EmitBlock(LoopExit.getBlock()); 1285 } 1286 1287 void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { 1288 if (!HaveInsertPoint()) 1289 return; 1290 // Emit inits for the linear variables. 1291 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1292 for (auto *Init : C->inits()) { 1293 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); 1294 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { 1295 AutoVarEmission Emission = EmitAutoVarAlloca(*VD); 1296 auto *OrigVD = cast<VarDecl>(Ref->getDecl()); 1297 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1298 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1299 VD->getInit()->getType(), VK_LValue, 1300 VD->getInit()->getExprLoc()); 1301 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), 1302 VD->getType()), 1303 /*capturedByInit=*/false); 1304 EmitAutoVarCleanups(Emission); 1305 } else 1306 EmitVarDecl(*VD); 1307 } 1308 // Emit the linear steps for the linear clauses. 1309 // If a step is not constant, it is pre-calculated before the loop. 1310 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) 1311 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { 1312 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); 1313 // Emit calculation of the linear step. 1314 EmitIgnoredExpr(CS); 1315 } 1316 } 1317 } 1318 1319 void CodeGenFunction::EmitOMPLinearClauseFinal( 1320 const OMPLoopDirective &D, 1321 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1322 if (!HaveInsertPoint()) 1323 return; 1324 llvm::BasicBlock *DoneBB = nullptr; 1325 // Emit the final values of the linear variables. 1326 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1327 auto IC = C->varlist_begin(); 1328 for (auto *F : C->finals()) { 1329 if (!DoneBB) { 1330 if (auto *Cond = CondGen(*this)) { 1331 // If the first post-update expression is found, emit conditional 1332 // block if it was requested. 1333 auto *ThenBB = createBasicBlock(".omp.linear.pu"); 1334 DoneBB = createBasicBlock(".omp.linear.pu.done"); 1335 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1336 EmitBlock(ThenBB); 1337 } 1338 } 1339 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); 1340 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1341 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1342 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); 1343 Address OrigAddr = EmitLValue(&DRE).getAddress(); 1344 CodeGenFunction::OMPPrivateScope VarScope(*this); 1345 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; }); 1346 (void)VarScope.Privatize(); 1347 EmitIgnoredExpr(F); 1348 ++IC; 1349 } 1350 if (auto *PostUpdate = C->getPostUpdateExpr()) 1351 EmitIgnoredExpr(PostUpdate); 1352 } 1353 if (DoneBB) 1354 EmitBlock(DoneBB, /*IsFinished=*/true); 1355 } 1356 1357 static void emitAlignedClause(CodeGenFunction &CGF, 1358 const OMPExecutableDirective &D) { 1359 if (!CGF.HaveInsertPoint()) 1360 return; 1361 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { 1362 unsigned ClauseAlignment = 0; 1363 if (auto AlignmentExpr = Clause->getAlignment()) { 1364 auto AlignmentCI = 1365 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); 1366 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); 1367 } 1368 for (auto E : Clause->varlists()) { 1369 unsigned Alignment = ClauseAlignment; 1370 if (Alignment == 0) { 1371 // OpenMP [2.8.1, Description] 1372 // If no optional parameter is specified, implementation-defined default 1373 // alignments for SIMD instructions on the target platforms are assumed. 1374 Alignment = 1375 CGF.getContext() 1376 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( 1377 E->getType()->getPointeeType())) 1378 .getQuantity(); 1379 } 1380 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && 1381 "alignment is not power of 2"); 1382 if (Alignment != 0) { 1383 llvm::Value *PtrValue = CGF.EmitScalarExpr(E); 1384 CGF.EmitAlignmentAssumption(PtrValue, Alignment); 1385 } 1386 } 1387 } 1388 } 1389 1390 void CodeGenFunction::EmitOMPPrivateLoopCounters( 1391 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { 1392 if (!HaveInsertPoint()) 1393 return; 1394 auto I = S.private_counters().begin(); 1395 for (auto *E : S.counters()) { 1396 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1397 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1398 (void)LoopScope.addPrivate(VD, [&]() -> Address { 1399 // Emit var without initialization. 1400 if (!LocalDeclMap.count(PrivateVD)) { 1401 auto VarEmission = EmitAutoVarAlloca(*PrivateVD); 1402 EmitAutoVarCleanups(VarEmission); 1403 } 1404 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), 1405 /*RefersToEnclosingVariableOrCapture=*/false, 1406 (*I)->getType(), VK_LValue, (*I)->getExprLoc()); 1407 return EmitLValue(&DRE).getAddress(); 1408 }); 1409 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || 1410 VD->hasGlobalStorage()) { 1411 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address { 1412 DeclRefExpr DRE(const_cast<VarDecl *>(VD), 1413 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), 1414 E->getType(), VK_LValue, E->getExprLoc()); 1415 return EmitLValue(&DRE).getAddress(); 1416 }); 1417 } 1418 ++I; 1419 } 1420 } 1421 1422 static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, 1423 const Expr *Cond, llvm::BasicBlock *TrueBlock, 1424 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { 1425 if (!CGF.HaveInsertPoint()) 1426 return; 1427 { 1428 CodeGenFunction::OMPPrivateScope PreCondScope(CGF); 1429 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); 1430 (void)PreCondScope.Privatize(); 1431 // Get initial values of real counters. 1432 for (auto I : S.inits()) { 1433 CGF.EmitIgnoredExpr(I); 1434 } 1435 } 1436 // Check that loop is executed at least one time. 1437 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); 1438 } 1439 1440 void CodeGenFunction::EmitOMPLinearClause( 1441 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { 1442 if (!HaveInsertPoint()) 1443 return; 1444 llvm::DenseSet<const VarDecl *> SIMDLCVs; 1445 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 1446 auto *LoopDirective = cast<OMPLoopDirective>(&D); 1447 for (auto *C : LoopDirective->counters()) { 1448 SIMDLCVs.insert( 1449 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); 1450 } 1451 } 1452 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1453 auto CurPrivate = C->privates().begin(); 1454 for (auto *E : C->varlists()) { 1455 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1456 auto *PrivateVD = 1457 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); 1458 if (!SIMDLCVs.count(VD->getCanonicalDecl())) { 1459 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { 1460 // Emit private VarDecl with copy init. 1461 EmitVarDecl(*PrivateVD); 1462 return GetAddrOfLocalVar(PrivateVD); 1463 }); 1464 assert(IsRegistered && "linear var already registered as private"); 1465 // Silence the warning about unused variable. 1466 (void)IsRegistered; 1467 } else 1468 EmitVarDecl(*PrivateVD); 1469 ++CurPrivate; 1470 } 1471 } 1472 } 1473 1474 static void emitSimdlenSafelenClause(CodeGenFunction &CGF, 1475 const OMPExecutableDirective &D, 1476 bool IsMonotonic) { 1477 if (!CGF.HaveInsertPoint()) 1478 return; 1479 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { 1480 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), 1481 /*ignoreResult=*/true); 1482 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 1483 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 1484 // In presence of finite 'safelen', it may be unsafe to mark all 1485 // the memory instructions parallel, because loop-carried 1486 // dependences of 'safelen' iterations are possible. 1487 if (!IsMonotonic) 1488 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); 1489 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { 1490 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), 1491 /*ignoreResult=*/true); 1492 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 1493 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 1494 // In presence of finite 'safelen', it may be unsafe to mark all 1495 // the memory instructions parallel, because loop-carried 1496 // dependences of 'safelen' iterations are possible. 1497 CGF.LoopStack.setParallel(false); 1498 } 1499 } 1500 1501 void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, 1502 bool IsMonotonic) { 1503 // Walk clauses and process safelen/lastprivate. 1504 LoopStack.setParallel(!IsMonotonic); 1505 LoopStack.setVectorizeEnable(true); 1506 emitSimdlenSafelenClause(*this, D, IsMonotonic); 1507 } 1508 1509 void CodeGenFunction::EmitOMPSimdFinal( 1510 const OMPLoopDirective &D, 1511 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1512 if (!HaveInsertPoint()) 1513 return; 1514 llvm::BasicBlock *DoneBB = nullptr; 1515 auto IC = D.counters().begin(); 1516 auto IPC = D.private_counters().begin(); 1517 for (auto F : D.finals()) { 1518 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); 1519 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); 1520 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); 1521 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || 1522 OrigVD->hasGlobalStorage() || CED) { 1523 if (!DoneBB) { 1524 if (auto *Cond = CondGen(*this)) { 1525 // If the first post-update expression is found, emit conditional 1526 // block if it was requested. 1527 auto *ThenBB = createBasicBlock(".omp.final.then"); 1528 DoneBB = createBasicBlock(".omp.final.done"); 1529 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1530 EmitBlock(ThenBB); 1531 } 1532 } 1533 Address OrigAddr = Address::invalid(); 1534 if (CED) 1535 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); 1536 else { 1537 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), 1538 /*RefersToEnclosingVariableOrCapture=*/false, 1539 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); 1540 OrigAddr = EmitLValue(&DRE).getAddress(); 1541 } 1542 OMPPrivateScope VarScope(*this); 1543 VarScope.addPrivate(OrigVD, 1544 [OrigAddr]() -> Address { return OrigAddr; }); 1545 (void)VarScope.Privatize(); 1546 EmitIgnoredExpr(F); 1547 } 1548 ++IC; 1549 ++IPC; 1550 } 1551 if (DoneBB) 1552 EmitBlock(DoneBB, /*IsFinished=*/true); 1553 } 1554 1555 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { 1556 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 1557 OMPLoopScope PreInitScope(CGF, S); 1558 // if (PreCond) { 1559 // for (IV in 0..LastIteration) BODY; 1560 // <Final counter/linear vars updates>; 1561 // } 1562 // 1563 1564 // Emit: if (PreCond) - begin. 1565 // If the condition constant folds and can be elided, avoid emitting the 1566 // whole loop. 1567 bool CondConstant; 1568 llvm::BasicBlock *ContBlock = nullptr; 1569 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 1570 if (!CondConstant) 1571 return; 1572 } else { 1573 auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); 1574 ContBlock = CGF.createBasicBlock("simd.if.end"); 1575 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 1576 CGF.getProfileCount(&S)); 1577 CGF.EmitBlock(ThenBlock); 1578 CGF.incrementProfileCounter(&S); 1579 } 1580 1581 // Emit the loop iteration variable. 1582 const Expr *IVExpr = S.getIterationVariable(); 1583 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 1584 CGF.EmitVarDecl(*IVDecl); 1585 CGF.EmitIgnoredExpr(S.getInit()); 1586 1587 // Emit the iterations count variable. 1588 // If it is not a variable, Sema decided to calculate iterations count on 1589 // each iteration (e.g., it is foldable into a constant). 1590 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 1591 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 1592 // Emit calculation of the iterations count. 1593 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 1594 } 1595 1596 CGF.EmitOMPSimdInit(S); 1597 1598 emitAlignedClause(CGF, S); 1599 CGF.EmitOMPLinearClauseInit(S); 1600 { 1601 OMPPrivateScope LoopScope(CGF); 1602 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 1603 CGF.EmitOMPLinearClause(S, LoopScope); 1604 CGF.EmitOMPPrivateClause(S, LoopScope); 1605 CGF.EmitOMPReductionClauseInit(S, LoopScope); 1606 bool HasLastprivateClause = 1607 CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 1608 (void)LoopScope.Privatize(); 1609 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 1610 S.getInc(), 1611 [&S](CodeGenFunction &CGF) { 1612 CGF.EmitOMPLoopBody(S, JumpDest()); 1613 CGF.EmitStopPoint(&S); 1614 }, 1615 [](CodeGenFunction &) {}); 1616 CGF.EmitOMPSimdFinal( 1617 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1618 // Emit final copy of the lastprivate variables at the end of loops. 1619 if (HasLastprivateClause) 1620 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); 1621 CGF.EmitOMPReductionClauseFinal(S); 1622 emitPostUpdateForReductionClause( 1623 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1624 } 1625 CGF.EmitOMPLinearClauseFinal( 1626 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1627 // Emit: if (PreCond) - end. 1628 if (ContBlock) { 1629 CGF.EmitBranch(ContBlock); 1630 CGF.EmitBlock(ContBlock, true); 1631 } 1632 }; 1633 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 1634 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 1635 } 1636 1637 void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic, 1638 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, 1639 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1640 auto &RT = CGM.getOpenMPRuntime(); 1641 1642 const Expr *IVExpr = S.getIterationVariable(); 1643 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1644 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1645 1646 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); 1647 1648 // Start the loop with a block that tests the condition. 1649 auto CondBlock = createBasicBlock("omp.dispatch.cond"); 1650 EmitBlock(CondBlock); 1651 LoopStack.push(CondBlock); 1652 1653 llvm::Value *BoolCondVal = nullptr; 1654 if (!DynamicOrOrdered) { 1655 // UB = min(UB, GlobalUB) 1656 EmitIgnoredExpr(S.getEnsureUpperBound()); 1657 // IV = LB 1658 EmitIgnoredExpr(S.getInit()); 1659 // IV < UB 1660 BoolCondVal = EvaluateExprAsBool(S.getCond()); 1661 } else { 1662 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, IL, 1663 LB, UB, ST); 1664 } 1665 1666 // If there are any cleanups between here and the loop-exit scope, 1667 // create a block to stage a loop exit along. 1668 auto ExitBlock = LoopExit.getBlock(); 1669 if (LoopScope.requiresCleanups()) 1670 ExitBlock = createBasicBlock("omp.dispatch.cleanup"); 1671 1672 auto LoopBody = createBasicBlock("omp.dispatch.body"); 1673 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); 1674 if (ExitBlock != LoopExit.getBlock()) { 1675 EmitBlock(ExitBlock); 1676 EmitBranchThroughCleanup(LoopExit); 1677 } 1678 EmitBlock(LoopBody); 1679 1680 // Emit "IV = LB" (in case of static schedule, we have already calculated new 1681 // LB for loop condition and emitted it above). 1682 if (DynamicOrOrdered) 1683 EmitIgnoredExpr(S.getInit()); 1684 1685 // Create a block for the increment. 1686 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); 1687 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1688 1689 // Generate !llvm.loop.parallel metadata for loads and stores for loops 1690 // with dynamic/guided scheduling and without ordered clause. 1691 if (!isOpenMPSimdDirective(S.getDirectiveKind())) 1692 LoopStack.setParallel(!IsMonotonic); 1693 else 1694 EmitOMPSimdInit(S, IsMonotonic); 1695 1696 SourceLocation Loc = S.getLocStart(); 1697 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), 1698 [&S, LoopExit](CodeGenFunction &CGF) { 1699 CGF.EmitOMPLoopBody(S, LoopExit); 1700 CGF.EmitStopPoint(&S); 1701 }, 1702 [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) { 1703 if (Ordered) { 1704 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd( 1705 CGF, Loc, IVSize, IVSigned); 1706 } 1707 }); 1708 1709 EmitBlock(Continue.getBlock()); 1710 BreakContinueStack.pop_back(); 1711 if (!DynamicOrOrdered) { 1712 // Emit "LB = LB + Stride", "UB = UB + Stride". 1713 EmitIgnoredExpr(S.getNextLowerBound()); 1714 EmitIgnoredExpr(S.getNextUpperBound()); 1715 } 1716 1717 EmitBranch(CondBlock); 1718 LoopStack.pop(); 1719 // Emit the fall-through block. 1720 EmitBlock(LoopExit.getBlock()); 1721 1722 // Tell the runtime we are done. 1723 if (!DynamicOrOrdered) 1724 RT.emitForStaticFinish(*this, S.getLocEnd()); 1725 1726 } 1727 1728 void CodeGenFunction::EmitOMPForOuterLoop( 1729 OpenMPScheduleClauseKind ScheduleKind, bool IsMonotonic, 1730 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, 1731 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1732 auto &RT = CGM.getOpenMPRuntime(); 1733 1734 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). 1735 const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind); 1736 1737 assert((Ordered || 1738 !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) && 1739 "static non-chunked schedule does not need outer loop"); 1740 1741 // Emit outer loop. 1742 // 1743 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1744 // When schedule(dynamic,chunk_size) is specified, the iterations are 1745 // distributed to threads in the team in chunks as the threads request them. 1746 // Each thread executes a chunk of iterations, then requests another chunk, 1747 // until no chunks remain to be distributed. Each chunk contains chunk_size 1748 // iterations, except for the last chunk to be distributed, which may have 1749 // fewer iterations. When no chunk_size is specified, it defaults to 1. 1750 // 1751 // When schedule(guided,chunk_size) is specified, the iterations are assigned 1752 // to threads in the team in chunks as the executing threads request them. 1753 // Each thread executes a chunk of iterations, then requests another chunk, 1754 // until no chunks remain to be assigned. For a chunk_size of 1, the size of 1755 // each chunk is proportional to the number of unassigned iterations divided 1756 // by the number of threads in the team, decreasing to 1. For a chunk_size 1757 // with value k (greater than 1), the size of each chunk is determined in the 1758 // same way, with the restriction that the chunks do not contain fewer than k 1759 // iterations (except for the last chunk to be assigned, which may have fewer 1760 // than k iterations). 1761 // 1762 // When schedule(auto) is specified, the decision regarding scheduling is 1763 // delegated to the compiler and/or runtime system. The programmer gives the 1764 // implementation the freedom to choose any possible mapping of iterations to 1765 // threads in the team. 1766 // 1767 // When schedule(runtime) is specified, the decision regarding scheduling is 1768 // deferred until run time, and the schedule and chunk size are taken from the 1769 // run-sched-var ICV. If the ICV is set to auto, the schedule is 1770 // implementation defined 1771 // 1772 // while(__kmpc_dispatch_next(&LB, &UB)) { 1773 // idx = LB; 1774 // while (idx <= UB) { BODY; ++idx; 1775 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. 1776 // } // inner loop 1777 // } 1778 // 1779 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1780 // When schedule(static, chunk_size) is specified, iterations are divided into 1781 // chunks of size chunk_size, and the chunks are assigned to the threads in 1782 // the team in a round-robin fashion in the order of the thread number. 1783 // 1784 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { 1785 // while (idx <= UB) { BODY; ++idx; } // inner loop 1786 // LB = LB + ST; 1787 // UB = UB + ST; 1788 // } 1789 // 1790 1791 const Expr *IVExpr = S.getIterationVariable(); 1792 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1793 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1794 1795 if (DynamicOrOrdered) { 1796 llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration()); 1797 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, 1798 IVSize, IVSigned, Ordered, UBVal, Chunk); 1799 } else { 1800 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, 1801 Ordered, IL, LB, UB, ST, Chunk); 1802 } 1803 1804 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, Ordered, LB, UB, 1805 ST, IL, Chunk); 1806 } 1807 1808 void CodeGenFunction::EmitOMPDistributeOuterLoop( 1809 OpenMPDistScheduleClauseKind ScheduleKind, 1810 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope, 1811 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1812 1813 auto &RT = CGM.getOpenMPRuntime(); 1814 1815 // Emit outer loop. 1816 // Same behavior as a OMPForOuterLoop, except that schedule cannot be 1817 // dynamic 1818 // 1819 1820 const Expr *IVExpr = S.getIterationVariable(); 1821 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1822 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1823 1824 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, 1825 IVSize, IVSigned, /* Ordered = */ false, 1826 IL, LB, UB, ST, Chunk); 1827 1828 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, 1829 S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk); 1830 } 1831 1832 /// \brief Emit a helper variable and return corresponding lvalue. 1833 static LValue EmitOMPHelperVar(CodeGenFunction &CGF, 1834 const DeclRefExpr *Helper) { 1835 auto VDecl = cast<VarDecl>(Helper->getDecl()); 1836 CGF.EmitVarDecl(*VDecl); 1837 return CGF.EmitLValue(Helper); 1838 } 1839 1840 namespace { 1841 struct ScheduleKindModifiersTy { 1842 OpenMPScheduleClauseKind Kind; 1843 OpenMPScheduleClauseModifier M1; 1844 OpenMPScheduleClauseModifier M2; 1845 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, 1846 OpenMPScheduleClauseModifier M1, 1847 OpenMPScheduleClauseModifier M2) 1848 : Kind(Kind), M1(M1), M2(M2) {} 1849 }; 1850 } // namespace 1851 1852 bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { 1853 // Emit the loop iteration variable. 1854 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 1855 auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); 1856 EmitVarDecl(*IVDecl); 1857 1858 // Emit the iterations count variable. 1859 // If it is not a variable, Sema decided to calculate iterations count on each 1860 // iteration (e.g., it is foldable into a constant). 1861 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 1862 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 1863 // Emit calculation of the iterations count. 1864 EmitIgnoredExpr(S.getCalcLastIteration()); 1865 } 1866 1867 auto &RT = CGM.getOpenMPRuntime(); 1868 1869 bool HasLastprivateClause; 1870 // Check pre-condition. 1871 { 1872 OMPLoopScope PreInitScope(*this, S); 1873 // Skip the entire loop if we don't meet the precondition. 1874 // If the condition constant folds and can be elided, avoid emitting the 1875 // whole loop. 1876 bool CondConstant; 1877 llvm::BasicBlock *ContBlock = nullptr; 1878 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 1879 if (!CondConstant) 1880 return false; 1881 } else { 1882 auto *ThenBlock = createBasicBlock("omp.precond.then"); 1883 ContBlock = createBasicBlock("omp.precond.end"); 1884 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 1885 getProfileCount(&S)); 1886 EmitBlock(ThenBlock); 1887 incrementProfileCounter(&S); 1888 } 1889 1890 llvm::DenseSet<const Expr *> EmittedFinals; 1891 emitAlignedClause(*this, S); 1892 EmitOMPLinearClauseInit(S); 1893 // Emit helper vars inits. 1894 LValue LB = 1895 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); 1896 LValue UB = 1897 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); 1898 LValue ST = 1899 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 1900 LValue IL = 1901 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 1902 1903 // Emit 'then' code. 1904 { 1905 OMPPrivateScope LoopScope(*this); 1906 if (EmitOMPFirstprivateClause(S, LoopScope)) { 1907 // Emit implicit barrier to synchronize threads and avoid data races on 1908 // initialization of firstprivate variables and post-update of 1909 // lastprivate variables. 1910 CGM.getOpenMPRuntime().emitBarrierCall( 1911 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 1912 /*ForceSimpleCall=*/true); 1913 } 1914 EmitOMPPrivateClause(S, LoopScope); 1915 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); 1916 EmitOMPReductionClauseInit(S, LoopScope); 1917 EmitOMPPrivateLoopCounters(S, LoopScope); 1918 EmitOMPLinearClause(S, LoopScope); 1919 (void)LoopScope.Privatize(); 1920 1921 // Detect the loop schedule kind and chunk. 1922 llvm::Value *Chunk = nullptr; 1923 OpenMPScheduleClauseKind ScheduleKind = OMPC_SCHEDULE_unknown; 1924 OpenMPScheduleClauseModifier M1 = OMPC_SCHEDULE_MODIFIER_unknown; 1925 OpenMPScheduleClauseModifier M2 = OMPC_SCHEDULE_MODIFIER_unknown; 1926 if (auto *C = S.getSingleClause<OMPScheduleClause>()) { 1927 ScheduleKind = C->getScheduleKind(); 1928 M1 = C->getFirstScheduleModifier(); 1929 M2 = C->getSecondScheduleModifier(); 1930 if (const auto *Ch = C->getChunkSize()) { 1931 Chunk = EmitScalarExpr(Ch); 1932 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 1933 S.getIterationVariable()->getType(), 1934 S.getLocStart()); 1935 } 1936 } 1937 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1938 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1939 const bool Ordered = S.getSingleClause<OMPOrderedClause>() != nullptr; 1940 // OpenMP 4.5, 2.7.1 Loop Construct, Description. 1941 // If the static schedule kind is specified or if the ordered clause is 1942 // specified, and if no monotonic modifier is specified, the effect will 1943 // be as if the monotonic modifier was specified. 1944 if (RT.isStaticNonchunked(ScheduleKind, 1945 /* Chunked */ Chunk != nullptr) && 1946 !Ordered) { 1947 if (isOpenMPSimdDirective(S.getDirectiveKind())) 1948 EmitOMPSimdInit(S, /*IsMonotonic=*/true); 1949 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1950 // When no chunk_size is specified, the iteration space is divided into 1951 // chunks that are approximately equal in size, and at most one chunk is 1952 // distributed to each thread. Note that the size of the chunks is 1953 // unspecified in this case. 1954 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, 1955 IVSize, IVSigned, Ordered, 1956 IL.getAddress(), LB.getAddress(), 1957 UB.getAddress(), ST.getAddress()); 1958 auto LoopExit = 1959 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 1960 // UB = min(UB, GlobalUB); 1961 EmitIgnoredExpr(S.getEnsureUpperBound()); 1962 // IV = LB; 1963 EmitIgnoredExpr(S.getInit()); 1964 // while (idx <= UB) { BODY; ++idx; } 1965 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 1966 S.getInc(), 1967 [&S, LoopExit](CodeGenFunction &CGF) { 1968 CGF.EmitOMPLoopBody(S, LoopExit); 1969 CGF.EmitStopPoint(&S); 1970 }, 1971 [](CodeGenFunction &) {}); 1972 EmitBlock(LoopExit.getBlock()); 1973 // Tell the runtime we are done. 1974 RT.emitForStaticFinish(*this, S.getLocStart()); 1975 } else { 1976 const bool IsMonotonic = Ordered || 1977 ScheduleKind == OMPC_SCHEDULE_static || 1978 ScheduleKind == OMPC_SCHEDULE_unknown || 1979 M1 == OMPC_SCHEDULE_MODIFIER_monotonic || 1980 M2 == OMPC_SCHEDULE_MODIFIER_monotonic; 1981 // Emit the outer loop, which requests its work chunk [LB..UB] from 1982 // runtime and runs the inner loop to process it. 1983 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, 1984 LB.getAddress(), UB.getAddress(), ST.getAddress(), 1985 IL.getAddress(), Chunk); 1986 } 1987 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 1988 EmitOMPSimdFinal(S, 1989 [&](CodeGenFunction &CGF) -> llvm::Value * { 1990 return CGF.Builder.CreateIsNotNull( 1991 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 1992 }); 1993 } 1994 EmitOMPReductionClauseFinal(S); 1995 // Emit post-update of the reduction variables if IsLastIter != 0. 1996 emitPostUpdateForReductionClause( 1997 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { 1998 return CGF.Builder.CreateIsNotNull( 1999 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2000 }); 2001 // Emit final copy of the lastprivate variables if IsLastIter != 0. 2002 if (HasLastprivateClause) 2003 EmitOMPLastprivateClauseFinal( 2004 S, isOpenMPSimdDirective(S.getDirectiveKind()), 2005 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); 2006 } 2007 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { 2008 return CGF.Builder.CreateIsNotNull( 2009 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2010 }); 2011 // We're now done with the loop, so jump to the continuation block. 2012 if (ContBlock) { 2013 EmitBranch(ContBlock); 2014 EmitBlock(ContBlock, true); 2015 } 2016 } 2017 return HasLastprivateClause; 2018 } 2019 2020 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { 2021 bool HasLastprivates = false; 2022 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 2023 PrePostActionTy &) { 2024 HasLastprivates = CGF.EmitOMPWorksharingLoop(S); 2025 }; 2026 { 2027 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2028 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, 2029 S.hasCancel()); 2030 } 2031 2032 // Emit an implicit barrier at the end. 2033 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { 2034 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); 2035 } 2036 } 2037 2038 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { 2039 bool HasLastprivates = false; 2040 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 2041 PrePostActionTy &) { 2042 HasLastprivates = CGF.EmitOMPWorksharingLoop(S); 2043 }; 2044 { 2045 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2046 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 2047 } 2048 2049 // Emit an implicit barrier at the end. 2050 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { 2051 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); 2052 } 2053 } 2054 2055 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, 2056 const Twine &Name, 2057 llvm::Value *Init = nullptr) { 2058 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); 2059 if (Init) 2060 CGF.EmitScalarInit(Init, LVal); 2061 return LVal; 2062 } 2063 2064 void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { 2065 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); 2066 auto *CS = dyn_cast<CompoundStmt>(Stmt); 2067 bool HasLastprivates = false; 2068 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF, 2069 PrePostActionTy &) { 2070 auto &C = CGF.CGM.getContext(); 2071 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 2072 // Emit helper vars inits. 2073 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", 2074 CGF.Builder.getInt32(0)); 2075 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) 2076 : CGF.Builder.getInt32(0); 2077 LValue UB = 2078 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); 2079 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", 2080 CGF.Builder.getInt32(1)); 2081 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", 2082 CGF.Builder.getInt32(0)); 2083 // Loop counter. 2084 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); 2085 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 2086 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); 2087 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 2088 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); 2089 // Generate condition for loop. 2090 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, 2091 OK_Ordinary, S.getLocStart(), 2092 /*fpContractable=*/false); 2093 // Increment for loop counter. 2094 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, 2095 S.getLocStart()); 2096 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { 2097 // Iterate through all sections and emit a switch construct: 2098 // switch (IV) { 2099 // case 0: 2100 // <SectionStmt[0]>; 2101 // break; 2102 // ... 2103 // case <NumSection> - 1: 2104 // <SectionStmt[<NumSection> - 1]>; 2105 // break; 2106 // } 2107 // .omp.sections.exit: 2108 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); 2109 auto *SwitchStmt = CGF.Builder.CreateSwitch( 2110 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, 2111 CS == nullptr ? 1 : CS->size()); 2112 if (CS) { 2113 unsigned CaseNumber = 0; 2114 for (auto *SubStmt : CS->children()) { 2115 auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); 2116 CGF.EmitBlock(CaseBB); 2117 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); 2118 CGF.EmitStmt(SubStmt); 2119 CGF.EmitBranch(ExitBB); 2120 ++CaseNumber; 2121 } 2122 } else { 2123 auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); 2124 CGF.EmitBlock(CaseBB); 2125 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); 2126 CGF.EmitStmt(Stmt); 2127 CGF.EmitBranch(ExitBB); 2128 } 2129 CGF.EmitBlock(ExitBB, /*IsFinished=*/true); 2130 }; 2131 2132 CodeGenFunction::OMPPrivateScope LoopScope(CGF); 2133 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { 2134 // Emit implicit barrier to synchronize threads and avoid data races on 2135 // initialization of firstprivate variables and post-update of lastprivate 2136 // variables. 2137 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 2138 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 2139 /*ForceSimpleCall=*/true); 2140 } 2141 CGF.EmitOMPPrivateClause(S, LoopScope); 2142 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 2143 CGF.EmitOMPReductionClauseInit(S, LoopScope); 2144 (void)LoopScope.Privatize(); 2145 2146 // Emit static non-chunked loop. 2147 CGF.CGM.getOpenMPRuntime().emitForStaticInit( 2148 CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32, 2149 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(), 2150 UB.getAddress(), ST.getAddress()); 2151 // UB = min(UB, GlobalUB); 2152 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); 2153 auto *MinUBGlobalUB = CGF.Builder.CreateSelect( 2154 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); 2155 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); 2156 // IV = LB; 2157 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); 2158 // while (idx <= UB) { BODY; ++idx; } 2159 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, 2160 [](CodeGenFunction &) {}); 2161 // Tell the runtime we are done. 2162 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart()); 2163 CGF.EmitOMPReductionClauseFinal(S); 2164 // Emit post-update of the reduction variables if IsLastIter != 0. 2165 emitPostUpdateForReductionClause( 2166 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * { 2167 return CGF.Builder.CreateIsNotNull( 2168 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2169 }); 2170 2171 // Emit final copy of the lastprivate variables if IsLastIter != 0. 2172 if (HasLastprivates) 2173 CGF.EmitOMPLastprivateClauseFinal( 2174 S, /*NoFinals=*/false, 2175 CGF.Builder.CreateIsNotNull( 2176 CGF.EmitLoadOfScalar(IL, S.getLocStart()))); 2177 }; 2178 2179 bool HasCancel = false; 2180 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) 2181 HasCancel = OSD->hasCancel(); 2182 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) 2183 HasCancel = OPSD->hasCancel(); 2184 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, 2185 HasCancel); 2186 // Emit barrier for lastprivates only if 'sections' directive has 'nowait' 2187 // clause. Otherwise the barrier will be generated by the codegen for the 2188 // directive. 2189 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { 2190 // Emit implicit barrier to synchronize threads and avoid data races on 2191 // initialization of firstprivate variables. 2192 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 2193 OMPD_unknown); 2194 } 2195 } 2196 2197 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { 2198 { 2199 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2200 EmitSections(S); 2201 } 2202 // Emit an implicit barrier at the end. 2203 if (!S.getSingleClause<OMPNowaitClause>()) { 2204 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 2205 OMPD_sections); 2206 } 2207 } 2208 2209 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { 2210 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2211 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2212 }; 2213 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2214 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, 2215 S.hasCancel()); 2216 } 2217 2218 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { 2219 llvm::SmallVector<const Expr *, 8> CopyprivateVars; 2220 llvm::SmallVector<const Expr *, 8> DestExprs; 2221 llvm::SmallVector<const Expr *, 8> SrcExprs; 2222 llvm::SmallVector<const Expr *, 8> AssignmentOps; 2223 // Check if there are any 'copyprivate' clauses associated with this 2224 // 'single' construct. 2225 // Build a list of copyprivate variables along with helper expressions 2226 // (<source>, <destination>, <destination>=<source> expressions) 2227 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { 2228 CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); 2229 DestExprs.append(C->destination_exprs().begin(), 2230 C->destination_exprs().end()); 2231 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); 2232 AssignmentOps.append(C->assignment_ops().begin(), 2233 C->assignment_ops().end()); 2234 } 2235 // Emit code for 'single' region along with 'copyprivate' clauses 2236 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2237 Action.Enter(CGF); 2238 OMPPrivateScope SingleScope(CGF); 2239 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); 2240 CGF.EmitOMPPrivateClause(S, SingleScope); 2241 (void)SingleScope.Privatize(); 2242 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2243 }; 2244 { 2245 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2246 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), 2247 CopyprivateVars, DestExprs, 2248 SrcExprs, AssignmentOps); 2249 } 2250 // Emit an implicit barrier at the end (to avoid data race on firstprivate 2251 // init or if no 'nowait' clause was specified and no 'copyprivate' clause). 2252 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { 2253 CGM.getOpenMPRuntime().emitBarrierCall( 2254 *this, S.getLocStart(), 2255 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); 2256 } 2257 } 2258 2259 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { 2260 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2261 Action.Enter(CGF); 2262 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2263 }; 2264 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2265 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); 2266 } 2267 2268 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { 2269 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2270 Action.Enter(CGF); 2271 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2272 }; 2273 Expr *Hint = nullptr; 2274 if (auto *HintClause = S.getSingleClause<OMPHintClause>()) 2275 Hint = HintClause->getHint(); 2276 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2277 CGM.getOpenMPRuntime().emitCriticalRegion(*this, 2278 S.getDirectiveName().getAsString(), 2279 CodeGen, S.getLocStart(), Hint); 2280 } 2281 2282 void CodeGenFunction::EmitOMPParallelForDirective( 2283 const OMPParallelForDirective &S) { 2284 // Emit directive as a combined directive that consists of two implicit 2285 // directives: 'parallel' with 'for' directive. 2286 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2287 CGF.EmitOMPWorksharingLoop(S); 2288 }; 2289 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen); 2290 } 2291 2292 void CodeGenFunction::EmitOMPParallelForSimdDirective( 2293 const OMPParallelForSimdDirective &S) { 2294 // Emit directive as a combined directive that consists of two implicit 2295 // directives: 'parallel' with 'for' directive. 2296 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2297 CGF.EmitOMPWorksharingLoop(S); 2298 }; 2299 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen); 2300 } 2301 2302 void CodeGenFunction::EmitOMPParallelSectionsDirective( 2303 const OMPParallelSectionsDirective &S) { 2304 // Emit directive as a combined directive that consists of two implicit 2305 // directives: 'parallel' with 'sections' directive. 2306 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2307 CGF.EmitSections(S); 2308 }; 2309 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen); 2310 } 2311 2312 void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S, 2313 const RegionCodeGenTy &BodyGen, 2314 const TaskGenTy &TaskGen, 2315 OMPTaskDataTy &Data) { 2316 // Emit outlined function for task construct. 2317 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2318 auto *I = CS->getCapturedDecl()->param_begin(); 2319 auto *PartId = std::next(I); 2320 auto *TaskT = std::next(I, 4); 2321 // Check if the task is final 2322 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { 2323 // If the condition constant folds and can be elided, try to avoid emitting 2324 // the condition and the dead arm of the if/else. 2325 auto *Cond = Clause->getCondition(); 2326 bool CondConstant; 2327 if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) 2328 Data.Final.setInt(CondConstant); 2329 else 2330 Data.Final.setPointer(EvaluateExprAsBool(Cond)); 2331 } else { 2332 // By default the task is not final. 2333 Data.Final.setInt(/*IntVal=*/false); 2334 } 2335 // The first function argument for tasks is a thread id, the second one is a 2336 // part id (0 for tied tasks, >=0 for untied task). 2337 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 2338 // Get list of private variables. 2339 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { 2340 auto IRef = C->varlist_begin(); 2341 for (auto *IInit : C->private_copies()) { 2342 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 2343 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 2344 Data.PrivateVars.push_back(*IRef); 2345 Data.PrivateCopies.push_back(IInit); 2346 } 2347 ++IRef; 2348 } 2349 } 2350 EmittedAsPrivate.clear(); 2351 // Get list of firstprivate variables. 2352 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { 2353 auto IRef = C->varlist_begin(); 2354 auto IElemInitRef = C->inits().begin(); 2355 for (auto *IInit : C->private_copies()) { 2356 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 2357 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 2358 Data.FirstprivateVars.push_back(*IRef); 2359 Data.FirstprivateCopies.push_back(IInit); 2360 Data.FirstprivateInits.push_back(*IElemInitRef); 2361 } 2362 ++IRef; 2363 ++IElemInitRef; 2364 } 2365 } 2366 // Build list of dependences. 2367 for (const auto *C : S.getClausesOfKind<OMPDependClause>()) 2368 for (auto *IRef : C->varlists()) 2369 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); 2370 auto &&CodeGen = [PartId, &S, &Data, CS, &BodyGen](CodeGenFunction &CGF, 2371 PrePostActionTy &Action) { 2372 // Set proper addresses for generated private copies. 2373 OMPPrivateScope Scope(CGF); 2374 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty()) { 2375 auto *CopyFn = CGF.Builder.CreateLoad( 2376 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); 2377 auto *PrivatesPtr = CGF.Builder.CreateLoad( 2378 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); 2379 // Map privates. 2380 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; 2381 llvm::SmallVector<llvm::Value *, 16> CallArgs; 2382 CallArgs.push_back(PrivatesPtr); 2383 for (auto *E : Data.PrivateVars) { 2384 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 2385 Address PrivatePtr = CGF.CreateMemTemp( 2386 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); 2387 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); 2388 CallArgs.push_back(PrivatePtr.getPointer()); 2389 } 2390 for (auto *E : Data.FirstprivateVars) { 2391 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 2392 Address PrivatePtr = 2393 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 2394 ".firstpriv.ptr.addr"); 2395 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); 2396 CallArgs.push_back(PrivatePtr.getPointer()); 2397 } 2398 CGF.EmitRuntimeCall(CopyFn, CallArgs); 2399 for (auto &&Pair : PrivatePtrs) { 2400 Address Replacement(CGF.Builder.CreateLoad(Pair.second), 2401 CGF.getContext().getDeclAlign(Pair.first)); 2402 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); 2403 } 2404 } 2405 (void)Scope.Privatize(); 2406 2407 Action.Enter(CGF); 2408 BodyGen(CGF); 2409 }; 2410 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( 2411 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, 2412 Data.NumberOfParts); 2413 OMPLexicalScope Scope(*this, S); 2414 TaskGen(*this, OutlinedFn, Data); 2415 } 2416 2417 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { 2418 // Emit outlined function for task construct. 2419 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2420 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 2421 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 2422 const Expr *IfCond = nullptr; 2423 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 2424 if (C->getNameModifier() == OMPD_unknown || 2425 C->getNameModifier() == OMPD_task) { 2426 IfCond = C->getCondition(); 2427 break; 2428 } 2429 } 2430 2431 OMPTaskDataTy Data; 2432 // Check if we should emit tied or untied task. 2433 Data.Tied = !S.getSingleClause<OMPUntiedClause>(); 2434 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { 2435 CGF.EmitStmt(CS->getCapturedStmt()); 2436 }; 2437 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 2438 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, 2439 const OMPTaskDataTy &Data) { 2440 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn, 2441 SharedsTy, CapturedStruct, IfCond, 2442 Data); 2443 }; 2444 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); 2445 } 2446 2447 void CodeGenFunction::EmitOMPTaskyieldDirective( 2448 const OMPTaskyieldDirective &S) { 2449 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); 2450 } 2451 2452 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { 2453 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); 2454 } 2455 2456 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { 2457 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); 2458 } 2459 2460 void CodeGenFunction::EmitOMPTaskgroupDirective( 2461 const OMPTaskgroupDirective &S) { 2462 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2463 Action.Enter(CGF); 2464 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2465 }; 2466 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2467 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); 2468 } 2469 2470 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { 2471 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { 2472 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { 2473 return llvm::makeArrayRef(FlushClause->varlist_begin(), 2474 FlushClause->varlist_end()); 2475 } 2476 return llvm::None; 2477 }(), S.getLocStart()); 2478 } 2479 2480 void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) { 2481 // Emit the loop iteration variable. 2482 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 2483 auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); 2484 EmitVarDecl(*IVDecl); 2485 2486 // Emit the iterations count variable. 2487 // If it is not a variable, Sema decided to calculate iterations count on each 2488 // iteration (e.g., it is foldable into a constant). 2489 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 2490 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 2491 // Emit calculation of the iterations count. 2492 EmitIgnoredExpr(S.getCalcLastIteration()); 2493 } 2494 2495 auto &RT = CGM.getOpenMPRuntime(); 2496 2497 // Check pre-condition. 2498 { 2499 OMPLoopScope PreInitScope(*this, S); 2500 // Skip the entire loop if we don't meet the precondition. 2501 // If the condition constant folds and can be elided, avoid emitting the 2502 // whole loop. 2503 bool CondConstant; 2504 llvm::BasicBlock *ContBlock = nullptr; 2505 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 2506 if (!CondConstant) 2507 return; 2508 } else { 2509 auto *ThenBlock = createBasicBlock("omp.precond.then"); 2510 ContBlock = createBasicBlock("omp.precond.end"); 2511 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 2512 getProfileCount(&S)); 2513 EmitBlock(ThenBlock); 2514 incrementProfileCounter(&S); 2515 } 2516 2517 // Emit 'then' code. 2518 { 2519 // Emit helper vars inits. 2520 LValue LB = 2521 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); 2522 LValue UB = 2523 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); 2524 LValue ST = 2525 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 2526 LValue IL = 2527 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 2528 2529 OMPPrivateScope LoopScope(*this); 2530 EmitOMPPrivateLoopCounters(S, LoopScope); 2531 (void)LoopScope.Privatize(); 2532 2533 // Detect the distribute schedule kind and chunk. 2534 llvm::Value *Chunk = nullptr; 2535 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; 2536 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) { 2537 ScheduleKind = C->getDistScheduleKind(); 2538 if (const auto *Ch = C->getChunkSize()) { 2539 Chunk = EmitScalarExpr(Ch); 2540 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 2541 S.getIterationVariable()->getType(), 2542 S.getLocStart()); 2543 } 2544 } 2545 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2546 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2547 2548 // OpenMP [2.10.8, distribute Construct, Description] 2549 // If dist_schedule is specified, kind must be static. If specified, 2550 // iterations are divided into chunks of size chunk_size, chunks are 2551 // assigned to the teams of the league in a round-robin fashion in the 2552 // order of the team number. When no chunk_size is specified, the 2553 // iteration space is divided into chunks that are approximately equal 2554 // in size, and at most one chunk is distributed to each team of the 2555 // league. The size of the chunks is unspecified in this case. 2556 if (RT.isStaticNonchunked(ScheduleKind, 2557 /* Chunked */ Chunk != nullptr)) { 2558 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, 2559 IVSize, IVSigned, /* Ordered = */ false, 2560 IL.getAddress(), LB.getAddress(), 2561 UB.getAddress(), ST.getAddress()); 2562 auto LoopExit = 2563 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 2564 // UB = min(UB, GlobalUB); 2565 EmitIgnoredExpr(S.getEnsureUpperBound()); 2566 // IV = LB; 2567 EmitIgnoredExpr(S.getInit()); 2568 // while (idx <= UB) { BODY; ++idx; } 2569 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 2570 S.getInc(), 2571 [&S, LoopExit](CodeGenFunction &CGF) { 2572 CGF.EmitOMPLoopBody(S, LoopExit); 2573 CGF.EmitStopPoint(&S); 2574 }, 2575 [](CodeGenFunction &) {}); 2576 EmitBlock(LoopExit.getBlock()); 2577 // Tell the runtime we are done. 2578 RT.emitForStaticFinish(*this, S.getLocStart()); 2579 } else { 2580 // Emit the outer loop, which requests its work chunk [LB..UB] from 2581 // runtime and runs the inner loop to process it. 2582 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, 2583 LB.getAddress(), UB.getAddress(), ST.getAddress(), 2584 IL.getAddress(), Chunk); 2585 } 2586 } 2587 2588 // We're now done with the loop, so jump to the continuation block. 2589 if (ContBlock) { 2590 EmitBranch(ContBlock); 2591 EmitBlock(ContBlock, true); 2592 } 2593 } 2594 } 2595 2596 void CodeGenFunction::EmitOMPDistributeDirective( 2597 const OMPDistributeDirective &S) { 2598 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2599 CGF.EmitOMPDistributeLoop(S); 2600 }; 2601 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2602 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen, 2603 false); 2604 } 2605 2606 static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, 2607 const CapturedStmt *S) { 2608 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); 2609 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; 2610 CGF.CapturedStmtInfo = &CapStmtInfo; 2611 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); 2612 Fn->addFnAttr(llvm::Attribute::NoInline); 2613 return Fn; 2614 } 2615 2616 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { 2617 if (!S.getAssociatedStmt()) 2618 return; 2619 auto *C = S.getSingleClause<OMPSIMDClause>(); 2620 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, 2621 PrePostActionTy &Action) { 2622 if (C) { 2623 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2624 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 2625 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 2626 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); 2627 CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars); 2628 } else { 2629 Action.Enter(CGF); 2630 CGF.EmitStmt( 2631 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2632 } 2633 }; 2634 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2635 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); 2636 } 2637 2638 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, 2639 QualType SrcType, QualType DestType, 2640 SourceLocation Loc) { 2641 assert(CGF.hasScalarEvaluationKind(DestType) && 2642 "DestType must have scalar evaluation kind."); 2643 assert(!Val.isAggregate() && "Must be a scalar or complex."); 2644 return Val.isScalar() 2645 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, 2646 Loc) 2647 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, 2648 DestType, Loc); 2649 } 2650 2651 static CodeGenFunction::ComplexPairTy 2652 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, 2653 QualType DestType, SourceLocation Loc) { 2654 assert(CGF.getEvaluationKind(DestType) == TEK_Complex && 2655 "DestType must have complex evaluation kind."); 2656 CodeGenFunction::ComplexPairTy ComplexVal; 2657 if (Val.isScalar()) { 2658 // Convert the input element to the element type of the complex. 2659 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 2660 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, 2661 DestElementType, Loc); 2662 ComplexVal = CodeGenFunction::ComplexPairTy( 2663 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); 2664 } else { 2665 assert(Val.isComplex() && "Must be a scalar or complex."); 2666 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); 2667 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 2668 ComplexVal.first = CGF.EmitScalarConversion( 2669 Val.getComplexVal().first, SrcElementType, DestElementType, Loc); 2670 ComplexVal.second = CGF.EmitScalarConversion( 2671 Val.getComplexVal().second, SrcElementType, DestElementType, Loc); 2672 } 2673 return ComplexVal; 2674 } 2675 2676 static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, 2677 LValue LVal, RValue RVal) { 2678 if (LVal.isGlobalReg()) { 2679 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); 2680 } else { 2681 CGF.EmitAtomicStore(RVal, LVal, 2682 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2683 : llvm::AtomicOrdering::Monotonic, 2684 LVal.isVolatile(), /*IsInit=*/false); 2685 } 2686 } 2687 2688 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, 2689 QualType RValTy, SourceLocation Loc) { 2690 switch (getEvaluationKind(LVal.getType())) { 2691 case TEK_Scalar: 2692 EmitStoreThroughLValue(RValue::get(convertToScalarValue( 2693 *this, RVal, RValTy, LVal.getType(), Loc)), 2694 LVal); 2695 break; 2696 case TEK_Complex: 2697 EmitStoreOfComplex( 2698 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, 2699 /*isInit=*/false); 2700 break; 2701 case TEK_Aggregate: 2702 llvm_unreachable("Must be a scalar or complex."); 2703 } 2704 } 2705 2706 static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, 2707 const Expr *X, const Expr *V, 2708 SourceLocation Loc) { 2709 // v = x; 2710 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); 2711 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); 2712 LValue XLValue = CGF.EmitLValue(X); 2713 LValue VLValue = CGF.EmitLValue(V); 2714 RValue Res = XLValue.isGlobalReg() 2715 ? CGF.EmitLoadOfLValue(XLValue, Loc) 2716 : CGF.EmitAtomicLoad( 2717 XLValue, Loc, 2718 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2719 : llvm::AtomicOrdering::Monotonic, 2720 XLValue.isVolatile()); 2721 // OpenMP, 2.12.6, atomic Construct 2722 // Any atomic construct with a seq_cst clause forces the atomically 2723 // performed operation to include an implicit flush operation without a 2724 // list. 2725 if (IsSeqCst) 2726 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2727 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); 2728 } 2729 2730 static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, 2731 const Expr *X, const Expr *E, 2732 SourceLocation Loc) { 2733 // x = expr; 2734 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); 2735 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); 2736 // OpenMP, 2.12.6, atomic Construct 2737 // Any atomic construct with a seq_cst clause forces the atomically 2738 // performed operation to include an implicit flush operation without a 2739 // list. 2740 if (IsSeqCst) 2741 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2742 } 2743 2744 static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, 2745 RValue Update, 2746 BinaryOperatorKind BO, 2747 llvm::AtomicOrdering AO, 2748 bool IsXLHSInRHSPart) { 2749 auto &Context = CGF.CGM.getContext(); 2750 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x' 2751 // expression is simple and atomic is allowed for the given type for the 2752 // target platform. 2753 if (BO == BO_Comma || !Update.isScalar() || 2754 !Update.getScalarVal()->getType()->isIntegerTy() || 2755 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && 2756 (Update.getScalarVal()->getType() != 2757 X.getAddress().getElementType())) || 2758 !X.getAddress().getElementType()->isIntegerTy() || 2759 !Context.getTargetInfo().hasBuiltinAtomic( 2760 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) 2761 return std::make_pair(false, RValue::get(nullptr)); 2762 2763 llvm::AtomicRMWInst::BinOp RMWOp; 2764 switch (BO) { 2765 case BO_Add: 2766 RMWOp = llvm::AtomicRMWInst::Add; 2767 break; 2768 case BO_Sub: 2769 if (!IsXLHSInRHSPart) 2770 return std::make_pair(false, RValue::get(nullptr)); 2771 RMWOp = llvm::AtomicRMWInst::Sub; 2772 break; 2773 case BO_And: 2774 RMWOp = llvm::AtomicRMWInst::And; 2775 break; 2776 case BO_Or: 2777 RMWOp = llvm::AtomicRMWInst::Or; 2778 break; 2779 case BO_Xor: 2780 RMWOp = llvm::AtomicRMWInst::Xor; 2781 break; 2782 case BO_LT: 2783 RMWOp = X.getType()->hasSignedIntegerRepresentation() 2784 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min 2785 : llvm::AtomicRMWInst::Max) 2786 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin 2787 : llvm::AtomicRMWInst::UMax); 2788 break; 2789 case BO_GT: 2790 RMWOp = X.getType()->hasSignedIntegerRepresentation() 2791 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max 2792 : llvm::AtomicRMWInst::Min) 2793 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax 2794 : llvm::AtomicRMWInst::UMin); 2795 break; 2796 case BO_Assign: 2797 RMWOp = llvm::AtomicRMWInst::Xchg; 2798 break; 2799 case BO_Mul: 2800 case BO_Div: 2801 case BO_Rem: 2802 case BO_Shl: 2803 case BO_Shr: 2804 case BO_LAnd: 2805 case BO_LOr: 2806 return std::make_pair(false, RValue::get(nullptr)); 2807 case BO_PtrMemD: 2808 case BO_PtrMemI: 2809 case BO_LE: 2810 case BO_GE: 2811 case BO_EQ: 2812 case BO_NE: 2813 case BO_AddAssign: 2814 case BO_SubAssign: 2815 case BO_AndAssign: 2816 case BO_OrAssign: 2817 case BO_XorAssign: 2818 case BO_MulAssign: 2819 case BO_DivAssign: 2820 case BO_RemAssign: 2821 case BO_ShlAssign: 2822 case BO_ShrAssign: 2823 case BO_Comma: 2824 llvm_unreachable("Unsupported atomic update operation"); 2825 } 2826 auto *UpdateVal = Update.getScalarVal(); 2827 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { 2828 UpdateVal = CGF.Builder.CreateIntCast( 2829 IC, X.getAddress().getElementType(), 2830 X.getType()->hasSignedIntegerRepresentation()); 2831 } 2832 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); 2833 return std::make_pair(true, RValue::get(Res)); 2834 } 2835 2836 std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( 2837 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, 2838 llvm::AtomicOrdering AO, SourceLocation Loc, 2839 const llvm::function_ref<RValue(RValue)> &CommonGen) { 2840 // Update expressions are allowed to have the following forms: 2841 // x binop= expr; -> xrval + expr; 2842 // x++, ++x -> xrval + 1; 2843 // x--, --x -> xrval - 1; 2844 // x = x binop expr; -> xrval binop expr 2845 // x = expr Op x; - > expr binop xrval; 2846 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); 2847 if (!Res.first) { 2848 if (X.isGlobalReg()) { 2849 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop 2850 // 'xrval'. 2851 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); 2852 } else { 2853 // Perform compare-and-swap procedure. 2854 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); 2855 } 2856 } 2857 return Res; 2858 } 2859 2860 static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, 2861 const Expr *X, const Expr *E, 2862 const Expr *UE, bool IsXLHSInRHSPart, 2863 SourceLocation Loc) { 2864 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 2865 "Update expr in 'atomic update' must be a binary operator."); 2866 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 2867 // Update expressions are allowed to have the following forms: 2868 // x binop= expr; -> xrval + expr; 2869 // x++, ++x -> xrval + 1; 2870 // x--, --x -> xrval - 1; 2871 // x = x binop expr; -> xrval binop expr 2872 // x = expr Op x; - > expr binop xrval; 2873 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); 2874 LValue XLValue = CGF.EmitLValue(X); 2875 RValue ExprRValue = CGF.EmitAnyExpr(E); 2876 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2877 : llvm::AtomicOrdering::Monotonic; 2878 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 2879 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 2880 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 2881 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 2882 auto Gen = 2883 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { 2884 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 2885 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 2886 return CGF.EmitAnyExpr(UE); 2887 }; 2888 (void)CGF.EmitOMPAtomicSimpleUpdateExpr( 2889 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 2890 // OpenMP, 2.12.6, atomic Construct 2891 // Any atomic construct with a seq_cst clause forces the atomically 2892 // performed operation to include an implicit flush operation without a 2893 // list. 2894 if (IsSeqCst) 2895 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2896 } 2897 2898 static RValue convertToType(CodeGenFunction &CGF, RValue Value, 2899 QualType SourceType, QualType ResType, 2900 SourceLocation Loc) { 2901 switch (CGF.getEvaluationKind(ResType)) { 2902 case TEK_Scalar: 2903 return RValue::get( 2904 convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); 2905 case TEK_Complex: { 2906 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); 2907 return RValue::getComplex(Res.first, Res.second); 2908 } 2909 case TEK_Aggregate: 2910 break; 2911 } 2912 llvm_unreachable("Must be a scalar or complex."); 2913 } 2914 2915 static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, 2916 bool IsPostfixUpdate, const Expr *V, 2917 const Expr *X, const Expr *E, 2918 const Expr *UE, bool IsXLHSInRHSPart, 2919 SourceLocation Loc) { 2920 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); 2921 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); 2922 RValue NewVVal; 2923 LValue VLValue = CGF.EmitLValue(V); 2924 LValue XLValue = CGF.EmitLValue(X); 2925 RValue ExprRValue = CGF.EmitAnyExpr(E); 2926 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2927 : llvm::AtomicOrdering::Monotonic; 2928 QualType NewVValType; 2929 if (UE) { 2930 // 'x' is updated with some additional value. 2931 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 2932 "Update expr in 'atomic capture' must be a binary operator."); 2933 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 2934 // Update expressions are allowed to have the following forms: 2935 // x binop= expr; -> xrval + expr; 2936 // x++, ++x -> xrval + 1; 2937 // x--, --x -> xrval - 1; 2938 // x = x binop expr; -> xrval binop expr 2939 // x = expr Op x; - > expr binop xrval; 2940 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 2941 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 2942 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 2943 NewVValType = XRValExpr->getType(); 2944 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 2945 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, 2946 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue { 2947 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 2948 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 2949 RValue Res = CGF.EmitAnyExpr(UE); 2950 NewVVal = IsPostfixUpdate ? XRValue : Res; 2951 return Res; 2952 }; 2953 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 2954 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 2955 if (Res.first) { 2956 // 'atomicrmw' instruction was generated. 2957 if (IsPostfixUpdate) { 2958 // Use old value from 'atomicrmw'. 2959 NewVVal = Res.second; 2960 } else { 2961 // 'atomicrmw' does not provide new value, so evaluate it using old 2962 // value of 'x'. 2963 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 2964 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); 2965 NewVVal = CGF.EmitAnyExpr(UE); 2966 } 2967 } 2968 } else { 2969 // 'x' is simply rewritten with some 'expr'. 2970 NewVValType = X->getType().getNonReferenceType(); 2971 ExprRValue = convertToType(CGF, ExprRValue, E->getType(), 2972 X->getType().getNonReferenceType(), Loc); 2973 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue { 2974 NewVVal = XRValue; 2975 return ExprRValue; 2976 }; 2977 // Try to perform atomicrmw xchg, otherwise simple exchange. 2978 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 2979 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, 2980 Loc, Gen); 2981 if (Res.first) { 2982 // 'atomicrmw' instruction was generated. 2983 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; 2984 } 2985 } 2986 // Emit post-update store to 'v' of old/new 'x' value. 2987 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); 2988 // OpenMP, 2.12.6, atomic Construct 2989 // Any atomic construct with a seq_cst clause forces the atomically 2990 // performed operation to include an implicit flush operation without a 2991 // list. 2992 if (IsSeqCst) 2993 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2994 } 2995 2996 static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, 2997 bool IsSeqCst, bool IsPostfixUpdate, 2998 const Expr *X, const Expr *V, const Expr *E, 2999 const Expr *UE, bool IsXLHSInRHSPart, 3000 SourceLocation Loc) { 3001 switch (Kind) { 3002 case OMPC_read: 3003 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); 3004 break; 3005 case OMPC_write: 3006 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); 3007 break; 3008 case OMPC_unknown: 3009 case OMPC_update: 3010 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); 3011 break; 3012 case OMPC_capture: 3013 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, 3014 IsXLHSInRHSPart, Loc); 3015 break; 3016 case OMPC_if: 3017 case OMPC_final: 3018 case OMPC_num_threads: 3019 case OMPC_private: 3020 case OMPC_firstprivate: 3021 case OMPC_lastprivate: 3022 case OMPC_reduction: 3023 case OMPC_safelen: 3024 case OMPC_simdlen: 3025 case OMPC_collapse: 3026 case OMPC_default: 3027 case OMPC_seq_cst: 3028 case OMPC_shared: 3029 case OMPC_linear: 3030 case OMPC_aligned: 3031 case OMPC_copyin: 3032 case OMPC_copyprivate: 3033 case OMPC_flush: 3034 case OMPC_proc_bind: 3035 case OMPC_schedule: 3036 case OMPC_ordered: 3037 case OMPC_nowait: 3038 case OMPC_untied: 3039 case OMPC_threadprivate: 3040 case OMPC_depend: 3041 case OMPC_mergeable: 3042 case OMPC_device: 3043 case OMPC_threads: 3044 case OMPC_simd: 3045 case OMPC_map: 3046 case OMPC_num_teams: 3047 case OMPC_thread_limit: 3048 case OMPC_priority: 3049 case OMPC_grainsize: 3050 case OMPC_nogroup: 3051 case OMPC_num_tasks: 3052 case OMPC_hint: 3053 case OMPC_dist_schedule: 3054 case OMPC_defaultmap: 3055 case OMPC_uniform: 3056 llvm_unreachable("Clause is not allowed in 'omp atomic'."); 3057 } 3058 } 3059 3060 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { 3061 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); 3062 OpenMPClauseKind Kind = OMPC_unknown; 3063 for (auto *C : S.clauses()) { 3064 // Find first clause (skip seq_cst clause, if it is first). 3065 if (C->getClauseKind() != OMPC_seq_cst) { 3066 Kind = C->getClauseKind(); 3067 break; 3068 } 3069 } 3070 3071 const auto *CS = 3072 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); 3073 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { 3074 enterFullExpression(EWC); 3075 } 3076 // Processing for statements under 'atomic capture'. 3077 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { 3078 for (const auto *C : Compound->body()) { 3079 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { 3080 enterFullExpression(EWC); 3081 } 3082 } 3083 } 3084 3085 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, 3086 PrePostActionTy &) { 3087 CGF.EmitStopPoint(CS); 3088 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), 3089 S.getV(), S.getExpr(), S.getUpdateExpr(), 3090 S.isXLHSInRHSPart(), S.getLocStart()); 3091 }; 3092 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 3093 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); 3094 } 3095 3096 std::pair<llvm::Function * /*OutlinedFn*/, llvm::Constant * /*OutlinedFnID*/> 3097 CodeGenFunction::EmitOMPTargetDirectiveOutlinedFunction( 3098 CodeGenModule &CGM, const OMPTargetDirective &S, StringRef ParentName, 3099 bool IsOffloadEntry) { 3100 llvm::Function *OutlinedFn = nullptr; 3101 llvm::Constant *OutlinedFnID = nullptr; 3102 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3103 OMPPrivateScope PrivateScope(CGF); 3104 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 3105 CGF.EmitOMPPrivateClause(S, PrivateScope); 3106 (void)PrivateScope.Privatize(); 3107 3108 Action.Enter(CGF); 3109 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3110 }; 3111 // Emit target region as a standalone region. 3112 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 3113 S, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, CodeGen); 3114 return std::make_pair(OutlinedFn, OutlinedFnID); 3115 } 3116 3117 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { 3118 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt()); 3119 3120 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 3121 GenerateOpenMPCapturedVars(CS, CapturedVars); 3122 3123 llvm::Function *Fn = nullptr; 3124 llvm::Constant *FnID = nullptr; 3125 3126 // Check if we have any if clause associated with the directive. 3127 const Expr *IfCond = nullptr; 3128 3129 if (auto *C = S.getSingleClause<OMPIfClause>()) { 3130 IfCond = C->getCondition(); 3131 } 3132 3133 // Check if we have any device clause associated with the directive. 3134 const Expr *Device = nullptr; 3135 if (auto *C = S.getSingleClause<OMPDeviceClause>()) { 3136 Device = C->getDevice(); 3137 } 3138 3139 // Check if we have an if clause whose conditional always evaluates to false 3140 // or if we do not have any targets specified. If so the target region is not 3141 // an offload entry point. 3142 bool IsOffloadEntry = true; 3143 if (IfCond) { 3144 bool Val; 3145 if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) 3146 IsOffloadEntry = false; 3147 } 3148 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3149 IsOffloadEntry = false; 3150 3151 assert(CurFuncDecl && "No parent declaration for target region!"); 3152 StringRef ParentName; 3153 // In case we have Ctors/Dtors we use the complete type variant to produce 3154 // the mangling of the device outlined kernel. 3155 if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl)) 3156 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); 3157 else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl)) 3158 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); 3159 else 3160 ParentName = 3161 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl))); 3162 3163 std::tie(Fn, FnID) = EmitOMPTargetDirectiveOutlinedFunction( 3164 CGM, S, ParentName, IsOffloadEntry); 3165 OMPLexicalScope Scope(*this, S); 3166 CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device, 3167 CapturedVars); 3168 } 3169 3170 static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, 3171 const OMPExecutableDirective &S, 3172 OpenMPDirectiveKind InnermostKind, 3173 const RegionCodeGenTy &CodeGen) { 3174 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 3175 auto OutlinedFn = CGF.CGM.getOpenMPRuntime(). 3176 emitParallelOrTeamsOutlinedFunction(S, 3177 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 3178 3179 const OMPTeamsDirective &TD = *dyn_cast<OMPTeamsDirective>(&S); 3180 const OMPNumTeamsClause *NT = TD.getSingleClause<OMPNumTeamsClause>(); 3181 const OMPThreadLimitClause *TL = TD.getSingleClause<OMPThreadLimitClause>(); 3182 if (NT || TL) { 3183 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr; 3184 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr; 3185 3186 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, 3187 S.getLocStart()); 3188 } 3189 3190 OMPLexicalScope Scope(CGF, S); 3191 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 3192 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 3193 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn, 3194 CapturedVars); 3195 } 3196 3197 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { 3198 // Emit parallel region as a standalone region. 3199 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 3200 OMPPrivateScope PrivateScope(CGF); 3201 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 3202 CGF.EmitOMPPrivateClause(S, PrivateScope); 3203 (void)PrivateScope.Privatize(); 3204 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3205 }; 3206 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen); 3207 } 3208 3209 void CodeGenFunction::EmitOMPCancellationPointDirective( 3210 const OMPCancellationPointDirective &S) { 3211 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), 3212 S.getCancelRegion()); 3213 } 3214 3215 void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { 3216 const Expr *IfCond = nullptr; 3217 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 3218 if (C->getNameModifier() == OMPD_unknown || 3219 C->getNameModifier() == OMPD_cancel) { 3220 IfCond = C->getCondition(); 3221 break; 3222 } 3223 } 3224 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, 3225 S.getCancelRegion()); 3226 } 3227 3228 CodeGenFunction::JumpDest 3229 CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { 3230 if (Kind == OMPD_parallel || Kind == OMPD_task) 3231 return ReturnBlock; 3232 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || 3233 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for); 3234 return BreakContinueStack.back().BreakBlock; 3235 } 3236 3237 // Generate the instructions for '#pragma omp target data' directive. 3238 void CodeGenFunction::EmitOMPTargetDataDirective( 3239 const OMPTargetDataDirective &S) { 3240 // The target data enclosed region is implemented just by emitting the 3241 // statement. 3242 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 3243 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3244 }; 3245 3246 // If we don't have target devices, don't bother emitting the data mapping 3247 // code. 3248 if (CGM.getLangOpts().OMPTargetTriples.empty()) { 3249 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 3250 3251 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_target_data, 3252 CodeGen); 3253 return; 3254 } 3255 3256 // Check if we have any if clause associated with the directive. 3257 const Expr *IfCond = nullptr; 3258 if (auto *C = S.getSingleClause<OMPIfClause>()) 3259 IfCond = C->getCondition(); 3260 3261 // Check if we have any device clause associated with the directive. 3262 const Expr *Device = nullptr; 3263 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 3264 Device = C->getDevice(); 3265 3266 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, CodeGen); 3267 } 3268 3269 void CodeGenFunction::EmitOMPTargetEnterDataDirective( 3270 const OMPTargetEnterDataDirective &S) { 3271 // If we don't have target devices, don't bother emitting the data mapping 3272 // code. 3273 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3274 return; 3275 3276 // Check if we have any if clause associated with the directive. 3277 const Expr *IfCond = nullptr; 3278 if (auto *C = S.getSingleClause<OMPIfClause>()) 3279 IfCond = C->getCondition(); 3280 3281 // Check if we have any device clause associated with the directive. 3282 const Expr *Device = nullptr; 3283 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 3284 Device = C->getDevice(); 3285 3286 CGM.getOpenMPRuntime().emitTargetEnterOrExitDataCall(*this, S, IfCond, 3287 Device); 3288 } 3289 3290 void CodeGenFunction::EmitOMPTargetExitDataDirective( 3291 const OMPTargetExitDataDirective &S) { 3292 // If we don't have target devices, don't bother emitting the data mapping 3293 // code. 3294 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3295 return; 3296 3297 // Check if we have any if clause associated with the directive. 3298 const Expr *IfCond = nullptr; 3299 if (auto *C = S.getSingleClause<OMPIfClause>()) 3300 IfCond = C->getCondition(); 3301 3302 // Check if we have any device clause associated with the directive. 3303 const Expr *Device = nullptr; 3304 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 3305 Device = C->getDevice(); 3306 3307 CGM.getOpenMPRuntime().emitTargetEnterOrExitDataCall(*this, S, IfCond, 3308 Device); 3309 } 3310 3311 void CodeGenFunction::EmitOMPTargetParallelDirective( 3312 const OMPTargetParallelDirective &S) { 3313 // TODO: codegen for target parallel. 3314 } 3315 3316 void CodeGenFunction::EmitOMPTargetParallelForDirective( 3317 const OMPTargetParallelForDirective &S) { 3318 // TODO: codegen for target parallel for. 3319 } 3320 3321 /// Emit a helper variable and return corresponding lvalue. 3322 static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, 3323 const ImplicitParamDecl *PVD, 3324 CodeGenFunction::OMPPrivateScope &Privates) { 3325 auto *VDecl = cast<VarDecl>(Helper->getDecl()); 3326 Privates.addPrivate( 3327 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); }); 3328 } 3329 3330 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { 3331 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); 3332 // Emit outlined function for task construct. 3333 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 3334 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 3335 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 3336 const Expr *IfCond = nullptr; 3337 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 3338 if (C->getNameModifier() == OMPD_unknown || 3339 C->getNameModifier() == OMPD_taskloop) { 3340 IfCond = C->getCondition(); 3341 break; 3342 } 3343 } 3344 3345 OMPTaskDataTy Data; 3346 // Check if taskloop must be emitted without taskgroup. 3347 Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); 3348 // TODO: Check if we should emit tied or untied task. 3349 Data.Tied = true; 3350 // Set scheduling for taskloop 3351 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { 3352 // grainsize clause 3353 Data.Schedule.setInt(/*IntVal=*/false); 3354 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); 3355 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { 3356 // num_tasks clause 3357 Data.Schedule.setInt(/*IntVal=*/true); 3358 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); 3359 } 3360 3361 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { 3362 // if (PreCond) { 3363 // for (IV in 0..LastIteration) BODY; 3364 // <Final counter/linear vars updates>; 3365 // } 3366 // 3367 3368 // Emit: if (PreCond) - begin. 3369 // If the condition constant folds and can be elided, avoid emitting the 3370 // whole loop. 3371 bool CondConstant; 3372 llvm::BasicBlock *ContBlock = nullptr; 3373 OMPLoopScope PreInitScope(CGF, S); 3374 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 3375 if (!CondConstant) 3376 return; 3377 } else { 3378 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); 3379 ContBlock = CGF.createBasicBlock("taskloop.if.end"); 3380 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 3381 CGF.getProfileCount(&S)); 3382 CGF.EmitBlock(ThenBlock); 3383 CGF.incrementProfileCounter(&S); 3384 } 3385 3386 if (isOpenMPSimdDirective(S.getDirectiveKind())) 3387 CGF.EmitOMPSimdInit(S); 3388 3389 OMPPrivateScope LoopScope(CGF); 3390 // Emit helper vars inits. 3391 enum { LowerBound = 5, UpperBound, Stride, LastIter }; 3392 auto *I = CS->getCapturedDecl()->param_begin(); 3393 auto *LBP = std::next(I, LowerBound); 3394 auto *UBP = std::next(I, UpperBound); 3395 auto *STP = std::next(I, Stride); 3396 auto *LIP = std::next(I, LastIter); 3397 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, 3398 LoopScope); 3399 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, 3400 LoopScope); 3401 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); 3402 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, 3403 LoopScope); 3404 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 3405 (void)LoopScope.Privatize(); 3406 // Emit the loop iteration variable. 3407 const Expr *IVExpr = S.getIterationVariable(); 3408 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 3409 CGF.EmitVarDecl(*IVDecl); 3410 CGF.EmitIgnoredExpr(S.getInit()); 3411 3412 // Emit the iterations count variable. 3413 // If it is not a variable, Sema decided to calculate iterations count on 3414 // each iteration (e.g., it is foldable into a constant). 3415 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 3416 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 3417 // Emit calculation of the iterations count. 3418 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 3419 } 3420 3421 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 3422 S.getInc(), 3423 [&S](CodeGenFunction &CGF) { 3424 CGF.EmitOMPLoopBody(S, JumpDest()); 3425 CGF.EmitStopPoint(&S); 3426 }, 3427 [](CodeGenFunction &) {}); 3428 // Emit: if (PreCond) - end. 3429 if (ContBlock) { 3430 CGF.EmitBranch(ContBlock); 3431 CGF.EmitBlock(ContBlock, true); 3432 } 3433 }; 3434 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 3435 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, 3436 const OMPTaskDataTy &Data) { 3437 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) { 3438 OMPLoopScope PreInitScope(CGF, S); 3439 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S, 3440 OutlinedFn, SharedsTy, 3441 CapturedStruct, IfCond, Data); 3442 }; 3443 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, 3444 CodeGen); 3445 }; 3446 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); 3447 } 3448 3449 void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { 3450 EmitOMPTaskLoopBasedDirective(S); 3451 } 3452 3453 void CodeGenFunction::EmitOMPTaskLoopSimdDirective( 3454 const OMPTaskLoopSimdDirective &S) { 3455 EmitOMPTaskLoopBasedDirective(S); 3456 } 3457