1 //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to emit OpenMP nodes as LLVM code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CGCleanup.h" 14 #include "CGOpenMPRuntime.h" 15 #include "CodeGenFunction.h" 16 #include "CodeGenModule.h" 17 #include "TargetInfo.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/Attr.h" 20 #include "clang/AST/DeclOpenMP.h" 21 #include "clang/AST/OpenMPClause.h" 22 #include "clang/AST/Stmt.h" 23 #include "clang/AST/StmtOpenMP.h" 24 #include "clang/Basic/OpenMPKinds.h" 25 #include "clang/Basic/PrettyStackTrace.h" 26 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/Instructions.h" 29 #include "llvm/Support/AtomicOrdering.h" 30 using namespace clang; 31 using namespace CodeGen; 32 using namespace llvm::omp; 33 34 namespace { 35 /// Lexical scope for OpenMP executable constructs, that handles correct codegen 36 /// for captured expressions. 37 class OMPLexicalScope : public CodeGenFunction::LexicalScope { 38 void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { 39 for (const auto *C : S.clauses()) { 40 if (const auto *CPI = OMPClauseWithPreInit::get(C)) { 41 if (const auto *PreInit = 42 cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { 43 for (const auto *I : PreInit->decls()) { 44 if (!I->hasAttr<OMPCaptureNoInitAttr>()) { 45 CGF.EmitVarDecl(cast<VarDecl>(*I)); 46 } else { 47 CodeGenFunction::AutoVarEmission Emission = 48 CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); 49 CGF.EmitAutoVarCleanups(Emission); 50 } 51 } 52 } 53 } 54 } 55 } 56 CodeGenFunction::OMPPrivateScope InlinedShareds; 57 58 static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { 59 return CGF.LambdaCaptureFields.lookup(VD) || 60 (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || 61 (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && 62 cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); 63 } 64 65 public: 66 OMPLexicalScope( 67 CodeGenFunction &CGF, const OMPExecutableDirective &S, 68 const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None, 69 const bool EmitPreInitStmt = true) 70 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), 71 InlinedShareds(CGF) { 72 if (EmitPreInitStmt) 73 emitPreInitStmt(CGF, S); 74 if (!CapturedRegion.hasValue()) 75 return; 76 assert(S.hasAssociatedStmt() && 77 "Expected associated statement for inlined directive."); 78 const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion); 79 for (const auto &C : CS->captures()) { 80 if (C.capturesVariable() || C.capturesVariableByCopy()) { 81 auto *VD = C.getCapturedVar(); 82 assert(VD == VD->getCanonicalDecl() && 83 "Canonical decl must be captured."); 84 DeclRefExpr DRE( 85 CGF.getContext(), const_cast<VarDecl *>(VD), 86 isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo && 87 InlinedShareds.isGlobalVarCaptured(VD)), 88 VD->getType().getNonReferenceType(), VK_LValue, C.getLocation()); 89 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { 90 return CGF.EmitLValue(&DRE).getAddress(CGF); 91 }); 92 } 93 } 94 (void)InlinedShareds.Privatize(); 95 } 96 }; 97 98 /// Lexical scope for OpenMP parallel construct, that handles correct codegen 99 /// for captured expressions. 100 class OMPParallelScope final : public OMPLexicalScope { 101 bool EmitPreInitStmt(const OMPExecutableDirective &S) { 102 OpenMPDirectiveKind Kind = S.getDirectiveKind(); 103 return !(isOpenMPTargetExecutionDirective(Kind) || 104 isOpenMPLoopBoundSharingDirective(Kind)) && 105 isOpenMPParallelDirective(Kind); 106 } 107 108 public: 109 OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) 110 : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, 111 EmitPreInitStmt(S)) {} 112 }; 113 114 /// Lexical scope for OpenMP teams construct, that handles correct codegen 115 /// for captured expressions. 116 class OMPTeamsScope final : public OMPLexicalScope { 117 bool EmitPreInitStmt(const OMPExecutableDirective &S) { 118 OpenMPDirectiveKind Kind = S.getDirectiveKind(); 119 return !isOpenMPTargetExecutionDirective(Kind) && 120 isOpenMPTeamsDirective(Kind); 121 } 122 123 public: 124 OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) 125 : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, 126 EmitPreInitStmt(S)) {} 127 }; 128 129 /// Private scope for OpenMP loop-based directives, that supports capturing 130 /// of used expression from loop statement. 131 class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { 132 void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { 133 CodeGenFunction::OMPMapVars PreCondVars; 134 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 135 for (const auto *E : S.counters()) { 136 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 137 EmittedAsPrivate.insert(VD->getCanonicalDecl()); 138 (void)PreCondVars.setVarAddr( 139 CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType())); 140 } 141 // Mark private vars as undefs. 142 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { 143 for (const Expr *IRef : C->varlists()) { 144 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); 145 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 146 (void)PreCondVars.setVarAddr( 147 CGF, OrigVD, 148 Address(llvm::UndefValue::get( 149 CGF.ConvertTypeForMem(CGF.getContext().getPointerType( 150 OrigVD->getType().getNonReferenceType()))), 151 CGF.getContext().getDeclAlign(OrigVD))); 152 } 153 } 154 } 155 (void)PreCondVars.apply(CGF); 156 // Emit init, __range and __end variables for C++ range loops. 157 const Stmt *Body = 158 S.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers(); 159 for (unsigned Cnt = 0; Cnt < S.getCollapsedNumber(); ++Cnt) { 160 Body = OMPLoopDirective::tryToFindNextInnerLoop( 161 Body, /*TryImperfectlyNestedLoops=*/true); 162 if (auto *For = dyn_cast<ForStmt>(Body)) { 163 Body = For->getBody(); 164 } else { 165 assert(isa<CXXForRangeStmt>(Body) && 166 "Expected canonical for loop or range-based for loop."); 167 auto *CXXFor = cast<CXXForRangeStmt>(Body); 168 if (const Stmt *Init = CXXFor->getInit()) 169 CGF.EmitStmt(Init); 170 CGF.EmitStmt(CXXFor->getRangeStmt()); 171 CGF.EmitStmt(CXXFor->getEndStmt()); 172 Body = CXXFor->getBody(); 173 } 174 } 175 if (const auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) { 176 for (const auto *I : PreInits->decls()) 177 CGF.EmitVarDecl(cast<VarDecl>(*I)); 178 } 179 PreCondVars.restore(CGF); 180 } 181 182 public: 183 OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) 184 : CodeGenFunction::RunCleanupsScope(CGF) { 185 emitPreInitStmt(CGF, S); 186 } 187 }; 188 189 class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { 190 CodeGenFunction::OMPPrivateScope InlinedShareds; 191 192 static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { 193 return CGF.LambdaCaptureFields.lookup(VD) || 194 (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || 195 (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && 196 cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); 197 } 198 199 public: 200 OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) 201 : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), 202 InlinedShareds(CGF) { 203 for (const auto *C : S.clauses()) { 204 if (const auto *CPI = OMPClauseWithPreInit::get(C)) { 205 if (const auto *PreInit = 206 cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { 207 for (const auto *I : PreInit->decls()) { 208 if (!I->hasAttr<OMPCaptureNoInitAttr>()) { 209 CGF.EmitVarDecl(cast<VarDecl>(*I)); 210 } else { 211 CodeGenFunction::AutoVarEmission Emission = 212 CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); 213 CGF.EmitAutoVarCleanups(Emission); 214 } 215 } 216 } 217 } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { 218 for (const Expr *E : UDP->varlists()) { 219 const Decl *D = cast<DeclRefExpr>(E)->getDecl(); 220 if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) 221 CGF.EmitVarDecl(*OED); 222 } 223 } 224 } 225 if (!isOpenMPSimdDirective(S.getDirectiveKind())) 226 CGF.EmitOMPPrivateClause(S, InlinedShareds); 227 if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { 228 if (const Expr *E = TG->getReductionRef()) 229 CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); 230 } 231 const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); 232 while (CS) { 233 for (auto &C : CS->captures()) { 234 if (C.capturesVariable() || C.capturesVariableByCopy()) { 235 auto *VD = C.getCapturedVar(); 236 assert(VD == VD->getCanonicalDecl() && 237 "Canonical decl must be captured."); 238 DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD), 239 isCapturedVar(CGF, VD) || 240 (CGF.CapturedStmtInfo && 241 InlinedShareds.isGlobalVarCaptured(VD)), 242 VD->getType().getNonReferenceType(), VK_LValue, 243 C.getLocation()); 244 InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { 245 return CGF.EmitLValue(&DRE).getAddress(CGF); 246 }); 247 } 248 } 249 CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); 250 } 251 (void)InlinedShareds.Privatize(); 252 } 253 }; 254 255 } // namespace 256 257 static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, 258 const OMPExecutableDirective &S, 259 const RegionCodeGenTy &CodeGen); 260 261 LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { 262 if (const auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { 263 if (const auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { 264 OrigVD = OrigVD->getCanonicalDecl(); 265 bool IsCaptured = 266 LambdaCaptureFields.lookup(OrigVD) || 267 (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || 268 (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); 269 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), IsCaptured, 270 OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); 271 return EmitLValue(&DRE); 272 } 273 } 274 return EmitLValue(E); 275 } 276 277 llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { 278 ASTContext &C = getContext(); 279 llvm::Value *Size = nullptr; 280 auto SizeInChars = C.getTypeSizeInChars(Ty); 281 if (SizeInChars.isZero()) { 282 // getTypeSizeInChars() returns 0 for a VLA. 283 while (const VariableArrayType *VAT = C.getAsVariableArrayType(Ty)) { 284 VlaSizePair VlaSize = getVLASize(VAT); 285 Ty = VlaSize.Type; 286 Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts) 287 : VlaSize.NumElts; 288 } 289 SizeInChars = C.getTypeSizeInChars(Ty); 290 if (SizeInChars.isZero()) 291 return llvm::ConstantInt::get(SizeTy, /*V=*/0); 292 return Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); 293 } 294 return CGM.getSize(SizeInChars); 295 } 296 297 void CodeGenFunction::GenerateOpenMPCapturedVars( 298 const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { 299 const RecordDecl *RD = S.getCapturedRecordDecl(); 300 auto CurField = RD->field_begin(); 301 auto CurCap = S.captures().begin(); 302 for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), 303 E = S.capture_init_end(); 304 I != E; ++I, ++CurField, ++CurCap) { 305 if (CurField->hasCapturedVLAType()) { 306 const VariableArrayType *VAT = CurField->getCapturedVLAType(); 307 llvm::Value *Val = VLASizeMap[VAT->getSizeExpr()]; 308 CapturedVars.push_back(Val); 309 } else if (CurCap->capturesThis()) { 310 CapturedVars.push_back(CXXThisValue); 311 } else if (CurCap->capturesVariableByCopy()) { 312 llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation()); 313 314 // If the field is not a pointer, we need to save the actual value 315 // and load it as a void pointer. 316 if (!CurField->getType()->isAnyPointerType()) { 317 ASTContext &Ctx = getContext(); 318 Address DstAddr = CreateMemTemp( 319 Ctx.getUIntPtrType(), 320 Twine(CurCap->getCapturedVar()->getName(), ".casted")); 321 LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); 322 323 llvm::Value *SrcAddrVal = EmitScalarConversion( 324 DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), 325 Ctx.getPointerType(CurField->getType()), CurCap->getLocation()); 326 LValue SrcLV = 327 MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); 328 329 // Store the value using the source type pointer. 330 EmitStoreThroughLValue(RValue::get(CV), SrcLV); 331 332 // Load the value using the destination type pointer. 333 CV = EmitLoadOfScalar(DstLV, CurCap->getLocation()); 334 } 335 CapturedVars.push_back(CV); 336 } else { 337 assert(CurCap->capturesVariable() && "Expected capture by reference."); 338 CapturedVars.push_back(EmitLValue(*I).getAddress(*this).getPointer()); 339 } 340 } 341 } 342 343 static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc, 344 QualType DstType, StringRef Name, 345 LValue AddrLV) { 346 ASTContext &Ctx = CGF.getContext(); 347 348 llvm::Value *CastedPtr = CGF.EmitScalarConversion( 349 AddrLV.getAddress(CGF).getPointer(), Ctx.getUIntPtrType(), 350 Ctx.getPointerType(DstType), Loc); 351 Address TmpAddr = 352 CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) 353 .getAddress(CGF); 354 return TmpAddr; 355 } 356 357 static QualType getCanonicalParamType(ASTContext &C, QualType T) { 358 if (T->isLValueReferenceType()) 359 return C.getLValueReferenceType( 360 getCanonicalParamType(C, T.getNonReferenceType()), 361 /*SpelledAsLValue=*/false); 362 if (T->isPointerType()) 363 return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); 364 if (const ArrayType *A = T->getAsArrayTypeUnsafe()) { 365 if (const auto *VLA = dyn_cast<VariableArrayType>(A)) 366 return getCanonicalParamType(C, VLA->getElementType()); 367 if (!A->isVariablyModifiedType()) 368 return C.getCanonicalType(T); 369 } 370 return C.getCanonicalParamType(T); 371 } 372 373 namespace { 374 /// Contains required data for proper outlined function codegen. 375 struct FunctionOptions { 376 /// Captured statement for which the function is generated. 377 const CapturedStmt *S = nullptr; 378 /// true if cast to/from UIntPtr is required for variables captured by 379 /// value. 380 const bool UIntPtrCastRequired = true; 381 /// true if only casted arguments must be registered as local args or VLA 382 /// sizes. 383 const bool RegisterCastedArgsOnly = false; 384 /// Name of the generated function. 385 const StringRef FunctionName; 386 /// Location of the non-debug version of the outlined function. 387 SourceLocation Loc; 388 explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, 389 bool RegisterCastedArgsOnly, StringRef FunctionName, 390 SourceLocation Loc) 391 : S(S), UIntPtrCastRequired(UIntPtrCastRequired), 392 RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), 393 FunctionName(FunctionName), Loc(Loc) {} 394 }; 395 } // namespace 396 397 static llvm::Function *emitOutlinedFunctionPrologue( 398 CodeGenFunction &CGF, FunctionArgList &Args, 399 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> 400 &LocalAddrs, 401 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> 402 &VLASizes, 403 llvm::Value *&CXXThisValue, const FunctionOptions &FO) { 404 const CapturedDecl *CD = FO.S->getCapturedDecl(); 405 const RecordDecl *RD = FO.S->getCapturedRecordDecl(); 406 assert(CD->hasBody() && "missing CapturedDecl body"); 407 408 CXXThisValue = nullptr; 409 // Build the argument list. 410 CodeGenModule &CGM = CGF.CGM; 411 ASTContext &Ctx = CGM.getContext(); 412 FunctionArgList TargetArgs; 413 Args.append(CD->param_begin(), 414 std::next(CD->param_begin(), CD->getContextParamPosition())); 415 TargetArgs.append( 416 CD->param_begin(), 417 std::next(CD->param_begin(), CD->getContextParamPosition())); 418 auto I = FO.S->captures().begin(); 419 FunctionDecl *DebugFunctionDecl = nullptr; 420 if (!FO.UIntPtrCastRequired) { 421 FunctionProtoType::ExtProtoInfo EPI; 422 QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI); 423 DebugFunctionDecl = FunctionDecl::Create( 424 Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(), 425 SourceLocation(), DeclarationName(), FunctionTy, 426 Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static, 427 /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); 428 } 429 for (const FieldDecl *FD : RD->fields()) { 430 QualType ArgType = FD->getType(); 431 IdentifierInfo *II = nullptr; 432 VarDecl *CapVar = nullptr; 433 434 // If this is a capture by copy and the type is not a pointer, the outlined 435 // function argument type should be uintptr and the value properly casted to 436 // uintptr. This is necessary given that the runtime library is only able to 437 // deal with pointers. We can pass in the same way the VLA type sizes to the 438 // outlined function. 439 if (FO.UIntPtrCastRequired && 440 ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || 441 I->capturesVariableArrayType())) 442 ArgType = Ctx.getUIntPtrType(); 443 444 if (I->capturesVariable() || I->capturesVariableByCopy()) { 445 CapVar = I->getCapturedVar(); 446 II = CapVar->getIdentifier(); 447 } else if (I->capturesThis()) { 448 II = &Ctx.Idents.get("this"); 449 } else { 450 assert(I->capturesVariableArrayType()); 451 II = &Ctx.Idents.get("vla"); 452 } 453 if (ArgType->isVariablyModifiedType()) 454 ArgType = getCanonicalParamType(Ctx, ArgType); 455 VarDecl *Arg; 456 if (DebugFunctionDecl && (CapVar || I->capturesThis())) { 457 Arg = ParmVarDecl::Create( 458 Ctx, DebugFunctionDecl, 459 CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(), 460 CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, 461 /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); 462 } else { 463 Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), 464 II, ArgType, ImplicitParamDecl::Other); 465 } 466 Args.emplace_back(Arg); 467 // Do not cast arguments if we emit function with non-original types. 468 TargetArgs.emplace_back( 469 FO.UIntPtrCastRequired 470 ? Arg 471 : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); 472 ++I; 473 } 474 Args.append( 475 std::next(CD->param_begin(), CD->getContextParamPosition() + 1), 476 CD->param_end()); 477 TargetArgs.append( 478 std::next(CD->param_begin(), CD->getContextParamPosition() + 1), 479 CD->param_end()); 480 481 // Create the function declaration. 482 const CGFunctionInfo &FuncInfo = 483 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); 484 llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); 485 486 auto *F = 487 llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, 488 FO.FunctionName, &CGM.getModule()); 489 CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); 490 if (CD->isNothrow()) 491 F->setDoesNotThrow(); 492 F->setDoesNotRecurse(); 493 494 // Generate the function. 495 CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, 496 FO.UIntPtrCastRequired ? FO.Loc : FO.S->getBeginLoc(), 497 FO.UIntPtrCastRequired ? FO.Loc 498 : CD->getBody()->getBeginLoc()); 499 unsigned Cnt = CD->getContextParamPosition(); 500 I = FO.S->captures().begin(); 501 for (const FieldDecl *FD : RD->fields()) { 502 // Do not map arguments if we emit function with non-original types. 503 Address LocalAddr(Address::invalid()); 504 if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { 505 LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], 506 TargetArgs[Cnt]); 507 } else { 508 LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); 509 } 510 // If we are capturing a pointer by copy we don't need to do anything, just 511 // use the value that we get from the arguments. 512 if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { 513 const VarDecl *CurVD = I->getCapturedVar(); 514 if (!FO.RegisterCastedArgsOnly) 515 LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); 516 ++Cnt; 517 ++I; 518 continue; 519 } 520 521 LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), 522 AlignmentSource::Decl); 523 if (FD->hasCapturedVLAType()) { 524 if (FO.UIntPtrCastRequired) { 525 ArgLVal = CGF.MakeAddrLValue( 526 castValueFromUintptr(CGF, I->getLocation(), FD->getType(), 527 Args[Cnt]->getName(), ArgLVal), 528 FD->getType(), AlignmentSource::Decl); 529 } 530 llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); 531 const VariableArrayType *VAT = FD->getCapturedVLAType(); 532 VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg); 533 } else if (I->capturesVariable()) { 534 const VarDecl *Var = I->getCapturedVar(); 535 QualType VarTy = Var->getType(); 536 Address ArgAddr = ArgLVal.getAddress(CGF); 537 if (ArgLVal.getType()->isLValueReferenceType()) { 538 ArgAddr = CGF.EmitLoadOfReference(ArgLVal); 539 } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { 540 assert(ArgLVal.getType()->isPointerType()); 541 ArgAddr = CGF.EmitLoadOfPointer( 542 ArgAddr, ArgLVal.getType()->castAs<PointerType>()); 543 } 544 if (!FO.RegisterCastedArgsOnly) { 545 LocalAddrs.insert( 546 {Args[Cnt], 547 {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); 548 } 549 } else if (I->capturesVariableByCopy()) { 550 assert(!FD->getType()->isAnyPointerType() && 551 "Not expecting a captured pointer."); 552 const VarDecl *Var = I->getCapturedVar(); 553 LocalAddrs.insert({Args[Cnt], 554 {Var, FO.UIntPtrCastRequired 555 ? castValueFromUintptr( 556 CGF, I->getLocation(), FD->getType(), 557 Args[Cnt]->getName(), ArgLVal) 558 : ArgLVal.getAddress(CGF)}}); 559 } else { 560 // If 'this' is captured, load it into CXXThisValue. 561 assert(I->capturesThis()); 562 CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); 563 LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress(CGF)}}); 564 } 565 ++Cnt; 566 ++I; 567 } 568 569 return F; 570 } 571 572 llvm::Function * 573 CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S, 574 SourceLocation Loc) { 575 assert( 576 CapturedStmtInfo && 577 "CapturedStmtInfo should be set when generating the captured function"); 578 const CapturedDecl *CD = S.getCapturedDecl(); 579 // Build the argument list. 580 bool NeedWrapperFunction = 581 getDebugInfo() && CGM.getCodeGenOpts().hasReducedDebugInfo(); 582 FunctionArgList Args; 583 llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; 584 llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; 585 SmallString<256> Buffer; 586 llvm::raw_svector_ostream Out(Buffer); 587 Out << CapturedStmtInfo->getHelperName(); 588 if (NeedWrapperFunction) 589 Out << "_debug__"; 590 FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, 591 Out.str(), Loc); 592 llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, 593 VLASizes, CXXThisValue, FO); 594 CodeGenFunction::OMPPrivateScope LocalScope(*this); 595 for (const auto &LocalAddrPair : LocalAddrs) { 596 if (LocalAddrPair.second.first) { 597 LocalScope.addPrivate(LocalAddrPair.second.first, [&LocalAddrPair]() { 598 return LocalAddrPair.second.second; 599 }); 600 } 601 } 602 (void)LocalScope.Privatize(); 603 for (const auto &VLASizePair : VLASizes) 604 VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; 605 PGO.assignRegionCounters(GlobalDecl(CD), F); 606 CapturedStmtInfo->EmitBody(*this, CD->getBody()); 607 (void)LocalScope.ForceCleanup(); 608 FinishFunction(CD->getBodyRBrace()); 609 if (!NeedWrapperFunction) 610 return F; 611 612 FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, 613 /*RegisterCastedArgsOnly=*/true, 614 CapturedStmtInfo->getHelperName(), Loc); 615 CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); 616 WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; 617 Args.clear(); 618 LocalAddrs.clear(); 619 VLASizes.clear(); 620 llvm::Function *WrapperF = 621 emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, 622 WrapperCGF.CXXThisValue, WrapperFO); 623 llvm::SmallVector<llvm::Value *, 4> CallArgs; 624 for (const auto *Arg : Args) { 625 llvm::Value *CallArg; 626 auto I = LocalAddrs.find(Arg); 627 if (I != LocalAddrs.end()) { 628 LValue LV = WrapperCGF.MakeAddrLValue( 629 I->second.second, 630 I->second.first ? I->second.first->getType() : Arg->getType(), 631 AlignmentSource::Decl); 632 CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); 633 } else { 634 auto EI = VLASizes.find(Arg); 635 if (EI != VLASizes.end()) { 636 CallArg = EI->second.second; 637 } else { 638 LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), 639 Arg->getType(), 640 AlignmentSource::Decl); 641 CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); 642 } 643 } 644 CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); 645 } 646 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, Loc, F, CallArgs); 647 WrapperCGF.FinishFunction(); 648 return WrapperF; 649 } 650 651 //===----------------------------------------------------------------------===// 652 // OpenMP Directive Emission 653 //===----------------------------------------------------------------------===// 654 void CodeGenFunction::EmitOMPAggregateAssign( 655 Address DestAddr, Address SrcAddr, QualType OriginalType, 656 const llvm::function_ref<void(Address, Address)> CopyGen) { 657 // Perform element-by-element initialization. 658 QualType ElementTy; 659 660 // Drill down to the base element type on both arrays. 661 const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe(); 662 llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); 663 SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); 664 665 llvm::Value *SrcBegin = SrcAddr.getPointer(); 666 llvm::Value *DestBegin = DestAddr.getPointer(); 667 // Cast from pointer to array type to pointer to single element. 668 llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements); 669 // The basic structure here is a while-do loop. 670 llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body"); 671 llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done"); 672 llvm::Value *IsEmpty = 673 Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); 674 Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); 675 676 // Enter the loop body, making that address the current address. 677 llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); 678 EmitBlock(BodyBB); 679 680 CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); 681 682 llvm::PHINode *SrcElementPHI = 683 Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); 684 SrcElementPHI->addIncoming(SrcBegin, EntryBB); 685 Address SrcElementCurrent = 686 Address(SrcElementPHI, 687 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 688 689 llvm::PHINode *DestElementPHI = 690 Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); 691 DestElementPHI->addIncoming(DestBegin, EntryBB); 692 Address DestElementCurrent = 693 Address(DestElementPHI, 694 DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); 695 696 // Emit copy. 697 CopyGen(DestElementCurrent, SrcElementCurrent); 698 699 // Shift the address forward by one element. 700 llvm::Value *DestElementNext = Builder.CreateConstGEP1_32( 701 DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); 702 llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32( 703 SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); 704 // Check whether we've reached the end. 705 llvm::Value *Done = 706 Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); 707 Builder.CreateCondBr(Done, DoneBB, BodyBB); 708 DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); 709 SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); 710 711 // Done. 712 EmitBlock(DoneBB, /*IsFinished=*/true); 713 } 714 715 void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, 716 Address SrcAddr, const VarDecl *DestVD, 717 const VarDecl *SrcVD, const Expr *Copy) { 718 if (OriginalType->isArrayType()) { 719 const auto *BO = dyn_cast<BinaryOperator>(Copy); 720 if (BO && BO->getOpcode() == BO_Assign) { 721 // Perform simple memcpy for simple copying. 722 LValue Dest = MakeAddrLValue(DestAddr, OriginalType); 723 LValue Src = MakeAddrLValue(SrcAddr, OriginalType); 724 EmitAggregateAssign(Dest, Src, OriginalType); 725 } else { 726 // For arrays with complex element types perform element by element 727 // copying. 728 EmitOMPAggregateAssign( 729 DestAddr, SrcAddr, OriginalType, 730 [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { 731 // Working with the single array element, so have to remap 732 // destination and source variables to corresponding array 733 // elements. 734 CodeGenFunction::OMPPrivateScope Remap(*this); 735 Remap.addPrivate(DestVD, [DestElement]() { return DestElement; }); 736 Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; }); 737 (void)Remap.Privatize(); 738 EmitIgnoredExpr(Copy); 739 }); 740 } 741 } else { 742 // Remap pseudo source variable to private copy. 743 CodeGenFunction::OMPPrivateScope Remap(*this); 744 Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; }); 745 Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; }); 746 (void)Remap.Privatize(); 747 // Emit copying of the whole variable. 748 EmitIgnoredExpr(Copy); 749 } 750 } 751 752 bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, 753 OMPPrivateScope &PrivateScope) { 754 if (!HaveInsertPoint()) 755 return false; 756 bool DeviceConstTarget = 757 getLangOpts().OpenMPIsDevice && 758 isOpenMPTargetExecutionDirective(D.getDirectiveKind()); 759 bool FirstprivateIsLastprivate = false; 760 llvm::DenseMap<const VarDecl *, OpenMPLastprivateModifier> Lastprivates; 761 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 762 for (const auto *D : C->varlists()) 763 Lastprivates.try_emplace( 764 cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl(), 765 C->getKind()); 766 } 767 llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; 768 llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; 769 getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); 770 // Force emission of the firstprivate copy if the directive does not emit 771 // outlined function, like omp for, omp simd, omp distribute etc. 772 bool MustEmitFirstprivateCopy = 773 CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; 774 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { 775 const auto *IRef = C->varlist_begin(); 776 const auto *InitsRef = C->inits().begin(); 777 for (const Expr *IInit : C->private_copies()) { 778 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 779 bool ThisFirstprivateIsLastprivate = 780 Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; 781 const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD); 782 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 783 if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && 784 !FD->getType()->isReferenceType() && 785 (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { 786 EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); 787 ++IRef; 788 ++InitsRef; 789 continue; 790 } 791 // Do not emit copy for firstprivate constant variables in target regions, 792 // captured by reference. 793 if (DeviceConstTarget && OrigVD->getType().isConstant(getContext()) && 794 FD && FD->getType()->isReferenceType() && 795 (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { 796 (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this, 797 OrigVD); 798 ++IRef; 799 ++InitsRef; 800 continue; 801 } 802 FirstprivateIsLastprivate = 803 FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; 804 if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { 805 const auto *VDInit = 806 cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); 807 bool IsRegistered; 808 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), 809 /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, 810 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 811 LValue OriginalLVal; 812 if (!FD) { 813 // Check if the firstprivate variable is just a constant value. 814 ConstantEmission CE = tryEmitAsConstant(&DRE); 815 if (CE && !CE.isReference()) { 816 // Constant value, no need to create a copy. 817 ++IRef; 818 ++InitsRef; 819 continue; 820 } 821 if (CE && CE.isReference()) { 822 OriginalLVal = CE.getReferenceLValue(*this, &DRE); 823 } else { 824 assert(!CE && "Expected non-constant firstprivate."); 825 OriginalLVal = EmitLValue(&DRE); 826 } 827 } else { 828 OriginalLVal = EmitLValue(&DRE); 829 } 830 QualType Type = VD->getType(); 831 if (Type->isArrayType()) { 832 // Emit VarDecl with copy init for arrays. 833 // Get the address of the original variable captured in current 834 // captured region. 835 IsRegistered = PrivateScope.addPrivate( 836 OrigVD, [this, VD, Type, OriginalLVal, VDInit]() { 837 AutoVarEmission Emission = EmitAutoVarAlloca(*VD); 838 const Expr *Init = VD->getInit(); 839 if (!isa<CXXConstructExpr>(Init) || 840 isTrivialInitializer(Init)) { 841 // Perform simple memcpy. 842 LValue Dest = 843 MakeAddrLValue(Emission.getAllocatedAddress(), Type); 844 EmitAggregateAssign(Dest, OriginalLVal, Type); 845 } else { 846 EmitOMPAggregateAssign( 847 Emission.getAllocatedAddress(), 848 OriginalLVal.getAddress(*this), Type, 849 [this, VDInit, Init](Address DestElement, 850 Address SrcElement) { 851 // Clean up any temporaries needed by the 852 // initialization. 853 RunCleanupsScope InitScope(*this); 854 // Emit initialization for single element. 855 setAddrOfLocalVar(VDInit, SrcElement); 856 EmitAnyExprToMem(Init, DestElement, 857 Init->getType().getQualifiers(), 858 /*IsInitializer*/ false); 859 LocalDeclMap.erase(VDInit); 860 }); 861 } 862 EmitAutoVarCleanups(Emission); 863 return Emission.getAllocatedAddress(); 864 }); 865 } else { 866 Address OriginalAddr = OriginalLVal.getAddress(*this); 867 IsRegistered = 868 PrivateScope.addPrivate(OrigVD, [this, VDInit, OriginalAddr, VD, 869 ThisFirstprivateIsLastprivate, 870 OrigVD, &Lastprivates, IRef]() { 871 // Emit private VarDecl with copy init. 872 // Remap temp VDInit variable to the address of the original 873 // variable (for proper handling of captured global variables). 874 setAddrOfLocalVar(VDInit, OriginalAddr); 875 EmitDecl(*VD); 876 LocalDeclMap.erase(VDInit); 877 if (ThisFirstprivateIsLastprivate && 878 Lastprivates[OrigVD->getCanonicalDecl()] == 879 OMPC_LASTPRIVATE_conditional) { 880 // Create/init special variable for lastprivate conditionals. 881 Address VDAddr = 882 CGM.getOpenMPRuntime().emitLastprivateConditionalInit( 883 *this, OrigVD); 884 llvm::Value *V = EmitLoadOfScalar( 885 MakeAddrLValue(GetAddrOfLocalVar(VD), (*IRef)->getType(), 886 AlignmentSource::Decl), 887 (*IRef)->getExprLoc()); 888 EmitStoreOfScalar(V, 889 MakeAddrLValue(VDAddr, (*IRef)->getType(), 890 AlignmentSource::Decl)); 891 LocalDeclMap.erase(VD); 892 setAddrOfLocalVar(VD, VDAddr); 893 return VDAddr; 894 } 895 return GetAddrOfLocalVar(VD); 896 }); 897 } 898 assert(IsRegistered && 899 "firstprivate var already registered as private"); 900 // Silence the warning about unused variable. 901 (void)IsRegistered; 902 } 903 ++IRef; 904 ++InitsRef; 905 } 906 } 907 return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); 908 } 909 910 void CodeGenFunction::EmitOMPPrivateClause( 911 const OMPExecutableDirective &D, 912 CodeGenFunction::OMPPrivateScope &PrivateScope) { 913 if (!HaveInsertPoint()) 914 return; 915 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 916 for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { 917 auto IRef = C->varlist_begin(); 918 for (const Expr *IInit : C->private_copies()) { 919 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 920 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 921 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 922 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { 923 // Emit private VarDecl with copy init. 924 EmitDecl(*VD); 925 return GetAddrOfLocalVar(VD); 926 }); 927 assert(IsRegistered && "private var already registered as private"); 928 // Silence the warning about unused variable. 929 (void)IsRegistered; 930 } 931 ++IRef; 932 } 933 } 934 } 935 936 bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { 937 if (!HaveInsertPoint()) 938 return false; 939 // threadprivate_var1 = master_threadprivate_var1; 940 // operator=(threadprivate_var2, master_threadprivate_var2); 941 // ... 942 // __kmpc_barrier(&loc, global_tid); 943 llvm::DenseSet<const VarDecl *> CopiedVars; 944 llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; 945 for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { 946 auto IRef = C->varlist_begin(); 947 auto ISrcRef = C->source_exprs().begin(); 948 auto IDestRef = C->destination_exprs().begin(); 949 for (const Expr *AssignOp : C->assignment_ops()) { 950 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 951 QualType Type = VD->getType(); 952 if (CopiedVars.insert(VD->getCanonicalDecl()).second) { 953 // Get the address of the master variable. If we are emitting code with 954 // TLS support, the address is passed from the master as field in the 955 // captured declaration. 956 Address MasterAddr = Address::invalid(); 957 if (getLangOpts().OpenMPUseTLS && 958 getContext().getTargetInfo().isTLSSupported()) { 959 assert(CapturedStmtInfo->lookup(VD) && 960 "Copyin threadprivates should have been captured!"); 961 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), true, 962 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 963 MasterAddr = EmitLValue(&DRE).getAddress(*this); 964 LocalDeclMap.erase(VD); 965 } else { 966 MasterAddr = 967 Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) 968 : CGM.GetAddrOfGlobal(VD), 969 getContext().getDeclAlign(VD)); 970 } 971 // Get the address of the threadprivate variable. 972 Address PrivateAddr = EmitLValue(*IRef).getAddress(*this); 973 if (CopiedVars.size() == 1) { 974 // At first check if current thread is a master thread. If it is, no 975 // need to copy data. 976 CopyBegin = createBasicBlock("copyin.not.master"); 977 CopyEnd = createBasicBlock("copyin.not.master.end"); 978 Builder.CreateCondBr( 979 Builder.CreateICmpNE( 980 Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), 981 Builder.CreatePtrToInt(PrivateAddr.getPointer(), 982 CGM.IntPtrTy)), 983 CopyBegin, CopyEnd); 984 EmitBlock(CopyBegin); 985 } 986 const auto *SrcVD = 987 cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); 988 const auto *DestVD = 989 cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 990 EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); 991 } 992 ++IRef; 993 ++ISrcRef; 994 ++IDestRef; 995 } 996 } 997 if (CopyEnd) { 998 // Exit out of copying procedure for non-master thread. 999 EmitBlock(CopyEnd, /*IsFinished=*/true); 1000 return true; 1001 } 1002 return false; 1003 } 1004 1005 bool CodeGenFunction::EmitOMPLastprivateClauseInit( 1006 const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { 1007 if (!HaveInsertPoint()) 1008 return false; 1009 bool HasAtLeastOneLastprivate = false; 1010 llvm::DenseSet<const VarDecl *> SIMDLCVs; 1011 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 1012 const auto *LoopDirective = cast<OMPLoopDirective>(&D); 1013 for (const Expr *C : LoopDirective->counters()) { 1014 SIMDLCVs.insert( 1015 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); 1016 } 1017 } 1018 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; 1019 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 1020 HasAtLeastOneLastprivate = true; 1021 if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && 1022 !getLangOpts().OpenMPSimd) 1023 break; 1024 const auto *IRef = C->varlist_begin(); 1025 const auto *IDestRef = C->destination_exprs().begin(); 1026 for (const Expr *IInit : C->private_copies()) { 1027 // Keep the address of the original variable for future update at the end 1028 // of the loop. 1029 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 1030 // Taskloops do not require additional initialization, it is done in 1031 // runtime support library. 1032 if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { 1033 const auto *DestVD = 1034 cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 1035 PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() { 1036 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), 1037 /*RefersToEnclosingVariableOrCapture=*/ 1038 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1039 (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); 1040 return EmitLValue(&DRE).getAddress(*this); 1041 }); 1042 // Check if the variable is also a firstprivate: in this case IInit is 1043 // not generated. Initialization of this variable will happen in codegen 1044 // for 'firstprivate' clause. 1045 if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { 1046 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); 1047 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD, C, 1048 OrigVD]() { 1049 if (C->getKind() == OMPC_LASTPRIVATE_conditional) { 1050 Address VDAddr = 1051 CGM.getOpenMPRuntime().emitLastprivateConditionalInit(*this, 1052 OrigVD); 1053 setAddrOfLocalVar(VD, VDAddr); 1054 return VDAddr; 1055 } 1056 // Emit private VarDecl with copy init. 1057 EmitDecl(*VD); 1058 return GetAddrOfLocalVar(VD); 1059 }); 1060 assert(IsRegistered && 1061 "lastprivate var already registered as private"); 1062 (void)IsRegistered; 1063 } 1064 } 1065 ++IRef; 1066 ++IDestRef; 1067 } 1068 } 1069 return HasAtLeastOneLastprivate; 1070 } 1071 1072 void CodeGenFunction::EmitOMPLastprivateClauseFinal( 1073 const OMPExecutableDirective &D, bool NoFinals, 1074 llvm::Value *IsLastIterCond) { 1075 if (!HaveInsertPoint()) 1076 return; 1077 // Emit following code: 1078 // if (<IsLastIterCond>) { 1079 // orig_var1 = private_orig_var1; 1080 // ... 1081 // orig_varn = private_orig_varn; 1082 // } 1083 llvm::BasicBlock *ThenBB = nullptr; 1084 llvm::BasicBlock *DoneBB = nullptr; 1085 if (IsLastIterCond) { 1086 // Emit implicit barrier if at least one lastprivate conditional is found 1087 // and this is not a simd mode. 1088 if (!getLangOpts().OpenMPSimd && 1089 llvm::any_of(D.getClausesOfKind<OMPLastprivateClause>(), 1090 [](const OMPLastprivateClause *C) { 1091 return C->getKind() == OMPC_LASTPRIVATE_conditional; 1092 })) { 1093 CGM.getOpenMPRuntime().emitBarrierCall(*this, D.getBeginLoc(), 1094 OMPD_unknown, 1095 /*EmitChecks=*/false, 1096 /*ForceSimpleCall=*/true); 1097 } 1098 ThenBB = createBasicBlock(".omp.lastprivate.then"); 1099 DoneBB = createBasicBlock(".omp.lastprivate.done"); 1100 Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); 1101 EmitBlock(ThenBB); 1102 } 1103 llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; 1104 llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; 1105 if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { 1106 auto IC = LoopDirective->counters().begin(); 1107 for (const Expr *F : LoopDirective->finals()) { 1108 const auto *D = 1109 cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); 1110 if (NoFinals) 1111 AlreadyEmittedVars.insert(D); 1112 else 1113 LoopCountersAndUpdates[D] = F; 1114 ++IC; 1115 } 1116 } 1117 for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { 1118 auto IRef = C->varlist_begin(); 1119 auto ISrcRef = C->source_exprs().begin(); 1120 auto IDestRef = C->destination_exprs().begin(); 1121 for (const Expr *AssignOp : C->assignment_ops()) { 1122 const auto *PrivateVD = 1123 cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 1124 QualType Type = PrivateVD->getType(); 1125 const auto *CanonicalVD = PrivateVD->getCanonicalDecl(); 1126 if (AlreadyEmittedVars.insert(CanonicalVD).second) { 1127 // If lastprivate variable is a loop control variable for loop-based 1128 // directive, update its value before copyin back to original 1129 // variable. 1130 if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) 1131 EmitIgnoredExpr(FinalExpr); 1132 const auto *SrcVD = 1133 cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); 1134 const auto *DestVD = 1135 cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); 1136 // Get the address of the private variable. 1137 Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); 1138 if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>()) 1139 PrivateAddr = 1140 Address(Builder.CreateLoad(PrivateAddr), 1141 CGM.getNaturalTypeAlignment(RefTy->getPointeeType())); 1142 // Store the last value to the private copy in the last iteration. 1143 if (C->getKind() == OMPC_LASTPRIVATE_conditional) 1144 CGM.getOpenMPRuntime().emitLastprivateConditionalFinalUpdate( 1145 *this, MakeAddrLValue(PrivateAddr, (*IRef)->getType()), PrivateVD, 1146 (*IRef)->getExprLoc()); 1147 // Get the address of the original variable. 1148 Address OriginalAddr = GetAddrOfLocalVar(DestVD); 1149 EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); 1150 } 1151 ++IRef; 1152 ++ISrcRef; 1153 ++IDestRef; 1154 } 1155 if (const Expr *PostUpdate = C->getPostUpdateExpr()) 1156 EmitIgnoredExpr(PostUpdate); 1157 } 1158 if (IsLastIterCond) 1159 EmitBlock(DoneBB, /*IsFinished=*/true); 1160 } 1161 1162 void CodeGenFunction::EmitOMPReductionClauseInit( 1163 const OMPExecutableDirective &D, 1164 CodeGenFunction::OMPPrivateScope &PrivateScope) { 1165 if (!HaveInsertPoint()) 1166 return; 1167 SmallVector<const Expr *, 4> Shareds; 1168 SmallVector<const Expr *, 4> Privates; 1169 SmallVector<const Expr *, 4> ReductionOps; 1170 SmallVector<const Expr *, 4> LHSs; 1171 SmallVector<const Expr *, 4> RHSs; 1172 OMPTaskDataTy Data; 1173 SmallVector<const Expr *, 4> TaskLHSs; 1174 SmallVector<const Expr *, 4> TaskRHSs; 1175 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1176 Shareds.append(C->varlist_begin(), C->varlist_end()); 1177 Privates.append(C->privates().begin(), C->privates().end()); 1178 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); 1179 LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 1180 RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 1181 if (C->getModifier() == OMPC_REDUCTION_task) { 1182 Data.ReductionVars.append(C->privates().begin(), C->privates().end()); 1183 Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end()); 1184 Data.ReductionCopies.append(C->privates().begin(), C->privates().end()); 1185 Data.ReductionOps.append(C->reduction_ops().begin(), 1186 C->reduction_ops().end()); 1187 TaskLHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 1188 TaskRHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 1189 } 1190 } 1191 ReductionCodeGen RedCG(Shareds, Shareds, Privates, ReductionOps); 1192 unsigned Count = 0; 1193 auto *ILHS = LHSs.begin(); 1194 auto *IRHS = RHSs.begin(); 1195 auto *IPriv = Privates.begin(); 1196 for (const Expr *IRef : Shareds) { 1197 const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); 1198 // Emit private VarDecl with reduction init. 1199 RedCG.emitSharedOrigLValue(*this, Count); 1200 RedCG.emitAggregateType(*this, Count); 1201 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); 1202 RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), 1203 RedCG.getSharedLValue(Count), 1204 [&Emission](CodeGenFunction &CGF) { 1205 CGF.EmitAutoVarInit(Emission); 1206 return true; 1207 }); 1208 EmitAutoVarCleanups(Emission); 1209 Address BaseAddr = RedCG.adjustPrivateAddress( 1210 *this, Count, Emission.getAllocatedAddress()); 1211 bool IsRegistered = PrivateScope.addPrivate( 1212 RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; }); 1213 assert(IsRegistered && "private var already registered as private"); 1214 // Silence the warning about unused variable. 1215 (void)IsRegistered; 1216 1217 const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); 1218 const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); 1219 QualType Type = PrivateVD->getType(); 1220 bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); 1221 if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { 1222 // Store the address of the original variable associated with the LHS 1223 // implicit variable. 1224 PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() { 1225 return RedCG.getSharedLValue(Count).getAddress(*this); 1226 }); 1227 PrivateScope.addPrivate( 1228 RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); }); 1229 } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || 1230 isa<ArraySubscriptExpr>(IRef)) { 1231 // Store the address of the original variable associated with the LHS 1232 // implicit variable. 1233 PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() { 1234 return RedCG.getSharedLValue(Count).getAddress(*this); 1235 }); 1236 PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() { 1237 return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), 1238 ConvertTypeForMem(RHSVD->getType()), 1239 "rhs.begin"); 1240 }); 1241 } else { 1242 QualType Type = PrivateVD->getType(); 1243 bool IsArray = getContext().getAsArrayType(Type) != nullptr; 1244 Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(*this); 1245 // Store the address of the original variable associated with the LHS 1246 // implicit variable. 1247 if (IsArray) { 1248 OriginalAddr = Builder.CreateElementBitCast( 1249 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); 1250 } 1251 PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; }); 1252 PrivateScope.addPrivate( 1253 RHSVD, [this, PrivateVD, RHSVD, IsArray]() { 1254 return IsArray 1255 ? Builder.CreateElementBitCast( 1256 GetAddrOfLocalVar(PrivateVD), 1257 ConvertTypeForMem(RHSVD->getType()), "rhs.begin") 1258 : GetAddrOfLocalVar(PrivateVD); 1259 }); 1260 } 1261 ++ILHS; 1262 ++IRHS; 1263 ++IPriv; 1264 ++Count; 1265 } 1266 if (!Data.ReductionVars.empty()) { 1267 Data.IsReductionWithTaskMod = true; 1268 Data.IsWorksharingReduction = 1269 isOpenMPWorksharingDirective(D.getDirectiveKind()); 1270 llvm::Value *ReductionDesc = CGM.getOpenMPRuntime().emitTaskReductionInit( 1271 *this, D.getBeginLoc(), TaskLHSs, TaskRHSs, Data); 1272 const Expr *TaskRedRef = nullptr; 1273 switch (D.getDirectiveKind()) { 1274 case OMPD_parallel: 1275 TaskRedRef = cast<OMPParallelDirective>(D).getTaskReductionRefExpr(); 1276 break; 1277 case OMPD_for: 1278 TaskRedRef = cast<OMPForDirective>(D).getTaskReductionRefExpr(); 1279 break; 1280 case OMPD_sections: 1281 TaskRedRef = cast<OMPSectionsDirective>(D).getTaskReductionRefExpr(); 1282 break; 1283 case OMPD_parallel_for: 1284 TaskRedRef = cast<OMPParallelForDirective>(D).getTaskReductionRefExpr(); 1285 break; 1286 case OMPD_parallel_master: 1287 TaskRedRef = 1288 cast<OMPParallelMasterDirective>(D).getTaskReductionRefExpr(); 1289 break; 1290 case OMPD_parallel_sections: 1291 TaskRedRef = 1292 cast<OMPParallelSectionsDirective>(D).getTaskReductionRefExpr(); 1293 break; 1294 case OMPD_target_parallel: 1295 TaskRedRef = 1296 cast<OMPTargetParallelDirective>(D).getTaskReductionRefExpr(); 1297 break; 1298 case OMPD_target_parallel_for: 1299 TaskRedRef = 1300 cast<OMPTargetParallelForDirective>(D).getTaskReductionRefExpr(); 1301 break; 1302 case OMPD_distribute_parallel_for: 1303 TaskRedRef = 1304 cast<OMPDistributeParallelForDirective>(D).getTaskReductionRefExpr(); 1305 break; 1306 case OMPD_teams_distribute_parallel_for: 1307 TaskRedRef = cast<OMPTeamsDistributeParallelForDirective>(D) 1308 .getTaskReductionRefExpr(); 1309 break; 1310 case OMPD_target_teams_distribute_parallel_for: 1311 TaskRedRef = cast<OMPTargetTeamsDistributeParallelForDirective>(D) 1312 .getTaskReductionRefExpr(); 1313 break; 1314 case OMPD_simd: 1315 case OMPD_for_simd: 1316 case OMPD_section: 1317 case OMPD_single: 1318 case OMPD_master: 1319 case OMPD_critical: 1320 case OMPD_parallel_for_simd: 1321 case OMPD_task: 1322 case OMPD_taskyield: 1323 case OMPD_barrier: 1324 case OMPD_taskwait: 1325 case OMPD_taskgroup: 1326 case OMPD_flush: 1327 case OMPD_depobj: 1328 case OMPD_scan: 1329 case OMPD_ordered: 1330 case OMPD_atomic: 1331 case OMPD_teams: 1332 case OMPD_target: 1333 case OMPD_cancellation_point: 1334 case OMPD_cancel: 1335 case OMPD_target_data: 1336 case OMPD_target_enter_data: 1337 case OMPD_target_exit_data: 1338 case OMPD_taskloop: 1339 case OMPD_taskloop_simd: 1340 case OMPD_master_taskloop: 1341 case OMPD_master_taskloop_simd: 1342 case OMPD_parallel_master_taskloop: 1343 case OMPD_parallel_master_taskloop_simd: 1344 case OMPD_distribute: 1345 case OMPD_target_update: 1346 case OMPD_distribute_parallel_for_simd: 1347 case OMPD_distribute_simd: 1348 case OMPD_target_parallel_for_simd: 1349 case OMPD_target_simd: 1350 case OMPD_teams_distribute: 1351 case OMPD_teams_distribute_simd: 1352 case OMPD_teams_distribute_parallel_for_simd: 1353 case OMPD_target_teams: 1354 case OMPD_target_teams_distribute: 1355 case OMPD_target_teams_distribute_parallel_for_simd: 1356 case OMPD_target_teams_distribute_simd: 1357 case OMPD_declare_target: 1358 case OMPD_end_declare_target: 1359 case OMPD_threadprivate: 1360 case OMPD_allocate: 1361 case OMPD_declare_reduction: 1362 case OMPD_declare_mapper: 1363 case OMPD_declare_simd: 1364 case OMPD_requires: 1365 case OMPD_declare_variant: 1366 case OMPD_begin_declare_variant: 1367 case OMPD_end_declare_variant: 1368 case OMPD_unknown: 1369 llvm_unreachable("Enexpected directive with task reductions."); 1370 } 1371 1372 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(TaskRedRef)->getDecl()); 1373 EmitVarDecl(*VD); 1374 EmitStoreOfScalar(ReductionDesc, GetAddrOfLocalVar(VD), 1375 /*Volatile=*/false, TaskRedRef->getType()); 1376 } 1377 } 1378 1379 void CodeGenFunction::EmitOMPReductionClauseFinal( 1380 const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { 1381 if (!HaveInsertPoint()) 1382 return; 1383 llvm::SmallVector<const Expr *, 8> Privates; 1384 llvm::SmallVector<const Expr *, 8> LHSExprs; 1385 llvm::SmallVector<const Expr *, 8> RHSExprs; 1386 llvm::SmallVector<const Expr *, 8> ReductionOps; 1387 bool HasAtLeastOneReduction = false; 1388 bool IsReductionWithTaskMod = false; 1389 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1390 HasAtLeastOneReduction = true; 1391 Privates.append(C->privates().begin(), C->privates().end()); 1392 LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 1393 RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 1394 ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); 1395 IsReductionWithTaskMod = 1396 IsReductionWithTaskMod || C->getModifier() == OMPC_REDUCTION_task; 1397 } 1398 if (HasAtLeastOneReduction) { 1399 if (IsReductionWithTaskMod) { 1400 CGM.getOpenMPRuntime().emitTaskReductionFini( 1401 *this, D.getBeginLoc(), 1402 isOpenMPWorksharingDirective(D.getDirectiveKind())); 1403 } 1404 bool WithNowait = D.getSingleClause<OMPNowaitClause>() || 1405 isOpenMPParallelDirective(D.getDirectiveKind()) || 1406 ReductionKind == OMPD_simd; 1407 bool SimpleReduction = ReductionKind == OMPD_simd; 1408 // Emit nowait reduction if nowait clause is present or directive is a 1409 // parallel directive (it always has implicit barrier). 1410 CGM.getOpenMPRuntime().emitReduction( 1411 *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps, 1412 {WithNowait, SimpleReduction, ReductionKind}); 1413 } 1414 } 1415 1416 static void emitPostUpdateForReductionClause( 1417 CodeGenFunction &CGF, const OMPExecutableDirective &D, 1418 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { 1419 if (!CGF.HaveInsertPoint()) 1420 return; 1421 llvm::BasicBlock *DoneBB = nullptr; 1422 for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { 1423 if (const Expr *PostUpdate = C->getPostUpdateExpr()) { 1424 if (!DoneBB) { 1425 if (llvm::Value *Cond = CondGen(CGF)) { 1426 // If the first post-update expression is found, emit conditional 1427 // block if it was requested. 1428 llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); 1429 DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); 1430 CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1431 CGF.EmitBlock(ThenBB); 1432 } 1433 } 1434 CGF.EmitIgnoredExpr(PostUpdate); 1435 } 1436 } 1437 if (DoneBB) 1438 CGF.EmitBlock(DoneBB, /*IsFinished=*/true); 1439 } 1440 1441 namespace { 1442 /// Codegen lambda for appending distribute lower and upper bounds to outlined 1443 /// parallel function. This is necessary for combined constructs such as 1444 /// 'distribute parallel for' 1445 typedef llvm::function_ref<void(CodeGenFunction &, 1446 const OMPExecutableDirective &, 1447 llvm::SmallVectorImpl<llvm::Value *> &)> 1448 CodeGenBoundParametersTy; 1449 } // anonymous namespace 1450 1451 static void 1452 checkForLastprivateConditionalUpdate(CodeGenFunction &CGF, 1453 const OMPExecutableDirective &S) { 1454 if (CGF.getLangOpts().OpenMP < 50) 1455 return; 1456 llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> PrivateDecls; 1457 for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { 1458 for (const Expr *Ref : C->varlists()) { 1459 if (!Ref->getType()->isScalarType()) 1460 continue; 1461 const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); 1462 if (!DRE) 1463 continue; 1464 PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); 1465 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); 1466 } 1467 } 1468 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { 1469 for (const Expr *Ref : C->varlists()) { 1470 if (!Ref->getType()->isScalarType()) 1471 continue; 1472 const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); 1473 if (!DRE) 1474 continue; 1475 PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); 1476 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); 1477 } 1478 } 1479 for (const auto *C : S.getClausesOfKind<OMPLinearClause>()) { 1480 for (const Expr *Ref : C->varlists()) { 1481 if (!Ref->getType()->isScalarType()) 1482 continue; 1483 const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); 1484 if (!DRE) 1485 continue; 1486 PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); 1487 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); 1488 } 1489 } 1490 // Privates should ne analyzed since they are not captured at all. 1491 // Task reductions may be skipped - tasks are ignored. 1492 // Firstprivates do not return value but may be passed by reference - no need 1493 // to check for updated lastprivate conditional. 1494 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { 1495 for (const Expr *Ref : C->varlists()) { 1496 if (!Ref->getType()->isScalarType()) 1497 continue; 1498 const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); 1499 if (!DRE) 1500 continue; 1501 PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); 1502 } 1503 } 1504 CGF.CGM.getOpenMPRuntime().checkAndEmitSharedLastprivateConditional( 1505 CGF, S, PrivateDecls); 1506 } 1507 1508 static void emitCommonOMPParallelDirective( 1509 CodeGenFunction &CGF, const OMPExecutableDirective &S, 1510 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, 1511 const CodeGenBoundParametersTy &CodeGenBoundParameters) { 1512 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); 1513 llvm::Function *OutlinedFn = 1514 CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( 1515 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 1516 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { 1517 CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); 1518 llvm::Value *NumThreads = 1519 CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), 1520 /*IgnoreResultAssign=*/true); 1521 CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( 1522 CGF, NumThreads, NumThreadsClause->getBeginLoc()); 1523 } 1524 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { 1525 CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); 1526 CGF.CGM.getOpenMPRuntime().emitProcBindClause( 1527 CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc()); 1528 } 1529 const Expr *IfCond = nullptr; 1530 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 1531 if (C->getNameModifier() == OMPD_unknown || 1532 C->getNameModifier() == OMPD_parallel) { 1533 IfCond = C->getCondition(); 1534 break; 1535 } 1536 } 1537 1538 OMPParallelScope Scope(CGF, S); 1539 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 1540 // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk 1541 // lower and upper bounds with the pragma 'for' chunking mechanism. 1542 // The following lambda takes care of appending the lower and upper bound 1543 // parameters when necessary 1544 CodeGenBoundParameters(CGF, S, CapturedVars); 1545 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 1546 CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn, 1547 CapturedVars, IfCond); 1548 } 1549 1550 static void emitEmptyBoundParameters(CodeGenFunction &, 1551 const OMPExecutableDirective &, 1552 llvm::SmallVectorImpl<llvm::Value *> &) {} 1553 1554 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { 1555 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { 1556 // Check if we have any if clause associated with the directive. 1557 llvm::Value *IfCond = nullptr; 1558 if (const auto *C = S.getSingleClause<OMPIfClause>()) 1559 IfCond = EmitScalarExpr(C->getCondition(), 1560 /*IgnoreResultAssign=*/true); 1561 1562 llvm::Value *NumThreads = nullptr; 1563 if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) 1564 NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads(), 1565 /*IgnoreResultAssign=*/true); 1566 1567 ProcBindKind ProcBind = OMP_PROC_BIND_default; 1568 if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) 1569 ProcBind = ProcBindClause->getProcBindKind(); 1570 1571 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 1572 1573 // The cleanup callback that finalizes all variabels at the given location, 1574 // thus calls destructors etc. 1575 auto FiniCB = [this](InsertPointTy IP) { 1576 OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); 1577 }; 1578 1579 // Privatization callback that performs appropriate action for 1580 // shared/private/firstprivate/lastprivate/copyin/... variables. 1581 // 1582 // TODO: This defaults to shared right now. 1583 auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, 1584 llvm::Value &Val, llvm::Value *&ReplVal) { 1585 // The next line is appropriate only for variables (Val) with the 1586 // data-sharing attribute "shared". 1587 ReplVal = &Val; 1588 1589 return CodeGenIP; 1590 }; 1591 1592 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); 1593 const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt(); 1594 1595 auto BodyGenCB = [ParallelRegionBodyStmt, 1596 this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, 1597 llvm::BasicBlock &ContinuationBB) { 1598 OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP, 1599 ContinuationBB); 1600 OMPBuilderCBHelpers::EmitOMPRegionBody(*this, ParallelRegionBodyStmt, 1601 CodeGenIP, ContinuationBB); 1602 }; 1603 1604 CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); 1605 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); 1606 Builder.restoreIP(OMPBuilder->CreateParallel(Builder, BodyGenCB, PrivCB, 1607 FiniCB, IfCond, NumThreads, 1608 ProcBind, S.hasCancel())); 1609 return; 1610 } 1611 1612 // Emit parallel region as a standalone region. 1613 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 1614 Action.Enter(CGF); 1615 OMPPrivateScope PrivateScope(CGF); 1616 bool Copyins = CGF.EmitOMPCopyinClause(S); 1617 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 1618 if (Copyins) { 1619 // Emit implicit barrier to synchronize threads and avoid data races on 1620 // propagation master's thread values of threadprivate variables to local 1621 // instances of that variables of all other implicit threads. 1622 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 1623 CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, 1624 /*ForceSimpleCall=*/true); 1625 } 1626 CGF.EmitOMPPrivateClause(S, PrivateScope); 1627 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 1628 (void)PrivateScope.Privatize(); 1629 CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); 1630 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); 1631 }; 1632 { 1633 auto LPCRegion = 1634 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 1635 emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, 1636 emitEmptyBoundParameters); 1637 emitPostUpdateForReductionClause(*this, S, 1638 [](CodeGenFunction &) { return nullptr; }); 1639 } 1640 // Check for outer lastprivate conditional update. 1641 checkForLastprivateConditionalUpdate(*this, S); 1642 } 1643 1644 static void emitBody(CodeGenFunction &CGF, const Stmt *S, const Stmt *NextLoop, 1645 int MaxLevel, int Level = 0) { 1646 assert(Level < MaxLevel && "Too deep lookup during loop body codegen."); 1647 const Stmt *SimplifiedS = S->IgnoreContainers(); 1648 if (const auto *CS = dyn_cast<CompoundStmt>(SimplifiedS)) { 1649 PrettyStackTraceLoc CrashInfo( 1650 CGF.getContext().getSourceManager(), CS->getLBracLoc(), 1651 "LLVM IR generation of compound statement ('{}')"); 1652 1653 // Keep track of the current cleanup stack depth, including debug scopes. 1654 CodeGenFunction::LexicalScope Scope(CGF, S->getSourceRange()); 1655 for (const Stmt *CurStmt : CS->body()) 1656 emitBody(CGF, CurStmt, NextLoop, MaxLevel, Level); 1657 return; 1658 } 1659 if (SimplifiedS == NextLoop) { 1660 if (const auto *For = dyn_cast<ForStmt>(SimplifiedS)) { 1661 S = For->getBody(); 1662 } else { 1663 assert(isa<CXXForRangeStmt>(SimplifiedS) && 1664 "Expected canonical for loop or range-based for loop."); 1665 const auto *CXXFor = cast<CXXForRangeStmt>(SimplifiedS); 1666 CGF.EmitStmt(CXXFor->getLoopVarStmt()); 1667 S = CXXFor->getBody(); 1668 } 1669 if (Level + 1 < MaxLevel) { 1670 NextLoop = OMPLoopDirective::tryToFindNextInnerLoop( 1671 S, /*TryImperfectlyNestedLoops=*/true); 1672 emitBody(CGF, S, NextLoop, MaxLevel, Level + 1); 1673 return; 1674 } 1675 } 1676 CGF.EmitStmt(S); 1677 } 1678 1679 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, 1680 JumpDest LoopExit) { 1681 RunCleanupsScope BodyScope(*this); 1682 // Update counters values on current iteration. 1683 for (const Expr *UE : D.updates()) 1684 EmitIgnoredExpr(UE); 1685 // Update the linear variables. 1686 // In distribute directives only loop counters may be marked as linear, no 1687 // need to generate the code for them. 1688 if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { 1689 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1690 for (const Expr *UE : C->updates()) 1691 EmitIgnoredExpr(UE); 1692 } 1693 } 1694 1695 // On a continue in the body, jump to the end. 1696 JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue"); 1697 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1698 for (const Expr *E : D.finals_conditions()) { 1699 if (!E) 1700 continue; 1701 // Check that loop counter in non-rectangular nest fits into the iteration 1702 // space. 1703 llvm::BasicBlock *NextBB = createBasicBlock("omp.body.next"); 1704 EmitBranchOnBoolExpr(E, NextBB, Continue.getBlock(), 1705 getProfileCount(D.getBody())); 1706 EmitBlock(NextBB); 1707 } 1708 // Emit loop variables for C++ range loops. 1709 const Stmt *Body = 1710 D.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers(); 1711 // Emit loop body. 1712 emitBody(*this, Body, 1713 OMPLoopDirective::tryToFindNextInnerLoop( 1714 Body, /*TryImperfectlyNestedLoops=*/true), 1715 D.getCollapsedNumber()); 1716 1717 // The end (updates/cleanups). 1718 EmitBlock(Continue.getBlock()); 1719 BreakContinueStack.pop_back(); 1720 } 1721 1722 void CodeGenFunction::EmitOMPInnerLoop( 1723 const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, 1724 const Expr *IncExpr, 1725 const llvm::function_ref<void(CodeGenFunction &)> BodyGen, 1726 const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) { 1727 auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); 1728 1729 // Start the loop with a block that tests the condition. 1730 auto CondBlock = createBasicBlock("omp.inner.for.cond"); 1731 EmitBlock(CondBlock); 1732 const SourceRange R = S.getSourceRange(); 1733 1734 // If attributes are attached, push to the basic block with them. 1735 const auto &OMPED = cast<OMPExecutableDirective>(S); 1736 const CapturedStmt *ICS = OMPED.getInnermostCapturedStmt(); 1737 const Stmt *SS = ICS->getCapturedStmt(); 1738 const AttributedStmt *AS = dyn_cast_or_null<AttributedStmt>(SS); 1739 if (AS) 1740 LoopStack.push(CondBlock, CGM.getContext(), CGM.getCodeGenOpts(), 1741 AS->getAttrs(), SourceLocToDebugLoc(R.getBegin()), 1742 SourceLocToDebugLoc(R.getEnd())); 1743 else 1744 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), 1745 SourceLocToDebugLoc(R.getEnd())); 1746 1747 // If there are any cleanups between here and the loop-exit scope, 1748 // create a block to stage a loop exit along. 1749 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 1750 if (RequiresCleanup) 1751 ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); 1752 1753 llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body"); 1754 1755 // Emit condition. 1756 EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); 1757 if (ExitBlock != LoopExit.getBlock()) { 1758 EmitBlock(ExitBlock); 1759 EmitBranchThroughCleanup(LoopExit); 1760 } 1761 1762 EmitBlock(LoopBody); 1763 incrementProfileCounter(&S); 1764 1765 // Create a block for the increment. 1766 JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); 1767 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 1768 1769 BodyGen(*this); 1770 1771 // Emit "IV = IV + 1" and a back-edge to the condition block. 1772 EmitBlock(Continue.getBlock()); 1773 EmitIgnoredExpr(IncExpr); 1774 PostIncGen(*this); 1775 BreakContinueStack.pop_back(); 1776 EmitBranch(CondBlock); 1777 LoopStack.pop(); 1778 // Emit the fall-through block. 1779 EmitBlock(LoopExit.getBlock()); 1780 } 1781 1782 bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { 1783 if (!HaveInsertPoint()) 1784 return false; 1785 // Emit inits for the linear variables. 1786 bool HasLinears = false; 1787 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1788 for (const Expr *Init : C->inits()) { 1789 HasLinears = true; 1790 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); 1791 if (const auto *Ref = 1792 dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { 1793 AutoVarEmission Emission = EmitAutoVarAlloca(*VD); 1794 const auto *OrigVD = cast<VarDecl>(Ref->getDecl()); 1795 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), 1796 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1797 VD->getInit()->getType(), VK_LValue, 1798 VD->getInit()->getExprLoc()); 1799 EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), 1800 VD->getType()), 1801 /*capturedByInit=*/false); 1802 EmitAutoVarCleanups(Emission); 1803 } else { 1804 EmitVarDecl(*VD); 1805 } 1806 } 1807 // Emit the linear steps for the linear clauses. 1808 // If a step is not constant, it is pre-calculated before the loop. 1809 if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep())) 1810 if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) { 1811 EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); 1812 // Emit calculation of the linear step. 1813 EmitIgnoredExpr(CS); 1814 } 1815 } 1816 return HasLinears; 1817 } 1818 1819 void CodeGenFunction::EmitOMPLinearClauseFinal( 1820 const OMPLoopDirective &D, 1821 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { 1822 if (!HaveInsertPoint()) 1823 return; 1824 llvm::BasicBlock *DoneBB = nullptr; 1825 // Emit the final values of the linear variables. 1826 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1827 auto IC = C->varlist_begin(); 1828 for (const Expr *F : C->finals()) { 1829 if (!DoneBB) { 1830 if (llvm::Value *Cond = CondGen(*this)) { 1831 // If the first post-update expression is found, emit conditional 1832 // block if it was requested. 1833 llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu"); 1834 DoneBB = createBasicBlock(".omp.linear.pu.done"); 1835 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 1836 EmitBlock(ThenBB); 1837 } 1838 } 1839 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); 1840 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), 1841 CapturedStmtInfo->lookup(OrigVD) != nullptr, 1842 (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); 1843 Address OrigAddr = EmitLValue(&DRE).getAddress(*this); 1844 CodeGenFunction::OMPPrivateScope VarScope(*this); 1845 VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); 1846 (void)VarScope.Privatize(); 1847 EmitIgnoredExpr(F); 1848 ++IC; 1849 } 1850 if (const Expr *PostUpdate = C->getPostUpdateExpr()) 1851 EmitIgnoredExpr(PostUpdate); 1852 } 1853 if (DoneBB) 1854 EmitBlock(DoneBB, /*IsFinished=*/true); 1855 } 1856 1857 static void emitAlignedClause(CodeGenFunction &CGF, 1858 const OMPExecutableDirective &D) { 1859 if (!CGF.HaveInsertPoint()) 1860 return; 1861 for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { 1862 llvm::APInt ClauseAlignment(64, 0); 1863 if (const Expr *AlignmentExpr = Clause->getAlignment()) { 1864 auto *AlignmentCI = 1865 cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); 1866 ClauseAlignment = AlignmentCI->getValue(); 1867 } 1868 for (const Expr *E : Clause->varlists()) { 1869 llvm::APInt Alignment(ClauseAlignment); 1870 if (Alignment == 0) { 1871 // OpenMP [2.8.1, Description] 1872 // If no optional parameter is specified, implementation-defined default 1873 // alignments for SIMD instructions on the target platforms are assumed. 1874 Alignment = 1875 CGF.getContext() 1876 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( 1877 E->getType()->getPointeeType())) 1878 .getQuantity(); 1879 } 1880 assert((Alignment == 0 || Alignment.isPowerOf2()) && 1881 "alignment is not power of 2"); 1882 if (Alignment != 0) { 1883 llvm::Value *PtrValue = CGF.EmitScalarExpr(E); 1884 CGF.emitAlignmentAssumption( 1885 PtrValue, E, /*No second loc needed*/ SourceLocation(), 1886 llvm::ConstantInt::get(CGF.getLLVMContext(), Alignment)); 1887 } 1888 } 1889 } 1890 } 1891 1892 void CodeGenFunction::EmitOMPPrivateLoopCounters( 1893 const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { 1894 if (!HaveInsertPoint()) 1895 return; 1896 auto I = S.private_counters().begin(); 1897 for (const Expr *E : S.counters()) { 1898 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1899 const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); 1900 // Emit var without initialization. 1901 AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD); 1902 EmitAutoVarCleanups(VarEmission); 1903 LocalDeclMap.erase(PrivateVD); 1904 (void)LoopScope.addPrivate(VD, [&VarEmission]() { 1905 return VarEmission.getAllocatedAddress(); 1906 }); 1907 if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || 1908 VD->hasGlobalStorage()) { 1909 (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { 1910 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), 1911 LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), 1912 E->getType(), VK_LValue, E->getExprLoc()); 1913 return EmitLValue(&DRE).getAddress(*this); 1914 }); 1915 } else { 1916 (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { 1917 return VarEmission.getAllocatedAddress(); 1918 }); 1919 } 1920 ++I; 1921 } 1922 // Privatize extra loop counters used in loops for ordered(n) clauses. 1923 for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) { 1924 if (!C->getNumForLoops()) 1925 continue; 1926 for (unsigned I = S.getCollapsedNumber(), 1927 E = C->getLoopNumIterations().size(); 1928 I < E; ++I) { 1929 const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I)); 1930 const auto *VD = cast<VarDecl>(DRE->getDecl()); 1931 // Override only those variables that can be captured to avoid re-emission 1932 // of the variables declared within the loops. 1933 if (DRE->refersToEnclosingVariableOrCapture()) { 1934 (void)LoopScope.addPrivate(VD, [this, DRE, VD]() { 1935 return CreateMemTemp(DRE->getType(), VD->getName()); 1936 }); 1937 } 1938 } 1939 } 1940 } 1941 1942 static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, 1943 const Expr *Cond, llvm::BasicBlock *TrueBlock, 1944 llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { 1945 if (!CGF.HaveInsertPoint()) 1946 return; 1947 { 1948 CodeGenFunction::OMPPrivateScope PreCondScope(CGF); 1949 CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); 1950 (void)PreCondScope.Privatize(); 1951 // Get initial values of real counters. 1952 for (const Expr *I : S.inits()) { 1953 CGF.EmitIgnoredExpr(I); 1954 } 1955 } 1956 // Create temp loop control variables with their init values to support 1957 // non-rectangular loops. 1958 CodeGenFunction::OMPMapVars PreCondVars; 1959 for (const Expr * E: S.dependent_counters()) { 1960 if (!E) 1961 continue; 1962 assert(!E->getType().getNonReferenceType()->isRecordType() && 1963 "dependent counter must not be an iterator."); 1964 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1965 Address CounterAddr = 1966 CGF.CreateMemTemp(VD->getType().getNonReferenceType()); 1967 (void)PreCondVars.setVarAddr(CGF, VD, CounterAddr); 1968 } 1969 (void)PreCondVars.apply(CGF); 1970 for (const Expr *E : S.dependent_inits()) { 1971 if (!E) 1972 continue; 1973 CGF.EmitIgnoredExpr(E); 1974 } 1975 // Check that loop is executed at least one time. 1976 CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); 1977 PreCondVars.restore(CGF); 1978 } 1979 1980 void CodeGenFunction::EmitOMPLinearClause( 1981 const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { 1982 if (!HaveInsertPoint()) 1983 return; 1984 llvm::DenseSet<const VarDecl *> SIMDLCVs; 1985 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 1986 const auto *LoopDirective = cast<OMPLoopDirective>(&D); 1987 for (const Expr *C : LoopDirective->counters()) { 1988 SIMDLCVs.insert( 1989 cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); 1990 } 1991 } 1992 for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { 1993 auto CurPrivate = C->privates().begin(); 1994 for (const Expr *E : C->varlists()) { 1995 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 1996 const auto *PrivateVD = 1997 cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); 1998 if (!SIMDLCVs.count(VD->getCanonicalDecl())) { 1999 bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() { 2000 // Emit private VarDecl with copy init. 2001 EmitVarDecl(*PrivateVD); 2002 return GetAddrOfLocalVar(PrivateVD); 2003 }); 2004 assert(IsRegistered && "linear var already registered as private"); 2005 // Silence the warning about unused variable. 2006 (void)IsRegistered; 2007 } else { 2008 EmitVarDecl(*PrivateVD); 2009 } 2010 ++CurPrivate; 2011 } 2012 } 2013 } 2014 2015 static void emitSimdlenSafelenClause(CodeGenFunction &CGF, 2016 const OMPExecutableDirective &D, 2017 bool IsMonotonic) { 2018 if (!CGF.HaveInsertPoint()) 2019 return; 2020 if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { 2021 RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), 2022 /*ignoreResult=*/true); 2023 auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 2024 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 2025 // In presence of finite 'safelen', it may be unsafe to mark all 2026 // the memory instructions parallel, because loop-carried 2027 // dependences of 'safelen' iterations are possible. 2028 if (!IsMonotonic) 2029 CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); 2030 } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { 2031 RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), 2032 /*ignoreResult=*/true); 2033 auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); 2034 CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); 2035 // In presence of finite 'safelen', it may be unsafe to mark all 2036 // the memory instructions parallel, because loop-carried 2037 // dependences of 'safelen' iterations are possible. 2038 CGF.LoopStack.setParallel(/*Enable=*/false); 2039 } 2040 } 2041 2042 void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, 2043 bool IsMonotonic) { 2044 // Walk clauses and process safelen/lastprivate. 2045 LoopStack.setParallel(!IsMonotonic); 2046 LoopStack.setVectorizeEnable(); 2047 emitSimdlenSafelenClause(*this, D, IsMonotonic); 2048 if (const auto *C = D.getSingleClause<OMPOrderClause>()) 2049 if (C->getKind() == OMPC_ORDER_concurrent) 2050 LoopStack.setParallel(/*Enable=*/true); 2051 } 2052 2053 void CodeGenFunction::EmitOMPSimdFinal( 2054 const OMPLoopDirective &D, 2055 const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { 2056 if (!HaveInsertPoint()) 2057 return; 2058 llvm::BasicBlock *DoneBB = nullptr; 2059 auto IC = D.counters().begin(); 2060 auto IPC = D.private_counters().begin(); 2061 for (const Expr *F : D.finals()) { 2062 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); 2063 const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); 2064 const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); 2065 if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || 2066 OrigVD->hasGlobalStorage() || CED) { 2067 if (!DoneBB) { 2068 if (llvm::Value *Cond = CondGen(*this)) { 2069 // If the first post-update expression is found, emit conditional 2070 // block if it was requested. 2071 llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then"); 2072 DoneBB = createBasicBlock(".omp.final.done"); 2073 Builder.CreateCondBr(Cond, ThenBB, DoneBB); 2074 EmitBlock(ThenBB); 2075 } 2076 } 2077 Address OrigAddr = Address::invalid(); 2078 if (CED) { 2079 OrigAddr = 2080 EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(*this); 2081 } else { 2082 DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(PrivateVD), 2083 /*RefersToEnclosingVariableOrCapture=*/false, 2084 (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); 2085 OrigAddr = EmitLValue(&DRE).getAddress(*this); 2086 } 2087 OMPPrivateScope VarScope(*this); 2088 VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); 2089 (void)VarScope.Privatize(); 2090 EmitIgnoredExpr(F); 2091 } 2092 ++IC; 2093 ++IPC; 2094 } 2095 if (DoneBB) 2096 EmitBlock(DoneBB, /*IsFinished=*/true); 2097 } 2098 2099 static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, 2100 const OMPLoopDirective &S, 2101 CodeGenFunction::JumpDest LoopExit) { 2102 CGF.EmitOMPLoopBody(S, LoopExit); 2103 CGF.EmitStopPoint(&S); 2104 } 2105 2106 /// Emit a helper variable and return corresponding lvalue. 2107 static LValue EmitOMPHelperVar(CodeGenFunction &CGF, 2108 const DeclRefExpr *Helper) { 2109 auto VDecl = cast<VarDecl>(Helper->getDecl()); 2110 CGF.EmitVarDecl(*VDecl); 2111 return CGF.EmitLValue(Helper); 2112 } 2113 2114 static void emitCommonSimdLoop(CodeGenFunction &CGF, const OMPLoopDirective &S, 2115 const RegionCodeGenTy &SimdInitGen, 2116 const RegionCodeGenTy &BodyCodeGen) { 2117 auto &&ThenGen = [&S, &SimdInitGen, &BodyCodeGen](CodeGenFunction &CGF, 2118 PrePostActionTy &) { 2119 CGOpenMPRuntime::NontemporalDeclsRAII NontemporalsRegion(CGF.CGM, S); 2120 CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF); 2121 SimdInitGen(CGF); 2122 2123 BodyCodeGen(CGF); 2124 }; 2125 auto &&ElseGen = [&BodyCodeGen](CodeGenFunction &CGF, PrePostActionTy &) { 2126 CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF); 2127 CGF.LoopStack.setVectorizeEnable(/*Enable=*/false); 2128 2129 BodyCodeGen(CGF); 2130 }; 2131 const Expr *IfCond = nullptr; 2132 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 2133 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 2134 if (CGF.getLangOpts().OpenMP >= 50 && 2135 (C->getNameModifier() == OMPD_unknown || 2136 C->getNameModifier() == OMPD_simd)) { 2137 IfCond = C->getCondition(); 2138 break; 2139 } 2140 } 2141 } 2142 if (IfCond) { 2143 CGF.CGM.getOpenMPRuntime().emitIfClause(CGF, IfCond, ThenGen, ElseGen); 2144 } else { 2145 RegionCodeGenTy ThenRCG(ThenGen); 2146 ThenRCG(CGF); 2147 } 2148 } 2149 2150 static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, 2151 PrePostActionTy &Action) { 2152 Action.Enter(CGF); 2153 assert(isOpenMPSimdDirective(S.getDirectiveKind()) && 2154 "Expected simd directive"); 2155 OMPLoopScope PreInitScope(CGF, S); 2156 // if (PreCond) { 2157 // for (IV in 0..LastIteration) BODY; 2158 // <Final counter/linear vars updates>; 2159 // } 2160 // 2161 if (isOpenMPDistributeDirective(S.getDirectiveKind()) || 2162 isOpenMPWorksharingDirective(S.getDirectiveKind()) || 2163 isOpenMPTaskLoopDirective(S.getDirectiveKind())) { 2164 (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); 2165 (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); 2166 } 2167 2168 // Emit: if (PreCond) - begin. 2169 // If the condition constant folds and can be elided, avoid emitting the 2170 // whole loop. 2171 bool CondConstant; 2172 llvm::BasicBlock *ContBlock = nullptr; 2173 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 2174 if (!CondConstant) 2175 return; 2176 } else { 2177 llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then"); 2178 ContBlock = CGF.createBasicBlock("simd.if.end"); 2179 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 2180 CGF.getProfileCount(&S)); 2181 CGF.EmitBlock(ThenBlock); 2182 CGF.incrementProfileCounter(&S); 2183 } 2184 2185 // Emit the loop iteration variable. 2186 const Expr *IVExpr = S.getIterationVariable(); 2187 const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 2188 CGF.EmitVarDecl(*IVDecl); 2189 CGF.EmitIgnoredExpr(S.getInit()); 2190 2191 // Emit the iterations count variable. 2192 // If it is not a variable, Sema decided to calculate iterations count on 2193 // each iteration (e.g., it is foldable into a constant). 2194 if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 2195 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 2196 // Emit calculation of the iterations count. 2197 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 2198 } 2199 2200 emitAlignedClause(CGF, S); 2201 (void)CGF.EmitOMPLinearClauseInit(S); 2202 { 2203 CodeGenFunction::OMPPrivateScope LoopScope(CGF); 2204 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 2205 CGF.EmitOMPLinearClause(S, LoopScope); 2206 CGF.EmitOMPPrivateClause(S, LoopScope); 2207 CGF.EmitOMPReductionClauseInit(S, LoopScope); 2208 CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion( 2209 CGF, S, CGF.EmitLValue(S.getIterationVariable())); 2210 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 2211 (void)LoopScope.Privatize(); 2212 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 2213 CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); 2214 2215 emitCommonSimdLoop( 2216 CGF, S, 2217 [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2218 CGF.EmitOMPSimdInit(S); 2219 }, 2220 [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { 2221 CGF.EmitOMPInnerLoop( 2222 S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), 2223 [&S](CodeGenFunction &CGF) { 2224 CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); 2225 CGF.EmitStopPoint(&S); 2226 }, 2227 [](CodeGenFunction &) {}); 2228 }); 2229 CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; }); 2230 // Emit final copy of the lastprivate variables at the end of loops. 2231 if (HasLastprivateClause) 2232 CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); 2233 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); 2234 emitPostUpdateForReductionClause(CGF, S, 2235 [](CodeGenFunction &) { return nullptr; }); 2236 } 2237 CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; }); 2238 // Emit: if (PreCond) - end. 2239 if (ContBlock) { 2240 CGF.EmitBranch(ContBlock); 2241 CGF.EmitBlock(ContBlock, true); 2242 } 2243 } 2244 2245 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { 2246 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2247 emitOMPSimdRegion(CGF, S, Action); 2248 }; 2249 { 2250 auto LPCRegion = 2251 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 2252 OMPLexicalScope Scope(*this, S, OMPD_unknown); 2253 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 2254 } 2255 // Check for outer lastprivate conditional update. 2256 checkForLastprivateConditionalUpdate(*this, S); 2257 } 2258 2259 void CodeGenFunction::EmitOMPOuterLoop( 2260 bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, 2261 CodeGenFunction::OMPPrivateScope &LoopScope, 2262 const CodeGenFunction::OMPLoopArguments &LoopArgs, 2263 const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, 2264 const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { 2265 CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); 2266 2267 const Expr *IVExpr = S.getIterationVariable(); 2268 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2269 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2270 2271 JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); 2272 2273 // Start the loop with a block that tests the condition. 2274 llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond"); 2275 EmitBlock(CondBlock); 2276 const SourceRange R = S.getSourceRange(); 2277 LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), 2278 SourceLocToDebugLoc(R.getEnd())); 2279 2280 llvm::Value *BoolCondVal = nullptr; 2281 if (!DynamicOrOrdered) { 2282 // UB = min(UB, GlobalUB) or 2283 // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. 2284 // 'distribute parallel for') 2285 EmitIgnoredExpr(LoopArgs.EUB); 2286 // IV = LB 2287 EmitIgnoredExpr(LoopArgs.Init); 2288 // IV < UB 2289 BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); 2290 } else { 2291 BoolCondVal = 2292 RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL, 2293 LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); 2294 } 2295 2296 // If there are any cleanups between here and the loop-exit scope, 2297 // create a block to stage a loop exit along. 2298 llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); 2299 if (LoopScope.requiresCleanups()) 2300 ExitBlock = createBasicBlock("omp.dispatch.cleanup"); 2301 2302 llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body"); 2303 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); 2304 if (ExitBlock != LoopExit.getBlock()) { 2305 EmitBlock(ExitBlock); 2306 EmitBranchThroughCleanup(LoopExit); 2307 } 2308 EmitBlock(LoopBody); 2309 2310 // Emit "IV = LB" (in case of static schedule, we have already calculated new 2311 // LB for loop condition and emitted it above). 2312 if (DynamicOrOrdered) 2313 EmitIgnoredExpr(LoopArgs.Init); 2314 2315 // Create a block for the increment. 2316 JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); 2317 BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); 2318 2319 emitCommonSimdLoop( 2320 *this, S, 2321 [&S, IsMonotonic](CodeGenFunction &CGF, PrePostActionTy &) { 2322 // Generate !llvm.loop.parallel metadata for loads and stores for loops 2323 // with dynamic/guided scheduling and without ordered clause. 2324 if (!isOpenMPSimdDirective(S.getDirectiveKind())) { 2325 CGF.LoopStack.setParallel(!IsMonotonic); 2326 if (const auto *C = S.getSingleClause<OMPOrderClause>()) 2327 if (C->getKind() == OMPC_ORDER_concurrent) 2328 CGF.LoopStack.setParallel(/*Enable=*/true); 2329 } else { 2330 CGF.EmitOMPSimdInit(S, IsMonotonic); 2331 } 2332 }, 2333 [&S, &LoopArgs, LoopExit, &CodeGenLoop, IVSize, IVSigned, &CodeGenOrdered, 2334 &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { 2335 SourceLocation Loc = S.getBeginLoc(); 2336 // when 'distribute' is not combined with a 'for': 2337 // while (idx <= UB) { BODY; ++idx; } 2338 // when 'distribute' is combined with a 'for' 2339 // (e.g. 'distribute parallel for') 2340 // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } 2341 CGF.EmitOMPInnerLoop( 2342 S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, 2343 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { 2344 CodeGenLoop(CGF, S, LoopExit); 2345 }, 2346 [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { 2347 CodeGenOrdered(CGF, Loc, IVSize, IVSigned); 2348 }); 2349 }); 2350 2351 EmitBlock(Continue.getBlock()); 2352 BreakContinueStack.pop_back(); 2353 if (!DynamicOrOrdered) { 2354 // Emit "LB = LB + Stride", "UB = UB + Stride". 2355 EmitIgnoredExpr(LoopArgs.NextLB); 2356 EmitIgnoredExpr(LoopArgs.NextUB); 2357 } 2358 2359 EmitBranch(CondBlock); 2360 LoopStack.pop(); 2361 // Emit the fall-through block. 2362 EmitBlock(LoopExit.getBlock()); 2363 2364 // Tell the runtime we are done. 2365 auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { 2366 if (!DynamicOrOrdered) 2367 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), 2368 S.getDirectiveKind()); 2369 }; 2370 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); 2371 } 2372 2373 void CodeGenFunction::EmitOMPForOuterLoop( 2374 const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, 2375 const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, 2376 const OMPLoopArguments &LoopArgs, 2377 const CodeGenDispatchBoundsTy &CGDispatchBounds) { 2378 CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); 2379 2380 // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). 2381 const bool DynamicOrOrdered = 2382 Ordered || RT.isDynamic(ScheduleKind.Schedule); 2383 2384 assert((Ordered || 2385 !RT.isStaticNonchunked(ScheduleKind.Schedule, 2386 LoopArgs.Chunk != nullptr)) && 2387 "static non-chunked schedule does not need outer loop"); 2388 2389 // Emit outer loop. 2390 // 2391 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 2392 // When schedule(dynamic,chunk_size) is specified, the iterations are 2393 // distributed to threads in the team in chunks as the threads request them. 2394 // Each thread executes a chunk of iterations, then requests another chunk, 2395 // until no chunks remain to be distributed. Each chunk contains chunk_size 2396 // iterations, except for the last chunk to be distributed, which may have 2397 // fewer iterations. When no chunk_size is specified, it defaults to 1. 2398 // 2399 // When schedule(guided,chunk_size) is specified, the iterations are assigned 2400 // to threads in the team in chunks as the executing threads request them. 2401 // Each thread executes a chunk of iterations, then requests another chunk, 2402 // until no chunks remain to be assigned. For a chunk_size of 1, the size of 2403 // each chunk is proportional to the number of unassigned iterations divided 2404 // by the number of threads in the team, decreasing to 1. For a chunk_size 2405 // with value k (greater than 1), the size of each chunk is determined in the 2406 // same way, with the restriction that the chunks do not contain fewer than k 2407 // iterations (except for the last chunk to be assigned, which may have fewer 2408 // than k iterations). 2409 // 2410 // When schedule(auto) is specified, the decision regarding scheduling is 2411 // delegated to the compiler and/or runtime system. The programmer gives the 2412 // implementation the freedom to choose any possible mapping of iterations to 2413 // threads in the team. 2414 // 2415 // When schedule(runtime) is specified, the decision regarding scheduling is 2416 // deferred until run time, and the schedule and chunk size are taken from the 2417 // run-sched-var ICV. If the ICV is set to auto, the schedule is 2418 // implementation defined 2419 // 2420 // while(__kmpc_dispatch_next(&LB, &UB)) { 2421 // idx = LB; 2422 // while (idx <= UB) { BODY; ++idx; 2423 // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. 2424 // } // inner loop 2425 // } 2426 // 2427 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 2428 // When schedule(static, chunk_size) is specified, iterations are divided into 2429 // chunks of size chunk_size, and the chunks are assigned to the threads in 2430 // the team in a round-robin fashion in the order of the thread number. 2431 // 2432 // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { 2433 // while (idx <= UB) { BODY; ++idx; } // inner loop 2434 // LB = LB + ST; 2435 // UB = UB + ST; 2436 // } 2437 // 2438 2439 const Expr *IVExpr = S.getIterationVariable(); 2440 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2441 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2442 2443 if (DynamicOrOrdered) { 2444 const std::pair<llvm::Value *, llvm::Value *> DispatchBounds = 2445 CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); 2446 llvm::Value *LBVal = DispatchBounds.first; 2447 llvm::Value *UBVal = DispatchBounds.second; 2448 CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, 2449 LoopArgs.Chunk}; 2450 RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize, 2451 IVSigned, Ordered, DipatchRTInputValues); 2452 } else { 2453 CGOpenMPRuntime::StaticRTInput StaticInit( 2454 IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, 2455 LoopArgs.ST, LoopArgs.Chunk); 2456 RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), 2457 ScheduleKind, StaticInit); 2458 } 2459 2460 auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, 2461 const unsigned IVSize, 2462 const bool IVSigned) { 2463 if (Ordered) { 2464 CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, 2465 IVSigned); 2466 } 2467 }; 2468 2469 OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, 2470 LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); 2471 OuterLoopArgs.IncExpr = S.getInc(); 2472 OuterLoopArgs.Init = S.getInit(); 2473 OuterLoopArgs.Cond = S.getCond(); 2474 OuterLoopArgs.NextLB = S.getNextLowerBound(); 2475 OuterLoopArgs.NextUB = S.getNextUpperBound(); 2476 EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, 2477 emitOMPLoopBodyWithStopPoint, CodeGenOrdered); 2478 } 2479 2480 static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, 2481 const unsigned IVSize, const bool IVSigned) {} 2482 2483 void CodeGenFunction::EmitOMPDistributeOuterLoop( 2484 OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, 2485 OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, 2486 const CodeGenLoopTy &CodeGenLoopContent) { 2487 2488 CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); 2489 2490 // Emit outer loop. 2491 // Same behavior as a OMPForOuterLoop, except that schedule cannot be 2492 // dynamic 2493 // 2494 2495 const Expr *IVExpr = S.getIterationVariable(); 2496 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2497 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2498 2499 CGOpenMPRuntime::StaticRTInput StaticInit( 2500 IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, 2501 LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); 2502 RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit); 2503 2504 // for combined 'distribute' and 'for' the increment expression of distribute 2505 // is stored in DistInc. For 'distribute' alone, it is in Inc. 2506 Expr *IncExpr; 2507 if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) 2508 IncExpr = S.getDistInc(); 2509 else 2510 IncExpr = S.getInc(); 2511 2512 // this routine is shared by 'omp distribute parallel for' and 2513 // 'omp distribute': select the right EUB expression depending on the 2514 // directive 2515 OMPLoopArguments OuterLoopArgs; 2516 OuterLoopArgs.LB = LoopArgs.LB; 2517 OuterLoopArgs.UB = LoopArgs.UB; 2518 OuterLoopArgs.ST = LoopArgs.ST; 2519 OuterLoopArgs.IL = LoopArgs.IL; 2520 OuterLoopArgs.Chunk = LoopArgs.Chunk; 2521 OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 2522 ? S.getCombinedEnsureUpperBound() 2523 : S.getEnsureUpperBound(); 2524 OuterLoopArgs.IncExpr = IncExpr; 2525 OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 2526 ? S.getCombinedInit() 2527 : S.getInit(); 2528 OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 2529 ? S.getCombinedCond() 2530 : S.getCond(); 2531 OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 2532 ? S.getCombinedNextLowerBound() 2533 : S.getNextLowerBound(); 2534 OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 2535 ? S.getCombinedNextUpperBound() 2536 : S.getNextUpperBound(); 2537 2538 EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, 2539 LoopScope, OuterLoopArgs, CodeGenLoopContent, 2540 emitEmptyOrdered); 2541 } 2542 2543 static std::pair<LValue, LValue> 2544 emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, 2545 const OMPExecutableDirective &S) { 2546 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); 2547 LValue LB = 2548 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); 2549 LValue UB = 2550 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); 2551 2552 // When composing 'distribute' with 'for' (e.g. as in 'distribute 2553 // parallel for') we need to use the 'distribute' 2554 // chunk lower and upper bounds rather than the whole loop iteration 2555 // space. These are parameters to the outlined function for 'parallel' 2556 // and we copy the bounds of the previous schedule into the 2557 // the current ones. 2558 LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); 2559 LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); 2560 llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( 2561 PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); 2562 PrevLBVal = CGF.EmitScalarConversion( 2563 PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), 2564 LS.getIterationVariable()->getType(), 2565 LS.getPrevLowerBoundVariable()->getExprLoc()); 2566 llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( 2567 PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); 2568 PrevUBVal = CGF.EmitScalarConversion( 2569 PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), 2570 LS.getIterationVariable()->getType(), 2571 LS.getPrevUpperBoundVariable()->getExprLoc()); 2572 2573 CGF.EmitStoreOfScalar(PrevLBVal, LB); 2574 CGF.EmitStoreOfScalar(PrevUBVal, UB); 2575 2576 return {LB, UB}; 2577 } 2578 2579 /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then 2580 /// we need to use the LB and UB expressions generated by the worksharing 2581 /// code generation support, whereas in non combined situations we would 2582 /// just emit 0 and the LastIteration expression 2583 /// This function is necessary due to the difference of the LB and UB 2584 /// types for the RT emission routines for 'for_static_init' and 2585 /// 'for_dispatch_init' 2586 static std::pair<llvm::Value *, llvm::Value *> 2587 emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, 2588 const OMPExecutableDirective &S, 2589 Address LB, Address UB) { 2590 const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); 2591 const Expr *IVExpr = LS.getIterationVariable(); 2592 // when implementing a dynamic schedule for a 'for' combined with a 2593 // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop 2594 // is not normalized as each team only executes its own assigned 2595 // distribute chunk 2596 QualType IteratorTy = IVExpr->getType(); 2597 llvm::Value *LBVal = 2598 CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); 2599 llvm::Value *UBVal = 2600 CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); 2601 return {LBVal, UBVal}; 2602 } 2603 2604 static void emitDistributeParallelForDistributeInnerBoundParams( 2605 CodeGenFunction &CGF, const OMPExecutableDirective &S, 2606 llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { 2607 const auto &Dir = cast<OMPLoopDirective>(S); 2608 LValue LB = 2609 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); 2610 llvm::Value *LBCast = 2611 CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(LB.getAddress(CGF)), 2612 CGF.SizeTy, /*isSigned=*/false); 2613 CapturedVars.push_back(LBCast); 2614 LValue UB = 2615 CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); 2616 2617 llvm::Value *UBCast = 2618 CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(UB.getAddress(CGF)), 2619 CGF.SizeTy, /*isSigned=*/false); 2620 CapturedVars.push_back(UBCast); 2621 } 2622 2623 static void 2624 emitInnerParallelForWhenCombined(CodeGenFunction &CGF, 2625 const OMPLoopDirective &S, 2626 CodeGenFunction::JumpDest LoopExit) { 2627 auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, 2628 PrePostActionTy &Action) { 2629 Action.Enter(CGF); 2630 bool HasCancel = false; 2631 if (!isOpenMPSimdDirective(S.getDirectiveKind())) { 2632 if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) 2633 HasCancel = D->hasCancel(); 2634 else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) 2635 HasCancel = D->hasCancel(); 2636 else if (const auto *D = 2637 dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) 2638 HasCancel = D->hasCancel(); 2639 } 2640 CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), 2641 HasCancel); 2642 CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), 2643 emitDistributeParallelForInnerBounds, 2644 emitDistributeParallelForDispatchBounds); 2645 }; 2646 2647 emitCommonOMPParallelDirective( 2648 CGF, S, 2649 isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, 2650 CGInlinedWorksharingLoop, 2651 emitDistributeParallelForDistributeInnerBoundParams); 2652 } 2653 2654 void CodeGenFunction::EmitOMPDistributeParallelForDirective( 2655 const OMPDistributeParallelForDirective &S) { 2656 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2657 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 2658 S.getDistInc()); 2659 }; 2660 OMPLexicalScope Scope(*this, S, OMPD_parallel); 2661 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); 2662 } 2663 2664 void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( 2665 const OMPDistributeParallelForSimdDirective &S) { 2666 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2667 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 2668 S.getDistInc()); 2669 }; 2670 OMPLexicalScope Scope(*this, S, OMPD_parallel); 2671 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); 2672 } 2673 2674 void CodeGenFunction::EmitOMPDistributeSimdDirective( 2675 const OMPDistributeSimdDirective &S) { 2676 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2677 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 2678 }; 2679 OMPLexicalScope Scope(*this, S, OMPD_unknown); 2680 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 2681 } 2682 2683 void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( 2684 CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { 2685 // Emit SPMD target parallel for region as a standalone region. 2686 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2687 emitOMPSimdRegion(CGF, S, Action); 2688 }; 2689 llvm::Function *Fn; 2690 llvm::Constant *Addr; 2691 // Emit target region as a standalone region. 2692 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 2693 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 2694 assert(Fn && Addr && "Target device function emission failed."); 2695 } 2696 2697 void CodeGenFunction::EmitOMPTargetSimdDirective( 2698 const OMPTargetSimdDirective &S) { 2699 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 2700 emitOMPSimdRegion(CGF, S, Action); 2701 }; 2702 emitCommonOMPTargetDirective(*this, S, CodeGen); 2703 } 2704 2705 namespace { 2706 struct ScheduleKindModifiersTy { 2707 OpenMPScheduleClauseKind Kind; 2708 OpenMPScheduleClauseModifier M1; 2709 OpenMPScheduleClauseModifier M2; 2710 ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, 2711 OpenMPScheduleClauseModifier M1, 2712 OpenMPScheduleClauseModifier M2) 2713 : Kind(Kind), M1(M1), M2(M2) {} 2714 }; 2715 } // namespace 2716 2717 bool CodeGenFunction::EmitOMPWorksharingLoop( 2718 const OMPLoopDirective &S, Expr *EUB, 2719 const CodeGenLoopBoundsTy &CodeGenLoopBounds, 2720 const CodeGenDispatchBoundsTy &CGDispatchBounds) { 2721 // Emit the loop iteration variable. 2722 const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 2723 const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); 2724 EmitVarDecl(*IVDecl); 2725 2726 // Emit the iterations count variable. 2727 // If it is not a variable, Sema decided to calculate iterations count on each 2728 // iteration (e.g., it is foldable into a constant). 2729 if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 2730 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 2731 // Emit calculation of the iterations count. 2732 EmitIgnoredExpr(S.getCalcLastIteration()); 2733 } 2734 2735 CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); 2736 2737 bool HasLastprivateClause; 2738 // Check pre-condition. 2739 { 2740 OMPLoopScope PreInitScope(*this, S); 2741 // Skip the entire loop if we don't meet the precondition. 2742 // If the condition constant folds and can be elided, avoid emitting the 2743 // whole loop. 2744 bool CondConstant; 2745 llvm::BasicBlock *ContBlock = nullptr; 2746 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 2747 if (!CondConstant) 2748 return false; 2749 } else { 2750 llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); 2751 ContBlock = createBasicBlock("omp.precond.end"); 2752 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 2753 getProfileCount(&S)); 2754 EmitBlock(ThenBlock); 2755 incrementProfileCounter(&S); 2756 } 2757 2758 RunCleanupsScope DoacrossCleanupScope(*this); 2759 bool Ordered = false; 2760 if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { 2761 if (OrderedClause->getNumForLoops()) 2762 RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations()); 2763 else 2764 Ordered = true; 2765 } 2766 2767 llvm::DenseSet<const Expr *> EmittedFinals; 2768 emitAlignedClause(*this, S); 2769 bool HasLinears = EmitOMPLinearClauseInit(S); 2770 // Emit helper vars inits. 2771 2772 std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); 2773 LValue LB = Bounds.first; 2774 LValue UB = Bounds.second; 2775 LValue ST = 2776 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 2777 LValue IL = 2778 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 2779 2780 // Emit 'then' code. 2781 { 2782 OMPPrivateScope LoopScope(*this); 2783 if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { 2784 // Emit implicit barrier to synchronize threads and avoid data races on 2785 // initialization of firstprivate variables and post-update of 2786 // lastprivate variables. 2787 CGM.getOpenMPRuntime().emitBarrierCall( 2788 *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, 2789 /*ForceSimpleCall=*/true); 2790 } 2791 EmitOMPPrivateClause(S, LoopScope); 2792 CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion( 2793 *this, S, EmitLValue(S.getIterationVariable())); 2794 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); 2795 EmitOMPReductionClauseInit(S, LoopScope); 2796 EmitOMPPrivateLoopCounters(S, LoopScope); 2797 EmitOMPLinearClause(S, LoopScope); 2798 (void)LoopScope.Privatize(); 2799 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 2800 CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); 2801 2802 // Detect the loop schedule kind and chunk. 2803 const Expr *ChunkExpr = nullptr; 2804 OpenMPScheduleTy ScheduleKind; 2805 if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { 2806 ScheduleKind.Schedule = C->getScheduleKind(); 2807 ScheduleKind.M1 = C->getFirstScheduleModifier(); 2808 ScheduleKind.M2 = C->getSecondScheduleModifier(); 2809 ChunkExpr = C->getChunkSize(); 2810 } else { 2811 // Default behaviour for schedule clause. 2812 CGM.getOpenMPRuntime().getDefaultScheduleAndChunk( 2813 *this, S, ScheduleKind.Schedule, ChunkExpr); 2814 } 2815 bool HasChunkSizeOne = false; 2816 llvm::Value *Chunk = nullptr; 2817 if (ChunkExpr) { 2818 Chunk = EmitScalarExpr(ChunkExpr); 2819 Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(), 2820 S.getIterationVariable()->getType(), 2821 S.getBeginLoc()); 2822 Expr::EvalResult Result; 2823 if (ChunkExpr->EvaluateAsInt(Result, getContext())) { 2824 llvm::APSInt EvaluatedChunk = Result.Val.getInt(); 2825 HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1); 2826 } 2827 } 2828 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 2829 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 2830 // OpenMP 4.5, 2.7.1 Loop Construct, Description. 2831 // If the static schedule kind is specified or if the ordered clause is 2832 // specified, and if no monotonic modifier is specified, the effect will 2833 // be as if the monotonic modifier was specified. 2834 bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule, 2835 /* Chunked */ Chunk != nullptr) && HasChunkSizeOne && 2836 isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); 2837 if ((RT.isStaticNonchunked(ScheduleKind.Schedule, 2838 /* Chunked */ Chunk != nullptr) || 2839 StaticChunkedOne) && 2840 !Ordered) { 2841 JumpDest LoopExit = 2842 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 2843 emitCommonSimdLoop( 2844 *this, S, 2845 [&S](CodeGenFunction &CGF, PrePostActionTy &) { 2846 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 2847 CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true); 2848 } else if (const auto *C = S.getSingleClause<OMPOrderClause>()) { 2849 if (C->getKind() == OMPC_ORDER_concurrent) 2850 CGF.LoopStack.setParallel(/*Enable=*/true); 2851 } 2852 }, 2853 [IVSize, IVSigned, Ordered, IL, LB, UB, ST, StaticChunkedOne, Chunk, 2854 &S, ScheduleKind, LoopExit, 2855 &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { 2856 // OpenMP [2.7.1, Loop Construct, Description, table 2-1] 2857 // When no chunk_size is specified, the iteration space is divided 2858 // into chunks that are approximately equal in size, and at most 2859 // one chunk is distributed to each thread. Note that the size of 2860 // the chunks is unspecified in this case. 2861 CGOpenMPRuntime::StaticRTInput StaticInit( 2862 IVSize, IVSigned, Ordered, IL.getAddress(CGF), 2863 LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF), 2864 StaticChunkedOne ? Chunk : nullptr); 2865 CGF.CGM.getOpenMPRuntime().emitForStaticInit( 2866 CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, 2867 StaticInit); 2868 // UB = min(UB, GlobalUB); 2869 if (!StaticChunkedOne) 2870 CGF.EmitIgnoredExpr(S.getEnsureUpperBound()); 2871 // IV = LB; 2872 CGF.EmitIgnoredExpr(S.getInit()); 2873 // For unchunked static schedule generate: 2874 // 2875 // while (idx <= UB) { 2876 // BODY; 2877 // ++idx; 2878 // } 2879 // 2880 // For static schedule with chunk one: 2881 // 2882 // while (IV <= PrevUB) { 2883 // BODY; 2884 // IV += ST; 2885 // } 2886 CGF.EmitOMPInnerLoop( 2887 S, LoopScope.requiresCleanups(), 2888 StaticChunkedOne ? S.getCombinedParForInDistCond() 2889 : S.getCond(), 2890 StaticChunkedOne ? S.getDistInc() : S.getInc(), 2891 [&S, LoopExit](CodeGenFunction &CGF) { 2892 CGF.EmitOMPLoopBody(S, LoopExit); 2893 CGF.EmitStopPoint(&S); 2894 }, 2895 [](CodeGenFunction &) {}); 2896 }); 2897 EmitBlock(LoopExit.getBlock()); 2898 // Tell the runtime we are done. 2899 auto &&CodeGen = [&S](CodeGenFunction &CGF) { 2900 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), 2901 S.getDirectiveKind()); 2902 }; 2903 OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); 2904 } else { 2905 const bool IsMonotonic = 2906 Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || 2907 ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || 2908 ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || 2909 ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; 2910 // Emit the outer loop, which requests its work chunk [LB..UB] from 2911 // runtime and runs the inner loop to process it. 2912 const OMPLoopArguments LoopArguments( 2913 LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), 2914 IL.getAddress(*this), Chunk, EUB); 2915 EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, 2916 LoopArguments, CGDispatchBounds); 2917 } 2918 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 2919 EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { 2920 return CGF.Builder.CreateIsNotNull( 2921 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 2922 }); 2923 } 2924 EmitOMPReductionClauseFinal( 2925 S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) 2926 ? /*Parallel and Simd*/ OMPD_parallel_for_simd 2927 : /*Parallel only*/ OMPD_parallel); 2928 // Emit post-update of the reduction variables if IsLastIter != 0. 2929 emitPostUpdateForReductionClause( 2930 *this, S, [IL, &S](CodeGenFunction &CGF) { 2931 return CGF.Builder.CreateIsNotNull( 2932 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 2933 }); 2934 // Emit final copy of the lastprivate variables if IsLastIter != 0. 2935 if (HasLastprivateClause) 2936 EmitOMPLastprivateClauseFinal( 2937 S, isOpenMPSimdDirective(S.getDirectiveKind()), 2938 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); 2939 } 2940 EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) { 2941 return CGF.Builder.CreateIsNotNull( 2942 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 2943 }); 2944 DoacrossCleanupScope.ForceCleanup(); 2945 // We're now done with the loop, so jump to the continuation block. 2946 if (ContBlock) { 2947 EmitBranch(ContBlock); 2948 EmitBlock(ContBlock, /*IsFinished=*/true); 2949 } 2950 } 2951 return HasLastprivateClause; 2952 } 2953 2954 /// The following two functions generate expressions for the loop lower 2955 /// and upper bounds in case of static and dynamic (dispatch) schedule 2956 /// of the associated 'for' or 'distribute' loop. 2957 static std::pair<LValue, LValue> 2958 emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { 2959 const auto &LS = cast<OMPLoopDirective>(S); 2960 LValue LB = 2961 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); 2962 LValue UB = 2963 EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); 2964 return {LB, UB}; 2965 } 2966 2967 /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not 2968 /// consider the lower and upper bound expressions generated by the 2969 /// worksharing loop support, but we use 0 and the iteration space size as 2970 /// constants 2971 static std::pair<llvm::Value *, llvm::Value *> 2972 emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, 2973 Address LB, Address UB) { 2974 const auto &LS = cast<OMPLoopDirective>(S); 2975 const Expr *IVExpr = LS.getIterationVariable(); 2976 const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); 2977 llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); 2978 llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); 2979 return {LBVal, UBVal}; 2980 } 2981 2982 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { 2983 bool HasLastprivates = false; 2984 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 2985 PrePostActionTy &) { 2986 OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); 2987 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), 2988 emitForLoopBounds, 2989 emitDispatchForLoopBounds); 2990 }; 2991 { 2992 auto LPCRegion = 2993 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 2994 OMPLexicalScope Scope(*this, S, OMPD_unknown); 2995 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, 2996 S.hasCancel()); 2997 } 2998 2999 // Emit an implicit barrier at the end. 3000 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) 3001 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); 3002 // Check for outer lastprivate conditional update. 3003 checkForLastprivateConditionalUpdate(*this, S); 3004 } 3005 3006 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { 3007 bool HasLastprivates = false; 3008 auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, 3009 PrePostActionTy &) { 3010 HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), 3011 emitForLoopBounds, 3012 emitDispatchForLoopBounds); 3013 }; 3014 { 3015 auto LPCRegion = 3016 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3017 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3018 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); 3019 } 3020 3021 // Emit an implicit barrier at the end. 3022 if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) 3023 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); 3024 // Check for outer lastprivate conditional update. 3025 checkForLastprivateConditionalUpdate(*this, S); 3026 } 3027 3028 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, 3029 const Twine &Name, 3030 llvm::Value *Init = nullptr) { 3031 LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); 3032 if (Init) 3033 CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); 3034 return LVal; 3035 } 3036 3037 void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { 3038 const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); 3039 const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt); 3040 bool HasLastprivates = false; 3041 auto &&CodeGen = [&S, CapturedStmt, CS, 3042 &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) { 3043 const ASTContext &C = CGF.getContext(); 3044 QualType KmpInt32Ty = 3045 C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); 3046 // Emit helper vars inits. 3047 LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", 3048 CGF.Builder.getInt32(0)); 3049 llvm::ConstantInt *GlobalUBVal = CS != nullptr 3050 ? CGF.Builder.getInt32(CS->size() - 1) 3051 : CGF.Builder.getInt32(0); 3052 LValue UB = 3053 createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); 3054 LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", 3055 CGF.Builder.getInt32(1)); 3056 LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", 3057 CGF.Builder.getInt32(0)); 3058 // Loop counter. 3059 LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); 3060 OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); 3061 CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); 3062 OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); 3063 CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); 3064 // Generate condition for loop. 3065 BinaryOperator *Cond = BinaryOperator::Create( 3066 C, &IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, OK_Ordinary, 3067 S.getBeginLoc(), FPOptions(C.getLangOpts())); 3068 // Increment for loop counter. 3069 UnaryOperator *Inc = UnaryOperator::Create( 3070 C, &IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, 3071 S.getBeginLoc(), true, FPOptions(C.getLangOpts())); 3072 auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) { 3073 // Iterate through all sections and emit a switch construct: 3074 // switch (IV) { 3075 // case 0: 3076 // <SectionStmt[0]>; 3077 // break; 3078 // ... 3079 // case <NumSection> - 1: 3080 // <SectionStmt[<NumSection> - 1]>; 3081 // break; 3082 // } 3083 // .omp.sections.exit: 3084 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); 3085 llvm::SwitchInst *SwitchStmt = 3086 CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()), 3087 ExitBB, CS == nullptr ? 1 : CS->size()); 3088 if (CS) { 3089 unsigned CaseNumber = 0; 3090 for (const Stmt *SubStmt : CS->children()) { 3091 auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); 3092 CGF.EmitBlock(CaseBB); 3093 SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); 3094 CGF.EmitStmt(SubStmt); 3095 CGF.EmitBranch(ExitBB); 3096 ++CaseNumber; 3097 } 3098 } else { 3099 llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case"); 3100 CGF.EmitBlock(CaseBB); 3101 SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); 3102 CGF.EmitStmt(CapturedStmt); 3103 CGF.EmitBranch(ExitBB); 3104 } 3105 CGF.EmitBlock(ExitBB, /*IsFinished=*/true); 3106 }; 3107 3108 CodeGenFunction::OMPPrivateScope LoopScope(CGF); 3109 if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { 3110 // Emit implicit barrier to synchronize threads and avoid data races on 3111 // initialization of firstprivate variables and post-update of lastprivate 3112 // variables. 3113 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 3114 CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, 3115 /*ForceSimpleCall=*/true); 3116 } 3117 CGF.EmitOMPPrivateClause(S, LoopScope); 3118 CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion(CGF, S, IV); 3119 HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 3120 CGF.EmitOMPReductionClauseInit(S, LoopScope); 3121 (void)LoopScope.Privatize(); 3122 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 3123 CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); 3124 3125 // Emit static non-chunked loop. 3126 OpenMPScheduleTy ScheduleKind; 3127 ScheduleKind.Schedule = OMPC_SCHEDULE_static; 3128 CGOpenMPRuntime::StaticRTInput StaticInit( 3129 /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(CGF), 3130 LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF)); 3131 CGF.CGM.getOpenMPRuntime().emitForStaticInit( 3132 CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit); 3133 // UB = min(UB, GlobalUB); 3134 llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc()); 3135 llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect( 3136 CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); 3137 CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); 3138 // IV = LB; 3139 CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV); 3140 // while (idx <= UB) { BODY; ++idx; } 3141 CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, Cond, Inc, BodyGen, 3142 [](CodeGenFunction &) {}); 3143 // Tell the runtime we are done. 3144 auto &&CodeGen = [&S](CodeGenFunction &CGF) { 3145 CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), 3146 S.getDirectiveKind()); 3147 }; 3148 CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); 3149 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); 3150 // Emit post-update of the reduction variables if IsLastIter != 0. 3151 emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) { 3152 return CGF.Builder.CreateIsNotNull( 3153 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 3154 }); 3155 3156 // Emit final copy of the lastprivate variables if IsLastIter != 0. 3157 if (HasLastprivates) 3158 CGF.EmitOMPLastprivateClauseFinal( 3159 S, /*NoFinals=*/false, 3160 CGF.Builder.CreateIsNotNull( 3161 CGF.EmitLoadOfScalar(IL, S.getBeginLoc()))); 3162 }; 3163 3164 bool HasCancel = false; 3165 if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) 3166 HasCancel = OSD->hasCancel(); 3167 else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) 3168 HasCancel = OPSD->hasCancel(); 3169 OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); 3170 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, 3171 HasCancel); 3172 // Emit barrier for lastprivates only if 'sections' directive has 'nowait' 3173 // clause. Otherwise the barrier will be generated by the codegen for the 3174 // directive. 3175 if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { 3176 // Emit implicit barrier to synchronize threads and avoid data races on 3177 // initialization of firstprivate variables. 3178 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), 3179 OMPD_unknown); 3180 } 3181 } 3182 3183 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { 3184 { 3185 auto LPCRegion = 3186 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3187 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3188 EmitSections(S); 3189 } 3190 // Emit an implicit barrier at the end. 3191 if (!S.getSingleClause<OMPNowaitClause>()) { 3192 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), 3193 OMPD_sections); 3194 } 3195 // Check for outer lastprivate conditional update. 3196 checkForLastprivateConditionalUpdate(*this, S); 3197 } 3198 3199 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { 3200 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 3201 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 3202 }; 3203 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3204 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, 3205 S.hasCancel()); 3206 } 3207 3208 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { 3209 llvm::SmallVector<const Expr *, 8> CopyprivateVars; 3210 llvm::SmallVector<const Expr *, 8> DestExprs; 3211 llvm::SmallVector<const Expr *, 8> SrcExprs; 3212 llvm::SmallVector<const Expr *, 8> AssignmentOps; 3213 // Check if there are any 'copyprivate' clauses associated with this 3214 // 'single' construct. 3215 // Build a list of copyprivate variables along with helper expressions 3216 // (<source>, <destination>, <destination>=<source> expressions) 3217 for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { 3218 CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); 3219 DestExprs.append(C->destination_exprs().begin(), 3220 C->destination_exprs().end()); 3221 SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); 3222 AssignmentOps.append(C->assignment_ops().begin(), 3223 C->assignment_ops().end()); 3224 } 3225 // Emit code for 'single' region along with 'copyprivate' clauses 3226 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3227 Action.Enter(CGF); 3228 OMPPrivateScope SingleScope(CGF); 3229 (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); 3230 CGF.EmitOMPPrivateClause(S, SingleScope); 3231 (void)SingleScope.Privatize(); 3232 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 3233 }; 3234 { 3235 auto LPCRegion = 3236 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3237 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3238 CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(), 3239 CopyprivateVars, DestExprs, 3240 SrcExprs, AssignmentOps); 3241 } 3242 // Emit an implicit barrier at the end (to avoid data race on firstprivate 3243 // init or if no 'nowait' clause was specified and no 'copyprivate' clause). 3244 if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { 3245 CGM.getOpenMPRuntime().emitBarrierCall( 3246 *this, S.getBeginLoc(), 3247 S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); 3248 } 3249 // Check for outer lastprivate conditional update. 3250 checkForLastprivateConditionalUpdate(*this, S); 3251 } 3252 3253 static void emitMaster(CodeGenFunction &CGF, const OMPExecutableDirective &S) { 3254 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3255 Action.Enter(CGF); 3256 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 3257 }; 3258 CGF.CGM.getOpenMPRuntime().emitMasterRegion(CGF, CodeGen, S.getBeginLoc()); 3259 } 3260 3261 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { 3262 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { 3263 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 3264 3265 const CapturedStmt *CS = S.getInnermostCapturedStmt(); 3266 const Stmt *MasterRegionBodyStmt = CS->getCapturedStmt(); 3267 3268 auto FiniCB = [this](InsertPointTy IP) { 3269 OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); 3270 }; 3271 3272 auto BodyGenCB = [MasterRegionBodyStmt, this](InsertPointTy AllocaIP, 3273 InsertPointTy CodeGenIP, 3274 llvm::BasicBlock &FiniBB) { 3275 OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB); 3276 OMPBuilderCBHelpers::EmitOMPRegionBody(*this, MasterRegionBodyStmt, 3277 CodeGenIP, FiniBB); 3278 }; 3279 3280 CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); 3281 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); 3282 Builder.restoreIP(OMPBuilder->CreateMaster(Builder, BodyGenCB, FiniCB)); 3283 3284 return; 3285 } 3286 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3287 emitMaster(*this, S); 3288 } 3289 3290 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { 3291 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { 3292 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 3293 3294 const CapturedStmt *CS = S.getInnermostCapturedStmt(); 3295 const Stmt *CriticalRegionBodyStmt = CS->getCapturedStmt(); 3296 const Expr *Hint = nullptr; 3297 if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) 3298 Hint = HintClause->getHint(); 3299 3300 // TODO: This is slightly different from what's currently being done in 3301 // clang. Fix the Int32Ty to IntPtrTy (pointer width size) when everything 3302 // about typing is final. 3303 llvm::Value *HintInst = nullptr; 3304 if (Hint) 3305 HintInst = 3306 Builder.CreateIntCast(EmitScalarExpr(Hint), CGM.Int32Ty, false); 3307 3308 auto FiniCB = [this](InsertPointTy IP) { 3309 OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); 3310 }; 3311 3312 auto BodyGenCB = [CriticalRegionBodyStmt, this](InsertPointTy AllocaIP, 3313 InsertPointTy CodeGenIP, 3314 llvm::BasicBlock &FiniBB) { 3315 OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB); 3316 OMPBuilderCBHelpers::EmitOMPRegionBody(*this, CriticalRegionBodyStmt, 3317 CodeGenIP, FiniBB); 3318 }; 3319 3320 CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); 3321 CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); 3322 Builder.restoreIP(OMPBuilder->CreateCritical( 3323 Builder, BodyGenCB, FiniCB, S.getDirectiveName().getAsString(), 3324 HintInst)); 3325 3326 return; 3327 } 3328 3329 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3330 Action.Enter(CGF); 3331 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 3332 }; 3333 const Expr *Hint = nullptr; 3334 if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) 3335 Hint = HintClause->getHint(); 3336 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3337 CGM.getOpenMPRuntime().emitCriticalRegion(*this, 3338 S.getDirectiveName().getAsString(), 3339 CodeGen, S.getBeginLoc(), Hint); 3340 } 3341 3342 void CodeGenFunction::EmitOMPParallelForDirective( 3343 const OMPParallelForDirective &S) { 3344 // Emit directive as a combined directive that consists of two implicit 3345 // directives: 'parallel' with 'for' directive. 3346 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3347 Action.Enter(CGF); 3348 OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); 3349 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, 3350 emitDispatchForLoopBounds); 3351 }; 3352 { 3353 auto LPCRegion = 3354 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3355 emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, 3356 emitEmptyBoundParameters); 3357 } 3358 // Check for outer lastprivate conditional update. 3359 checkForLastprivateConditionalUpdate(*this, S); 3360 } 3361 3362 void CodeGenFunction::EmitOMPParallelForSimdDirective( 3363 const OMPParallelForSimdDirective &S) { 3364 // Emit directive as a combined directive that consists of two implicit 3365 // directives: 'parallel' with 'for' directive. 3366 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3367 Action.Enter(CGF); 3368 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, 3369 emitDispatchForLoopBounds); 3370 }; 3371 { 3372 auto LPCRegion = 3373 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3374 emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, 3375 emitEmptyBoundParameters); 3376 } 3377 // Check for outer lastprivate conditional update. 3378 checkForLastprivateConditionalUpdate(*this, S); 3379 } 3380 3381 void CodeGenFunction::EmitOMPParallelMasterDirective( 3382 const OMPParallelMasterDirective &S) { 3383 // Emit directive as a combined directive that consists of two implicit 3384 // directives: 'parallel' with 'master' directive. 3385 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3386 Action.Enter(CGF); 3387 OMPPrivateScope PrivateScope(CGF); 3388 bool Copyins = CGF.EmitOMPCopyinClause(S); 3389 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 3390 if (Copyins) { 3391 // Emit implicit barrier to synchronize threads and avoid data races on 3392 // propagation master's thread values of threadprivate variables to local 3393 // instances of that variables of all other implicit threads. 3394 CGF.CGM.getOpenMPRuntime().emitBarrierCall( 3395 CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, 3396 /*ForceSimpleCall=*/true); 3397 } 3398 CGF.EmitOMPPrivateClause(S, PrivateScope); 3399 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 3400 (void)PrivateScope.Privatize(); 3401 emitMaster(CGF, S); 3402 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); 3403 }; 3404 { 3405 auto LPCRegion = 3406 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3407 emitCommonOMPParallelDirective(*this, S, OMPD_master, CodeGen, 3408 emitEmptyBoundParameters); 3409 emitPostUpdateForReductionClause(*this, S, 3410 [](CodeGenFunction &) { return nullptr; }); 3411 } 3412 // Check for outer lastprivate conditional update. 3413 checkForLastprivateConditionalUpdate(*this, S); 3414 } 3415 3416 void CodeGenFunction::EmitOMPParallelSectionsDirective( 3417 const OMPParallelSectionsDirective &S) { 3418 // Emit directive as a combined directive that consists of two implicit 3419 // directives: 'parallel' with 'sections' directive. 3420 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3421 Action.Enter(CGF); 3422 CGF.EmitSections(S); 3423 }; 3424 { 3425 auto LPCRegion = 3426 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3427 emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, 3428 emitEmptyBoundParameters); 3429 } 3430 // Check for outer lastprivate conditional update. 3431 checkForLastprivateConditionalUpdate(*this, S); 3432 } 3433 3434 void CodeGenFunction::EmitOMPTaskBasedDirective( 3435 const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, 3436 const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, 3437 OMPTaskDataTy &Data) { 3438 // Emit outlined function for task construct. 3439 const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); 3440 auto I = CS->getCapturedDecl()->param_begin(); 3441 auto PartId = std::next(I); 3442 auto TaskT = std::next(I, 4); 3443 // Check if the task is final 3444 if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { 3445 // If the condition constant folds and can be elided, try to avoid emitting 3446 // the condition and the dead arm of the if/else. 3447 const Expr *Cond = Clause->getCondition(); 3448 bool CondConstant; 3449 if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) 3450 Data.Final.setInt(CondConstant); 3451 else 3452 Data.Final.setPointer(EvaluateExprAsBool(Cond)); 3453 } else { 3454 // By default the task is not final. 3455 Data.Final.setInt(/*IntVal=*/false); 3456 } 3457 // Check if the task has 'priority' clause. 3458 if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { 3459 const Expr *Prio = Clause->getPriority(); 3460 Data.Priority.setInt(/*IntVal=*/true); 3461 Data.Priority.setPointer(EmitScalarConversion( 3462 EmitScalarExpr(Prio), Prio->getType(), 3463 getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), 3464 Prio->getExprLoc())); 3465 } 3466 // The first function argument for tasks is a thread id, the second one is a 3467 // part id (0 for tied tasks, >=0 for untied task). 3468 llvm::DenseSet<const VarDecl *> EmittedAsPrivate; 3469 // Get list of private variables. 3470 for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { 3471 auto IRef = C->varlist_begin(); 3472 for (const Expr *IInit : C->private_copies()) { 3473 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 3474 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 3475 Data.PrivateVars.push_back(*IRef); 3476 Data.PrivateCopies.push_back(IInit); 3477 } 3478 ++IRef; 3479 } 3480 } 3481 EmittedAsPrivate.clear(); 3482 // Get list of firstprivate variables. 3483 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { 3484 auto IRef = C->varlist_begin(); 3485 auto IElemInitRef = C->inits().begin(); 3486 for (const Expr *IInit : C->private_copies()) { 3487 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 3488 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 3489 Data.FirstprivateVars.push_back(*IRef); 3490 Data.FirstprivateCopies.push_back(IInit); 3491 Data.FirstprivateInits.push_back(*IElemInitRef); 3492 } 3493 ++IRef; 3494 ++IElemInitRef; 3495 } 3496 } 3497 // Get list of lastprivate variables (for taskloops). 3498 llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; 3499 for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { 3500 auto IRef = C->varlist_begin(); 3501 auto ID = C->destination_exprs().begin(); 3502 for (const Expr *IInit : C->private_copies()) { 3503 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); 3504 if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { 3505 Data.LastprivateVars.push_back(*IRef); 3506 Data.LastprivateCopies.push_back(IInit); 3507 } 3508 LastprivateDstsOrigs.insert( 3509 {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), 3510 cast<DeclRefExpr>(*IRef)}); 3511 ++IRef; 3512 ++ID; 3513 } 3514 } 3515 SmallVector<const Expr *, 4> LHSs; 3516 SmallVector<const Expr *, 4> RHSs; 3517 for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { 3518 Data.ReductionVars.append(C->varlist_begin(), C->varlist_end()); 3519 Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end()); 3520 Data.ReductionCopies.append(C->privates().begin(), C->privates().end()); 3521 Data.ReductionOps.append(C->reduction_ops().begin(), 3522 C->reduction_ops().end()); 3523 LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 3524 RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 3525 } 3526 Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( 3527 *this, S.getBeginLoc(), LHSs, RHSs, Data); 3528 // Build list of dependences. 3529 for (const auto *C : S.getClausesOfKind<OMPDependClause>()) { 3530 OMPTaskDataTy::DependData &DD = 3531 Data.Dependences.emplace_back(C->getDependencyKind(), C->getModifier()); 3532 DD.DepExprs.append(C->varlist_begin(), C->varlist_end()); 3533 } 3534 auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, 3535 CapturedRegion](CodeGenFunction &CGF, 3536 PrePostActionTy &Action) { 3537 // Set proper addresses for generated private copies. 3538 OMPPrivateScope Scope(CGF); 3539 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> FirstprivatePtrs; 3540 if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || 3541 !Data.LastprivateVars.empty()) { 3542 llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( 3543 CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); 3544 enum { PrivatesParam = 2, CopyFnParam = 3 }; 3545 llvm::Value *CopyFn = CGF.Builder.CreateLoad( 3546 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); 3547 llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( 3548 CS->getCapturedDecl()->getParam(PrivatesParam))); 3549 // Map privates. 3550 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; 3551 llvm::SmallVector<llvm::Value *, 16> CallArgs; 3552 CallArgs.push_back(PrivatesPtr); 3553 for (const Expr *E : Data.PrivateVars) { 3554 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 3555 Address PrivatePtr = CGF.CreateMemTemp( 3556 CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); 3557 PrivatePtrs.emplace_back(VD, PrivatePtr); 3558 CallArgs.push_back(PrivatePtr.getPointer()); 3559 } 3560 for (const Expr *E : Data.FirstprivateVars) { 3561 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 3562 Address PrivatePtr = 3563 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 3564 ".firstpriv.ptr.addr"); 3565 PrivatePtrs.emplace_back(VD, PrivatePtr); 3566 FirstprivatePtrs.emplace_back(VD, PrivatePtr); 3567 CallArgs.push_back(PrivatePtr.getPointer()); 3568 } 3569 for (const Expr *E : Data.LastprivateVars) { 3570 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 3571 Address PrivatePtr = 3572 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 3573 ".lastpriv.ptr.addr"); 3574 PrivatePtrs.emplace_back(VD, PrivatePtr); 3575 CallArgs.push_back(PrivatePtr.getPointer()); 3576 } 3577 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( 3578 CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); 3579 for (const auto &Pair : LastprivateDstsOrigs) { 3580 const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); 3581 DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(OrigVD), 3582 /*RefersToEnclosingVariableOrCapture=*/ 3583 CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, 3584 Pair.second->getType(), VK_LValue, 3585 Pair.second->getExprLoc()); 3586 Scope.addPrivate(Pair.first, [&CGF, &DRE]() { 3587 return CGF.EmitLValue(&DRE).getAddress(CGF); 3588 }); 3589 } 3590 for (const auto &Pair : PrivatePtrs) { 3591 Address Replacement(CGF.Builder.CreateLoad(Pair.second), 3592 CGF.getContext().getDeclAlign(Pair.first)); 3593 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); 3594 } 3595 } 3596 if (Data.Reductions) { 3597 OMPPrivateScope FirstprivateScope(CGF); 3598 for (const auto &Pair : FirstprivatePtrs) { 3599 Address Replacement(CGF.Builder.CreateLoad(Pair.second), 3600 CGF.getContext().getDeclAlign(Pair.first)); 3601 FirstprivateScope.addPrivate(Pair.first, 3602 [Replacement]() { return Replacement; }); 3603 } 3604 (void)FirstprivateScope.Privatize(); 3605 OMPLexicalScope LexScope(CGF, S, CapturedRegion); 3606 ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionVars, 3607 Data.ReductionCopies, Data.ReductionOps); 3608 llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( 3609 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); 3610 for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { 3611 RedCG.emitSharedOrigLValue(CGF, Cnt); 3612 RedCG.emitAggregateType(CGF, Cnt); 3613 // FIXME: This must removed once the runtime library is fixed. 3614 // Emit required threadprivate variables for 3615 // initializer/combiner/finalizer. 3616 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), 3617 RedCG, Cnt); 3618 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( 3619 CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); 3620 Replacement = 3621 Address(CGF.EmitScalarConversion( 3622 Replacement.getPointer(), CGF.getContext().VoidPtrTy, 3623 CGF.getContext().getPointerType( 3624 Data.ReductionCopies[Cnt]->getType()), 3625 Data.ReductionCopies[Cnt]->getExprLoc()), 3626 Replacement.getAlignment()); 3627 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); 3628 Scope.addPrivate(RedCG.getBaseDecl(Cnt), 3629 [Replacement]() { return Replacement; }); 3630 } 3631 } 3632 // Privatize all private variables except for in_reduction items. 3633 (void)Scope.Privatize(); 3634 SmallVector<const Expr *, 4> InRedVars; 3635 SmallVector<const Expr *, 4> InRedPrivs; 3636 SmallVector<const Expr *, 4> InRedOps; 3637 SmallVector<const Expr *, 4> TaskgroupDescriptors; 3638 for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { 3639 auto IPriv = C->privates().begin(); 3640 auto IRed = C->reduction_ops().begin(); 3641 auto ITD = C->taskgroup_descriptors().begin(); 3642 for (const Expr *Ref : C->varlists()) { 3643 InRedVars.emplace_back(Ref); 3644 InRedPrivs.emplace_back(*IPriv); 3645 InRedOps.emplace_back(*IRed); 3646 TaskgroupDescriptors.emplace_back(*ITD); 3647 std::advance(IPriv, 1); 3648 std::advance(IRed, 1); 3649 std::advance(ITD, 1); 3650 } 3651 } 3652 // Privatize in_reduction items here, because taskgroup descriptors must be 3653 // privatized earlier. 3654 OMPPrivateScope InRedScope(CGF); 3655 if (!InRedVars.empty()) { 3656 ReductionCodeGen RedCG(InRedVars, InRedVars, InRedPrivs, InRedOps); 3657 for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { 3658 RedCG.emitSharedOrigLValue(CGF, Cnt); 3659 RedCG.emitAggregateType(CGF, Cnt); 3660 // The taskgroup descriptor variable is always implicit firstprivate and 3661 // privatized already during processing of the firstprivates. 3662 // FIXME: This must removed once the runtime library is fixed. 3663 // Emit required threadprivate variables for 3664 // initializer/combiner/finalizer. 3665 CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), 3666 RedCG, Cnt); 3667 llvm::Value *ReductionsPtr; 3668 if (const Expr *TRExpr = TaskgroupDescriptors[Cnt]) { 3669 ReductionsPtr = CGF.EmitLoadOfScalar(CGF.EmitLValue(TRExpr), 3670 TRExpr->getExprLoc()); 3671 } else { 3672 ReductionsPtr = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); 3673 } 3674 Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( 3675 CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); 3676 Replacement = Address( 3677 CGF.EmitScalarConversion( 3678 Replacement.getPointer(), CGF.getContext().VoidPtrTy, 3679 CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), 3680 InRedPrivs[Cnt]->getExprLoc()), 3681 Replacement.getAlignment()); 3682 Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); 3683 InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), 3684 [Replacement]() { return Replacement; }); 3685 } 3686 } 3687 (void)InRedScope.Privatize(); 3688 3689 Action.Enter(CGF); 3690 BodyGen(CGF); 3691 }; 3692 llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( 3693 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, 3694 Data.NumberOfParts); 3695 OMPLexicalScope Scope(*this, S, llvm::None, 3696 !isOpenMPParallelDirective(S.getDirectiveKind()) && 3697 !isOpenMPSimdDirective(S.getDirectiveKind())); 3698 TaskGen(*this, OutlinedFn, Data); 3699 } 3700 3701 static ImplicitParamDecl * 3702 createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, 3703 QualType Ty, CapturedDecl *CD, 3704 SourceLocation Loc) { 3705 auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, 3706 ImplicitParamDecl::Other); 3707 auto *OrigRef = DeclRefExpr::Create( 3708 C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, 3709 /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); 3710 auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, 3711 ImplicitParamDecl::Other); 3712 auto *PrivateRef = DeclRefExpr::Create( 3713 C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, 3714 /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); 3715 QualType ElemType = C.getBaseElementType(Ty); 3716 auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, 3717 ImplicitParamDecl::Other); 3718 auto *InitRef = DeclRefExpr::Create( 3719 C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, 3720 /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); 3721 PrivateVD->setInitStyle(VarDecl::CInit); 3722 PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, 3723 InitRef, /*BasePath=*/nullptr, 3724 VK_RValue)); 3725 Data.FirstprivateVars.emplace_back(OrigRef); 3726 Data.FirstprivateCopies.emplace_back(PrivateRef); 3727 Data.FirstprivateInits.emplace_back(InitRef); 3728 return OrigVD; 3729 } 3730 3731 void CodeGenFunction::EmitOMPTargetTaskBasedDirective( 3732 const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, 3733 OMPTargetDataInfo &InputInfo) { 3734 // Emit outlined function for task construct. 3735 const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); 3736 Address CapturedStruct = GenerateCapturedStmtArgument(*CS); 3737 QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 3738 auto I = CS->getCapturedDecl()->param_begin(); 3739 auto PartId = std::next(I); 3740 auto TaskT = std::next(I, 4); 3741 OMPTaskDataTy Data; 3742 // The task is not final. 3743 Data.Final.setInt(/*IntVal=*/false); 3744 // Get list of firstprivate variables. 3745 for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { 3746 auto IRef = C->varlist_begin(); 3747 auto IElemInitRef = C->inits().begin(); 3748 for (auto *IInit : C->private_copies()) { 3749 Data.FirstprivateVars.push_back(*IRef); 3750 Data.FirstprivateCopies.push_back(IInit); 3751 Data.FirstprivateInits.push_back(*IElemInitRef); 3752 ++IRef; 3753 ++IElemInitRef; 3754 } 3755 } 3756 OMPPrivateScope TargetScope(*this); 3757 VarDecl *BPVD = nullptr; 3758 VarDecl *PVD = nullptr; 3759 VarDecl *SVD = nullptr; 3760 if (InputInfo.NumberOfTargetItems > 0) { 3761 auto *CD = CapturedDecl::Create( 3762 getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); 3763 llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); 3764 QualType BaseAndPointersType = getContext().getConstantArrayType( 3765 getContext().VoidPtrTy, ArrSize, nullptr, ArrayType::Normal, 3766 /*IndexTypeQuals=*/0); 3767 BPVD = createImplicitFirstprivateForType( 3768 getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); 3769 PVD = createImplicitFirstprivateForType( 3770 getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); 3771 QualType SizesType = getContext().getConstantArrayType( 3772 getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1), 3773 ArrSize, nullptr, ArrayType::Normal, 3774 /*IndexTypeQuals=*/0); 3775 SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, 3776 S.getBeginLoc()); 3777 TargetScope.addPrivate( 3778 BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); 3779 TargetScope.addPrivate(PVD, 3780 [&InputInfo]() { return InputInfo.PointersArray; }); 3781 TargetScope.addPrivate(SVD, 3782 [&InputInfo]() { return InputInfo.SizesArray; }); 3783 } 3784 (void)TargetScope.Privatize(); 3785 // Build list of dependences. 3786 for (const auto *C : S.getClausesOfKind<OMPDependClause>()) { 3787 OMPTaskDataTy::DependData &DD = 3788 Data.Dependences.emplace_back(C->getDependencyKind(), C->getModifier()); 3789 DD.DepExprs.append(C->varlist_begin(), C->varlist_end()); 3790 } 3791 auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, 3792 &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { 3793 // Set proper addresses for generated private copies. 3794 OMPPrivateScope Scope(CGF); 3795 if (!Data.FirstprivateVars.empty()) { 3796 llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( 3797 CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); 3798 enum { PrivatesParam = 2, CopyFnParam = 3 }; 3799 llvm::Value *CopyFn = CGF.Builder.CreateLoad( 3800 CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); 3801 llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( 3802 CS->getCapturedDecl()->getParam(PrivatesParam))); 3803 // Map privates. 3804 llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; 3805 llvm::SmallVector<llvm::Value *, 16> CallArgs; 3806 CallArgs.push_back(PrivatesPtr); 3807 for (const Expr *E : Data.FirstprivateVars) { 3808 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 3809 Address PrivatePtr = 3810 CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), 3811 ".firstpriv.ptr.addr"); 3812 PrivatePtrs.emplace_back(VD, PrivatePtr); 3813 CallArgs.push_back(PrivatePtr.getPointer()); 3814 } 3815 CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( 3816 CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); 3817 for (const auto &Pair : PrivatePtrs) { 3818 Address Replacement(CGF.Builder.CreateLoad(Pair.second), 3819 CGF.getContext().getDeclAlign(Pair.first)); 3820 Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); 3821 } 3822 } 3823 // Privatize all private variables except for in_reduction items. 3824 (void)Scope.Privatize(); 3825 if (InputInfo.NumberOfTargetItems > 0) { 3826 InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( 3827 CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0); 3828 InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( 3829 CGF.GetAddrOfLocalVar(PVD), /*Index=*/0); 3830 InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( 3831 CGF.GetAddrOfLocalVar(SVD), /*Index=*/0); 3832 } 3833 3834 Action.Enter(CGF); 3835 OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); 3836 BodyGen(CGF); 3837 }; 3838 llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( 3839 S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, 3840 Data.NumberOfParts); 3841 llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); 3842 IntegerLiteral IfCond(getContext(), TrueOrFalse, 3843 getContext().getIntTypeForBitwidth(32, /*Signed=*/0), 3844 SourceLocation()); 3845 3846 CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn, 3847 SharedsTy, CapturedStruct, &IfCond, Data); 3848 } 3849 3850 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { 3851 // Emit outlined function for task construct. 3852 const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); 3853 Address CapturedStruct = GenerateCapturedStmtArgument(*CS); 3854 QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 3855 const Expr *IfCond = nullptr; 3856 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 3857 if (C->getNameModifier() == OMPD_unknown || 3858 C->getNameModifier() == OMPD_task) { 3859 IfCond = C->getCondition(); 3860 break; 3861 } 3862 } 3863 3864 OMPTaskDataTy Data; 3865 // Check if we should emit tied or untied task. 3866 Data.Tied = !S.getSingleClause<OMPUntiedClause>(); 3867 auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { 3868 CGF.EmitStmt(CS->getCapturedStmt()); 3869 }; 3870 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 3871 IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, 3872 const OMPTaskDataTy &Data) { 3873 CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn, 3874 SharedsTy, CapturedStruct, IfCond, 3875 Data); 3876 }; 3877 auto LPCRegion = 3878 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 3879 EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); 3880 } 3881 3882 void CodeGenFunction::EmitOMPTaskyieldDirective( 3883 const OMPTaskyieldDirective &S) { 3884 CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc()); 3885 } 3886 3887 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { 3888 CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier); 3889 } 3890 3891 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { 3892 CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc()); 3893 } 3894 3895 void CodeGenFunction::EmitOMPTaskgroupDirective( 3896 const OMPTaskgroupDirective &S) { 3897 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 3898 Action.Enter(CGF); 3899 if (const Expr *E = S.getReductionRef()) { 3900 SmallVector<const Expr *, 4> LHSs; 3901 SmallVector<const Expr *, 4> RHSs; 3902 OMPTaskDataTy Data; 3903 for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { 3904 Data.ReductionVars.append(C->varlist_begin(), C->varlist_end()); 3905 Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end()); 3906 Data.ReductionCopies.append(C->privates().begin(), C->privates().end()); 3907 Data.ReductionOps.append(C->reduction_ops().begin(), 3908 C->reduction_ops().end()); 3909 LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); 3910 RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); 3911 } 3912 llvm::Value *ReductionDesc = 3913 CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(), 3914 LHSs, RHSs, Data); 3915 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 3916 CGF.EmitVarDecl(*VD); 3917 CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), 3918 /*Volatile=*/false, E->getType()); 3919 } 3920 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 3921 }; 3922 OMPLexicalScope Scope(*this, S, OMPD_unknown); 3923 CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc()); 3924 } 3925 3926 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { 3927 llvm::AtomicOrdering AO = S.getSingleClause<OMPFlushClause>() 3928 ? llvm::AtomicOrdering::NotAtomic 3929 : llvm::AtomicOrdering::AcquireRelease; 3930 CGM.getOpenMPRuntime().emitFlush( 3931 *this, 3932 [&S]() -> ArrayRef<const Expr *> { 3933 if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) 3934 return llvm::makeArrayRef(FlushClause->varlist_begin(), 3935 FlushClause->varlist_end()); 3936 return llvm::None; 3937 }(), 3938 S.getBeginLoc(), AO); 3939 } 3940 3941 void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) { 3942 const auto *DO = S.getSingleClause<OMPDepobjClause>(); 3943 LValue DOLVal = EmitLValue(DO->getDepobj()); 3944 if (const auto *DC = S.getSingleClause<OMPDependClause>()) { 3945 OMPTaskDataTy::DependData Dependencies(DC->getDependencyKind(), 3946 DC->getModifier()); 3947 Dependencies.DepExprs.append(DC->varlist_begin(), DC->varlist_end()); 3948 Address DepAddr = CGM.getOpenMPRuntime().emitDepobjDependClause( 3949 *this, Dependencies, DC->getBeginLoc()); 3950 EmitStoreOfScalar(DepAddr.getPointer(), DOLVal); 3951 return; 3952 } 3953 if (const auto *DC = S.getSingleClause<OMPDestroyClause>()) { 3954 CGM.getOpenMPRuntime().emitDestroyClause(*this, DOLVal, DC->getBeginLoc()); 3955 return; 3956 } 3957 if (const auto *UC = S.getSingleClause<OMPUpdateClause>()) { 3958 CGM.getOpenMPRuntime().emitUpdateClause( 3959 *this, DOLVal, UC->getDependencyKind(), UC->getBeginLoc()); 3960 return; 3961 } 3962 } 3963 3964 void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, 3965 const CodeGenLoopTy &CodeGenLoop, 3966 Expr *IncExpr) { 3967 // Emit the loop iteration variable. 3968 const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); 3969 const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); 3970 EmitVarDecl(*IVDecl); 3971 3972 // Emit the iterations count variable. 3973 // If it is not a variable, Sema decided to calculate iterations count on each 3974 // iteration (e.g., it is foldable into a constant). 3975 if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 3976 EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 3977 // Emit calculation of the iterations count. 3978 EmitIgnoredExpr(S.getCalcLastIteration()); 3979 } 3980 3981 CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); 3982 3983 bool HasLastprivateClause = false; 3984 // Check pre-condition. 3985 { 3986 OMPLoopScope PreInitScope(*this, S); 3987 // Skip the entire loop if we don't meet the precondition. 3988 // If the condition constant folds and can be elided, avoid emitting the 3989 // whole loop. 3990 bool CondConstant; 3991 llvm::BasicBlock *ContBlock = nullptr; 3992 if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 3993 if (!CondConstant) 3994 return; 3995 } else { 3996 llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); 3997 ContBlock = createBasicBlock("omp.precond.end"); 3998 emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, 3999 getProfileCount(&S)); 4000 EmitBlock(ThenBlock); 4001 incrementProfileCounter(&S); 4002 } 4003 4004 emitAlignedClause(*this, S); 4005 // Emit 'then' code. 4006 { 4007 // Emit helper vars inits. 4008 4009 LValue LB = EmitOMPHelperVar( 4010 *this, cast<DeclRefExpr>( 4011 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 4012 ? S.getCombinedLowerBoundVariable() 4013 : S.getLowerBoundVariable()))); 4014 LValue UB = EmitOMPHelperVar( 4015 *this, cast<DeclRefExpr>( 4016 (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 4017 ? S.getCombinedUpperBoundVariable() 4018 : S.getUpperBoundVariable()))); 4019 LValue ST = 4020 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); 4021 LValue IL = 4022 EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); 4023 4024 OMPPrivateScope LoopScope(*this); 4025 if (EmitOMPFirstprivateClause(S, LoopScope)) { 4026 // Emit implicit barrier to synchronize threads and avoid data races 4027 // on initialization of firstprivate variables and post-update of 4028 // lastprivate variables. 4029 CGM.getOpenMPRuntime().emitBarrierCall( 4030 *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, 4031 /*ForceSimpleCall=*/true); 4032 } 4033 EmitOMPPrivateClause(S, LoopScope); 4034 if (isOpenMPSimdDirective(S.getDirectiveKind()) && 4035 !isOpenMPParallelDirective(S.getDirectiveKind()) && 4036 !isOpenMPTeamsDirective(S.getDirectiveKind())) 4037 EmitOMPReductionClauseInit(S, LoopScope); 4038 HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); 4039 EmitOMPPrivateLoopCounters(S, LoopScope); 4040 (void)LoopScope.Privatize(); 4041 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 4042 CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); 4043 4044 // Detect the distribute schedule kind and chunk. 4045 llvm::Value *Chunk = nullptr; 4046 OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; 4047 if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) { 4048 ScheduleKind = C->getDistScheduleKind(); 4049 if (const Expr *Ch = C->getChunkSize()) { 4050 Chunk = EmitScalarExpr(Ch); 4051 Chunk = EmitScalarConversion(Chunk, Ch->getType(), 4052 S.getIterationVariable()->getType(), 4053 S.getBeginLoc()); 4054 } 4055 } else { 4056 // Default behaviour for dist_schedule clause. 4057 CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk( 4058 *this, S, ScheduleKind, Chunk); 4059 } 4060 const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); 4061 const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); 4062 4063 // OpenMP [2.10.8, distribute Construct, Description] 4064 // If dist_schedule is specified, kind must be static. If specified, 4065 // iterations are divided into chunks of size chunk_size, chunks are 4066 // assigned to the teams of the league in a round-robin fashion in the 4067 // order of the team number. When no chunk_size is specified, the 4068 // iteration space is divided into chunks that are approximately equal 4069 // in size, and at most one chunk is distributed to each team of the 4070 // league. The size of the chunks is unspecified in this case. 4071 bool StaticChunked = RT.isStaticChunked( 4072 ScheduleKind, /* Chunked */ Chunk != nullptr) && 4073 isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); 4074 if (RT.isStaticNonchunked(ScheduleKind, 4075 /* Chunked */ Chunk != nullptr) || 4076 StaticChunked) { 4077 CGOpenMPRuntime::StaticRTInput StaticInit( 4078 IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(*this), 4079 LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), 4080 StaticChunked ? Chunk : nullptr); 4081 RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, 4082 StaticInit); 4083 JumpDest LoopExit = 4084 getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); 4085 // UB = min(UB, GlobalUB); 4086 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 4087 ? S.getCombinedEnsureUpperBound() 4088 : S.getEnsureUpperBound()); 4089 // IV = LB; 4090 EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 4091 ? S.getCombinedInit() 4092 : S.getInit()); 4093 4094 const Expr *Cond = 4095 isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) 4096 ? S.getCombinedCond() 4097 : S.getCond(); 4098 4099 if (StaticChunked) 4100 Cond = S.getCombinedDistCond(); 4101 4102 // For static unchunked schedules generate: 4103 // 4104 // 1. For distribute alone, codegen 4105 // while (idx <= UB) { 4106 // BODY; 4107 // ++idx; 4108 // } 4109 // 4110 // 2. When combined with 'for' (e.g. as in 'distribute parallel for') 4111 // while (idx <= UB) { 4112 // <CodeGen rest of pragma>(LB, UB); 4113 // idx += ST; 4114 // } 4115 // 4116 // For static chunk one schedule generate: 4117 // 4118 // while (IV <= GlobalUB) { 4119 // <CodeGen rest of pragma>(LB, UB); 4120 // LB += ST; 4121 // UB += ST; 4122 // UB = min(UB, GlobalUB); 4123 // IV = LB; 4124 // } 4125 // 4126 emitCommonSimdLoop( 4127 *this, S, 4128 [&S](CodeGenFunction &CGF, PrePostActionTy &) { 4129 if (isOpenMPSimdDirective(S.getDirectiveKind())) 4130 CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true); 4131 }, 4132 [&S, &LoopScope, Cond, IncExpr, LoopExit, &CodeGenLoop, 4133 StaticChunked](CodeGenFunction &CGF, PrePostActionTy &) { 4134 CGF.EmitOMPInnerLoop( 4135 S, LoopScope.requiresCleanups(), Cond, IncExpr, 4136 [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { 4137 CodeGenLoop(CGF, S, LoopExit); 4138 }, 4139 [&S, StaticChunked](CodeGenFunction &CGF) { 4140 if (StaticChunked) { 4141 CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound()); 4142 CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound()); 4143 CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound()); 4144 CGF.EmitIgnoredExpr(S.getCombinedInit()); 4145 } 4146 }); 4147 }); 4148 EmitBlock(LoopExit.getBlock()); 4149 // Tell the runtime we are done. 4150 RT.emitForStaticFinish(*this, S.getEndLoc(), S.getDirectiveKind()); 4151 } else { 4152 // Emit the outer loop, which requests its work chunk [LB..UB] from 4153 // runtime and runs the inner loop to process it. 4154 const OMPLoopArguments LoopArguments = { 4155 LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), 4156 IL.getAddress(*this), Chunk}; 4157 EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, 4158 CodeGenLoop); 4159 } 4160 if (isOpenMPSimdDirective(S.getDirectiveKind())) { 4161 EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { 4162 return CGF.Builder.CreateIsNotNull( 4163 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 4164 }); 4165 } 4166 if (isOpenMPSimdDirective(S.getDirectiveKind()) && 4167 !isOpenMPParallelDirective(S.getDirectiveKind()) && 4168 !isOpenMPTeamsDirective(S.getDirectiveKind())) { 4169 EmitOMPReductionClauseFinal(S, OMPD_simd); 4170 // Emit post-update of the reduction variables if IsLastIter != 0. 4171 emitPostUpdateForReductionClause( 4172 *this, S, [IL, &S](CodeGenFunction &CGF) { 4173 return CGF.Builder.CreateIsNotNull( 4174 CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); 4175 }); 4176 } 4177 // Emit final copy of the lastprivate variables if IsLastIter != 0. 4178 if (HasLastprivateClause) { 4179 EmitOMPLastprivateClauseFinal( 4180 S, /*NoFinals=*/false, 4181 Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); 4182 } 4183 } 4184 4185 // We're now done with the loop, so jump to the continuation block. 4186 if (ContBlock) { 4187 EmitBranch(ContBlock); 4188 EmitBlock(ContBlock, true); 4189 } 4190 } 4191 } 4192 4193 void CodeGenFunction::EmitOMPDistributeDirective( 4194 const OMPDistributeDirective &S) { 4195 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 4196 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 4197 }; 4198 OMPLexicalScope Scope(*this, S, OMPD_unknown); 4199 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); 4200 } 4201 4202 static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, 4203 const CapturedStmt *S, 4204 SourceLocation Loc) { 4205 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); 4206 CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; 4207 CGF.CapturedStmtInfo = &CapStmtInfo; 4208 llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S, Loc); 4209 Fn->setDoesNotRecurse(); 4210 return Fn; 4211 } 4212 4213 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { 4214 if (S.hasClausesOfKind<OMPDependClause>()) { 4215 assert(!S.getAssociatedStmt() && 4216 "No associated statement must be in ordered depend construct."); 4217 for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) 4218 CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); 4219 return; 4220 } 4221 const auto *C = S.getSingleClause<OMPSIMDClause>(); 4222 auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, 4223 PrePostActionTy &Action) { 4224 const CapturedStmt *CS = S.getInnermostCapturedStmt(); 4225 if (C) { 4226 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 4227 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 4228 llvm::Function *OutlinedFn = 4229 emitOutlinedOrderedFunction(CGM, CS, S.getBeginLoc()); 4230 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), 4231 OutlinedFn, CapturedVars); 4232 } else { 4233 Action.Enter(CGF); 4234 CGF.EmitStmt(CS->getCapturedStmt()); 4235 } 4236 }; 4237 OMPLexicalScope Scope(*this, S, OMPD_unknown); 4238 CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C); 4239 } 4240 4241 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, 4242 QualType SrcType, QualType DestType, 4243 SourceLocation Loc) { 4244 assert(CGF.hasScalarEvaluationKind(DestType) && 4245 "DestType must have scalar evaluation kind."); 4246 assert(!Val.isAggregate() && "Must be a scalar or complex."); 4247 return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, 4248 DestType, Loc) 4249 : CGF.EmitComplexToScalarConversion( 4250 Val.getComplexVal(), SrcType, DestType, Loc); 4251 } 4252 4253 static CodeGenFunction::ComplexPairTy 4254 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, 4255 QualType DestType, SourceLocation Loc) { 4256 assert(CGF.getEvaluationKind(DestType) == TEK_Complex && 4257 "DestType must have complex evaluation kind."); 4258 CodeGenFunction::ComplexPairTy ComplexVal; 4259 if (Val.isScalar()) { 4260 // Convert the input element to the element type of the complex. 4261 QualType DestElementType = 4262 DestType->castAs<ComplexType>()->getElementType(); 4263 llvm::Value *ScalarVal = CGF.EmitScalarConversion( 4264 Val.getScalarVal(), SrcType, DestElementType, Loc); 4265 ComplexVal = CodeGenFunction::ComplexPairTy( 4266 ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); 4267 } else { 4268 assert(Val.isComplex() && "Must be a scalar or complex."); 4269 QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); 4270 QualType DestElementType = 4271 DestType->castAs<ComplexType>()->getElementType(); 4272 ComplexVal.first = CGF.EmitScalarConversion( 4273 Val.getComplexVal().first, SrcElementType, DestElementType, Loc); 4274 ComplexVal.second = CGF.EmitScalarConversion( 4275 Val.getComplexVal().second, SrcElementType, DestElementType, Loc); 4276 } 4277 return ComplexVal; 4278 } 4279 4280 static void emitSimpleAtomicStore(CodeGenFunction &CGF, llvm::AtomicOrdering AO, 4281 LValue LVal, RValue RVal) { 4282 if (LVal.isGlobalReg()) 4283 CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); 4284 else 4285 CGF.EmitAtomicStore(RVal, LVal, AO, LVal.isVolatile(), /*isInit=*/false); 4286 } 4287 4288 static RValue emitSimpleAtomicLoad(CodeGenFunction &CGF, 4289 llvm::AtomicOrdering AO, LValue LVal, 4290 SourceLocation Loc) { 4291 if (LVal.isGlobalReg()) 4292 return CGF.EmitLoadOfLValue(LVal, Loc); 4293 return CGF.EmitAtomicLoad( 4294 LVal, Loc, llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO), 4295 LVal.isVolatile()); 4296 } 4297 4298 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, 4299 QualType RValTy, SourceLocation Loc) { 4300 switch (getEvaluationKind(LVal.getType())) { 4301 case TEK_Scalar: 4302 EmitStoreThroughLValue(RValue::get(convertToScalarValue( 4303 *this, RVal, RValTy, LVal.getType(), Loc)), 4304 LVal); 4305 break; 4306 case TEK_Complex: 4307 EmitStoreOfComplex( 4308 convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, 4309 /*isInit=*/false); 4310 break; 4311 case TEK_Aggregate: 4312 llvm_unreachable("Must be a scalar or complex."); 4313 } 4314 } 4315 4316 static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO, 4317 const Expr *X, const Expr *V, 4318 SourceLocation Loc) { 4319 // v = x; 4320 assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); 4321 assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); 4322 LValue XLValue = CGF.EmitLValue(X); 4323 LValue VLValue = CGF.EmitLValue(V); 4324 RValue Res = emitSimpleAtomicLoad(CGF, AO, XLValue, Loc); 4325 // OpenMP, 2.17.7, atomic Construct 4326 // If the read or capture clause is specified and the acquire, acq_rel, or 4327 // seq_cst clause is specified then the strong flush on exit from the atomic 4328 // operation is also an acquire flush. 4329 switch (AO) { 4330 case llvm::AtomicOrdering::Acquire: 4331 case llvm::AtomicOrdering::AcquireRelease: 4332 case llvm::AtomicOrdering::SequentiallyConsistent: 4333 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4334 llvm::AtomicOrdering::Acquire); 4335 break; 4336 case llvm::AtomicOrdering::Monotonic: 4337 case llvm::AtomicOrdering::Release: 4338 break; 4339 case llvm::AtomicOrdering::NotAtomic: 4340 case llvm::AtomicOrdering::Unordered: 4341 llvm_unreachable("Unexpected ordering."); 4342 } 4343 CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); 4344 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V); 4345 } 4346 4347 static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF, 4348 llvm::AtomicOrdering AO, const Expr *X, 4349 const Expr *E, SourceLocation Loc) { 4350 // x = expr; 4351 assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); 4352 emitSimpleAtomicStore(CGF, AO, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); 4353 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); 4354 // OpenMP, 2.17.7, atomic Construct 4355 // If the write, update, or capture clause is specified and the release, 4356 // acq_rel, or seq_cst clause is specified then the strong flush on entry to 4357 // the atomic operation is also a release flush. 4358 switch (AO) { 4359 case llvm::AtomicOrdering::Release: 4360 case llvm::AtomicOrdering::AcquireRelease: 4361 case llvm::AtomicOrdering::SequentiallyConsistent: 4362 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4363 llvm::AtomicOrdering::Release); 4364 break; 4365 case llvm::AtomicOrdering::Acquire: 4366 case llvm::AtomicOrdering::Monotonic: 4367 break; 4368 case llvm::AtomicOrdering::NotAtomic: 4369 case llvm::AtomicOrdering::Unordered: 4370 llvm_unreachable("Unexpected ordering."); 4371 } 4372 } 4373 4374 static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, 4375 RValue Update, 4376 BinaryOperatorKind BO, 4377 llvm::AtomicOrdering AO, 4378 bool IsXLHSInRHSPart) { 4379 ASTContext &Context = CGF.getContext(); 4380 // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x' 4381 // expression is simple and atomic is allowed for the given type for the 4382 // target platform. 4383 if (BO == BO_Comma || !Update.isScalar() || 4384 !Update.getScalarVal()->getType()->isIntegerTy() || !X.isSimple() || 4385 (!isa<llvm::ConstantInt>(Update.getScalarVal()) && 4386 (Update.getScalarVal()->getType() != 4387 X.getAddress(CGF).getElementType())) || 4388 !X.getAddress(CGF).getElementType()->isIntegerTy() || 4389 !Context.getTargetInfo().hasBuiltinAtomic( 4390 Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) 4391 return std::make_pair(false, RValue::get(nullptr)); 4392 4393 llvm::AtomicRMWInst::BinOp RMWOp; 4394 switch (BO) { 4395 case BO_Add: 4396 RMWOp = llvm::AtomicRMWInst::Add; 4397 break; 4398 case BO_Sub: 4399 if (!IsXLHSInRHSPart) 4400 return std::make_pair(false, RValue::get(nullptr)); 4401 RMWOp = llvm::AtomicRMWInst::Sub; 4402 break; 4403 case BO_And: 4404 RMWOp = llvm::AtomicRMWInst::And; 4405 break; 4406 case BO_Or: 4407 RMWOp = llvm::AtomicRMWInst::Or; 4408 break; 4409 case BO_Xor: 4410 RMWOp = llvm::AtomicRMWInst::Xor; 4411 break; 4412 case BO_LT: 4413 RMWOp = X.getType()->hasSignedIntegerRepresentation() 4414 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min 4415 : llvm::AtomicRMWInst::Max) 4416 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin 4417 : llvm::AtomicRMWInst::UMax); 4418 break; 4419 case BO_GT: 4420 RMWOp = X.getType()->hasSignedIntegerRepresentation() 4421 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max 4422 : llvm::AtomicRMWInst::Min) 4423 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax 4424 : llvm::AtomicRMWInst::UMin); 4425 break; 4426 case BO_Assign: 4427 RMWOp = llvm::AtomicRMWInst::Xchg; 4428 break; 4429 case BO_Mul: 4430 case BO_Div: 4431 case BO_Rem: 4432 case BO_Shl: 4433 case BO_Shr: 4434 case BO_LAnd: 4435 case BO_LOr: 4436 return std::make_pair(false, RValue::get(nullptr)); 4437 case BO_PtrMemD: 4438 case BO_PtrMemI: 4439 case BO_LE: 4440 case BO_GE: 4441 case BO_EQ: 4442 case BO_NE: 4443 case BO_Cmp: 4444 case BO_AddAssign: 4445 case BO_SubAssign: 4446 case BO_AndAssign: 4447 case BO_OrAssign: 4448 case BO_XorAssign: 4449 case BO_MulAssign: 4450 case BO_DivAssign: 4451 case BO_RemAssign: 4452 case BO_ShlAssign: 4453 case BO_ShrAssign: 4454 case BO_Comma: 4455 llvm_unreachable("Unsupported atomic update operation"); 4456 } 4457 llvm::Value *UpdateVal = Update.getScalarVal(); 4458 if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { 4459 UpdateVal = CGF.Builder.CreateIntCast( 4460 IC, X.getAddress(CGF).getElementType(), 4461 X.getType()->hasSignedIntegerRepresentation()); 4462 } 4463 llvm::Value *Res = 4464 CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(CGF), UpdateVal, AO); 4465 return std::make_pair(true, RValue::get(Res)); 4466 } 4467 4468 std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( 4469 LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, 4470 llvm::AtomicOrdering AO, SourceLocation Loc, 4471 const llvm::function_ref<RValue(RValue)> CommonGen) { 4472 // Update expressions are allowed to have the following forms: 4473 // x binop= expr; -> xrval + expr; 4474 // x++, ++x -> xrval + 1; 4475 // x--, --x -> xrval - 1; 4476 // x = x binop expr; -> xrval binop expr 4477 // x = expr Op x; - > expr binop xrval; 4478 auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); 4479 if (!Res.first) { 4480 if (X.isGlobalReg()) { 4481 // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop 4482 // 'xrval'. 4483 EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); 4484 } else { 4485 // Perform compare-and-swap procedure. 4486 EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); 4487 } 4488 } 4489 return Res; 4490 } 4491 4492 static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF, 4493 llvm::AtomicOrdering AO, const Expr *X, 4494 const Expr *E, const Expr *UE, 4495 bool IsXLHSInRHSPart, SourceLocation Loc) { 4496 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 4497 "Update expr in 'atomic update' must be a binary operator."); 4498 const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 4499 // Update expressions are allowed to have the following forms: 4500 // x binop= expr; -> xrval + expr; 4501 // x++, ++x -> xrval + 1; 4502 // x--, --x -> xrval - 1; 4503 // x = x binop expr; -> xrval binop expr 4504 // x = expr Op x; - > expr binop xrval; 4505 assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); 4506 LValue XLValue = CGF.EmitLValue(X); 4507 RValue ExprRValue = CGF.EmitAnyExpr(E); 4508 const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 4509 const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 4510 const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 4511 const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 4512 auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) { 4513 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 4514 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 4515 return CGF.EmitAnyExpr(UE); 4516 }; 4517 (void)CGF.EmitOMPAtomicSimpleUpdateExpr( 4518 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 4519 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); 4520 // OpenMP, 2.17.7, atomic Construct 4521 // If the write, update, or capture clause is specified and the release, 4522 // acq_rel, or seq_cst clause is specified then the strong flush on entry to 4523 // the atomic operation is also a release flush. 4524 switch (AO) { 4525 case llvm::AtomicOrdering::Release: 4526 case llvm::AtomicOrdering::AcquireRelease: 4527 case llvm::AtomicOrdering::SequentiallyConsistent: 4528 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4529 llvm::AtomicOrdering::Release); 4530 break; 4531 case llvm::AtomicOrdering::Acquire: 4532 case llvm::AtomicOrdering::Monotonic: 4533 break; 4534 case llvm::AtomicOrdering::NotAtomic: 4535 case llvm::AtomicOrdering::Unordered: 4536 llvm_unreachable("Unexpected ordering."); 4537 } 4538 } 4539 4540 static RValue convertToType(CodeGenFunction &CGF, RValue Value, 4541 QualType SourceType, QualType ResType, 4542 SourceLocation Loc) { 4543 switch (CGF.getEvaluationKind(ResType)) { 4544 case TEK_Scalar: 4545 return RValue::get( 4546 convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); 4547 case TEK_Complex: { 4548 auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); 4549 return RValue::getComplex(Res.first, Res.second); 4550 } 4551 case TEK_Aggregate: 4552 break; 4553 } 4554 llvm_unreachable("Must be a scalar or complex."); 4555 } 4556 4557 static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF, 4558 llvm::AtomicOrdering AO, 4559 bool IsPostfixUpdate, const Expr *V, 4560 const Expr *X, const Expr *E, 4561 const Expr *UE, bool IsXLHSInRHSPart, 4562 SourceLocation Loc) { 4563 assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); 4564 assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); 4565 RValue NewVVal; 4566 LValue VLValue = CGF.EmitLValue(V); 4567 LValue XLValue = CGF.EmitLValue(X); 4568 RValue ExprRValue = CGF.EmitAnyExpr(E); 4569 QualType NewVValType; 4570 if (UE) { 4571 // 'x' is updated with some additional value. 4572 assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && 4573 "Update expr in 'atomic capture' must be a binary operator."); 4574 const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); 4575 // Update expressions are allowed to have the following forms: 4576 // x binop= expr; -> xrval + expr; 4577 // x++, ++x -> xrval + 1; 4578 // x--, --x -> xrval - 1; 4579 // x = x binop expr; -> xrval binop expr 4580 // x = expr Op x; - > expr binop xrval; 4581 const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); 4582 const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); 4583 const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; 4584 NewVValType = XRValExpr->getType(); 4585 const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; 4586 auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, 4587 IsPostfixUpdate](RValue XRValue) { 4588 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 4589 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); 4590 RValue Res = CGF.EmitAnyExpr(UE); 4591 NewVVal = IsPostfixUpdate ? XRValue : Res; 4592 return Res; 4593 }; 4594 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 4595 XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); 4596 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); 4597 if (Res.first) { 4598 // 'atomicrmw' instruction was generated. 4599 if (IsPostfixUpdate) { 4600 // Use old value from 'atomicrmw'. 4601 NewVVal = Res.second; 4602 } else { 4603 // 'atomicrmw' does not provide new value, so evaluate it using old 4604 // value of 'x'. 4605 CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); 4606 CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); 4607 NewVVal = CGF.EmitAnyExpr(UE); 4608 } 4609 } 4610 } else { 4611 // 'x' is simply rewritten with some 'expr'. 4612 NewVValType = X->getType().getNonReferenceType(); 4613 ExprRValue = convertToType(CGF, ExprRValue, E->getType(), 4614 X->getType().getNonReferenceType(), Loc); 4615 auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) { 4616 NewVVal = XRValue; 4617 return ExprRValue; 4618 }; 4619 // Try to perform atomicrmw xchg, otherwise simple exchange. 4620 auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( 4621 XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, 4622 Loc, Gen); 4623 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); 4624 if (Res.first) { 4625 // 'atomicrmw' instruction was generated. 4626 NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; 4627 } 4628 } 4629 // Emit post-update store to 'v' of old/new 'x' value. 4630 CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); 4631 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V); 4632 // OpenMP, 2.17.7, atomic Construct 4633 // If the write, update, or capture clause is specified and the release, 4634 // acq_rel, or seq_cst clause is specified then the strong flush on entry to 4635 // the atomic operation is also a release flush. 4636 // If the read or capture clause is specified and the acquire, acq_rel, or 4637 // seq_cst clause is specified then the strong flush on exit from the atomic 4638 // operation is also an acquire flush. 4639 switch (AO) { 4640 case llvm::AtomicOrdering::Release: 4641 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4642 llvm::AtomicOrdering::Release); 4643 break; 4644 case llvm::AtomicOrdering::Acquire: 4645 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4646 llvm::AtomicOrdering::Acquire); 4647 break; 4648 case llvm::AtomicOrdering::AcquireRelease: 4649 case llvm::AtomicOrdering::SequentiallyConsistent: 4650 CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, 4651 llvm::AtomicOrdering::AcquireRelease); 4652 break; 4653 case llvm::AtomicOrdering::Monotonic: 4654 break; 4655 case llvm::AtomicOrdering::NotAtomic: 4656 case llvm::AtomicOrdering::Unordered: 4657 llvm_unreachable("Unexpected ordering."); 4658 } 4659 } 4660 4661 static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, 4662 llvm::AtomicOrdering AO, bool IsPostfixUpdate, 4663 const Expr *X, const Expr *V, const Expr *E, 4664 const Expr *UE, bool IsXLHSInRHSPart, 4665 SourceLocation Loc) { 4666 switch (Kind) { 4667 case OMPC_read: 4668 emitOMPAtomicReadExpr(CGF, AO, X, V, Loc); 4669 break; 4670 case OMPC_write: 4671 emitOMPAtomicWriteExpr(CGF, AO, X, E, Loc); 4672 break; 4673 case OMPC_unknown: 4674 case OMPC_update: 4675 emitOMPAtomicUpdateExpr(CGF, AO, X, E, UE, IsXLHSInRHSPart, Loc); 4676 break; 4677 case OMPC_capture: 4678 emitOMPAtomicCaptureExpr(CGF, AO, IsPostfixUpdate, V, X, E, UE, 4679 IsXLHSInRHSPart, Loc); 4680 break; 4681 case OMPC_if: 4682 case OMPC_final: 4683 case OMPC_num_threads: 4684 case OMPC_private: 4685 case OMPC_firstprivate: 4686 case OMPC_lastprivate: 4687 case OMPC_reduction: 4688 case OMPC_task_reduction: 4689 case OMPC_in_reduction: 4690 case OMPC_safelen: 4691 case OMPC_simdlen: 4692 case OMPC_allocator: 4693 case OMPC_allocate: 4694 case OMPC_collapse: 4695 case OMPC_default: 4696 case OMPC_seq_cst: 4697 case OMPC_acq_rel: 4698 case OMPC_acquire: 4699 case OMPC_release: 4700 case OMPC_relaxed: 4701 case OMPC_shared: 4702 case OMPC_linear: 4703 case OMPC_aligned: 4704 case OMPC_copyin: 4705 case OMPC_copyprivate: 4706 case OMPC_flush: 4707 case OMPC_depobj: 4708 case OMPC_proc_bind: 4709 case OMPC_schedule: 4710 case OMPC_ordered: 4711 case OMPC_nowait: 4712 case OMPC_untied: 4713 case OMPC_threadprivate: 4714 case OMPC_depend: 4715 case OMPC_mergeable: 4716 case OMPC_device: 4717 case OMPC_threads: 4718 case OMPC_simd: 4719 case OMPC_map: 4720 case OMPC_num_teams: 4721 case OMPC_thread_limit: 4722 case OMPC_priority: 4723 case OMPC_grainsize: 4724 case OMPC_nogroup: 4725 case OMPC_num_tasks: 4726 case OMPC_hint: 4727 case OMPC_dist_schedule: 4728 case OMPC_defaultmap: 4729 case OMPC_uniform: 4730 case OMPC_to: 4731 case OMPC_from: 4732 case OMPC_use_device_ptr: 4733 case OMPC_use_device_addr: 4734 case OMPC_is_device_ptr: 4735 case OMPC_unified_address: 4736 case OMPC_unified_shared_memory: 4737 case OMPC_reverse_offload: 4738 case OMPC_dynamic_allocators: 4739 case OMPC_atomic_default_mem_order: 4740 case OMPC_device_type: 4741 case OMPC_match: 4742 case OMPC_nontemporal: 4743 case OMPC_order: 4744 case OMPC_destroy: 4745 case OMPC_detach: 4746 case OMPC_inclusive: 4747 case OMPC_exclusive: 4748 case OMPC_uses_allocators: 4749 case OMPC_affinity: 4750 llvm_unreachable("Clause is not allowed in 'omp atomic'."); 4751 } 4752 } 4753 4754 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { 4755 llvm::AtomicOrdering AO = llvm::AtomicOrdering::Monotonic; 4756 bool MemOrderingSpecified = false; 4757 if (S.getSingleClause<OMPSeqCstClause>()) { 4758 AO = llvm::AtomicOrdering::SequentiallyConsistent; 4759 MemOrderingSpecified = true; 4760 } else if (S.getSingleClause<OMPAcqRelClause>()) { 4761 AO = llvm::AtomicOrdering::AcquireRelease; 4762 MemOrderingSpecified = true; 4763 } else if (S.getSingleClause<OMPAcquireClause>()) { 4764 AO = llvm::AtomicOrdering::Acquire; 4765 MemOrderingSpecified = true; 4766 } else if (S.getSingleClause<OMPReleaseClause>()) { 4767 AO = llvm::AtomicOrdering::Release; 4768 MemOrderingSpecified = true; 4769 } else if (S.getSingleClause<OMPRelaxedClause>()) { 4770 AO = llvm::AtomicOrdering::Monotonic; 4771 MemOrderingSpecified = true; 4772 } 4773 OpenMPClauseKind Kind = OMPC_unknown; 4774 for (const OMPClause *C : S.clauses()) { 4775 // Find first clause (skip seq_cst|acq_rel|aqcuire|release|relaxed clause, 4776 // if it is first). 4777 if (C->getClauseKind() != OMPC_seq_cst && 4778 C->getClauseKind() != OMPC_acq_rel && 4779 C->getClauseKind() != OMPC_acquire && 4780 C->getClauseKind() != OMPC_release && 4781 C->getClauseKind() != OMPC_relaxed) { 4782 Kind = C->getClauseKind(); 4783 break; 4784 } 4785 } 4786 if (!MemOrderingSpecified) { 4787 llvm::AtomicOrdering DefaultOrder = 4788 CGM.getOpenMPRuntime().getDefaultMemoryOrdering(); 4789 if (DefaultOrder == llvm::AtomicOrdering::Monotonic || 4790 DefaultOrder == llvm::AtomicOrdering::SequentiallyConsistent || 4791 (DefaultOrder == llvm::AtomicOrdering::AcquireRelease && 4792 Kind == OMPC_capture)) { 4793 AO = DefaultOrder; 4794 } else if (DefaultOrder == llvm::AtomicOrdering::AcquireRelease) { 4795 if (Kind == OMPC_unknown || Kind == OMPC_update || Kind == OMPC_write) { 4796 AO = llvm::AtomicOrdering::Release; 4797 } else if (Kind == OMPC_read) { 4798 assert(Kind == OMPC_read && "Unexpected atomic kind."); 4799 AO = llvm::AtomicOrdering::Acquire; 4800 } 4801 } 4802 } 4803 4804 const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); 4805 if (const auto *FE = dyn_cast<FullExpr>(CS)) 4806 enterFullExpression(FE); 4807 // Processing for statements under 'atomic capture'. 4808 if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { 4809 for (const Stmt *C : Compound->body()) { 4810 if (const auto *FE = dyn_cast<FullExpr>(C)) 4811 enterFullExpression(FE); 4812 } 4813 } 4814 4815 auto &&CodeGen = [&S, Kind, AO, CS](CodeGenFunction &CGF, 4816 PrePostActionTy &) { 4817 CGF.EmitStopPoint(CS); 4818 emitOMPAtomicExpr(CGF, Kind, AO, S.isPostfixUpdate(), S.getX(), S.getV(), 4819 S.getExpr(), S.getUpdateExpr(), S.isXLHSInRHSPart(), 4820 S.getBeginLoc()); 4821 }; 4822 OMPLexicalScope Scope(*this, S, OMPD_unknown); 4823 CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); 4824 } 4825 4826 static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, 4827 const OMPExecutableDirective &S, 4828 const RegionCodeGenTy &CodeGen) { 4829 assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); 4830 CodeGenModule &CGM = CGF.CGM; 4831 4832 // On device emit this construct as inlined code. 4833 if (CGM.getLangOpts().OpenMPIsDevice) { 4834 OMPLexicalScope Scope(CGF, S, OMPD_target); 4835 CGM.getOpenMPRuntime().emitInlinedDirective( 4836 CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { 4837 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 4838 }); 4839 return; 4840 } 4841 4842 auto LPCRegion = 4843 CGOpenMPRuntime::LastprivateConditionalRAII::disable(CGF, S); 4844 llvm::Function *Fn = nullptr; 4845 llvm::Constant *FnID = nullptr; 4846 4847 const Expr *IfCond = nullptr; 4848 // Check for the at most one if clause associated with the target region. 4849 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 4850 if (C->getNameModifier() == OMPD_unknown || 4851 C->getNameModifier() == OMPD_target) { 4852 IfCond = C->getCondition(); 4853 break; 4854 } 4855 } 4856 4857 // Check if we have any device clause associated with the directive. 4858 llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device( 4859 nullptr, OMPC_DEVICE_unknown); 4860 if (auto *C = S.getSingleClause<OMPDeviceClause>()) 4861 Device.setPointerAndInt(C->getDevice(), C->getModifier()); 4862 4863 // Check if we have an if clause whose conditional always evaluates to false 4864 // or if we do not have any targets specified. If so the target region is not 4865 // an offload entry point. 4866 bool IsOffloadEntry = true; 4867 if (IfCond) { 4868 bool Val; 4869 if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) 4870 IsOffloadEntry = false; 4871 } 4872 if (CGM.getLangOpts().OMPTargetTriples.empty()) 4873 IsOffloadEntry = false; 4874 4875 assert(CGF.CurFuncDecl && "No parent declaration for target region!"); 4876 StringRef ParentName; 4877 // In case we have Ctors/Dtors we use the complete type variant to produce 4878 // the mangling of the device outlined kernel. 4879 if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) 4880 ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); 4881 else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) 4882 ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); 4883 else 4884 ParentName = 4885 CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); 4886 4887 // Emit target region as a standalone region. 4888 CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, 4889 IsOffloadEntry, CodeGen); 4890 OMPLexicalScope Scope(CGF, S, OMPD_task); 4891 auto &&SizeEmitter = 4892 [IsOffloadEntry](CodeGenFunction &CGF, 4893 const OMPLoopDirective &D) -> llvm::Value * { 4894 if (IsOffloadEntry) { 4895 OMPLoopScope(CGF, D); 4896 // Emit calculation of the iterations count. 4897 llvm::Value *NumIterations = CGF.EmitScalarExpr(D.getNumIterations()); 4898 NumIterations = CGF.Builder.CreateIntCast(NumIterations, CGF.Int64Ty, 4899 /*isSigned=*/false); 4900 return NumIterations; 4901 } 4902 return nullptr; 4903 }; 4904 CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device, 4905 SizeEmitter); 4906 } 4907 4908 static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, 4909 PrePostActionTy &Action) { 4910 Action.Enter(CGF); 4911 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 4912 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 4913 CGF.EmitOMPPrivateClause(S, PrivateScope); 4914 (void)PrivateScope.Privatize(); 4915 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 4916 CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); 4917 4918 CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); 4919 } 4920 4921 void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, 4922 StringRef ParentName, 4923 const OMPTargetDirective &S) { 4924 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 4925 emitTargetRegion(CGF, S, Action); 4926 }; 4927 llvm::Function *Fn; 4928 llvm::Constant *Addr; 4929 // Emit target region as a standalone region. 4930 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 4931 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 4932 assert(Fn && Addr && "Target device function emission failed."); 4933 } 4934 4935 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { 4936 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 4937 emitTargetRegion(CGF, S, Action); 4938 }; 4939 emitCommonOMPTargetDirective(*this, S, CodeGen); 4940 } 4941 4942 static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, 4943 const OMPExecutableDirective &S, 4944 OpenMPDirectiveKind InnermostKind, 4945 const RegionCodeGenTy &CodeGen) { 4946 const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); 4947 llvm::Function *OutlinedFn = 4948 CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( 4949 S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); 4950 4951 const auto *NT = S.getSingleClause<OMPNumTeamsClause>(); 4952 const auto *TL = S.getSingleClause<OMPThreadLimitClause>(); 4953 if (NT || TL) { 4954 const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr; 4955 const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr; 4956 4957 CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, 4958 S.getBeginLoc()); 4959 } 4960 4961 OMPTeamsScope Scope(CGF, S); 4962 llvm::SmallVector<llvm::Value *, 16> CapturedVars; 4963 CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); 4964 CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn, 4965 CapturedVars); 4966 } 4967 4968 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { 4969 // Emit teams region as a standalone region. 4970 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 4971 Action.Enter(CGF); 4972 OMPPrivateScope PrivateScope(CGF); 4973 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 4974 CGF.EmitOMPPrivateClause(S, PrivateScope); 4975 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 4976 (void)PrivateScope.Privatize(); 4977 CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); 4978 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 4979 }; 4980 emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); 4981 emitPostUpdateForReductionClause(*this, S, 4982 [](CodeGenFunction &) { return nullptr; }); 4983 } 4984 4985 static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, 4986 const OMPTargetTeamsDirective &S) { 4987 auto *CS = S.getCapturedStmt(OMPD_teams); 4988 Action.Enter(CGF); 4989 // Emit teams region as a standalone region. 4990 auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { 4991 Action.Enter(CGF); 4992 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 4993 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 4994 CGF.EmitOMPPrivateClause(S, PrivateScope); 4995 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 4996 (void)PrivateScope.Privatize(); 4997 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 4998 CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); 4999 CGF.EmitStmt(CS->getCapturedStmt()); 5000 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5001 }; 5002 emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); 5003 emitPostUpdateForReductionClause(CGF, S, 5004 [](CodeGenFunction &) { return nullptr; }); 5005 } 5006 5007 void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( 5008 CodeGenModule &CGM, StringRef ParentName, 5009 const OMPTargetTeamsDirective &S) { 5010 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5011 emitTargetTeamsRegion(CGF, Action, S); 5012 }; 5013 llvm::Function *Fn; 5014 llvm::Constant *Addr; 5015 // Emit target region as a standalone region. 5016 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5017 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5018 assert(Fn && Addr && "Target device function emission failed."); 5019 } 5020 5021 void CodeGenFunction::EmitOMPTargetTeamsDirective( 5022 const OMPTargetTeamsDirective &S) { 5023 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5024 emitTargetTeamsRegion(CGF, Action, S); 5025 }; 5026 emitCommonOMPTargetDirective(*this, S, CodeGen); 5027 } 5028 5029 static void 5030 emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, 5031 const OMPTargetTeamsDistributeDirective &S) { 5032 Action.Enter(CGF); 5033 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5034 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 5035 }; 5036 5037 // Emit teams region as a standalone region. 5038 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5039 PrePostActionTy &Action) { 5040 Action.Enter(CGF); 5041 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 5042 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5043 (void)PrivateScope.Privatize(); 5044 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, 5045 CodeGenDistribute); 5046 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5047 }; 5048 emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); 5049 emitPostUpdateForReductionClause(CGF, S, 5050 [](CodeGenFunction &) { return nullptr; }); 5051 } 5052 5053 void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( 5054 CodeGenModule &CGM, StringRef ParentName, 5055 const OMPTargetTeamsDistributeDirective &S) { 5056 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5057 emitTargetTeamsDistributeRegion(CGF, Action, S); 5058 }; 5059 llvm::Function *Fn; 5060 llvm::Constant *Addr; 5061 // Emit target region as a standalone region. 5062 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5063 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5064 assert(Fn && Addr && "Target device function emission failed."); 5065 } 5066 5067 void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( 5068 const OMPTargetTeamsDistributeDirective &S) { 5069 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5070 emitTargetTeamsDistributeRegion(CGF, Action, S); 5071 }; 5072 emitCommonOMPTargetDirective(*this, S, CodeGen); 5073 } 5074 5075 static void emitTargetTeamsDistributeSimdRegion( 5076 CodeGenFunction &CGF, PrePostActionTy &Action, 5077 const OMPTargetTeamsDistributeSimdDirective &S) { 5078 Action.Enter(CGF); 5079 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5080 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 5081 }; 5082 5083 // Emit teams region as a standalone region. 5084 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5085 PrePostActionTy &Action) { 5086 Action.Enter(CGF); 5087 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 5088 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5089 (void)PrivateScope.Privatize(); 5090 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, 5091 CodeGenDistribute); 5092 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5093 }; 5094 emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); 5095 emitPostUpdateForReductionClause(CGF, S, 5096 [](CodeGenFunction &) { return nullptr; }); 5097 } 5098 5099 void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( 5100 CodeGenModule &CGM, StringRef ParentName, 5101 const OMPTargetTeamsDistributeSimdDirective &S) { 5102 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5103 emitTargetTeamsDistributeSimdRegion(CGF, Action, S); 5104 }; 5105 llvm::Function *Fn; 5106 llvm::Constant *Addr; 5107 // Emit target region as a standalone region. 5108 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5109 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5110 assert(Fn && Addr && "Target device function emission failed."); 5111 } 5112 5113 void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( 5114 const OMPTargetTeamsDistributeSimdDirective &S) { 5115 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5116 emitTargetTeamsDistributeSimdRegion(CGF, Action, S); 5117 }; 5118 emitCommonOMPTargetDirective(*this, S, CodeGen); 5119 } 5120 5121 void CodeGenFunction::EmitOMPTeamsDistributeDirective( 5122 const OMPTeamsDistributeDirective &S) { 5123 5124 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5125 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 5126 }; 5127 5128 // Emit teams region as a standalone region. 5129 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5130 PrePostActionTy &Action) { 5131 Action.Enter(CGF); 5132 OMPPrivateScope PrivateScope(CGF); 5133 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5134 (void)PrivateScope.Privatize(); 5135 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, 5136 CodeGenDistribute); 5137 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5138 }; 5139 emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); 5140 emitPostUpdateForReductionClause(*this, S, 5141 [](CodeGenFunction &) { return nullptr; }); 5142 } 5143 5144 void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( 5145 const OMPTeamsDistributeSimdDirective &S) { 5146 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5147 CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); 5148 }; 5149 5150 // Emit teams region as a standalone region. 5151 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5152 PrePostActionTy &Action) { 5153 Action.Enter(CGF); 5154 OMPPrivateScope PrivateScope(CGF); 5155 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5156 (void)PrivateScope.Privatize(); 5157 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, 5158 CodeGenDistribute); 5159 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5160 }; 5161 emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); 5162 emitPostUpdateForReductionClause(*this, S, 5163 [](CodeGenFunction &) { return nullptr; }); 5164 } 5165 5166 void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( 5167 const OMPTeamsDistributeParallelForDirective &S) { 5168 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5169 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 5170 S.getDistInc()); 5171 }; 5172 5173 // Emit teams region as a standalone region. 5174 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5175 PrePostActionTy &Action) { 5176 Action.Enter(CGF); 5177 OMPPrivateScope PrivateScope(CGF); 5178 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5179 (void)PrivateScope.Privatize(); 5180 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, 5181 CodeGenDistribute); 5182 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5183 }; 5184 emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); 5185 emitPostUpdateForReductionClause(*this, S, 5186 [](CodeGenFunction &) { return nullptr; }); 5187 } 5188 5189 void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( 5190 const OMPTeamsDistributeParallelForSimdDirective &S) { 5191 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5192 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 5193 S.getDistInc()); 5194 }; 5195 5196 // Emit teams region as a standalone region. 5197 auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5198 PrePostActionTy &Action) { 5199 Action.Enter(CGF); 5200 OMPPrivateScope PrivateScope(CGF); 5201 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5202 (void)PrivateScope.Privatize(); 5203 CGF.CGM.getOpenMPRuntime().emitInlinedDirective( 5204 CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); 5205 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5206 }; 5207 emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for_simd, 5208 CodeGen); 5209 emitPostUpdateForReductionClause(*this, S, 5210 [](CodeGenFunction &) { return nullptr; }); 5211 } 5212 5213 static void emitTargetTeamsDistributeParallelForRegion( 5214 CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, 5215 PrePostActionTy &Action) { 5216 Action.Enter(CGF); 5217 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5218 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 5219 S.getDistInc()); 5220 }; 5221 5222 // Emit teams region as a standalone region. 5223 auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5224 PrePostActionTy &Action) { 5225 Action.Enter(CGF); 5226 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 5227 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5228 (void)PrivateScope.Privatize(); 5229 CGF.CGM.getOpenMPRuntime().emitInlinedDirective( 5230 CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); 5231 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5232 }; 5233 5234 emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, 5235 CodeGenTeams); 5236 emitPostUpdateForReductionClause(CGF, S, 5237 [](CodeGenFunction &) { return nullptr; }); 5238 } 5239 5240 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( 5241 CodeGenModule &CGM, StringRef ParentName, 5242 const OMPTargetTeamsDistributeParallelForDirective &S) { 5243 // Emit SPMD target teams distribute parallel for region as a standalone 5244 // region. 5245 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5246 emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); 5247 }; 5248 llvm::Function *Fn; 5249 llvm::Constant *Addr; 5250 // Emit target region as a standalone region. 5251 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5252 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5253 assert(Fn && Addr && "Target device function emission failed."); 5254 } 5255 5256 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( 5257 const OMPTargetTeamsDistributeParallelForDirective &S) { 5258 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5259 emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); 5260 }; 5261 emitCommonOMPTargetDirective(*this, S, CodeGen); 5262 } 5263 5264 static void emitTargetTeamsDistributeParallelForSimdRegion( 5265 CodeGenFunction &CGF, 5266 const OMPTargetTeamsDistributeParallelForSimdDirective &S, 5267 PrePostActionTy &Action) { 5268 Action.Enter(CGF); 5269 auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5270 CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, 5271 S.getDistInc()); 5272 }; 5273 5274 // Emit teams region as a standalone region. 5275 auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, 5276 PrePostActionTy &Action) { 5277 Action.Enter(CGF); 5278 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 5279 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5280 (void)PrivateScope.Privatize(); 5281 CGF.CGM.getOpenMPRuntime().emitInlinedDirective( 5282 CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); 5283 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); 5284 }; 5285 5286 emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, 5287 CodeGenTeams); 5288 emitPostUpdateForReductionClause(CGF, S, 5289 [](CodeGenFunction &) { return nullptr; }); 5290 } 5291 5292 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( 5293 CodeGenModule &CGM, StringRef ParentName, 5294 const OMPTargetTeamsDistributeParallelForSimdDirective &S) { 5295 // Emit SPMD target teams distribute parallel for simd region as a standalone 5296 // region. 5297 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5298 emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); 5299 }; 5300 llvm::Function *Fn; 5301 llvm::Constant *Addr; 5302 // Emit target region as a standalone region. 5303 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5304 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5305 assert(Fn && Addr && "Target device function emission failed."); 5306 } 5307 5308 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( 5309 const OMPTargetTeamsDistributeParallelForSimdDirective &S) { 5310 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5311 emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); 5312 }; 5313 emitCommonOMPTargetDirective(*this, S, CodeGen); 5314 } 5315 5316 void CodeGenFunction::EmitOMPCancellationPointDirective( 5317 const OMPCancellationPointDirective &S) { 5318 CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(), 5319 S.getCancelRegion()); 5320 } 5321 5322 void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { 5323 const Expr *IfCond = nullptr; 5324 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 5325 if (C->getNameModifier() == OMPD_unknown || 5326 C->getNameModifier() == OMPD_cancel) { 5327 IfCond = C->getCondition(); 5328 break; 5329 } 5330 } 5331 if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { 5332 // TODO: This check is necessary as we only generate `omp parallel` through 5333 // the OpenMPIRBuilder for now. 5334 if (S.getCancelRegion() == OMPD_parallel) { 5335 llvm::Value *IfCondition = nullptr; 5336 if (IfCond) 5337 IfCondition = EmitScalarExpr(IfCond, 5338 /*IgnoreResultAssign=*/true); 5339 return Builder.restoreIP( 5340 OMPBuilder->CreateCancel(Builder, IfCondition, S.getCancelRegion())); 5341 } 5342 } 5343 5344 CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond, 5345 S.getCancelRegion()); 5346 } 5347 5348 CodeGenFunction::JumpDest 5349 CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { 5350 if (Kind == OMPD_parallel || Kind == OMPD_task || 5351 Kind == OMPD_target_parallel || Kind == OMPD_taskloop || 5352 Kind == OMPD_master_taskloop || Kind == OMPD_parallel_master_taskloop) 5353 return ReturnBlock; 5354 assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || 5355 Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || 5356 Kind == OMPD_distribute_parallel_for || 5357 Kind == OMPD_target_parallel_for || 5358 Kind == OMPD_teams_distribute_parallel_for || 5359 Kind == OMPD_target_teams_distribute_parallel_for); 5360 return OMPCancelStack.getExitBlock(); 5361 } 5362 5363 void CodeGenFunction::EmitOMPUseDevicePtrClause( 5364 const OMPClause &NC, OMPPrivateScope &PrivateScope, 5365 const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { 5366 const auto &C = cast<OMPUseDevicePtrClause>(NC); 5367 auto OrigVarIt = C.varlist_begin(); 5368 auto InitIt = C.inits().begin(); 5369 for (const Expr *PvtVarIt : C.private_copies()) { 5370 const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); 5371 const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); 5372 const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); 5373 5374 // In order to identify the right initializer we need to match the 5375 // declaration used by the mapping logic. In some cases we may get 5376 // OMPCapturedExprDecl that refers to the original declaration. 5377 const ValueDecl *MatchingVD = OrigVD; 5378 if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { 5379 // OMPCapturedExprDecl are used to privative fields of the current 5380 // structure. 5381 const auto *ME = cast<MemberExpr>(OED->getInit()); 5382 assert(isa<CXXThisExpr>(ME->getBase()) && 5383 "Base should be the current struct!"); 5384 MatchingVD = ME->getMemberDecl(); 5385 } 5386 5387 // If we don't have information about the current list item, move on to 5388 // the next one. 5389 auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); 5390 if (InitAddrIt == CaptureDeviceAddrMap.end()) 5391 continue; 5392 5393 bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD, 5394 InitAddrIt, InitVD, 5395 PvtVD]() { 5396 // Initialize the temporary initialization variable with the address we 5397 // get from the runtime library. We have to cast the source address 5398 // because it is always a void *. References are materialized in the 5399 // privatization scope, so the initialization here disregards the fact 5400 // the original variable is a reference. 5401 QualType AddrQTy = 5402 getContext().getPointerType(OrigVD->getType().getNonReferenceType()); 5403 llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); 5404 Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); 5405 setAddrOfLocalVar(InitVD, InitAddr); 5406 5407 // Emit private declaration, it will be initialized by the value we 5408 // declaration we just added to the local declarations map. 5409 EmitDecl(*PvtVD); 5410 5411 // The initialization variables reached its purpose in the emission 5412 // of the previous declaration, so we don't need it anymore. 5413 LocalDeclMap.erase(InitVD); 5414 5415 // Return the address of the private variable. 5416 return GetAddrOfLocalVar(PvtVD); 5417 }); 5418 assert(IsRegistered && "firstprivate var already registered as private"); 5419 // Silence the warning about unused variable. 5420 (void)IsRegistered; 5421 5422 ++OrigVarIt; 5423 ++InitIt; 5424 } 5425 } 5426 5427 // Generate the instructions for '#pragma omp target data' directive. 5428 void CodeGenFunction::EmitOMPTargetDataDirective( 5429 const OMPTargetDataDirective &S) { 5430 CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); 5431 5432 // Create a pre/post action to signal the privatization of the device pointer. 5433 // This action can be replaced by the OpenMP runtime code generation to 5434 // deactivate privatization. 5435 bool PrivatizeDevicePointers = false; 5436 class DevicePointerPrivActionTy : public PrePostActionTy { 5437 bool &PrivatizeDevicePointers; 5438 5439 public: 5440 explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) 5441 : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} 5442 void Enter(CodeGenFunction &CGF) override { 5443 PrivatizeDevicePointers = true; 5444 } 5445 }; 5446 DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); 5447 5448 auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( 5449 CodeGenFunction &CGF, PrePostActionTy &Action) { 5450 auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5451 CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); 5452 }; 5453 5454 // Codegen that selects whether to generate the privatization code or not. 5455 auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, 5456 &InnermostCodeGen](CodeGenFunction &CGF, 5457 PrePostActionTy &Action) { 5458 RegionCodeGenTy RCG(InnermostCodeGen); 5459 PrivatizeDevicePointers = false; 5460 5461 // Call the pre-action to change the status of PrivatizeDevicePointers if 5462 // needed. 5463 Action.Enter(CGF); 5464 5465 if (PrivatizeDevicePointers) { 5466 OMPPrivateScope PrivateScope(CGF); 5467 // Emit all instances of the use_device_ptr clause. 5468 for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) 5469 CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, 5470 Info.CaptureDeviceAddrMap); 5471 (void)PrivateScope.Privatize(); 5472 RCG(CGF); 5473 } else { 5474 RCG(CGF); 5475 } 5476 }; 5477 5478 // Forward the provided action to the privatization codegen. 5479 RegionCodeGenTy PrivRCG(PrivCodeGen); 5480 PrivRCG.setAction(Action); 5481 5482 // Notwithstanding the body of the region is emitted as inlined directive, 5483 // we don't use an inline scope as changes in the references inside the 5484 // region are expected to be visible outside, so we do not privative them. 5485 OMPLexicalScope Scope(CGF, S); 5486 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, 5487 PrivRCG); 5488 }; 5489 5490 RegionCodeGenTy RCG(CodeGen); 5491 5492 // If we don't have target devices, don't bother emitting the data mapping 5493 // code. 5494 if (CGM.getLangOpts().OMPTargetTriples.empty()) { 5495 RCG(*this); 5496 return; 5497 } 5498 5499 // Check if we have any if clause associated with the directive. 5500 const Expr *IfCond = nullptr; 5501 if (const auto *C = S.getSingleClause<OMPIfClause>()) 5502 IfCond = C->getCondition(); 5503 5504 // Check if we have any device clause associated with the directive. 5505 const Expr *Device = nullptr; 5506 if (const auto *C = S.getSingleClause<OMPDeviceClause>()) 5507 Device = C->getDevice(); 5508 5509 // Set the action to signal privatization of device pointers. 5510 RCG.setAction(PrivAction); 5511 5512 // Emit region code. 5513 CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, 5514 Info); 5515 } 5516 5517 void CodeGenFunction::EmitOMPTargetEnterDataDirective( 5518 const OMPTargetEnterDataDirective &S) { 5519 // If we don't have target devices, don't bother emitting the data mapping 5520 // code. 5521 if (CGM.getLangOpts().OMPTargetTriples.empty()) 5522 return; 5523 5524 // Check if we have any if clause associated with the directive. 5525 const Expr *IfCond = nullptr; 5526 if (const auto *C = S.getSingleClause<OMPIfClause>()) 5527 IfCond = C->getCondition(); 5528 5529 // Check if we have any device clause associated with the directive. 5530 const Expr *Device = nullptr; 5531 if (const auto *C = S.getSingleClause<OMPDeviceClause>()) 5532 Device = C->getDevice(); 5533 5534 OMPLexicalScope Scope(*this, S, OMPD_task); 5535 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); 5536 } 5537 5538 void CodeGenFunction::EmitOMPTargetExitDataDirective( 5539 const OMPTargetExitDataDirective &S) { 5540 // If we don't have target devices, don't bother emitting the data mapping 5541 // code. 5542 if (CGM.getLangOpts().OMPTargetTriples.empty()) 5543 return; 5544 5545 // Check if we have any if clause associated with the directive. 5546 const Expr *IfCond = nullptr; 5547 if (const auto *C = S.getSingleClause<OMPIfClause>()) 5548 IfCond = C->getCondition(); 5549 5550 // Check if we have any device clause associated with the directive. 5551 const Expr *Device = nullptr; 5552 if (const auto *C = S.getSingleClause<OMPDeviceClause>()) 5553 Device = C->getDevice(); 5554 5555 OMPLexicalScope Scope(*this, S, OMPD_task); 5556 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); 5557 } 5558 5559 static void emitTargetParallelRegion(CodeGenFunction &CGF, 5560 const OMPTargetParallelDirective &S, 5561 PrePostActionTy &Action) { 5562 // Get the captured statement associated with the 'parallel' region. 5563 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); 5564 Action.Enter(CGF); 5565 auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { 5566 Action.Enter(CGF); 5567 CodeGenFunction::OMPPrivateScope PrivateScope(CGF); 5568 (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); 5569 CGF.EmitOMPPrivateClause(S, PrivateScope); 5570 CGF.EmitOMPReductionClauseInit(S, PrivateScope); 5571 (void)PrivateScope.Privatize(); 5572 if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) 5573 CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); 5574 // TODO: Add support for clauses. 5575 CGF.EmitStmt(CS->getCapturedStmt()); 5576 CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); 5577 }; 5578 emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, 5579 emitEmptyBoundParameters); 5580 emitPostUpdateForReductionClause(CGF, S, 5581 [](CodeGenFunction &) { return nullptr; }); 5582 } 5583 5584 void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( 5585 CodeGenModule &CGM, StringRef ParentName, 5586 const OMPTargetParallelDirective &S) { 5587 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5588 emitTargetParallelRegion(CGF, S, Action); 5589 }; 5590 llvm::Function *Fn; 5591 llvm::Constant *Addr; 5592 // Emit target region as a standalone region. 5593 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5594 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5595 assert(Fn && Addr && "Target device function emission failed."); 5596 } 5597 5598 void CodeGenFunction::EmitOMPTargetParallelDirective( 5599 const OMPTargetParallelDirective &S) { 5600 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5601 emitTargetParallelRegion(CGF, S, Action); 5602 }; 5603 emitCommonOMPTargetDirective(*this, S, CodeGen); 5604 } 5605 5606 static void emitTargetParallelForRegion(CodeGenFunction &CGF, 5607 const OMPTargetParallelForDirective &S, 5608 PrePostActionTy &Action) { 5609 Action.Enter(CGF); 5610 // Emit directive as a combined directive that consists of two implicit 5611 // directives: 'parallel' with 'for' directive. 5612 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5613 Action.Enter(CGF); 5614 CodeGenFunction::OMPCancelStackRAII CancelRegion( 5615 CGF, OMPD_target_parallel_for, S.hasCancel()); 5616 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, 5617 emitDispatchForLoopBounds); 5618 }; 5619 emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, 5620 emitEmptyBoundParameters); 5621 } 5622 5623 void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( 5624 CodeGenModule &CGM, StringRef ParentName, 5625 const OMPTargetParallelForDirective &S) { 5626 // Emit SPMD target parallel for region as a standalone region. 5627 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5628 emitTargetParallelForRegion(CGF, S, Action); 5629 }; 5630 llvm::Function *Fn; 5631 llvm::Constant *Addr; 5632 // Emit target region as a standalone region. 5633 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5634 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5635 assert(Fn && Addr && "Target device function emission failed."); 5636 } 5637 5638 void CodeGenFunction::EmitOMPTargetParallelForDirective( 5639 const OMPTargetParallelForDirective &S) { 5640 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5641 emitTargetParallelForRegion(CGF, S, Action); 5642 }; 5643 emitCommonOMPTargetDirective(*this, S, CodeGen); 5644 } 5645 5646 static void 5647 emitTargetParallelForSimdRegion(CodeGenFunction &CGF, 5648 const OMPTargetParallelForSimdDirective &S, 5649 PrePostActionTy &Action) { 5650 Action.Enter(CGF); 5651 // Emit directive as a combined directive that consists of two implicit 5652 // directives: 'parallel' with 'for' directive. 5653 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5654 Action.Enter(CGF); 5655 CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, 5656 emitDispatchForLoopBounds); 5657 }; 5658 emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, 5659 emitEmptyBoundParameters); 5660 } 5661 5662 void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( 5663 CodeGenModule &CGM, StringRef ParentName, 5664 const OMPTargetParallelForSimdDirective &S) { 5665 // Emit SPMD target parallel for region as a standalone region. 5666 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5667 emitTargetParallelForSimdRegion(CGF, S, Action); 5668 }; 5669 llvm::Function *Fn; 5670 llvm::Constant *Addr; 5671 // Emit target region as a standalone region. 5672 CGM.getOpenMPRuntime().emitTargetOutlinedFunction( 5673 S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); 5674 assert(Fn && Addr && "Target device function emission failed."); 5675 } 5676 5677 void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( 5678 const OMPTargetParallelForSimdDirective &S) { 5679 auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5680 emitTargetParallelForSimdRegion(CGF, S, Action); 5681 }; 5682 emitCommonOMPTargetDirective(*this, S, CodeGen); 5683 } 5684 5685 /// Emit a helper variable and return corresponding lvalue. 5686 static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, 5687 const ImplicitParamDecl *PVD, 5688 CodeGenFunction::OMPPrivateScope &Privates) { 5689 const auto *VDecl = cast<VarDecl>(Helper->getDecl()); 5690 Privates.addPrivate(VDecl, 5691 [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); }); 5692 } 5693 5694 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { 5695 assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); 5696 // Emit outlined function for task construct. 5697 const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); 5698 Address CapturedStruct = Address::invalid(); 5699 { 5700 OMPLexicalScope Scope(*this, S, OMPD_taskloop, /*EmitPreInitStmt=*/false); 5701 CapturedStruct = GenerateCapturedStmtArgument(*CS); 5702 } 5703 QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); 5704 const Expr *IfCond = nullptr; 5705 for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { 5706 if (C->getNameModifier() == OMPD_unknown || 5707 C->getNameModifier() == OMPD_taskloop) { 5708 IfCond = C->getCondition(); 5709 break; 5710 } 5711 } 5712 5713 OMPTaskDataTy Data; 5714 // Check if taskloop must be emitted without taskgroup. 5715 Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); 5716 // TODO: Check if we should emit tied or untied task. 5717 Data.Tied = true; 5718 // Set scheduling for taskloop 5719 if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { 5720 // grainsize clause 5721 Data.Schedule.setInt(/*IntVal=*/false); 5722 Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); 5723 } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { 5724 // num_tasks clause 5725 Data.Schedule.setInt(/*IntVal=*/true); 5726 Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); 5727 } 5728 5729 auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { 5730 // if (PreCond) { 5731 // for (IV in 0..LastIteration) BODY; 5732 // <Final counter/linear vars updates>; 5733 // } 5734 // 5735 5736 // Emit: if (PreCond) - begin. 5737 // If the condition constant folds and can be elided, avoid emitting the 5738 // whole loop. 5739 bool CondConstant; 5740 llvm::BasicBlock *ContBlock = nullptr; 5741 OMPLoopScope PreInitScope(CGF, S); 5742 if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { 5743 if (!CondConstant) 5744 return; 5745 } else { 5746 llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); 5747 ContBlock = CGF.createBasicBlock("taskloop.if.end"); 5748 emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, 5749 CGF.getProfileCount(&S)); 5750 CGF.EmitBlock(ThenBlock); 5751 CGF.incrementProfileCounter(&S); 5752 } 5753 5754 (void)CGF.EmitOMPLinearClauseInit(S); 5755 5756 OMPPrivateScope LoopScope(CGF); 5757 // Emit helper vars inits. 5758 enum { LowerBound = 5, UpperBound, Stride, LastIter }; 5759 auto *I = CS->getCapturedDecl()->param_begin(); 5760 auto *LBP = std::next(I, LowerBound); 5761 auto *UBP = std::next(I, UpperBound); 5762 auto *STP = std::next(I, Stride); 5763 auto *LIP = std::next(I, LastIter); 5764 mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, 5765 LoopScope); 5766 mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, 5767 LoopScope); 5768 mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); 5769 mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, 5770 LoopScope); 5771 CGF.EmitOMPPrivateLoopCounters(S, LoopScope); 5772 CGF.EmitOMPLinearClause(S, LoopScope); 5773 bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); 5774 (void)LoopScope.Privatize(); 5775 // Emit the loop iteration variable. 5776 const Expr *IVExpr = S.getIterationVariable(); 5777 const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); 5778 CGF.EmitVarDecl(*IVDecl); 5779 CGF.EmitIgnoredExpr(S.getInit()); 5780 5781 // Emit the iterations count variable. 5782 // If it is not a variable, Sema decided to calculate iterations count on 5783 // each iteration (e.g., it is foldable into a constant). 5784 if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { 5785 CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); 5786 // Emit calculation of the iterations count. 5787 CGF.EmitIgnoredExpr(S.getCalcLastIteration()); 5788 } 5789 5790 { 5791 OMPLexicalScope Scope(CGF, S, OMPD_taskloop, /*EmitPreInitStmt=*/false); 5792 emitCommonSimdLoop( 5793 CGF, S, 5794 [&S](CodeGenFunction &CGF, PrePostActionTy &) { 5795 if (isOpenMPSimdDirective(S.getDirectiveKind())) 5796 CGF.EmitOMPSimdInit(S); 5797 }, 5798 [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { 5799 CGF.EmitOMPInnerLoop( 5800 S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), 5801 [&S](CodeGenFunction &CGF) { 5802 CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); 5803 CGF.EmitStopPoint(&S); 5804 }, 5805 [](CodeGenFunction &) {}); 5806 }); 5807 } 5808 // Emit: if (PreCond) - end. 5809 if (ContBlock) { 5810 CGF.EmitBranch(ContBlock); 5811 CGF.EmitBlock(ContBlock, true); 5812 } 5813 // Emit final copy of the lastprivate variables if IsLastIter != 0. 5814 if (HasLastprivateClause) { 5815 CGF.EmitOMPLastprivateClauseFinal( 5816 S, isOpenMPSimdDirective(S.getDirectiveKind()), 5817 CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( 5818 CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, 5819 (*LIP)->getType(), S.getBeginLoc()))); 5820 } 5821 CGF.EmitOMPLinearClauseFinal(S, [LIP, &S](CodeGenFunction &CGF) { 5822 return CGF.Builder.CreateIsNotNull( 5823 CGF.EmitLoadOfScalar(CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, 5824 (*LIP)->getType(), S.getBeginLoc())); 5825 }); 5826 }; 5827 auto &&TaskGen = [&S, SharedsTy, CapturedStruct, 5828 IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, 5829 const OMPTaskDataTy &Data) { 5830 auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond, 5831 &Data](CodeGenFunction &CGF, PrePostActionTy &) { 5832 OMPLoopScope PreInitScope(CGF, S); 5833 CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S, 5834 OutlinedFn, SharedsTy, 5835 CapturedStruct, IfCond, Data); 5836 }; 5837 CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, 5838 CodeGen); 5839 }; 5840 if (Data.Nogroup) { 5841 EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); 5842 } else { 5843 CGM.getOpenMPRuntime().emitTaskgroupRegion( 5844 *this, 5845 [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, 5846 PrePostActionTy &Action) { 5847 Action.Enter(CGF); 5848 CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, 5849 Data); 5850 }, 5851 S.getBeginLoc()); 5852 } 5853 } 5854 5855 void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { 5856 auto LPCRegion = 5857 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5858 EmitOMPTaskLoopBasedDirective(S); 5859 } 5860 5861 void CodeGenFunction::EmitOMPTaskLoopSimdDirective( 5862 const OMPTaskLoopSimdDirective &S) { 5863 auto LPCRegion = 5864 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5865 OMPLexicalScope Scope(*this, S); 5866 EmitOMPTaskLoopBasedDirective(S); 5867 } 5868 5869 void CodeGenFunction::EmitOMPMasterTaskLoopDirective( 5870 const OMPMasterTaskLoopDirective &S) { 5871 auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5872 Action.Enter(CGF); 5873 EmitOMPTaskLoopBasedDirective(S); 5874 }; 5875 auto LPCRegion = 5876 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5877 OMPLexicalScope Scope(*this, S, llvm::None, /*EmitPreInitStmt=*/false); 5878 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); 5879 } 5880 5881 void CodeGenFunction::EmitOMPMasterTaskLoopSimdDirective( 5882 const OMPMasterTaskLoopSimdDirective &S) { 5883 auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5884 Action.Enter(CGF); 5885 EmitOMPTaskLoopBasedDirective(S); 5886 }; 5887 auto LPCRegion = 5888 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5889 OMPLexicalScope Scope(*this, S); 5890 CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); 5891 } 5892 5893 void CodeGenFunction::EmitOMPParallelMasterTaskLoopDirective( 5894 const OMPParallelMasterTaskLoopDirective &S) { 5895 auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5896 auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF, 5897 PrePostActionTy &Action) { 5898 Action.Enter(CGF); 5899 CGF.EmitOMPTaskLoopBasedDirective(S); 5900 }; 5901 OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false); 5902 CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen, 5903 S.getBeginLoc()); 5904 }; 5905 auto LPCRegion = 5906 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5907 emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop, CodeGen, 5908 emitEmptyBoundParameters); 5909 } 5910 5911 void CodeGenFunction::EmitOMPParallelMasterTaskLoopSimdDirective( 5912 const OMPParallelMasterTaskLoopSimdDirective &S) { 5913 auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { 5914 auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF, 5915 PrePostActionTy &Action) { 5916 Action.Enter(CGF); 5917 CGF.EmitOMPTaskLoopBasedDirective(S); 5918 }; 5919 OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false); 5920 CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen, 5921 S.getBeginLoc()); 5922 }; 5923 auto LPCRegion = 5924 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); 5925 emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop_simd, CodeGen, 5926 emitEmptyBoundParameters); 5927 } 5928 5929 // Generate the instructions for '#pragma omp target update' directive. 5930 void CodeGenFunction::EmitOMPTargetUpdateDirective( 5931 const OMPTargetUpdateDirective &S) { 5932 // If we don't have target devices, don't bother emitting the data mapping 5933 // code. 5934 if (CGM.getLangOpts().OMPTargetTriples.empty()) 5935 return; 5936 5937 // Check if we have any if clause associated with the directive. 5938 const Expr *IfCond = nullptr; 5939 if (const auto *C = S.getSingleClause<OMPIfClause>()) 5940 IfCond = C->getCondition(); 5941 5942 // Check if we have any device clause associated with the directive. 5943 const Expr *Device = nullptr; 5944 if (const auto *C = S.getSingleClause<OMPDeviceClause>()) 5945 Device = C->getDevice(); 5946 5947 OMPLexicalScope Scope(*this, S, OMPD_task); 5948 CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); 5949 } 5950 5951 void CodeGenFunction::EmitSimpleOMPExecutableDirective( 5952 const OMPExecutableDirective &D) { 5953 if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) 5954 return; 5955 auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { 5956 OMPPrivateScope GlobalsScope(CGF); 5957 if (isOpenMPTaskingDirective(D.getDirectiveKind())) { 5958 // Capture global firstprivates to avoid crash. 5959 for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { 5960 for (const Expr *Ref : C->varlists()) { 5961 const auto *DRE = cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); 5962 if (!DRE) 5963 continue; 5964 const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); 5965 if (!VD || VD->hasLocalStorage()) 5966 continue; 5967 if (!CGF.LocalDeclMap.count(VD)) { 5968 LValue GlobLVal = CGF.EmitLValue(Ref); 5969 GlobalsScope.addPrivate( 5970 VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); }); 5971 } 5972 } 5973 } 5974 } 5975 if (isOpenMPSimdDirective(D.getDirectiveKind())) { 5976 (void)GlobalsScope.Privatize(); 5977 emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); 5978 } else { 5979 if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { 5980 for (const Expr *E : LD->counters()) { 5981 const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); 5982 if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) { 5983 LValue GlobLVal = CGF.EmitLValue(E); 5984 GlobalsScope.addPrivate( 5985 VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); }); 5986 } 5987 if (isa<OMPCapturedExprDecl>(VD)) { 5988 // Emit only those that were not explicitly referenced in clauses. 5989 if (!CGF.LocalDeclMap.count(VD)) 5990 CGF.EmitVarDecl(*VD); 5991 } 5992 } 5993 for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) { 5994 if (!C->getNumForLoops()) 5995 continue; 5996 for (unsigned I = LD->getCollapsedNumber(), 5997 E = C->getLoopNumIterations().size(); 5998 I < E; ++I) { 5999 if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( 6000 cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) { 6001 // Emit only those that were not explicitly referenced in clauses. 6002 if (!CGF.LocalDeclMap.count(VD)) 6003 CGF.EmitVarDecl(*VD); 6004 } 6005 } 6006 } 6007 } 6008 (void)GlobalsScope.Privatize(); 6009 CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); 6010 } 6011 }; 6012 { 6013 auto LPCRegion = 6014 CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, D); 6015 OMPSimdLexicalScope Scope(*this, D); 6016 CGM.getOpenMPRuntime().emitInlinedDirective( 6017 *this, 6018 isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd 6019 : D.getDirectiveKind(), 6020 CodeGen); 6021 } 6022 // Check for outer lastprivate conditional update. 6023 checkForLastprivateConditionalUpdate(*this, D); 6024 } 6025