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