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