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