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 if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) 744 break; 745 auto IRef = C->varlist_begin(); 746 auto IDestRef = C->destination_exprs().begin(); 747 for (auto *IInit : C->private_copies()) { 748 // Keep the address of the original variable for future update at the end 749 // of the loop. 750 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 751 // Taskloops do not require additional initialization, it is done in 752 // runtime support library. 753 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { 754 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 755 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address { 756 DeclRefExpr DRE( 757 const_cast<VarDecl *>(OrigVD), 758 /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( 759 OrigVD) != nullptr, 760 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 761 return EmitLValue(&DRE).getAddress(); 762 }); 763 // Check if the variable is also a firstprivate: in this case IInit is 764 // not generated. Initialization of this variable will happen in codegen 765 // for 'firstprivate' clause. 766 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { 767 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 768 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { 769 // Emit private VarDecl with copy init. 770 EmitDecl(*VD); 771 return GetAddrOfLocalVar(VD); 772 }); 773 assert(IsRegistered && 774 "lastprivate var already registered as private"); 775 (void)IsRegistered; 776 } 777 } 778 ++IRef; 779 ++IDestRef; 780 } 781 } 782 return HasAtLeastOneLastprivate; 783 } 784 785 void CodeGenFunction::EmitOMPLastprivateClauseFinal( 786 const OMPExecutableDirective &D, bool NoFinals, 787 llvm::Value *IsLastIterCond) { 788 if (!HaveInsertPoint()) 789 return; 790 // Emit following code: 791 // if (<IsLastIterCond>) { 792 // orig_var1 = private_orig_var1; 793 // ... 794 // orig_varn = private_orig_varn; 795 // } 796 llvm::BasicBlock *ThenBB = nullptr; 797 llvm::BasicBlock *DoneBB = nullptr; 798 if (IsLastIterCond) { 799 ThenBB = createBasicBlock(".omp.lastprivate.then"); 800 DoneBB = createBasicBlock(".omp.lastprivate.done"); 801 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); 802 EmitBlock(ThenBB); 803 } 804 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; 805 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; 806 if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { 807 auto IC = LoopDirective->counters().begin(); 808 for (auto F : LoopDirective->finals()) { 809 auto *D = 810 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); 811 if (NoFinals) 812 AlreadyEmittedVars.insert(D); 813 else 814 LoopCountersAndUpdates[D] = F; 815 ++IC; 816 } 817 } 818 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 819 auto IRef = C->varlist_begin(); 820 auto ISrcRef = C->source_exprs().begin(); 821 auto IDestRef = C->destination_exprs().begin(); 822 for (auto *AssignOp : C->assignment_ops()) { 823 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 824 QualType Type = PrivateVD->getType(); 825 auto *CanonicalVD = PrivateVD->getCanonicalDecl(); 826 if (AlreadyEmittedVars.insert(CanonicalVD).second) { 827 // If lastprivate variable is a loop control variable for loop-based 828 // directive, update its value before copyin back to original 829 // variable. 830 if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) 831 EmitIgnoredExpr(FinalExpr); 832 auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); 833 auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 834 // Get the address of the original variable. 835 Address OriginalAddr = GetAddrOfLocalVar(DestVD); 836 // Get the address of the private variable. 837 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); 838 if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>()) 839 PrivateAddr = 840 Address(Builder.CreateLoad(PrivateAddr), 841 getNaturalTypeAlignment(RefTy->getPointeeType())); 842 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); 843 } 844 ++IRef; 845 ++ISrcRef; 846 ++IDestRef; 847 } 848 if (auto *PostUpdate = C->getPostUpdateExpr()) 849 EmitIgnoredExpr(PostUpdate); 850 } 851 if (IsLastIterCond) 852 EmitBlock(DoneBB, /*IsFinished=*/true); 853 } 854 855 static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, 856 LValue BaseLV, llvm::Value *Addr) { 857 Address Tmp = Address::invalid(); 858 Address TopTmp = Address::invalid(); 859 Address MostTopTmp = Address::invalid(); 860 BaseTy = BaseTy.getNonReferenceType(); 861 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && 862 !CGF.getContext().hasSameType(BaseTy, ElTy)) { 863 Tmp = CGF.CreateMemTemp(BaseTy); 864 if (TopTmp.isValid()) 865 CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); 866 else 867 MostTopTmp = Tmp; 868 TopTmp = Tmp; 869 BaseTy = BaseTy->getPointeeType(); 870 } 871 llvm::Type *Ty = BaseLV.getPointer()->getType(); 872 if (Tmp.isValid()) 873 Ty = Tmp.getElementType(); 874 Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); 875 if (Tmp.isValid()) { 876 CGF.Builder.CreateStore(Addr, Tmp); 877 return MostTopTmp; 878 } 879 return Address(Addr, BaseLV.getAlignment()); 880 } 881 882 static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, 883 LValue BaseLV) { 884 BaseTy = BaseTy.getNonReferenceType(); 885 while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && 886 !CGF.getContext().hasSameType(BaseTy, ElTy)) { 887 if (auto *PtrTy = BaseTy->getAs<PointerType>()) 888 BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); 889 else { 890 BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(), 891 BaseTy->castAs<ReferenceType>()); 892 } 893 BaseTy = BaseTy->getPointeeType(); 894 } 895 return CGF.MakeAddrLValue( 896 Address( 897 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( 898 BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()), 899 BaseLV.getAlignment()), 900 BaseLV.getType(), BaseLV.getAlignmentSource()); 901 } 902 903 void CodeGenFunction::EmitOMPReductionClauseInit( 904 const OMPExecutableDirective &D, 905 CodeGenFunction::OMPPrivateScope &PrivateScope) { 906 if (!HaveInsertPoint()) 907 return; 908 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 909 auto ILHS = C->lhs_exprs().begin(); 910 auto IRHS = C->rhs_exprs().begin(); 911 auto IPriv = C->privates().begin(); 912 auto IRed = C->reduction_ops().begin(); 913 for (auto IRef : C->varlists()) { 914 auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); 915 auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); 916 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); 917 auto *DRD = getReductionInit(*IRed); 918 if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) { 919 auto *Base = OASE->getBase()->IgnoreParenImpCasts(); 920 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) 921 Base = TempOASE->getBase()->IgnoreParenImpCasts(); 922 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 923 Base = TempASE->getBase()->IgnoreParenImpCasts(); 924 auto *DE = cast<DeclRefExpr>(Base); 925 auto *OrigVD = cast<VarDecl>(DE->getDecl()); 926 auto OASELValueLB = EmitOMPArraySectionExpr(OASE); 927 auto OASELValueUB = 928 EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); 929 auto OriginalBaseLValue = EmitLValue(DE); 930 LValue BaseLValue = 931 loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(), 932 OriginalBaseLValue); 933 // Store the address of the original variable associated with the LHS 934 // implicit variable. 935 PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address { 936 return OASELValueLB.getAddress(); 937 }); 938 // Emit reduction copy. 939 bool IsRegistered = PrivateScope.addPrivate( 940 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB, 941 OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address { 942 // Emit VarDecl with copy init for arrays. 943 // Get the address of the original variable captured in current 944 // captured region. 945 auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(), 946 OASELValueLB.getPointer()); 947 Size = Builder.CreateNUWAdd( 948 Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); 949 CodeGenFunction::OpaqueValueMapping OpaqueMap( 950 *this, cast<OpaqueValueExpr>( 951 getContext() 952 .getAsVariableArrayType(PrivateVD->getType()) 953 ->getSizeExpr()), 954 RValue::get(Size)); 955 EmitVariablyModifiedType(PrivateVD->getType()); 956 auto Emission = EmitAutoVarAlloca(*PrivateVD); 957 auto Addr = Emission.getAllocatedAddress(); 958 auto *Init = PrivateVD->getInit(); 959 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), 960 DRD ? *IRed : Init, 961 OASELValueLB.getAddress()); 962 EmitAutoVarCleanups(Emission); 963 // Emit private VarDecl with reduction init. 964 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), 965 OASELValueLB.getPointer()); 966 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); 967 return castToBase(*this, OrigVD->getType(), 968 OASELValueLB.getType(), OriginalBaseLValue, 969 Ptr); 970 }); 971 assert(IsRegistered && "private var already registered as private"); 972 // Silence the warning about unused variable. 973 (void)IsRegistered; 974 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { 975 return GetAddrOfLocalVar(PrivateVD); 976 }); 977 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) { 978 auto *Base = ASE->getBase()->IgnoreParenImpCasts(); 979 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) 980 Base = TempASE->getBase()->IgnoreParenImpCasts(); 981 auto *DE = cast<DeclRefExpr>(Base); 982 auto *OrigVD = cast<VarDecl>(DE->getDecl()); 983 auto ASELValue = EmitLValue(ASE); 984 auto OriginalBaseLValue = EmitLValue(DE); 985 LValue BaseLValue = loadToBegin( 986 *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue); 987 // Store the address of the original variable associated with the LHS 988 // implicit variable. 989 PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address { 990 return ASELValue.getAddress(); 991 }); 992 // Emit reduction copy. 993 bool IsRegistered = PrivateScope.addPrivate( 994 OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue, 995 OriginalBaseLValue, DRD, IRed]() -> Address { 996 // Emit private VarDecl with reduction init. 997 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); 998 auto Addr = Emission.getAllocatedAddress(); 999 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { 1000 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, 1001 ASELValue.getAddress(), 1002 ASELValue.getType()); 1003 } else 1004 EmitAutoVarInit(Emission); 1005 EmitAutoVarCleanups(Emission); 1006 auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), 1007 ASELValue.getPointer()); 1008 auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); 1009 return castToBase(*this, OrigVD->getType(), ASELValue.getType(), 1010 OriginalBaseLValue, Ptr); 1011 }); 1012 assert(IsRegistered && "private var already registered as private"); 1013 // Silence the warning about unused variable. 1014 (void)IsRegistered; 1015 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { 1016 return Builder.CreateElementBitCast( 1017 GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()), 1018 "rhs.begin"); 1019 }); 1020 } else { 1021 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); 1022 QualType Type = PrivateVD->getType(); 1023 if (getContext().getAsArrayType(Type)) { 1024 // Store the address of the original variable associated with the LHS 1025 // implicit variable. 1026 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1027 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1028 IRef->getType(), VK_LValue, IRef->getExprLoc()); 1029 Address OriginalAddr = EmitLValue(&DRE).getAddress(); 1030 PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr, 1031 LHSVD]() -> Address { 1032 OriginalAddr = Builder.CreateElementBitCast( 1033 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); 1034 return OriginalAddr; 1035 }); 1036 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { 1037 if (Type->isVariablyModifiedType()) { 1038 CodeGenFunction::OpaqueValueMapping OpaqueMap( 1039 *this, cast<OpaqueValueExpr>( 1040 getContext() 1041 .getAsVariableArrayType(PrivateVD->getType()) 1042 ->getSizeExpr()), 1043 RValue::get( 1044 getTypeSize(OrigVD->getType().getNonReferenceType()))); 1045 EmitVariablyModifiedType(Type); 1046 } 1047 auto Emission = EmitAutoVarAlloca(*PrivateVD); 1048 auto Addr = Emission.getAllocatedAddress(); 1049 auto *Init = PrivateVD->getInit(); 1050 EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), 1051 DRD ? *IRed : Init, OriginalAddr); 1052 EmitAutoVarCleanups(Emission); 1053 return Emission.getAllocatedAddress(); 1054 }); 1055 assert(IsRegistered && "private var already registered as private"); 1056 // Silence the warning about unused variable. 1057 (void)IsRegistered; 1058 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { 1059 return Builder.CreateElementBitCast( 1060 GetAddrOfLocalVar(PrivateVD), 1061 ConvertTypeForMem(RHSVD->getType()), "rhs.begin"); 1062 }); 1063 } else { 1064 // Store the address of the original variable associated with the LHS 1065 // implicit variable. 1066 Address OriginalAddr = Address::invalid(); 1067 PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef, 1068 &OriginalAddr]() -> Address { 1069 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1070 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1071 IRef->getType(), VK_LValue, IRef->getExprLoc()); 1072 OriginalAddr = EmitLValue(&DRE).getAddress(); 1073 return OriginalAddr; 1074 }); 1075 // Emit reduction copy. 1076 bool IsRegistered = PrivateScope.addPrivate( 1077 OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address { 1078 // Emit private VarDecl with reduction init. 1079 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); 1080 auto Addr = Emission.getAllocatedAddress(); 1081 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { 1082 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, 1083 OriginalAddr, 1084 PrivateVD->getType()); 1085 } else 1086 EmitAutoVarInit(Emission); 1087 EmitAutoVarCleanups(Emission); 1088 return Addr; 1089 }); 1090 assert(IsRegistered && "private var already registered as private"); 1091 // Silence the warning about unused variable. 1092 (void)IsRegistered; 1093 PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { 1094 return GetAddrOfLocalVar(PrivateVD); 1095 }); 1096 } 1097 } 1098 ++ILHS; 1099 ++IRHS; 1100 ++IPriv; 1101 ++IRed; 1102 } 1103 } 1104 } 1105 1106 void CodeGenFunction::EmitOMPReductionClauseFinal( 1107 const OMPExecutableDirective &D) { 1108 if (!HaveInsertPoint()) 1109 return; 1110 llvm::SmallVector<const Expr *, 8> Privates; 1111 llvm::SmallVector<const Expr *, 8> LHSExprs; 1112 llvm::SmallVector<const Expr *, 8> RHSExprs; 1113 llvm::SmallVector<const Expr *, 8> ReductionOps; 1114 bool HasAtLeastOneReduction = false; 1115 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1116 HasAtLeastOneReduction = true; 1117 Privates.append(C->privates().begin(), C->privates().end()); 1118 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 1119 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 1120 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); 1121 } 1122 if (HasAtLeastOneReduction) { 1123 // Emit nowait reduction if nowait clause is present or directive is a 1124 // parallel directive (it always has implicit barrier). 1125 CGM.getOpenMPRuntime().emitReduction( 1126 *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, 1127 D.getSingleClause<OMPNowaitClause>() || 1128 isOpenMPParallelDirective(D.getDirectiveKind()) || 1129 D.getDirectiveKind() == OMPD_simd, 1130 D.getDirectiveKind() == OMPD_simd); 1131 } 1132 } 1133 1134 static void emitPostUpdateForReductionClause( 1135 CodeGenFunction &CGF, const OMPExecutableDirective &D, 1136 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1137 if (!CGF.HaveInsertPoint()) 1138 return; 1139 llvm::BasicBlock *DoneBB = nullptr; 1140 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1141 if (auto *PostUpdate = C->getPostUpdateExpr()) { 1142 if (!DoneBB) { 1143 if (auto *Cond = CondGen(CGF)) { 1144 // If the first post-update expression is found, emit conditional 1145 // block if it was requested. 1146 auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); 1147 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); 1148 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1149 CGF.EmitBlock(ThenBB); 1150 } 1151 } 1152 CGF.EmitIgnoredExpr(PostUpdate); 1153 } 1154 } 1155 if (DoneBB) 1156 CGF.EmitBlock(DoneBB, /*IsFinished=*/true); 1157 } 1158 1159 static void emitCommonOMPParallelDirective(CodeGenFunction &CGF, 1160 const OMPExecutableDirective &S, 1161 OpenMPDirectiveKind InnermostKind, 1162 const RegionCodeGenTy &CodeGen) { 1163 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 1164 auto OutlinedFn = CGF.CGM.getOpenMPRuntime(). 1165 emitParallelOrTeamsOutlinedFunction(S, 1166 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 1167 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { 1168 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); 1169 auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), 1170 /*IgnoreResultAssign*/ true); 1171 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( 1172 CGF, NumThreads, NumThreadsClause->getLocStart()); 1173 } 1174 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { 1175 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); 1176 CGF.CGM.getOpenMPRuntime().emitProcBindClause( 1177 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); 1178 } 1179 const Expr *IfCond = nullptr; 1180 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 1181 if (C->getNameModifier() == OMPD_unknown || 1182 C->getNameModifier() == OMPD_parallel) { 1183 IfCond = C->getCondition(); 1184 break; 1185 } 1186 } 1187 1188 OMPLexicalScope Scope(CGF, S); 1189 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 1190 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 1191 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, 1192 CapturedVars, IfCond); 1193 } 1194 1195 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { 1196 // Emit parallel region as a standalone region. 1197 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 1198 OMPPrivateScope PrivateScope(CGF); 1199 bool Copyins = CGF.EmitOMPCopyinClause(S); 1200 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 1201 if (Copyins) { 1202 // Emit implicit barrier to synchronize threads and avoid data races on 1203 // propagation master's thread values of threadprivate variables to local 1204 // instances of that variables of all other implicit threads. 1205 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 1206 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 1207 /*ForceSimpleCall=*/true); 1208 } 1209 CGF.EmitOMPPrivateClause(S, PrivateScope); 1210 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 1211 (void)PrivateScope.Privatize(); 1212 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 1213 CGF.EmitOMPReductionClauseFinal(S); 1214 }; 1215 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen); 1216 emitPostUpdateForReductionClause( 1217 *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1218 } 1219 1220 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, 1221 JumpDest LoopExit) { 1222 RunCleanupsScope BodyScope(*this); 1223 // Update counters values on current iteration. 1224 for (auto I : D.updates()) { 1225 EmitIgnoredExpr(I); 1226 } 1227 // Update the linear variables. 1228 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1229 for (auto *U : C->updates()) 1230 EmitIgnoredExpr(U); 1231 } 1232 1233 // On a continue in the body, jump to the end. 1234 auto Continue = getJumpDestInCurrentScope("omp.body.continue"); 1235 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1236 // Emit loop body. 1237 EmitStmt(D.getBody()); 1238 // The end (updates/cleanups). 1239 EmitBlock(Continue.getBlock()); 1240 BreakContinueStack.pop_back(); 1241 } 1242 1243 void CodeGenFunction::EmitOMPInnerLoop( 1244 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, 1245 const Expr *IncExpr, 1246 const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, 1247 const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { 1248 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); 1249 1250 // Start the loop with a block that tests the condition. 1251 auto CondBlock = createBasicBlock("omp.inner.for.cond"); 1252 EmitBlock(CondBlock); 1253 LoopStack.push(CondBlock); 1254 1255 // If there are any cleanups between here and the loop-exit scope, 1256 // create a block to stage a loop exit along. 1257 auto ExitBlock = LoopExit.getBlock(); 1258 if (RequiresCleanup) 1259 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); 1260 1261 auto LoopBody = createBasicBlock("omp.inner.for.body"); 1262 1263 // Emit condition. 1264 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); 1265 if (ExitBlock != LoopExit.getBlock()) { 1266 EmitBlock(ExitBlock); 1267 EmitBranchThroughCleanup(LoopExit); 1268 } 1269 1270 EmitBlock(LoopBody); 1271 incrementProfileCounter(&S); 1272 1273 // Create a block for the increment. 1274 auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); 1275 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1276 1277 BodyGen(*this); 1278 1279 // Emit "IV = IV + 1" and a back-edge to the condition block. 1280 EmitBlock(Continue.getBlock()); 1281 EmitIgnoredExpr(IncExpr); 1282 PostIncGen(*this); 1283 BreakContinueStack.pop_back(); 1284 EmitBranch(CondBlock); 1285 LoopStack.pop(); 1286 // Emit the fall-through block. 1287 EmitBlock(LoopExit.getBlock()); 1288 } 1289 1290 void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { 1291 if (!HaveInsertPoint()) 1292 return; 1293 // Emit inits for the linear variables. 1294 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1295 for (auto *Init : C->inits()) { 1296 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); 1297 if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { 1298 AutoVarEmission Emission = EmitAutoVarAlloca(*VD); 1299 auto *OrigVD = cast<VarDecl>(Ref->getDecl()); 1300 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1301 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1302 VD->getInit()->getType(), VK_LValue, 1303 VD->getInit()->getExprLoc()); 1304 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), 1305 VD->getType()), 1306 /*capturedByInit=*/false); 1307 EmitAutoVarCleanups(Emission); 1308 } else 1309 EmitVarDecl(*VD); 1310 } 1311 // Emit the linear steps for the linear clauses. 1312 // If a step is not constant, it is pre-calculated before the loop. 1313 if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) 1314 if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { 1315 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); 1316 // Emit calculation of the linear step. 1317 EmitIgnoredExpr(CS); 1318 } 1319 } 1320 } 1321 1322 void CodeGenFunction::EmitOMPLinearClauseFinal( 1323 const OMPLoopDirective &D, 1324 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1325 if (!HaveInsertPoint()) 1326 return; 1327 llvm::BasicBlock *DoneBB = nullptr; 1328 // Emit the final values of the linear variables. 1329 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1330 auto IC = C->varlist_begin(); 1331 for (auto *F : C->finals()) { 1332 if (!DoneBB) { 1333 if (auto *Cond = CondGen(*this)) { 1334 // If the first post-update expression is found, emit conditional 1335 // block if it was requested. 1336 auto *ThenBB = createBasicBlock(".omp.linear.pu"); 1337 DoneBB = createBasicBlock(".omp.linear.pu.done"); 1338 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1339 EmitBlock(ThenBB); 1340 } 1341 } 1342 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); 1343 DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), 1344 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1345 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); 1346 Address OrigAddr = EmitLValue(&DRE).getAddress(); 1347 CodeGenFunction::OMPPrivateScope VarScope(*this); 1348 VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; }); 1349 (void)VarScope.Privatize(); 1350 EmitIgnoredExpr(F); 1351 ++IC; 1352 } 1353 if (auto *PostUpdate = C->getPostUpdateExpr()) 1354 EmitIgnoredExpr(PostUpdate); 1355 } 1356 if (DoneBB) 1357 EmitBlock(DoneBB, /*IsFinished=*/true); 1358 } 1359 1360 static void emitAlignedClause(CodeGenFunction &CGF, 1361 const OMPExecutableDirective &D) { 1362 if (!CGF.HaveInsertPoint()) 1363 return; 1364 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { 1365 unsigned ClauseAlignment = 0; 1366 if (auto AlignmentExpr = Clause->getAlignment()) { 1367 auto AlignmentCI = 1368 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); 1369 ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); 1370 } 1371 for (auto E : Clause->varlists()) { 1372 unsigned Alignment = ClauseAlignment; 1373 if (Alignment == 0) { 1374 // OpenMP [2.8.1, Description] 1375 // If no optional parameter is specified, implementation-defined default 1376 // alignments for SIMD instructions on the target platforms are assumed. 1377 Alignment = 1378 CGF.getContext() 1379 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( 1380 E->getType()->getPointeeType())) 1381 .getQuantity(); 1382 } 1383 assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && 1384 "alignment is not power of 2"); 1385 if (Alignment != 0) { 1386 llvm::Value *PtrValue = CGF.EmitScalarExpr(E); 1387 CGF.EmitAlignmentAssumption(PtrValue, Alignment); 1388 } 1389 } 1390 } 1391 } 1392 1393 void CodeGenFunction::EmitOMPPrivateLoopCounters( 1394 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { 1395 if (!HaveInsertPoint()) 1396 return; 1397 auto I = S.private_counters().begin(); 1398 for (auto *E : S.counters()) { 1399 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1400 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1401 (void)LoopScope.addPrivate(VD, [&]() -> Address { 1402 // Emit var without initialization. 1403 if (!LocalDeclMap.count(PrivateVD)) { 1404 auto VarEmission = EmitAutoVarAlloca(*PrivateVD); 1405 EmitAutoVarCleanups(VarEmission); 1406 } 1407 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), 1408 /*RefersToEnclosingVariableOrCapture=*/false, 1409 (*I)->getType(), VK_LValue, (*I)->getExprLoc()); 1410 return EmitLValue(&DRE).getAddress(); 1411 }); 1412 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || 1413 VD->hasGlobalStorage()) { 1414 (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address { 1415 DeclRefExpr DRE(const_cast<VarDecl *>(VD), 1416 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), 1417 E->getType(), VK_LValue, E->getExprLoc()); 1418 return EmitLValue(&DRE).getAddress(); 1419 }); 1420 } 1421 ++I; 1422 } 1423 } 1424 1425 static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, 1426 const Expr *Cond, llvm::BasicBlock *TrueBlock, 1427 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { 1428 if (!CGF.HaveInsertPoint()) 1429 return; 1430 { 1431 CodeGenFunction::OMPPrivateScope PreCondScope(CGF); 1432 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); 1433 (void)PreCondScope.Privatize(); 1434 // Get initial values of real counters. 1435 for (auto I : S.inits()) { 1436 CGF.EmitIgnoredExpr(I); 1437 } 1438 } 1439 // Check that loop is executed at least one time. 1440 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); 1441 } 1442 1443 void CodeGenFunction::EmitOMPLinearClause( 1444 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { 1445 if (!HaveInsertPoint()) 1446 return; 1447 llvm::DenseSet<const VarDecl *> SIMDLCVs; 1448 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 1449 auto *LoopDirective = cast<OMPLoopDirective>(&D); 1450 for (auto *C : LoopDirective->counters()) { 1451 SIMDLCVs.insert( 1452 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); 1453 } 1454 } 1455 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1456 auto CurPrivate = C->privates().begin(); 1457 for (auto *E : C->varlists()) { 1458 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1459 auto *PrivateVD = 1460 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); 1461 if (!SIMDLCVs.count(VD->getCanonicalDecl())) { 1462 bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { 1463 // Emit private VarDecl with copy init. 1464 EmitVarDecl(*PrivateVD); 1465 return GetAddrOfLocalVar(PrivateVD); 1466 }); 1467 assert(IsRegistered && "linear var already registered as private"); 1468 // Silence the warning about unused variable. 1469 (void)IsRegistered; 1470 } else 1471 EmitVarDecl(*PrivateVD); 1472 ++CurPrivate; 1473 } 1474 } 1475 } 1476 1477 static void emitSimdlenSafelenClause(CodeGenFunction &CGF, 1478 const OMPExecutableDirective &D, 1479 bool IsMonotonic) { 1480 if (!CGF.HaveInsertPoint()) 1481 return; 1482 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { 1483 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), 1484 /*ignoreResult=*/true); 1485 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 1486 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 1487 // In presence of finite 'safelen', it may be unsafe to mark all 1488 // the memory instructions parallel, because loop-carried 1489 // dependences of 'safelen' iterations are possible. 1490 if (!IsMonotonic) 1491 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); 1492 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { 1493 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), 1494 /*ignoreResult=*/true); 1495 llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 1496 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 1497 // In presence of finite 'safelen', it may be unsafe to mark all 1498 // the memory instructions parallel, because loop-carried 1499 // dependences of 'safelen' iterations are possible. 1500 CGF.LoopStack.setParallel(false); 1501 } 1502 } 1503 1504 void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, 1505 bool IsMonotonic) { 1506 // Walk clauses and process safelen/lastprivate. 1507 LoopStack.setParallel(!IsMonotonic); 1508 LoopStack.setVectorizeEnable(true); 1509 emitSimdlenSafelenClause(*this, D, IsMonotonic); 1510 } 1511 1512 void CodeGenFunction::EmitOMPSimdFinal( 1513 const OMPLoopDirective &D, 1514 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { 1515 if (!HaveInsertPoint()) 1516 return; 1517 llvm::BasicBlock *DoneBB = nullptr; 1518 auto IC = D.counters().begin(); 1519 auto IPC = D.private_counters().begin(); 1520 for (auto F : D.finals()) { 1521 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); 1522 auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); 1523 auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); 1524 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || 1525 OrigVD->hasGlobalStorage() || CED) { 1526 if (!DoneBB) { 1527 if (auto *Cond = CondGen(*this)) { 1528 // If the first post-update expression is found, emit conditional 1529 // block if it was requested. 1530 auto *ThenBB = createBasicBlock(".omp.final.then"); 1531 DoneBB = createBasicBlock(".omp.final.done"); 1532 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1533 EmitBlock(ThenBB); 1534 } 1535 } 1536 Address OrigAddr = Address::invalid(); 1537 if (CED) 1538 OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); 1539 else { 1540 DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), 1541 /*RefersToEnclosingVariableOrCapture=*/false, 1542 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); 1543 OrigAddr = EmitLValue(&DRE).getAddress(); 1544 } 1545 OMPPrivateScope VarScope(*this); 1546 VarScope.addPrivate(OrigVD, 1547 [OrigAddr]() -> Address { return OrigAddr; }); 1548 (void)VarScope.Privatize(); 1549 EmitIgnoredExpr(F); 1550 } 1551 ++IC; 1552 ++IPC; 1553 } 1554 if (DoneBB) 1555 EmitBlock(DoneBB, /*IsFinished=*/true); 1556 } 1557 1558 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { 1559 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 1560 OMPLoopScope PreInitScope(CGF, S); 1561 // if (PreCond) { 1562 // for (IV in 0..LastIteration) BODY; 1563 // <Final counter/linear vars updates>; 1564 // } 1565 // 1566 1567 // Emit: if (PreCond) - begin. 1568 // If the condition constant folds and can be elided, avoid emitting the 1569 // whole loop. 1570 bool CondConstant; 1571 llvm::BasicBlock *ContBlock = nullptr; 1572 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 1573 if (!CondConstant) 1574 return; 1575 } else { 1576 auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); 1577 ContBlock = CGF.createBasicBlock("simd.if.end"); 1578 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 1579 CGF.getProfileCount(&S)); 1580 CGF.EmitBlock(ThenBlock); 1581 CGF.incrementProfileCounter(&S); 1582 } 1583 1584 // Emit the loop iteration variable. 1585 const Expr *IVExpr = S.getIterationVariable(); 1586 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 1587 CGF.EmitVarDecl(*IVDecl); 1588 CGF.EmitIgnoredExpr(S.getInit()); 1589 1590 // Emit the iterations count variable. 1591 // If it is not a variable, Sema decided to calculate iterations count on 1592 // each iteration (e.g., it is foldable into a constant). 1593 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 1594 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 1595 // Emit calculation of the iterations count. 1596 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 1597 } 1598 1599 CGF.EmitOMPSimdInit(S); 1600 1601 emitAlignedClause(CGF, S); 1602 CGF.EmitOMPLinearClauseInit(S); 1603 { 1604 OMPPrivateScope LoopScope(CGF); 1605 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 1606 CGF.EmitOMPLinearClause(S, LoopScope); 1607 CGF.EmitOMPPrivateClause(S, LoopScope); 1608 CGF.EmitOMPReductionClauseInit(S, LoopScope); 1609 bool HasLastprivateClause = 1610 CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 1611 (void)LoopScope.Privatize(); 1612 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 1613 S.getInc(), 1614 [&S](CodeGenFunction &CGF) { 1615 CGF.EmitOMPLoopBody(S, JumpDest()); 1616 CGF.EmitStopPoint(&S); 1617 }, 1618 [](CodeGenFunction &) {}); 1619 CGF.EmitOMPSimdFinal( 1620 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1621 // Emit final copy of the lastprivate variables at the end of loops. 1622 if (HasLastprivateClause) 1623 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); 1624 CGF.EmitOMPReductionClauseFinal(S); 1625 emitPostUpdateForReductionClause( 1626 CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1627 } 1628 CGF.EmitOMPLinearClauseFinal( 1629 S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); 1630 // Emit: if (PreCond) - end. 1631 if (ContBlock) { 1632 CGF.EmitBranch(ContBlock); 1633 CGF.EmitBlock(ContBlock, true); 1634 } 1635 }; 1636 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 1637 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 1638 } 1639 1640 void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic, 1641 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, 1642 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1643 auto &RT = CGM.getOpenMPRuntime(); 1644 1645 const Expr *IVExpr = S.getIterationVariable(); 1646 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1647 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1648 1649 auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); 1650 1651 // Start the loop with a block that tests the condition. 1652 auto CondBlock = createBasicBlock("omp.dispatch.cond"); 1653 EmitBlock(CondBlock); 1654 LoopStack.push(CondBlock); 1655 1656 llvm::Value *BoolCondVal = nullptr; 1657 if (!DynamicOrOrdered) { 1658 // UB = min(UB, GlobalUB) 1659 EmitIgnoredExpr(S.getEnsureUpperBound()); 1660 // IV = LB 1661 EmitIgnoredExpr(S.getInit()); 1662 // IV < UB 1663 BoolCondVal = EvaluateExprAsBool(S.getCond()); 1664 } else { 1665 BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, IL, 1666 LB, UB, ST); 1667 } 1668 1669 // If there are any cleanups between here and the loop-exit scope, 1670 // create a block to stage a loop exit along. 1671 auto ExitBlock = LoopExit.getBlock(); 1672 if (LoopScope.requiresCleanups()) 1673 ExitBlock = createBasicBlock("omp.dispatch.cleanup"); 1674 1675 auto LoopBody = createBasicBlock("omp.dispatch.body"); 1676 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); 1677 if (ExitBlock != LoopExit.getBlock()) { 1678 EmitBlock(ExitBlock); 1679 EmitBranchThroughCleanup(LoopExit); 1680 } 1681 EmitBlock(LoopBody); 1682 1683 // Emit "IV = LB" (in case of static schedule, we have already calculated new 1684 // LB for loop condition and emitted it above). 1685 if (DynamicOrOrdered) 1686 EmitIgnoredExpr(S.getInit()); 1687 1688 // Create a block for the increment. 1689 auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); 1690 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1691 1692 // Generate !llvm.loop.parallel metadata for loads and stores for loops 1693 // with dynamic/guided scheduling and without ordered clause. 1694 if (!isOpenMPSimdDirective(S.getDirectiveKind())) 1695 LoopStack.setParallel(!IsMonotonic); 1696 else 1697 EmitOMPSimdInit(S, IsMonotonic); 1698 1699 SourceLocation Loc = S.getLocStart(); 1700 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), 1701 [&S, LoopExit](CodeGenFunction &CGF) { 1702 CGF.EmitOMPLoopBody(S, LoopExit); 1703 CGF.EmitStopPoint(&S); 1704 }, 1705 [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) { 1706 if (Ordered) { 1707 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd( 1708 CGF, Loc, IVSize, IVSigned); 1709 } 1710 }); 1711 1712 EmitBlock(Continue.getBlock()); 1713 BreakContinueStack.pop_back(); 1714 if (!DynamicOrOrdered) { 1715 // Emit "LB = LB + Stride", "UB = UB + Stride". 1716 EmitIgnoredExpr(S.getNextLowerBound()); 1717 EmitIgnoredExpr(S.getNextUpperBound()); 1718 } 1719 1720 EmitBranch(CondBlock); 1721 LoopStack.pop(); 1722 // Emit the fall-through block. 1723 EmitBlock(LoopExit.getBlock()); 1724 1725 // Tell the runtime we are done. 1726 if (!DynamicOrOrdered) 1727 RT.emitForStaticFinish(*this, S.getLocEnd()); 1728 1729 } 1730 1731 void CodeGenFunction::EmitOMPForOuterLoop( 1732 OpenMPScheduleClauseKind ScheduleKind, bool IsMonotonic, 1733 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, 1734 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1735 auto &RT = CGM.getOpenMPRuntime(); 1736 1737 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). 1738 const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind); 1739 1740 assert((Ordered || 1741 !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) && 1742 "static non-chunked schedule does not need outer loop"); 1743 1744 // Emit outer loop. 1745 // 1746 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1747 // When schedule(dynamic,chunk_size) is specified, the iterations are 1748 // distributed to threads in the team in chunks as the threads request them. 1749 // Each thread executes a chunk of iterations, then requests another chunk, 1750 // until no chunks remain to be distributed. Each chunk contains chunk_size 1751 // iterations, except for the last chunk to be distributed, which may have 1752 // fewer iterations. When no chunk_size is specified, it defaults to 1. 1753 // 1754 // When schedule(guided,chunk_size) is specified, the iterations are assigned 1755 // to threads in the team in chunks as the executing threads request them. 1756 // Each thread executes a chunk of iterations, then requests another chunk, 1757 // until no chunks remain to be assigned. For a chunk_size of 1, the size of 1758 // each chunk is proportional to the number of unassigned iterations divided 1759 // by the number of threads in the team, decreasing to 1. For a chunk_size 1760 // with value k (greater than 1), the size of each chunk is determined in the 1761 // same way, with the restriction that the chunks do not contain fewer than k 1762 // iterations (except for the last chunk to be assigned, which may have fewer 1763 // than k iterations). 1764 // 1765 // When schedule(auto) is specified, the decision regarding scheduling is 1766 // delegated to the compiler and/or runtime system. The programmer gives the 1767 // implementation the freedom to choose any possible mapping of iterations to 1768 // threads in the team. 1769 // 1770 // When schedule(runtime) is specified, the decision regarding scheduling is 1771 // deferred until run time, and the schedule and chunk size are taken from the 1772 // run-sched-var ICV. If the ICV is set to auto, the schedule is 1773 // implementation defined 1774 // 1775 // while(__kmpc_dispatch_next(&LB, &UB)) { 1776 // idx = LB; 1777 // while (idx <= UB) { BODY; ++idx; 1778 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. 1779 // } // inner loop 1780 // } 1781 // 1782 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1783 // When schedule(static, chunk_size) is specified, iterations are divided into 1784 // chunks of size chunk_size, and the chunks are assigned to the threads in 1785 // the team in a round-robin fashion in the order of the thread number. 1786 // 1787 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { 1788 // while (idx <= UB) { BODY; ++idx; } // inner loop 1789 // LB = LB + ST; 1790 // UB = UB + ST; 1791 // } 1792 // 1793 1794 const Expr *IVExpr = S.getIterationVariable(); 1795 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1796 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1797 1798 if (DynamicOrOrdered) { 1799 llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration()); 1800 RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, 1801 IVSize, IVSigned, Ordered, UBVal, Chunk); 1802 } else { 1803 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, 1804 Ordered, IL, LB, UB, ST, Chunk); 1805 } 1806 1807 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, Ordered, LB, UB, 1808 ST, IL, Chunk); 1809 } 1810 1811 void CodeGenFunction::EmitOMPDistributeOuterLoop( 1812 OpenMPDistScheduleClauseKind ScheduleKind, 1813 const OMPDistributeDirective &S, OMPPrivateScope &LoopScope, 1814 Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { 1815 1816 auto &RT = CGM.getOpenMPRuntime(); 1817 1818 // Emit outer loop. 1819 // Same behavior as a OMPForOuterLoop, except that schedule cannot be 1820 // dynamic 1821 // 1822 1823 const Expr *IVExpr = S.getIterationVariable(); 1824 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1825 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1826 1827 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, 1828 IVSize, IVSigned, /* Ordered = */ false, 1829 IL, LB, UB, ST, Chunk); 1830 1831 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, 1832 S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk); 1833 } 1834 1835 /// \brief Emit a helper variable and return corresponding lvalue. 1836 static LValue EmitOMPHelperVar(CodeGenFunction &CGF, 1837 const DeclRefExpr *Helper) { 1838 auto VDecl = cast<VarDecl>(Helper->getDecl()); 1839 CGF.EmitVarDecl(*VDecl); 1840 return CGF.EmitLValue(Helper); 1841 } 1842 1843 namespace { 1844 struct ScheduleKindModifiersTy { 1845 OpenMPScheduleClauseKind Kind; 1846 OpenMPScheduleClauseModifier M1; 1847 OpenMPScheduleClauseModifier M2; 1848 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, 1849 OpenMPScheduleClauseModifier M1, 1850 OpenMPScheduleClauseModifier M2) 1851 : Kind(Kind), M1(M1), M2(M2) {} 1852 }; 1853 } // namespace 1854 1855 bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { 1856 // Emit the loop iteration variable. 1857 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 1858 auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); 1859 EmitVarDecl(*IVDecl); 1860 1861 // Emit the iterations count variable. 1862 // If it is not a variable, Sema decided to calculate iterations count on each 1863 // iteration (e.g., it is foldable into a constant). 1864 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 1865 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 1866 // Emit calculation of the iterations count. 1867 EmitIgnoredExpr(S.getCalcLastIteration()); 1868 } 1869 1870 auto &RT = CGM.getOpenMPRuntime(); 1871 1872 bool HasLastprivateClause; 1873 // Check pre-condition. 1874 { 1875 OMPLoopScope PreInitScope(*this, S); 1876 // Skip the entire loop if we don't meet the precondition. 1877 // If the condition constant folds and can be elided, avoid emitting the 1878 // whole loop. 1879 bool CondConstant; 1880 llvm::BasicBlock *ContBlock = nullptr; 1881 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 1882 if (!CondConstant) 1883 return false; 1884 } else { 1885 auto *ThenBlock = createBasicBlock("omp.precond.then"); 1886 ContBlock = createBasicBlock("omp.precond.end"); 1887 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 1888 getProfileCount(&S)); 1889 EmitBlock(ThenBlock); 1890 incrementProfileCounter(&S); 1891 } 1892 1893 llvm::DenseSet<const Expr *> EmittedFinals; 1894 emitAlignedClause(*this, S); 1895 EmitOMPLinearClauseInit(S); 1896 // Emit helper vars inits. 1897 LValue LB = 1898 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); 1899 LValue UB = 1900 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); 1901 LValue ST = 1902 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 1903 LValue IL = 1904 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 1905 1906 // Emit 'then' code. 1907 { 1908 OMPPrivateScope LoopScope(*this); 1909 if (EmitOMPFirstprivateClause(S, LoopScope)) { 1910 // Emit implicit barrier to synchronize threads and avoid data races on 1911 // initialization of firstprivate variables and post-update of 1912 // lastprivate variables. 1913 CGM.getOpenMPRuntime().emitBarrierCall( 1914 *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 1915 /*ForceSimpleCall=*/true); 1916 } 1917 EmitOMPPrivateClause(S, LoopScope); 1918 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); 1919 EmitOMPReductionClauseInit(S, LoopScope); 1920 EmitOMPPrivateLoopCounters(S, LoopScope); 1921 EmitOMPLinearClause(S, LoopScope); 1922 (void)LoopScope.Privatize(); 1923 1924 // Detect the loop schedule kind and chunk. 1925 llvm::Value *Chunk = nullptr; 1926 OpenMPScheduleClauseKind ScheduleKind = OMPC_SCHEDULE_unknown; 1927 OpenMPScheduleClauseModifier M1 = OMPC_SCHEDULE_MODIFIER_unknown; 1928 OpenMPScheduleClauseModifier M2 = OMPC_SCHEDULE_MODIFIER_unknown; 1929 if (auto *C = S.getSingleClause<OMPScheduleClause>()) { 1930 ScheduleKind = C->getScheduleKind(); 1931 M1 = C->getFirstScheduleModifier(); 1932 M2 = C->getSecondScheduleModifier(); 1933 if (const auto *Ch = C->getChunkSize()) { 1934 Chunk = EmitScalarExpr(Ch); 1935 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 1936 S.getIterationVariable()->getType(), 1937 S.getLocStart()); 1938 } 1939 } 1940 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 1941 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 1942 const bool Ordered = S.getSingleClause<OMPOrderedClause>() != nullptr; 1943 // OpenMP 4.5, 2.7.1 Loop Construct, Description. 1944 // If the static schedule kind is specified or if the ordered clause is 1945 // specified, and if no monotonic modifier is specified, the effect will 1946 // be as if the monotonic modifier was specified. 1947 if (RT.isStaticNonchunked(ScheduleKind, 1948 /* Chunked */ Chunk != nullptr) && 1949 !Ordered) { 1950 if (isOpenMPSimdDirective(S.getDirectiveKind())) 1951 EmitOMPSimdInit(S, /*IsMonotonic=*/true); 1952 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 1953 // When no chunk_size is specified, the iteration space is divided into 1954 // chunks that are approximately equal in size, and at most one chunk is 1955 // distributed to each thread. Note that the size of the chunks is 1956 // unspecified in this case. 1957 RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, 1958 IVSize, IVSigned, Ordered, 1959 IL.getAddress(), LB.getAddress(), 1960 UB.getAddress(), ST.getAddress()); 1961 auto LoopExit = 1962 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 1963 // UB = min(UB, GlobalUB); 1964 EmitIgnoredExpr(S.getEnsureUpperBound()); 1965 // IV = LB; 1966 EmitIgnoredExpr(S.getInit()); 1967 // while (idx <= UB) { BODY; ++idx; } 1968 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 1969 S.getInc(), 1970 [&S, LoopExit](CodeGenFunction &CGF) { 1971 CGF.EmitOMPLoopBody(S, LoopExit); 1972 CGF.EmitStopPoint(&S); 1973 }, 1974 [](CodeGenFunction &) {}); 1975 EmitBlock(LoopExit.getBlock()); 1976 // Tell the runtime we are done. 1977 RT.emitForStaticFinish(*this, S.getLocStart()); 1978 } else { 1979 const bool IsMonotonic = Ordered || 1980 ScheduleKind == OMPC_SCHEDULE_static || 1981 ScheduleKind == OMPC_SCHEDULE_unknown || 1982 M1 == OMPC_SCHEDULE_MODIFIER_monotonic || 1983 M2 == OMPC_SCHEDULE_MODIFIER_monotonic; 1984 // Emit the outer loop, which requests its work chunk [LB..UB] from 1985 // runtime and runs the inner loop to process it. 1986 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, 1987 LB.getAddress(), UB.getAddress(), ST.getAddress(), 1988 IL.getAddress(), Chunk); 1989 } 1990 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 1991 EmitOMPSimdFinal(S, 1992 [&](CodeGenFunction &CGF) -> llvm::Value * { 1993 return CGF.Builder.CreateIsNotNull( 1994 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 1995 }); 1996 } 1997 EmitOMPReductionClauseFinal(S); 1998 // Emit post-update of the reduction variables if IsLastIter != 0. 1999 emitPostUpdateForReductionClause( 2000 *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { 2001 return CGF.Builder.CreateIsNotNull( 2002 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2003 }); 2004 // Emit final copy of the lastprivate variables if IsLastIter != 0. 2005 if (HasLastprivateClause) 2006 EmitOMPLastprivateClauseFinal( 2007 S, isOpenMPSimdDirective(S.getDirectiveKind()), 2008 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); 2009 } 2010 EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { 2011 return CGF.Builder.CreateIsNotNull( 2012 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2013 }); 2014 // We're now done with the loop, so jump to the continuation block. 2015 if (ContBlock) { 2016 EmitBranch(ContBlock); 2017 EmitBlock(ContBlock, true); 2018 } 2019 } 2020 return HasLastprivateClause; 2021 } 2022 2023 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { 2024 bool HasLastprivates = false; 2025 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 2026 PrePostActionTy &) { 2027 HasLastprivates = CGF.EmitOMPWorksharingLoop(S); 2028 }; 2029 { 2030 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2031 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, 2032 S.hasCancel()); 2033 } 2034 2035 // Emit an implicit barrier at the end. 2036 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { 2037 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); 2038 } 2039 } 2040 2041 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { 2042 bool HasLastprivates = false; 2043 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 2044 PrePostActionTy &) { 2045 HasLastprivates = CGF.EmitOMPWorksharingLoop(S); 2046 }; 2047 { 2048 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2049 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 2050 } 2051 2052 // Emit an implicit barrier at the end. 2053 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { 2054 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); 2055 } 2056 } 2057 2058 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, 2059 const Twine &Name, 2060 llvm::Value *Init = nullptr) { 2061 auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); 2062 if (Init) 2063 CGF.EmitScalarInit(Init, LVal); 2064 return LVal; 2065 } 2066 2067 void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { 2068 auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); 2069 auto *CS = dyn_cast<CompoundStmt>(Stmt); 2070 bool HasLastprivates = false; 2071 auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF, 2072 PrePostActionTy &) { 2073 auto &C = CGF.CGM.getContext(); 2074 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 2075 // Emit helper vars inits. 2076 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", 2077 CGF.Builder.getInt32(0)); 2078 auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) 2079 : CGF.Builder.getInt32(0); 2080 LValue UB = 2081 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); 2082 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", 2083 CGF.Builder.getInt32(1)); 2084 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", 2085 CGF.Builder.getInt32(0)); 2086 // Loop counter. 2087 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); 2088 OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 2089 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); 2090 OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); 2091 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); 2092 // Generate condition for loop. 2093 BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, 2094 OK_Ordinary, S.getLocStart(), 2095 /*fpContractable=*/false); 2096 // Increment for loop counter. 2097 UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, 2098 S.getLocStart()); 2099 auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { 2100 // Iterate through all sections and emit a switch construct: 2101 // switch (IV) { 2102 // case 0: 2103 // <SectionStmt[0]>; 2104 // break; 2105 // ... 2106 // case <NumSection> - 1: 2107 // <SectionStmt[<NumSection> - 1]>; 2108 // break; 2109 // } 2110 // .omp.sections.exit: 2111 auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); 2112 auto *SwitchStmt = CGF.Builder.CreateSwitch( 2113 CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, 2114 CS == nullptr ? 1 : CS->size()); 2115 if (CS) { 2116 unsigned CaseNumber = 0; 2117 for (auto *SubStmt : CS->children()) { 2118 auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); 2119 CGF.EmitBlock(CaseBB); 2120 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); 2121 CGF.EmitStmt(SubStmt); 2122 CGF.EmitBranch(ExitBB); 2123 ++CaseNumber; 2124 } 2125 } else { 2126 auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); 2127 CGF.EmitBlock(CaseBB); 2128 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); 2129 CGF.EmitStmt(Stmt); 2130 CGF.EmitBranch(ExitBB); 2131 } 2132 CGF.EmitBlock(ExitBB, /*IsFinished=*/true); 2133 }; 2134 2135 CodeGenFunction::OMPPrivateScope LoopScope(CGF); 2136 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { 2137 // Emit implicit barrier to synchronize threads and avoid data races on 2138 // initialization of firstprivate variables and post-update of lastprivate 2139 // variables. 2140 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 2141 CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, 2142 /*ForceSimpleCall=*/true); 2143 } 2144 CGF.EmitOMPPrivateClause(S, LoopScope); 2145 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 2146 CGF.EmitOMPReductionClauseInit(S, LoopScope); 2147 (void)LoopScope.Privatize(); 2148 2149 // Emit static non-chunked loop. 2150 CGF.CGM.getOpenMPRuntime().emitForStaticInit( 2151 CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32, 2152 /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(), 2153 UB.getAddress(), ST.getAddress()); 2154 // UB = min(UB, GlobalUB); 2155 auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); 2156 auto *MinUBGlobalUB = CGF.Builder.CreateSelect( 2157 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); 2158 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); 2159 // IV = LB; 2160 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); 2161 // while (idx <= UB) { BODY; ++idx; } 2162 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, 2163 [](CodeGenFunction &) {}); 2164 // Tell the runtime we are done. 2165 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart()); 2166 CGF.EmitOMPReductionClauseFinal(S); 2167 // Emit post-update of the reduction variables if IsLastIter != 0. 2168 emitPostUpdateForReductionClause( 2169 CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * { 2170 return CGF.Builder.CreateIsNotNull( 2171 CGF.EmitLoadOfScalar(IL, S.getLocStart())); 2172 }); 2173 2174 // Emit final copy of the lastprivate variables if IsLastIter != 0. 2175 if (HasLastprivates) 2176 CGF.EmitOMPLastprivateClauseFinal( 2177 S, /*NoFinals=*/false, 2178 CGF.Builder.CreateIsNotNull( 2179 CGF.EmitLoadOfScalar(IL, S.getLocStart()))); 2180 }; 2181 2182 bool HasCancel = false; 2183 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) 2184 HasCancel = OSD->hasCancel(); 2185 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) 2186 HasCancel = OPSD->hasCancel(); 2187 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, 2188 HasCancel); 2189 // Emit barrier for lastprivates only if 'sections' directive has 'nowait' 2190 // clause. Otherwise the barrier will be generated by the codegen for the 2191 // directive. 2192 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { 2193 // Emit implicit barrier to synchronize threads and avoid data races on 2194 // initialization of firstprivate variables. 2195 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 2196 OMPD_unknown); 2197 } 2198 } 2199 2200 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { 2201 { 2202 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2203 EmitSections(S); 2204 } 2205 // Emit an implicit barrier at the end. 2206 if (!S.getSingleClause<OMPNowaitClause>()) { 2207 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), 2208 OMPD_sections); 2209 } 2210 } 2211 2212 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { 2213 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2214 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2215 }; 2216 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2217 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, 2218 S.hasCancel()); 2219 } 2220 2221 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { 2222 llvm::SmallVector<const Expr *, 8> CopyprivateVars; 2223 llvm::SmallVector<const Expr *, 8> DestExprs; 2224 llvm::SmallVector<const Expr *, 8> SrcExprs; 2225 llvm::SmallVector<const Expr *, 8> AssignmentOps; 2226 // Check if there are any 'copyprivate' clauses associated with this 2227 // 'single' construct. 2228 // Build a list of copyprivate variables along with helper expressions 2229 // (<source>, <destination>, <destination>=<source> expressions) 2230 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { 2231 CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); 2232 DestExprs.append(C->destination_exprs().begin(), 2233 C->destination_exprs().end()); 2234 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); 2235 AssignmentOps.append(C->assignment_ops().begin(), 2236 C->assignment_ops().end()); 2237 } 2238 // Emit code for 'single' region along with 'copyprivate' clauses 2239 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2240 Action.Enter(CGF); 2241 OMPPrivateScope SingleScope(CGF); 2242 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); 2243 CGF.EmitOMPPrivateClause(S, SingleScope); 2244 (void)SingleScope.Privatize(); 2245 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2246 }; 2247 { 2248 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2249 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), 2250 CopyprivateVars, DestExprs, 2251 SrcExprs, AssignmentOps); 2252 } 2253 // Emit an implicit barrier at the end (to avoid data race on firstprivate 2254 // init or if no 'nowait' clause was specified and no 'copyprivate' clause). 2255 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { 2256 CGM.getOpenMPRuntime().emitBarrierCall( 2257 *this, S.getLocStart(), 2258 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); 2259 } 2260 } 2261 2262 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { 2263 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2264 Action.Enter(CGF); 2265 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2266 }; 2267 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2268 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); 2269 } 2270 2271 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { 2272 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2273 Action.Enter(CGF); 2274 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2275 }; 2276 Expr *Hint = nullptr; 2277 if (auto *HintClause = S.getSingleClause<OMPHintClause>()) 2278 Hint = HintClause->getHint(); 2279 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2280 CGM.getOpenMPRuntime().emitCriticalRegion(*this, 2281 S.getDirectiveName().getAsString(), 2282 CodeGen, S.getLocStart(), Hint); 2283 } 2284 2285 void CodeGenFunction::EmitOMPParallelForDirective( 2286 const OMPParallelForDirective &S) { 2287 // Emit directive as a combined directive that consists of two implicit 2288 // directives: 'parallel' with 'for' directive. 2289 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2290 CGF.EmitOMPWorksharingLoop(S); 2291 }; 2292 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen); 2293 } 2294 2295 void CodeGenFunction::EmitOMPParallelForSimdDirective( 2296 const OMPParallelForSimdDirective &S) { 2297 // Emit directive as a combined directive that consists of two implicit 2298 // directives: 'parallel' with 'for' directive. 2299 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2300 CGF.EmitOMPWorksharingLoop(S); 2301 }; 2302 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen); 2303 } 2304 2305 void CodeGenFunction::EmitOMPParallelSectionsDirective( 2306 const OMPParallelSectionsDirective &S) { 2307 // Emit directive as a combined directive that consists of two implicit 2308 // directives: 'parallel' with 'sections' directive. 2309 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2310 CGF.EmitSections(S); 2311 }; 2312 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen); 2313 } 2314 2315 void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S, 2316 const RegionCodeGenTy &BodyGen, 2317 const TaskGenTy &TaskGen, 2318 OMPTaskDataTy &Data) { 2319 // Emit outlined function for task construct. 2320 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2321 auto *I = CS->getCapturedDecl()->param_begin(); 2322 auto *PartId = std::next(I); 2323 auto *TaskT = std::next(I, 4); 2324 // Check if the task is final 2325 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { 2326 // If the condition constant folds and can be elided, try to avoid emitting 2327 // the condition and the dead arm of the if/else. 2328 auto *Cond = Clause->getCondition(); 2329 bool CondConstant; 2330 if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) 2331 Data.Final.setInt(CondConstant); 2332 else 2333 Data.Final.setPointer(EvaluateExprAsBool(Cond)); 2334 } else { 2335 // By default the task is not final. 2336 Data.Final.setInt(/*IntVal=*/false); 2337 } 2338 // The first function argument for tasks is a thread id, the second one is a 2339 // part id (0 for tied tasks, >=0 for untied task). 2340 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 2341 // Get list of private variables. 2342 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { 2343 auto IRef = C->varlist_begin(); 2344 for (auto *IInit : C->private_copies()) { 2345 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 2346 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 2347 Data.PrivateVars.push_back(*IRef); 2348 Data.PrivateCopies.push_back(IInit); 2349 } 2350 ++IRef; 2351 } 2352 } 2353 EmittedAsPrivate.clear(); 2354 // Get list of firstprivate variables. 2355 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { 2356 auto IRef = C->varlist_begin(); 2357 auto IElemInitRef = C->inits().begin(); 2358 for (auto *IInit : C->private_copies()) { 2359 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 2360 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 2361 Data.FirstprivateVars.push_back(*IRef); 2362 Data.FirstprivateCopies.push_back(IInit); 2363 Data.FirstprivateInits.push_back(*IElemInitRef); 2364 } 2365 ++IRef; 2366 ++IElemInitRef; 2367 } 2368 } 2369 // Get list of lastprivate variables (for taskloops). 2370 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; 2371 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { 2372 auto IRef = C->varlist_begin(); 2373 auto ID = C->destination_exprs().begin(); 2374 for (auto *IInit : C->private_copies()) { 2375 auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 2376 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 2377 Data.LastprivateVars.push_back(*IRef); 2378 Data.LastprivateCopies.push_back(IInit); 2379 } 2380 LastprivateDstsOrigs.insert( 2381 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), 2382 cast<DeclRefExpr>(*IRef)}); 2383 ++IRef; 2384 ++ID; 2385 } 2386 } 2387 // Build list of dependences. 2388 for (const auto *C : S.getClausesOfKind<OMPDependClause>()) 2389 for (auto *IRef : C->varlists()) 2390 Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); 2391 auto &&CodeGen = [PartId, &S, &Data, CS, &BodyGen, &LastprivateDstsOrigs]( 2392 CodeGenFunction &CGF, PrePostActionTy &Action) { 2393 // Set proper addresses for generated private copies. 2394 OMPPrivateScope Scope(CGF); 2395 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || 2396 !Data.LastprivateVars.empty()) { 2397 auto *CopyFn = CGF.Builder.CreateLoad( 2398 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); 2399 auto *PrivatesPtr = CGF.Builder.CreateLoad( 2400 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); 2401 // Map privates. 2402 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; 2403 llvm::SmallVector<llvm::Value *, 16> CallArgs; 2404 CallArgs.push_back(PrivatesPtr); 2405 for (auto *E : Data.PrivateVars) { 2406 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 2407 Address PrivatePtr = CGF.CreateMemTemp( 2408 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); 2409 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); 2410 CallArgs.push_back(PrivatePtr.getPointer()); 2411 } 2412 for (auto *E : Data.FirstprivateVars) { 2413 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 2414 Address PrivatePtr = 2415 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 2416 ".firstpriv.ptr.addr"); 2417 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); 2418 CallArgs.push_back(PrivatePtr.getPointer()); 2419 } 2420 for (auto *E : Data.LastprivateVars) { 2421 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 2422 Address PrivatePtr = 2423 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 2424 ".lastpriv.ptr.addr"); 2425 PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); 2426 CallArgs.push_back(PrivatePtr.getPointer()); 2427 } 2428 CGF.EmitRuntimeCall(CopyFn, CallArgs); 2429 for (auto &&Pair : LastprivateDstsOrigs) { 2430 auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); 2431 DeclRefExpr DRE( 2432 const_cast<VarDecl *>(OrigVD), 2433 /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup( 2434 OrigVD) != nullptr, 2435 Pair.second->getType(), VK_LValue, Pair.second->getExprLoc()); 2436 Scope.addPrivate(Pair.first, [&CGF, &DRE]() { 2437 return CGF.EmitLValue(&DRE).getAddress(); 2438 }); 2439 } 2440 for (auto &&Pair : PrivatePtrs) { 2441 Address Replacement(CGF.Builder.CreateLoad(Pair.second), 2442 CGF.getContext().getDeclAlign(Pair.first)); 2443 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); 2444 } 2445 } 2446 (void)Scope.Privatize(); 2447 2448 Action.Enter(CGF); 2449 BodyGen(CGF); 2450 }; 2451 auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( 2452 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, 2453 Data.NumberOfParts); 2454 OMPLexicalScope Scope(*this, S); 2455 TaskGen(*this, OutlinedFn, Data); 2456 } 2457 2458 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { 2459 // Emit outlined function for task construct. 2460 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2461 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 2462 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 2463 const Expr *IfCond = nullptr; 2464 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 2465 if (C->getNameModifier() == OMPD_unknown || 2466 C->getNameModifier() == OMPD_task) { 2467 IfCond = C->getCondition(); 2468 break; 2469 } 2470 } 2471 2472 OMPTaskDataTy Data; 2473 // Check if we should emit tied or untied task. 2474 Data.Tied = !S.getSingleClause<OMPUntiedClause>(); 2475 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { 2476 CGF.EmitStmt(CS->getCapturedStmt()); 2477 }; 2478 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 2479 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, 2480 const OMPTaskDataTy &Data) { 2481 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn, 2482 SharedsTy, CapturedStruct, IfCond, 2483 Data); 2484 }; 2485 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); 2486 } 2487 2488 void CodeGenFunction::EmitOMPTaskyieldDirective( 2489 const OMPTaskyieldDirective &S) { 2490 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); 2491 } 2492 2493 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { 2494 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); 2495 } 2496 2497 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { 2498 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); 2499 } 2500 2501 void CodeGenFunction::EmitOMPTaskgroupDirective( 2502 const OMPTaskgroupDirective &S) { 2503 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2504 Action.Enter(CGF); 2505 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2506 }; 2507 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2508 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); 2509 } 2510 2511 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { 2512 CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { 2513 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { 2514 return llvm::makeArrayRef(FlushClause->varlist_begin(), 2515 FlushClause->varlist_end()); 2516 } 2517 return llvm::None; 2518 }(), S.getLocStart()); 2519 } 2520 2521 void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) { 2522 // Emit the loop iteration variable. 2523 auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 2524 auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); 2525 EmitVarDecl(*IVDecl); 2526 2527 // Emit the iterations count variable. 2528 // If it is not a variable, Sema decided to calculate iterations count on each 2529 // iteration (e.g., it is foldable into a constant). 2530 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 2531 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 2532 // Emit calculation of the iterations count. 2533 EmitIgnoredExpr(S.getCalcLastIteration()); 2534 } 2535 2536 auto &RT = CGM.getOpenMPRuntime(); 2537 2538 // Check pre-condition. 2539 { 2540 OMPLoopScope PreInitScope(*this, S); 2541 // Skip the entire loop if we don't meet the precondition. 2542 // If the condition constant folds and can be elided, avoid emitting the 2543 // whole loop. 2544 bool CondConstant; 2545 llvm::BasicBlock *ContBlock = nullptr; 2546 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 2547 if (!CondConstant) 2548 return; 2549 } else { 2550 auto *ThenBlock = createBasicBlock("omp.precond.then"); 2551 ContBlock = createBasicBlock("omp.precond.end"); 2552 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 2553 getProfileCount(&S)); 2554 EmitBlock(ThenBlock); 2555 incrementProfileCounter(&S); 2556 } 2557 2558 // Emit 'then' code. 2559 { 2560 // Emit helper vars inits. 2561 LValue LB = 2562 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); 2563 LValue UB = 2564 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); 2565 LValue ST = 2566 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 2567 LValue IL = 2568 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 2569 2570 OMPPrivateScope LoopScope(*this); 2571 EmitOMPPrivateLoopCounters(S, LoopScope); 2572 (void)LoopScope.Privatize(); 2573 2574 // Detect the distribute schedule kind and chunk. 2575 llvm::Value *Chunk = nullptr; 2576 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; 2577 if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) { 2578 ScheduleKind = C->getDistScheduleKind(); 2579 if (const auto *Ch = C->getChunkSize()) { 2580 Chunk = EmitScalarExpr(Ch); 2581 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 2582 S.getIterationVariable()->getType(), 2583 S.getLocStart()); 2584 } 2585 } 2586 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2587 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2588 2589 // OpenMP [2.10.8, distribute Construct, Description] 2590 // If dist_schedule is specified, kind must be static. If specified, 2591 // iterations are divided into chunks of size chunk_size, chunks are 2592 // assigned to the teams of the league in a round-robin fashion in the 2593 // order of the team number. When no chunk_size is specified, the 2594 // iteration space is divided into chunks that are approximately equal 2595 // in size, and at most one chunk is distributed to each team of the 2596 // league. The size of the chunks is unspecified in this case. 2597 if (RT.isStaticNonchunked(ScheduleKind, 2598 /* Chunked */ Chunk != nullptr)) { 2599 RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, 2600 IVSize, IVSigned, /* Ordered = */ false, 2601 IL.getAddress(), LB.getAddress(), 2602 UB.getAddress(), ST.getAddress()); 2603 auto LoopExit = 2604 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 2605 // UB = min(UB, GlobalUB); 2606 EmitIgnoredExpr(S.getEnsureUpperBound()); 2607 // IV = LB; 2608 EmitIgnoredExpr(S.getInit()); 2609 // while (idx <= UB) { BODY; ++idx; } 2610 EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 2611 S.getInc(), 2612 [&S, LoopExit](CodeGenFunction &CGF) { 2613 CGF.EmitOMPLoopBody(S, LoopExit); 2614 CGF.EmitStopPoint(&S); 2615 }, 2616 [](CodeGenFunction &) {}); 2617 EmitBlock(LoopExit.getBlock()); 2618 // Tell the runtime we are done. 2619 RT.emitForStaticFinish(*this, S.getLocStart()); 2620 } else { 2621 // Emit the outer loop, which requests its work chunk [LB..UB] from 2622 // runtime and runs the inner loop to process it. 2623 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, 2624 LB.getAddress(), UB.getAddress(), ST.getAddress(), 2625 IL.getAddress(), Chunk); 2626 } 2627 } 2628 2629 // We're now done with the loop, so jump to the continuation block. 2630 if (ContBlock) { 2631 EmitBranch(ContBlock); 2632 EmitBlock(ContBlock, true); 2633 } 2634 } 2635 } 2636 2637 void CodeGenFunction::EmitOMPDistributeDirective( 2638 const OMPDistributeDirective &S) { 2639 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2640 CGF.EmitOMPDistributeLoop(S); 2641 }; 2642 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2643 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen, 2644 false); 2645 } 2646 2647 static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, 2648 const CapturedStmt *S) { 2649 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); 2650 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; 2651 CGF.CapturedStmtInfo = &CapStmtInfo; 2652 auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); 2653 Fn->addFnAttr(llvm::Attribute::NoInline); 2654 return Fn; 2655 } 2656 2657 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { 2658 if (!S.getAssociatedStmt()) 2659 return; 2660 auto *C = S.getSingleClause<OMPSIMDClause>(); 2661 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, 2662 PrePostActionTy &Action) { 2663 if (C) { 2664 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 2665 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 2666 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 2667 auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); 2668 CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars); 2669 } else { 2670 Action.Enter(CGF); 2671 CGF.EmitStmt( 2672 cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 2673 } 2674 }; 2675 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 2676 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); 2677 } 2678 2679 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, 2680 QualType SrcType, QualType DestType, 2681 SourceLocation Loc) { 2682 assert(CGF.hasScalarEvaluationKind(DestType) && 2683 "DestType must have scalar evaluation kind."); 2684 assert(!Val.isAggregate() && "Must be a scalar or complex."); 2685 return Val.isScalar() 2686 ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, 2687 Loc) 2688 : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, 2689 DestType, Loc); 2690 } 2691 2692 static CodeGenFunction::ComplexPairTy 2693 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, 2694 QualType DestType, SourceLocation Loc) { 2695 assert(CGF.getEvaluationKind(DestType) == TEK_Complex && 2696 "DestType must have complex evaluation kind."); 2697 CodeGenFunction::ComplexPairTy ComplexVal; 2698 if (Val.isScalar()) { 2699 // Convert the input element to the element type of the complex. 2700 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 2701 auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, 2702 DestElementType, Loc); 2703 ComplexVal = CodeGenFunction::ComplexPairTy( 2704 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); 2705 } else { 2706 assert(Val.isComplex() && "Must be a scalar or complex."); 2707 auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); 2708 auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); 2709 ComplexVal.first = CGF.EmitScalarConversion( 2710 Val.getComplexVal().first, SrcElementType, DestElementType, Loc); 2711 ComplexVal.second = CGF.EmitScalarConversion( 2712 Val.getComplexVal().second, SrcElementType, DestElementType, Loc); 2713 } 2714 return ComplexVal; 2715 } 2716 2717 static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, 2718 LValue LVal, RValue RVal) { 2719 if (LVal.isGlobalReg()) { 2720 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); 2721 } else { 2722 CGF.EmitAtomicStore(RVal, LVal, 2723 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2724 : llvm::AtomicOrdering::Monotonic, 2725 LVal.isVolatile(), /*IsInit=*/false); 2726 } 2727 } 2728 2729 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, 2730 QualType RValTy, SourceLocation Loc) { 2731 switch (getEvaluationKind(LVal.getType())) { 2732 case TEK_Scalar: 2733 EmitStoreThroughLValue(RValue::get(convertToScalarValue( 2734 *this, RVal, RValTy, LVal.getType(), Loc)), 2735 LVal); 2736 break; 2737 case TEK_Complex: 2738 EmitStoreOfComplex( 2739 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, 2740 /*isInit=*/false); 2741 break; 2742 case TEK_Aggregate: 2743 llvm_unreachable("Must be a scalar or complex."); 2744 } 2745 } 2746 2747 static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, 2748 const Expr *X, const Expr *V, 2749 SourceLocation Loc) { 2750 // v = x; 2751 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); 2752 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); 2753 LValue XLValue = CGF.EmitLValue(X); 2754 LValue VLValue = CGF.EmitLValue(V); 2755 RValue Res = XLValue.isGlobalReg() 2756 ? CGF.EmitLoadOfLValue(XLValue, Loc) 2757 : CGF.EmitAtomicLoad( 2758 XLValue, Loc, 2759 IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2760 : llvm::AtomicOrdering::Monotonic, 2761 XLValue.isVolatile()); 2762 // OpenMP, 2.12.6, atomic Construct 2763 // Any atomic construct with a seq_cst clause forces the atomically 2764 // performed operation to include an implicit flush operation without a 2765 // list. 2766 if (IsSeqCst) 2767 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2768 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); 2769 } 2770 2771 static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, 2772 const Expr *X, const Expr *E, 2773 SourceLocation Loc) { 2774 // x = expr; 2775 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); 2776 emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); 2777 // OpenMP, 2.12.6, atomic Construct 2778 // Any atomic construct with a seq_cst clause forces the atomically 2779 // performed operation to include an implicit flush operation without a 2780 // list. 2781 if (IsSeqCst) 2782 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2783 } 2784 2785 static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, 2786 RValue Update, 2787 BinaryOperatorKind BO, 2788 llvm::AtomicOrdering AO, 2789 bool IsXLHSInRHSPart) { 2790 auto &Context = CGF.CGM.getContext(); 2791 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x' 2792 // expression is simple and atomic is allowed for the given type for the 2793 // target platform. 2794 if (BO == BO_Comma || !Update.isScalar() || 2795 !Update.getScalarVal()->getType()->isIntegerTy() || 2796 !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && 2797 (Update.getScalarVal()->getType() != 2798 X.getAddress().getElementType())) || 2799 !X.getAddress().getElementType()->isIntegerTy() || 2800 !Context.getTargetInfo().hasBuiltinAtomic( 2801 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) 2802 return std::make_pair(false, RValue::get(nullptr)); 2803 2804 llvm::AtomicRMWInst::BinOp RMWOp; 2805 switch (BO) { 2806 case BO_Add: 2807 RMWOp = llvm::AtomicRMWInst::Add; 2808 break; 2809 case BO_Sub: 2810 if (!IsXLHSInRHSPart) 2811 return std::make_pair(false, RValue::get(nullptr)); 2812 RMWOp = llvm::AtomicRMWInst::Sub; 2813 break; 2814 case BO_And: 2815 RMWOp = llvm::AtomicRMWInst::And; 2816 break; 2817 case BO_Or: 2818 RMWOp = llvm::AtomicRMWInst::Or; 2819 break; 2820 case BO_Xor: 2821 RMWOp = llvm::AtomicRMWInst::Xor; 2822 break; 2823 case BO_LT: 2824 RMWOp = X.getType()->hasSignedIntegerRepresentation() 2825 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min 2826 : llvm::AtomicRMWInst::Max) 2827 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin 2828 : llvm::AtomicRMWInst::UMax); 2829 break; 2830 case BO_GT: 2831 RMWOp = X.getType()->hasSignedIntegerRepresentation() 2832 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max 2833 : llvm::AtomicRMWInst::Min) 2834 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax 2835 : llvm::AtomicRMWInst::UMin); 2836 break; 2837 case BO_Assign: 2838 RMWOp = llvm::AtomicRMWInst::Xchg; 2839 break; 2840 case BO_Mul: 2841 case BO_Div: 2842 case BO_Rem: 2843 case BO_Shl: 2844 case BO_Shr: 2845 case BO_LAnd: 2846 case BO_LOr: 2847 return std::make_pair(false, RValue::get(nullptr)); 2848 case BO_PtrMemD: 2849 case BO_PtrMemI: 2850 case BO_LE: 2851 case BO_GE: 2852 case BO_EQ: 2853 case BO_NE: 2854 case BO_AddAssign: 2855 case BO_SubAssign: 2856 case BO_AndAssign: 2857 case BO_OrAssign: 2858 case BO_XorAssign: 2859 case BO_MulAssign: 2860 case BO_DivAssign: 2861 case BO_RemAssign: 2862 case BO_ShlAssign: 2863 case BO_ShrAssign: 2864 case BO_Comma: 2865 llvm_unreachable("Unsupported atomic update operation"); 2866 } 2867 auto *UpdateVal = Update.getScalarVal(); 2868 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { 2869 UpdateVal = CGF.Builder.CreateIntCast( 2870 IC, X.getAddress().getElementType(), 2871 X.getType()->hasSignedIntegerRepresentation()); 2872 } 2873 auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); 2874 return std::make_pair(true, RValue::get(Res)); 2875 } 2876 2877 std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( 2878 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, 2879 llvm::AtomicOrdering AO, SourceLocation Loc, 2880 const llvm::function_ref<RValue(RValue)> &CommonGen) { 2881 // Update expressions are allowed to have the following forms: 2882 // x binop= expr; -> xrval + expr; 2883 // x++, ++x -> xrval + 1; 2884 // x--, --x -> xrval - 1; 2885 // x = x binop expr; -> xrval binop expr 2886 // x = expr Op x; - > expr binop xrval; 2887 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); 2888 if (!Res.first) { 2889 if (X.isGlobalReg()) { 2890 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop 2891 // 'xrval'. 2892 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); 2893 } else { 2894 // Perform compare-and-swap procedure. 2895 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); 2896 } 2897 } 2898 return Res; 2899 } 2900 2901 static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, 2902 const Expr *X, const Expr *E, 2903 const Expr *UE, bool IsXLHSInRHSPart, 2904 SourceLocation Loc) { 2905 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 2906 "Update expr in 'atomic update' must be a binary operator."); 2907 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 2908 // Update expressions are allowed to have the following forms: 2909 // x binop= expr; -> xrval + expr; 2910 // x++, ++x -> xrval + 1; 2911 // x--, --x -> xrval - 1; 2912 // x = x binop expr; -> xrval binop expr 2913 // x = expr Op x; - > expr binop xrval; 2914 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); 2915 LValue XLValue = CGF.EmitLValue(X); 2916 RValue ExprRValue = CGF.EmitAnyExpr(E); 2917 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2918 : llvm::AtomicOrdering::Monotonic; 2919 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 2920 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 2921 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 2922 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 2923 auto Gen = 2924 [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { 2925 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 2926 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 2927 return CGF.EmitAnyExpr(UE); 2928 }; 2929 (void)CGF.EmitOMPAtomicSimpleUpdateExpr( 2930 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 2931 // OpenMP, 2.12.6, atomic Construct 2932 // Any atomic construct with a seq_cst clause forces the atomically 2933 // performed operation to include an implicit flush operation without a 2934 // list. 2935 if (IsSeqCst) 2936 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 2937 } 2938 2939 static RValue convertToType(CodeGenFunction &CGF, RValue Value, 2940 QualType SourceType, QualType ResType, 2941 SourceLocation Loc) { 2942 switch (CGF.getEvaluationKind(ResType)) { 2943 case TEK_Scalar: 2944 return RValue::get( 2945 convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); 2946 case TEK_Complex: { 2947 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); 2948 return RValue::getComplex(Res.first, Res.second); 2949 } 2950 case TEK_Aggregate: 2951 break; 2952 } 2953 llvm_unreachable("Must be a scalar or complex."); 2954 } 2955 2956 static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, 2957 bool IsPostfixUpdate, const Expr *V, 2958 const Expr *X, const Expr *E, 2959 const Expr *UE, bool IsXLHSInRHSPart, 2960 SourceLocation Loc) { 2961 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); 2962 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); 2963 RValue NewVVal; 2964 LValue VLValue = CGF.EmitLValue(V); 2965 LValue XLValue = CGF.EmitLValue(X); 2966 RValue ExprRValue = CGF.EmitAnyExpr(E); 2967 auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent 2968 : llvm::AtomicOrdering::Monotonic; 2969 QualType NewVValType; 2970 if (UE) { 2971 // 'x' is updated with some additional value. 2972 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 2973 "Update expr in 'atomic capture' must be a binary operator."); 2974 auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 2975 // Update expressions are allowed to have the following forms: 2976 // x binop= expr; -> xrval + expr; 2977 // x++, ++x -> xrval + 1; 2978 // x--, --x -> xrval - 1; 2979 // x = x binop expr; -> xrval binop expr 2980 // x = expr Op x; - > expr binop xrval; 2981 auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 2982 auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 2983 auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 2984 NewVValType = XRValExpr->getType(); 2985 auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 2986 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, 2987 IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue { 2988 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 2989 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 2990 RValue Res = CGF.EmitAnyExpr(UE); 2991 NewVVal = IsPostfixUpdate ? XRValue : Res; 2992 return Res; 2993 }; 2994 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 2995 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 2996 if (Res.first) { 2997 // 'atomicrmw' instruction was generated. 2998 if (IsPostfixUpdate) { 2999 // Use old value from 'atomicrmw'. 3000 NewVVal = Res.second; 3001 } else { 3002 // 'atomicrmw' does not provide new value, so evaluate it using old 3003 // value of 'x'. 3004 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 3005 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); 3006 NewVVal = CGF.EmitAnyExpr(UE); 3007 } 3008 } 3009 } else { 3010 // 'x' is simply rewritten with some 'expr'. 3011 NewVValType = X->getType().getNonReferenceType(); 3012 ExprRValue = convertToType(CGF, ExprRValue, E->getType(), 3013 X->getType().getNonReferenceType(), Loc); 3014 auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue { 3015 NewVVal = XRValue; 3016 return ExprRValue; 3017 }; 3018 // Try to perform atomicrmw xchg, otherwise simple exchange. 3019 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 3020 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, 3021 Loc, Gen); 3022 if (Res.first) { 3023 // 'atomicrmw' instruction was generated. 3024 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; 3025 } 3026 } 3027 // Emit post-update store to 'v' of old/new 'x' value. 3028 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); 3029 // OpenMP, 2.12.6, atomic Construct 3030 // Any atomic construct with a seq_cst clause forces the atomically 3031 // performed operation to include an implicit flush operation without a 3032 // list. 3033 if (IsSeqCst) 3034 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); 3035 } 3036 3037 static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, 3038 bool IsSeqCst, bool IsPostfixUpdate, 3039 const Expr *X, const Expr *V, const Expr *E, 3040 const Expr *UE, bool IsXLHSInRHSPart, 3041 SourceLocation Loc) { 3042 switch (Kind) { 3043 case OMPC_read: 3044 EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); 3045 break; 3046 case OMPC_write: 3047 EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); 3048 break; 3049 case OMPC_unknown: 3050 case OMPC_update: 3051 EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); 3052 break; 3053 case OMPC_capture: 3054 EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, 3055 IsXLHSInRHSPart, Loc); 3056 break; 3057 case OMPC_if: 3058 case OMPC_final: 3059 case OMPC_num_threads: 3060 case OMPC_private: 3061 case OMPC_firstprivate: 3062 case OMPC_lastprivate: 3063 case OMPC_reduction: 3064 case OMPC_safelen: 3065 case OMPC_simdlen: 3066 case OMPC_collapse: 3067 case OMPC_default: 3068 case OMPC_seq_cst: 3069 case OMPC_shared: 3070 case OMPC_linear: 3071 case OMPC_aligned: 3072 case OMPC_copyin: 3073 case OMPC_copyprivate: 3074 case OMPC_flush: 3075 case OMPC_proc_bind: 3076 case OMPC_schedule: 3077 case OMPC_ordered: 3078 case OMPC_nowait: 3079 case OMPC_untied: 3080 case OMPC_threadprivate: 3081 case OMPC_depend: 3082 case OMPC_mergeable: 3083 case OMPC_device: 3084 case OMPC_threads: 3085 case OMPC_simd: 3086 case OMPC_map: 3087 case OMPC_num_teams: 3088 case OMPC_thread_limit: 3089 case OMPC_priority: 3090 case OMPC_grainsize: 3091 case OMPC_nogroup: 3092 case OMPC_num_tasks: 3093 case OMPC_hint: 3094 case OMPC_dist_schedule: 3095 case OMPC_defaultmap: 3096 case OMPC_uniform: 3097 llvm_unreachable("Clause is not allowed in 'omp atomic'."); 3098 } 3099 } 3100 3101 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { 3102 bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); 3103 OpenMPClauseKind Kind = OMPC_unknown; 3104 for (auto *C : S.clauses()) { 3105 // Find first clause (skip seq_cst clause, if it is first). 3106 if (C->getClauseKind() != OMPC_seq_cst) { 3107 Kind = C->getClauseKind(); 3108 break; 3109 } 3110 } 3111 3112 const auto *CS = 3113 S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); 3114 if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { 3115 enterFullExpression(EWC); 3116 } 3117 // Processing for statements under 'atomic capture'. 3118 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { 3119 for (const auto *C : Compound->body()) { 3120 if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { 3121 enterFullExpression(EWC); 3122 } 3123 } 3124 } 3125 3126 auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, 3127 PrePostActionTy &) { 3128 CGF.EmitStopPoint(CS); 3129 EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), 3130 S.getV(), S.getExpr(), S.getUpdateExpr(), 3131 S.isXLHSInRHSPart(), S.getLocStart()); 3132 }; 3133 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 3134 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); 3135 } 3136 3137 std::pair<llvm::Function * /*OutlinedFn*/, llvm::Constant * /*OutlinedFnID*/> 3138 CodeGenFunction::EmitOMPTargetDirectiveOutlinedFunction( 3139 CodeGenModule &CGM, const OMPTargetDirective &S, StringRef ParentName, 3140 bool IsOffloadEntry) { 3141 llvm::Function *OutlinedFn = nullptr; 3142 llvm::Constant *OutlinedFnID = nullptr; 3143 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3144 OMPPrivateScope PrivateScope(CGF); 3145 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 3146 CGF.EmitOMPPrivateClause(S, PrivateScope); 3147 (void)PrivateScope.Privatize(); 3148 3149 Action.Enter(CGF); 3150 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3151 }; 3152 // Emit target region as a standalone region. 3153 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 3154 S, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, CodeGen); 3155 return std::make_pair(OutlinedFn, OutlinedFnID); 3156 } 3157 3158 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { 3159 const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt()); 3160 3161 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 3162 GenerateOpenMPCapturedVars(CS, CapturedVars); 3163 3164 llvm::Function *Fn = nullptr; 3165 llvm::Constant *FnID = nullptr; 3166 3167 // Check if we have any if clause associated with the directive. 3168 const Expr *IfCond = nullptr; 3169 3170 if (auto *C = S.getSingleClause<OMPIfClause>()) { 3171 IfCond = C->getCondition(); 3172 } 3173 3174 // Check if we have any device clause associated with the directive. 3175 const Expr *Device = nullptr; 3176 if (auto *C = S.getSingleClause<OMPDeviceClause>()) { 3177 Device = C->getDevice(); 3178 } 3179 3180 // Check if we have an if clause whose conditional always evaluates to false 3181 // or if we do not have any targets specified. If so the target region is not 3182 // an offload entry point. 3183 bool IsOffloadEntry = true; 3184 if (IfCond) { 3185 bool Val; 3186 if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) 3187 IsOffloadEntry = false; 3188 } 3189 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3190 IsOffloadEntry = false; 3191 3192 assert(CurFuncDecl && "No parent declaration for target region!"); 3193 StringRef ParentName; 3194 // In case we have Ctors/Dtors we use the complete type variant to produce 3195 // the mangling of the device outlined kernel. 3196 if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl)) 3197 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); 3198 else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl)) 3199 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); 3200 else 3201 ParentName = 3202 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl))); 3203 3204 std::tie(Fn, FnID) = EmitOMPTargetDirectiveOutlinedFunction( 3205 CGM, S, ParentName, IsOffloadEntry); 3206 OMPLexicalScope Scope(*this, S); 3207 CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device, 3208 CapturedVars); 3209 } 3210 3211 static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, 3212 const OMPExecutableDirective &S, 3213 OpenMPDirectiveKind InnermostKind, 3214 const RegionCodeGenTy &CodeGen) { 3215 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 3216 auto OutlinedFn = CGF.CGM.getOpenMPRuntime(). 3217 emitParallelOrTeamsOutlinedFunction(S, 3218 *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 3219 3220 const OMPTeamsDirective &TD = *dyn_cast<OMPTeamsDirective>(&S); 3221 const OMPNumTeamsClause *NT = TD.getSingleClause<OMPNumTeamsClause>(); 3222 const OMPThreadLimitClause *TL = TD.getSingleClause<OMPThreadLimitClause>(); 3223 if (NT || TL) { 3224 Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr; 3225 Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr; 3226 3227 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, 3228 S.getLocStart()); 3229 } 3230 3231 OMPLexicalScope Scope(CGF, S); 3232 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 3233 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 3234 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn, 3235 CapturedVars); 3236 } 3237 3238 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { 3239 // Emit parallel region as a standalone region. 3240 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 3241 OMPPrivateScope PrivateScope(CGF); 3242 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 3243 CGF.EmitOMPPrivateClause(S, PrivateScope); 3244 (void)PrivateScope.Privatize(); 3245 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3246 }; 3247 emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen); 3248 } 3249 3250 void CodeGenFunction::EmitOMPCancellationPointDirective( 3251 const OMPCancellationPointDirective &S) { 3252 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), 3253 S.getCancelRegion()); 3254 } 3255 3256 void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { 3257 const Expr *IfCond = nullptr; 3258 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 3259 if (C->getNameModifier() == OMPD_unknown || 3260 C->getNameModifier() == OMPD_cancel) { 3261 IfCond = C->getCondition(); 3262 break; 3263 } 3264 } 3265 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, 3266 S.getCancelRegion()); 3267 } 3268 3269 CodeGenFunction::JumpDest 3270 CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { 3271 if (Kind == OMPD_parallel || Kind == OMPD_task) 3272 return ReturnBlock; 3273 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || 3274 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for); 3275 return BreakContinueStack.back().BreakBlock; 3276 } 3277 3278 // Generate the instructions for '#pragma omp target data' directive. 3279 void CodeGenFunction::EmitOMPTargetDataDirective( 3280 const OMPTargetDataDirective &S) { 3281 // The target data enclosed region is implemented just by emitting the 3282 // statement. 3283 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 3284 CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); 3285 }; 3286 3287 // If we don't have target devices, don't bother emitting the data mapping 3288 // code. 3289 if (CGM.getLangOpts().OMPTargetTriples.empty()) { 3290 OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); 3291 3292 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_target_data, 3293 CodeGen); 3294 return; 3295 } 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().emitTargetDataCalls(*this, S, IfCond, Device, CodeGen); 3308 } 3309 3310 void CodeGenFunction::EmitOMPTargetEnterDataDirective( 3311 const OMPTargetEnterDataDirective &S) { 3312 // If we don't have target devices, don't bother emitting the data mapping 3313 // code. 3314 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3315 return; 3316 3317 // Check if we have any if clause associated with the directive. 3318 const Expr *IfCond = nullptr; 3319 if (auto *C = S.getSingleClause<OMPIfClause>()) 3320 IfCond = C->getCondition(); 3321 3322 // Check if we have any device clause associated with the directive. 3323 const Expr *Device = nullptr; 3324 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 3325 Device = C->getDevice(); 3326 3327 CGM.getOpenMPRuntime().emitTargetEnterOrExitDataCall(*this, S, IfCond, 3328 Device); 3329 } 3330 3331 void CodeGenFunction::EmitOMPTargetExitDataDirective( 3332 const OMPTargetExitDataDirective &S) { 3333 // If we don't have target devices, don't bother emitting the data mapping 3334 // code. 3335 if (CGM.getLangOpts().OMPTargetTriples.empty()) 3336 return; 3337 3338 // Check if we have any if clause associated with the directive. 3339 const Expr *IfCond = nullptr; 3340 if (auto *C = S.getSingleClause<OMPIfClause>()) 3341 IfCond = C->getCondition(); 3342 3343 // Check if we have any device clause associated with the directive. 3344 const Expr *Device = nullptr; 3345 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 3346 Device = C->getDevice(); 3347 3348 CGM.getOpenMPRuntime().emitTargetEnterOrExitDataCall(*this, S, IfCond, 3349 Device); 3350 } 3351 3352 void CodeGenFunction::EmitOMPTargetParallelDirective( 3353 const OMPTargetParallelDirective &S) { 3354 // TODO: codegen for target parallel. 3355 } 3356 3357 void CodeGenFunction::EmitOMPTargetParallelForDirective( 3358 const OMPTargetParallelForDirective &S) { 3359 // TODO: codegen for target parallel for. 3360 } 3361 3362 /// Emit a helper variable and return corresponding lvalue. 3363 static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, 3364 const ImplicitParamDecl *PVD, 3365 CodeGenFunction::OMPPrivateScope &Privates) { 3366 auto *VDecl = cast<VarDecl>(Helper->getDecl()); 3367 Privates.addPrivate( 3368 VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); }); 3369 } 3370 3371 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { 3372 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); 3373 // Emit outlined function for task construct. 3374 auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); 3375 auto CapturedStruct = GenerateCapturedStmtArgument(*CS); 3376 auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 3377 const Expr *IfCond = nullptr; 3378 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 3379 if (C->getNameModifier() == OMPD_unknown || 3380 C->getNameModifier() == OMPD_taskloop) { 3381 IfCond = C->getCondition(); 3382 break; 3383 } 3384 } 3385 3386 OMPTaskDataTy Data; 3387 // Check if taskloop must be emitted without taskgroup. 3388 Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); 3389 // TODO: Check if we should emit tied or untied task. 3390 Data.Tied = true; 3391 // Set scheduling for taskloop 3392 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { 3393 // grainsize clause 3394 Data.Schedule.setInt(/*IntVal=*/false); 3395 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); 3396 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { 3397 // num_tasks clause 3398 Data.Schedule.setInt(/*IntVal=*/true); 3399 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); 3400 } 3401 3402 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { 3403 // if (PreCond) { 3404 // for (IV in 0..LastIteration) BODY; 3405 // <Final counter/linear vars updates>; 3406 // } 3407 // 3408 3409 // Emit: if (PreCond) - begin. 3410 // If the condition constant folds and can be elided, avoid emitting the 3411 // whole loop. 3412 bool CondConstant; 3413 llvm::BasicBlock *ContBlock = nullptr; 3414 OMPLoopScope PreInitScope(CGF, S); 3415 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 3416 if (!CondConstant) 3417 return; 3418 } else { 3419 auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); 3420 ContBlock = CGF.createBasicBlock("taskloop.if.end"); 3421 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 3422 CGF.getProfileCount(&S)); 3423 CGF.EmitBlock(ThenBlock); 3424 CGF.incrementProfileCounter(&S); 3425 } 3426 3427 if (isOpenMPSimdDirective(S.getDirectiveKind())) 3428 CGF.EmitOMPSimdInit(S); 3429 3430 OMPPrivateScope LoopScope(CGF); 3431 // Emit helper vars inits. 3432 enum { LowerBound = 5, UpperBound, Stride, LastIter }; 3433 auto *I = CS->getCapturedDecl()->param_begin(); 3434 auto *LBP = std::next(I, LowerBound); 3435 auto *UBP = std::next(I, UpperBound); 3436 auto *STP = std::next(I, Stride); 3437 auto *LIP = std::next(I, LastIter); 3438 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, 3439 LoopScope); 3440 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, 3441 LoopScope); 3442 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); 3443 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, 3444 LoopScope); 3445 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 3446 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 3447 (void)LoopScope.Privatize(); 3448 // Emit the loop iteration variable. 3449 const Expr *IVExpr = S.getIterationVariable(); 3450 const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 3451 CGF.EmitVarDecl(*IVDecl); 3452 CGF.EmitIgnoredExpr(S.getInit()); 3453 3454 // Emit the iterations count variable. 3455 // If it is not a variable, Sema decided to calculate iterations count on 3456 // each iteration (e.g., it is foldable into a constant). 3457 if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 3458 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 3459 // Emit calculation of the iterations count. 3460 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 3461 } 3462 3463 CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), 3464 S.getInc(), 3465 [&S](CodeGenFunction &CGF) { 3466 CGF.EmitOMPLoopBody(S, JumpDest()); 3467 CGF.EmitStopPoint(&S); 3468 }, 3469 [](CodeGenFunction &) {}); 3470 // Emit: if (PreCond) - end. 3471 if (ContBlock) { 3472 CGF.EmitBranch(ContBlock); 3473 CGF.EmitBlock(ContBlock, true); 3474 } 3475 // Emit final copy of the lastprivate variables if IsLastIter != 0. 3476 if (HasLastprivateClause) { 3477 CGF.EmitOMPLastprivateClauseFinal( 3478 S, isOpenMPSimdDirective(S.getDirectiveKind()), 3479 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( 3480 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, 3481 (*LIP)->getType(), S.getLocStart()))); 3482 } 3483 }; 3484 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 3485 IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, 3486 const OMPTaskDataTy &Data) { 3487 auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) { 3488 OMPLoopScope PreInitScope(CGF, S); 3489 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S, 3490 OutlinedFn, SharedsTy, 3491 CapturedStruct, IfCond, Data); 3492 }; 3493 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, 3494 CodeGen); 3495 }; 3496 EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); 3497 } 3498 3499 void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { 3500 EmitOMPTaskLoopBasedDirective(S); 3501 } 3502 3503 void CodeGenFunction::EmitOMPTaskLoopSimdDirective( 3504 const OMPTaskLoopSimdDirective &S) { 3505 EmitOMPTaskLoopBasedDirective(S); 3506 } 3507