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